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/environment.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/accelerators/accelerator.h"
17 #include "ui/events/event_constants.h"
18 #include "ui/events/keycodes/keyboard_code_conversion_x.h"
19 #include "ui/gfx/size.h"
23 const char kAuraTransientParent
[] = "aura-transient-parent";
25 void CommonInitFromCommandLine(const CommandLine
& command_line
,
26 void (*init_func
)(gint
*, gchar
***)) {
27 const std::vector
<std::string
>& args
= command_line
.argv();
28 int argc
= args
.size();
29 scoped_ptr
<char *[]> argv(new char *[argc
+ 1]);
30 for (size_t i
= 0; i
< args
.size(); ++i
) {
31 // TODO(piman@google.com): can gtk_init modify argv? Just being safe
33 argv
[i
] = strdup(args
[i
].c_str());
36 char **argv_pointer
= argv
.get();
38 init_func(&argc
, &argv_pointer
);
39 for (size_t i
= 0; i
< args
.size(); ++i
) {
48 void GtkInitFromCommandLine(const CommandLine
& command_line
) {
49 CommonInitFromCommandLine(command_line
, gtk_init
);
52 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because
53 // of how this library is structured as a stand alone .so, we can't call code
54 // from browser and above.
55 std::string
GetDesktopName(base::Environment
* env
) {
56 #if defined(GOOGLE_CHROME_BUILD)
57 return "google-chrome.desktop";
58 #else // CHROMIUM_BUILD
59 // Allow $CHROME_DESKTOP to override the built-in value, so that development
60 // versions can set themselves as the default without interfering with
61 // non-official, packaged versions using the built-in value.
63 if (env
->GetVar("CHROME_DESKTOP", &name
) && !name
.empty())
65 return "chromium-browser.desktop";
69 void SetAlwaysShowImage(GtkWidget
* image_menu_item
) {
70 gtk_image_menu_item_set_always_show_image(
71 GTK_IMAGE_MENU_ITEM(image_menu_item
), TRUE
);
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 // Set |dialog| as transient for |parent|, which will keep it on top and center
107 // it above |parent|.
108 void SetGtkTransientForAura(GtkWidget
* dialog
, aura::Window
* parent
) {
109 gtk_widget_realize(dialog
);
110 GdkWindow
* gdk_window
= gtk_widget_get_window(dialog
);
112 // TODO(erg): Check to make sure we're using X11 if wayland or some other
113 // display server ever happens. Otherwise, this will crash.
114 XSetTransientForHint(GDK_WINDOW_XDISPLAY(gdk_window
),
115 GDK_WINDOW_XID(gdk_window
),
116 parent
->GetHost()->GetAcceleratedWidget());
118 // We also set the |parent| as a property of |dialog|, so that we can unlink
120 g_object_set_data(G_OBJECT(dialog
), kAuraTransientParent
, parent
);
123 aura::Window
* GetAuraTransientParent(GtkWidget
* dialog
) {
124 return reinterpret_cast<aura::Window
*>(
125 g_object_get_data(G_OBJECT(dialog
), kAuraTransientParent
));
128 void ClearAuraTransientParent(GtkWidget
* dialog
) {
129 g_object_set_data(G_OBJECT(dialog
), kAuraTransientParent
, NULL
);
132 } // namespace libgtk2ui