autocomplete_edit_view_mac.h revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_
6#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_
7#pragma once
8
9#import <Cocoa/Cocoa.h>
10
11#include "base/scoped_ptr.h"
12#include "chrome/browser/autocomplete/autocomplete_edit_view.h"
13#include "chrome/browser/cocoa/location_bar/autocomplete_text_field.h"
14
15class AutocompleteEditController;
16class AutocompletePopupViewMac;
17class Clipboard;
18class Profile;
19class ToolbarModel;
20
21// Implements AutocompleteEditView on an AutocompleteTextField.
22
23class AutocompleteEditViewMac : public AutocompleteEditView,
24                                public AutocompleteTextFieldObserver {
25 public:
26  AutocompleteEditViewMac(AutocompleteEditController* controller,
27                          ToolbarModel* toolbar_model,
28                          Profile* profile,
29                          CommandUpdater* command_updater,
30                          AutocompleteTextField* field);
31  virtual ~AutocompleteEditViewMac();
32
33  // Implement the AutocompleteEditView interface.
34  virtual AutocompleteEditModel* model() { return model_.get(); }
35  virtual const AutocompleteEditModel* model() const { return model_.get(); }
36
37  virtual void SaveStateToTab(TabContents* tab);
38  virtual void Update(const TabContents* tab_for_state_restoring);
39
40  virtual void OpenURL(const GURL& url,
41                       WindowOpenDisposition disposition,
42                       PageTransition::Type transition,
43                       const GURL& alternate_nav_url,
44                       size_t selected_line,
45                       const std::wstring& keyword);
46
47  virtual std::wstring GetText() const;
48
49  virtual bool IsEditingOrEmpty() const;
50  virtual int GetIcon() const;
51
52  virtual void SetUserText(const std::wstring& text);
53  virtual void SetUserText(const std::wstring& text,
54                           const std::wstring& display_text,
55                           bool update_popup);
56
57  virtual void SetWindowTextAndCaretPos(const std::wstring& text,
58                                        size_t caret_pos);
59
60  virtual void ReplaceSelection(const string16& text);
61
62  virtual void SetForcedQuery();
63
64  virtual bool IsSelectAll();
65  virtual void GetSelectionBounds(std::wstring::size_type* start,
66                                  std::wstring::size_type* end);
67
68  virtual void SelectAll(bool reversed);
69  virtual void RevertAll();
70  virtual void UpdatePopup();
71  virtual void ClosePopup();
72  virtual void SetFocus();
73  virtual void OnTemporaryTextMaybeChanged(const std::wstring& display_text,
74                                           bool save_original_selection);
75  virtual bool OnInlineAutocompleteTextMaybeChanged(
76      const std::wstring& display_text, size_t user_text_length);
77  virtual void OnRevertTemporaryText();
78  virtual void OnBeforePossibleChange();
79  virtual bool OnAfterPossibleChange();
80  virtual gfx::NativeView GetNativeView() const;
81  virtual CommandUpdater* GetCommandUpdater();
82
83  // Implement the AutocompleteTextFieldObserver interface.
84  virtual void OnControlKeyChanged(bool pressed);
85  virtual bool CanCopy();
86  virtual void CopyToPasteboard(NSPasteboard* pboard);
87  virtual void OnPaste();
88  virtual bool CanPasteAndGo();
89  virtual int GetPasteActionStringId();
90  virtual void OnPasteAndGo();
91  virtual void OnFrameChanged();
92  virtual void OnDidBeginEditing();
93  virtual void OnDidChange();
94  virtual void OnDidEndEditing();
95  virtual bool OnDoCommandBySelector(SEL cmd);
96  virtual void OnSetFocus(bool control_down);
97  virtual void OnKillFocus();
98
99  // Helper for LocationBarViewMac.  Optionally selects all in |field_|.
100  void FocusLocation(bool select_all);
101
102  // Helper to get appropriate contents from |clipboard|.  Returns
103  // empty string if no appropriate data is found on |clipboard|.
104  static std::wstring GetClipboardText(Clipboard* clipboard);
105
106  // Helper to get the font to use in the field, exposed for the
107  // popup.
108  static NSFont* GetFieldFont();
109
110  // If |resource_id| has a PDF image which can be used, return it.
111  // Otherwise return the PNG image from the resource bundle.
112  static NSImage* ImageForResource(int resource_id);
113
114 private:
115  // Called when the user hits backspace in |field_|.  Checks whether
116  // keyword search is being terminated.  Returns true if the
117  // backspace should be intercepted (not forwarded on to the standard
118  // machinery).
119  bool OnBackspacePressed();
120
121  // Returns the field's currently selected range.  Only valid if the
122  // field has focus.
123  NSRange GetSelectedRange() const;
124
125  // Returns true if |field_| is first-responder in the window.  Used
126  // in various DCHECKS to make sure code is running in appropriate
127  // situations.
128  bool IsFirstResponder() const;
129
130  // If |model_| believes it has focus, grab focus if needed and set
131  // the selection to |range|.  Otherwise does nothing.
132  void SetSelectedRange(const NSRange range);
133
134  // Update the field with |display_text| and highlight the host and
135  // scheme (if it's an URL or URL-fragment).
136  void SetText(const std::wstring& display_text);
137
138  // Update the field with |display_text| and set the selection.
139  void SetTextAndSelectedRange(const std::wstring& display_text,
140                               const NSRange range);
141
142  // Pass the current content of |field_| to SetText(), maintaining
143  // any selection.  Named to be consistent with GTK and Windows,
144  // though here we cannot really do the in-place operation they do.
145  void EmphasizeURLComponents();
146
147  // Calculates text attributes according to |display_text| and applies them
148  // to the given |as| object.
149  void ApplyTextAttributes(const std::wstring& display_text,
150                           NSMutableAttributedString* as);
151
152  scoped_ptr<AutocompleteEditModel> model_;
153  scoped_ptr<AutocompletePopupViewMac> popup_view_;
154
155  AutocompleteEditController* controller_;
156  ToolbarModel* toolbar_model_;
157
158  // The object that handles additional command functionality exposed on the
159  // edit, such as invoking the keyword editor.
160  CommandUpdater* command_updater_;
161
162  AutocompleteTextField* field_;  // owned by tab controller
163
164  // Selection at the point where the user started using the
165  // arrows to move around in the popup.
166  NSRange saved_temporary_selection_;
167
168  // Tracking state before and after a possible change for reporting
169  // to model_.
170  NSRange selection_before_change_;
171  std::wstring text_before_change_;
172
173  // The maximum/standard line height for the displayed text.
174  CGFloat line_height_;
175
176  DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewMac);
177};
178
179#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_MAC_H_
180