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 ~OmniboxViewMac() override
;
32 void SaveStateToTab(content::WebContents
* tab
) override
;
33 void OnTabChanged(const content::WebContents
* web_contents
) override
;
34 void ResetTabState(content::WebContents
* web_contents
) override
;
35 void Update() override
;
36 void UpdatePlaceholderText() override
;
37 void OpenMatch(const AutocompleteMatch
& match
,
38 WindowOpenDisposition disposition
,
39 const GURL
& alternate_nav_url
,
40 const base::string16
& pasted_text
,
41 size_t selected_line
) override
;
42 base::string16
GetText() const override
;
43 void SetWindowTextAndCaretPos(const base::string16
& text
,
46 bool notify_text_changed
) override
;
47 void SetForcedQuery() override
;
48 bool IsSelectAll() const override
;
49 bool DeleteAtEndPressed() override
;
50 void GetSelectionBounds(base::string16::size_type
* start
,
51 base::string16::size_type
* end
) const override
;
52 void SelectAll(bool reversed
) override
;
53 void RevertAll() override
;
54 void UpdatePopup() override
;
55 void CloseOmniboxPopup() override
;
56 void SetFocus() override
;
57 void ApplyCaretVisibility() override
;
58 void OnTemporaryTextMaybeChanged(const base::string16
& display_text
,
59 bool save_original_selection
,
60 bool notify_text_changed
) override
;
61 bool OnInlineAutocompleteTextMaybeChanged(const base::string16
& display_text
,
62 size_t user_text_length
) override
;
63 void OnInlineAutocompleteTextCleared() override
;
64 void OnRevertTemporaryText() override
;
65 void OnBeforePossibleChange() override
;
66 bool OnAfterPossibleChange() override
;
67 gfx::NativeView
GetNativeView() const override
;
68 gfx::NativeView
GetRelativeWindowForPopup() const override
;
69 void SetGrayTextAutocompletion(const base::string16
& input
) override
;
70 base::string16
GetGrayTextAutocompletion() const override
;
71 int GetTextWidth() const override
;
72 int GetWidth() const override
;
73 bool IsImeComposing() const override
;
75 // Implement the AutocompleteTextFieldObserver interface.
76 NSRange
SelectionRangeForProposedRange(NSRange proposed_range
) override
;
77 void OnControlKeyChanged(bool pressed
) override
;
78 bool CanCopy() override
;
79 void CopyToPasteboard(NSPasteboard
* pboard
) override
;
80 bool ShouldEnableShowURL() override
;
81 void ShowURL() override
;
82 void OnPaste() override
;
83 bool CanPasteAndGo() override
;
84 int GetPasteActionStringId() override
;
85 void OnPasteAndGo() override
;
86 void OnFrameChanged() override
;
87 void ClosePopup() override
;
88 void OnDidBeginEditing() override
;
89 void OnBeforeChange() override
;
90 void OnDidChange() override
;
91 void OnDidEndEditing() override
;
92 bool OnDoCommandBySelector(SEL cmd
) override
;
93 void OnSetFocus(bool control_down
) override
;
94 void OnKillFocus() override
;
95 void OnMouseDown(NSInteger button_number
) override
;
96 bool ShouldSelectAllOnMouseDown() override
;
98 // Helper for LocationBarViewMac. Optionally selects all in |field_|.
99 void FocusLocation(bool select_all
);
101 // Helper to get the font to use in the field, exposed for the
103 // The style parameter specifies the new style for the font, and is a
104 // bitmask of the values: BOLD, ITALIC and UNDERLINE (see ui/gfx/font.h).
105 static NSFont
* GetFieldFont(int style
);
107 // If |resource_id| has a PDF image which can be used, return it.
108 // Otherwise return the PNG image from the resource bundle.
109 static NSImage
* ImageForResource(int resource_id
);
111 // Color used to draw suggest text.
112 static NSColor
* SuggestTextColor();
114 AutocompleteTextField
* field() const { return field_
; }
117 // Called when the user hits backspace in |field_|. Checks whether
118 // keyword search is being terminated. Returns true if the
119 // backspace should be intercepted (not forwarded on to the standard
121 bool OnBackspacePressed();
123 // Returns the field's currently selected range. Only valid if the
125 NSRange
GetSelectedRange() const;
127 // Returns the field's currently marked range. Only valid if the field has
129 NSRange
GetMarkedRange() const;
131 // Returns true if |field_| is first-responder in the window. Used
132 // in various DCHECKS to make sure code is running in appropriate
134 bool IsFirstResponder() const;
136 // If |model_| believes it has focus, grab focus if needed and set
137 // the selection to |range|. Otherwise does nothing.
138 void SetSelectedRange(const NSRange range
);
140 // Update the field with |display_text| and highlight the host and scheme (if
141 // it's an URL or URL-fragment). Resets any suggest text that may be present.
142 void SetText(const base::string16
& display_text
);
144 // Internal implementation of SetText. Does not reset the suggest text before
145 // setting the display text. Most callers should use |SetText()| instead.
146 void SetTextInternal(const base::string16
& display_text
);
148 // Update the field with |display_text| and set the selection.
149 void SetTextAndSelectedRange(const base::string16
& display_text
,
150 const NSRange range
);
152 // Pass the current content of |field_| to SetText(), maintaining
153 // any selection. Named to be consistent with GTK and Windows,
154 // though here we cannot really do the in-place operation they do.
155 void EmphasizeURLComponents() override
;
157 // Apply our font and paragraph style to |as|.
158 void ApplyTextStyle(NSMutableAttributedString
* as
);
160 // Calculates text attributes according to |display_text| and applies them
161 // to the given |as| object.
162 void ApplyTextAttributes(const base::string16
& display_text
,
163 NSMutableAttributedString
* as
);
165 // Return the number of UTF-16 units in the current buffer, excluding the
167 int GetOmniboxTextLength() const override
;
168 NSUInteger
GetTextLength() const;
170 // Returns true if the caret is at the end of the content.
171 bool IsCaretAtEnd() const;
173 scoped_ptr
<OmniboxPopupView
> popup_view_
;
175 AutocompleteTextField
* field_
; // owned by tab controller
177 // Selection at the point where the user started using the
178 // arrows to move around in the popup.
179 NSRange saved_temporary_selection_
;
181 // Tracking state before and after a possible change for reporting
183 NSRange selection_before_change_
;
184 base::string16 text_before_change_
;
185 NSRange marked_range_before_change_
;
187 // Was delete pressed?
188 bool delete_was_pressed_
;
190 // Was the delete key pressed with an empty selection at the end of the edit?
191 bool delete_at_end_pressed_
;
193 base::string16 suggest_text_
;
195 // State used to coalesce changes to text and selection to avoid drawing
197 bool in_coalesced_update_block_
;
198 bool do_coalesced_text_update_
;
199 base::string16 coalesced_text_update_
;
200 bool do_coalesced_range_update_
;
201 NSRange coalesced_range_update_
;
203 DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac
);
206 #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_