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 // The style parameter specifies the new style for the font, and is a
100 // bitmask of the values: BOLD, ITALIC and UNDERLINE (see ui/gfx/font.h).
101 static NSFont* GetFieldFont(int style);
103 // If |resource_id| has a PDF image which can be used, return it.
104 // Otherwise return the PNG image from the resource bundle.
105 static NSImage* ImageForResource(int resource_id);
107 // Color used to draw suggest text.
108 static NSColor* SuggestTextColor();
110 AutocompleteTextField* field() const { return field_; }
113 // Called when the user hits backspace in |field_|. Checks whether
114 // keyword search is being terminated. Returns true if the
115 // backspace should be intercepted (not forwarded on to the standard
117 bool OnBackspacePressed();
119 // Returns the field's currently selected range. Only valid if the
121 NSRange GetSelectedRange() const;
123 // Returns the field's currently marked range. Only valid if the field has
125 NSRange GetMarkedRange() const;
127 // Returns true if |field_| is first-responder in the window. Used
128 // in various DCHECKS to make sure code is running in appropriate
130 bool IsFirstResponder() const;
132 // If |model_| believes it has focus, grab focus if needed and set
133 // the selection to |range|. Otherwise does nothing.
134 void SetSelectedRange(const NSRange range);
136 // Update the field with |display_text| and highlight the host and scheme (if
137 // it's an URL or URL-fragment). Resets any suggest text that may be present.
138 void SetText(const base::string16& display_text);
140 // Internal implementation of SetText. Does not reset the suggest text before
141 // setting the display text. Most callers should use |SetText()| instead.
142 void SetTextInternal(const base::string16& display_text);
144 // Update the field with |display_text| and set the selection.
145 void SetTextAndSelectedRange(const base::string16& display_text,
146 const NSRange range);
148 // Pass the current content of |field_| to SetText(), maintaining
149 // any selection. Named to be consistent with GTK and Windows,
150 // though here we cannot really do the in-place operation they do.
151 virtual void EmphasizeURLComponents() OVERRIDE;
153 // Calculates text attributes according to |display_text| and applies them
154 // to the given |as| object.
155 void ApplyTextAttributes(const base::string16& display_text,
156 NSMutableAttributedString* as);
158 // Return the number of UTF-16 units in the current buffer, excluding the
160 virtual int GetOmniboxTextLength() const OVERRIDE;
161 NSUInteger GetTextLength() const;
163 // Returns true if the caret is at the end of the content.
164 bool IsCaretAtEnd() const;
166 scoped_ptr<OmniboxPopupView> popup_view_;
168 AutocompleteTextField* field_; // owned by tab controller
170 // Selection at the point where the user started using the
171 // arrows to move around in the popup.
172 NSRange saved_temporary_selection_;
174 // Tracking state before and after a possible change for reporting
176 NSRange selection_before_change_;
177 base::string16 text_before_change_;
178 NSRange marked_range_before_change_;
180 // Was delete pressed?
181 bool delete_was_pressed_;
183 // Was the delete key pressed with an empty selection at the end of the edit?
184 bool delete_at_end_pressed_;
186 base::string16 suggest_text_;
188 DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac);
191 #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_