| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GTK_GTK_UTIL_H_ | 5 #ifndef CHROME_BROWSER_GTK_GTK_UTIL_H_ |
| 6 #define CHROME_BROWSER_GTK_GTK_UTIL_H_ | 6 #define CHROME_BROWSER_GTK_GTK_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <gtk/gtk.h> | 9 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 10 #include <string> | 10 // TODO(msw): remove this file once all includes have been updated. |
| 11 #include <vector> | |
| 12 | |
| 13 #include "app/x11_util.h" | |
| 14 #include "base/string16.h" | |
| 15 #include "gfx/point.h" | |
| 16 #include "gfx/rect.h" | |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebDragOperation.h" | |
| 18 #include "webkit/glue/window_open_disposition.h" | |
| 19 | |
| 20 typedef struct _cairo cairo_t; | |
| 21 typedef struct _GdkColor GdkColor; | |
| 22 typedef struct _GtkWidget GtkWidget; | |
| 23 | |
| 24 class BrowserWindow; | |
| 25 class GtkThemeProvider; | |
| 26 class GURL; | |
| 27 class Profile; | |
| 28 struct RendererPreferences; // from common/renderer_preferences.h | |
| 29 | |
| 30 const int kSkiaToGDKMultiplier = 257; | |
| 31 | |
| 32 // Define a macro for creating GdkColors from RGB values. This is a macro to | |
| 33 // allow static construction of literals, etc. Use this like: | |
| 34 // GdkColor white = GDK_COLOR_RGB(0xff, 0xff, 0xff); | |
| 35 #define GDK_COLOR_RGB(r, g, b) {0, r * kSkiaToGDKMultiplier, \ | |
| 36 g * kSkiaToGDKMultiplier, b * kSkiaToGDKMultiplier} | |
| 37 | |
| 38 namespace event_utils { | |
| 39 | |
| 40 // Translates event flags into what kind of disposition they represent. | |
| 41 // For example, a middle click would mean to open a background tab. | |
| 42 // event_flags are the state in the GdkEvent structure. | |
| 43 WindowOpenDisposition DispositionFromEventFlags(guint state); | |
| 44 | |
| 45 } // namespace event_utils | |
| 46 | |
| 47 namespace gtk_util { | |
| 48 | |
| 49 extern const GdkColor kGdkWhite; | |
| 50 extern const GdkColor kGdkGray; | |
| 51 extern const GdkColor kGdkBlack; | |
| 52 extern const GdkColor kGdkGreen; | |
| 53 | |
| 54 // Constants relating to the layout of dialog windows: | |
| 55 // (See http://library.gnome.org/devel/hig-book/stable/design-window.html.en) | |
| 56 | |
| 57 // Spacing between controls of the same group. | |
| 58 const int kControlSpacing = 6; | |
| 59 | |
| 60 // Horizontal spacing between a label and its control. | |
| 61 const int kLabelSpacing = 12; | |
| 62 | |
| 63 // Indent of the controls within each group. | |
| 64 const int kGroupIndent = 12; | |
| 65 | |
| 66 // Space around the outside of a dialog's contents. | |
| 67 const int kContentAreaBorder = 12; | |
| 68 | |
| 69 // Spacing between groups of controls. | |
| 70 const int kContentAreaSpacing = 18; | |
| 71 | |
| 72 // Horizontal Spacing between controls in a form. | |
| 73 const int kFormControlSpacing = 10; | |
| 74 | |
| 75 // Create a table of labeled controls, using proper spacing and alignment. | |
| 76 // Arguments should be pairs of const char*, GtkWidget*, concluding with a | |
| 77 // NULL. The first argument is a vector in which to place all labels | |
| 78 // produced. It can be NULL if you don't need to keep track of the label | |
| 79 // widgets. The second argument is a color to force the label text to. It can | |
| 80 // be NULL to get the system default. | |
| 81 // | |
| 82 // For example: | |
| 83 // controls = CreateLabeledControlsGroup(NULL, | |
| 84 // "Name:", title_entry_, | |
| 85 // "Folder:", folder_combobox_, | |
| 86 // NULL); | |
| 87 GtkWidget* CreateLabeledControlsGroup( | |
| 88 std::vector<GtkWidget*>* labels, | |
| 89 const char* text, ...); | |
| 90 | |
| 91 // Create a GtkBin with |child| as its child widget. This bin will paint a | |
| 92 // border of color |color| with the sizes specified in pixels. | |
| 93 GtkWidget* CreateGtkBorderBin(GtkWidget* child, const GdkColor* color, | |
| 94 int top, int bottom, int left, int right); | |
| 95 | |
| 96 // Left-align the given GtkMisc and return the same pointer. | |
| 97 GtkWidget* LeftAlignMisc(GtkWidget* misc); | |
| 98 | |
| 99 // Create a left-aligned label with the given text in bold. | |
| 100 GtkWidget* CreateBoldLabel(const std::string& text); | |
| 101 | |
| 102 // As above, but uses number of characters/lines directly rather than looking up | |
| 103 // a resource. | |
| 104 void GetWidgetSizeFromCharacters(GtkWidget* widget, | |
| 105 double width_chars, double height_lines, | |
| 106 int* width, int* height); | |
| 107 | |
| 108 // Calculates the size of given widget based on the size specified in number of | |
| 109 // characters/lines (in locale specific resource file) and font metrics. | |
| 110 // NOTE: Make sure to realize |widget| before using this method, or a default | |
| 111 // font size will be used instead of the actual font size. | |
| 112 void GetWidgetSizeFromResources(GtkWidget* widget, | |
| 113 int width_chars, int height_lines, | |
| 114 int* width, int* height); | |
| 115 | |
| 116 // As above, but a convenience method for configuring dialog size. | |
| 117 // |width_id| and |height_id| are resource IDs for the size. If either of these | |
| 118 // are set to -1, the respective size will be set to the widget default. | |
| 119 // |resizable| also controls whether the dialog will be resizable | |
| 120 // (this info is also necessary for getting the width-setting code | |
| 121 // right). | |
| 122 void SetWindowSizeFromResources(GtkWindow* window, | |
| 123 int width_id, int height_id, bool resizable); | |
| 124 | |
| 125 // Places |window| approximately over center of |parent|, it also moves window | |
| 126 // to parent's desktop. Use this only for non-modal dialogs, such as the | |
| 127 // options window and content settings window; otherwise you should be using | |
| 128 // transient_for. | |
| 129 void CenterOverWindow(GtkWindow* window, GtkWindow* parent); | |
| 130 | |
| 131 // Puts all browser windows in one window group; this will make any dialog | |
| 132 // spawned app modal. | |
| 133 void MakeAppModalWindowGroup(); | |
| 134 | |
| 135 // Called after an app modal dialog that used MakeAppModalWindowGroup() was | |
| 136 // dismissed. Returns each browser window to its own window group. | |
| 137 void AppModalDismissedUngroupWindows(); | |
| 138 | |
| 139 // Remove all children from this container. | |
| 140 void RemoveAllChildren(GtkWidget* container); | |
| 141 | |
| 142 // Force the font size of the widget to |size_pixels|. | |
| 143 void ForceFontSizePixels(GtkWidget* widget, double size_pixels); | |
| 144 | |
| 145 // Undoes the effects of a previous ForceFontSizePixels() call. Safe to call | |
| 146 // even if ForceFontSizePixels() was never called. | |
| 147 void UndoForceFontSize(GtkWidget* widget); | |
| 148 | |
| 149 // Gets the position of a gtk widget in screen coordinates. | |
| 150 gfx::Point GetWidgetScreenPosition(GtkWidget* widget); | |
| 151 | |
| 152 // Returns the bounds of the specified widget in screen coordinates. | |
| 153 gfx::Rect GetWidgetScreenBounds(GtkWidget* widget); | |
| 154 | |
| 155 // Retuns size of the |widget| without window manager decorations. | |
| 156 gfx::Size GetWidgetSize(GtkWidget* widget); | |
| 157 | |
| 158 // Converts a point in a widget to screen coordinates. The point |p| is | |
| 159 // relative to the widget's top-left origin. | |
| 160 void ConvertWidgetPointToScreen(GtkWidget* widget, gfx::Point* p); | |
| 161 | |
| 162 // Initialize some GTK settings so that our dialogs are consistent. | |
| 163 void InitRCStyles(); | |
| 164 | |
| 165 // Stick the widget in the given hbox without expanding vertically. The widget | |
| 166 // is packed at the start of the hbox. This is useful for widgets that would | |
| 167 // otherwise expand to fill the vertical space of the hbox | |
| 168 // (e.g. buttons). Returns the vbox that widget was packed in. | |
| 169 GtkWidget* CenterWidgetInHBox(GtkWidget* hbox, GtkWidget* widget, | |
| 170 bool pack_at_end, int padding); | |
| 171 | |
| 172 // Returns true if the screen is composited, false otherwise. | |
| 173 bool IsScreenComposited(); | |
| 174 | |
| 175 // Enumerates the top-level gdk windows of the current display. | |
| 176 void EnumerateTopLevelWindows(x11_util::EnumerateWindowsDelegate* delegate); | |
| 177 | |
| 178 // Set that clicking the button with the given mouse buttons will cause a click | |
| 179 // event. | |
| 180 // NOTE: If you need to connect to the button-press-event or | |
| 181 // button-release-event signals, do so before calling this function. | |
| 182 void SetButtonClickableByMouseButtons(GtkWidget* button, | |
| 183 bool left, bool middle, bool right); | |
| 184 | |
| 185 // Set that a button causes a page navigation. In particular, it will accept | |
| 186 // middle clicks. Warning: only call this *after* you have connected your | |
| 187 // own handlers for button-press and button-release events, or you will not get | |
| 188 // those events. | |
| 189 void SetButtonTriggersNavigation(GtkWidget* button); | |
| 190 | |
| 191 // Returns the mirrored x value for |bounds| if the layout is RTL; otherwise, | |
| 192 // the original value is returned unchanged. | |
| 193 int MirroredLeftPointForRect(GtkWidget* widget, const gfx::Rect& bounds); | |
| 194 | |
| 195 // Returns the mirrored x value for the point |x| if the layout is RTL; | |
| 196 // otherwise, the original value is returned unchanged. | |
| 197 int MirroredXCoordinate(GtkWidget* widget, int x); | |
| 198 | |
| 199 // Returns true if the pointer is currently inside the widget. | |
| 200 bool WidgetContainsCursor(GtkWidget* widget); | |
| 201 | |
| 202 // Sets the icon of |window| to the product icon (potentially used in window | |
| 203 // border or alt-tab list). | |
| 204 void SetWindowIcon(GtkWindow* window); | |
| 205 | |
| 206 // Sets the default window icon for windows created in this app. | |
| 207 void SetDefaultWindowIcon(); | |
| 208 | |
| 209 // Adds an action button with the given text to the dialog. Only useful when you | |
| 210 // want a stock icon but not the stock text to go with it. Returns the button. | |
| 211 GtkWidget* AddButtonToDialog(GtkWidget* dialog, const gchar* text, | |
| 212 const gchar* stock_id, gint response_id); | |
| 213 | |
| 214 GtkWidget* BuildDialogButton(GtkWidget* dialog, int ids_id, | |
| 215 const gchar* stock_id); | |
| 216 | |
| 217 GtkWidget* CreateEntryImageHBox(GtkWidget* entry, GtkWidget* image); | |
| 218 | |
| 219 // Sets all the foreground color states of |label| to |color|. | |
| 220 void SetLabelColor(GtkWidget* label, const GdkColor* color); | |
| 221 | |
| 222 // Adds the given widget to an alignment identing it by |kGroupIndent|. | |
| 223 GtkWidget* IndentWidget(GtkWidget* content); | |
| 224 | |
| 225 // Sets (or resets) the font settings in |prefs| (used when creating new | |
| 226 // renderers) based on GtkSettings (which itself comes from XSETTINGS). | |
| 227 void UpdateGtkFontSettings(RendererPreferences* prefs); | |
| 228 | |
| 229 // Get the current location of the mouse cursor relative to the screen. | |
| 230 gfx::Point ScreenPoint(GtkWidget* widget); | |
| 231 | |
| 232 // Get the current location of the mouse cursor relative to the widget. | |
| 233 gfx::Point ClientPoint(GtkWidget* widget); | |
| 234 | |
| 235 // Reverses a point in RTL mode. Used in making vectors of GdkPoints for window | |
| 236 // shapes. | |
| 237 GdkPoint MakeBidiGdkPoint(gint x, gint y, gint width, bool ltr); | |
| 238 | |
| 239 // Draws a GTK text entry with the style parameters of GtkEntry | |
| 240 // |offscreen_entry| onto |widget_to_draw_on| in the rectangle |rec|. Drawing | |
| 241 // is only done in the clip rectangle |dirty_rec|. | |
| 242 void DrawTextEntryBackground(GtkWidget* offscreen_entry, | |
| 243 GtkWidget* widget_to_draw_on, | |
| 244 GdkRectangle* dirty_rec, | |
| 245 GdkRectangle* rec); | |
| 246 | |
| 247 // Draws the background of the toolbar area subject to the expose rectangle | |
| 248 // |event| and starting image tiling from |tabstrip_origin|. | |
| 249 void DrawThemedToolbarBackground(GtkWidget* widget, | |
| 250 cairo_t* cr, | |
| 251 GdkEventExpose* event, | |
| 252 const gfx::Point& tabstrip_origin, | |
| 253 GtkThemeProvider* provider); | |
| 254 | |
| 255 // Returns the two colors averaged together. | |
| 256 GdkColor AverageColors(GdkColor color_one, GdkColor color_two); | |
| 257 | |
| 258 // Show the image for the given menu item, even if the user's default is to not | |
| 259 // show images. Only to be used for favicons or other menus where the image is | |
| 260 // crucial to its functionality. | |
| 261 void SetAlwaysShowImage(GtkWidget* image_menu_item); | |
| 262 | |
| 263 // Stacks a |popup| window directly on top of a |toplevel| window. | |
| 264 void StackPopupWindow(GtkWidget* popup, GtkWidget* toplevel); | |
| 265 | |
| 266 // Get a rectangle corresponding to a widget's allocation relative to its | |
| 267 // toplevel window's origin. | |
| 268 gfx::Rect GetWidgetRectRelativeToToplevel(GtkWidget* widget); | |
| 269 | |
| 270 // Don't allow the widget to paint anything, and instead propagate the expose | |
| 271 // to its children. This is similar to calling | |
| 272 // | |
| 273 // gtk_widget_set_app_paintable(container, TRUE); | |
| 274 // | |
| 275 // except that it will always work, and it should be called after any custom | |
| 276 // expose events are connected. | |
| 277 void SuppressDefaultPainting(GtkWidget* container); | |
| 278 | |
| 279 // Get the window open disposition from the state in gtk_get_current_event(). | |
| 280 // This is designed to be called inside a "clicked" event handler. It is an | |
| 281 // error to call it when gtk_get_current_event() won't return a GdkEventButton*. | |
| 282 WindowOpenDisposition DispositionForCurrentButtonPressEvent(); | |
| 283 | |
| 284 // Safely grabs all input (with X grabs and an application grab), returning true | |
| 285 // for success. | |
| 286 bool GrabAllInput(GtkWidget* widget); | |
| 287 | |
| 288 // Returns a rectangle that represents the widget's bounds. The rectangle it | |
| 289 // returns is the same as widget->allocation, but anchored at (0, 0). | |
| 290 gfx::Rect WidgetBounds(GtkWidget* widget); | |
| 291 | |
| 292 // Update the timestamp for the given window. This is usually the time of the | |
| 293 // last user event, but on rare occasions we wish to update it despite not | |
| 294 // receiving a user event. | |
| 295 void SetWMLastUserActionTime(GtkWindow* window); | |
| 296 | |
| 297 // The current system time, using the format expected by the X server, but not | |
| 298 // retrieved from the X server. NOTE: You should almost never need to use this | |
| 299 // function, instead using the timestamp from the latest GDK event. | |
| 300 guint32 XTimeNow(); | |
| 301 | |
| 302 // Uses the autocomplete controller for |profile| to convert the contents of the | |
| 303 // PRIMARY selection to a parsed URL. Returns true and sets |url| on success, | |
| 304 // otherwise returns false. | |
| 305 bool URLFromPrimarySelection(Profile* profile, GURL* url); | |
| 306 | |
| 307 // Set the colormap of the given window to rgba to allow transparency. | |
| 308 bool AddWindowAlphaChannel(GtkWidget* window); | |
| 309 | |
| 310 // Get the default colors for a text entry. Parameters may be NULL. | |
| 311 void GetTextColors(GdkColor* normal_base, | |
| 312 GdkColor* selected_base, | |
| 313 GdkColor* normal_text, | |
| 314 GdkColor* selected_text); | |
| 315 | |
| 316 // Wrappers to show a GtkDialog. On Linux, it merely calls gtk_widget_show_all. | |
| 317 // On ChromeOs, it calls ShowNativeDialog which hosts the its vbox | |
| 318 // in a view based Window. | |
| 319 void ShowDialog(GtkWidget* dialog); | |
| 320 void ShowDialogWithLocalizedSize(GtkWidget* dialog, | |
| 321 int width_id, | |
| 322 int height_id, | |
| 323 bool resizeable); | |
| 324 void ShowModalDialogWithMinLocalizedWidth(GtkWidget* dialog, | |
| 325 int width_id); | |
| 326 | |
| 327 // Wrapper to present a window. On Linux, it just calls gtk_window_present or | |
| 328 // gtk_window_present_with_time for non-zero timestamp. For ChromeOS, it first | |
| 329 // finds the host window of the dialog contents and then present it. | |
| 330 void PresentWindow(GtkWidget* window, int timestamp); | |
| 331 | |
| 332 // Get real window for given dialog. On ChromeOS, this gives the native dialog | |
| 333 // host window. On Linux, it merely returns the passed in dialog. | |
| 334 GtkWindow* GetDialogWindow(GtkWidget* dialog); | |
| 335 | |
| 336 // Gets dialog window bounds. | |
| 337 gfx::Rect GetDialogBounds(GtkWidget* dialog); | |
| 338 | |
| 339 // Returns the stock menu item label for the "preferences" item - returns an | |
| 340 // empty string if no stock item found. | |
| 341 string16 GetStockPreferencesMenuLabel(); | |
| 342 | |
| 343 // Checks whether a widget is actually visible, i.e. whether it and all its | |
| 344 // ancestors up to its toplevel are visible. | |
| 345 bool IsWidgetAncestryVisible(GtkWidget* widget); | |
| 346 | |
| 347 // Sets the GTK font from the given font name (ex. "Arial Black, 10"). | |
| 348 void SetGtkFont(const std::string& font_name); | |
| 349 | |
| 350 // Sets the given label's size request to |pixel_width|. This will cause the | |
| 351 // label to wrap if it needs to. The reason for this function is that some | |
| 352 // versions of GTK mis-align labels that have a size request and line wrapping, | |
| 353 // and this function hides the complexity of the workaround. | |
| 354 void SetLabelWidth(GtkWidget* label, int pixel_width); | |
| 355 | |
| 356 // Make the |label| shrinkable within a GthChromeShrinkableHBox | |
| 357 // It calculates the real size request of a label and set its ellipsize mode to | |
| 358 // PANGO_ELLIPSIZE_END. | |
| 359 // It must be done when the label is mapped (become visible on the screen), | |
| 360 // to make sure the pango can get correct font information for the calculation. | |
| 361 void InitLabelSizeRequestAndEllipsizeMode(GtkWidget* label); | |
| 362 | |
| 363 // Convenience methods for converting between web drag operations and the GDK | |
| 364 // equivalent. | |
| 365 GdkDragAction WebDragOpToGdkDragAction(WebKit::WebDragOperationsMask op); | |
| 366 WebKit::WebDragOperationsMask GdkDragActionToWebDragOp(GdkDragAction action); | |
| 367 | |
| 368 // A helper function for gtk_message_dialog_new() to work around a few KDE 3 | |
| 369 // window manager bugs. You should always call it after creating a dialog with | |
| 370 // gtk_message_dialog_new. | |
| 371 void ApplyMessageDialogQuirks(GtkWidget* dialog); | |
| 372 | |
| 373 // Performs Cut/Copy/Paste operation on the |window|. | |
| 374 void DoCut(BrowserWindow* window); | |
| 375 void DoCopy(BrowserWindow* window); | |
| 376 void DoPaste(BrowserWindow* window); | |
| 377 | |
| 378 } // namespace gtk_util | |
| 379 | 11 |
| 380 #endif // CHROME_BROWSER_GTK_GTK_UTIL_H_ | 12 #endif // CHROME_BROWSER_GTK_GTK_UTIL_H_ |
| OLD | NEW |