Update To 11.40.268.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/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "chrome/browser/ui/libgtk2ui/gtk2_signal.h"
16 #include "chrome/browser/ui/libgtk2ui/gtk2_signal_registrar.h"
17 #include "chrome/browser/ui/libgtk2ui/libgtk2ui_export.h"
18 #include "chrome/browser/ui/libgtk2ui/owned_widget_gtk2.h"
19 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h"
20 #include "ui/gfx/color_utils.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 class ScopedPangoFontDescription;
34 }
35
36 namespace libgtk2ui {
37 class Gtk2Border;
38 class Gtk2KeyBindingsHandler;
39 class Gtk2SignalRegistrar;
40 class GConfListener;
41
42 // Interface to GTK2 desktop features.
43 //
44 class Gtk2UI : public views::LinuxUI {
45  public:
46   Gtk2UI();
47   ~Gtk2UI() override;
48
49   typedef base::Callback<ui::NativeTheme*(aura::Window* window)>
50       NativeThemeGetter;
51
52   // Setters used by GConfListener:
53   void SetWindowButtonOrdering(
54       const std::vector<views::FrameButton>& leading_buttons,
55       const std::vector<views::FrameButton>& trailing_buttons);
56   void SetNonClientMiddleClickAction(NonClientMiddleClickAction action);
57
58   // Draws the GTK button border for state |gtk_state| onto a bitmap.
59   SkBitmap DrawGtkButtonBorder(int gtk_state,
60                                bool focused,
61                                bool call_to_action,
62                                int width,
63                                int height) const;
64
65   // ui::LinuxInputMethodContextFactory:
66   scoped_ptr<ui::LinuxInputMethodContext> CreateInputMethodContext(
67       ui::LinuxInputMethodContextDelegate* delegate) const override;
68
69   // gfx::LinuxFontDelegate:
70   gfx::FontRenderParams GetDefaultFontRenderParams() const override;
71   scoped_ptr<gfx::ScopedPangoFontDescription> GetDefaultPangoFontDescription()
72       const override;
73   double GetFontDPI() const override;
74
75   // ui::LinuxShellDialog:
76   ui::SelectFileDialog* CreateSelectFileDialog(
77       ui::SelectFileDialog::Listener* listener,
78       ui::SelectFilePolicy* policy) const override;
79
80   // ui::LinuxUI:
81   void Initialize() override;
82   gfx::Image GetThemeImageNamed(int id) const override;
83   bool GetColor(int id, SkColor* color) const override;
84   bool HasCustomImage(int id) const override;
85   SkColor GetFocusRingColor() const override;
86   SkColor GetThumbActiveColor() const override;
87   SkColor GetThumbInactiveColor() const override;
88   SkColor GetTrackColor() const override;
89   SkColor GetActiveSelectionBgColor() const override;
90   SkColor GetActiveSelectionFgColor() const override;
91   SkColor GetInactiveSelectionBgColor() const override;
92   SkColor GetInactiveSelectionFgColor() const override;
93   double GetCursorBlinkInterval() const override;
94   ui::NativeTheme* GetNativeTheme(aura::Window* window) const override;
95   void SetNativeThemeOverride(const NativeThemeGetter& callback) override;
96   bool GetDefaultUsesSystemTheme() const override;
97   void SetDownloadCount(int count) const override;
98   void SetProgressFraction(float percentage) const override;
99   bool IsStatusIconSupported() const override;
100   scoped_ptr<views::StatusIconLinux> CreateLinuxStatusIcon(
101       const gfx::ImageSkia& image,
102       const base::string16& tool_tip) const override;
103   gfx::Image GetIconForContentType(const std::string& content_type,
104                                    int size) const override;
105   scoped_ptr<views::Border> CreateNativeBorder(
106       views::LabelButton* owning_button,
107       scoped_ptr<views::LabelButtonBorder> border) override;
108   void AddWindowButtonOrderObserver(
109       views::WindowButtonOrderObserver* observer) override;
110   void RemoveWindowButtonOrderObserver(
111       views::WindowButtonOrderObserver* observer) override;
112   bool UnityIsRunning() override;
113   NonClientMiddleClickAction GetNonClientMiddleClickAction() override;
114   void NotifyWindowManagerStartupComplete() override;
115
116   // ui::TextEditKeybindingDelegate:
117   bool MatchEvent(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   // Extracts colors and tints from the GTK theme, both for the
133   // ThemeService interface and the colors we send to webkit.
134   void LoadGtkValues();
135
136   // Reads in explicit theme frame colors from the ChromeGtkFrame style class
137   // or generates them per our fallback algorithm.
138   GdkColor BuildFrameColors(GtkStyle* frame_style);
139
140   // Sets the underlying theme colors/tints from a GTK color.
141   void SetThemeColorFromGtk(int id, const GdkColor* color);
142   void SetThemeTintFromGtk(int id, const GdkColor* color);
143
144   // Creates and returns a frame color, either using |gtk_base| verbatim if
145   // non-NULL, or tinting |base| with |tint|. Also sets |color_id| and
146   // |tint_id| to the returned color.
147   GdkColor BuildAndSetFrameColor(const GdkColor* base,
148                                  const GdkColor* gtk_base,
149                                  const color_utils::HSL& tint,
150                                  int color_id,
151                                  int tint_id);
152
153   // Lazily generates each bitmap used in the gtk theme.
154   SkBitmap GenerateGtkThemeBitmap(int id) const;
155
156   // Creates a GTK+ version of IDR_THEME_FRAME. Instead of tinting, this
157   // creates a theme configurable gradient ending with |color_id| at the
158   // bottom, and |gradient_name| at the top if that color is specified in the
159   // theme.
160   SkBitmap GenerateFrameImage(int color_id,
161                               const char* gradient_name) const;
162
163   // Takes the base frame image |base_id| and tints it with |tint_id|.
164   SkBitmap GenerateTabImage(int base_id) const;
165
166   // Tints an icon based on tint.
167   SkBitmap GenerateTintedIcon(int base_id,
168                               const color_utils::HSL& tint) const;
169
170   // Renders a GTK icon as a SkBitmap, with prelight/active border if
171   // appropriate.
172   SkBitmap GenerateGTKIcon(int base_id) const;
173
174   // Renders a GTK button border the size of the image |sizing_idr| in
175   // |gtk_state|.
176   SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
177
178   // Returns the tint for buttons that contrasts with the normal window
179   // background color.
180   void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
181
182   // Returns a tint that's the color of the current normal text in an entry.
183   void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
184
185   // Returns a tint that's the color of the current highlighted text in an
186   // entry.
187   void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
188
189   // Gets a color for the background of the call to action button.
190   SkColor CallToActionBgColor(int gtk_state) const;
191
192   // Frees all calculated images and color data.
193   void ClearAllThemeData();
194
195   // Handles signal from GTK that our theme has been changed.
196   CHROMEGTK_CALLBACK_1(Gtk2UI, void, OnStyleSet, GtkStyle*);
197
198   GtkWidget* fake_window_;
199   GtkWidget* fake_frame_;
200   OwnedWidgetGtk fake_label_;
201   OwnedWidgetGtk fake_entry_;
202
203   // Tracks all the signals we have connected to on various widgets.
204   scoped_ptr<Gtk2SignalRegistrar> signals_;
205
206   // Tints and colors calculated by LoadGtkValues() that are given to the
207   // caller while |use_gtk_| is true.
208   ColorMap colors_;
209   TintMap tints_;
210
211   // Colors used to tint certain icons.
212   color_utils::HSL button_tint_;
213   color_utils::HSL entry_tint_;
214   color_utils::HSL selected_entry_tint_;
215
216   // Colors that we pass to WebKit. These are generated each time the theme
217   // changes.
218   SkColor focus_ring_color_;
219   SkColor thumb_active_color_;
220   SkColor thumb_inactive_color_;
221   SkColor track_color_;
222   SkColor active_selection_bg_color_;
223   SkColor active_selection_fg_color_;
224   SkColor inactive_selection_bg_color_;
225   SkColor inactive_selection_fg_color_;
226
227   // Pango description for the default UI font.
228   scoped_ptr<gfx::ScopedPangoFontDescription> default_font_description_;
229
230 #if defined(USE_GCONF)
231   // Currently, the only source of window button configuration. This will
232   // change if we ever have to support XFCE's configuration system or KDE's.
233   scoped_ptr<GConfListener> gconf_listener_;
234 #endif  // defined(USE_GCONF)
235
236   // If either of these vectors are non-empty, they represent the current
237   // window button configuration.
238   std::vector<views::FrameButton> leading_buttons_;
239   std::vector<views::FrameButton> trailing_buttons_;
240
241   scoped_ptr<Gtk2KeyBindingsHandler> key_bindings_handler_;
242
243   // Objects to notify when the window frame button order changes.
244   ObserverList<views::WindowButtonOrderObserver> observer_list_;
245
246   // Whether we should lower the window on a middle click to the non client
247   // area.
248   NonClientMiddleClickAction middle_click_action_;
249
250   // Image cache of lazily created images.
251   mutable ImageCache gtk_images_;
252
253   // Used to override the native theme for a window. If no override is provided
254   // or the callback returns NULL, Gtk2UI will default to a NativeThemeGtk2
255   // instance.
256   NativeThemeGetter native_theme_overrider_;
257
258   DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
259 };
260
261 }  // namespace libgtk2ui
262
263 // Access point to the GTK2 desktop system. This should be the only symbol that
264 // is exported in the library; everything else should be used through the
265 // interface, because eventually this .so will be loaded through dlopen at
266 // runtime so our main binary can conditionally load GTK2 or GTK3 or EFL or
267 // QT or whatever.
268 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
269
270 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_