| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 "base/gfx/gtk_native_view_id_manager.h" | |
| 6 | |
| 7 #include "base/gfx/rect.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/rand_util.h" | |
| 10 | |
| 11 #include <gtk/gtk.h> | |
| 12 #include <gdk/gdkx.h> | |
| 13 | |
| 14 // ----------------------------------------------------------------------------- | |
| 15 // Bounce functions for GTK to callback into a C++ object... | |
| 16 | |
| 17 static void OnRealize(gfx::NativeView widget, void* arg) { | |
| 18 GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg); | |
| 19 manager->OnRealize(widget); | |
| 20 } | |
| 21 | |
| 22 static void OnUnrealize(gfx::NativeView widget, void *arg) { | |
| 23 GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg); | |
| 24 manager->OnUnrealize(widget); | |
| 25 } | |
| 26 | |
| 27 static void OnDestroy(GtkObject* obj, void* arg) { | |
| 28 GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg); | |
| 29 manager->OnDestroy(reinterpret_cast<GtkWidget*>(obj)); | |
| 30 } | |
| 31 | |
| 32 // ----------------------------------------------------------------------------- | |
| 33 | |
| 34 | |
| 35 // ----------------------------------------------------------------------------- | |
| 36 // Public functions... | |
| 37 | |
| 38 GtkNativeViewManager::GtkNativeViewManager() { | |
| 39 } | |
| 40 | |
| 41 gfx::NativeViewId GtkNativeViewManager::GetIdForWidget(gfx::NativeView widget) { | |
| 42 // This is just for unit tests: | |
| 43 if (!widget) | |
| 44 return 0; | |
| 45 | |
| 46 AutoLock locked(lock_); | |
| 47 | |
| 48 std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i = | |
| 49 native_view_to_id_.find(widget); | |
| 50 | |
| 51 if (i != native_view_to_id_.end()) | |
| 52 return i->second; | |
| 53 | |
| 54 gfx::NativeViewId new_id = | |
| 55 static_cast<gfx::NativeViewId>(base::RandUint64()); | |
| 56 while (id_to_info_.find(new_id) != id_to_info_.end()) | |
| 57 new_id = static_cast<gfx::NativeViewId>(base::RandUint64()); | |
| 58 | |
| 59 NativeViewInfo info; | |
| 60 if (GTK_WIDGET_REALIZED(widget)) { | |
| 61 GdkWindow *gdk_window = widget->window; | |
| 62 CHECK(gdk_window); | |
| 63 info.x_window_id = GDK_WINDOW_XID(gdk_window); | |
| 64 } | |
| 65 | |
| 66 native_view_to_id_[widget] = new_id; | |
| 67 id_to_info_[new_id] = info; | |
| 68 | |
| 69 g_signal_connect(widget, "realize", G_CALLBACK(::OnRealize), this); | |
| 70 g_signal_connect(widget, "unrealize", G_CALLBACK(::OnUnrealize), this); | |
| 71 g_signal_connect(widget, "destroy", G_CALLBACK(::OnDestroy), this); | |
| 72 | |
| 73 return new_id; | |
| 74 } | |
| 75 | |
| 76 bool GtkNativeViewManager::GetXIDForId(XID* output, gfx::NativeViewId id) { | |
| 77 AutoLock locked(lock_); | |
| 78 | |
| 79 std::map<gfx::NativeViewId, NativeViewInfo>::const_iterator i = | |
| 80 id_to_info_.find(id); | |
| 81 | |
| 82 if (i == id_to_info_.end()) | |
| 83 return false; | |
| 84 | |
| 85 *output = i->second.x_window_id; | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 // ----------------------------------------------------------------------------- | |
| 90 | |
| 91 | |
| 92 // ----------------------------------------------------------------------------- | |
| 93 // Private functions... | |
| 94 | |
| 95 gfx::NativeViewId GtkNativeViewManager::GetWidgetId(gfx::NativeView widget) { | |
| 96 lock_.AssertAcquired(); | |
| 97 | |
| 98 std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i = | |
| 99 native_view_to_id_.find(widget); | |
| 100 | |
| 101 CHECK(i != native_view_to_id_.end()); | |
| 102 return i->second; | |
| 103 } | |
| 104 | |
| 105 void GtkNativeViewManager::OnRealize(gfx::NativeView widget) { | |
| 106 AutoLock locked(lock_); | |
| 107 | |
| 108 const gfx::NativeViewId id = GetWidgetId(widget); | |
| 109 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = | |
| 110 id_to_info_.find(id); | |
| 111 | |
| 112 CHECK(i != id_to_info_.end()); | |
| 113 CHECK(widget->window); | |
| 114 | |
| 115 i->second.x_window_id = GDK_WINDOW_XID(widget->window); | |
| 116 } | |
| 117 | |
| 118 void GtkNativeViewManager::OnUnrealize(gfx::NativeView widget) { | |
| 119 AutoLock locked(lock_); | |
| 120 | |
| 121 const gfx::NativeViewId id = GetWidgetId(widget); | |
| 122 std::map<gfx::NativeViewId, NativeViewInfo>::iterator i = | |
| 123 id_to_info_.find(id); | |
| 124 | |
| 125 CHECK(i != id_to_info_.end()); | |
| 126 | |
| 127 i->second.x_window_id = 0; | |
| 128 } | |
| 129 | |
| 130 void GtkNativeViewManager::OnDestroy(gfx::NativeView widget) { | |
| 131 AutoLock locked(lock_); | |
| 132 | |
| 133 std::map<gfx::NativeView, gfx::NativeViewId>::iterator i = | |
| 134 native_view_to_id_.find(widget); | |
| 135 CHECK(i != native_view_to_id_.end()); | |
| 136 | |
| 137 std::map<gfx::NativeViewId, NativeViewInfo>::iterator j = | |
| 138 id_to_info_.find(i->second); | |
| 139 CHECK(j != id_to_info_.end()); | |
| 140 | |
| 141 native_view_to_id_.erase(i); | |
| 142 id_to_info_.erase(j); | |
| 143 } | |
| 144 | |
| 145 // ----------------------------------------------------------------------------- | |
| OLD | NEW |