summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete/autocomplete_edit_view_gtk.h
blob: b9c83a3123a3b3093f186c5c51f24c068c682d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_GTK_H_
#define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_GTK_H_

#include <gtk/gtk.h>

#include <string>

#include "base/basictypes.h"
#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "chrome/browser/autocomplete/autocomplete_edit_view.h"
#include "chrome/browser/toolbar_model.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
#include "chrome/common/owned_widget_gtk.h"
#include "chrome/common/page_transition_types.h"
#include "webkit/glue/window_open_disposition.h"

class AutocompleteEditController;
class AutocompleteEditModel;
class AutocompletePopupPositioner;
class AutocompletePopupView;
class CommandUpdater;
class Profile;
class TabContents;

#if !defined(TOOLKIT_VIEWS)
class GtkThemeProvider;
#endif

class AutocompleteEditViewGtk : public AutocompleteEditView,
                                public NotificationObserver {
 public:
  // Modeled like the Windows CHARRANGE.  Represent a pair of cursor position
  // offsets.  Since GtkTextIters are invalid after the buffer is changed, we
  // work in character offsets (not bytes).
  struct CharRange {
    CharRange() : cp_min(0), cp_max(0) { }
    CharRange(int n, int x) : cp_min(n), cp_max(x) { }

    // Work in integers to match the gint GTK APIs.
    int cp_min;  // For a selection: Represents the start.
    int cp_max;  // For a selection: Represents the end (insert position).
  };

  AutocompleteEditViewGtk(AutocompleteEditController* controller,
                          ToolbarModel* toolbar_model,
                          Profile* profile,
                          CommandUpdater* command_updater,
                          bool popup_window_mode,
                          AutocompletePopupPositioner* popup_positioner);
  ~AutocompleteEditViewGtk();

  // Initialize, create the underlying widgets, etc.
  void Init();

  GtkWidget* widget() { return alignment_.get(); }

  // Grab keyboard input focus, putting focus on the location widget.
  void SetFocus();

  // Implement the AutocompleteEditView interface.
  virtual AutocompleteEditModel* model() { return model_.get(); }
  virtual const AutocompleteEditModel* model() const { return model_.get(); }

  virtual void SaveStateToTab(TabContents* tab);

  virtual void Update(const TabContents* tab_for_state_restoring);

  virtual void OpenURL(const GURL& url,
                       WindowOpenDisposition disposition,
                       PageTransition::Type transition,
                       const GURL& alternate_nav_url,
                       size_t selected_line,
                       const std::wstring& keyword);

  virtual std::wstring GetText() const;

  virtual void SetUserText(const std::wstring& text) {
    SetUserText(text, text, true);
  }
  virtual void SetUserText(const std::wstring& text,
                           const std::wstring& display_text,
                           bool update_popup);

  virtual void SetWindowTextAndCaretPos(const std::wstring& text,
                                        size_t caret_pos);

  virtual void SetForcedQuery();

  virtual bool IsSelectAll();
  virtual void SelectAll(bool reversed);
  virtual void RevertAll();

  virtual void UpdatePopup();
  virtual void ClosePopup();

  virtual void OnTemporaryTextMaybeChanged(const std::wstring& display_text,
                                           bool save_original_selection);
  virtual bool OnInlineAutocompleteTextMaybeChanged(
      const std::wstring& display_text, size_t user_text_length);
  virtual void OnRevertTemporaryText();
  virtual void OnBeforePossibleChange();
  virtual bool OnAfterPossibleChange();
  virtual gfx::NativeView GetNativeView() const;

  // Overridden from NotificationObserver:
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  void SetBaseColor();

 private:
  // TODO(deanm): Would be nice to insulate the thunkers better, etc.
  static void HandleBeginUserActionThunk(GtkTextBuffer* unused, gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleBeginUserAction();
  }
  void HandleBeginUserAction();

  static void HandleEndUserActionThunk(GtkTextBuffer* unused, gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleEndUserAction();
  }
  void HandleEndUserAction();

  static gboolean HandleKeyPressThunk(GtkWidget* widget,
                                      GdkEventKey* event,
                                      gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleKeyPress(
        widget, event);
  }
  gboolean HandleKeyPress(GtkWidget* widget, GdkEventKey* event);

  static gboolean HandleKeyReleaseThunk(GtkWidget* widget,
                                        GdkEventKey* event,
                                        gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleKeyRelease(
        widget, event);
  }
  gboolean HandleKeyRelease(GtkWidget* widget, GdkEventKey* event);

  static gboolean HandleViewButtonPressThunk(GtkWidget* view,
                                             GdkEventButton* event,
                                             gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewButtonPress(event);
  }
  gboolean HandleViewButtonPress(GdkEventButton* event);

  static gboolean HandleViewButtonReleaseThunk(GtkWidget* view,
                                               GdkEventButton* event,
                                               gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewButtonRelease(event);
  }
  gboolean HandleViewButtonRelease(GdkEventButton* event);

  static gboolean HandleViewFocusInThunk(GtkWidget* view,
                                          GdkEventFocus* event,
                                          gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewFocusIn();
  }
  gboolean HandleViewFocusIn();

  static gboolean HandleViewFocusOutThunk(GtkWidget* view,
                                          GdkEventFocus* event,
                                          gpointer self) {
    return reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewFocusOut();
  }
  gboolean HandleViewFocusOut();

  static void HandleViewMoveCursorThunk(GtkWidget* view,
                                        GtkMovementStep step,
                                        gint count,
                                        gboolean extend_selection,
                                        gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewMoveCursor(step, count, extend_selection);
  }
  void HandleViewMoveCursor(GtkMovementStep step,
                            gint count,
                            gboolean extendion_selection);

  static void HandleViewSizeRequestThunk(GtkWidget* view,
                                         GtkRequisition* req,
                                         gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleViewSizeRequest(req);
  }
  void HandleViewSizeRequest(GtkRequisition* req);

  static void HandlePopulatePopupThunk(GtkEntry* entry,
                                       GtkMenu* menu,
                                       gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandlePopulatePopup(menu);
  }
  void HandlePopulatePopup(GtkMenu* menu);

  static void HandleEditSearchEnginesThunk(GtkMenuItem* menuitem,
                                           gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleEditSearchEngines();
  }
  void HandleEditSearchEngines();

  static void HandlePasteAndGoThunk(GtkMenuItem* menuitem,
                                    AutocompleteEditViewGtk* self) {
    self->HandlePasteAndGo();
  }
  void HandlePasteAndGo();

  static void HandlePasteAndGoReceivedTextThunk(GtkClipboard* clipboard,
                                                const gchar* text,
                                                gpointer self) {
    // If there is nothing to paste (|text| is NULL), do nothing.
    if (!text)
      return;
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandlePasteAndGoReceivedText(UTF8ToWide(text));
  }
  void HandlePasteAndGoReceivedText(const std::wstring& text);

  static void HandleMarkSetThunk(GtkTextBuffer* buffer,
                                 GtkTextIter* location,
                                 GtkTextMark* mark,
                                 gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleMarkSet(buffer, location, mark);
  }
  void HandleMarkSet(GtkTextBuffer* buffer,
                     GtkTextIter* location,
                     GtkTextMark* mark);

  static void HandleDragDataReceivedThunk(GtkWidget* widget,
                                          GdkDragContext* context,
                                          gint x, gint y,
                                          GtkSelectionData* selection_data,
                                          guint target_type,
                                          guint time,
                                          gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleDragDataReceived(context, x, y, selection_data, target_type,
                               time);
  }
  void HandleDragDataReceived(GdkDragContext* context, gint x, gint y,
                              GtkSelectionData* selection_data,
                              guint target_type, guint time);

  static void HandleInsertTextThunk(GtkTextBuffer* buffer,
                                    GtkTextIter* location,
                                    const gchar* text,
                                    gint len,
                                    gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->
        HandleInsertText(buffer, location, text, len);
  }
  void HandleInsertText(GtkTextBuffer* buffer,
                        GtkTextIter* location,
                        const gchar* text,
                        gint len);

  static void HandleBackSpaceThunk(GtkTextView* text_view, gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleBackSpace();
  }
  void HandleBackSpace();

  static void HandleCopyClipboardThunk(GtkTextView* text_view, gpointer self) {
    reinterpret_cast<AutocompleteEditViewGtk*>(self)->HandleCopyClipboard();
  }
  void HandleCopyClipboard();

  // Actual implementation of SelectAll(), but also provides control over
  // whether the PRIMARY selection is set to the selected text (in SelectAll(),
  // it isn't, but we want set the selection when the user clicks in the entry).
  void SelectAllInternal(bool reversed, bool update_primary_selection);

  // Get ready to update |text_buffer_|'s highlighting without making changes to
  // the PRIMARY selection.  Removes the clipboard from |text_buffer_| and
  // blocks the "mark-set" signal handler.
  void StartUpdatingHighlightedText();

  // Finish updating |text_buffer_|'s highlighting such that future changes will
  // automatically update the PRIMARY selection.  Undoes
  // StartUpdatingHighlightedText()'s changes.
  void FinishUpdatingHighlightedText();

  // Get the character indices of the current selection.  This honors
  // direction, cp_max is the insertion point, and cp_min is the bound.
  CharRange GetSelection();

  // Translate from character positions to iterators for the current buffer.
  void ItersFromCharRange(const CharRange& range,
                          GtkTextIter* iter_min,
                          GtkTextIter* iter_max);

  // Return the number of characers in the current buffer.
  int GetTextLength();

  // Try to parse the current text as a URL and colorize the components.
  void EmphasizeURLComponents();

  // Internally invoked whenever the text changes in some way.
  void TextChanged();

  // Save |selected_text| as the PRIMARY X selection.
  void SavePrimarySelection(const std::string& selected_text);

  // The widget we expose, used for vertically centering the real text edit,
  // since the height will change based on the font / font size, etc.
  OwnedWidgetGtk alignment_;

  // The actual text entry which will be owned by the alignment_.
  GtkWidget* text_view_;

  GtkTextTagTable* tag_table_;
  GtkTextBuffer* text_buffer_;
  GtkTextTag* faded_text_tag_;
  GtkTextTag* secure_scheme_tag_;
  GtkTextTag* insecure_scheme_tag_;
  GtkTextTag* normal_text_tag_;

  scoped_ptr<AutocompleteEditModel> model_;
  scoped_ptr<AutocompletePopupView> popup_view_;
  AutocompleteEditController* controller_;
  ToolbarModel* toolbar_model_;

  // The object that handles additional command functionality exposed on the
  // edit, such as invoking the keyword editor.
  CommandUpdater* command_updater_;

  // When true, the location bar view is read only and also is has a slightly
  // different presentation (smaller font size). This is used for popups.
  bool popup_window_mode_;

  ToolbarModel::SecurityLevel scheme_security_level_;

  // Tracking state before and after a possible change.
  std::wstring text_before_change_;
  CharRange sel_before_change_;

  // The most-recently-selected text from the entry.  This is updated on-the-fly
  // as the user selects text.  It is used in cases where we need to make the
  // PRIMARY selection persist even after the user has unhighlighted the text in
  // the view (e.g. when they highlight some text and then click to unhighlight
  // it, we pass this string to SavePrimarySelection()).
  std::string selected_text_;

  // Has the current value of |selected_text_| been saved as the PRIMARY
  // selection?
  bool selection_saved_;

  // ID of the signal handler for "mark-set" on |text_buffer_|.
  gulong mark_set_handler_id_;

  // Is the first mouse button currently down?
  bool button_1_pressed_;

  // Did the user change the selected text in the middle of the current click?
  // If so, we don't select all of the text when the button is released -- we
  // don't want to blow away their selection.
  bool text_selected_during_click_;

  // Was the text view already focused before the user clicked in it?  We use
  // this to figure out whether we should select all of the text when the button
  // is released (we only do so if the view was initially unfocused).
  bool text_view_focused_before_button_press_;

#if !defined(TOOLKIT_VIEWS)
  // Supplies colors, et cetera.
  GtkThemeProvider* theme_provider_;

  NotificationRegistrar registrar_;
#endif

  // If a character is inserted, store it in this variable so that it can
  // be used later in "key-press-event" signal handler to determine if a Tab or
  // Enter key event is handled by IME or not.
  char char_inserted_;

  DISALLOW_COPY_AND_ASSIGN(AutocompleteEditViewGtk);
};

#endif  // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_VIEW_GTK_H_