1 // Copyright (c) 2012 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_GTK_INFOBARS_INFOBAR_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_INFOBARS_INFOBAR_GTK_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/infobars/infobar.h"
13 #include "chrome/browser/infobars/infobar_delegate.h"
14 #include "chrome/browser/ui/gtk/menu_gtk.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "third_party/skia/include/core/SkColor.h"
18 #include "ui/base/gtk/gtk_signal.h"
19 #include "ui/base/gtk/owned_widget_gtk.h"
21 class CustomDrawButton
;
22 class GtkThemeService
;
25 class GtkSignalRegistrar
;
29 class InfoBarGtk
: public InfoBar
,
30 public content::NotificationObserver
{
32 // Conversion from cairo colors to SkColor.
33 typedef void (InfoBarGtk::*ColorGetter
)(InfoBarDelegate::Type
,
34 double* r
, double* g
, double* b
);
36 explicit InfoBarGtk(scoped_ptr
<InfoBarDelegate
> delegate
);
37 virtual ~InfoBarGtk();
39 // Get the top level native GTK widget for this infobar.
40 GtkWidget
* widget() { return widget_
.get(); }
42 GdkColor
GetBorderColor() const;
44 // Returns the target height of the infobar if the bar is animating,
45 // otherwise 0. We care about this number since we want to prevent
46 // unnecessary renderer repaints while animating.
47 int AnimatingHeight() const;
49 SkColor
ConvertGetColor(ColorGetter getter
);
51 // Retrieves the component colors for the infobar's background
52 // gradient. (This varies by infobars and can be animated to change).
53 virtual void GetTopColor(InfoBarDelegate::Type type
,
54 double* r
, double* g
, double* b
);
55 virtual void GetBottomColor(InfoBarDelegate::Type type
,
56 double* r
, double* g
, double* b
);
59 // Spacing after message (and before buttons).
60 static const int kEndOfLabelSpacing
;
64 // Inits any widgets and related objects necessary.
66 // NOTE: Subclasses who need to init widgets should override this function and
67 // explicitly call their parent's implementation first, then continue with
68 // further work they need to do. Failing to call the parent implementation
69 // first (or at all), or setting up widgets in the constructor instead of
70 // here, will lead to bad side effects like crashing.
71 virtual void PlatformSpecificSetOwner() OVERRIDE
;
73 virtual void PlatformSpecificShow(bool animate
) OVERRIDE
;
74 virtual void PlatformSpecificOnCloseSoon() OVERRIDE
;
75 virtual void PlatformSpecificOnHeightsRecalculated() OVERRIDE
;
77 // content::NotificationObserver:
78 virtual void Observe(int type
,
79 const content::NotificationSource
& source
,
80 const content::NotificationDetails
& details
) OVERRIDE
;
82 // Styles the close button as if we're doing Chrome-stlye widget rendering.
83 void ForceCloseButtonToUseChromeTheme();
85 GtkWidget
* hbox() { return hbox_
; }
87 // Returns the signal registrar for this infobar. All signals representing
88 // user actions on visible widgets must go through this registrar!
89 ui::GtkSignalRegistrar
* signals() { return signals_
.get(); }
91 // Creates a label with the appropriate font and color for the current
92 // gtk-theme state. It is InfoBarGtk's responsibility to observe browser
93 // theme changes and update the label's state.
94 GtkWidget
* CreateLabel(const std::string
& text
);
96 // Creates a link button with the appropriate current gtk-theme state.
97 GtkWidget
* CreateLinkButton(const std::string
& text
);
99 // Builds a button with an arrow in it to emulate the menu-button style from
100 // the windows version.
101 static GtkWidget
* CreateMenuButton(const std::string
& text
);
103 // Adds |display_text| to the infobar. If |link_text| is not empty, it is
104 // rendered as a hyperlink and inserted into |display_text| at |link_offset|,
105 // or right aligned in the infobar if |link_offset| is |npos|. If a link is
106 // supplied, |link_callback| must not be null. It will be invoked on click.
107 void AddLabelWithInlineLink(const base::string16
& display_text
,
108 const base::string16
& link_text
,
112 // Shows the menu with |model| with the context of |sender|.
113 void ShowMenuWithModel(GtkWidget
* sender
,
114 MenuGtk::Delegate
* delegate
,
115 ui::MenuModel
* model
);
118 void GetBackgroundColor(SkColor color
, double* r
, double* g
, double* b
);
119 void UpdateBorderColor();
121 CHROMEGTK_CALLBACK_0(InfoBarGtk
, void, OnCloseButton
);
122 CHROMEGTK_CALLBACK_1(InfoBarGtk
, gboolean
, OnBackgroundExpose
,
124 CHROMEGTK_CALLBACK_2(InfoBarGtk
, void, OnChildSizeRequest
, GtkWidget
*,
127 // A GtkExpandedContainer that contains |bg_box_| so we can vary the height of
129 ui::OwnedWidgetGtk widget_
;
131 // The second highest widget in the hierarchy (after the |widget_|).
134 // The hbox that holds infobar elements (button, text, icon, etc.).
137 // The x that closes the bar.
138 scoped_ptr
<CustomDrawButton
> close_button_
;
140 // The theme provider, used for getting border colors.
141 GtkThemeService
* theme_service_
;
143 content::NotificationRegistrar registrar_
;
145 // A list of signals which we clear out once we're closing.
146 scoped_ptr
<ui::GtkSignalRegistrar
> signals_
;
148 // The current menu displayed. Can be null. We own this on the base class so
149 // we can cancel the menu while we're closing.
150 scoped_ptr
<MenuGtk
> menu_
;
152 DISALLOW_COPY_AND_ASSIGN(InfoBarGtk
);
155 #endif // CHROME_BROWSER_UI_GTK_INFOBARS_INFOBAR_GTK_H_