[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 1 | // Copyright (c) 2009 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_GTK_CUSTOM_BUTTON_H_ |
| 6 | #define CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_ |
| 7 | |
| 8 | #include <gtk/gtk.h> |
| 9 | |
| 10 | #include <string> |
| 11 | |
[email protected] | c12fac9 | 2009-05-05 20:27:00 | [diff] [blame] | 12 | #include "base/gfx/rect.h" |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame] | 13 | #include "base/scoped_ptr.h" |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 14 | #include "chrome/common/notification_observer.h" |
| 15 | #include "chrome/common/notification_registrar.h" |
[email protected] | 6d8924d | 2009-04-06 21:21:34 | [diff] [blame] | 16 | #include "chrome/common/owned_widget_gtk.h" |
[email protected] | 4f14f81 | 2009-07-24 17:39:34 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkBitmap.h" |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 18 | |
[email protected] | eafb5b7b | 2009-09-10 18:16:15 | [diff] [blame] | 19 | class CairoCachedSurface; |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 20 | class GtkThemeProvider; |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 21 | |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame] | 22 | // These classes implement two kinds of custom-drawn buttons. They're |
| 23 | // used on the toolbar and the bookmarks bar. |
| 24 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 25 | // CustomDrawButtonBase provides the base for building a custom drawn button. |
| 26 | // It handles managing the pixbufs containing all the static images used to draw |
| 27 | // the button. It also manages painting these pixbufs. |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 28 | class CustomDrawButtonBase : public NotificationObserver { |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 29 | public: |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 30 | // If the images come from ResourceBundle rather than the theme provider, |
| 31 | // pass in NULL for |theme_provider|. |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 32 | CustomDrawButtonBase(GtkThemeProvider* theme_provider, |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 33 | int normal_id, |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 34 | int active_id, |
| 35 | int highlight_id, |
[email protected] | 4efb56d8 | 2009-11-11 22:56:30 | [diff] [blame] | 36 | int depressed_id, |
| 37 | int background_id); |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 38 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 39 | ~CustomDrawButtonBase(); |
| 40 | |
[email protected] | eafb5b7b | 2009-09-10 18:16:15 | [diff] [blame] | 41 | // Returns the dimensions of the first surface. |
| 42 | int Width() const; |
| 43 | int Height() const; |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 44 | |
| 45 | gboolean OnExpose(GtkWidget* widget, GdkEventExpose* e); |
| 46 | |
[email protected] | 6463cb2a | 2009-05-26 20:18:21 | [diff] [blame] | 47 | void set_paint_override(int state) { paint_override_ = state; } |
[email protected] | d35d20d | 2009-07-21 23:38:29 | [diff] [blame] | 48 | int paint_override() const { return paint_override_; } |
[email protected] | 6463cb2a | 2009-05-26 20:18:21 | [diff] [blame] | 49 | |
[email protected] | 4f14f81 | 2009-07-24 17:39:34 | [diff] [blame] | 50 | // Set the background details. |
| 51 | void SetBackground(SkColor color, SkBitmap* image, SkBitmap* mask); |
| 52 | |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 53 | // Provide NotificationObserver implementation. |
| 54 | virtual void Observe(NotificationType type, |
| 55 | const NotificationSource& source, |
| 56 | const NotificationDetails& details); |
| 57 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 58 | private: |
[email protected] | eafb5b7b | 2009-09-10 18:16:15 | [diff] [blame] | 59 | // We store one surface for each possible state of the button; |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 60 | // INSENSITIVE is the last available state; |
[email protected] | eafb5b7b | 2009-09-10 18:16:15 | [diff] [blame] | 61 | scoped_ptr<CairoCachedSurface> surfaces_[GTK_STATE_INSENSITIVE + 1]; |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 62 | |
[email protected] | 4f14f81 | 2009-07-24 17:39:34 | [diff] [blame] | 63 | // The background image. |
[email protected] | eafb5b7b | 2009-09-10 18:16:15 | [diff] [blame] | 64 | scoped_ptr<CairoCachedSurface> background_image_; |
[email protected] | 4f14f81 | 2009-07-24 17:39:34 | [diff] [blame] | 65 | |
[email protected] | 6463cb2a | 2009-05-26 20:18:21 | [diff] [blame] | 66 | // If non-negative, the state to paint the button. |
| 67 | int paint_override_; |
| 68 | |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 69 | // We need to remember the image ids that the user passes in and the theme |
| 70 | // provider so we can reload images if the user changes theme. |
| 71 | int normal_id_; |
| 72 | int active_id_; |
| 73 | int highlight_id_; |
| 74 | int depressed_id_; |
[email protected] | 4efb56d8 | 2009-11-11 22:56:30 | [diff] [blame] | 75 | int button_background_id_; |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 76 | GtkThemeProvider* theme_provider_; |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 77 | |
| 78 | // Used to listen for theme change notifications. |
| 79 | NotificationRegistrar registrar_; |
| 80 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(CustomDrawButtonBase); |
| 82 | }; |
| 83 | |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame] | 84 | // CustomDrawButton is a plain button where all its various states are drawn |
[email protected] | a5166af6 | 2009-07-03 00:42:29 | [diff] [blame] | 85 | // with static images. In GTK rendering mode, it will show the standard button |
| 86 | // with GTK |stock_id|. |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 87 | class CustomDrawButton : public NotificationObserver { |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 88 | public: |
| 89 | // The constructor takes 4 resource ids. If a resource doesn't exist for a |
| 90 | // button, pass in 0. |
| 91 | CustomDrawButton(int normal_id, |
| 92 | int active_id, |
| 93 | int highlight_id, |
[email protected] | bcb71cf1 | 2009-07-24 21:03:05 | [diff] [blame] | 94 | int depressed_id); |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 95 | |
| 96 | // Same as above, but uses themed (and possibly tinted) images. |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 97 | CustomDrawButton(GtkThemeProvider* theme_provider, |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 98 | int normal_id, |
| 99 | int active_id, |
| 100 | int highlight_id, |
| 101 | int depressed_id, |
[email protected] | 4efb56d8 | 2009-11-11 22:56:30 | [diff] [blame] | 102 | int background_id, |
[email protected] | bcb71cf1 | 2009-07-24 21:03:05 | [diff] [blame] | 103 | const char* stock_id, |
| 104 | GtkIconSize stock_size); |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 105 | |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 106 | ~CustomDrawButton(); |
| 107 | |
[email protected] | af62bb4 | 2009-07-10 19:41:53 | [diff] [blame] | 108 | void Init(); |
| 109 | |
[email protected] | 6d8924d | 2009-04-06 21:21:34 | [diff] [blame] | 110 | GtkWidget* widget() const { return widget_.get(); } |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 111 | |
[email protected] | c12fac9 | 2009-05-05 20:27:00 | [diff] [blame] | 112 | gfx::Rect bounds() const { |
[email protected] | b39b708 | 2009-06-05 23:03:14 | [diff] [blame] | 113 | return gfx::Rect(widget_->allocation.x, |
| 114 | widget_->allocation.y, |
| 115 | widget_->allocation.width, |
| 116 | widget_->allocation.height); |
[email protected] | c12fac9 | 2009-05-05 20:27:00 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | b39b708 | 2009-06-05 23:03:14 | [diff] [blame] | 119 | int width() const { return widget_->allocation.width; } |
[email protected] | 6c940e69 | 2009-06-23 20:29:07 | [diff] [blame] | 120 | int height() const { return widget_->allocation.height; } |
[email protected] | c12fac9 | 2009-05-05 20:27:00 | [diff] [blame] | 121 | |
[email protected] | 6463cb2a | 2009-05-26 20:18:21 | [diff] [blame] | 122 | // Set the state to draw. We will paint the widget as if it were in this |
| 123 | // state. |
| 124 | void SetPaintOverride(GtkStateType state); |
| 125 | |
| 126 | // Resume normal drawing of the widget's state. |
| 127 | void UnsetPaintOverride(); |
| 128 | |
[email protected] | 4f14f81 | 2009-07-24 17:39:34 | [diff] [blame] | 129 | // Set the background details. |
| 130 | void SetBackground(SkColor color, SkBitmap* image, SkBitmap* mask); |
| 131 | |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 132 | // Provide NotificationObserver implementation. |
| 133 | virtual void Observe(NotificationType type, |
| 134 | const NotificationSource& source, |
| 135 | const NotificationDetails& details); |
| 136 | |
[email protected] | 2d93381 | 2009-07-23 16:44:11 | [diff] [blame] | 137 | // Returns a standard close button. Pass a |theme_provider| to use Gtk icons |
| 138 | // in Gtk rendering mode. |
| 139 | static CustomDrawButton* CloseButton(GtkThemeProvider* theme_provider); |
[email protected] | 3dda0dc | 2009-04-06 23:33:08 | [diff] [blame] | 140 | |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 141 | private: |
[email protected] | 2d93381 | 2009-07-23 16:44:11 | [diff] [blame] | 142 | // Sets the button to themed or not. |
| 143 | void SetBrowserTheme(); |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 144 | |
[email protected] | a5166af6 | 2009-07-03 00:42:29 | [diff] [blame] | 145 | // Callback for custom button expose, used to draw the custom graphics. |
| 146 | static gboolean OnCustomExpose(GtkWidget* widget, |
| 147 | GdkEventExpose* e, |
| 148 | CustomDrawButton* obj); |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 149 | |
| 150 | // The actual button widget. |
[email protected] | 6d8924d | 2009-04-06 21:21:34 | [diff] [blame] | 151 | OwnedWidgetGtk widget_; |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 152 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 153 | CustomDrawButtonBase button_base_; |
| 154 | |
[email protected] | 2d93381 | 2009-07-23 16:44:11 | [diff] [blame] | 155 | // Our theme provider. |
[email protected] | d35d20d | 2009-07-21 23:38:29 | [diff] [blame] | 156 | GtkThemeProvider* theme_provider_; |
| 157 | |
[email protected] | bcb71cf1 | 2009-07-24 21:03:05 | [diff] [blame] | 158 | // The stock icon name and size. |
[email protected] | a5166af6 | 2009-07-03 00:42:29 | [diff] [blame] | 159 | const char* gtk_stock_name_; |
[email protected] | bcb71cf1 | 2009-07-24 21:03:05 | [diff] [blame] | 160 | GtkIconSize icon_size_; |
[email protected] | a5166af6 | 2009-07-03 00:42:29 | [diff] [blame] | 161 | |
[email protected] | 5fdafb2 | 2009-07-13 23:23:08 | [diff] [blame] | 162 | // Used to listen for theme change notifications. |
| 163 | NotificationRegistrar registrar_; |
| 164 | |
[email protected] | 64f001a | 2009-04-09 06:40:57 | [diff] [blame] | 165 | DISALLOW_COPY_AND_ASSIGN(CustomDrawButton); |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 166 | }; |
| 167 | |
[email protected] | 72cf811 | 2009-02-19 22:47:53 | [diff] [blame] | 168 | #endif // CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_ |