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/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"
24 typedef struct _GdkColor GdkColor;
25 typedef struct _GtkBorder GtkBorder;
26 typedef struct _GtkStyle GtkStyle;
27 typedef struct _GtkWidget GtkWidget;
33 class ScopedPangoFontDescription;
38 class Gtk2KeyBindingsHandler;
39 class Gtk2SignalRegistrar;
42 // Interface to GTK2 desktop features.
44 class Gtk2UI : public views::LinuxUI {
49 typedef base::Callback<ui::NativeTheme*(aura::Window* window)>
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);
58 // Draws the GTK button border for state |gtk_state| onto a bitmap.
59 SkBitmap DrawGtkButtonBorder(int gtk_state,
65 // ui::LinuxInputMethodContextFactory:
66 scoped_ptr<ui::LinuxInputMethodContext> CreateInputMethodContext(
67 ui::LinuxInputMethodContextDelegate* delegate) const override;
69 // gfx::LinuxFontDelegate:
70 gfx::FontRenderParams GetDefaultFontRenderParams() const override;
71 scoped_ptr<gfx::ScopedPangoFontDescription> GetDefaultPangoFontDescription()
73 double GetFontDPI() const override;
75 // ui::LinuxShellDialog:
76 ui::SelectFileDialog* CreateSelectFileDialog(
77 ui::SelectFileDialog::Listener* listener,
78 ui::SelectFilePolicy* policy) const override;
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;
116 // ui::TextEditKeybindingDelegate:
117 bool MatchEvent(const ui::Event& event,
118 std::vector<ui::TextEditCommandAuraLinux>* commands) override;
121 typedef std::map<int, SkColor> ColorMap;
122 typedef std::map<int, color_utils::HSL> TintMap;
123 typedef std::map<int, gfx::Image> ImageCache;
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);
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();
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);
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);
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,
153 // Lazily generates each bitmap used in the gtk theme.
154 SkBitmap GenerateGtkThemeBitmap(int id) const;
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
160 SkBitmap GenerateFrameImage(int color_id,
161 const char* gradient_name) const;
163 // Takes the base frame image |base_id| and tints it with |tint_id|.
164 SkBitmap GenerateTabImage(int base_id) const;
166 // Tints an icon based on tint.
167 SkBitmap GenerateTintedIcon(int base_id,
168 const color_utils::HSL& tint) const;
170 // Renders a GTK icon as a SkBitmap, with prelight/active border if
172 SkBitmap GenerateGTKIcon(int base_id) const;
174 // Renders a GTK button border the size of the image |sizing_idr| in
176 SkBitmap GenerateToolbarBezel(int gtk_state, int sizing_idr) const;
178 // Returns the tint for buttons that contrasts with the normal window
180 void GetNormalButtonTintHSL(color_utils::HSL* tint) const;
182 // Returns a tint that's the color of the current normal text in an entry.
183 void GetNormalEntryForegroundHSL(color_utils::HSL* tint) const;
185 // Returns a tint that's the color of the current highlighted text in an
187 void GetSelectedEntryForegroundHSL(color_utils::HSL* tint) const;
189 // Gets a color for the background of the call to action button.
190 SkColor CallToActionBgColor(int gtk_state) const;
192 // Frees all calculated images and color data.
193 void ClearAllThemeData();
195 // Handles signal from GTK that our theme has been changed.
196 CHROMEGTK_CALLBACK_1(Gtk2UI, void, OnStyleSet, GtkStyle*);
198 GtkWidget* fake_window_;
199 GtkWidget* fake_frame_;
200 OwnedWidgetGtk fake_label_;
201 OwnedWidgetGtk fake_entry_;
203 // Tracks all the signals we have connected to on various widgets.
204 scoped_ptr<Gtk2SignalRegistrar> signals_;
206 // Tints and colors calculated by LoadGtkValues() that are given to the
207 // caller while |use_gtk_| is true.
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_;
216 // Colors that we pass to WebKit. These are generated each time the theme
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_;
227 // Pango description for the default UI font.
228 scoped_ptr<gfx::ScopedPangoFontDescription> default_font_description_;
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)
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_;
241 scoped_ptr<Gtk2KeyBindingsHandler> key_bindings_handler_;
243 // Objects to notify when the window frame button order changes.
244 ObserverList<views::WindowButtonOrderObserver> observer_list_;
246 // Whether we should lower the window on a middle click to the non client
248 NonClientMiddleClickAction middle_click_action_;
250 // Image cache of lazily created images.
251 mutable ImageCache gtk_images_;
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
256 NativeThemeGetter native_theme_overrider_;
258 DISALLOW_COPY_AND_ASSIGN(Gtk2UI);
261 } // namespace libgtk2ui
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
268 LIBGTK2UI_EXPORT views::LinuxUI* BuildGtk2UI();
270 #endif // CHROME_BROWSER_UI_LIBGTK2UI_GTK2_UI_H_