Upstream version 5.34.104.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/libgtk2ui_export.h"
15 #include "chrome/browser/ui/libgtk2ui/owned_widget_gtk2.h"
16 #include "ui/gfx/color_utils.h"
17 #include "ui/gfx/geometry/insets.h"
18 #include "ui/views/linux_ui/linux_ui.h"
19 #include "ui/views/window/frame_buttons.h"
20
21 typedef struct _GdkColor GdkColor;
22 typedef struct _GtkBorder GtkBorder;
23 typedef struct _GtkStyle GtkStyle;
24 typedef struct _GtkWidget GtkWidget;
25
26 class SkBitmap;
27
28 namespace gfx {
29 class Image;
30 }
31
32 namespace libgtk2ui {
33 class Gtk2Border;
34 class GConfTitlebarListener;
35
36 // Interface to GTK2 desktop features.
37 //
38 class Gtk2UI : public views::LinuxUI {
39  public:
40   Gtk2UI();
41   virtual ~Gtk2UI();
42
43   void SetWindowButtonOrdering(
44     const std::vector<views::FrameButton>& leading_buttons,
45     const std::vector<views::FrameButton>& trailing_buttons);
46
47   // Draws the GTK button border for state |gtk_state| onto a bitmap.
48   SkBitmap DrawGtkButtonBorder(int gtk_state,
49                                bool focused,
50                                int width,
51                                int height) const;
52
53   // Returns the current insets for a button.
54   gfx::Insets GetButtonInsets() const;
55
56   // We keep track of live Gtk2Border objects since we must alert them to theme
57   // changes.
58   void AddGtkBorder(Gtk2Border* border);
59   void RemoveGtkBorder(Gtk2Border* border);
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 void SetUseSystemTheme(bool use_system_theme) OVERRIDE;
93   virtual bool GetUseSystemTheme() const OVERRIDE;
94   virtual bool GetDefaultUsesSystemTheme() const OVERRIDE;
95   virtual void SetDownloadCount(int count) const OVERRIDE;
96   virtual void SetProgressFraction(float percentage) const OVERRIDE;
97   virtual bool IsStatusIconSupported() const OVERRIDE;
98   virtual scoped_ptr<views::StatusIconLinux> CreateLinuxStatusIcon(
99       const gfx::ImageSkia& image,
100       const base::string16& tool_tip) const OVERRIDE;
101   virtual gfx::Image GetIconForContentType(
102       const std::string& content_type, int size) const OVERRIDE;
103   virtual scoped_ptr<views::Border> CreateNativeBorder(
104       views::LabelButton* owning_button,
105       scoped_ptr<views::Border> border) OVERRIDE;
106   virtual void AddWindowButtonOrderObserver(
107       views::WindowButtonOrderObserver* observer) OVERRIDE;
108   virtual void RemoveWindowButtonOrderObserver(
109       views::WindowButtonOrderObserver* observer) OVERRIDE;
110   virtual bool UnityIsRunning() OVERRIDE;
111   virtual void NotifyWindowManagerStartupComplete() OVERRIDE;
112
113  private:
114   typedef std::map<int, SkColor> ColorMap;
115   typedef std::map<int, color_utils::HSL> TintMap;
116   typedef std::map<int, gfx::Image> ImageCache;
117
118   // This method returns the colors webkit will use for the scrollbars. When no
119   // colors are specified by the GTK+ theme, this function averages of the
120   // thumb part and of the track colors.
121   void GetScrollbarColors(GdkColor* thumb_active_color,
122                           GdkColor* thumb_inactive_color,
123                           GdkColor* track_color);
124
125   // Gets the name of the current icon theme and passes it to our low level XDG
126   // integration.
127   void SetXDGIconTheme();
128
129   // Extracts colors and tints from the GTK theme, both for the
130   // ThemeService interface and the colors we send to webkit.
131   void LoadGtkValues();
132
133   // Reads in explicit theme frame colors from the ChromeGtkFrame style class
134   // or generates them per our fallback algorithm.
135   GdkColor BuildFrameColors(GtkStyle* frame_style);
136
137   // Sets the underlying theme colors/tints from a GTK color.
138   void SetThemeColorFromGtk(int id, const GdkColor* color);
139   void SetThemeTintFromGtk(int id, const GdkColor* color);
140
141   // Creates and returns a frame color, either using |gtk_base| verbatim if
142   // non-NULL, or tinting |base| with |tint|. Also sets |color_id| and
143   // |tint_id| to the returned color.
144   GdkColor BuildAndSetFrameColor(const GdkColor* base,
145                                  const GdkColor* gtk_base,
146                                  const color_utils::HSL& tint,
147                                  int color_id,
148                                  int tint_id);
149
150   // Lazily generates each bitmap used in the gtk theme.
151   SkBitmap GenerateGtkThemeBitmap(int id) const;
152
153   // Creates a GTK+ version of IDR_THEME_FRAME. Instead of tinting, this
154   // creates a theme configurable gradient ending with |color_id| at the
155   // bottom, and |gradient_name| at the top if that color is specified in the
156   // theme.
157   SkBitmap GenerateFrameImage(int color_id,
158                               const char* gradient_name) const;
159
160   // Takes the base frame image |base_id| and tints it with |tint_id|.
161   SkBitmap GenerateTabImage(int base_id) const;
162
163   // Tints an icon based on tint.
164   SkBitmap GenerateTintedIcon(int base_id,
165                               const color_utils::HSL& tint) const;
166
167   // Renders a GTK icon as a SkBitmap, with prelight/active border if
168   // appropriate.
169   SkBitmap GenerateGTKIcon(int base_id) const;
170
171   // Renders a GTK button border the size of the image |sizing_idr| in
172   // |gtk_state|.
173   SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
174
175   // Returns the tint for buttons that contrasts with the normal window
176   // background color.
177   void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
178
179   // Returns a tint that's the color of the current normal text in an entry.
180   void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
181
182   // Returns a tint that's the color of the current highlighted text in an
183   // entry.
184   void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
185
186   // Create a GTK window and button and queries what "default-border" is, which
187   // corresponds with our insets.
188   void UpdateButtonInsets();
189
190   // Frees all calculated images and color data.
191   void ClearAllThemeData();
192
193   GtkWidget* fake_window_;
194   GtkWidget* fake_frame_;
195   OwnedWidgetGtk fake_label_;
196   OwnedWidgetGtk fake_entry_;
197
198   // Tints and colors calculated by LoadGtkValues() that are given to the
199   // caller while |use_gtk_| is true.
200   ColorMap colors_;
201   TintMap tints_;
202
203   // Colors used to tint certain icons.
204   color_utils::HSL button_tint_;
205   color_utils::HSL entry_tint_;
206   color_utils::HSL selected_entry_tint_;
207
208   // Colors that we pass to WebKit. These are generated each time the theme
209   // changes.
210   SkColor focus_ring_color_;
211   SkColor thumb_active_color_;
212   SkColor thumb_inactive_color_;
213   SkColor track_color_;
214   SkColor active_selection_bg_color_;
215   SkColor active_selection_fg_color_;
216   SkColor inactive_selection_bg_color_;
217   SkColor inactive_selection_fg_color_;
218
219   gfx::Insets button_insets_;
220
221 #if defined(USE_GCONF)
222   // Currently, the only source of window button configuration. This will
223   // change if we ever have to support XFCE's configuration system or KDE's.
224   scoped_ptr<GConfTitlebarListener> titlebar_listener_;
225 #endif  // defined(USE_GCONF)
226
227   // If either of these vectors are non-empty, they represent the current
228   // window button configuration.
229   std::vector<views::FrameButton> leading_buttons_;
230   std::vector<views::FrameButton> trailing_buttons_;
231
232   // Objects to notify when the window frame button order changes.
233   ObserverList<views::WindowButtonOrderObserver> observer_list_;
234
235   // Borders to notify when the theme state changes.
236   ObserverList<Gtk2Border> border_list_;
237
238   // Image cache of lazily created images.
239   mutable ImageCache gtk_images_;
240
241   // Whether to use the Gtk2 version of the native theme.
242   bool use_gtk_;
243
244   DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
245 };
246
247 }  // namespace libgtk2ui
248
249 // Access point to the GTK2 desktop system. This should be the only symbol that
250 // is exported in the library; everything else should be used through the
251 // interface, because eventually this .so will be loaded through dlopen at
252 // runtime so our main binary can conditionally load GTK2 or GTK3 or EFL or
253 // QT or whatever.
254 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
255
256 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_