]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dataview.cpp
Always set colormap for PangoContext. This silences myriads
[wxWidgets.git] / src / gtk / dataview.cpp
CommitLineData
790b137e 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/dataview.cpp
790b137e
RR
3// Purpose: wxDataViewCtrl GTK+2 implementation
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
790b137e
RR
13#if wxUSE_DATAVIEWCTRL
14
15#include "wx/dataview.h"
4ed7af08
RR
16
17#ifndef wxUSE_GENERICDATAVIEWCTRL
18
e4db172a
WS
19#ifndef WX_PRECOMP
20 #include "wx/log.h"
ed4b0fdc 21 #include "wx/dcclient.h"
ed2fbeb8 22 #include "wx/sizer.h"
e4db172a
WS
23#endif
24
790b137e 25#include "wx/stockitem.h"
7ea3a0de
RR
26#include "wx/calctrl.h"
27#include "wx/popupwin.h"
2586d4a1
RR
28#include "wx/icon.h"
29
790b137e
RR
30
31#include "wx/gtk/private.h"
32#include "wx/gtk/win_gtk.h"
33
34#include <gobject/gvaluecollector.h>
1557c77b
RR
35#include <gtk/gtktreemodel.h>
36#include <gtk/gtktreednd.h>
790b137e 37
4d496ecb
RR
38#include <gdk/gdkkeysyms.h>
39
790b137e
RR
40//-----------------------------------------------------------------------------
41// classes
42//-----------------------------------------------------------------------------
43
44class wxDataViewCtrl;
45
790b137e
RR
46//-----------------------------------------------------------------------------
47// data
48//-----------------------------------------------------------------------------
49
50extern bool g_blockEventsOnDrag;
51
52//-----------------------------------------------------------------------------
53// define new GTK+ class wxGtkListStore
54//-----------------------------------------------------------------------------
55
56extern "C" {
57
58#define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ())
59#define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore))
60#define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
61#define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE))
62#define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE))
63#define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
64
65GType gtk_wx_list_store_get_type (void);
66
67typedef struct _GtkWxListStore GtkWxListStore;
68typedef struct _GtkWxListStoreClass GtkWxListStoreClass;
69
70struct _GtkWxListStore
71{
72 GObject parent;
73
74 /*< private >*/
75 gint stamp;
76 wxDataViewListModel *model;
77};
78
79struct _GtkWxListStoreClass
80{
e152afc3 81 GObjectClass list_parent_class;
790b137e
RR
82};
83
e152afc3 84static GtkWxListStore *wxgtk_list_store_new (void);
790b137e 85static void wxgtk_list_store_init (GtkWxListStore *list_store);
1557c77b 86static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass);
790b137e
RR
87static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface);
88static void wxgtk_list_store_finalize (GObject *object);
89static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model);
90static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model);
91static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
93763ad5 92 gint index);
790b137e 93static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
94 GtkTreeIter *iter,
95 GtkTreePath *path);
790b137e 96static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model,
93763ad5 97 GtkTreeIter *iter);
790b137e 98static void wxgtk_list_store_get_value (GtkTreeModel *tree_model,
93763ad5
WS
99 GtkTreeIter *iter,
100 gint column,
101 GValue *value);
790b137e 102static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
93763ad5 103 GtkTreeIter *iter);
790b137e 104static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
105 GtkTreeIter *iter,
106 GtkTreeIter *parent);
790b137e 107static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
93763ad5 108 GtkTreeIter *iter);
790b137e 109static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
93763ad5 110 GtkTreeIter *iter);
790b137e 111static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
112 GtkTreeIter *iter,
113 GtkTreeIter *parent,
114 gint n);
790b137e 115static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
116 GtkTreeIter *iter,
117 GtkTreeIter *child);
790b137e 118
e152afc3 119static GObjectClass *list_parent_class = NULL;
790b137e
RR
120
121GType
111f83c4 122gtk_wx_list_store_get_type (void)
790b137e 123{
93763ad5 124 static GType list_store_type = 0;
790b137e 125
93763ad5 126 if (!list_store_type)
790b137e 127 {
93763ad5
WS
128 static const GTypeInfo list_store_info =
129 {
130 sizeof (GtkWxListStoreClass),
131 NULL, /* base_init */
132 NULL, /* base_finalize */
133 (GClassInitFunc) wxgtk_list_store_class_init,
134 NULL, /* class_finalize */
135 NULL, /* class_data */
136 sizeof (GtkWxListStore),
137 0,
138 (GInstanceInitFunc) wxgtk_list_store_init,
139 };
790b137e
RR
140
141 static const GInterfaceInfo tree_model_info =
142 {
93763ad5
WS
143 (GInterfaceInitFunc) wxgtk_list_store_tree_model_init,
144 NULL,
145 NULL
790b137e
RR
146 };
147
148 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore",
93763ad5 149 &list_store_info, (GTypeFlags)0 );
790b137e
RR
150
151 g_type_add_interface_static (list_store_type,
93763ad5
WS
152 GTK_TYPE_TREE_MODEL,
153 &tree_model_info);
790b137e
RR
154 }
155
156 return list_store_type;
157}
158
1557c77b 159static GtkWxListStore *
e152afc3 160wxgtk_list_store_new(void)
1557c77b 161{
e152afc3
RR
162 GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL);
163 return retval;
1557c77b
RR
164}
165
790b137e 166static void
1557c77b 167wxgtk_list_store_class_init (GtkWxListStoreClass *klass)
790b137e 168{
e152afc3
RR
169 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
170 GObjectClass *object_class = (GObjectClass*) klass;
790b137e
RR
171 object_class->finalize = wxgtk_list_store_finalize;
172}
173
174static void
175wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface)
176{
177 iface->get_flags = wxgtk_list_store_get_flags;
178 iface->get_n_columns = wxgtk_list_store_get_n_columns;
179 iface->get_column_type = wxgtk_list_store_get_column_type;
180 iface->get_iter = wxgtk_list_store_get_iter;
181 iface->get_path = wxgtk_list_store_get_path;
182 iface->get_value = wxgtk_list_store_get_value;
183 iface->iter_next = wxgtk_list_store_iter_next;
184 iface->iter_children = wxgtk_list_store_iter_children;
185 iface->iter_has_child = wxgtk_list_store_iter_has_child;
186 iface->iter_n_children = wxgtk_list_store_iter_n_children;
187 iface->iter_nth_child = wxgtk_list_store_iter_nth_child;
188 iface->iter_parent = wxgtk_list_store_iter_parent;
189}
190
191static void
192wxgtk_list_store_init (GtkWxListStore *list_store)
193{
194 list_store->model = NULL;
1557c77b 195 list_store->stamp = g_random_int();
790b137e
RR
196}
197
198static void
199wxgtk_list_store_finalize (GObject *object)
200{
1557c77b 201 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
790b137e
RR
202
203 /* we need to sort out, which class deletes what */
111f83c4 204 /* delete list_store->model; */
790b137e
RR
205
206 /* must chain up */
e152afc3 207 (* list_parent_class->finalize) (object);
790b137e 208}
93763ad5 209
790b137e
RR
210} // extern "C"
211
212//-----------------------------------------------------------------------------
213// implement callbacks from wxGtkListStore class by letting
214// them call the methods of wxWidgets' wxDataViewListModel
215//-----------------------------------------------------------------------------
216
217static GtkTreeModelFlags
218wxgtk_list_store_get_flags (GtkTreeModel *tree_model)
219{
1557c77b 220 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 );
790b137e 221
67cbdfc1
RR
222 // GTK+ list store uses a linked list for storing the
223 // items and a pointer to a child is used as the member
224 // field of a GtkTreeIter. This means that the iter is
225 // valid in the GtkListStore as long as the child exists.
226 // We use the index of the row and since the index of a
227 // specific row will change if a row above is deleted,
228 // the iter does not persist
229 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY;
790b137e
RR
230}
231
232static gint
233wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model)
234{
235 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
236 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0);
237
239eaa41 238 return list_store->model->GetNumberOfCols();
790b137e
RR
239}
240
241static GType
242wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
93763ad5 243 gint index)
790b137e
RR
244{
245 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
246 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID);
247
1557c77b 248 GType gtype = G_TYPE_INVALID;
93763ad5 249
1557c77b 250 wxString wxtype = list_store->model->GetColType( (size_t) index );
93763ad5 251
1557c77b
RR
252 if (wxtype == wxT("string"))
253 gtype = G_TYPE_STRING;
790b137e
RR
254
255 return gtype;
256}
257
258static gboolean
259wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
93763ad5
WS
260 GtkTreeIter *iter,
261 GtkTreePath *path)
790b137e
RR
262{
263 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
264 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
265 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
266
1557c77b 267 size_t i = (size_t)gtk_tree_path_get_indices (path)[0];
790b137e 268
1557c77b 269 if (i >= list_store->model->GetNumberOfRows())
239eaa41 270 return FALSE;
790b137e 271
239eaa41
RR
272 iter->stamp = list_store->stamp;
273 // user_data is just the index
274 iter->user_data = (gpointer) i;
790b137e
RR
275
276 return TRUE;
277}
278
279static GtkTreePath *
280wxgtk_list_store_get_path (GtkTreeModel *tree_model,
93763ad5 281 GtkTreeIter *iter)
790b137e 282{
790b137e 283 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL);
239eaa41 284 g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL);
93763ad5 285
239eaa41
RR
286 GtkTreePath *retval = gtk_tree_path_new ();
287 // user_data is just the index
7a9f5d5a 288 int i = (wxUIntPtr) iter->user_data;
239eaa41
RR
289 gtk_tree_path_append_index (retval, i);
290 return retval;
790b137e
RR
291}
292
293static void
294wxgtk_list_store_get_value (GtkTreeModel *tree_model,
93763ad5
WS
295 GtkTreeIter *iter,
296 gint column,
297 GValue *value)
790b137e 298{
1557c77b 299 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
790b137e 300 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) );
239eaa41 301
1557c77b
RR
302 wxDataViewListModel *model = list_store->model;
303 wxString mtype = model->GetColType( (size_t) column );
304 if (mtype == wxT("string"))
305 {
3f3af7e7 306 wxVariant variant;
1557c77b 307 g_value_init( value, G_TYPE_STRING );
3f3af7e7 308 model->GetValue( variant, (size_t) column, (size_t) iter->user_data );
1557c77b
RR
309 g_value_set_string( value, wxGTK_CONV(variant.GetString()) );
310 }
311 else
312 {
313 }
93763ad5 314
790b137e
RR
315#if 0
316 GtkTreeDataList *list;
317 gint tmp_column = column;
318
319 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
320 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
321
322 list = G_SLIST (iter->user_data)->data;
323
324 while (tmp_column-- > 0 && list)
325 list = list->next;
326
327 if (list == NULL)
328 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
329 else
330 _gtk_tree_data_list_node_to_value (list,
93763ad5
WS
331 GTK_LIST_STORE (tree_model)->column_headers[column],
332 value);
790b137e
RR
333#endif
334
335}
336
337static gboolean
338wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
93763ad5 339 GtkTreeIter *iter)
790b137e 340{
790b137e 341 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
1557c77b 342 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
93763ad5 343
67cbdfc1 344 g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE);
790b137e 345
7a9f5d5a 346 int n = (wxUIntPtr) iter->user_data;
93763ad5 347
67cbdfc1
RR
348 if (n == -1)
349 return FALSE;
93763ad5 350
1a367564 351 if (n >= (int) list_store->model->GetNumberOfRows()-1)
67cbdfc1 352 return FALSE;
93763ad5 353
111f83c4 354 iter->user_data = (gpointer) ++n;
790b137e 355
67cbdfc1 356 return TRUE;
790b137e
RR
357}
358
359static gboolean
360wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
93763ad5
WS
361 GtkTreeIter *iter,
362 GtkTreeIter *parent)
790b137e 363{
67cbdfc1 364 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
1557c77b 365 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
93763ad5 366
67cbdfc1 367 // this is a list, nodes have no children
790b137e
RR
368 if (parent)
369 return FALSE;
370
67cbdfc1
RR
371 iter->stamp = list_store->stamp;
372 iter->user_data = (gpointer) -1;
93763ad5 373
67cbdfc1 374 return TRUE;
790b137e
RR
375}
376
377static gboolean
378wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
93763ad5 379 GtkTreeIter *iter)
790b137e
RR
380{
381 return FALSE;
382}
383
384static gint
385wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
93763ad5 386 GtkTreeIter *iter)
790b137e
RR
387{
388 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1);
1557c77b 389 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
93763ad5 390
790b137e 391 if (iter == NULL)
1557c77b 392 return (gint) list_store->model->GetNumberOfRows();
790b137e 393
67cbdfc1 394 g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
93763ad5 395
790b137e
RR
396 return 0;
397}
398
399static gboolean
400wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
93763ad5
WS
401 GtkTreeIter *iter,
402 GtkTreeIter *parent,
403 gint n)
790b137e 404{
67cbdfc1 405 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
1557c77b 406 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
93763ad5 407
67cbdfc1
RR
408 if (parent)
409 return FALSE;
790b137e 410
67cbdfc1
RR
411 if (n < 0)
412 return FALSE;
93763ad5 413
1557c77b 414 if (n >= (gint) list_store->model->GetNumberOfRows())
67cbdfc1 415 return FALSE;
790b137e 416
67cbdfc1
RR
417 iter->stamp = list_store->stamp;
418 iter->user_data = (gpointer) n;
93763ad5 419
67cbdfc1 420 return TRUE;
790b137e
RR
421}
422
423static gboolean
424wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
93763ad5
WS
425 GtkTreeIter *iter,
426 GtkTreeIter *child)
790b137e
RR
427{
428 return FALSE;
429}
430
e152afc3
RR
431//-----------------------------------------------------------------------------
432// define new GTK+ class wxGtkCellRenderer
433//-----------------------------------------------------------------------------
434
435extern "C" {
436
437#define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
438#define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
439#define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
440#define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
441#define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
442#define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
443
444GType gtk_wx_cell_renderer_get_type (void);
445
446typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
447typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
448
449struct _GtkWxCellRenderer
450{
451 GtkCellRenderer parent;
452
453 /*< private >*/
454 wxDataViewCustomCell *cell;
4d496ecb 455 guint32 last_click;
e152afc3
RR
456};
457
458struct _GtkWxCellRendererClass
459{
460 GtkCellRendererClass cell_parent_class;
e152afc3
RR
461};
462
463
464static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
553f7d8f
RR
465static void gtk_wx_cell_renderer_init (
466 GtkWxCellRenderer *cell );
467static void gtk_wx_cell_renderer_class_init(
468 GtkWxCellRendererClass *klass );
469static void gtk_wx_cell_renderer_finalize (
470 GObject *object );
471static void gtk_wx_cell_renderer_get_size (
472 GtkCellRenderer *cell,
93763ad5
WS
473 GtkWidget *widget,
474 GdkRectangle *rectangle,
475 gint *x_offset,
476 gint *y_offset,
477 gint *width,
478 gint *height );
553f7d8f
RR
479static void gtk_wx_cell_renderer_render (
480 GtkCellRenderer *cell,
93763ad5
WS
481 GdkWindow *window,
482 GtkWidget *widget,
483 GdkRectangle *background_area,
484 GdkRectangle *cell_area,
485 GdkRectangle *expose_area,
486 GtkCellRendererState flags );
553f7d8f
RR
487static gboolean gtk_wx_cell_renderer_activate(
488 GtkCellRenderer *cell,
489 GdkEvent *event,
490 GtkWidget *widget,
491 const gchar *path,
492 GdkRectangle *background_area,
493 GdkRectangle *cell_area,
494 GtkCellRendererState flags );
e152afc3
RR
495
496static GObjectClass *cell_parent_class = NULL;
497
498} // extern "C"
499
93763ad5 500GType
e152afc3
RR
501gtk_wx_cell_renderer_get_type (void)
502{
553f7d8f 503 static GType cell_wx_type = 0;
e152afc3 504
553f7d8f 505 if (!cell_wx_type)
e152afc3 506 {
553f7d8f
RR
507 static const GTypeInfo cell_wx_info =
508 {
509 sizeof (GtkWxCellRendererClass),
93763ad5
WS
510 NULL, /* base_init */
511 NULL, /* base_finalize */
553f7d8f 512 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
93763ad5
WS
513 NULL, /* class_finalize */
514 NULL, /* class_data */
553f7d8f
RR
515 sizeof (GtkWxCellRenderer),
516 0, /* n_preallocs */
517 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
518 };
519
93763ad5 520 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
553f7d8f 521 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
e152afc3
RR
522 }
523
553f7d8f 524 return cell_wx_type;
e152afc3
RR
525}
526
527static void
528gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
529{
530 cell->cell = NULL;
4d496ecb 531 cell->last_click = 0;
e152afc3
RR
532}
533
534static void
535gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
536{
537 GObjectClass *object_class = G_OBJECT_CLASS (klass);
538 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
539
540 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
541
542 object_class->finalize = gtk_wx_cell_renderer_finalize;
543
544 cell_class->get_size = gtk_wx_cell_renderer_get_size;
545 cell_class->render = gtk_wx_cell_renderer_render;
553f7d8f 546 cell_class->activate = gtk_wx_cell_renderer_activate;
e152afc3
RR
547}
548
549static void
550gtk_wx_cell_renderer_finalize (GObject *object)
551{
552 /* must chain up */
553 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
554}
555
556GtkCellRenderer*
557gtk_wx_cell_renderer_new (void)
558{
559 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
560}
561
562static void
563gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
93763ad5
WS
564 GtkWidget *widget,
565 GdkRectangle *cell_area,
566 gint *x_offset,
567 gint *y_offset,
568 gint *width,
569 gint *height)
e152afc3
RR
570{
571 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
572 wxDataViewCustomCell *cell = wxrenderer->cell;
93763ad5 573
e152afc3
RR
574 wxSize size = cell->GetSize();
575
576 gint calc_width = (gint) renderer->xpad * 2 + size.x;
577 gint calc_height = (gint) renderer->ypad * 2 + size.y;
93763ad5
WS
578
579 if (x_offset)
e152afc3 580 *x_offset = 0;
93763ad5 581 if (y_offset)
e152afc3
RR
582 *y_offset = 0;
583
584 if (cell_area && size.x > 0 && size.y > 0)
585 {
586 if (x_offset)
93763ad5 587 {
e152afc3
RR
588 *x_offset = (gint)((renderer->xalign *
589 (cell_area->width - calc_width - 2 * renderer->xpad)));
590 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
93763ad5 591 }
e152afc3
RR
592 if (y_offset)
593 {
594 *y_offset = (gint)((renderer->yalign *
595 (cell_area->height - calc_height - 2 * renderer->ypad)));
596 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
597 }
598 }
599
600 if (width)
601 *width = calc_width;
93763ad5 602
e152afc3
RR
603 if (height)
604 *height = calc_height;
605}
606
607static void
608gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
93763ad5
WS
609 GdkWindow *window,
610 GtkWidget *widget,
611 GdkRectangle *background_area,
612 GdkRectangle *cell_area,
613 GdkRectangle *expose_area,
614 GtkCellRendererState flags)
e152afc3
RR
615
616{
617 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
618 wxDataViewCustomCell *cell = wxrenderer->cell;
93763ad5 619
e152afc3
RR
620 GdkRectangle rect;
621 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
93763ad5
WS
622 &rect.x,
623 &rect.y,
624 &rect.width,
625 &rect.height);
e152afc3
RR
626
627 rect.x += cell_area->x;
628 rect.y += cell_area->y;
629 rect.width -= renderer->xpad * 2;
630 rect.height -= renderer->ypad * 2;
93763ad5 631
e152afc3
RR
632 GdkRectangle dummy;
633 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
634 {
635 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
636 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
4d496ecb
RR
637 if (dc->m_window == NULL)
638 {
639 dc->m_window = window;
640 dc->SetUpDC();
641 }
93763ad5 642
e152afc3
RR
643 int state = 0;
644 if (flags & GTK_CELL_RENDERER_SELECTED)
645 state |= wxDATAVIEW_CELL_SELECTED;
646 if (flags & GTK_CELL_RENDERER_PRELIT)
647 state |= wxDATAVIEW_CELL_PRELIT;
648 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
649 state |= wxDATAVIEW_CELL_INSENSITIVE;
650 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
651 state |= wxDATAVIEW_CELL_INSENSITIVE;
652 if (flags & GTK_CELL_RENDERER_FOCUSED)
653 state |= wxDATAVIEW_CELL_FOCUSED;
654 cell->Render( renderrect, dc, state );
93763ad5 655 }
e152afc3
RR
656}
657
93763ad5 658static gboolean
553f7d8f
RR
659gtk_wx_cell_renderer_activate(
660 GtkCellRenderer *renderer,
661 GdkEvent *event,
662 GtkWidget *widget,
663 const gchar *path,
664 GdkRectangle *background_area,
665 GdkRectangle *cell_area,
666 GtkCellRendererState flags )
667{
668 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
669 wxDataViewCustomCell *cell = wxrenderer->cell;
93763ad5 670
553f7d8f
RR
671 GdkRectangle rect;
672 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
93763ad5
WS
673 &rect.x,
674 &rect.y,
675 &rect.width,
676 &rect.height);
553f7d8f
RR
677
678 rect.x += cell_area->x;
679 rect.y += cell_area->y;
680 rect.width -= renderer->xpad * 2;
681 rect.height -= renderer->ypad * 2;
93763ad5 682
553f7d8f 683 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
93763ad5 684
4d496ecb 685 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 686
4d496ecb
RR
687 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
688 size_t model_row = (size_t)gtk_tree_path_get_indices (treepath)[0];
689 gtk_tree_path_free( treepath );
93763ad5 690
4d496ecb 691 size_t model_col = cell->GetOwner()->GetModelColumn();
93763ad5 692
4d496ecb
RR
693 if (event->type == GDK_BUTTON_PRESS)
694 {
695 GdkEventButton *button_event = (GdkEventButton*) event;
93763ad5 696 wxPoint pt( ((int) button_event->x) - renderrect.x,
4d496ecb 697 ((int) button_event->y) - renderrect.y );
93763ad5 698
4d496ecb
RR
699 bool ret = false;
700 if (button_event->button == 1)
701 {
702 if (cell->LeftClick( pt, renderrect, model, model_col, model_row ))
703 ret = true;
7ea3a0de 704 // TODO: query system double-click time
4d496ecb
RR
705 if (button_event->time - wxrenderer->last_click < 400)
706 if (cell->Activate( renderrect, model, model_col, model_row ))
707 ret = true;
708 }
709 if (button_event->button == 3)
710 {
711 if (cell->RightClick( pt, renderrect, model, model_col, model_row ))
712 ret = true;
713 }
93763ad5 714
4d496ecb 715 wxrenderer->last_click = button_event->time;
93763ad5 716
4d496ecb
RR
717 return ret;
718 }
93763ad5 719
4d496ecb 720 return false;
553f7d8f
RR
721}
722
93763ad5 723// ---------------------------------------------------------
6e2e590f 724// wxGtkDataViewListModelNotifier
93763ad5 725// ---------------------------------------------------------
6e2e590f
RR
726
727class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier
728{
729public:
730 wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model );
93763ad5 731
6e2e590f
RR
732 virtual bool RowAppended();
733 virtual bool RowPrepended();
734 virtual bool RowInserted( size_t before );
735 virtual bool RowDeleted( size_t row );
736 virtual bool RowChanged( size_t row );
a7f61f76 737 virtual bool ValueChanged( size_t col, size_t row );
4eccd3a1 738 virtual bool RowsReordered( size_t *new_order );
6e2e590f 739 virtual bool Cleared();
93763ad5 740
6e2e590f
RR
741 GtkWxListStore *m_gtk_store;
742 wxDataViewListModel *m_wx_model;
743};
744
93763ad5 745// ---------------------------------------------------------
6e2e590f 746// wxGtkDataViewListModelNotifier
93763ad5 747// ---------------------------------------------------------
6e2e590f 748
93763ad5 749wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
6e2e590f
RR
750 GtkWxListStore* gtk_store, wxDataViewListModel *wx_model )
751{
752 m_gtk_store = gtk_store;
753 m_wx_model = wx_model;
754}
93763ad5 755
6e2e590f
RR
756bool wxGtkDataViewListModelNotifier::RowAppended()
757{
758 size_t pos = m_wx_model->GetNumberOfRows()-1;
93763ad5 759
6e2e590f
RR
760 GtkTreeIter iter;
761 iter.stamp = m_gtk_store->stamp;
762 iter.user_data = (gpointer) pos;
93763ad5 763
6e2e590f
RR
764 GtkTreePath *path = gtk_tree_path_new ();
765 gtk_tree_path_append_index (path, (gint) pos);
766 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
767 gtk_tree_path_free (path);
93763ad5 768
6e2e590f
RR
769 return true;
770}
771
772bool wxGtkDataViewListModelNotifier::RowPrepended()
773{
605c2c4a
RR
774 GtkTreeIter iter;
775 iter.stamp = m_gtk_store->stamp;
776 iter.user_data = (gpointer) 0;
93763ad5 777
605c2c4a
RR
778 GtkTreePath *path = gtk_tree_path_new ();
779 gtk_tree_path_append_index (path, (gint) 0);
780 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
781 gtk_tree_path_free (path);
93763ad5 782
605c2c4a 783 return true;
6e2e590f
RR
784}
785
786bool wxGtkDataViewListModelNotifier::RowInserted( size_t before )
787{
4627af27
RR
788 GtkTreeIter iter;
789 iter.stamp = m_gtk_store->stamp;
790 iter.user_data = (gpointer) before;
791
792 GtkTreePath *path = gtk_tree_path_new ();
793 gtk_tree_path_append_index (path, (gint) before);
794 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
795 gtk_tree_path_free (path);
796
797 return true;
6e2e590f
RR
798}
799
800bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row )
801{
4627af27
RR
802 GtkTreePath *path = gtk_tree_path_new ();
803 gtk_tree_path_append_index (path, (gint) row);
804 gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store), path);
805 gtk_tree_path_free (path);
806
807 return true;
6e2e590f
RR
808}
809
810bool wxGtkDataViewListModelNotifier::RowChanged( size_t row )
811{
a7f61f76
RR
812 GtkTreeIter iter;
813 iter.stamp = m_gtk_store->stamp;
814 iter.user_data = (gpointer) row;
815 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter);
816 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter);
817 gtk_tree_path_free (path);
818
819 return true;
6e2e590f
RR
820}
821
8f850e28 822bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col, size_t model_row )
6e2e590f 823{
4eccd3a1 824 // This adds GTK+'s missing MVC logic for ValueChanged
8f850e28
RR
825 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
826 while (node)
827 {
828 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
829 if (viewing_column->m_modelColumn == model_col)
830 {
831 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
832 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle());
833
834 // Get cell area
835 GtkTreePath *path = gtk_tree_path_new();
836 gtk_tree_path_append_index( path, model_row );
837 GdkRectangle cell_area;
838 gtk_tree_view_get_cell_area( widget, path, column, &cell_area );
93763ad5 839 gtk_tree_path_free( path );
8f850e28
RR
840
841 int ydiff = column->button->allocation.height;
842 // Redraw
93763ad5 843 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
8f850e28
RR
844 cell_area.x, ydiff + cell_area.y, cell_area.width, cell_area.height );
845 }
846
847 node = node->GetNext();
848 }
93763ad5 849
8f850e28 850 return true;
6e2e590f
RR
851}
852
4eccd3a1
RR
853bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order )
854{
855 // Assume sizeof(size_t)= == sizeof(gint)
856
857 GtkTreePath *path = gtk_tree_path_new ();
858 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order);
859 gtk_tree_path_free (path);
860
861 // This adds GTK+'s missing MVC logic for RowsReordered
862 wxNode *node = GetOwner()->m_viewingColumns.GetFirst();
863 while (node)
864 {
865 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
866 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
867 // Doesn't work yet...
93763ad5 868 gtk_widget_queue_draw( GTK_WIDGET(widget) );
4eccd3a1
RR
869
870 node = node->GetNext();
871 }
93763ad5 872
4eccd3a1
RR
873 return true;
874}
875
6e2e590f
RR
876bool wxGtkDataViewListModelNotifier::Cleared()
877{
878 return false;
879}
880
93763ad5 881// ---------------------------------------------------------
6842a71a 882// wxDataViewCell
93763ad5 883// ---------------------------------------------------------
6842a71a
RR
884
885IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase)
886
887wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) :
888 wxDataViewCellBase( varianttype, mode )
889{
890 m_renderer = NULL;
891}
892
93763ad5 893// ---------------------------------------------------------
6842a71a 894// wxDataViewTextCell
93763ad5 895// ---------------------------------------------------------
6842a71a 896
a7f61f76 897extern "C" {
93763ad5 898static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
a7f61f76
RR
899 gchar *arg1, gchar *arg2, gpointer user_data );
900}
901
93763ad5 902static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
a7f61f76
RR
903 gchar *arg1, gchar *arg2, gpointer user_data )
904{
905 wxDataViewTextCell *cell = (wxDataViewTextCell*) user_data;
93763ad5 906
a7f61f76
RR
907 wxString tmp = wxGTK_CONV_BACK( arg2 );
908 wxVariant value = tmp;
909 if (!cell->Validate( value ))
910 return;
93763ad5 911
a7f61f76 912 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 913
a7f61f76
RR
914 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
915 size_t model_row = (size_t)gtk_tree_path_get_indices (path)[0];
916 gtk_tree_path_free( path );
93763ad5 917
a7f61f76 918 size_t model_col = cell->GetOwner()->GetModelColumn();
93763ad5 919
a7f61f76
RR
920 model->SetValue( value, model_col, model_row );
921 model->ValueChanged( model_col, model_row );
922}
923
cbc9145c 924IMPLEMENT_CLASS(wxDataViewTextCell, wxDataViewCell)
6842a71a
RR
925
926wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) :
927 wxDataViewCell( varianttype, mode )
928{
929 m_renderer = (void*) gtk_cell_renderer_text_new();
93763ad5 930
a7f61f76
RR
931 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
932 {
933 GValue gvalue = { 0, };
934 g_value_init( &gvalue, G_TYPE_BOOLEAN );
935 g_value_set_boolean( &gvalue, true );
936 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
937 g_value_unset( &gvalue );
93763ad5 938
a7f61f76
RR
939 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
940 }
6842a71a 941}
790b137e 942
7b4fde82
RR
943bool wxDataViewTextCell::SetValue( const wxVariant &value )
944{
945 wxString tmp = value;
93763ad5 946
7b4fde82
RR
947 GValue gvalue = { 0, };
948 g_value_init( &gvalue, G_TYPE_STRING );
949 g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
950 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
951 g_value_unset( &gvalue );
93763ad5 952
7b4fde82
RR
953 return true;
954}
955
a7f61f76
RR
956bool wxDataViewTextCell::GetValue( wxVariant &value )
957{
958 GValue gvalue = { 0, };
959 g_value_init( &gvalue, G_TYPE_STRING );
960 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
93763ad5 961 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) );
a7f61f76 962 g_value_unset( &gvalue );
93763ad5 963
a7f61f76
RR
964 value = tmp;
965
966 return true;
967}
968
cbc9145c
RR
969// ---------------------------------------------------------
970// wxDataViewBitmapCell
971// ---------------------------------------------------------
972
973IMPLEMENT_CLASS(wxDataViewBitmapCell, wxDataViewCell)
974
975wxDataViewBitmapCell::wxDataViewBitmapCell( const wxString &varianttype, wxDataViewCellMode mode ) :
976 wxDataViewCell( varianttype, mode )
977{
978 m_renderer = (void*) gtk_cell_renderer_pixbuf_new();
979}
980
981bool wxDataViewBitmapCell::SetValue( const wxVariant &value )
982{
983 if (value.GetType() == wxT("wxBitmap"))
984 {
2586d4a1
RR
985 wxBitmap bitmap;
986 bitmap << value;
987
988 // This may create a Pixbuf representation in the
989 // wxBitmap object (and it will stay there)
990 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
991
992 GValue gvalue = { 0, };
993 g_value_init( &gvalue, G_TYPE_OBJECT );
994 g_value_set_object( &gvalue, pixbuf );
995 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
996 g_value_unset( &gvalue );
997
998 return true;
999 }
1000
1001 if (value.GetType() == wxT("wxIcon"))
1002 {
1003 wxIcon bitmap;
1004 bitmap << value;
cbc9145c
RR
1005
1006 // This may create a Pixbuf representation in the
1007 // wxBitmap object (and it will stay there)
2586d4a1 1008 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
cbc9145c
RR
1009
1010 GValue gvalue = { 0, };
1011 g_value_init( &gvalue, G_TYPE_OBJECT );
1012 g_value_set_object( &gvalue, pixbuf );
1013 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1014 g_value_unset( &gvalue );
1015
1016 return true;
1017 }
1018
1019 return false;
1020}
1021
1022bool wxDataViewBitmapCell::GetValue( wxVariant &value )
1023{
1024 return false;
1025}
1026
93763ad5 1027// ---------------------------------------------------------
605c2c4a 1028// wxDataViewToggleCell
93763ad5 1029// ---------------------------------------------------------
fa28826d 1030
605c2c4a 1031extern "C" {
93763ad5 1032static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1033 gchar *path, gpointer user_data );
1034}
1035
93763ad5 1036static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
605c2c4a
RR
1037 gchar *path, gpointer user_data )
1038{
1039 wxDataViewToggleCell *cell = (wxDataViewToggleCell*) user_data;
1040
93763ad5 1041 // get old value
605c2c4a
RR
1042 GValue gvalue = { 0, };
1043 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1044 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
93763ad5 1045 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a
RR
1046 g_value_unset( &gvalue );
1047 // invert it
1048 tmp = !tmp;
93763ad5 1049
605c2c4a
RR
1050 wxVariant value = tmp;
1051 if (!cell->Validate( value ))
1052 return;
93763ad5 1053
605c2c4a 1054 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
93763ad5 1055
605c2c4a
RR
1056 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
1057 size_t model_row = (size_t)gtk_tree_path_get_indices (gtk_path)[0];
1058 gtk_tree_path_free( gtk_path );
93763ad5 1059
605c2c4a 1060 size_t model_col = cell->GetOwner()->GetModelColumn();
93763ad5 1061
605c2c4a
RR
1062 model->SetValue( value, model_col, model_row );
1063 model->ValueChanged( model_col, model_row );
1064}
1065
cbc9145c 1066IMPLEMENT_CLASS(wxDataViewToggleCell, wxDataViewCell)
605c2c4a 1067
93763ad5 1068wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype,
605c2c4a
RR
1069 wxDataViewCellMode mode ) :
1070 wxDataViewCell( varianttype, mode )
1071{
1072 m_renderer = (void*) gtk_cell_renderer_toggle_new();
93763ad5 1073
0fdc2321 1074 if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
605c2c4a 1075 {
553f7d8f
RR
1076 g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1077 }
1078 else
1079 {
1080
605c2c4a
RR
1081 GValue gvalue = { 0, };
1082 g_value_init( &gvalue, G_TYPE_BOOLEAN );
553f7d8f 1083 g_value_set_boolean( &gvalue, false );
605c2c4a
RR
1084 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1085 g_value_unset( &gvalue );
553f7d8f
RR
1086
1087 GValue gvalue2 = { 0, };
1088 g_value_init( &gvalue2, gtk_cell_renderer_mode_get_type() );
1089 g_value_set_enum( &gvalue2, GTK_CELL_RENDERER_MODE_INERT );
1090 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue2 );
1091 g_value_unset( &gvalue2 );
1092
605c2c4a
RR
1093 }
1094}
1095
1096bool wxDataViewToggleCell::SetValue( const wxVariant &value )
1097{
1098 bool tmp = value;
93763ad5 1099
605c2c4a
RR
1100 GValue gvalue = { 0, };
1101 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1102 g_value_set_boolean( &gvalue, tmp );
1103 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1104 g_value_unset( &gvalue );
93763ad5 1105
605c2c4a
RR
1106 return true;
1107}
1108
1109bool wxDataViewToggleCell::GetValue( wxVariant &value )
1110{
1111 GValue gvalue = { 0, };
1112 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1113 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
93763ad5 1114 bool tmp = g_value_get_boolean( &gvalue );
605c2c4a 1115 g_value_unset( &gvalue );
93763ad5 1116
605c2c4a
RR
1117 value = tmp;
1118
1119 return true;
1120}
93763ad5
WS
1121
1122// ---------------------------------------------------------
e152afc3 1123// wxDataViewCustomCell
93763ad5 1124// ---------------------------------------------------------
e152afc3
RR
1125
1126class wxDataViewCtrlDC: public wxWindowDC
1127{
1128public:
1129 wxDataViewCtrlDC( wxDataViewCtrl *window )
1130 {
1a367564 1131 GtkWidget *widget = window->m_treeview;
e152afc3
RR
1132 // Set later
1133 m_window = NULL;
4d496ecb 1134
e152afc3
RR
1135 m_context = window->GtkGetPangoDefaultContext();
1136 m_layout = pango_layout_new( m_context );
1137 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1138
1139 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1140
4d496ecb
RR
1141 // Set m_window later
1142 // SetUpDC();
1143 // m_owner = window;
e152afc3
RR
1144 }
1145};
1146
93763ad5 1147// ---------------------------------------------------------
e152afc3 1148// wxDataViewCustomCell
93763ad5 1149// ---------------------------------------------------------
e152afc3 1150
cbc9145c 1151IMPLEMENT_CLASS(wxDataViewCustomCell, wxDataViewCell)
e152afc3 1152
93763ad5 1153wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype,
ad63bf41 1154 wxDataViewCellMode mode, bool no_init ) :
e152afc3
RR
1155 wxDataViewCell( varianttype, mode )
1156{
1157 m_dc = NULL;
93763ad5 1158
ad63bf41
RR
1159 if (no_init)
1160 m_renderer = NULL;
1161 else
1162 Init();
1163}
1164
1165bool wxDataViewCustomCell::Init()
1166{
e152afc3
RR
1167 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1168 renderer->cell = this;
93763ad5 1169
e152afc3 1170 m_renderer = (void*) renderer;
93763ad5 1171
553f7d8f
RR
1172 if (m_mode & wxDATAVIEW_CELL_ACTIVATABLE)
1173 {
1174 GValue gvalue = { 0, };
1175 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1176 g_value_set_enum( &gvalue, GTK_CELL_RENDERER_MODE_ACTIVATABLE );
1177 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
1178 g_value_unset( &gvalue );
1179 }
93763ad5 1180
ad63bf41 1181 return true;
e152afc3
RR
1182}
1183
1184wxDataViewCustomCell::~wxDataViewCustomCell()
1185{
1186 if (m_dc)
1187 delete m_dc;
1188}
1189
1190wxDC *wxDataViewCustomCell::GetDC()
1191{
1192 if (m_dc == NULL)
4d496ecb
RR
1193 {
1194 if (GetOwner() == NULL)
1195 return NULL;
1196 if (GetOwner()->GetOwner() == NULL)
1197 return NULL;
e152afc3 1198 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
4d496ecb 1199 }
93763ad5 1200
e152afc3
RR
1201 return m_dc;
1202}
93763ad5
WS
1203
1204// ---------------------------------------------------------
ad63bf41 1205// wxDataViewProgressCell
93763ad5 1206// ---------------------------------------------------------
ad63bf41 1207
cbc9145c 1208IMPLEMENT_CLASS(wxDataViewProgressCell, wxDataViewCustomCell)
ad63bf41 1209
93763ad5 1210wxDataViewProgressCell::wxDataViewProgressCell( const wxString &label,
ad63bf41 1211 const wxString &varianttype, wxDataViewCellMode mode ) :
93763ad5 1212 wxDataViewCustomCell( varianttype, mode, true )
ad63bf41
RR
1213{
1214 m_label = label;
1215 m_value = 0;
93763ad5 1216
ad63bf41
RR
1217#ifdef __WXGTK26__
1218 if (!gtk_check_version(2,6,0))
1219 {
1220 m_renderer = (void*) gtk_cell_renderer_progress_new();
93763ad5 1221
ad63bf41
RR
1222 GValue gvalue = { 0, };
1223 g_value_init( &gvalue, G_TYPE_STRING );
1224 g_value_set_boolean( &gvalue, wxGTK_CONV(m_label) );
1225 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1226 g_value_unset( &gvalue );
1227 }
1228 else
1229#endif
1230 {
1231 // Use custom cell code
1232 wxDataViewCustomCell::Init();
1233 }
1234}
1235
1236wxDataViewProgressCell::~wxDataViewProgressCell()
1237{
1238}
1239
1240bool wxDataViewProgressCell::SetValue( const wxVariant &value )
1241{
1242#ifdef __WXGTK26__
1243 if (!gtk_check_version(2,6,0))
1244 {
1245 gint tmp = (int) value;
1246 GValue gvalue = { 0, };
1247 g_value_init( &gvalue, G_TYPE_INT );
1248 g_value_set_boolean( &gvalue, tmp );
1249 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1250 g_value_unset( &gvalue );
1251 }
1252 else
1253#endif
1254 {
1255 m_value = (long) value;
93763ad5 1256
ad63bf41
RR
1257 if (m_value < 0) m_value = 0;
1258 if (m_value > 100) m_value = 100;
1259 }
93763ad5 1260
ad63bf41
RR
1261 return true;
1262}
93763ad5 1263
ad63bf41
RR
1264bool wxDataViewProgressCell::Render( wxRect cell, wxDC *dc, int state )
1265{
1266 double pct = (double)m_value / 100.0;
1267 wxRect bar = cell;
1268 bar.width = (int)(cell.width * pct);
1269 dc->SetPen( *wxTRANSPARENT_PEN );
1270 dc->SetBrush( *wxBLUE_BRUSH );
1271 dc->DrawRectangle( bar );
1272
1273 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1274 dc->SetPen( *wxBLACK_PEN );
1275 dc->DrawRectangle( cell );
93763ad5 1276
ad63bf41
RR
1277 return true;
1278}
1279
1280wxSize wxDataViewProgressCell::GetSize()
1281{
1282 return wxSize(40,12);
1283}
93763ad5
WS
1284
1285// ---------------------------------------------------------
4d496ecb 1286// wxDataViewDateCell
93763ad5 1287// ---------------------------------------------------------
4d496ecb 1288
7ea3a0de
RR
1289class wxDataViewDateCellPopupTransient: public wxPopupTransientWindow
1290{
93763ad5 1291public:
7ea3a0de
RR
1292 wxDataViewDateCellPopupTransient( wxWindow* parent, wxDateTime *value,
1293 wxDataViewListModel *model, size_t col, size_t row ) :
1294 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1295 {
1296 m_model = model;
1297 m_col = col;
1298 m_row = row;
1299 m_cal = new wxCalendarCtrl( this, -1, *value );
1300 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1301 sizer->Add( m_cal, 1, wxGROW );
1302 SetSizer( sizer );
1303 sizer->Fit( this );
1304 }
93763ad5 1305
7ea3a0de
RR
1306 virtual void OnDismiss()
1307 {
1308 }
93763ad5 1309
7ea3a0de 1310 void OnCalendar( wxCalendarEvent &event );
93763ad5 1311
7ea3a0de 1312 wxCalendarCtrl *m_cal;
93763ad5 1313 wxDataViewListModel *m_model;
7ea3a0de
RR
1314 size_t m_col;
1315 size_t m_row;
93763ad5 1316
7ea3a0de
RR
1317private:
1318 DECLARE_EVENT_TABLE()
1319};
1320
1321BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient,wxPopupTransientWindow)
1322 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar )
1323END_EVENT_TABLE()
1324
1325void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent &event )
1326{
1327 wxDateTime date = event.GetDate();
1328 wxVariant value = date;
1329 m_model->SetValue( value, m_col, m_row );
1330 m_model->ValueChanged( m_col, m_row );
1331 DismissAndNotify();
1332}
1333
cbc9145c 1334IMPLEMENT_CLASS(wxDataViewDateCell, wxDataViewCustomCell)
4d496ecb
RR
1335
1336wxDataViewDateCell::wxDataViewDateCell( const wxString &varianttype,
1337 wxDataViewCellMode mode ) :
1338 wxDataViewCustomCell( varianttype, mode )
1339{
1340}
93763ad5 1341
4d496ecb
RR
1342bool wxDataViewDateCell::SetValue( const wxVariant &value )
1343{
1344 m_date = value.GetDateTime();
93763ad5 1345
4d496ecb
RR
1346 return true;
1347}
1348
1349bool wxDataViewDateCell::Render( wxRect cell, wxDC *dc, int state )
1350{
1351 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1352 wxString tmp = m_date.FormatDate();
1353 dc->DrawText( tmp, cell.x, cell.y );
1354
1355 return true;
1356}
1357
1358wxSize wxDataViewDateCell::GetSize()
1359{
1360 wxDataViewCtrl* view = GetOwner()->GetOwner();
1361 wxString tmp = m_date.FormatDate();
1362 wxCoord x,y,d;
1363 view->GetTextExtent( tmp, &x, &y, &d );
1364 return wxSize(x,y+d);
1365}
1366
1367bool wxDataViewDateCell::Activate( wxRect cell, wxDataViewListModel *model, size_t col, size_t row )
1368{
3f3af7e7
RR
1369 wxVariant variant;
1370 model->GetValue( variant, col, row );
7ea3a0de
RR
1371 wxDateTime value = variant.GetDateTime();
1372
93763ad5 1373 wxDataViewDateCellPopupTransient *popup = new wxDataViewDateCellPopupTransient(
7ea3a0de
RR
1374 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
1375 wxPoint pos = wxGetMousePosition();
1376 popup->Move( pos );
1377 popup->Layout();
1378 popup->Popup( popup->m_cal );
4d496ecb
RR
1379
1380 return true;
1381}
1382
93763ad5 1383// ---------------------------------------------------------
605c2c4a 1384// wxDataViewColumn
93763ad5 1385// ---------------------------------------------------------
7b4fde82
RR
1386
1387extern "C" {
1388static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1389 GtkCellRenderer *cell,
1390 GtkTreeModel *model,
1391 GtkTreeIter *iter,
1392 gpointer data );
1393}
1394
1395
1396static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1397 GtkCellRenderer *renderer,
1398 GtkTreeModel *model,
1399 GtkTreeIter *iter,
1400 gpointer data )
1401{
1402 g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
1403 GtkWxListStore *list_store = (GtkWxListStore *) model;
93763ad5 1404
7b4fde82 1405 wxDataViewCell *cell = (wxDataViewCell*) data;
a7f61f76
RR
1406
1407 size_t model_row = (size_t) iter->user_data;
93763ad5 1408
3f3af7e7
RR
1409 wxVariant value;
1410 list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row );
7b4fde82
RR
1411
1412 if (value.GetType() != cell->GetVariantType())
cbc9145c
RR
1413 wxLogError( wxT("Wrong type, required: %s but: %s"),
1414 value.GetType().c_str(),
1415 cell->GetVariantType().c_str() );
93763ad5 1416
7b4fde82
RR
1417 cell->SetValue( value );
1418}
1419
cbc9145c 1420IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
fa28826d 1421
533544f2 1422wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell, size_t model_column,
008b5a66
RR
1423 int width, int flags ) :
1424 wxDataViewColumnBase( title, cell, model_column, width, flags )
fa28826d 1425{
6842a71a 1426 GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
93763ad5 1427
6842a71a 1428 GtkTreeViewColumn *column = gtk_tree_view_column_new();
93763ad5 1429
6842a71a 1430 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
e4db172a 1431
008b5a66
RR
1432 if (flags & wxDATAVIEW_COL_RESIZABLE)
1433 gtk_tree_view_column_set_resizable( column, true );
1434 if (flags & wxDATAVIEW_COL_HIDDEN)
1435 gtk_tree_view_column_set_visible( column, false );
1436 if (flags & wxDATAVIEW_COL_SORTABLE)
1437 gtk_tree_view_column_set_sort_indicator( column, true );
e4db172a 1438
008b5a66
RR
1439 if (width > 0)
1440 {
1441 gtk_tree_view_column_set_fixed_width( column, width );
1442 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
1443 }
e4db172a 1444
008b5a66 1445 gtk_tree_view_column_pack_end( column, renderer, FALSE );
93763ad5
WS
1446
1447 gtk_tree_view_column_set_cell_data_func( column, renderer,
7b4fde82 1448 wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
93763ad5 1449
fa28826d
RR
1450 m_column = (void*) column;
1451}
1452
1453wxDataViewColumn::~wxDataViewColumn()
1454{
1455}
1456
1457void wxDataViewColumn::SetTitle( const wxString &title )
1458{
1459 wxDataViewColumnBase::SetTitle( title );
93763ad5 1460
fa28826d
RR
1461 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1462 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1463}
1464
533544f2
RR
1465int wxDataViewColumn::GetWidth()
1466{
1467 return gtk_tree_view_column_get_width( (GtkTreeViewColumn *)m_column );
1468}
1469
1470void wxDataViewColumn::SetFixedWidth( int width )
1471{
1472 gtk_tree_view_column_set_fixed_width( (GtkTreeViewColumn *)m_column, width );
1473}
1474
1475int wxDataViewColumn::GetFixedWidth()
1476{
1477 return gtk_tree_view_column_get_fixed_width( (GtkTreeViewColumn *)m_column );
1478}
1479
790b137e
RR
1480//-----------------------------------------------------------------------------
1481// wxDataViewCtrl
1482//-----------------------------------------------------------------------------
1483
239eaa41
RR
1484IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1485
1486wxDataViewCtrl::~wxDataViewCtrl()
1487{
8f850e28
RR
1488 if (m_notifier)
1489 GetModel()->RemoveNotifier( m_notifier );
239eaa41
RR
1490}
1491
1492void wxDataViewCtrl::Init()
1493{
8f850e28 1494 m_notifier = NULL;
239eaa41
RR
1495}
1496
1497bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
93763ad5 1498 const wxPoint& pos, const wxSize& size,
239eaa41
RR
1499 long style, const wxValidator& validator )
1500{
1501 Init();
93763ad5
WS
1502
1503 m_needParent = true;
1504 m_acceptsFocus = true;
239eaa41
RR
1505
1506 if (!PreCreation( parent, pos, size ) ||
1507 !CreateBase( parent, id, pos, size, style, validator ))
1508 {
1509 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
93763ad5 1510 return false;
239eaa41 1511 }
1a367564
RR
1512
1513 m_widget = gtk_scrolled_window_new (NULL, NULL);
6493aaca
VZ
1514
1515 GtkScrolledWindowSetBorder(m_widget, style);
1a367564
RR
1516
1517 m_treeview = gtk_tree_view_new();
1518 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
ed4b0fdc 1519
daebb44c
RR
1520 if (style & wxDV_MULTIPLE)
1521 {
1522 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
1523 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
1524 }
93763ad5 1525
1a367564
RR
1526 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
1527 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
1528 gtk_widget_show (m_treeview);
93763ad5 1529
239eaa41
RR
1530 m_parent->DoAddChild( this );
1531
1532 PostCreation(size);
1533
1534 return true;
1535}
1536
6e2e590f 1537bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
239eaa41
RR
1538{
1539 if (!wxDataViewCtrlBase::AssociateModel( model ))
1540 return false;
1541
6e2e590f
RR
1542 GtkWxListStore *gtk_store = wxgtk_list_store_new();
1543 gtk_store->model = model;
1544
8f850e28 1545 m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model );
6e2e590f 1546
93763ad5 1547 model->AddNotifier( m_notifier );
1557c77b 1548
1a367564
RR
1549 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) );
1550 g_object_unref( gtk_store );
93763ad5 1551
239eaa41
RR
1552 return true;
1553}
790b137e 1554
fa28826d
RR
1555bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
1556{
1557 if (!wxDataViewCtrlBase::AppendColumn(col))
1558 return false;
93763ad5 1559
fa28826d
RR
1560 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
1561
1a367564 1562 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
fa28826d
RR
1563
1564 return true;
1565}
1566
93763ad5 1567#endif
4ed7af08 1568 // !wxUSE_GENERICDATAVIEWCTRL
790b137e 1569
93763ad5 1570#endif
4ed7af08 1571 // wxUSE_DATAVIEWCTRL