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