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_GTK_TABS_TAB_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_TABS_TAB_GTK_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "chrome/browser/ui/gtk/tabs/tab_renderer_gtk.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "ui/base/gtk/gtk_signal.h"
16 class TabStripMenuController;
19 class TabGtk : public TabRendererGtk, public base::MessageLoopForUI::Observer {
21 // An interface implemented by an object that can help this Tab complete
22 // various actions. The index parameter is the index of this Tab in the
23 // TabRenderer::Model.
26 // Returns true if the specified Tab is active.
27 virtual bool IsTabActive(const TabGtk* tab) const = 0;
29 // Returns true if the specified Tab is selected.
30 virtual bool IsTabSelected(const TabGtk* tab) const = 0;
32 // Returns true if the specified Tab is pinned.
33 virtual bool IsTabPinned(const TabGtk* tab) const = 0;
35 // Returns true if the specified Tab is detached.
36 virtual bool IsTabDetached(const TabGtk* tab) const = 0;
38 // Activates the specified Tab.
39 virtual void ActivateTab(TabGtk* tab) = 0;
41 // Toggles selection of the specified Tab.
42 virtual void ToggleTabSelection(TabGtk* tab) = 0;
44 // Extends selection from the anchor to the specified Tab.
45 virtual void ExtendTabSelection(TabGtk* tab) = 0;
47 // Closes the specified Tab.
48 virtual void CloseTab(TabGtk* tab) = 0;
50 // Returns true if the specified command is enabled for the specified Tab.
51 virtual bool IsCommandEnabledForTab(
52 TabStripModel::ContextMenuCommand command_id,
53 const TabGtk* tab) const = 0;
55 // Executes the specified command for the specified Tab.
56 virtual void ExecuteCommandForTab(
57 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0;
59 // Starts/Stops highlighting the tabs that will be affected by the
60 // specified command for the specified Tab.
61 virtual void StartHighlightTabsForCommand(
62 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0;
63 virtual void StopHighlightTabsForCommand(
64 TabStripModel::ContextMenuCommand command_id, TabGtk* tab) = 0;
65 virtual void StopAllHighlighting() = 0;
67 // Potentially starts a drag for the specified Tab.
68 virtual void MaybeStartDrag(TabGtk* tab, const gfx::Point& point) = 0;
70 // Continues dragging a Tab.
71 virtual void ContinueDrag(GdkDragContext* context) = 0;
73 // Ends dragging a Tab. |canceled| is true if the drag was aborted in a way
74 // other than the user releasing the mouse. Returns whether the tab has been
76 virtual bool EndDrag(bool canceled) = 0;
78 // Returns true if the associated TabStrip's delegate supports tab moving or
79 // detaching. Used by the Frame to determine if dragging on the Tab
80 // itself should move the window in cases where there's only one
82 virtual bool HasAvailableDragActions() const = 0;
84 // Returns the theme provider for icons and colors.
85 virtual GtkThemeService* GetThemeProvider() = 0;
87 // Returns a context menu controller for |tab|. Caller takes ownership of
88 // the pointed object.
89 virtual TabStripMenuController* GetTabStripMenuControllerForTab(
93 virtual ~TabDelegate() {}
96 explicit TabGtk(TabDelegate* delegate);
99 // Access the delegate.
100 TabDelegate* delegate() const { return delegate_; }
102 GtkWidget* widget() const { return event_box_; }
104 // Used to set/check whether this Tab is being animated closed.
105 void set_closing(bool closing) { closing_ = closing; }
106 bool closing() const { return closing_; }
108 // Used to set/check whether this Tab is being dragged.
109 void set_dragging(bool dragging) { dragging_ = dragging; }
110 bool dragging() const { return dragging_; }
112 // Raise to top of Z-order.
115 // TabRendererGtk overrides:
116 virtual bool IsActive() const OVERRIDE;
117 virtual bool IsSelected() const OVERRIDE;
118 virtual bool IsVisible() const OVERRIDE;
119 virtual void SetVisible(bool visible) const OVERRIDE;
120 virtual void CloseButtonClicked() OVERRIDE;
121 virtual void UpdateData(content::WebContents* contents,
123 bool loading_only) OVERRIDE;
124 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
127 class TabGtkObserverHelper;
129 // MessageLoop::Observer implementation:
130 virtual void WillProcessEvent(GdkEvent* event) OVERRIDE;
131 virtual void DidProcessEvent(GdkEvent* event) OVERRIDE;
133 // button-press-event handler that handles mouse clicks.
134 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnButtonPressEvent, GdkEventButton*);
136 // button-release-event handler that handles mouse click releases.
137 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnButtonReleaseEvent, GdkEventButton*);
139 // drag-begin is emitted when the drag is started. We connect so that we can
140 // set the drag icon to a transparent pixbuf.
141 CHROMEGTK_CALLBACK_1(TabGtk, void, OnDragBegin, GdkDragContext*);
143 // drag-failed is emitted when the drag is finished. In our case the signal
144 // does not imply failure as we don't use the drag-n-drop API to transfer drop
146 CHROMEGTK_CALLBACK_2(TabGtk, gboolean, OnDragFailed, GdkDragContext*,
149 // When a drag is ending, a fake button release event is passed to the drag
150 // widget to fake letting go of the mouse button. We need a callback for
151 // this event because it is the only way to catch drag end events when the
152 // user presses space or return.
153 CHROMEGTK_CALLBACK_1(TabGtk, gboolean, OnDragButtonReleased, GdkEventButton*);
155 // Invoked when the context menu closes.
156 void ContextMenuClosed();
158 // Sets whether the tooltip should be shown or not, depending on the size of
160 void UpdateTooltipState();
162 // Creates the drag widget used to track a drag operation.
163 void CreateDragWidget();
165 // Destroys the drag widget.
166 void DestroyDragWidget();
168 // Starts the dragging operation. |drag_offset| is the offset inside the tab
169 // bounds where the grab occurred.
170 void StartDragging(gfx::Point drag_offset);
172 // Ends the dragging operations. |canceled| is true if the operation was
174 void EndDrag(bool canceled);
176 // An instance of a delegate object that can perform various actions based on
178 TabDelegate* delegate_;
180 // True if the tab is being animated closed.
183 // True if the tab is being dragged.
186 // The context menu controller.
187 scoped_ptr<TabStripMenuController> menu_controller_;
189 // The windowless widget used to collect input events for the tab. We can't
190 // use an OwnedWidgetGtk because of the way the dragged tab controller
191 // destroys the source tab. The source tab is destroyed when the drag ends
192 // before we let gtk handle the end of the drag. This results in the widget
193 // having an extra reference, which will cause OwnedWidgetGtk.Destroy to
195 GtkWidget* event_box_;
197 // A copy of the last button press event, used to initiate a drag.
198 GdkEvent* last_mouse_down_;
200 // A GtkInivisible used to track the drag event. GtkInvisibles are of the
201 // type GInitiallyUnowned, but the widget initialization code sinks the
202 // reference, so we can't used an OwnedWidgetGtk here.
203 GtkWidget* drag_widget_;
205 // The cached width of the title in pixels, updated whenever the title
209 // Keep track of whether or not we have an observer.
210 scoped_ptr<TabGtkObserverHelper> observer_;
212 // Used to destroy the drag widget after a return to the message loop.
213 base::WeakPtrFactory<TabGtk> destroy_factory_;
215 // Due to a bug in GTK+, we need to force the end of a drag when we get a
216 // mouse release event on the the dragged widget, otherwise, we don't know
217 // when the drag has ended when the user presses space or enter. We queue
218 // a task to end the drag and only run it if GTK+ didn't send us the
219 // drag-failed event.
220 base::WeakPtrFactory<TabGtk> drag_end_factory_;
222 DISALLOW_COPY_AND_ASSIGN(TabGtk);
225 #endif // CHROME_BROWSER_UI_GTK_TABS_TAB_GTK_H_