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 "webkit/plugins/npapi/gtk_plugin_container.h" | |
6 | |
7 #include <gtk/gtk.h> | |
8 | |
9 #include "base/basictypes.h" | |
10 | |
11 namespace webkit { | |
12 namespace npapi { | |
13 | |
14 namespace { | |
15 | |
16 // NOTE: This class doesn't have constructors/destructors, it is created | |
17 // through GLib's object management. | |
18 class GtkPluginContainer : public GtkSocket { | |
19 public: | |
20 // Sets the requested size of the widget. | |
21 void set_size(int width, int height) { | |
22 width_ = width; | |
23 height_ = height; | |
24 } | |
25 | |
26 // Casts a widget into a GtkPluginContainer, after checking the type. | |
27 template <class T> | |
28 static GtkPluginContainer *CastChecked(T *instance) { | |
29 return G_TYPE_CHECK_INSTANCE_CAST(instance, GetType(), GtkPluginContainer); | |
30 } | |
31 | |
32 // Create and register our custom container type with GTK. | |
33 static GType GetType() { | |
34 static GType type = 0; // We only want to register our type once. | |
35 if (!type) { | |
36 static const GTypeInfo info = { | |
37 sizeof(GtkSocketClass), | |
38 NULL, NULL, | |
39 static_cast<GClassInitFunc>(&ClassInit), | |
40 NULL, NULL, | |
41 sizeof(GtkPluginContainer), | |
42 0, &InstanceInit, | |
43 }; | |
44 type = g_type_register_static(GTK_TYPE_SOCKET, | |
45 "GtkPluginContainer", | |
46 &info, | |
47 static_cast<GTypeFlags>(0)); | |
48 } | |
49 return type; | |
50 } | |
51 | |
52 // Implementation of the class initializer. | |
53 static void ClassInit(gpointer klass, gpointer class_data_unusued) { | |
54 GtkWidgetClass* widget_class = reinterpret_cast<GtkWidgetClass*>(klass); | |
55 widget_class->size_request = &HandleSizeRequest; | |
56 } | |
57 | |
58 // Implementation of the instance initializer (constructor). | |
59 static void InstanceInit(GTypeInstance *instance, gpointer klass) { | |
60 GtkPluginContainer *container = CastChecked(instance); | |
61 container->set_size(0, 0); | |
62 } | |
63 | |
64 // Report our allocation size during size requisition. | |
65 static void HandleSizeRequest(GtkWidget* widget, | |
66 GtkRequisition* requisition) { | |
67 GtkPluginContainer *container = CastChecked(widget); | |
68 requisition->width = container->width_; | |
69 requisition->height = container->height_; | |
70 } | |
71 | |
72 int width_; | |
73 int height_; | |
74 DISALLOW_IMPLICIT_CONSTRUCTORS(GtkPluginContainer); | |
75 }; | |
76 | |
77 } // namespace | |
78 | |
79 // Create a new instance of our GTK widget object. | |
80 GtkWidget* gtk_plugin_container_new() { | |
81 return GTK_WIDGET(g_object_new(GtkPluginContainer::GetType(), NULL)); | |
82 } | |
83 | |
84 void gtk_plugin_container_set_size(GtkWidget *widget, int width, int height) { | |
85 GtkPluginContainer::CastChecked(widget)->set_size(width, height); | |
86 // Signal the parent that the size request has changed. | |
87 gtk_widget_queue_resize_no_redraw(widget); | |
88 } | |
89 | |
90 } // namespace npapi | |
91 } // namespace webkit | |
OLD | NEW |