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"
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"
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
34 argv
[i
] = strdup(args
[i
].c_str());
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
) {
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.
68 if (env
->GetVar("CHROME_DESKTOP", &name
) && !name
.empty())
70 return "chromium-browser.desktop";
74 guint
GetGdkKeyCodeForAccelerator(const ui::Accelerator
& accelerator
) {
75 // The second parameter is false because accelerator keys are expressed in
76 // terms of the non-shift-modified key.
77 return XKeysymForWindowsKeyCode(accelerator
.key_code(), false);
80 GdkModifierType
GetGdkModifierForAccelerator(
81 const ui::Accelerator
& accelerator
) {
82 int event_flag
= accelerator
.modifiers();
84 if (event_flag
& ui::EF_SHIFT_DOWN
)
85 modifier
|= GDK_SHIFT_MASK
;
86 if (event_flag
& ui::EF_CONTROL_DOWN
)
87 modifier
|= GDK_CONTROL_MASK
;
88 if (event_flag
& ui::EF_ALT_DOWN
)
89 modifier
|= GDK_MOD1_MASK
;
90 return static_cast<GdkModifierType
>(modifier
);
93 int EventFlagsFromGdkState(guint state
) {
94 int flags
= ui::EF_NONE
;
95 flags
|= (state
& GDK_LOCK_MASK
) ? ui::EF_CAPS_LOCK_DOWN
: ui::EF_NONE
;
96 flags
|= (state
& GDK_CONTROL_MASK
) ? ui::EF_CONTROL_DOWN
: ui::EF_NONE
;
97 flags
|= (state
& GDK_SHIFT_MASK
) ? ui::EF_SHIFT_DOWN
: ui::EF_NONE
;
98 flags
|= (state
& GDK_MOD1_MASK
) ? ui::EF_ALT_DOWN
: ui::EF_NONE
;
99 flags
|= (state
& GDK_BUTTON1_MASK
) ? ui::EF_LEFT_MOUSE_BUTTON
: ui::EF_NONE
;
101 (state
& GDK_BUTTON2_MASK
) ? ui::EF_MIDDLE_MOUSE_BUTTON
: ui::EF_NONE
;
102 flags
|= (state
& GDK_BUTTON3_MASK
) ? ui::EF_RIGHT_MOUSE_BUTTON
: ui::EF_NONE
;
106 void TurnButtonBlue(GtkWidget
* button
) {
107 #if GTK_MAJOR_VERSION == 2
108 gtk_widget_set_can_default(button
, true);
110 gtk_style_context_add_class(gtk_widget_get_style_context(button
),
115 void SetGtkTransientForAura(GtkWidget
* dialog
, aura::Window
* parent
) {
116 if (!parent
|| !parent
->GetHost())
119 gtk_widget_realize(dialog
);
120 GdkWindow
* gdk_window
= gtk_widget_get_window(dialog
);
122 // TODO(erg): Check to make sure we're using X11 if wayland or some other
123 // display server ever happens. Otherwise, this will crash.
124 XSetTransientForHint(GDK_WINDOW_XDISPLAY(gdk_window
),
125 GDK_WINDOW_XID(gdk_window
),
126 parent
->GetHost()->GetAcceleratedWidget());
128 // We also set the |parent| as a property of |dialog|, so that we can unlink
130 g_object_set_data(G_OBJECT(dialog
), kAuraTransientParent
, parent
);
133 aura::Window
* GetAuraTransientParent(GtkWidget
* dialog
) {
134 return reinterpret_cast<aura::Window
*>(
135 g_object_get_data(G_OBJECT(dialog
), kAuraTransientParent
));
138 void ClearAuraTransientParent(GtkWidget
* dialog
) {
139 g_object_set_data(G_OBJECT(dialog
), kAuraTransientParent
, NULL
);
142 } // namespace libgtk2ui