1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
6 #define CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
8 #import <Cocoa/Cocoa.h>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
13 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 class OmniboxPopupView
;
21 // Implements OmniboxView on an AutocompleteTextField.
22 class OmniboxViewMac
: public OmniboxView
,
23 public AutocompleteTextFieldObserver
{
25 OmniboxViewMac(OmniboxEditController
* controller
,
27 CommandUpdater
* command_updater
,
28 AutocompleteTextField
* field
);
29 virtual ~OmniboxViewMac();
32 virtual void SaveStateToTab(content::WebContents
* tab
) OVERRIDE
;
33 virtual void OnTabChanged(const content::WebContents
* web_contents
) OVERRIDE
;
34 virtual void Update() OVERRIDE
;
35 virtual base::string16
GetText() const OVERRIDE
;
36 virtual void SetWindowTextAndCaretPos(const base::string16
& text
,
39 bool notify_text_changed
) OVERRIDE
;
40 virtual void SetForcedQuery() OVERRIDE
;
41 virtual bool IsSelectAll() const OVERRIDE
;
42 virtual bool DeleteAtEndPressed() OVERRIDE
;
43 virtual void GetSelectionBounds(
44 base::string16::size_type
* start
,
45 base::string16::size_type
* end
) const OVERRIDE
;
46 virtual void SelectAll(bool reversed
) OVERRIDE
;
47 virtual void RevertAll() OVERRIDE
;
48 virtual void UpdatePopup() OVERRIDE
;
49 virtual void CloseOmniboxPopup() OVERRIDE
;
50 virtual void SetFocus() OVERRIDE
;
51 virtual void ApplyCaretVisibility() OVERRIDE
;
52 virtual void OnTemporaryTextMaybeChanged(
53 const base::string16
& display_text
,
54 bool save_original_selection
,
55 bool notify_text_changed
) OVERRIDE
;
56 virtual bool OnInlineAutocompleteTextMaybeChanged(
57 const base::string16
& display_text
, size_t user_text_length
) OVERRIDE
;
58 virtual void OnInlineAutocompleteTextCleared() OVERRIDE
;
59 virtual void OnRevertTemporaryText() OVERRIDE
;
60 virtual void OnBeforePossibleChange() OVERRIDE
;
61 virtual bool OnAfterPossibleChange() OVERRIDE
;
62 virtual gfx::NativeView
GetNativeView() const OVERRIDE
;
63 virtual gfx::NativeView
GetRelativeWindowForPopup() const OVERRIDE
;
64 virtual void SetGrayTextAutocompletion(const base::string16
& input
) OVERRIDE
;
65 virtual base::string16
GetGrayTextAutocompletion() const OVERRIDE
;
66 virtual int GetTextWidth() const OVERRIDE
;
67 virtual int GetWidth() const OVERRIDE
;
68 virtual bool IsImeComposing() const OVERRIDE
;
70 // Implement the AutocompleteTextFieldObserver interface.
71 virtual NSRange
SelectionRangeForProposedRange(
72 NSRange proposed_range
) OVERRIDE
;
73 virtual void OnControlKeyChanged(bool pressed
) OVERRIDE
;
74 virtual bool CanCopy() OVERRIDE
;
75 virtual void CopyToPasteboard(NSPasteboard
* pboard
) OVERRIDE
;
76 virtual bool ShouldEnableShowURL() OVERRIDE
;
77 virtual void ShowURL() OVERRIDE
;
78 virtual void OnPaste() OVERRIDE
;
79 virtual bool CanPasteAndGo() OVERRIDE
;
80 virtual int GetPasteActionStringId() OVERRIDE
;
81 virtual void OnPasteAndGo() OVERRIDE
;
82 virtual void OnFrameChanged() OVERRIDE
;
83 virtual void ClosePopup() OVERRIDE
;
84 virtual void OnDidBeginEditing() OVERRIDE
;
85 virtual void OnBeforeChange() OVERRIDE
;
86 virtual void OnDidChange() OVERRIDE
;
87 virtual void OnDidEndEditing() OVERRIDE
;
88 virtual bool OnDoCommandBySelector(SEL cmd
) OVERRIDE
;
89 virtual void OnSetFocus(bool control_down
) OVERRIDE
;
90 virtual void OnKillFocus() OVERRIDE
;
91 virtual void OnMouseDown(NSInteger button_number
) OVERRIDE
;
92 virtual bool ShouldSelectAllOnMouseDown() OVERRIDE
;
94 // Helper for LocationBarViewMac. Optionally selects all in |field_|.
95 void FocusLocation(bool select_all
);
97 // Helper to get the font to use in the field, exposed for the
99 static NSFont
* GetFieldFont();
101 // If |resource_id| has a PDF image which can be used, return it.
102 // Otherwise return the PNG image from the resource bundle.
103 static NSImage
* ImageForResource(int resource_id
);
105 // Color used to draw suggest text.
106 static NSColor
* SuggestTextColor();
108 AutocompleteTextField
* field() const { return field_
; }
111 // Called when the user hits backspace in |field_|. Checks whether
112 // keyword search is being terminated. Returns true if the
113 // backspace should be intercepted (not forwarded on to the standard
115 bool OnBackspacePressed();
117 // Returns the field's currently selected range. Only valid if the
119 NSRange
GetSelectedRange() const;
121 // Returns the field's currently marked range. Only valid if the field has
123 NSRange
GetMarkedRange() const;
125 // Returns true if |field_| is first-responder in the window. Used
126 // in various DCHECKS to make sure code is running in appropriate
128 bool IsFirstResponder() const;
130 // If |model_| believes it has focus, grab focus if needed and set
131 // the selection to |range|. Otherwise does nothing.
132 void SetSelectedRange(const NSRange range
);
134 // Update the field with |display_text| and highlight the host and scheme (if
135 // it's an URL or URL-fragment). Resets any suggest text that may be present.
136 void SetText(const base::string16
& display_text
);
138 // Internal implementation of SetText. Does not reset the suggest text before
139 // setting the display text. Most callers should use |SetText()| instead.
140 void SetTextInternal(const base::string16
& display_text
);
142 // Update the field with |display_text| and set the selection.
143 void SetTextAndSelectedRange(const base::string16
& display_text
,
144 const NSRange range
);
146 // Pass the current content of |field_| to SetText(), maintaining
147 // any selection. Named to be consistent with GTK and Windows,
148 // though here we cannot really do the in-place operation they do.
149 virtual void EmphasizeURLComponents() OVERRIDE
;
151 // Calculates text attributes according to |display_text| and applies them
152 // to the given |as| object.
153 void ApplyTextAttributes(const base::string16
& display_text
,
154 NSMutableAttributedString
* as
);
156 // Return the number of UTF-16 units in the current buffer, excluding the
158 virtual int GetOmniboxTextLength() const OVERRIDE
;
159 NSUInteger
GetTextLength() const;
161 // Returns true if the caret is at the end of the content.
162 bool IsCaretAtEnd() const;
164 scoped_ptr
<OmniboxPopupView
> popup_view_
;
166 AutocompleteTextField
* field_
; // owned by tab controller
168 // Selection at the point where the user started using the
169 // arrows to move around in the popup.
170 NSRange saved_temporary_selection_
;
172 // Tracking state before and after a possible change for reporting
174 NSRange selection_before_change_
;
175 base::string16 text_before_change_
;
176 NSRange marked_range_before_change_
;
178 // Was delete pressed?
179 bool delete_was_pressed_
;
181 // Was the delete key pressed with an empty selection at the end of the edit?
182 bool delete_at_end_pressed_
;
184 base::string16 suggest_text_
;
186 DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac
);
189 #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_