+//-----------------------------------------------------------------------------
+// define new GTK+ class wxGtkCellRenderer
+//-----------------------------------------------------------------------------
+
+extern "C" {
+
+#define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
+#define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
+#define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
+#define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
+#define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
+#define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
+
+GType gtk_wx_cell_renderer_get_type (void);
+
+typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
+typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
+
+struct _GtkWxCellRenderer
+{
+ GtkCellRenderer parent;
+
+ /*< private >*/
+ wxDataViewCustomCell *cell;
+ guint32 last_click;
+};
+
+struct _GtkWxCellRendererClass
+{
+ GtkCellRendererClass cell_parent_class;
+};
+
+
+static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
+static void gtk_wx_cell_renderer_init (
+ GtkWxCellRenderer *cell );
+static void gtk_wx_cell_renderer_class_init(
+ GtkWxCellRendererClass *klass );
+static void gtk_wx_cell_renderer_finalize (
+ GObject *object );
+static void gtk_wx_cell_renderer_get_size (
+ GtkCellRenderer *cell,
+ GtkWidget *widget,
+ GdkRectangle *rectangle,
+ gint *x_offset,
+ gint *y_offset,
+ gint *width,
+ gint *height );
+static void gtk_wx_cell_renderer_render (
+ GtkCellRenderer *cell,
+ GdkWindow *window,
+ GtkWidget *widget,
+ GdkRectangle *background_area,
+ GdkRectangle *cell_area,
+ GdkRectangle *expose_area,
+ GtkCellRendererState flags );
+static gboolean gtk_wx_cell_renderer_activate(
+ GtkCellRenderer *cell,
+ GdkEvent *event,
+ GtkWidget *widget,
+ const gchar *path,
+ GdkRectangle *background_area,
+ GdkRectangle *cell_area,
+ GtkCellRendererState flags );
+
+static GObjectClass *cell_parent_class = NULL;
+
+} // extern "C"
+
+GType
+gtk_wx_cell_renderer_get_type (void)
+{
+ static GType cell_wx_type = 0;
+
+ if (!cell_wx_type)
+ {
+ static const GTypeInfo cell_wx_info =
+ {
+ sizeof (GtkWxCellRendererClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) gtk_wx_cell_renderer_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (GtkWxCellRenderer),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) gtk_wx_cell_renderer_init,
+ };
+
+ cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
+ "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
+ }
+
+ return cell_wx_type;
+}
+
+static void
+gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
+{
+ cell->cell = NULL;
+ cell->last_click = 0;
+}
+
+static void
+gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
+
+ cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
+
+ object_class->finalize = gtk_wx_cell_renderer_finalize;
+
+ cell_class->get_size = gtk_wx_cell_renderer_get_size;
+ cell_class->render = gtk_wx_cell_renderer_render;
+ cell_class->activate = gtk_wx_cell_renderer_activate;
+}
+
+static void
+gtk_wx_cell_renderer_finalize (GObject *object)
+{
+ /* must chain up */
+ (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
+}
+
+GtkCellRenderer*
+gtk_wx_cell_renderer_new (void)
+{
+ return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
+}
+
+static void
+gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
+ GtkWidget *widget,
+ GdkRectangle *cell_area,
+ gint *x_offset,
+ gint *y_offset,
+ gint *width,
+ gint *height)
+{
+ GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
+ wxDataViewCustomCell *cell = wxrenderer->cell;
+
+ wxSize size = cell->GetSize();
+
+ gint calc_width = (gint) renderer->xpad * 2 + size.x;
+ gint calc_height = (gint) renderer->ypad * 2 + size.y;
+
+ if (x_offset)
+ *x_offset = 0;
+ if (y_offset)
+ *y_offset = 0;
+
+ if (cell_area && size.x > 0 && size.y > 0)
+ {
+ if (x_offset)
+ {
+ *x_offset = (gint)((renderer->xalign *
+ (cell_area->width - calc_width - 2 * renderer->xpad)));
+ *x_offset = MAX (*x_offset, 0) + renderer->xpad;
+ }
+ if (y_offset)
+ {
+ *y_offset = (gint)((renderer->yalign *
+ (cell_area->height - calc_height - 2 * renderer->ypad)));
+ *y_offset = MAX (*y_offset, 0) + renderer->ypad;
+ }
+ }
+
+ if (width)
+ *width = calc_width;
+
+ if (height)
+ *height = calc_height;
+}
+
+static void
+gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
+ GdkWindow *window,
+ GtkWidget *widget,
+ GdkRectangle *background_area,
+ GdkRectangle *cell_area,
+ GdkRectangle *expose_area,
+ GtkCellRendererState flags)
+
+{
+ GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
+ wxDataViewCustomCell *cell = wxrenderer->cell;
+
+ GdkRectangle rect;
+ gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
+ &rect.x,
+ &rect.y,
+ &rect.width,
+ &rect.height);
+
+ rect.x += cell_area->x;
+ rect.y += cell_area->y;
+ rect.width -= renderer->xpad * 2;
+ rect.height -= renderer->ypad * 2;
+
+ GdkRectangle dummy;
+ if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
+ {
+ wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
+ wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
+ if (dc->m_window == NULL)
+ {
+ dc->m_window = window;
+ dc->SetUpDC();
+ }
+
+ int state = 0;
+ if (flags & GTK_CELL_RENDERER_SELECTED)
+ state |= wxDATAVIEW_CELL_SELECTED;
+ if (flags & GTK_CELL_RENDERER_PRELIT)
+ state |= wxDATAVIEW_CELL_PRELIT;
+ if (flags & GTK_CELL_RENDERER_INSENSITIVE)
+ state |= wxDATAVIEW_CELL_INSENSITIVE;
+ if (flags & GTK_CELL_RENDERER_INSENSITIVE)
+ state |= wxDATAVIEW_CELL_INSENSITIVE;
+ if (flags & GTK_CELL_RENDERER_FOCUSED)
+ state |= wxDATAVIEW_CELL_FOCUSED;
+ cell->Render( renderrect, dc, state );
+ }
+}
+
+static gboolean
+gtk_wx_cell_renderer_activate(
+ GtkCellRenderer *renderer,
+ GdkEvent *event,
+ GtkWidget *widget,
+ const gchar *path,
+ GdkRectangle *background_area,
+ GdkRectangle *cell_area,
+ GtkCellRendererState flags )
+{
+ GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
+ wxDataViewCustomCell *cell = wxrenderer->cell;
+
+ GdkRectangle rect;
+ gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
+ &rect.x,
+ &rect.y,
+ &rect.width,
+ &rect.height);
+
+ rect.x += cell_area->x;
+ rect.y += cell_area->y;
+ rect.width -= renderer->xpad * 2;
+ rect.height -= renderer->ypad * 2;
+
+ wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
+
+ wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
+
+ GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
+ size_t model_row = (size_t)gtk_tree_path_get_indices (treepath)[0];
+ gtk_tree_path_free( treepath );
+
+ size_t model_col = cell->GetOwner()->GetModelColumn();
+
+ if (event->type == GDK_BUTTON_PRESS)
+ {
+ GdkEventButton *button_event = (GdkEventButton*) event;
+ wxPoint pt( ((int) button_event->x) - renderrect.x,
+ ((int) button_event->y) - renderrect.y );
+
+ bool ret = false;
+ if (button_event->button == 1)
+ {
+ if (cell->LeftClick( pt, renderrect, model, model_col, model_row ))
+ ret = true;
+ // TODO: query system double-click time
+ if (button_event->time - wxrenderer->last_click < 400)
+ if (cell->Activate( renderrect, model, model_col, model_row ))
+ ret = true;
+ }
+ if (button_event->button == 3)
+ {
+ if (cell->RightClick( pt, renderrect, model, model_col, model_row ))
+ ret = true;
+ }
+
+ wxrenderer->last_click = button_event->time;
+
+ return ret;
+ }
+
+ return false;
+}
+