- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / omnibox / omnibox_view_mac.h
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.
4
5 #ifndef CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
6 #define CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_
7
8 #import <Cocoa/Cocoa.h>
9
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"
14
15 class OmniboxPopupView;
16
17 namespace ui {
18 class Clipboard;
19 }
20
21 // Implements OmniboxView on an AutocompleteTextField.
22 class OmniboxViewMac : public OmniboxView,
23                        public AutocompleteTextFieldObserver {
24  public:
25   OmniboxViewMac(OmniboxEditController* controller,
26                  Profile* profile,
27                  CommandUpdater* command_updater,
28                  AutocompleteTextField* field);
29   virtual ~OmniboxViewMac();
30
31   // OmniboxView:
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,
37                                         size_t caret_pos,
38                                         bool update_popup,
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;
66
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;
90
91   // Helper for LocationBarViewMac.  Optionally selects all in |field_|.
92   void FocusLocation(bool select_all);
93
94   // Helper to get the font to use in the field, exposed for the
95   // popup.
96   static NSFont* GetFieldFont();
97
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);
101
102   // Color used to draw suggest text.
103   static NSColor* SuggestTextColor();
104
105   AutocompleteTextField* field() const { return field_; }
106
107  private:
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
111   // machinery).
112   bool OnBackspacePressed();
113
114   // Returns the field's currently selected range.  Only valid if the
115   // field has focus.
116   NSRange GetSelectedRange() const;
117
118   // Returns the field's currently marked range. Only valid if the field has
119   // focus.
120   NSRange GetMarkedRange() const;
121
122   // Returns true if |field_| is first-responder in the window.  Used
123   // in various DCHECKS to make sure code is running in appropriate
124   // situations.
125   bool IsFirstResponder() const;
126
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);
130
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);
134
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);
138
139   // Update the field with |display_text| and set the selection.
140   void SetTextAndSelectedRange(const string16& display_text,
141                                const NSRange range);
142
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;
147
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);
152
153   // Return the number of UTF-16 units in the current buffer, excluding the
154   // suggested text.
155   virtual int GetOmniboxTextLength() const OVERRIDE;
156   NSUInteger GetTextLength() const;
157
158   // Returns true if the caret is at the end of the content.
159   bool IsCaretAtEnd() const;
160
161   scoped_ptr<OmniboxPopupView> popup_view_;
162
163   AutocompleteTextField* field_;  // owned by tab controller
164
165   // Selection at the point where the user started using the
166   // arrows to move around in the popup.
167   NSRange saved_temporary_selection_;
168
169   // Tracking state before and after a possible change for reporting
170   // to model_.
171   NSRange selection_before_change_;
172   string16 text_before_change_;
173   NSRange marked_range_before_change_;
174
175   // Was delete pressed?
176   bool delete_was_pressed_;
177
178   // Was the delete key pressed with an empty selection at the end of the edit?
179   bool delete_at_end_pressed_;
180
181   string16 suggest_text_;
182
183   DISALLOW_COPY_AND_ASSIGN(OmniboxViewMac);
184 };
185
186 #endif  // CHROME_BROWSER_UI_COCOA_OMNIBOX_OMNIBOX_VIEW_MAC_H_