1 // Copyright (c) 2011 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_GTK_TABS_TAB_GTK_H_ 6 #define CHROME_BROWSER_UI_GTK_TABS_TAB_GTK_H_ 7 #pragma once 8 9 #include "base/basictypes.h" 10 #include "base/message_loop.h" 11 #include "chrome/browser/tabs/tab_strip_model.h" 12 #include "chrome/browser/ui/gtk/tabs/tab_renderer_gtk.h" 13 #include "ui/base/gtk/gtk_signal.h" 14 15 namespace gfx { 16 class Path; 17 } 18 19 namespace ui { 20 class ThemeProvider; 21 } 22 23 class TabGtk : public TabRendererGtk, 24 public MessageLoopForUI::Observer { 25 public: 26 // An interface implemented by an object that can help this Tab complete 27 // various actions. The index parameter is the index of this Tab in the 28 // TabRenderer::Model. 29 class TabDelegate { 30 public: 31 // Returns true if the specified Tab is selected. 32 virtual bool IsTabSelected(const TabGtk* tab) const = 0; 33 34 // Returns true if the specified Tab is pinned. 35 virtual bool IsTabPinned(const TabGtk* tab) const = 0; 36 37 // Returns true if the specified Tab is detached. 38 virtual bool IsTabDetached(const TabGtk* tab) const = 0; 39 40 // Selects the specified Tab. 41 virtual void SelectTab(TabGtk* tab) = 0; 42 43 // Closes the specified Tab. 44 virtual void CloseTab(TabGtk* tab) = 0; 45 46 // Returns true if the specified command is enabled for the specified Tab. 47 virtual bool IsCommandEnabledForTab( 48 TabStripModel::ContextMenuCommand command_id, 49 const TabGtk* tab) const = 0; 50 51 // Executes the specified command for the specified Tab. 52 virtual void ExecuteCommandForTab( 53 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0; 54 55 // Starts/Stops highlighting the tabs that will be affected by the 56 // specified command for the specified Tab. 57 virtual void StartHighlightTabsForCommand( 58 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0; 59 virtual void StopHighlightTabsForCommand( 60 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0; 61 virtual void StopAllHighlighting() = 0; 62 63 // Potentially starts a drag for the specified Tab. 64 virtual void MaybeStartDrag(TabGtk* tab, const gfx::Point& point) = 0; 65 66 // Continues dragging a Tab. 67 virtual void ContinueDrag(GdkDragContext* context) = 0; 68 69 // Ends dragging a Tab. |canceled| is true if the drag was aborted in a way 70 // other than the user releasing the mouse. Returns whether the tab has been 71 // destroyed. 72 virtual bool EndDrag(bool canceled) = 0; 73 74 // Returns true if the associated TabStrip's delegate supports tab moving or 75 // detaching. Used by the Frame to determine if dragging on the Tab 76 // itself should move the window in cases where there's only one 77 // non drag-able Tab. 78 virtual bool HasAvailableDragActions() const = 0; 79 80 // Returns the theme provider for icons and colors. 81 virtual ui::ThemeProvider* GetThemeProvider() = 0; 82 83 protected: ~TabDelegate()84 virtual ~TabDelegate() {} 85 }; 86 87 explicit TabGtk(TabDelegate* delegate); 88 virtual ~TabGtk(); 89 90 // Access the delegate. delegate()91 TabDelegate* delegate() const { return delegate_; } 92 widget()93 GtkWidget* widget() const { return event_box_; } 94 95 // Used to set/check whether this Tab is being animated closed. set_closing(bool closing)96 void set_closing(bool closing) { closing_ = closing; } closing()97 bool closing() const { return closing_; } 98 99 // Used to set/check whether this Tab is being dragged. set_dragging(bool dragging)100 void set_dragging(bool dragging) { dragging_ = dragging; } dragging()101 bool dragging() const { return dragging_; } 102 103 // TabRendererGtk overrides: 104 virtual bool IsSelected() const; 105 virtual bool IsVisible() const; 106 virtual void SetVisible(bool visible) const; 107 virtual void CloseButtonClicked(); 108 virtual void UpdateData(TabContents* contents, bool app, bool loading_only); 109 virtual void SetBounds(const gfx::Rect& bounds); 110 111 private: 112 class ContextMenuController; 113 class TabGtkObserverHelper; 114 friend class ContextMenuController; 115 116 // MessageLoop::Observer implementation: 117 virtual void WillProcessEvent(GdkEvent* event); 118 virtual void DidProcessEvent(GdkEvent* event); 119 120 // button-press-event handler that handles mouse clicks. 121 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnButtonPressEvent, GdkEventButton*); 122 123 // button-release-event handler that handles mouse click releases. 124 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnButtonReleaseEvent, GdkEventButton*); 125 126 // drag-begin is emitted when the drag is started. We connect so that we can 127 // set the drag icon to a transparent pixbuf. 128 CHROMEGTK_CALLBACK_1(TabGtk, void, OnDragBegin, GdkDragContext*); 129 130 // drag-failed is emitted when the drag is finished. In our case the signal 131 // does not imply failure as we don't use the drag-n-drop API to transfer drop 132 // data. 133 CHROMEGTK_CALLBACK_2(TabGtk, gboolean, OnDragFailed, GdkDragContext*, 134 GtkDragResult); 135 136 // When a drag is ending, a fake button release event is passed to the drag 137 // widget to fake letting go of the mouse button. We need a callback for 138 // this event because it is the only way to catch drag end events when the 139 // user presses space or return. 140 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnDragButtonReleased, GdkEventButton*); 141 142 // Invoked when the context menu closes. 143 void ContextMenuClosed(); 144 145 // Sets whether the tooltip should be shown or not, depending on the size of 146 // the tab. 147 void UpdateTooltipState(); 148 149 // Creates the drag widget used to track a drag operation. 150 void CreateDragWidget(); 151 152 // Destroys the drag widget. 153 void DestroyDragWidget(); 154 155 // Starts the dragging operation. |drag_offset| is the offset inside the tab 156 // bounds where the grab occurred. 157 void StartDragging(gfx::Point drag_offset); 158 159 // Ends the dragging operations. |canceled| is true if the operation was 160 // canceled. 161 void EndDrag(bool canceled); 162 163 // An instance of a delegate object that can perform various actions based on 164 // user gestures. 165 TabDelegate* delegate_; 166 167 // True if the tab is being animated closed. 168 bool closing_; 169 170 // True if the tab is being dragged. 171 bool dragging_; 172 173 // The context menu controller. 174 scoped_ptr<ContextMenuController> menu_controller_; 175 176 // The windowless widget used to collect input events for the tab. We can't 177 // use an OwnedWidgetGtk because of the way the dragged tab controller 178 // destroys the source tab. The source tab is destroyed when the drag ends 179 // before we let gtk handle the end of the drag. This results in the widget 180 // having an extra reference, which will cause OwnedWidgetGtk.Destroy to 181 // DCHECK. 182 GtkWidget* event_box_; 183 184 // A copy of the last button press event, used to initiate a drag. 185 GdkEvent* last_mouse_down_; 186 187 // A GtkInivisible used to track the drag event. GtkInvisibles are of the 188 // type GInitiallyUnowned, but the widget initialization code sinks the 189 // reference, so we can't used an OwnedWidgetGtk here. 190 GtkWidget* drag_widget_; 191 192 // The cached width of the title in pixels, updated whenever the title 193 // changes. 194 int title_width_; 195 196 // Keep track of whether or not we have an observer. 197 scoped_ptr<TabGtkObserverHelper> observer_; 198 199 // Used to destroy the drag widget after a return to the message loop. 200 ScopedRunnableMethodFactory<TabGtk> destroy_factory_; 201 202 // Due to a bug in GTK+, we need to force the end of a drag when we get a 203 // mouse release event on the the dragged widget, otherwise, we don't know 204 // when the drag has ended when the user presses space or enter. We queue 205 // a task to end the drag and only run it if GTK+ didn't send us the 206 // drag-failed event. 207 ScopedRunnableMethodFactory<TabGtk> drag_end_factory_; 208 209 DISALLOW_COPY_AND_ASSIGN(TabGtk); 210 }; 211 212 #endif // CHROME_BROWSER_UI_GTK_TABS_TAB_GTK_H_ 213