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_GTK_UTIL_H_
6 #define CHROME_BROWSER_UI_GTK_GTK_UTIL_H_
12 #include "base/strings/string16.h"
13 #include "ui/base/window_open_disposition.h"
14 #include "ui/base/x/x11_util.h"
15 #include "ui/gfx/point.h"
16 #include "ui/gfx/rect.h"
18 typedef struct _cairo cairo_t
;
19 typedef struct _GtkWidget GtkWidget
;
22 class GtkThemeService
;
32 // Create a table of labeled controls, using proper spacing and alignment.
33 // Arguments should be pairs of const char*, GtkWidget*, concluding with a
34 // NULL. The first argument is a vector in which to place all labels
35 // produced. It can be NULL if you don't need to keep track of the label
36 // widgets. The second argument is a color to force the label text to. It can
37 // be NULL to get the system default.
40 // controls = CreateLabeledControlsGroup(NULL,
41 // "Name:", title_entry_,
42 // "Folder:", folder_combobox_,
44 GtkWidget
* CreateLabeledControlsGroup(
45 std::vector
<GtkWidget
*>* labels
,
46 const char* text
, ...);
48 // Create a GtkBin with |child| as its child widget. This bin will paint a
49 // border of color |color| with the sizes specified in pixels.
50 GtkWidget
* CreateGtkBorderBin(GtkWidget
* child
, const GdkColor
* color
,
51 int top
, int bottom
, int left
, int right
);
53 // Left-align the given GtkMisc and return the same pointer.
54 GtkWidget
* LeftAlignMisc(GtkWidget
* misc
);
56 // Create a left-aligned label with the given text in bold.
57 GtkWidget
* CreateBoldLabel(const std::string
& text
);
59 // As above, but uses number of characters/lines directly rather than looking up
61 void GetWidgetSizeFromCharacters(GtkWidget
* widget
,
62 double width_chars
, double height_lines
,
63 int* width
, int* height
);
65 // Calculates the size of given widget based on the size specified in number of
66 // characters/lines (in locale specific resource file) and font metrics.
67 // NOTE: Make sure to realize |widget| before using this method, or a default
68 // font size will be used instead of the actual font size.
69 void GetWidgetSizeFromResources(GtkWidget
* widget
,
70 int width_chars
, int height_lines
,
71 int* width
, int* height
);
73 // As above, but a convenience method for configuring dialog size.
74 // |width_id| and |height_id| are resource IDs for the size. If either of these
75 // are set to -1, the respective size will be set to the widget default.
76 // |resizable| also controls whether the dialog will be resizable
77 // (this info is also necessary for getting the width-setting code
79 void SetWindowSizeFromResources(GtkWindow
* window
,
80 int width_id
, int height_id
, bool resizable
);
82 // Puts all browser windows in one window group; this will make any dialog
84 void MakeAppModalWindowGroup();
86 // Called after an app modal dialog that used MakeAppModalWindowGroup() was
87 // dismissed. Returns each browser window to its own window group.
88 void AppModalDismissedUngroupWindows();
90 // Remove all children from this container.
91 void RemoveAllChildren(GtkWidget
* container
);
93 // Force the font size of the widget to |size_pixels|.
94 void ForceFontSizePixels(GtkWidget
* widget
, double size_pixels
);
96 // Undoes the effects of a previous ForceFontSizePixels() call. Safe to call
97 // even if ForceFontSizePixels() was never called.
98 void UndoForceFontSize(GtkWidget
* widget
);
100 // Retuns size of the |widget| without window manager decorations.
101 gfx::Size
GetWidgetSize(GtkWidget
* widget
);
103 // Converts a point in a widget to screen coordinates. The point |p| is
104 // relative to the widget's top-left origin.
105 void ConvertWidgetPointToScreen(GtkWidget
* widget
, gfx::Point
* p
);
107 // Stick the widget in the given hbox without expanding vertically. The widget
108 // is packed at the start of the hbox. This is useful for widgets that would
109 // otherwise expand to fill the vertical space of the hbox
110 // (e.g. buttons). Returns the vbox that widget was packed in.
111 GtkWidget
* CenterWidgetInHBox(GtkWidget
* hbox
, GtkWidget
* widget
,
112 bool pack_at_end
, int padding
);
114 // Set that clicking the button with the given mouse buttons will cause a click
116 // NOTE: If you need to connect to the button-press-event or
117 // button-release-event signals, do so before calling this function.
118 void SetButtonClickableByMouseButtons(GtkWidget
* button
,
119 bool left
, bool middle
, bool right
);
121 // Set that a button causes a page navigation. In particular, it will accept
122 // middle clicks. Warning: only call this *after* you have connected your
123 // own handlers for button-press and button-release events, or you will not get
125 void SetButtonTriggersNavigation(GtkWidget
* button
);
127 // Returns the mirrored x value for |bounds| if the layout is RTL; otherwise,
128 // the original value is returned unchanged.
129 int MirroredLeftPointForRect(GtkWidget
* widget
, const gfx::Rect
& bounds
);
131 // Returns the mirrored right value for |bounds| if the layout is RTL;
132 // otherwise, the original value is returned unchanged.
133 int MirroredRightPointForRect(GtkWidget
* widget
, const gfx::Rect
& bounds
);
135 // Returns the mirrored x value for the point |x| if the layout is RTL;
136 // otherwise, the original value is returned unchanged.
137 int MirroredXCoordinate(GtkWidget
* widget
, int x
);
139 // Returns true if the pointer is currently inside the widget.
140 bool WidgetContainsCursor(GtkWidget
* widget
);
142 // Sets the default window icon for all windows created in this app. This icon
143 // will only be used if a window has not explicitly been assigned an icon
144 // (e.g. by SetWindowIcon()).
146 // |window| is only used to determine if a themed icon exists. If so, we use
147 // that icon, otherwise we use the icon packaged with Chrome.
148 void SetDefaultWindowIcon(GtkWindow
* window
);
150 // Sets the icon of |window| to the Chrome product icon, overlaid with
151 // |profile|'s avatar icon (or the Incognito icon for Incognito windows). It
152 // first looks for a themed icon, then falls back to the product icons
153 // packaged with Chrome.
154 void SetWindowIcon(GtkWindow
* window
, Profile
* profile
);
156 // Sets the icon of |window| to |icon|, overlaid with |profile|'s avatar icon
157 // (or the Incognito icon for Incognito windows). It first looks for a themed
158 // icon, then falls back to the product icons packaged with Chrome.
160 // Note that |icon| will be modified by this function.
161 void SetWindowIcon(GtkWindow
* window
, Profile
* profile
, GdkPixbuf
* icon
);
163 // Adds an action button with the given text to the dialog. Only useful when you
164 // want a stock icon but not the stock text to go with it. Returns the button.
165 GtkWidget
* AddButtonToDialog(GtkWidget
* dialog
, const gchar
* text
,
166 const gchar
* stock_id
, gint response_id
);
168 GtkWidget
* BuildDialogButton(GtkWidget
* dialog
, int ids_id
,
169 const gchar
* stock_id
);
171 GtkWidget
* CreateEntryImageHBox(GtkWidget
* entry
, GtkWidget
* image
);
173 // Sets all the foreground color states of |label| to |color|.
174 void SetLabelColor(GtkWidget
* label
, const GdkColor
* color
);
176 // Adds the given widget to an alignment identing it by |kGroupIndent|.
177 GtkWidget
* IndentWidget(GtkWidget
* content
);
179 // Reverses a point in RTL mode. Used in making vectors of GdkPoints for window
181 GdkPoint
MakeBidiGdkPoint(gint x
, gint y
, gint width
, bool ltr
);
183 // Creates a tooltip string to be passed to gtk_widget_set_tooltip_markup from
184 // the title and URL.
185 std::string
BuildTooltipTitleFor(base::string16 title
, const GURL
& url
);
187 // Draws a GTK text entry with the style parameters of GtkEntry
188 // |offscreen_entry| onto |widget_to_draw_on| in the rectangle |rec|. Drawing
189 // is only done in the clip rectangle |dirty_rec|.
190 void DrawTextEntryBackground(GtkWidget
* offscreen_entry
,
191 GtkWidget
* widget_to_draw_on
,
192 GdkRectangle
* dirty_rec
,
195 // Set up the text to be displayed by |layout|.
196 void SetLayoutText(PangoLayout
* layout
, const base::string16
& text
);
198 // Draws the background of the toolbar area subject to the expose rectangle
199 // |event| and starting image tiling from |tabstrip_origin|.
200 void DrawThemedToolbarBackground(GtkWidget
* widget
,
202 GdkEventExpose
* event
,
203 const gfx::Point
& tabstrip_origin
,
204 GtkThemeService
* provider
);
206 // Draw an entire pixbuf without dithering.
207 void DrawFullImage(cairo_t
* cr
,
209 const gfx::Image
& image
,
213 // Returns the two colors averaged together.
214 GdkColor
AverageColors(GdkColor color_one
, GdkColor color_two
);
216 // Show the image for the given menu item, even if the user's default is to not
217 // show images. Only to be used for favicons or other menus where the image is
218 // crucial to its functionality.
219 void SetAlwaysShowImage(GtkWidget
* image_menu_item
);
221 // Get a rectangle corresponding to a widget's allocation relative to its
222 // toplevel window's origin.
223 gfx::Rect
GetWidgetRectRelativeToToplevel(GtkWidget
* widget
);
225 // Don't allow the widget to paint anything, and instead propagate the expose
226 // to its children. This is similar to calling
228 // gtk_widget_set_app_paintable(container, TRUE);
230 // except that it will always work, and it should be called after any custom
231 // expose events are connected.
232 void SuppressDefaultPainting(GtkWidget
* container
);
234 // Safely grabs all input (with X grabs and an application grab), returning true
236 bool GrabAllInput(GtkWidget
* widget
);
238 // Returns a rectangle that represents the widget's bounds. The rectangle it
239 // returns is the same as gtk_widget_get_allocation, but anchored at (0, 0).
240 gfx::Rect
WidgetBounds(GtkWidget
* widget
);
242 // Update the timestamp for the given window. This is usually the time of the
243 // last user event, but on rare occasions we wish to update it despite not
244 // receiving a user event.
245 void SetWMLastUserActionTime(GtkWindow
* window
);
247 // The current system time, using the format expected by the X server, but not
248 // retrieved from the X server. NOTE: You should almost never need to use this
249 // function, instead using the timestamp from the latest GDK event.
252 // Uses the autocomplete controller for |profile| to convert the contents of the
253 // PRIMARY selection to a parsed URL. Returns true and sets |url| on success,
254 // otherwise returns false.
255 bool URLFromPrimarySelection(Profile
* profile
, GURL
* url
);
257 // Set the colormap of the given window to rgba to allow transparency.
258 bool AddWindowAlphaChannel(GtkWidget
* window
);
260 // Get the default colors for a text entry. Parameters may be NULL.
261 void GetTextColors(GdkColor
* normal_base
,
262 GdkColor
* selected_base
,
263 GdkColor
* normal_text
,
264 GdkColor
* selected_text
);
266 // Wrappers to show a GtkDialog. On Linux, it merely calls gtk_widget_show_all.
267 // On ChromeOs, it calls ShowNativeDialog which hosts the its vbox
268 // in a view based Window.
269 void ShowDialog(GtkWidget
* dialog
);
270 void ShowDialogWithLocalizedSize(GtkWidget
* dialog
,
274 void ShowDialogWithMinLocalizedWidth(GtkWidget
* dialog
,
277 // Wrapper to present a window. On Linux, it just calls gtk_window_present or
278 // gtk_window_present_with_time for non-zero timestamp.
279 void PresentWindow(GtkWidget
* window
, int timestamp
);
281 // Gets dialog window bounds.
282 gfx::Rect
GetDialogBounds(GtkWidget
* dialog
);
284 // Returns the stock menu item label for the "preferences" item - returns an
285 // empty string if no stock item found.
286 base::string16
GetStockPreferencesMenuLabel();
288 // Checks whether a widget is actually visible, i.e. whether it and all its
289 // ancestors up to its toplevel are visible.
290 bool IsWidgetAncestryVisible(GtkWidget
* widget
);
292 // Sets the given label's size request to |pixel_width|. This will cause the
293 // label to wrap if it needs to. The reason for this function is that some
294 // versions of GTK mis-align labels that have a size request and line wrapping,
295 // and this function hides the complexity of the workaround.
296 void SetLabelWidth(GtkWidget
* label
, int pixel_width
);
298 // Make the |label| shrinkable within a GthChromeShrinkableHBox
299 // It calculates the real size request of a label and set its ellipsize mode to
300 // PANGO_ELLIPSIZE_END.
301 // It must be done when the label is mapped (become visible on the screen),
302 // to make sure the pango can get correct font information for the calculation.
303 void InitLabelSizeRequestAndEllipsizeMode(GtkWidget
* label
);
305 // A helper function for gtk_message_dialog_new() to work around a few KDE 3
306 // window manager bugs. You should always call it after creating a dialog with
307 // gtk_message_dialog_new.
308 void ApplyMessageDialogQuirks(GtkWidget
* dialog
);
310 } // namespace gtk_util
312 #endif // CHROME_BROWSER_UI_GTK_GTK_UTIL_H_