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 string16 GetText() const OVERRIDE;
36 virtual void SetWindowTextAndCaretPos(const 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(string16::size_type* start,
44 string16::size_type* end) const OVERRIDE;
45 virtual void SelectAll(bool reversed) OVERRIDE;
46 virtual void RevertAll() OVERRIDE;
47 virtual void UpdatePopup() OVERRIDE;
48 virtual void CloseOmniboxPopup() OVERRIDE;
49 virtual void SetFocus() OVERRIDE;
50 virtual void ApplyCaretVisibility() OVERRIDE;
51 virtual void OnTemporaryTextMaybeChanged(
52 const string16& display_text,
53 bool save_original_selection,
54 bool notify_text_changed) OVERRIDE;
55 virtual bool OnInlineAutocompleteTextMaybeChanged(
56 const string16& display_text, size_t user_text_length) OVERRIDE;
57 virtual void OnRevertTemporaryText() OVERRIDE;
58 virtual void OnBeforePossibleChange() OVERRIDE;
59 virtual bool OnAfterPossibleChange() OVERRIDE;
60 virtual gfx::NativeView GetNativeView() const OVERRIDE;
61 virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE;
62 virtual void SetGrayTextAutocompletion(const string16& input) OVERRIDE;
63 virtual string16 GetGrayTextAutocompletion() const OVERRIDE;
64 virtual int TextWidth() const OVERRIDE;
65 virtual bool IsImeComposing() const OVERRIDE;
67 // Implement the AutocompleteTextFieldObserver interface.
68 virtual NSRange SelectionRangeForProposedRange(
69 NSRange proposed_range) OVERRIDE;
70 virtual void OnControlKeyChanged(bool pressed) OVERRIDE;
71 virtual bool CanCopy() OVERRIDE;
72 virtual void CopyToPasteboard(NSPasteboard* pboard) OVERRIDE;
73 virtual bool ShouldEnableShowURL() OVERRIDE;
74 virtual void ShowURL() OVERRIDE;
75 virtual void OnPaste() OVERRIDE;
76 virtual bool CanPasteAndGo() OVERRIDE;
77 virtual int GetPasteActionStringId() OVERRIDE;
78 virtual void OnPasteAndGo() OVERRIDE;
79 virtual void OnFrameChanged() OVERRIDE;
80 virtual void ClosePopup() OVERRIDE;
81 virtual void OnDidBeginEditing() OVERRIDE;
82 virtual void OnBeforeChange() OVERRIDE;
83 virtual void OnDidChange() OVERRIDE;
84 virtual void OnDidEndEditing() OVERRIDE;
85 virtual bool OnDoCommandBySelector(SEL cmd) OVERRIDE;
86 virtual void OnSetFocus(bool control_down) OVERRIDE;
87 virtual void OnKillFocus() OVERRIDE;
88 virtual void OnMouseDown(NSInteger button_number) OVERRIDE;
89 virtual bool ShouldSelectAllOnMouseDown() OVERRIDE;
91 // Helper for LocationBarViewMac. Optionally selects all in |field_|.
92 void FocusLocation(bool select_all);
94 // Helper to get the font to use in the field, exposed for the
96 static NSFont* GetFieldFont();
98 // If |resource_id| has a PDF image which can be used, return it.
99 // Otherwise return the PNG image from the resource bundle.
100 static NSImage* ImageForResource(int resource_id);
102 // Color used to draw suggest text.
103 static NSColor* SuggestTextColor();
105 AutocompleteTextField* field() const { return field_; }
108 // Called when the user hits backspace in |field_|. Checks whether
109 // keyword search is being terminated. Returns true if the
110 // backspace should be intercepted (not forwarded on to the standard
112 bool OnBackspacePressed();
114 // Returns the field's currently selected range. Only valid if the
116 NSRange GetSelectedRange() const;
118 // Returns the field's currently marked range. Only valid if the field has
120 NSRange GetMarkedRange() const;
122 // Returns true if |field_| is first-responder in the window. Used
123 // in various DCHECKS to make sure code is running in appropriate
125 bool IsFirstResponder() const;
127 // If |model_| believes it has focus, grab focus if needed and set
128 // the selection to |range|. Otherwise does nothing.
129 void SetSelectedRange(const NSRange range);
131 // Update the field with |display_text| and highlight the host and scheme (if
132 // it's an URL or URL-fragment). Resets any suggest text that may be present.
133 void SetText(const string16& display_text);
135 // Internal implementation of SetText. Does not reset the suggest text before
136 // setting the display text. Most callers should use |SetText()| instead.
137 void SetTextInternal(const string16& display_text);
139 // Update the field with |display_text| and set the selection.
140 void SetTextAndSelectedRange(const string16& display_text,
141 const NSRange range);
143 // Pass the current content of |field_| to SetText(), maintaining
144 // any selection. Named to be consistent with GTK and Windows,
145 // though here we cannot really do the in-place operation they do.
146 virtual void EmphasizeURLComponents() OVERRIDE;
148 // Calculates text attributes according to |display_text| and applies them
149 // to the given |as| object.
150 void ApplyTextAttributes(const string16& display_text,
151 NSMutableAttributedString* as);
153 // Return the number of UTF-16 units in the current buffer, excluding the
155 virtual int GetOmniboxTextLength() const OVERRIDE;
156 NSUInteger GetTextLength() const;
158 // Returns true if the caret is at the end of the content.
159 bool IsCaretAtEnd() const;
161 scoped_ptr<OmniboxPopupView> popup_view_;
163 AutocompleteTextField* field_; // owned by tab controller
165 // Selection at the point where the user started using the
166 // arrows to move around in the popup.
167 NSRange saved_temporary_selection_;
169 // Tracking state before and after a possible change for reporting
171 NSRange selection_before_change_;
172 string16 text_before_change_;
173 NSRange marked_range_before_change_;
175 // Was delete pressed?
176 bool delete_was_pressed_;
178 // Was the delete key pressed with an empty selection at the end of the edit?
179 bool delete_at_end_pressed_;
181 string16 suggest_text_;
183 DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac);
186 #endif // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_