blob: 250f8ec7c8fa07e349a7feb4c76d276bd7adf6e1 [file] [log] [blame]
[email protected]72cf8112009-02-19 22:47:531// 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]c12fac92009-05-05 20:27:0012#include "base/gfx/rect.h"
[email protected]e6ba5402009-02-20 19:15:0213#include "base/scoped_ptr.h"
[email protected]af62bb42009-07-10 19:41:5314#include "chrome/common/notification_observer.h"
15#include "chrome/common/notification_registrar.h"
[email protected]6d8924d2009-04-06 21:21:3416#include "chrome/common/owned_widget_gtk.h"
[email protected]4f14f812009-07-24 17:39:3417#include "third_party/skia/include/core/SkBitmap.h"
[email protected]72cf8112009-02-19 22:47:5318
[email protected]eafb5b7b2009-09-10 18:16:1519class CairoCachedSurface;
[email protected]5fdafb22009-07-13 23:23:0820class GtkThemeProvider;
[email protected]af62bb42009-07-10 19:41:5321
[email protected]e6ba5402009-02-20 19:15:0222// These classes implement two kinds of custom-drawn buttons. They're
23// used on the toolbar and the bookmarks bar.
24
[email protected]64f001a2009-04-09 06:40:5725// 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]af62bb42009-07-10 19:41:5328class CustomDrawButtonBase : public NotificationObserver {
[email protected]64f001a2009-04-09 06:40:5729 public:
[email protected]af62bb42009-07-10 19:41:5330 // If the images come from ResourceBundle rather than the theme provider,
31 // pass in NULL for |theme_provider|.
[email protected]5fdafb22009-07-13 23:23:0832 CustomDrawButtonBase(GtkThemeProvider* theme_provider,
[email protected]af62bb42009-07-10 19:41:5333 int normal_id,
[email protected]64f001a2009-04-09 06:40:5734 int active_id,
35 int highlight_id,
[email protected]4efb56d82009-11-11 22:56:3036 int depressed_id,
37 int background_id);
[email protected]af62bb42009-07-10 19:41:5338
[email protected]64f001a2009-04-09 06:40:5739 ~CustomDrawButtonBase();
40
[email protected]eafb5b7b2009-09-10 18:16:1541 // Returns the dimensions of the first surface.
42 int Width() const;
43 int Height() const;
[email protected]64f001a2009-04-09 06:40:5744
45 gboolean OnExpose(GtkWidget* widget, GdkEventExpose* e);
46
[email protected]6463cb2a2009-05-26 20:18:2147 void set_paint_override(int state) { paint_override_ = state; }
[email protected]d35d20d2009-07-21 23:38:2948 int paint_override() const { return paint_override_; }
[email protected]6463cb2a2009-05-26 20:18:2149
[email protected]4f14f812009-07-24 17:39:3450 // Set the background details.
51 void SetBackground(SkColor color, SkBitmap* image, SkBitmap* mask);
52
[email protected]af62bb42009-07-10 19:41:5353 // Provide NotificationObserver implementation.
54 virtual void Observe(NotificationType type,
55 const NotificationSource& source,
56 const NotificationDetails& details);
57
[email protected]64f001a2009-04-09 06:40:5758 private:
[email protected]eafb5b7b2009-09-10 18:16:1559 // We store one surface for each possible state of the button;
[email protected]64f001a2009-04-09 06:40:5760 // INSENSITIVE is the last available state;
[email protected]eafb5b7b2009-09-10 18:16:1561 scoped_ptr<CairoCachedSurface> surfaces_[GTK_STATE_INSENSITIVE + 1];
[email protected]64f001a2009-04-09 06:40:5762
[email protected]4f14f812009-07-24 17:39:3463 // The background image.
[email protected]eafb5b7b2009-09-10 18:16:1564 scoped_ptr<CairoCachedSurface> background_image_;
[email protected]4f14f812009-07-24 17:39:3465
[email protected]6463cb2a2009-05-26 20:18:2166 // If non-negative, the state to paint the button.
67 int paint_override_;
68
[email protected]af62bb42009-07-10 19:41:5369 // 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]4efb56d82009-11-11 22:56:3075 int button_background_id_;
[email protected]5fdafb22009-07-13 23:23:0876 GtkThemeProvider* theme_provider_;
[email protected]af62bb42009-07-10 19:41:5377
78 // Used to listen for theme change notifications.
79 NotificationRegistrar registrar_;
80
[email protected]64f001a2009-04-09 06:40:5781 DISALLOW_COPY_AND_ASSIGN(CustomDrawButtonBase);
82};
83
[email protected]e6ba5402009-02-20 19:15:0284// CustomDrawButton is a plain button where all its various states are drawn
[email protected]a5166af62009-07-03 00:42:2985// with static images. In GTK rendering mode, it will show the standard button
86// with GTK |stock_id|.
[email protected]5fdafb22009-07-13 23:23:0887class CustomDrawButton : public NotificationObserver {
[email protected]72cf8112009-02-19 22:47:5388 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]bcb71cf12009-07-24 21:03:0594 int depressed_id);
[email protected]af62bb42009-07-10 19:41:5395
96 // Same as above, but uses themed (and possibly tinted) images.
[email protected]5fdafb22009-07-13 23:23:0897 CustomDrawButton(GtkThemeProvider* theme_provider,
[email protected]af62bb42009-07-10 19:41:5398 int normal_id,
99 int active_id,
100 int highlight_id,
101 int depressed_id,
[email protected]4efb56d82009-11-11 22:56:30102 int background_id,
[email protected]bcb71cf12009-07-24 21:03:05103 const char* stock_id,
104 GtkIconSize stock_size);
[email protected]af62bb42009-07-10 19:41:53105
[email protected]72cf8112009-02-19 22:47:53106 ~CustomDrawButton();
107
[email protected]af62bb42009-07-10 19:41:53108 void Init();
109
[email protected]6d8924d2009-04-06 21:21:34110 GtkWidget* widget() const { return widget_.get(); }
[email protected]72cf8112009-02-19 22:47:53111
[email protected]c12fac92009-05-05 20:27:00112 gfx::Rect bounds() const {
[email protected]b39b7082009-06-05 23:03:14113 return gfx::Rect(widget_->allocation.x,
114 widget_->allocation.y,
115 widget_->allocation.width,
116 widget_->allocation.height);
[email protected]c12fac92009-05-05 20:27:00117 }
118
[email protected]b39b7082009-06-05 23:03:14119 int width() const { return widget_->allocation.width; }
[email protected]6c940e692009-06-23 20:29:07120 int height() const { return widget_->allocation.height; }
[email protected]c12fac92009-05-05 20:27:00121
[email protected]6463cb2a2009-05-26 20:18:21122 // 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]4f14f812009-07-24 17:39:34129 // Set the background details.
130 void SetBackground(SkColor color, SkBitmap* image, SkBitmap* mask);
131
[email protected]5fdafb22009-07-13 23:23:08132 // Provide NotificationObserver implementation.
133 virtual void Observe(NotificationType type,
134 const NotificationSource& source,
135 const NotificationDetails& details);
136
[email protected]2d933812009-07-23 16:44:11137 // 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]3dda0dc2009-04-06 23:33:08140
[email protected]72cf8112009-02-19 22:47:53141 private:
[email protected]2d933812009-07-23 16:44:11142 // Sets the button to themed or not.
143 void SetBrowserTheme();
[email protected]5fdafb22009-07-13 23:23:08144
[email protected]a5166af62009-07-03 00:42:29145 // 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]72cf8112009-02-19 22:47:53149
150 // The actual button widget.
[email protected]6d8924d2009-04-06 21:21:34151 OwnedWidgetGtk widget_;
[email protected]72cf8112009-02-19 22:47:53152
[email protected]64f001a2009-04-09 06:40:57153 CustomDrawButtonBase button_base_;
154
[email protected]2d933812009-07-23 16:44:11155 // Our theme provider.
[email protected]d35d20d2009-07-21 23:38:29156 GtkThemeProvider* theme_provider_;
157
[email protected]bcb71cf12009-07-24 21:03:05158 // The stock icon name and size.
[email protected]a5166af62009-07-03 00:42:29159 const char* gtk_stock_name_;
[email protected]bcb71cf12009-07-24 21:03:05160 GtkIconSize icon_size_;
[email protected]a5166af62009-07-03 00:42:29161
[email protected]5fdafb22009-07-13 23:23:08162 // Used to listen for theme change notifications.
163 NotificationRegistrar registrar_;
164
[email protected]64f001a2009-04-09 06:40:57165 DISALLOW_COPY_AND_ASSIGN(CustomDrawButton);
[email protected]72cf8112009-02-19 22:47:53166};
167
[email protected]72cf8112009-02-19 22:47:53168#endif // CHROME_BROWSER_GTK_CUSTOM_BUTTON_H_