]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
8edf067ec940e43897df0c89116f0b261c5741fa
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
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
13 #include "wx/defs.h"
14
15 #if wxUSE_DATAVIEWCTRL
16
17 #include "wx/dataview.h"
18 #include "wx/stockitem.h"
19 #include "wx/dcclient.h"
20
21 #include "wx/gtk/private.h"
22 #include "wx/gtk/win_gtk.h"
23
24 #include <gobject/gvaluecollector.h>
25 #include <gtk/gtktreemodel.h>
26 #include <gtk/gtktreednd.h>
27
28 //-----------------------------------------------------------------------------
29 // classes
30 //-----------------------------------------------------------------------------
31
32 class wxDataViewCtrl;
33
34 //-----------------------------------------------------------------------------
35 // idle system
36 //-----------------------------------------------------------------------------
37
38 extern void wxapp_install_idle_handler();
39 extern bool g_isIdle;
40
41 //-----------------------------------------------------------------------------
42 // data
43 //-----------------------------------------------------------------------------
44
45 extern bool g_blockEventsOnDrag;
46
47 //-----------------------------------------------------------------------------
48 // define new GTK+ class wxGtkListStore
49 //-----------------------------------------------------------------------------
50
51 extern "C" {
52
53 #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ())
54 #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore))
55 #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
56 #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE))
57 #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE))
58 #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
59
60 GType gtk_wx_list_store_get_type (void);
61
62 typedef struct _GtkWxListStore GtkWxListStore;
63 typedef struct _GtkWxListStoreClass GtkWxListStoreClass;
64
65 struct _GtkWxListStore
66 {
67 GObject parent;
68
69 /*< private >*/
70 gint stamp;
71 wxDataViewListModel *model;
72 };
73
74 struct _GtkWxListStoreClass
75 {
76 GObjectClass list_parent_class;
77
78 };
79
80 static GtkWxListStore *wxgtk_list_store_new (void);
81 static void wxgtk_list_store_init (GtkWxListStore *list_store);
82 static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass);
83 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface);
84 static void wxgtk_list_store_finalize (GObject *object);
85 static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model);
86 static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model);
87 static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
88 gint index);
89 static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
90 GtkTreeIter *iter,
91 GtkTreePath *path);
92 static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model,
93 GtkTreeIter *iter);
94 static void wxgtk_list_store_get_value (GtkTreeModel *tree_model,
95 GtkTreeIter *iter,
96 gint column,
97 GValue *value);
98 static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
99 GtkTreeIter *iter);
100 static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
101 GtkTreeIter *iter,
102 GtkTreeIter *parent);
103 static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
104 GtkTreeIter *iter);
105 static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
106 GtkTreeIter *iter);
107 static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
108 GtkTreeIter *iter,
109 GtkTreeIter *parent,
110 gint n);
111 static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
112 GtkTreeIter *iter,
113 GtkTreeIter *child);
114
115 static GObjectClass *list_parent_class = NULL;
116
117 GType
118 gtk_wx_list_store_get_type (void)
119 {
120 static GType list_store_type = 0;
121
122 if (!list_store_type)
123 {
124 static const GTypeInfo list_store_info =
125 {
126 sizeof (GtkWxListStoreClass),
127 NULL, /* base_init */
128 NULL, /* base_finalize */
129 (GClassInitFunc) wxgtk_list_store_class_init,
130 NULL, /* class_finalize */
131 NULL, /* class_data */
132 sizeof (GtkWxListStore),
133 0,
134 (GInstanceInitFunc) wxgtk_list_store_init,
135 };
136
137 static const GInterfaceInfo tree_model_info =
138 {
139 (GInterfaceInitFunc) wxgtk_list_store_tree_model_init,
140 NULL,
141 NULL
142 };
143
144 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore",
145 &list_store_info, (GTypeFlags)0 );
146
147 g_type_add_interface_static (list_store_type,
148 GTK_TYPE_TREE_MODEL,
149 &tree_model_info);
150 }
151
152 return list_store_type;
153 }
154
155 static GtkWxListStore *
156 wxgtk_list_store_new(void)
157 {
158 GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL);
159 return retval;
160 }
161
162 static void
163 wxgtk_list_store_class_init (GtkWxListStoreClass *klass)
164 {
165 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
166 GObjectClass *object_class = (GObjectClass*) klass;
167 object_class->finalize = wxgtk_list_store_finalize;
168 }
169
170 static void
171 wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface)
172 {
173 iface->get_flags = wxgtk_list_store_get_flags;
174 iface->get_n_columns = wxgtk_list_store_get_n_columns;
175 iface->get_column_type = wxgtk_list_store_get_column_type;
176 iface->get_iter = wxgtk_list_store_get_iter;
177 iface->get_path = wxgtk_list_store_get_path;
178 iface->get_value = wxgtk_list_store_get_value;
179 iface->iter_next = wxgtk_list_store_iter_next;
180 iface->iter_children = wxgtk_list_store_iter_children;
181 iface->iter_has_child = wxgtk_list_store_iter_has_child;
182 iface->iter_n_children = wxgtk_list_store_iter_n_children;
183 iface->iter_nth_child = wxgtk_list_store_iter_nth_child;
184 iface->iter_parent = wxgtk_list_store_iter_parent;
185 }
186
187 static void
188 wxgtk_list_store_init (GtkWxListStore *list_store)
189 {
190 list_store->model = NULL;
191 list_store->stamp = g_random_int();
192 }
193
194 static void
195 wxgtk_list_store_finalize (GObject *object)
196 {
197 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
198
199 /* we need to sort out, which class deletes what */
200 /* delete list_store->model; */
201
202 /* must chain up */
203 (* list_parent_class->finalize) (object);
204 }
205
206 } // extern "C"
207
208 //-----------------------------------------------------------------------------
209 // implement callbacks from wxGtkListStore class by letting
210 // them call the methods of wxWidgets' wxDataViewListModel
211 //-----------------------------------------------------------------------------
212
213 static GtkTreeModelFlags
214 wxgtk_list_store_get_flags (GtkTreeModel *tree_model)
215 {
216 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 );
217
218 // GTK+ list store uses a linked list for storing the
219 // items and a pointer to a child is used as the member
220 // field of a GtkTreeIter. This means that the iter is
221 // valid in the GtkListStore as long as the child exists.
222 // We use the index of the row and since the index of a
223 // specific row will change if a row above is deleted,
224 // the iter does not persist
225 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY;
226 }
227
228 static gint
229 wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model)
230 {
231 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
232 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), 0);
233
234 return list_store->model->GetNumberOfCols();
235 }
236
237 static GType
238 wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
239 gint index)
240 {
241 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
242 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), G_TYPE_INVALID);
243
244 GType gtype = G_TYPE_INVALID;
245
246 wxString wxtype = list_store->model->GetColType( (size_t) index );
247
248 if (wxtype == wxT("string"))
249 gtype = G_TYPE_STRING;
250
251 return gtype;
252 }
253
254 static gboolean
255 wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
256 GtkTreeIter *iter,
257 GtkTreePath *path)
258 {
259 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
260 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
261 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
262
263 size_t i = (size_t)gtk_tree_path_get_indices (path)[0];
264
265 if (i >= list_store->model->GetNumberOfRows())
266 return FALSE;
267
268 iter->stamp = list_store->stamp;
269 // user_data is just the index
270 iter->user_data = (gpointer) i;
271
272 return TRUE;
273 }
274
275 static GtkTreePath *
276 wxgtk_list_store_get_path (GtkTreeModel *tree_model,
277 GtkTreeIter *iter)
278 {
279 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL);
280 g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL);
281
282 GtkTreePath *retval = gtk_tree_path_new ();
283 // user_data is just the index
284 int i = (int) iter->user_data;
285 gtk_tree_path_append_index (retval, i);
286 return retval;
287 }
288
289 static void
290 wxgtk_list_store_get_value (GtkTreeModel *tree_model,
291 GtkTreeIter *iter,
292 gint column,
293 GValue *value)
294 {
295 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
296 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) );
297
298 wxDataViewListModel *model = list_store->model;
299 wxString mtype = model->GetColType( (size_t) column );
300 if (mtype == wxT("string"))
301 {
302 g_value_init( value, G_TYPE_STRING );
303 wxVariant variant = model->GetValue( (size_t) column, (size_t) iter->user_data );
304 g_value_set_string( value, wxGTK_CONV(variant.GetString()) );
305 }
306 else
307 {
308 }
309
310 #if 0
311 GtkTreeDataList *list;
312 gint tmp_column = column;
313
314 g_return_if_fail (column < GTK_LIST_STORE (tree_model)->n_columns);
315 g_return_if_fail (GTK_LIST_STORE (tree_model)->stamp == iter->stamp);
316
317 list = G_SLIST (iter->user_data)->data;
318
319 while (tmp_column-- > 0 && list)
320 list = list->next;
321
322 if (list == NULL)
323 g_value_init (value, GTK_LIST_STORE (tree_model)->column_headers[column]);
324 else
325 _gtk_tree_data_list_node_to_value (list,
326 GTK_LIST_STORE (tree_model)->column_headers[column],
327 value);
328 #endif
329
330 }
331
332 static gboolean
333 wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
334 GtkTreeIter *iter)
335 {
336 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
337 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
338
339 g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE);
340
341 int n = (int) iter->user_data;
342
343 if (n == -1)
344 return FALSE;
345
346 if (n >= (int) list_store->model->GetNumberOfRows())
347 return FALSE;
348
349 iter->user_data = (gpointer) ++n;
350
351 return TRUE;
352 }
353
354 static gboolean
355 wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
356 GtkTreeIter *iter,
357 GtkTreeIter *parent)
358 {
359 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
360 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
361
362 // this is a list, nodes have no children
363 if (parent)
364 return FALSE;
365
366 iter->stamp = list_store->stamp;
367 iter->user_data = (gpointer) -1;
368
369 return TRUE;
370 }
371
372 static gboolean
373 wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
374 GtkTreeIter *iter)
375 {
376 return FALSE;
377 }
378
379 static gint
380 wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
381 GtkTreeIter *iter)
382 {
383 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1);
384 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
385
386 if (iter == NULL)
387 return (gint) list_store->model->GetNumberOfRows();
388
389 g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
390
391 return 0;
392 }
393
394 static gboolean
395 wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
396 GtkTreeIter *iter,
397 GtkTreeIter *parent,
398 gint n)
399 {
400 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
401 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
402
403 if (parent)
404 return FALSE;
405
406 if (n < 0)
407 return FALSE;
408
409 if (n >= (gint) list_store->model->GetNumberOfRows())
410 return FALSE;
411
412 iter->stamp = list_store->stamp;
413 iter->user_data = (gpointer) n;
414
415 return TRUE;
416 }
417
418 static gboolean
419 wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
420 GtkTreeIter *iter,
421 GtkTreeIter *child)
422 {
423 return FALSE;
424 }
425
426 //-----------------------------------------------------------------------------
427 // define new GTK+ class wxGtkCellRenderer
428 //-----------------------------------------------------------------------------
429
430 extern "C" {
431
432 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
433 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
434 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
435 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
436 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
437 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
438
439 GType gtk_wx_cell_renderer_get_type (void);
440
441 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
442 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
443
444 struct _GtkWxCellRenderer
445 {
446 GtkCellRenderer parent;
447
448 /*< private >*/
449 wxDataViewCustomCell *cell;
450 };
451
452 struct _GtkWxCellRendererClass
453 {
454 GtkCellRendererClass cell_parent_class;
455
456 };
457
458
459 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
460 static void gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell);
461 static void gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass);
462 static void gtk_wx_cell_renderer_finalize (GObject *object);
463 static void gtk_wx_cell_renderer_get_size (GtkCellRenderer *cell,
464 GtkWidget *widget,
465 GdkRectangle *rectangle,
466 gint *x_offset,
467 gint *y_offset,
468 gint *width,
469 gint *height);
470 static void gtk_wx_cell_renderer_render (GtkCellRenderer *cell,
471 GdkWindow *window,
472 GtkWidget *widget,
473 GdkRectangle *background_area,
474 GdkRectangle *cell_area,
475 GdkRectangle *expose_area,
476 GtkCellRendererState flags);
477
478 static GObjectClass *cell_parent_class = NULL;
479
480 } // extern "C"
481
482 GType
483 gtk_wx_cell_renderer_get_type (void)
484 {
485 static GType cell_wx_type = 0;
486
487 if (!cell_wx_type)
488 {
489 static const GTypeInfo cell_wx_info =
490 {
491 sizeof (GtkWxCellRendererClass),
492 NULL, /* base_init */
493 NULL, /* base_finalize */
494 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
495 NULL, /* class_finalize */
496 NULL, /* class_data */
497 sizeof (GtkWxCellRenderer),
498 0, /* n_preallocs */
499 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
500 };
501
502 cell_wx_type =
503 g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkWxCellRenderer",
504 &cell_wx_info, (GTypeFlags)0);
505 }
506
507 return cell_wx_type;
508 }
509
510 static void
511 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
512 {
513 cell->cell = NULL;
514 }
515
516 static void
517 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
518 {
519 GObjectClass *object_class = G_OBJECT_CLASS (klass);
520 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
521
522 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
523
524 object_class->finalize = gtk_wx_cell_renderer_finalize;
525
526 cell_class->get_size = gtk_wx_cell_renderer_get_size;
527 cell_class->render = gtk_wx_cell_renderer_render;
528 }
529
530 static void
531 gtk_wx_cell_renderer_finalize (GObject *object)
532 {
533 /* must chain up */
534 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
535 }
536
537 GtkCellRenderer*
538 gtk_wx_cell_renderer_new (void)
539 {
540 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
541 }
542
543 static void
544 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
545 GtkWidget *widget,
546 GdkRectangle *cell_area,
547 gint *x_offset,
548 gint *y_offset,
549 gint *width,
550 gint *height)
551 {
552 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
553 wxDataViewCustomCell *cell = wxrenderer->cell;
554
555 wxSize size = cell->GetSize();
556
557 gint calc_width = (gint) renderer->xpad * 2 + size.x;
558 gint calc_height = (gint) renderer->ypad * 2 + size.y;
559
560 if (x_offset)
561 *x_offset = 0;
562 if (y_offset)
563 *y_offset = 0;
564
565 if (cell_area && size.x > 0 && size.y > 0)
566 {
567 if (x_offset)
568 {
569 *x_offset = (gint)((renderer->xalign *
570 (cell_area->width - calc_width - 2 * renderer->xpad)));
571 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
572 }
573 if (y_offset)
574 {
575 *y_offset = (gint)((renderer->yalign *
576 (cell_area->height - calc_height - 2 * renderer->ypad)));
577 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
578 }
579 }
580
581 if (width)
582 *width = calc_width;
583
584 if (height)
585 *height = calc_height;
586 }
587
588 static void
589 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
590 GdkWindow *window,
591 GtkWidget *widget,
592 GdkRectangle *background_area,
593 GdkRectangle *cell_area,
594 GdkRectangle *expose_area,
595 GtkCellRendererState flags)
596
597 {
598 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
599 wxDataViewCustomCell *cell = wxrenderer->cell;
600
601 GdkRectangle rect;
602 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
603 &rect.x,
604 &rect.y,
605 &rect.width,
606 &rect.height);
607
608 rect.x += cell_area->x;
609 rect.y += cell_area->y;
610 rect.width -= renderer->xpad * 2;
611 rect.height -= renderer->ypad * 2;
612
613 GdkRectangle dummy;
614 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
615 {
616 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
617 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
618 dc->m_window = window;
619
620 int state = 0;
621 if (flags & GTK_CELL_RENDERER_SELECTED)
622 state |= wxDATAVIEW_CELL_SELECTED;
623 if (flags & GTK_CELL_RENDERER_PRELIT)
624 state |= wxDATAVIEW_CELL_PRELIT;
625 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
626 state |= wxDATAVIEW_CELL_INSENSITIVE;
627 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
628 state |= wxDATAVIEW_CELL_INSENSITIVE;
629 if (flags & GTK_CELL_RENDERER_FOCUSED)
630 state |= wxDATAVIEW_CELL_FOCUSED;
631 cell->Render( renderrect, dc, state );
632 }
633 }
634
635 // ---------------------------------------------------------
636 // wxGtkDataViewListModelNotifier
637 // ---------------------------------------------------------
638
639 class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier
640 {
641 public:
642 wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model );
643
644 virtual bool RowAppended();
645 virtual bool RowPrepended();
646 virtual bool RowInserted( size_t before );
647 virtual bool RowDeleted( size_t row );
648 virtual bool RowChanged( size_t row );
649 virtual bool ValueChanged( size_t col, size_t row );
650 virtual bool Cleared();
651
652 GtkWxListStore *m_gtk_store;
653 wxDataViewListModel *m_wx_model;
654 };
655
656 // ---------------------------------------------------------
657 // wxGtkDataViewListModelNotifier
658 // ---------------------------------------------------------
659
660 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
661 GtkWxListStore* gtk_store, wxDataViewListModel *wx_model )
662 {
663 m_gtk_store = gtk_store;
664 m_wx_model = wx_model;
665 }
666
667 bool wxGtkDataViewListModelNotifier::RowAppended()
668 {
669 size_t pos = m_wx_model->GetNumberOfRows()-1;
670
671 GtkTreeIter iter;
672 iter.stamp = m_gtk_store->stamp;
673 iter.user_data = (gpointer) pos;
674
675 GtkTreePath *path = gtk_tree_path_new ();
676 gtk_tree_path_append_index (path, (gint) pos);
677 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
678 gtk_tree_path_free (path);
679
680 return true;
681 }
682
683 bool wxGtkDataViewListModelNotifier::RowPrepended()
684 {
685 GtkTreeIter iter;
686 iter.stamp = m_gtk_store->stamp;
687 iter.user_data = (gpointer) 0;
688
689 GtkTreePath *path = gtk_tree_path_new ();
690 gtk_tree_path_append_index (path, (gint) 0);
691 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
692 gtk_tree_path_free (path);
693
694 return true;
695 }
696
697 bool wxGtkDataViewListModelNotifier::RowInserted( size_t before )
698 {
699 return false;
700 }
701
702 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row )
703 {
704 return false;
705 }
706
707 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row )
708 {
709 GtkTreeIter iter;
710 iter.stamp = m_gtk_store->stamp;
711 iter.user_data = (gpointer) row;
712 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter);
713 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter);
714 gtk_tree_path_free (path);
715
716 return true;
717 }
718
719 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t col, size_t row )
720 {
721 return RowChanged( row );
722 }
723
724 bool wxGtkDataViewListModelNotifier::Cleared()
725 {
726 return false;
727 }
728
729 // ---------------------------------------------------------
730 // wxDataViewCell
731 // ---------------------------------------------------------
732
733 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell, wxDataViewCellBase)
734
735 wxDataViewCell::wxDataViewCell( const wxString &varianttype, wxDataViewCellMode mode ) :
736 wxDataViewCellBase( varianttype, mode )
737 {
738 m_renderer = NULL;
739 }
740
741 // ---------------------------------------------------------
742 // wxDataViewTextCell
743 // ---------------------------------------------------------
744
745 extern "C" {
746 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
747 gchar *arg1, gchar *arg2, gpointer user_data );
748 }
749
750 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
751 gchar *arg1, gchar *arg2, gpointer user_data )
752 {
753 wxDataViewTextCell *cell = (wxDataViewTextCell*) user_data;
754
755 wxString tmp = wxGTK_CONV_BACK( arg2 );
756 wxVariant value = tmp;
757 if (!cell->Validate( value ))
758 return;
759
760 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
761
762 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
763 size_t model_row = (size_t)gtk_tree_path_get_indices (path)[0];
764 gtk_tree_path_free( path );
765
766 size_t model_col = cell->GetOwner()->GetModelColumn();
767
768 model->SetValue( value, model_col, model_row );
769 model->ValueChanged( model_col, model_row );
770 }
771
772 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell, wxDataViewCell)
773
774 wxDataViewTextCell::wxDataViewTextCell( const wxString &varianttype, wxDataViewCellMode mode ) :
775 wxDataViewCell( varianttype, mode )
776 {
777 m_renderer = (void*) gtk_cell_renderer_text_new();
778
779 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
780 {
781 GValue gvalue = { 0, };
782 g_value_init( &gvalue, G_TYPE_BOOLEAN );
783 g_value_set_boolean( &gvalue, true );
784 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
785 g_value_unset( &gvalue );
786
787 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
788 }
789 }
790
791 bool wxDataViewTextCell::SetValue( const wxVariant &value )
792 {
793 wxString tmp = value;
794
795 GValue gvalue = { 0, };
796 g_value_init( &gvalue, G_TYPE_STRING );
797 g_value_set_string( &gvalue, wxGTK_CONV( tmp ) );
798 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
799 g_value_unset( &gvalue );
800
801 return true;
802 }
803
804 bool wxDataViewTextCell::GetValue( wxVariant &value )
805 {
806 GValue gvalue = { 0, };
807 g_value_init( &gvalue, G_TYPE_STRING );
808 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
809 wxString tmp = wxGTK_CONV_BACK( g_value_get_string( &gvalue ) );
810 g_value_unset( &gvalue );
811
812 value = tmp;
813
814 return true;
815 }
816
817 // ---------------------------------------------------------
818 // wxDataViewToggleCell
819 // ---------------------------------------------------------
820
821 extern "C" {
822 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
823 gchar *path, gpointer user_data );
824 }
825
826 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
827 gchar *path, gpointer user_data )
828 {
829 wxDataViewToggleCell *cell = (wxDataViewToggleCell*) user_data;
830
831 // get old value
832 GValue gvalue = { 0, };
833 g_value_init( &gvalue, G_TYPE_BOOLEAN );
834 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
835 bool tmp = g_value_get_boolean( &gvalue );
836 g_value_unset( &gvalue );
837 // invert it
838 tmp = !tmp;
839
840 wxVariant value = tmp;
841 if (!cell->Validate( value ))
842 return;
843
844 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
845
846 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
847 size_t model_row = (size_t)gtk_tree_path_get_indices (gtk_path)[0];
848 gtk_tree_path_free( gtk_path );
849
850 size_t model_col = cell->GetOwner()->GetModelColumn();
851
852 model->SetValue( value, model_col, model_row );
853 model->ValueChanged( model_col, model_row );
854 }
855
856 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell, wxDataViewCell)
857
858 wxDataViewToggleCell::wxDataViewToggleCell( const wxString &varianttype,
859 wxDataViewCellMode mode ) :
860 wxDataViewCell( varianttype, mode )
861 {
862 m_renderer = (void*) gtk_cell_renderer_toggle_new();
863
864 if (m_mode & wxDATAVIEW_CELL_EDITABLE)
865 {
866 GValue gvalue = { 0, };
867 g_value_init( &gvalue, G_TYPE_BOOLEAN );
868 g_value_set_boolean( &gvalue, true );
869 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
870 g_value_unset( &gvalue );
871
872 g_signal_connect_after( m_renderer, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
873 }
874 }
875
876 bool wxDataViewToggleCell::SetValue( const wxVariant &value )
877 {
878 bool tmp = value;
879
880 GValue gvalue = { 0, };
881 g_value_init( &gvalue, G_TYPE_BOOLEAN );
882 g_value_set_boolean( &gvalue, tmp );
883 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
884 g_value_unset( &gvalue );
885
886 return true;
887 }
888
889 bool wxDataViewToggleCell::GetValue( wxVariant &value )
890 {
891 GValue gvalue = { 0, };
892 g_value_init( &gvalue, G_TYPE_BOOLEAN );
893 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
894 bool tmp = g_value_get_boolean( &gvalue );
895 g_value_unset( &gvalue );
896
897 value = tmp;
898
899 return true;
900 }
901
902 // ---------------------------------------------------------
903 // wxDataViewCustomCell
904 // ---------------------------------------------------------
905
906 class wxDataViewCtrlDC: public wxWindowDC
907 {
908 public:
909 wxDataViewCtrlDC( wxDataViewCtrl *window )
910 {
911 GtkWidget *widget = window->GetHandle();
912 // Set later
913 m_window = NULL;
914
915 m_context = window->GtkGetPangoDefaultContext();
916 m_layout = pango_layout_new( m_context );
917 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
918
919 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
920
921 SetUpDC();
922
923 m_owner = window;
924 }
925 };
926
927 // ---------------------------------------------------------
928 // wxDataViewCustomCell
929 // ---------------------------------------------------------
930
931 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell, wxDataViewCell)
932
933 wxDataViewCustomCell::wxDataViewCustomCell( const wxString &varianttype,
934 wxDataViewCellMode mode ) :
935 wxDataViewCell( varianttype, mode )
936 {
937 m_dc = NULL;
938
939 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
940 renderer->cell = this;
941
942 m_renderer = (void*) renderer;
943 }
944
945 wxDataViewCustomCell::~wxDataViewCustomCell()
946 {
947 if (m_dc)
948 delete m_dc;
949 }
950
951 wxDC *wxDataViewCustomCell::GetDC()
952 {
953 if (m_dc == NULL)
954 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
955
956 return m_dc;
957 }
958
959 // ---------------------------------------------------------
960 // wxDataViewColumn
961 // ---------------------------------------------------------
962
963 extern "C" {
964 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
965 GtkCellRenderer *cell,
966 GtkTreeModel *model,
967 GtkTreeIter *iter,
968 gpointer data );
969 }
970
971
972 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
973 GtkCellRenderer *renderer,
974 GtkTreeModel *model,
975 GtkTreeIter *iter,
976 gpointer data )
977 {
978 g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
979 GtkWxListStore *list_store = (GtkWxListStore *) model;
980
981 wxDataViewCell *cell = (wxDataViewCell*) data;
982
983 size_t model_row = (size_t) iter->user_data;
984
985 wxVariant value = list_store->model->GetValue(
986 cell->GetOwner()->GetModelColumn(), model_row );
987
988 if (value.GetType() != cell->GetVariantType())
989 wxPrintf( wxT("Wrong type\n") );
990
991 cell->SetValue( value );
992 }
993
994 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
995
996 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewCell *cell,
997 size_t model_column, int flags ) :
998 wxDataViewColumnBase( title, cell, model_column, flags )
999 {
1000 GtkCellRenderer *renderer = (GtkCellRenderer *) cell->GetGtkHandle();
1001
1002 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1003
1004 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1005
1006 gtk_tree_view_column_pack_start( column, renderer, TRUE );
1007
1008 gtk_tree_view_column_set_cell_data_func( column, renderer,
1009 wxGtkTreeCellDataFunc, (gpointer) cell, NULL );
1010
1011 m_column = (void*) column;
1012 }
1013
1014 wxDataViewColumn::~wxDataViewColumn()
1015 {
1016 }
1017
1018 void wxDataViewColumn::SetTitle( const wxString &title )
1019 {
1020 wxDataViewColumnBase::SetTitle( title );
1021
1022 GtkTreeViewColumn *column = (GtkTreeViewColumn *)m_column;
1023 gtk_tree_view_column_set_title( column, wxGTK_CONV(title) );
1024 }
1025
1026 //-----------------------------------------------------------------------------
1027 // wxDataViewCtrl
1028 //-----------------------------------------------------------------------------
1029
1030 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1031
1032 wxDataViewCtrl::~wxDataViewCtrl()
1033 {
1034 }
1035
1036 void wxDataViewCtrl::Init()
1037 {
1038 }
1039
1040 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
1041 const wxPoint& pos, const wxSize& size,
1042 long style, const wxValidator& validator )
1043 {
1044 Init();
1045
1046 m_needParent = TRUE;
1047 m_acceptsFocus = TRUE;
1048
1049 if (!PreCreation( parent, pos, size ) ||
1050 !CreateBase( parent, id, pos, size, style, validator ))
1051 {
1052 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1053 return FALSE;
1054 }
1055
1056 m_widget = gtk_tree_view_new();
1057
1058 m_parent->DoAddChild( this );
1059
1060 PostCreation(size);
1061
1062 return true;
1063 }
1064
1065 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
1066 {
1067 if (!wxDataViewCtrlBase::AssociateModel( model ))
1068 return false;
1069
1070 GtkWxListStore *gtk_store = wxgtk_list_store_new();
1071 gtk_store->model = model;
1072
1073 wxGtkDataViewListModelNotifier *notifier =
1074 new wxGtkDataViewListModelNotifier( gtk_store, model );
1075
1076 model->SetNotifier( notifier );
1077
1078 gtk_tree_view_set_model( GTK_TREE_VIEW(m_widget), GTK_TREE_MODEL(gtk_store) );
1079
1080 return true;
1081 }
1082
1083 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
1084 {
1085 if (!wxDataViewCtrlBase::AppendColumn(col))
1086 return false;
1087
1088 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
1089
1090 gtk_tree_view_append_column( GTK_TREE_VIEW(m_widget), column );
1091
1092 return true;
1093 }
1094
1095
1096 #endif // wxUSE_DATAVIEWCTRL
1097