Home | History | Annotate | Download | only in frame
      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_CHROMEOS_FRAME_PANEL_CONTROLLER_H_
      6 #define CHROME_BROWSER_CHROMEOS_FRAME_PANEL_CONTROLLER_H_
      7 #pragma once
      8 
      9 #include <gtk/gtk.h>
     10 
     11 #include "third_party/cros/chromeos_wm_ipc_enums.h"
     12 #include "ui/base/x/x11_util.h"
     13 #include "views/controls/button/button.h"
     14 
     15 class BrowserView;
     16 class SkBitmap;
     17 typedef unsigned long XID;
     18 
     19 namespace base {
     20 class Time;
     21 }
     22 
     23 namespace views {
     24 class ImageButton;
     25 class ImageView;
     26 class Label;
     27 class MouseEvent;
     28 class Widget;
     29 }
     30 
     31 namespace chromeos {
     32 
     33 // Controls interactions with the WM for popups / panels.
     34 class PanelController {
     35  public:
     36   enum State {
     37     INITIAL,
     38     EXPANDED,
     39     MINIMIZED,
     40   };
     41 
     42   // Delegate to control panel's appearance and behavior.
     43   class Delegate {
     44    public:
     45     // Retrieves the title string of the panel.
     46     virtual string16 GetPanelTitle() = 0;
     47 
     48     // Retrieves the icon to use in the panel's titlebar.
     49     virtual SkBitmap GetPanelIcon() = 0;
     50 
     51     // Can the panel be closed?  Called before ClosePanel() when the close
     52     // button is pressed to give beforeunload handlers a chance to cancel.
     53     virtual bool CanClosePanel() = 0;
     54 
     55     // Close the panel. Called when a close button is pressed.
     56     virtual void ClosePanel() = 0;
     57 
     58     // Activate the panel. Called when maximized.
     59     virtual void ActivatePanel() = 0;
     60   };
     61 
     62   PanelController(Delegate* delegate_window,
     63                   GtkWindow* window);
     64   virtual ~PanelController() {}
     65 
     66   // Initializes the panel controller with the initial state of the focus and
     67   // the window bounds.
     68   void Init(bool initial_focus, const gfx::Rect& init_bounds, XID creator_xid,
     69             WmIpcPanelUserResizeType resize_type);
     70 
     71   bool TitleMousePressed(const views::MouseEvent& event);
     72   void TitleMouseReleased(const views::MouseEvent& event);
     73   void TitleMouseCaptureLost();
     74   bool TitleMouseDragged(const views::MouseEvent& event);
     75   bool PanelClientEvent(GdkEventClient* event);
     76   void OnFocusIn();
     77   void OnFocusOut();
     78 
     79   void UpdateTitleBar();
     80   void SetUrgent(bool urgent);
     81   void Close();
     82 
     83   void SetState(State state);
     84 
     85   bool urgent() { return urgent_; }
     86 
     87  private:
     88   class TitleContentView : public views::View,
     89                            public views::ButtonListener {
     90    public:
     91     explicit TitleContentView(PanelController* panelController);
     92     virtual ~TitleContentView();
     93 
     94     // Overridden from View:
     95     virtual void Layout() OVERRIDE;
     96     virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
     97     virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
     98     virtual void OnMouseCaptureLost() OVERRIDE;
     99     virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
    100 
    101     void OnFocusIn();
    102     void OnFocusOut();
    103     void OnClose();
    104 
    105     views::ImageView* title_icon() { return title_icon_; }
    106     views::Label* title_label() { return title_label_; }
    107     views::ImageButton* close_button() { return close_button_; }
    108 
    109     // ButtonListener methods.
    110     virtual void ButtonPressed(views::Button* sender,
    111                                const views::Event& event) OVERRIDE;
    112    private:
    113     views::ImageView* title_icon_;
    114     views::Label* title_label_;
    115     views::ImageButton* close_button_;
    116     PanelController* panel_controller_;
    117     DISALLOW_COPY_AND_ASSIGN(TitleContentView);
    118   };
    119 
    120   // Called from TitleContentView's ButtonPressed handler.
    121   void OnCloseButtonPressed();
    122 
    123   // Dispatches client events to PanelController instances
    124   static bool OnPanelClientEvent(
    125       GtkWidget* widget,
    126       GdkEventClient* event,
    127       PanelController* panel_controller);
    128 
    129   // Panel's delegate.
    130   Delegate* delegate_;
    131 
    132   // Gtk object for content.
    133   GtkWindow* panel_;
    134   // X id for content.
    135   XID panel_xid_;
    136 
    137   // Views object representing title.
    138   views::Widget* title_window_;
    139   // Gtk object representing title.
    140   GtkWidget* title_;
    141   // X id representing title.
    142   XID title_xid_;
    143 
    144   // Views object, holds title and close button.
    145   TitleContentView* title_content_;
    146 
    147   // Is the panel expanded or collapsed?
    148   bool expanded_;
    149 
    150   // Is the mouse button currently down?
    151   bool mouse_down_;
    152 
    153   // Cursor's absolute position when the mouse button was pressed.
    154   int mouse_down_abs_x_;
    155   int mouse_down_abs_y_;
    156 
    157   // Cursor's offset from the upper-left corner of the titlebar when the
    158   // mouse button was pressed.
    159   int mouse_down_offset_x_;
    160   int mouse_down_offset_y_;
    161 
    162   // Is the titlebar currently being dragged?  That is, has the cursor
    163   // moved more than kDragThreshold away from its starting position?
    164   bool dragging_;
    165 
    166   // GTK client event handler id.
    167   int client_event_handler_id_;
    168 
    169   // Focused state.
    170   bool focused_;
    171 
    172   // Urgent (highlight) state.
    173   bool urgent_;
    174 
    175   // Timestamp to prevent setting urgent immediately after clearing it.
    176   base::TimeTicks urgent_cleared_time_;
    177 
    178   DISALLOW_COPY_AND_ASSIGN(PanelController);
    179 };
    180 
    181 }  // namespace chromeos
    182 
    183 #endif  // CHROME_BROWSER_CHROMEOS_FRAME_PANEL_CONTROLLER_H_
    184