Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / libgtk2ui / gtk2_ui.h
1 // Copyright (c) 2013 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_LIBGTK2UI_GTK2_UI_H_
6 #define CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/observer_list.h"
14 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h"
15 #include "chrome/browser/ui/libgtk2ui/gtk2_signal_registrar.h"
16 #include "chrome/browser/ui/libgtk2ui/libgtk2ui_export.h"
17 #include "chrome/browser/ui/libgtk2ui/owned_widget_gtk2.h"
18 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h"
19 #include "ui/gfx/color_utils.h"
20 #include "ui/gfx/geometry/insets.h"
21 #include "ui/views/linux_ui/linux_ui.h"
22 #include "ui/views/window/frame_buttons.h"
23
24 typedef struct _GdkColor GdkColor;
25 typedef struct _GtkBorder GtkBorder;
26 typedef struct _GtkStyle GtkStyle;
27 typedef struct _GtkWidget GtkWidget;
28
29 class SkBitmap;
30
31 namespace gfx {
32 class Image;
33 }
34
35 namespace libgtk2ui {
36 class Gtk2Border;
37 class Gtk2KeyBindingsHandler;
38 class Gtk2SignalRegistrar;
39 class GConfTitlebarListener;
40
41 // Interface to GTK2 desktop features.
42 //
43 class Gtk2UI : public views::LinuxUI {
44  public:
45   Gtk2UI();
46   virtual ~Gtk2UI();
47
48   void SetWindowButtonOrdering(
49     const std::vector<views::FrameButton>& leading_buttons,
50     const std::vector<views::FrameButton>& trailing_buttons);
51
52   // Draws the GTK button border for state |gtk_state| onto a bitmap.
53   SkBitmap DrawGtkButtonBorder(int gtk_state,
54                                bool focused,
55                                int width,
56                                int height) const;
57
58   // Returns the current insets for a button.
59   gfx::Insets GetButtonInsets() const;
60
61   // ui::LinuxInputMethodContextFactory:
62   virtual scoped_ptr<ui::LinuxInputMethodContext> CreateInputMethodContext(
63       ui::LinuxInputMethodContextDelegate* delegate) const OVERRIDE;
64
65   // gfx::LinuxFontDelegate:
66   virtual bool UseAntialiasing() const OVERRIDE;
67   virtual gfx::FontRenderParams::Hinting GetHintingStyle() const OVERRIDE;
68   virtual gfx::FontRenderParams::SubpixelRendering
69       GetSubpixelRenderingStyle() const OVERRIDE;
70   virtual std::string GetDefaultFontName() const OVERRIDE;
71
72   // ui::LinuxShellDialog:
73   virtual ui::SelectFileDialog* CreateSelectFileDialog(
74       ui::SelectFileDialog::Listener* listener,
75       ui::SelectFilePolicy* policy) const OVERRIDE;
76
77   // ui::LinuxUI:
78   virtual void Initialize() OVERRIDE;
79   virtual gfx::Image GetThemeImageNamed(int id) const OVERRIDE;
80   virtual bool GetColor(int id, SkColor* color) const OVERRIDE;
81   virtual bool HasCustomImage(int id) const OVERRIDE;
82   virtual SkColor GetFocusRingColor() const OVERRIDE;
83   virtual SkColor GetThumbActiveColor() const OVERRIDE;
84   virtual SkColor GetThumbInactiveColor() const OVERRIDE;
85   virtual SkColor GetTrackColor() const OVERRIDE;
86   virtual SkColor GetActiveSelectionBgColor() const OVERRIDE;
87   virtual SkColor GetActiveSelectionFgColor() const OVERRIDE;
88   virtual SkColor GetInactiveSelectionBgColor() const OVERRIDE;
89   virtual SkColor GetInactiveSelectionFgColor() const OVERRIDE;
90   virtual double GetCursorBlinkInterval() const OVERRIDE;
91   virtual ui::NativeTheme* GetNativeTheme() const OVERRIDE;
92   virtual bool GetDefaultUsesSystemTheme() const OVERRIDE;
93   virtual void SetDownloadCount(int count) const OVERRIDE;
94   virtual void SetProgressFraction(float percentage) const OVERRIDE;
95   virtual bool IsStatusIconSupported() const OVERRIDE;
96   virtual scoped_ptr<views::StatusIconLinux> CreateLinuxStatusIcon(
97       const gfx::ImageSkia& image,
98       const base::string16& tool_tip) const OVERRIDE;
99   virtual gfx::Image GetIconForContentType(
100       const std::string& content_type, int size) const OVERRIDE;
101   virtual scoped_ptr<views::Border> CreateNativeBorder(
102       views::LabelButton* owning_button,
103       scoped_ptr<views::Border> border) OVERRIDE;
104   virtual void AddWindowButtonOrderObserver(
105       views::WindowButtonOrderObserver* observer) OVERRIDE;
106   virtual void RemoveWindowButtonOrderObserver(
107       views::WindowButtonOrderObserver* observer) OVERRIDE;
108   virtual void AddNativeThemeChangeObserver(
109       views::NativeThemeChangeObserver* observer) OVERRIDE;
110   virtual void RemoveNativeThemeChangeObserver(
111       views::NativeThemeChangeObserver* observer) OVERRIDE;
112   virtual bool UnityIsRunning() OVERRIDE;
113   virtual void NotifyWindowManagerStartupComplete() OVERRIDE;
114
115   // ui::TextEditKeybindingDelegate:
116   virtual bool MatchEvent(
117       const ui::Event& event,
118       std::vector<ui::TextEditCommandAuraLinux>* commands) OVERRIDE;
119
120  private:
121   typedef std::map<int, SkColor> ColorMap;
122   typedef std::map<int, color_utils::HSL> TintMap;
123   typedef std::map<int, gfx::Image> ImageCache;
124
125   // This method returns the colors webkit will use for the scrollbars. When no
126   // colors are specified by the GTK+ theme, this function averages of the
127   // thumb part and of the track colors.
128   void GetScrollbarColors(GdkColor* thumb_active_color,
129                           GdkColor* thumb_inactive_color,
130                           GdkColor* track_color);
131
132   // Gets the name of the current icon theme and passes it to our low level XDG
133   // integration.
134   void SetXDGIconTheme();
135
136   // Extracts colors and tints from the GTK theme, both for the
137   // ThemeService interface and the colors we send to webkit.
138   void LoadGtkValues();
139
140   // Reads in explicit theme frame colors from the ChromeGtkFrame style class
141   // or generates them per our fallback algorithm.
142   GdkColor BuildFrameColors(GtkStyle* frame_style);
143
144   // Sets the underlying theme colors/tints from a GTK color.
145   void SetThemeColorFromGtk(int id, const GdkColor* color);
146   void SetThemeTintFromGtk(int id, const GdkColor* color);
147
148   // Creates and returns a frame color, either using |gtk_base| verbatim if
149   // non-NULL, or tinting |base| with |tint|. Also sets |color_id| and
150   // |tint_id| to the returned color.
151   GdkColor BuildAndSetFrameColor(const GdkColor* base,
152                                  const GdkColor* gtk_base,
153                                  const color_utils::HSL& tint,
154                                  int color_id,
155                                  int tint_id);
156
157   // Lazily generates each bitmap used in the gtk theme.
158   SkBitmap GenerateGtkThemeBitmap(int id) const;
159
160   // Creates a GTK+ version of IDR_THEME_FRAME. Instead of tinting, this
161   // creates a theme configurable gradient ending with |color_id| at the
162   // bottom, and |gradient_name| at the top if that color is specified in the
163   // theme.
164   SkBitmap GenerateFrameImage(int color_id,
165                               const char* gradient_name) const;
166
167   // Takes the base frame image |base_id| and tints it with |tint_id|.
168   SkBitmap GenerateTabImage(int base_id) const;
169
170   // Tints an icon based on tint.
171   SkBitmap GenerateTintedIcon(int base_id,
172                               const color_utils::HSL& tint) const;
173
174   // Renders a GTK icon as a SkBitmap, with prelight/active border if
175   // appropriate.
176   SkBitmap GenerateGTKIcon(int base_id) const;
177
178   // Renders a GTK button border the size of the image |sizing_idr| in
179   // |gtk_state|.
180   SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
181
182   // Returns the tint for buttons that contrasts with the normal window
183   // background color.
184   void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
185
186   // Returns a tint that's the color of the current normal text in an entry.
187   void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
188
189   // Returns a tint that's the color of the current highlighted text in an
190   // entry.
191   void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
192
193   // Create a GTK window and button and queries what "default-border" is, which
194   // corresponds with our insets.
195   void UpdateButtonInsets();
196
197   // Frees all calculated images and color data.
198   void ClearAllThemeData();
199
200   // Handles signal from GTK that our theme has been changed.
201   CHROMEGTK_CALLBACK_1(Gtk2UI, void, OnStyleSet, GtkStyle*);
202
203   GtkWidget* fake_window_;
204   GtkWidget* fake_frame_;
205   OwnedWidgetGtk fake_label_;
206   OwnedWidgetGtk fake_entry_;
207
208   // Tracks all the signals we have connected to on various widgets.
209   scoped_ptr<Gtk2SignalRegistrar> signals_;
210
211   // Tints and colors calculated by LoadGtkValues() that are given to the
212   // caller while |use_gtk_| is true.
213   ColorMap colors_;
214   TintMap tints_;
215
216   // Colors used to tint certain icons.
217   color_utils::HSL button_tint_;
218   color_utils::HSL entry_tint_;
219   color_utils::HSL selected_entry_tint_;
220
221   // Colors that we pass to WebKit. These are generated each time the theme
222   // changes.
223   SkColor focus_ring_color_;
224   SkColor thumb_active_color_;
225   SkColor thumb_inactive_color_;
226   SkColor track_color_;
227   SkColor active_selection_bg_color_;
228   SkColor active_selection_fg_color_;
229   SkColor inactive_selection_bg_color_;
230   SkColor inactive_selection_fg_color_;
231
232   gfx::Insets button_insets_;
233
234 #if defined(USE_GCONF)
235   // Currently, the only source of window button configuration. This will
236   // change if we ever have to support XFCE's configuration system or KDE's.
237   scoped_ptr<GConfTitlebarListener> titlebar_listener_;
238 #endif  // defined(USE_GCONF)
239
240   // If either of these vectors are non-empty, they represent the current
241   // window button configuration.
242   std::vector<views::FrameButton> leading_buttons_;
243   std::vector<views::FrameButton> trailing_buttons_;
244
245   scoped_ptr<Gtk2KeyBindingsHandler> key_bindings_handler_;
246
247   // Objects to notify when the window frame button order changes.
248   ObserverList<views::WindowButtonOrderObserver> observer_list_;
249
250   // Observers to notify when the theme state changes.
251   ObserverList<views::NativeThemeChangeObserver> theme_change_observers_;
252
253   // Image cache of lazily created images.
254   mutable ImageCache gtk_images_;
255
256   // Whether to use the Gtk2 version of the native theme.
257   bool use_gtk_;
258
259   DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
260 };
261
262 }  // namespace libgtk2ui
263
264 // Access point to the GTK2 desktop system. This should be the only symbol that
265 // is exported in the library; everything else should be used through the
266 // interface, because eventually this .so will be loaded through dlopen at
267 // runtime so our main binary can conditionally load GTK2 or GTK3 or EFL or
268 // QT or whatever.
269 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
270
271 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_