Home | History | Annotate | Download | only in libgtk2ui
      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/libgtk2ui/gtk2_util.h"
      6 
      7 #include <gtk/gtk.h>
      8 
      9 #include "base/command_line.h"
     10 #include "base/environment.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "ui/base/accelerators/accelerator.h"
     13 #include "ui/base/events/event_constants.h"
     14 #include "ui/base/keycodes/keyboard_code_conversion_x.h"
     15 #include "ui/gfx/size.h"
     16 
     17 namespace {
     18 
     19 void CommonInitFromCommandLine(const CommandLine& command_line,
     20                                void (*init_func)(gint*, gchar***)) {
     21   const std::vector<std::string>& args = command_line.argv();
     22   int argc = args.size();
     23   scoped_ptr<char *[]> argv(new char *[argc + 1]);
     24   for (size_t i = 0; i < args.size(); ++i) {
     25     // TODO(piman (at) google.com): can gtk_init modify argv? Just being safe
     26     // here.
     27     argv[i] = strdup(args[i].c_str());
     28   }
     29   argv[argc] = NULL;
     30   char **argv_pointer = argv.get();
     31 
     32   init_func(&argc, &argv_pointer);
     33   for (size_t i = 0; i < args.size(); ++i) {
     34     free(argv[i]);
     35   }
     36 }
     37 
     38 }  // namespace
     39 
     40 namespace libgtk2ui {
     41 
     42 void GtkInitFromCommandLine(const CommandLine& command_line) {
     43   CommonInitFromCommandLine(command_line, gtk_init);
     44 }
     45 
     46 // TODO(erg): This method was copied out of shell_integration_linux.cc. Because
     47 // of how this library is structured as a stand alone .so, we can't call code
     48 // from browser and above.
     49 std::string GetDesktopName(base::Environment* env) {
     50 #if defined(GOOGLE_CHROME_BUILD)
     51   return "google-chrome.desktop";
     52 #else  // CHROMIUM_BUILD
     53   // Allow $CHROME_DESKTOP to override the built-in value, so that development
     54   // versions can set themselves as the default without interfering with
     55   // non-official, packaged versions using the built-in value.
     56   std::string name;
     57   if (env->GetVar("CHROME_DESKTOP", &name) && !name.empty())
     58     return name;
     59   return "chromium-browser.desktop";
     60 #endif
     61 }
     62 
     63 void SetAlwaysShowImage(GtkWidget* image_menu_item) {
     64   gtk_image_menu_item_set_always_show_image(
     65       GTK_IMAGE_MENU_ITEM(image_menu_item), TRUE);
     66 }
     67 
     68 guint GetGdkKeyCodeForAccelerator(const ui::Accelerator& accelerator) {
     69   // The second parameter is false because accelerator keys are expressed in
     70   // terms of the non-shift-modified key.
     71   return XKeysymForWindowsKeyCode(accelerator.key_code(), false);
     72 }
     73 
     74 GdkModifierType GetGdkModifierForAccelerator(
     75     const ui::Accelerator& accelerator) {
     76   int event_flag = accelerator.modifiers();
     77   int modifier = 0;
     78   if (event_flag & ui::EF_SHIFT_DOWN)
     79     modifier |= GDK_SHIFT_MASK;
     80   if (event_flag & ui::EF_CONTROL_DOWN)
     81     modifier |= GDK_CONTROL_MASK;
     82   if (event_flag & ui::EF_ALT_DOWN)
     83     modifier |= GDK_MOD1_MASK;
     84   return static_cast<GdkModifierType>(modifier);
     85 }
     86 
     87 int EventFlagsFromGdkState(guint state) {
     88   int flags = ui::EF_NONE;
     89   flags |= (state & GDK_LOCK_MASK) ? ui::EF_CAPS_LOCK_DOWN : ui::EF_NONE;
     90   flags |= (state & GDK_CONTROL_MASK) ? ui::EF_CONTROL_DOWN : ui::EF_NONE;
     91   flags |= (state & GDK_SHIFT_MASK) ? ui::EF_SHIFT_DOWN : ui::EF_NONE;
     92   flags |= (state & GDK_MOD1_MASK) ? ui::EF_ALT_DOWN : ui::EF_NONE;
     93   flags |= (state & GDK_BUTTON1_MASK) ? ui::EF_LEFT_MOUSE_BUTTON : ui::EF_NONE;
     94   flags |=
     95       (state & GDK_BUTTON2_MASK) ? ui::EF_MIDDLE_MOUSE_BUTTON : ui::EF_NONE;
     96   flags |= (state & GDK_BUTTON3_MASK) ? ui::EF_RIGHT_MOUSE_BUTTON : ui::EF_NONE;
     97   return flags;
     98 }
     99 
    100 }  // namespace libgtk2ui
    101