platform_accelerator_gtk.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
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 "ui/base/accelerators/platform_accelerator_gtk.h" 6 7#include "ui/events/keycodes/keyboard_code_conversion_gtk.h" 8 9namespace ui { 10 11namespace { 12 13int GdkModifierToEventFlag(GdkModifierType gdk_modifier) { 14 int event_flags = 0; 15 if (gdk_modifier & GDK_SHIFT_MASK) 16 event_flags |= EF_SHIFT_DOWN; 17 if (gdk_modifier & GDK_CONTROL_MASK) 18 event_flags |= EF_CONTROL_DOWN; 19 if (gdk_modifier & GDK_MOD1_MASK) 20 event_flags |= EF_ALT_DOWN; 21 return event_flags; 22} 23 24GdkModifierType EventFlagToGdkModifier(int event_flag) { 25 int modifier = 0; 26 if (event_flag & EF_SHIFT_DOWN) 27 modifier |= GDK_SHIFT_MASK; 28 if (event_flag & EF_CONTROL_DOWN) 29 modifier |= GDK_CONTROL_MASK; 30 if (event_flag & EF_ALT_DOWN) 31 modifier |= GDK_MOD1_MASK; 32 return static_cast<GdkModifierType>(modifier); 33} 34 35} // namespace 36 37PlatformAcceleratorGtk::PlatformAcceleratorGtk() 38 : gdk_key_code_(0), 39 gdk_modifier_(static_cast<GdkModifierType>(0)) { 40} 41 42PlatformAcceleratorGtk::PlatformAcceleratorGtk(guint gdk_key_code, 43 GdkModifierType gdk_modifier) 44 : gdk_key_code_(gdk_key_code), 45 gdk_modifier_(gdk_modifier) { 46} 47 48PlatformAcceleratorGtk::~PlatformAcceleratorGtk() { 49} 50 51scoped_ptr<PlatformAccelerator> PlatformAcceleratorGtk::CreateCopy() const { 52 scoped_ptr<PlatformAcceleratorGtk> copy(new PlatformAcceleratorGtk); 53 copy->gdk_key_code_ = gdk_key_code_; 54 copy->gdk_modifier_ = gdk_modifier_; 55 return scoped_ptr<PlatformAccelerator>(copy.release()); 56} 57 58bool PlatformAcceleratorGtk::Equals(const PlatformAccelerator& rhs) const { 59 const PlatformAcceleratorGtk& rhs_gtk = 60 static_cast<const PlatformAcceleratorGtk&>(rhs); 61 return gdk_key_code_ == rhs_gtk.gdk_key_code_ && 62 gdk_modifier_ == rhs_gtk.gdk_modifier_; 63} 64 65Accelerator AcceleratorForGdkKeyCodeAndModifier(guint gdk_key_code, 66 GdkModifierType gdk_modifier) { 67 ui::Accelerator accelerator(ui::WindowsKeyCodeForGdkKeyCode(gdk_key_code), 68 ui::GdkModifierToEventFlag(gdk_modifier)); 69 scoped_ptr<PlatformAccelerator> platform_accelerator( 70 new PlatformAcceleratorGtk(gdk_key_code, gdk_modifier)); 71 accelerator.set_platform_accelerator(platform_accelerator.Pass()); 72 return accelerator; 73} 74 75guint GetGdkKeyCodeForAccelerator(const Accelerator& accelerator) { 76 if (accelerator.platform_accelerator()) { 77 return static_cast<const PlatformAcceleratorGtk*>( 78 accelerator.platform_accelerator())->gdk_key_code(); 79 } 80 // The second parameter is false because accelerator keys are expressed in 81 // terms of the non-shift-modified key. 82 return GdkKeyCodeForWindowsKeyCode(accelerator.key_code(), false); 83} 84 85GdkModifierType GetGdkModifierForAccelerator(const Accelerator& accelerator) { 86 if (accelerator.platform_accelerator()) { 87 return static_cast<const PlatformAcceleratorGtk*>( 88 accelerator.platform_accelerator())->gdk_modifier(); 89 } 90 return EventFlagToGdkModifier(accelerator.modifiers()); 91} 92 93} // namespace ui 94