OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/libgtkui/gtk2_util.h" | |
6 | |
7 #include <gdk/gdk.h> | |
8 #include <gdk/gdkx.h> | |
9 #include <gtk/gtk.h> | |
10 #include <stddef.h> | |
11 | |
12 #include <memory> | |
13 | |
14 #include "base/command_line.h" | |
15 #include "base/debug/leak_annotations.h" | |
16 #include "base/environment.h" | |
17 #include "ui/aura/window.h" | |
18 #include "ui/aura/window_tree_host.h" | |
19 #include "ui/base/accelerators/accelerator.h" | |
20 #include "ui/events/event_constants.h" | |
21 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | |
22 #include "ui/gfx/geometry/size.h" | |
23 | |
24 namespace { | |
25 | |
26 const char kAuraTransientParent[] = "aura-transient-parent"; | |
27 | |
28 void CommonInitFromCommandLine(const base::CommandLine& command_line, | |
29 void (*init_func)(gint*, gchar***)) { | |
30 const std::vector<std::string>& args = command_line.argv(); | |
31 int argc = args.size(); | |
32 std::unique_ptr<char* []> argv(new char*[argc + 1]); | |
33 for (size_t i = 0; i < args.size(); ++i) { | |
34 // TODO(piman@google.com): can gtk_init modify argv? Just being safe | |
35 // here. | |
36 argv[i] = strdup(args[i].c_str()); | |
37 } | |
38 argv[argc] = NULL; | |
39 char **argv_pointer = argv.get(); | |
40 | |
41 { | |
42 // http://crbug.com/423873 | |
43 ANNOTATE_SCOPED_MEMORY_LEAK; | |
44 init_func(&argc, &argv_pointer); | |
45 } | |
46 for (size_t i = 0; i < args.size(); ++i) { | |
47 free(argv[i]); | |
48 } | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 namespace libgtkui { | |
54 | |
55 void GtkInitFromCommandLine(const base::CommandLine& command_line) { | |
56 CommonInitFromCommandLine(command_line, gtk_init); | |
57 } | |
58 | |
59 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because | |
60 // of how this library is structured as a stand alone .so, we can't call code | |
61 // from browser and above. | |
62 std::string GetDesktopName(base::Environment* env) { | |
63 #if defined(GOOGLE_CHROME_BUILD) | |
64 return "google-chrome.desktop"; | |
65 #else // CHROMIUM_BUILD | |
66 // Allow $CHROME_DESKTOP to override the built-in value, so that development | |
67 // versions can set themselves as the default without interfering with | |
68 // non-official, packaged versions using the built-in value. | |
69 std::string name; | |
70 if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty()) | |
71 return name; | |
72 return "chromium-browser.desktop"; | |
73 #endif | |
74 } | |
75 | |
76 guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) { | |
77 // The second parameter is false because accelerator keys are expressed in | |
78 // terms of the non-shift-modified key. | |
79 return XKeysymForWindowsKeyCode(accelerator.key_code(), false); | |
80 } | |
81 | |
82 GdkModifierType GetGdkModifierForAccelerator( | |
83 const ui::Accelerator& accelerator) { | |
84 int event_flag = accelerator.modifiers(); | |
85 int modifier = 0; | |
86 if (event_flag & ui::EF_SHIFT_DOWN) | |
87 modifier |= GDK_SHIFT_MASK; | |
88 if (event_flag & ui::EF_CONTROL_DOWN) | |
89 modifier |= GDK_CONTROL_MASK; | |
90 if (event_flag & ui::EF_ALT_DOWN) | |
91 modifier |= GDK_MOD1_MASK; | |
92 return static_cast<GdkModifierType>(modifier); | |
93 } | |
94 | |
95 int EventFlagsFromGdkState(guint state) { | |
96 int flags = ui::EF_NONE; | |
97 flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE; | |
98 flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_ON : ui::EF_NONE; | |
99 flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE; | |
100 flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE; | |
101 flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE; | |
102 flags |= | |
103 (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE; | |
104 flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE; | |
105 return flags; | |
106 } | |
107 | |
108 void TurnButtonBlue(GtkWidget* button) { | |
109 #if GTK_MAJOR_VERSION == 2 | |
110 gtk_widget_set_can_default(button, true); | |
111 #else | |
112 gtk_style_context_add_class(gtk_widget_get_style_context(button), | |
113 "suggested-action"); | |
114 #endif | |
115 } | |
116 | |
117 void SetGtkTransientForAura(GtkWidget* dialog, aura::Window* parent) { | |
118 if (!parent || !parent->GetHost()) | |
119 return; | |
120 | |
121 gtk_widget_realize(dialog); | |
122 GdkWindow* gdk_window = gtk_widget_get_window(dialog); | |
123 | |
124 // TODO(erg): Check to make sure we're using X11 if wayland or some other | |
125 // display server ever happens. Otherwise, this will crash. | |
126 XSetTransientForHint(GDK_WINDOW_XDISPLAY(gdk_window), | |
127 GDK_WINDOW_XID(gdk_window), | |
128 parent->GetHost()->GetAcceleratedWidget()); | |
129 | |
130 // We also set the |parent| as a property of |dialog|, so that we can unlink | |
131 // the two later. | |
132 g_object_set_data(G_OBJECT(dialog), kAuraTransientParent, parent); | |
133 } | |
134 | |
135 aura::Window* GetAuraTransientParent(GtkWidget* dialog) { | |
136 return reinterpret_cast<aura::Window*>( | |
137 g_object_get_data(G_OBJECT(dialog), kAuraTransientParent)); | |
138 } | |
139 | |
140 void ClearAuraTransientParent(GtkWidget* dialog) { | |
141 g_object_set_data(G_OBJECT(dialog), kAuraTransientParent, NULL); | |
142 } | |
143 | |
144 } // namespace libgtkui | |
OLD | NEW |