• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/views/linux_ui/linux_ui.h"
18 #include "ui/views/window/frame_buttons.h"
19 
20 typedef struct _GdkColor GdkColor;
21 typedef struct _GtkStyle GtkStyle;
22 typedef struct _GtkWidget GtkWidget;
23 
24 class SkBitmap;
25 
26 namespace gfx {
27 class Image;
28 }
29 
30 namespace libgtk2ui {
31 class GConfTitlebarListener;
32 
33 // Interface to GTK2 desktop features.
34 //
35 class Gtk2UI : public views::LinuxUI {
36  public:
37   Gtk2UI();
38   virtual ~Gtk2UI();
39 
40   void SetWindowButtonOrdering(
41     const std::vector<views::FrameButton>& leading_buttons,
42     const std::vector<views::FrameButton>& trailing_buttons);
43 
44   // ui::LinuxInputMethodContextFactory:
45   virtual scoped_ptr<ui::LinuxInputMethodContext> CreateInputMethodContext(
46       ui::LinuxInputMethodContextDelegate* delegate) const OVERRIDE;
47 
48   // ui::LinuxShellDialog:
49   virtual ui::SelectFileDialog* CreateSelectFileDialog(
50       ui::SelectFileDialog::Listener* listener,
51       ui::SelectFilePolicy* policy) const OVERRIDE;
52 
53   // ui::LinuxUI:
54   virtual void Initialize() OVERRIDE;
55   virtual gfx::Image GetThemeImageNamed(int id) const OVERRIDE;
56   virtual bool GetColor(int id, SkColor* color) const OVERRIDE;
57   virtual bool HasCustomImage(int id) const OVERRIDE;
58   virtual SkColor GetFocusRingColor() const OVERRIDE;
59   virtual SkColor GetThumbActiveColor() const OVERRIDE;
60   virtual SkColor GetThumbInactiveColor() const OVERRIDE;
61   virtual SkColor GetTrackColor() const OVERRIDE;
62   virtual SkColor GetActiveSelectionBgColor() const OVERRIDE;
63   virtual SkColor GetActiveSelectionFgColor() const OVERRIDE;
64   virtual SkColor GetInactiveSelectionBgColor() const OVERRIDE;
65   virtual SkColor GetInactiveSelectionFgColor() const OVERRIDE;
66   virtual double GetCursorBlinkInterval() const OVERRIDE;
67   virtual ui::NativeTheme* GetNativeTheme() const OVERRIDE;
68   virtual void SetUseSystemTheme(bool use_system_theme) OVERRIDE;
69   virtual bool GetDefaultUsesSystemTheme() const OVERRIDE;
70   virtual void SetDownloadCount(int count) const OVERRIDE;
71   virtual void SetProgressFraction(float percentage) const OVERRIDE;
72   virtual bool IsStatusIconSupported() const OVERRIDE;
73   virtual scoped_ptr<views::StatusIconLinux> CreateLinuxStatusIcon(
74       const gfx::ImageSkia& image,
75       const base::string16& tool_tip) const OVERRIDE;
76   virtual gfx::Image GetIconForContentType(
77       const std::string& content_type, int size) const OVERRIDE;
78   virtual void AddWindowButtonOrderObserver(
79       views::WindowButtonOrderObserver* observer) OVERRIDE;
80   virtual void RemoveWindowButtonOrderObserver(
81       views::WindowButtonOrderObserver* observer) OVERRIDE;
82   virtual bool UnityIsRunning() OVERRIDE;
83 
84  private:
85   typedef std::map<int, SkColor> ColorMap;
86   typedef std::map<int, color_utils::HSL> TintMap;
87   typedef std::map<int, gfx::Image> ImageCache;
88 
89   // This method returns the colors webkit will use for the scrollbars. When no
90   // colors are specified by the GTK+ theme, this function averages of the
91   // thumb part and of the track colors.
92   void GetScrollbarColors(GdkColor* thumb_active_color,
93                           GdkColor* thumb_inactive_color,
94                           GdkColor* track_color);
95 
96   // Gets the name of the current icon theme and passes it to our low level XDG
97   // integration.
98   void SetXDGIconTheme();
99 
100   // Extracts colors and tints from the GTK theme, both for the
101   // ThemeService interface and the colors we send to webkit.
102   void LoadGtkValues();
103 
104   // Reads in explicit theme frame colors from the ChromeGtkFrame style class
105   // or generates them per our fallback algorithm.
106   GdkColor BuildFrameColors(GtkStyle* frame_style);
107 
108   // Sets the underlying theme colors/tints from a GTK color.
109   void SetThemeColorFromGtk(int id, const GdkColor* color);
110   void SetThemeTintFromGtk(int id, const GdkColor* color);
111 
112   // Creates and returns a frame color, either using |gtk_base| verbatim if
113   // non-NULL, or tinting |base| with |tint|. Also sets |color_id| and
114   // |tint_id| to the returned color.
115   GdkColor BuildAndSetFrameColor(const GdkColor* base,
116                                  const GdkColor* gtk_base,
117                                  const color_utils::HSL& tint,
118                                  int color_id,
119                                  int tint_id);
120 
121   // Lazily generates each bitmap used in the gtk theme.
122   SkBitmap GenerateGtkThemeBitmap(int id) const;
123 
124   // Creates a GTK+ version of IDR_THEME_FRAME. Instead of tinting, this
125   // creates a theme configurable gradient ending with |color_id| at the
126   // bottom, and |gradient_name| at the top if that color is specified in the
127   // theme.
128   SkBitmap GenerateFrameImage(int color_id,
129                               const char* gradient_name) const;
130 
131   // Takes the base frame image |base_id| and tints it with |tint_id|.
132   SkBitmap GenerateTabImage(int base_id) const;
133 
134   // Tints an icon based on tint.
135   SkBitmap GenerateTintedIcon(int base_id,
136                               const color_utils::HSL& tint) const;
137 
138   // Renders a GTK icon as a SkBitmap, with prelight/active border if
139   // appropriate.
140   SkBitmap GenerateGTKIcon(int base_id) const;
141 
142   // Renders a GTK button border the size of the image |sizing_idr| in
143   // |gtk_state|.
144   SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
145 
146   // Returns the tint for buttons that contrasts with the normal window
147   // background color.
148   void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
149 
150   // Returns a tint that's the color of the current normal text in an entry.
151   void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
152 
153   // Returns a tint that's the color of the current highlighted text in an
154   // entry.
155   void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
156 
157   // Draws the GTK button border for state |gtk_state| onto a bitmap.
158   SkBitmap DrawGtkButtonBorder(int gtk_state, int width, int height) const;
159 
160   // Frees all calculated images and color data.
161   void ClearAllThemeData();
162 
163   GtkWidget* fake_window_;
164   GtkWidget* fake_frame_;
165   OwnedWidgetGtk fake_label_;
166   OwnedWidgetGtk fake_entry_;
167 
168   // Tints and colors calculated by LoadGtkValues() that are given to the
169   // caller while |use_gtk_| is true.
170   ColorMap colors_;
171   TintMap tints_;
172 
173   // Colors used to tint certain icons.
174   color_utils::HSL button_tint_;
175   color_utils::HSL entry_tint_;
176   color_utils::HSL selected_entry_tint_;
177 
178   // Colors that we pass to WebKit. These are generated each time the theme
179   // changes.
180   SkColor focus_ring_color_;
181   SkColor thumb_active_color_;
182   SkColor thumb_inactive_color_;
183   SkColor track_color_;
184   SkColor active_selection_bg_color_;
185   SkColor active_selection_fg_color_;
186   SkColor inactive_selection_bg_color_;
187   SkColor inactive_selection_fg_color_;
188 
189 #if defined(USE_GCONF)
190   // Currently, the only source of window button configuration. This will
191   // change if we ever have to support XFCE's configuration system or KDE's.
192   scoped_ptr<GConfTitlebarListener> titlebar_listener_;
193 #endif  // defined(USE_GCONF)
194 
195   // If either of these vectors are non-empty, they represent the current
196   // window button configuration.
197   std::vector<views::FrameButton> leading_buttons_;
198   std::vector<views::FrameButton> trailing_buttons_;
199 
200   // Objects to notify when the window frame button order changes.
201   ObserverList<views::WindowButtonOrderObserver> observer_list_;
202 
203   // Image cache of lazily created images.
204   mutable ImageCache gtk_images_;
205 
206   // Whether to use the Gtk2 version of the native theme.
207   bool use_gtk_;
208 
209   DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
210 };
211 
212 }  // namespace libgtk2ui
213 
214 // Access point to the GTK2 desktop system. This should be the only symbol that
215 // is exported in the library; everything else should be used through the
216 // interface, because eventually this .so will be loaded through dlopen at
217 // runtime so our main binary can conditionally load GTK2 or GTK3 or EFL or
218 // QT or whatever.
219 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
220 
221 #endif  // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_
222