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