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.
5 #ifndef CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_
6 #define CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_
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"
21 typedef struct _GdkColor GdkColor;
22 typedef struct _GtkBorder GtkBorder;
23 typedef struct _GtkStyle GtkStyle;
24 typedef struct _GtkWidget GtkWidget;
34 class GConfTitlebarListener;
36 // Interface to GTK2 desktop features.
38 class Gtk2UI : public views::LinuxUI {
43 void SetWindowButtonOrdering(
44 const std::vector<views::FrameButton>& leading_buttons,
45 const std::vector<views::FrameButton>& trailing_buttons);
47 // Draws the GTK button border for state |gtk_state| onto a bitmap.
48 SkBitmap DrawGtkButtonBorder(int gtk_state,
53 // Returns the current insets for a button.
54 gfx::Insets GetButtonInsets() const;
56 // We keep track of live Gtk2Border objects since we must alert them to theme
58 void AddGtkBorder(Gtk2Border* border);
59 void RemoveGtkBorder(Gtk2Border* border);
61 // ui::LinuxInputMethodContextFactory:
62 virtual scoped_ptr<ui::LinuxInputMethodContext> CreateInputMethodContext(
63 ui::LinuxInputMethodContextDelegate* delegate) const OVERRIDE;
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;
72 // ui::LinuxShellDialog:
73 virtual ui::SelectFileDialog* CreateSelectFileDialog(
74 ui::SelectFileDialog::Listener* listener,
75 ui::SelectFilePolicy* policy) const OVERRIDE;
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;
114 typedef std::map<int, SkColor> ColorMap;
115 typedef std::map<int, color_utils::HSL> TintMap;
116 typedef std::map<int, gfx::Image> ImageCache;
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);
125 // Gets the name of the current icon theme and passes it to our low level XDG
127 void SetXDGIconTheme();
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();
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);
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);
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,
150 // Lazily generates each bitmap used in the gtk theme.
151 SkBitmap GenerateGtkThemeBitmap(int id) const;
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
157 SkBitmap GenerateFrameImage(int color_id,
158 const char* gradient_name) const;
160 // Takes the base frame image |base_id| and tints it with |tint_id|.
161 SkBitmap GenerateTabImage(int base_id) const;
163 // Tints an icon based on tint.
164 SkBitmap GenerateTintedIcon(int base_id,
165 const color_utils::HSL& tint) const;
167 // Renders a GTK icon as a SkBitmap, with prelight/active border if
169 SkBitmap GenerateGTKIcon(int base_id) const;
171 // Renders a GTK button border the size of the image |sizing_idr| in
173 SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
175 // Returns the tint for buttons that contrasts with the normal window
177 void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
179 // Returns a tint that's the color of the current normal text in an entry.
180 void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
182 // Returns a tint that's the color of the current highlighted text in an
184 void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
186 // Create a GTK window and button and queries what "default-border" is, which
187 // corresponds with our insets.
188 void UpdateButtonInsets();
190 // Frees all calculated images and color data.
191 void ClearAllThemeData();
193 GtkWidget* fake_window_;
194 GtkWidget* fake_frame_;
195 OwnedWidgetGtk fake_label_;
196 OwnedWidgetGtk fake_entry_;
198 // Tints and colors calculated by LoadGtkValues() that are given to the
199 // caller while |use_gtk_| is true.
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_;
208 // Colors that we pass to WebKit. These are generated each time the theme
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_;
219 gfx::Insets button_insets_;
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)
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_;
232 // Objects to notify when the window frame button order changes.
233 ObserverList<views::WindowButtonOrderObserver> observer_list_;
235 // Borders to notify when the theme state changes.
236 ObserverList<Gtk2Border> border_list_;
238 // Image cache of lazily created images.
239 mutable ImageCache gtk_images_;
241 // Whether to use the Gtk2 version of the native theme.
244 DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
247 } // namespace libgtk2ui
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
254 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
256 #endif // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_