Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / libgtk2ui / gtk2_util.cc
blob8bac9cb3dc39151dad6ab40234337680cb4f2ad0
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 #include "chrome/browser/ui/libgtk2ui/gtk2_util.h"
7 #include <gdk/gdk.h>
8 #include <gdk/gdkx.h>
9 #include <gtk/gtk.h>
11 #include "base/command_line.h"
12 #include "base/debug/leak_annotations.h"
13 #include "base/environment.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/base/accelerators/accelerator.h"
18 #include "ui/events/event_constants.h"
19 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
20 #include "ui/gfx/geometry/size.h"
22 namespace {
24 const char kAuraTransientParent[] = "aura-transient-parent";
26 void CommonInitFromCommandLine(const base::CommandLine& command_line,
27 void (*init_func)(gint*, gchar***)) {
28 const std::vector<std::string>& args = command_line.argv();
29 int argc = args.size();
30 scoped_ptr<char *[]> argv(new char *[argc + 1]);
31 for (size_t i = 0; i < args.size(); ++i) {
32 // TODO(piman@google.com): can gtk_init modify argv? Just being safe
33 // here.
34 argv[i] = strdup(args[i].c_str());
36 argv[argc] = NULL;
37 char **argv_pointer = argv.get();
40 // http://crbug.com/423873
41 ANNOTATE_SCOPED_MEMORY_LEAK;
42 init_func(&argc, &argv_pointer);
44 for (size_t i = 0; i < args.size(); ++i) {
45 free(argv[i]);
49 } // namespace
51 namespace libgtk2ui {
53 void GtkInitFromCommandLine(const base::CommandLine& command_line) {
54 CommonInitFromCommandLine(command_line, gtk_init);
57 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because
58 // of how this library is structured as a stand alone .so, we can't call code
59 // from browser and above.
60 std::string GetDesktopName(base::Environment* env) {
61 #if defined(GOOGLE_CHROME_BUILD)
62 return "google-chrome.desktop";
63 #else // CHROMIUM_BUILD
64 // Allow $CHROME_DESKTOP to override the built-in value, so that development
65 // versions can set themselves as the default without interfering with
66 // non-official, packaged versions using the built-in value.
67 std::string name;
68 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
69 return name;
70 return "chromium-browser.desktop";
71 #endif
74 void SetAlwaysShowImage(GtkWidget* image_menu_item) {
75 gtk_image_menu_item_set_always_show_image(
76 GTK_IMAGE_MENU_ITEM(image_menu_item), TRUE);
79 guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) {
80 // The second parameter is false because accelerator keys are expressed in
81 // terms of the non-shift-modified key.
82 return XKeysymForWindowsKeyCode(accelerator.key_code(), false);
85 GdkModifierType GetGdkModifierForAccelerator(
86 const ui::Accelerator& accelerator) {
87 int event_flag = accelerator.modifiers();
88 int modifier = 0;
89 if (event_flag & ui::EF_SHIFT_DOWN)
90 modifier |= GDK_SHIFT_MASK;
91 if (event_flag & ui::EF_CONTROL_DOWN)
92 modifier |= GDK_CONTROL_MASK;
93 if (event_flag & ui::EF_ALT_DOWN)
94 modifier |= GDK_MOD1_MASK;
95 return static_cast<GdkModifierType>(modifier);
98 int EventFlagsFromGdkState(guint state) {
99 int flags = ui::EF_NONE;
100 flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
101 flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
102 flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
103 flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE;
104 flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE;
105 flags |=
106 (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE;
107 flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE;
108 return flags;
111 void SetGtkTransientForAura(GtkWidget* dialog, aura::Window* parent) {
112 if (!parent || !parent->GetHost())
113 return;
115 gtk_widget_realize(dialog);
116 GdkWindow* gdk_window = gtk_widget_get_window(dialog);
118 // TODO(erg): Check to make sure we're using X11 if wayland or some other
119 // display server ever happens. Otherwise, this will crash.
120 XSetTransientForHint(GDK_WINDOW_XDISPLAY(gdk_window),
121 GDK_WINDOW_XID(gdk_window),
122 parent->GetHost()->GetAcceleratedWidget());
124 // We also set the |parent| as a property of |dialog|, so that we can unlink
125 // the two later.
126 g_object_set_data(G_OBJECT(dialog), kAuraTransientParent, parent);
129 aura::Window* GetAuraTransientParent(GtkWidget* dialog) {
130 return reinterpret_cast<aura::Window*>(
131 g_object_get_data(G_OBJECT(dialog), kAuraTransientParent));
134 void ClearAuraTransientParent(GtkWidget* dialog) {
135 g_object_set_data(G_OBJECT(dialog), kAuraTransientParent, NULL);
138 GtkStateType GetGtkState(ui::NativeTheme::State state) {
139 switch (state) {
140 case ui::NativeTheme::kDisabled: return GTK_STATE_INSENSITIVE;
141 case ui::NativeTheme::kHovered: return GTK_STATE_PRELIGHT;
142 case ui::NativeTheme::kNormal: return GTK_STATE_NORMAL;
143 case ui::NativeTheme::kPressed: return GTK_STATE_ACTIVE;
144 case ui::NativeTheme::kNumStates: NOTREACHED();
146 return GTK_STATE_NORMAL;
149 } // namespace libgtk2ui