Home | History | Annotate | Download | only in gtk
      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 // A helper class that contains the gtk widgets that make up the titlebar.  The
      6 // titlebar consists of the tabstrip and if the custom chrome frame is turned
      7 // on, it includes the taller titlebar and minimize, restore, maximize, and
      8 // close buttons.
      9 
     10 #ifndef CHROME_BROWSER_UI_GTK_BROWSER_TITLEBAR_H_
     11 #define CHROME_BROWSER_UI_GTK_BROWSER_TITLEBAR_H_
     12 #pragma once
     13 
     14 #include <gtk/gtk.h>
     15 
     16 #include "base/memory/scoped_ptr.h"
     17 #include "content/common/notification_observer.h"
     18 #include "content/common/notification_registrar.h"
     19 #include "ui/base/gtk/gtk_signal.h"
     20 #include "ui/base/models/simple_menu_model.h"
     21 #include "ui/base/x/active_window_watcher_x.h"
     22 
     23 class BrowserWindowGtk;
     24 class CustomDrawButton;
     25 class GtkThemeService;
     26 class MenuGtk;
     27 class PopupPageMenuModel;
     28 class TabContents;
     29 
     30 class BrowserTitlebar : public NotificationObserver,
     31                         public ui::ActiveWindowWatcherX::Observer,
     32                         public ui::SimpleMenuModel::Delegate {
     33  public:
     34   // A default button order string for when we aren't asking gconf for the
     35   // metacity configuration.
     36   static const char kDefaultButtonString[];
     37 
     38   BrowserTitlebar(BrowserWindowGtk* browser_window, GtkWindow* window);
     39   virtual ~BrowserTitlebar();
     40 
     41   GtkWidget* widget() {
     42     return container_;
     43   }
     44 
     45   void set_window(GtkWindow* window) { window_ = window; }
     46 
     47   // Builds the buttons based on the metacity |button_string|.
     48   void BuildButtons(const std::string& button_string);
     49 
     50   // Update the appearance of the title bar based on whether we're showing a
     51   // custom frame or not.  If |use_custom_frame| is true, we show an extra
     52   // tall titlebar and the min/max/close buttons.
     53   void UpdateCustomFrame(bool use_custom_frame);
     54 
     55   // Updates the title and icon when in app or popup mode (no tabstrip).
     56   void UpdateTitleAndIcon();
     57 
     58   // Called by the browser asking us to update the loading throbber.
     59   // |tab_contents| is the tab that is associated with the window throbber.
     60   // |tab_contents| can be null.
     61   void UpdateThrobber(TabContents* tab_contents);
     62 
     63   // On Windows, right clicking in the titlebar background brings up the system
     64   // menu.  There's no such thing on linux, so we just show the menu items we
     65   // add to the menu.
     66   void ShowContextMenu(GdkEventButton* event);
     67 
     68  private:
     69   // A helper class to keep track of which frame of the throbber animation
     70   // we're showing.
     71   class Throbber {
     72    public:
     73     Throbber() : current_frame_(0), current_waiting_frame_(0) {}
     74 
     75     // Get the next frame in the animation. The image is owned by the throbber
     76     // so the caller doesn't need to unref.  |is_waiting| is true if we're
     77     // still waiting for a response.
     78     GdkPixbuf* GetNextFrame(bool is_waiting);
     79 
     80     // Reset back to the first frame.
     81     void Reset();
     82    private:
     83     // Make sure the frames are loaded.
     84     static void InitFrames();
     85 
     86     int current_frame_;
     87     int current_waiting_frame_;
     88   };
     89 
     90   class ContextMenuModel : public ui::SimpleMenuModel {
     91    public:
     92     explicit ContextMenuModel(ui::SimpleMenuModel::Delegate* delegate);
     93   };
     94 
     95   // Build the titlebar, the space above the tab
     96   // strip, and (maybe) the min, max, close buttons.  |container| is the gtk
     97   // continer that we put the widget into.
     98   void Init();
     99 
    100   // Lazily builds and returns |titlebar_{left,right}_buttons_vbox_| and their
    101   // subtrees. We do this lazily because in most situations, only one of them
    102   // is allocated (though the user can (and do) manually mess with their gconf
    103   // settings to get absolutely horrid combinations of buttons on both sides.
    104   GtkWidget* GetButtonHBox(bool left_side);
    105 
    106   // Constructs a CustomDraw button given 3 image ids (IDR_), the box to place
    107   // the button into, and a tooltip id (IDS_).
    108   CustomDrawButton* BuildTitlebarButton(int image, int image_pressed,
    109                                         int image_hot, GtkWidget* box,
    110                                         int tooltip);
    111 
    112   // Update the titlebar spacing based on the custom frame and maximized state.
    113   void UpdateTitlebarAlignment();
    114 
    115   // Updates the color of the title bar. Called whenever we have a state
    116   // change in the window.
    117   void UpdateTextColor();
    118 
    119   // Show the menu that the user gets from left-clicking the favicon.
    120   void ShowFaviconMenu(GdkEventButton* event);
    121 
    122   // The maximize button was clicked, take an action depending on which mouse
    123   // button the user pressed.
    124   void MaximizeButtonClicked();
    125 
    126   // Updates the visibility of the maximize and restore buttons; only one can
    127   // be visible at a time.
    128   void UpdateMaximizeRestoreVisibility();
    129 
    130   // Callback for changes to window state.  This includes
    131   // maximizing/restoring/minimizing the window.
    132   CHROMEG_CALLBACK_1(BrowserTitlebar, gboolean, OnWindowStateChanged,
    133                      GtkWindow*, GdkEventWindowState*);
    134 
    135   // Callback for mousewheel events.
    136   CHROMEGTK_CALLBACK_1(BrowserTitlebar, gboolean, OnScroll,
    137                        GdkEventScroll*);
    138 
    139   // Callback for min/max/close buttons.
    140   CHROMEGTK_CALLBACK_0(BrowserTitlebar, void, OnButtonClicked);
    141 
    142   // Callback for favicon.
    143   CHROMEGTK_CALLBACK_1(BrowserTitlebar, gboolean, OnButtonPressed,
    144                        GdkEventButton*);
    145 
    146   // -- Context Menu -----------------------------------------------------------
    147 
    148   // SimpleMenuModel::Delegate implementation:
    149   virtual bool IsCommandIdEnabled(int command_id) const;
    150   virtual bool IsCommandIdChecked(int command_id) const;
    151   virtual void ExecuteCommand(int command_id);
    152   virtual bool GetAcceleratorForCommandId(int command_id,
    153                                           ui::Accelerator* accelerator);
    154 
    155   // Overridden from NotificationObserver:
    156   virtual void Observe(NotificationType type,
    157                        const NotificationSource& source,
    158                        const NotificationDetails& details);
    159 
    160   // Overriden from ActiveWindowWatcher::Observer.
    161   virtual void ActiveWindowChanged(GdkWindow* active_window);
    162 
    163   // Pointers to the browser window that owns us and it's GtkWindow.
    164   BrowserWindowGtk* browser_window_;
    165   GtkWindow* window_;
    166 
    167   // The container widget the holds the hbox which contains the whole titlebar.
    168   GtkWidget* container_;
    169 
    170   // The hbox that contains the whole titlebar.
    171   GtkWidget* container_hbox_;
    172 
    173   // VBoxes that holds the min/max/close buttons box and an optional padding
    174   // that defines the skyline if the user turns off window manager decorations.
    175   // There is a left and right version of this box since we try to integrate
    176   // with the recent Ubuntu theme changes which put the buttons on the left.
    177   GtkWidget* titlebar_left_buttons_vbox_;
    178   GtkWidget* titlebar_right_buttons_vbox_;
    179 
    180   // HBoxes that contains the actual min/max/close buttons.
    181   GtkWidget* titlebar_left_buttons_hbox_;
    182   GtkWidget* titlebar_right_buttons_hbox_;
    183 
    184   // Spy frame. One of these frames holds the spy guy in incognito mode. It's
    185   // the side with the least buttons. These are NULL when we aren't in
    186   // incognito mode.
    187   GtkWidget* titlebar_left_spy_frame_;
    188   GtkWidget* titlebar_right_spy_frame_;
    189 
    190   // Padding between the titlebar buttons and the top of the screen. Only show
    191   // when not maximized.
    192   GtkWidget* top_padding_left_;
    193   GtkWidget* top_padding_right_;
    194 
    195   // Gtk alignment that contains the tab strip.  If the user turns off window
    196   // manager decorations, we draw this taller.
    197   GtkWidget* titlebar_alignment_;
    198 
    199   // The favicon and page title used when in app mode or popup mode.
    200   GtkWidget* app_mode_favicon_;
    201   GtkWidget* app_mode_title_;
    202 
    203   // Whether we are using a custom frame.
    204   bool using_custom_frame_;
    205 
    206   // Whether we have focus (gtk_window_is_active() sometimes returns the wrong
    207   // value, so manually track the focus-in and focus-out events.)
    208   bool window_has_focus_;
    209 
    210   // We change the size of these three buttons when the window is maximized, so
    211   // we use these structs to keep track of their original size.
    212   GtkRequisition close_button_req_;
    213   GtkRequisition minimize_button_req_;
    214   GtkRequisition restore_button_req_;
    215 
    216   // Maximize and restore widgets in the titlebar.
    217   scoped_ptr<CustomDrawButton> minimize_button_;
    218   scoped_ptr<CustomDrawButton> maximize_button_;
    219   scoped_ptr<CustomDrawButton> restore_button_;
    220   scoped_ptr<CustomDrawButton> close_button_;
    221 
    222   // The context menu view and model.
    223   scoped_ptr<MenuGtk> context_menu_;
    224   scoped_ptr<ContextMenuModel> context_menu_model_;
    225 
    226   // The favicon menu view and model.
    227   scoped_ptr<MenuGtk> favicon_menu_;
    228   scoped_ptr<PopupPageMenuModel> favicon_menu_model_;
    229 
    230   // The throbber used when the window is in app mode or popup window mode.
    231   Throbber throbber_;
    232 
    233   // Theme provider for building buttons.
    234   GtkThemeService* theme_service_;
    235 
    236   NotificationRegistrar registrar_;
    237 };
    238 
    239 #endif  // CHROME_BROWSER_UI_GTK_BROWSER_TITLEBAR_H_
    240