diff options
author | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-07 02:06:01 +0000 |
---|---|---|
committer | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-07 02:06:01 +0000 |
commit | 4a6bef33b6897ceff27eee304afe1a6da5fee4ff (patch) | |
tree | 64c7e2cb508ccbe6a9e50be1da7c99b016ea5b52 | |
parent | ccb824344b58ae4acbc0b7b30ebcba048a032836 (diff) | |
download | chromium_src-4a6bef33b6897ceff27eee304afe1a6da5fee4ff.zip chromium_src-4a6bef33b6897ceff27eee304afe1a6da5fee4ff.tar.gz chromium_src-4a6bef33b6897ceff27eee304afe1a6da5fee4ff.tar.bz2 |
aura: A few changes to have aura_demo compile and run on linux.
* Make 'aura' a component, and export Desktop and Window.
* Events (Key and Mouse) from X Events.
* Rip non-gtk bits out of gfx/gtk_util into gfx/linux_util
BUG=93934,93933
TEST=none
Review URL: http://codereview.chromium.org/7833016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99898 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | aura/aura.gyp | 6 | ||||
-rw-r--r-- | aura/aura_export.h | 30 | ||||
-rw-r--r-- | aura/demo/demo_main.cc | 5 | ||||
-rw-r--r-- | aura/desktop.h | 3 | ||||
-rw-r--r-- | aura/desktop_host_linux.cc | 24 | ||||
-rw-r--r-- | aura/event.h | 5 | ||||
-rw-r--r-- | aura/event_x.cc | 193 | ||||
-rw-r--r-- | aura/window.h | 3 | ||||
-rw-r--r-- | base/base.gypi | 2 | ||||
-rw-r--r-- | base/message_loop.cc | 2 | ||||
-rw-r--r-- | base/message_loop.h | 2 | ||||
-rw-r--r-- | ui/gfx/gtk_util.cc | 80 | ||||
-rw-r--r-- | ui/gfx/gtk_util.h | 44 | ||||
-rw-r--r-- | ui/gfx/image/image.cc | 5 | ||||
-rw-r--r-- | ui/gfx/linux_util.cc | 99 | ||||
-rw-r--r-- | ui/gfx/linux_util.h | 66 | ||||
-rw-r--r-- | ui/gfx/platform_font_gtk.cc | 2 | ||||
-rw-r--r-- | ui/gfx/screen_aura.cc | 4 | ||||
-rw-r--r-- | ui/ui.gyp | 8 | ||||
-rw-r--r-- | views/metrics_aura.cc | 4 | ||||
-rw-r--r-- | views/views_export.h | 2 |
21 files changed, 456 insertions, 133 deletions
diff --git a/aura/aura.gyp b/aura/aura.gyp index de8586f..6b22ee4 100644 --- a/aura/aura.gyp +++ b/aura/aura.gyp @@ -9,7 +9,7 @@ 'targets': [ { 'target_name': 'aura', - 'type': 'static_library', + 'type': '<(component)', 'dependencies': [ '../base/base.gyp:base', '../base/base.gyp:base_i18n', @@ -19,6 +19,9 @@ '../ui/ui.gyp:ui', '../ui/ui.gyp:ui_resources', ], + 'defines': [ + 'AURA_IMPLEMENTATION', + ], 'sources': [ 'desktop_host.h', 'desktop_host_linux.cc', @@ -29,6 +32,7 @@ 'event.cc', 'event.h', 'event_win.cc', + 'event_x.cc', 'focus_manager.cc', 'focus_manager.h', 'hit_test.h', diff --git a/aura/aura_export.h b/aura/aura_export.h new file mode 100644 index 0000000..1ebab90 --- /dev/null +++ b/aura/aura_export.h @@ -0,0 +1,30 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef AURA_AURA_EXPORT_H +#define AURA_AURA_EXPORT_H +#pragma once + +// Defines AURA_EXPORT so that functionality implemented by the aura module +// can be exported to consumers. + +#if defined(COMPONENT_BUILD) +#if defined(WIN32) + +#if defined(AURA_IMPLEMENTATION) +#define AURA_EXPORT __declspec(dllexport) +#else +#define AURA_EXPORT __declspec(dllimport) +#endif // defined(AURA_IMPLEMENTATION) + +#else // defined(WIN32) +#define AURA_EXPORT __attribute__((visibility("default"))) +#endif + +#else // defined(COMPONENT_BUILD) +#define AURA_EXPORT +#endif + +#endif // AURA_AURA_EXPORT_H + diff --git a/aura/demo/demo_main.cc b/aura/demo/demo_main.cc index 7b87559..85a39cc 100644 --- a/aura/demo/demo_main.cc +++ b/aura/demo/demo_main.cc @@ -16,6 +16,11 @@ #include "ui/gfx/canvas_skia.h" #include "ui/gfx/rect.h" +#if defined(USE_X11) +#include "aura/hit_test.h" +#include "base/message_pump_x.h" +#endif + namespace { // Trivial WindowDelegate implementation that draws a colored background. diff --git a/aura/desktop.h b/aura/desktop.h index 88f02f3c..3d292af 100644 --- a/aura/desktop.h +++ b/aura/desktop.h @@ -7,6 +7,7 @@ #pragma once #include "aura/root_window.h" +#include "aura/aura_export.h" #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "ui/gfx/native_widget_types.h" @@ -25,7 +26,7 @@ class DesktopHost; class MouseEvent; // Desktop is responsible for hosting a set of windows. -class Desktop { +class AURA_EXPORT Desktop { public: Desktop(); ~Desktop(); diff --git a/aura/desktop_host_linux.cc b/aura/desktop_host_linux.cc index 746be03..723e571 100644 --- a/aura/desktop_host_linux.cc +++ b/aura/desktop_host_linux.cc @@ -5,6 +5,7 @@ #include "aura/desktop_host.h" #include "aura/desktop.h" +#include "aura/event.h" #include "base/message_loop.h" #include "base/message_pump_x.h" @@ -28,6 +29,7 @@ class DesktopHostLinux : public DesktopHost { virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; virtual void Show() OVERRIDE; virtual gfx::Size GetSize() OVERRIDE; + virtual void SetSize(const gfx::Size& size) OVERRIDE; Desktop* desktop_; @@ -68,13 +70,27 @@ DesktopHostLinux::~DesktopHostLinux() { base::MessagePumpDispatcher::DispatchStatus DesktopHostLinux::Dispatch( XEvent* xev) { - // TODO(sad): Create events and dispatch to the appropriate window. + bool handled = false; switch (xev->type) { case Expose: desktop_->Draw(); + handled = true; break; + case KeyPress: + case KeyRelease: { + KeyEvent keyev(xev); + handled = desktop_->OnKeyEvent(keyev); + break; + } + case ButtonPress: + case ButtonRelease: + case MotionNotify: { + MouseEvent mouseev(xev); + handled = desktop_->OnMouseEvent(mouseev); + break; + } } - return EVENT_IGNORED; + return handled ? EVENT_PROCESSED : EVENT_IGNORED; } void DesktopHostLinux::SetDesktop(Desktop* desktop) { @@ -92,6 +108,10 @@ gfx::Size DesktopHostLinux::GetSize() { return bounds_.size(); } +void DesktopHostLinux::SetSize(const gfx::Size& size) { + XResizeWindow(xdisplay_, xwindow_, size.width(), size.height()); +} + } // namespace // static diff --git a/aura/event.h b/aura/event.h index 27a9843..884f9b0 100644 --- a/aura/event.h +++ b/aura/event.h @@ -12,12 +12,15 @@ #include "ui/base/keycodes/keyboard_codes.h" #include "ui/gfx/point.h" +#if defined(USE_X11) +typedef union _XEvent XEvent; +#endif + namespace aura { #if defined(OS_WIN) typedef MSG NativeEvent; #elif defined(USE_X11) -typedef union _XEvent XEvent; typedef XEvent* NativeEvent; #endif diff --git a/aura/event_x.cc b/aura/event_x.cc new file mode 100644 index 0000000..793be36 --- /dev/null +++ b/aura/event_x.cc @@ -0,0 +1,193 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "aura/event.h" + +#include <X11/Xlib.h> +#include <X11/extensions/XInput2.h> + +#include "base/logging.h" +#include "ui/base/keycodes/keyboard_code_conversion_x.h" + +namespace aura { + +namespace { + +int GetEventFlagsFromXState(unsigned int state) { + int flags = 0; + if (state & ControlMask) + flags |= ui::EF_CONTROL_DOWN; + if (state & ShiftMask) + flags |= ui::EF_SHIFT_DOWN; + if (state & Mod1Mask) + flags |= ui::EF_ALT_DOWN; + if (state & LockMask) + flags |= ui::EF_CAPS_LOCK_DOWN; + if (state & Button1Mask) + flags |= ui::EF_LEFT_BUTTON_DOWN; + if (state & Button2Mask) + flags |= ui::EF_MIDDLE_BUTTON_DOWN; + if (state & Button3Mask) + flags |= ui::EF_RIGHT_BUTTON_DOWN; + + return flags; +} + +// Get the event flag for the button in XButtonEvent. During a ButtonPress +// event, |state| in XButtonEvent does not include the button that has just been +// pressed. Instead |state| contains flags for the buttons (if any) that had +// already been pressed before the current button, and |button| stores the most +// current pressed button. So, if you press down left mouse button, and while +// pressing it down, press down the right mouse button, then for the latter +// event, |state| would have Button1Mask set but not Button3Mask, and |button| +// would be 3. +int GetEventFlagsForButton(int button) { + switch (button) { + case 1: + return ui::EF_LEFT_BUTTON_DOWN; + case 2: + return ui::EF_MIDDLE_BUTTON_DOWN; + case 3: + return ui::EF_RIGHT_BUTTON_DOWN; + } + + DLOG(WARNING) << "Unexpected button (" << button << ") received."; + return 0; +} + +int GetButtonMaskForX2Event(XIDeviceEvent* xievent) { + int buttonflags = 0; + + for (int i = 0; i < 8 * xievent->buttons.mask_len; i++) { + if (XIMaskIsSet(xievent->buttons.mask, i)) { + buttonflags |= GetEventFlagsForButton(i); + } + } + + return buttonflags; +} + +ui::EventType EventTypeFromNative(NativeEvent native_event) { + switch (native_event->type) { + case KeyPress: + return ui::ET_KEY_PRESSED; + case KeyRelease: + return ui::ET_KEY_RELEASED; + case ButtonPress: + if (native_event->xbutton.button == 4 || + native_event->xbutton.button == 5) + return ui::ET_MOUSEWHEEL; + return ui::ET_MOUSE_PRESSED; + case ButtonRelease: + if (native_event->xbutton.button == 4 || + native_event->xbutton.button == 5) + return ui::ET_MOUSEWHEEL; + return ui::ET_MOUSE_RELEASED; + case MotionNotify: + if (native_event->xmotion.state & + (Button1Mask | Button2Mask | Button3Mask)) + return ui::ET_MOUSE_DRAGGED; + return ui::ET_MOUSE_MOVED; + case GenericEvent: { + XIDeviceEvent* xievent = + static_cast<XIDeviceEvent*>(native_event->xcookie.data); + // TODO(sad): Determine if sourceid is a touch device. + switch (xievent->evtype) { + case XI_ButtonPress: + return (xievent->detail == 4 || xievent->detail == 5) ? + ui::ET_MOUSEWHEEL : ui::ET_MOUSE_PRESSED; + case XI_ButtonRelease: + return (xievent->detail == 4 || xievent->detail == 5) ? + ui::ET_MOUSEWHEEL : ui::ET_MOUSE_RELEASED; + case XI_Motion: + return GetButtonMaskForX2Event(xievent) ? ui::ET_MOUSE_DRAGGED : + ui::ET_MOUSE_MOVED; + } + } + default: + NOTREACHED(); + break; + } + return ui::ET_UNKNOWN; +} + +gfx::Point GetEventLocation(XEvent* xev) { + switch (xev->type) { + case ButtonPress: + case ButtonRelease: + return gfx::Point(xev->xbutton.x, xev->xbutton.y); + + case MotionNotify: + return gfx::Point(xev->xmotion.x, xev->xmotion.y); + + case GenericEvent: { + XIDeviceEvent* xievent = + static_cast<XIDeviceEvent*>(xev->xcookie.data); + return gfx::Point(static_cast<int>(xievent->event_x), + static_cast<int>(xievent->event_y)); + } + } + + return gfx::Point(); +} + +int GetLocatedEventFlags(XEvent* xev) { + switch (xev->type) { + case ButtonPress: + case ButtonRelease: + return GetEventFlagsFromXState(xev->xbutton.state) | + GetEventFlagsForButton(xev->xbutton.button); + + case MotionNotify: + return GetEventFlagsFromXState(xev->xmotion.state); + + case GenericEvent: { + XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(xev->xcookie.data); + bool touch = false; // TODO(sad): Determine if xievent->sourceid is a + // touch device. + switch (xievent->evtype) { + case XI_ButtonPress: + case XI_ButtonRelease: + return GetButtonMaskForX2Event(xievent) | + GetEventFlagsFromXState(xievent->mods.effective) | + (touch ? 0 : GetEventFlagsForButton(xievent->detail)); + + case XI_Motion: + return GetButtonMaskForX2Event(xievent) | + GetEventFlagsFromXState(xievent->mods.effective); + } + } + } + + return 0; +} + +} // namespace + +void Event::Init() { + memset(&native_event_, 0, sizeof(native_event_)); +} + +void Event::InitWithNativeEvent(NativeEvent native_event) { + native_event_ = native_event; +} + +LocatedEvent::LocatedEvent(NativeEvent native_event) + : Event(native_event, EventTypeFromNative(native_event), + GetLocatedEventFlags(native_event)), + location_(GetEventLocation(native_event)) { +} + +MouseEvent::MouseEvent(NativeEvent native_event) + : LocatedEvent(native_event) { +} + +KeyEvent::KeyEvent(NativeEvent native_event) + : Event(native_event, + EventTypeFromNative(native_event), + GetEventFlagsFromXState(native_event->xbutton.state)), + key_code_(ui::KeyboardCodeFromXKeyEvent(native_event)) { +} + +} // namespace aura diff --git a/aura/window.h b/aura/window.h index 5ebfb1a..d5d6e65 100644 --- a/aura/window.h +++ b/aura/window.h @@ -8,6 +8,7 @@ #include <vector> +#include "aura/aura_export.h" #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "ui/gfx/compositor/layer_delegate.h" @@ -35,7 +36,7 @@ class FocusManager; // Aura window implementation. Interesting events are sent to the // WindowDelegate. // TODO(beng): resolve ownership. -class Window : public ui::LayerDelegate { +class AURA_EXPORT Window : public ui::LayerDelegate { public: enum Visibility { // Don't display the window onscreen and don't let it receive mouse diff --git a/base/base.gypi b/base/base.gypi index 5aed38c..f1fbb98 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -383,7 +383,7 @@ 'message_pump_x.cc', ], }], - [ 'touchui==0', { + [ 'touchui==0 and use_aura==0', { 'sources!' : [ 'message_pump_x.cc', ], }, { 'sources!' : [ 'message_pump_gtk.cc', ], diff --git a/base/message_loop.cc b/base/message_loop.cc index 19ce904..4a206e9 100644 --- a/base/message_loop.cc +++ b/base/message_loop.cc @@ -151,7 +151,7 @@ MessageLoop::MessageLoop(Type type) #elif defined(USE_WAYLAND) #define MESSAGE_PUMP_UI new base::MessagePumpWayland() #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() -#elif defined(TOUCH_UI) +#elif defined(TOUCH_UI) || defined(USE_AURA) #define MESSAGE_PUMP_UI new base::MessagePumpX() #define MESSAGE_PUMP_IO new base::MessagePumpLibevent() #elif defined(OS_NACL) diff --git a/base/message_loop.h b/base/message_loop.h index 601120f..4742b5f 100644 --- a/base/message_loop.h +++ b/base/message_loop.h @@ -31,7 +31,7 @@ #if defined(USE_WAYLAND) #include "base/message_pump_wayland.h" -#elif defined(TOUCH_UI) +#elif defined(TOUCH_UI) || defined(USE_AURA) #include "base/message_pump_x.h" #else #include "base/message_pump_gtk.h" diff --git a/ui/gfx/gtk_util.cc b/ui/gfx/gtk_util.cc index 45ae56a..aa336e8 100644 --- a/ui/gfx/gtk_util.cc +++ b/ui/gfx/gtk_util.cc @@ -55,34 +55,6 @@ void FreePixels(guchar* pixels, gpointer data) { free(data); } -// Common implementation of ConvertAcceleratorsFromWindowsStyle() and -// RemoveWindowsStyleAccelerators(). -// Replaces all ampersands (as used in our grd files to indicate mnemonics) -// to |target|. Similarly any underscores get replaced with two underscores as -// is needed by GTK. -std::string ConvertAmperstandsTo(const std::string& label, - const std::string& target) { - std::string ret; - ret.reserve(label.length() * 2); - for (size_t i = 0; i < label.length(); ++i) { - if ('_' == label[i]) { - ret.push_back('_'); - ret.push_back('_'); - } else if ('&' == label[i]) { - if (i + 1 < label.length() && '&' == label[i + 1]) { - ret.push_back('&'); - ++i; - } else { - ret.append(target); - } - } else { - ret.push_back(label[i]); - } - } - - return ret; -} - } // namespace namespace gfx { @@ -164,60 +136,16 @@ void SubtractRectanglesFromRegion(GdkRegion* region, } } -PangoContext* GetPangoContext() { -#if defined(USE_WAYLAND) - PangoFontMap* font_map = pango_cairo_font_map_get_default(); - PangoContext* default_context = pango_font_map_create_context(font_map); -#else - PangoContext* default_context = gdk_pango_context_get(); -#endif - return default_context; -} - -double GetPangoResolution() { - static double resolution; - static bool determined_resolution = false; - if (!determined_resolution) { - determined_resolution = true; - PangoContext* default_context = GetPangoContext(); - resolution = pango_cairo_context_get_resolution(default_context); - g_object_unref(default_context); - } - return resolution; -} - GdkCursor* GetCursor(int type) { static GdkCursorCache impl; return impl.GetCursorImpl(static_cast<GdkCursorType>(type)); } -std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { - return ConvertAmperstandsTo(label, "_"); -} - -std::string RemoveWindowsStyleAccelerators(const std::string& label) { - return ConvertAmperstandsTo(label, ""); -} - -uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { - if (stride == 0) - stride = width * 4; - - uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); - - // We have to copy the pixels and swap from BGRA to RGBA. - for (int i = 0; i < height; ++i) { - for (int j = 0; j < width; ++j) { - int idx = i * stride + j * 4; - new_pixels[idx] = pixels[idx + 2]; - new_pixels[idx + 1] = pixels[idx + 1]; - new_pixels[idx + 2] = pixels[idx]; - new_pixels[idx + 3] = pixels[idx + 3]; - } - } - - return new_pixels; +#if !defined(USE_WAYLAND) && !defined(USE_AURA) +PangoContext* GetPangoContext() { + return gdk_pango_context_get(); } +#endif void InitRCStyles() { static const char kRCText[] = diff --git a/ui/gfx/gtk_util.h b/ui/gfx/gtk_util.h index 62f9064..c519942 100644 --- a/ui/gfx/gtk_util.h +++ b/ui/gfx/gtk_util.h @@ -14,13 +14,12 @@ #include "base/memory/scoped_ptr.h" #include "ui/base/ui_export.h" +#include "ui/gfx/linux_util.h" typedef struct _GdkPixbuf GdkPixbuf; typedef struct _GdkRegion GdkRegion; typedef struct _GdkCursor GdkCursor; -typedef struct _PangoContext PangoContext; - class CommandLine; class SkBitmap; @@ -42,54 +41,13 @@ UI_EXPORT GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap); UI_EXPORT void SubtractRectanglesFromRegion(GdkRegion* region, const std::vector<Rect>& cutouts); -// Creates and returns a PangoContext. The caller owns the context. -PangoContext* GetPangoContext(); - -// Returns the resolution (DPI) used by pango. A negative values means the -// resolution hasn't been set. -double GetPangoResolution(); - // Returns a static instance of a GdkCursor* object, sharable across the // process. Caller must gdk_cursor_ref() it if they want to assume ownership. UI_EXPORT GdkCursor* GetCursor(int type); -// Change windows accelerator style to GTK style. (GTK uses _ for -// accelerators. Windows uses & with && as an escape for &.) -UI_EXPORT std::string ConvertAcceleratorsFromWindowsStyle( - const std::string& label); - -// Removes the "&" accelerators from a Windows label. -UI_EXPORT std::string RemoveWindowsStyleAccelerators(const std::string& label); - -// Makes a copy of |pixels| with the ordering changed from BGRA to RGBA. -// The caller is responsible for free()ing the data. If |stride| is 0, it's -// assumed to be 4 * |width|. -uint8_t* BGRAToRGBA(const uint8_t* pixels, - int width, - int height, - int stride); - // Initialize some GTK settings so that our dialogs are consistent. UI_EXPORT void InitRCStyles(); } // namespace gfx -// It's not legal C++ to have a templatized typedefs, so we wrap it in a -// struct. When using this, you need to include ::Type. E.g., -// ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); -template<class T> -struct ScopedGObject { - // A helper class that will g_object_unref |p| when it goes out of scope. - // This never adds a ref, it only unrefs. - template<class U> - struct GObjectUnrefer { - void operator()(U* ptr) const { - if (ptr) - g_object_unref(ptr); - } - }; - - typedef scoped_ptr_malloc<T, GObjectUnrefer<T> > Type; -}; - #endif // UI_GFX_GTK_UTIL_H_ diff --git a/ui/gfx/image/image.cc b/ui/gfx/image/image.cc index 9bd6842..7dc6e62 100644 --- a/ui/gfx/image/image.cc +++ b/ui/gfx/image/image.cc @@ -354,7 +354,10 @@ internal::ImageRep* Image::GetRepresentation( if (default_rep->type() == Image::kImageRepSkia) { internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); internal::ImageRep* native_rep = NULL; -#if defined(TOOLKIT_USES_GTK) +#if defined(USE_AURA) + skia_rep = NULL; + NOTIMPLEMENTED(); +#elif defined(TOOLKIT_USES_GTK) if (rep_type == Image::kImageRepGdk) { GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); native_rep = new internal::ImageRepGdk(pixbuf); diff --git a/ui/gfx/linux_util.cc b/ui/gfx/linux_util.cc new file mode 100644 index 0000000..a189233 --- /dev/null +++ b/ui/gfx/linux_util.cc @@ -0,0 +1,99 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/linux_util.h" + +#include <pango/pango.h> +#include <pango/pangocairo.h> +#include <stdlib.h> + +#include "base/basictypes.h" +#include "base/command_line.h" +#include "base/linux_util.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkUnPreMultiply.h" +#include "ui/gfx/rect.h" + +namespace { + +// Common implementation of ConvertAcceleratorsFromWindowsStyle() and +// RemoveWindowsStyleAccelerators(). +// Replaces all ampersands (as used in our grd files to indicate mnemonics) +// to |target|. Similarly any underscores get replaced with two underscores as +// is needed by GTK. +std::string ConvertAmperstandsTo(const std::string& label, + const std::string& target) { + std::string ret; + ret.reserve(label.length() * 2); + for (size_t i = 0; i < label.length(); ++i) { + if ('_' == label[i]) { + ret.push_back('_'); + ret.push_back('_'); + } else if ('&' == label[i]) { + if (i + 1 < label.length() && '&' == label[i + 1]) { + ret.push_back('&'); + ++i; + } else { + ret.append(target); + } + } else { + ret.push_back(label[i]); + } + } + + return ret; +} + +} // namespace + +namespace gfx { + +#if defined(USE_WAYLAND) || defined(USE_AURA) +PangoContext* GetPangoContext() { + PangoFontMap* font_map = pango_cairo_font_map_get_default(); + return pango_font_map_create_context(font_map); +} +#endif + +double GetPangoResolution() { + static double resolution; + static bool determined_resolution = false; + if (!determined_resolution) { + determined_resolution = true; + PangoContext* default_context = GetPangoContext(); + resolution = pango_cairo_context_get_resolution(default_context); + g_object_unref(default_context); + } + return resolution; +} + +std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { + return ConvertAmperstandsTo(label, "_"); +} + +std::string RemoveWindowsStyleAccelerators(const std::string& label) { + return ConvertAmperstandsTo(label, ""); +} + +uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) { + if (stride == 0) + stride = width * 4; + + uint8_t* new_pixels = static_cast<uint8_t*>(malloc(height * stride)); + + // We have to copy the pixels and swap from BGRA to RGBA. + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + int idx = i * stride + j * 4; + new_pixels[idx] = pixels[idx + 2]; + new_pixels[idx + 1] = pixels[idx + 1]; + new_pixels[idx + 2] = pixels[idx]; + new_pixels[idx + 3] = pixels[idx + 3]; + } + } + + return new_pixels; +} + +} // namespace gfx diff --git a/ui/gfx/linux_util.h b/ui/gfx/linux_util.h new file mode 100644 index 0000000..1c1f3ff --- /dev/null +++ b/ui/gfx/linux_util.h @@ -0,0 +1,66 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_GFX_LINUX_UTIL_H_ +#define UI_GFX_LINUX_UTIL_H_ +#pragma once + +#include <glib-object.h> +#include <stdint.h> + +#include <string> +#include <vector> + +#include "base/memory/scoped_ptr.h" +#include "ui/base/ui_export.h" + +typedef struct _PangoContext PangoContext; + +class SkBitmap; + +namespace gfx { + +class Rect; + +// Creates and returns a PangoContext. The caller owns the context. +PangoContext* GetPangoContext(); + +// Returns the resolution (DPI) used by pango. A negative values means the +// resolution hasn't been set. +double GetPangoResolution(); + +// Change windows accelerator style to GTK style. (GTK uses _ for +// accelerators. Windows uses & with && as an escape for &.) +UI_EXPORT std::string ConvertAcceleratorsFromWindowsStyle( + const std::string& label); + +// Removes the "&" accelerators from a Windows label. +UI_EXPORT std::string RemoveWindowsStyleAccelerators(const std::string& label); + +// Makes a copy of |pixels| with the ordering changed from BGRA to RGBA. +// The caller is responsible for free()ing the data. If |stride| is 0, it's +// assumed to be 4 * |width|. +uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride); + +} // namespace gfx + +// It's not legal C++ to have a templatized typedefs, so we wrap it in a +// struct. When using this, you need to include ::Type. E.g., +// ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); +template<class T> +struct ScopedGObject { + // A helper class that will g_object_unref |p| when it goes out of scope. + // This never adds a ref, it only unrefs. + template<class U> + struct GObjectUnrefer { + void operator()(U* ptr) const { + if (ptr) + g_object_unref(ptr); + } + }; + + typedef scoped_ptr_malloc<T, GObjectUnrefer<T> > Type; +}; + +#endif // UI_GFX_LINUX_UTIL_H_ diff --git a/ui/gfx/platform_font_gtk.cc b/ui/gfx/platform_font_gtk.cc index e743638..05e43fe 100644 --- a/ui/gfx/platform_font_gtk.cc +++ b/ui/gfx/platform_font_gtk.cc @@ -17,7 +17,7 @@ #include "third_party/skia/include/core/SkPaint.h" #include "ui/gfx/canvas_skia.h" #include "ui/gfx/font.h" -#include "ui/gfx/gtk_util.h" +#include "ui/gfx/linux_util.h" #if !defined(USE_WAYLAND) #include <gdk/gdk.h> diff --git a/ui/gfx/screen_aura.cc b/ui/gfx/screen_aura.cc index 75b0afc..b47a832 100644 --- a/ui/gfx/screen_aura.cc +++ b/ui/gfx/screen_aura.cc @@ -4,7 +4,9 @@ #include "ui/gfx/screen.h" +#if defined(OS_WIN) #include <windows.h> +#endif #include "base/logging.h" @@ -17,6 +19,8 @@ gfx::Point Screen::GetCursorScreenPoint() { GetCursorPos(&pt); return gfx::Point(pt); #endif + NOTIMPLEMENTED(); + return gfx::Point(); } // static @@ -294,9 +294,11 @@ 'conditions': [ ['use_aura==1', { 'sources/': [ + ['exclude', 'gfx/gtk_'], + ['exclude', 'gfx/screen_gtk.cc'], ['exclude', 'gfx/screen_win.cc'], ['exclude', 'base/win/mouse_wheel_util.cc'], - ['exclude', 'base/win/mouse_wheel_util.h'], + ['exclude', 'base/win/mouse_wheel_util.h'], ], }], ['toolkit_uses_gtk == 1', { @@ -315,6 +317,8 @@ 'gfx/gtk_preserve_window.h', 'gfx/gtk_util.cc', 'gfx/gtk_util.h', + 'gfx/linux_util.cc', + 'gfx/linux_util.h', 'gfx/native_theme_linux.cc', 'gfx/native_theme_linux.h', ], @@ -360,6 +364,8 @@ ['include', 'gfx/path_gtk.cc'], ['include', 'gfx/platform_font_gtk.cc'], ['include', 'gfx/platform_font_gtk.h'], + ['include', 'gfx/linux_util.cc'], + ['include', 'gfx/linux_util.h'], ], }], ['OS=="win"', { diff --git a/views/metrics_aura.cc b/views/metrics_aura.cc index 6c8fef4..46fb9b9 100644 --- a/views/metrics_aura.cc +++ b/views/metrics_aura.cc @@ -4,7 +4,9 @@ #include "views/metrics.h" +#if defined(OS_WIN) #include <windows.h> +#endif namespace views { @@ -24,7 +26,7 @@ int GetMenuShowDelay() { return delay; #else return 0; -#endif; +#endif } } // namespace views diff --git a/views/views_export.h b/views/views_export.h index b38c8ed..312bf4e 100644 --- a/views/views_export.h +++ b/views/views_export.h @@ -26,4 +26,4 @@ #define VIEWS_EXPORT #endif -#endif // UI_UI_EXPORT_H_ +#endif // VIEWS_VIEWS_EXPORT_H_ |