]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dataview.cpp
Moved all the coordinate system calculation to wxDCBase
[wxWidgets.git] / src / gtk / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 #if wxUSE_DATAVIEWCTRL
14
15 #include "wx/dataview.h"
16
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
18
19 #ifndef WX_PRECOMP
20 #include "wx/log.h"
21 #include "wx/dcclient.h"
22 #include "wx/sizer.h"
23 #endif
24
25 #include "wx/stockitem.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
28 #include "wx/icon.h"
29
30
31 #include "wx/gtk/private.h"
32 #include "wx/gtk/win_gtk.h"
33
34 #include <gobject/gvaluecollector.h>
35 #include <gtk/gtktreemodel.h>
36 #include <gtk/gtktreednd.h>
37
38 #include <gdk/gdkkeysyms.h>
39
40 //-----------------------------------------------------------------------------
41 // classes
42 //-----------------------------------------------------------------------------
43
44 class wxDataViewCtrl;
45
46 //-----------------------------------------------------------------------------
47 // data
48 //-----------------------------------------------------------------------------
49
50 extern bool g_blockEventsOnDrag;
51
52 //-----------------------------------------------------------------------------
53 // define new GTK+ class wxGtkListStore
54 //-----------------------------------------------------------------------------
55
56 extern "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
65 GType gtk_wx_list_store_get_type (void);
66
67 typedef struct _GtkWxListStore GtkWxListStore;
68 typedef struct _GtkWxListStoreClass GtkWxListStoreClass;
69
70 struct _GtkWxListStore
71 {
72 GObject parent;
73
74 /*< private >*/
75 gint stamp;
76 wxDataViewListModel *model;
77 };
78
79 struct _GtkWxListStoreClass
80 {
81 GObjectClass list_parent_class;
82 };
83
84 static GtkWxListStore *wxgtk_list_store_new (void);
85 static void wxgtk_list_store_init (GtkWxListStore *list_store);
86 static void wxgtk_list_store_class_init (GtkWxListStoreClass *klass);
87 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface *iface);
88 static void wxgtk_list_store_finalize (GObject *object);
89 static GtkTreeModelFlags wxgtk_list_store_get_flags (GtkTreeModel *tree_model);
90 static gint wxgtk_list_store_get_n_columns (GtkTreeModel *tree_model);
91 static GType wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
92 gint index);
93 static gboolean wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
94 GtkTreeIter *iter,
95 GtkTreePath *path);
96 static GtkTreePath *wxgtk_list_store_get_path (GtkTreeModel *tree_model,
97 GtkTreeIter *iter);
98 static void wxgtk_list_store_get_value (GtkTreeModel *tree_model,
99 GtkTreeIter *iter,
100 gint column,
101 GValue *value);
102 static gboolean wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
103 GtkTreeIter *iter);
104 static gboolean wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
105 GtkTreeIter *iter,
106 GtkTreeIter *parent);
107 static gboolean wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
108 GtkTreeIter *iter);
109 static gint wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
110 GtkTreeIter *iter);
111 static gboolean wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
112 GtkTreeIter *iter,
113 GtkTreeIter *parent,
114 gint n);
115 static gboolean wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
116 GtkTreeIter *iter,
117 GtkTreeIter *child);
118
119 static GObjectClass *list_parent_class = NULL;
120
121 GType
122 gtk_wx_list_store_get_type (void)
123 {
124 static GType list_store_type = 0;
125
126 if (!list_store_type)
127 {
128 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 };
140
141 static const GInterfaceInfo tree_model_info =
142 {
143 (GInterfaceInitFunc) wxgtk_list_store_tree_model_init,
144 NULL,
145 NULL
146 };
147
148 list_store_type = g_type_register_static (G_TYPE_OBJECT, "GtkWxListStore",
149 &list_store_info, (GTypeFlags)0 );
150
151 g_type_add_interface_static (list_store_type,
152 GTK_TYPE_TREE_MODEL,
153 &tree_model_info);
154 }
155
156 return list_store_type;
157 }
158
159 static GtkWxListStore *
160 wxgtk_list_store_new(void)
161 {
162 GtkWxListStore *retval = (GtkWxListStore *) g_object_new (GTK_TYPE_WX_LIST_STORE, NULL);
163 return retval;
164 }
165
166 static void
167 wxgtk_list_store_class_init (GtkWxListStoreClass *klass)
168 {
169 list_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
170 GObjectClass *object_class = (GObjectClass*) klass;
171 object_class->finalize = wxgtk_list_store_finalize;
172 }
173
174 static void
175 wxgtk_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
191 static void
192 wxgtk_list_store_init (GtkWxListStore *list_store)
193 {
194 list_store->model = NULL;
195 list_store->stamp = g_random_int();
196 }
197
198 static void
199 wxgtk_list_store_finalize (GObject *object)
200 {
201 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
202
203 /* we need to sort out, which class deletes what */
204 /* delete list_store->model; */
205
206 /* must chain up */
207 (* list_parent_class->finalize) (object);
208 }
209
210 } // extern "C"
211
212 //-----------------------------------------------------------------------------
213 // implement callbacks from wxGtkListStore class by letting
214 // them call the methods of wxWidgets' wxDataViewListModel
215 //-----------------------------------------------------------------------------
216
217 static GtkTreeModelFlags
218 wxgtk_list_store_get_flags (GtkTreeModel *tree_model)
219 {
220 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), (GtkTreeModelFlags)0 );
221
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;
230 }
231
232 static gint
233 wxgtk_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
238 return list_store->model->GetColumnCount();
239 }
240
241 static GType
242 wxgtk_list_store_get_column_type (GtkTreeModel *tree_model,
243 gint index)
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
248 GType gtype = G_TYPE_INVALID;
249
250 wxString wxtype = list_store->model->GetColumnType( (unsigned int) index );
251
252 if (wxtype == wxT("string"))
253 gtype = G_TYPE_STRING;
254 else
255 {
256 wxFAIL_MSG( _T("non-string columns not supported yet") );
257 }
258
259 return gtype;
260 }
261
262 static gboolean
263 wxgtk_list_store_get_iter (GtkTreeModel *tree_model,
264 GtkTreeIter *iter,
265 GtkTreePath *path)
266 {
267 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
268 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
269 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
270
271 unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
272
273 if (i >= list_store->model->GetRowCount())
274 return FALSE;
275
276 iter->stamp = list_store->stamp;
277 // user_data is just the index
278 iter->user_data = (gpointer) i;
279
280 return TRUE;
281 }
282
283 static GtkTreePath *
284 wxgtk_list_store_get_path (GtkTreeModel *tree_model,
285 GtkTreeIter *iter)
286 {
287 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), NULL);
288 g_return_val_if_fail (iter->stamp == GTK_WX_LIST_STORE (tree_model)->stamp, NULL);
289
290 GtkTreePath *retval = gtk_tree_path_new ();
291 // user_data is just the index
292 int i = (wxUIntPtr) iter->user_data;
293 gtk_tree_path_append_index (retval, i);
294 return retval;
295 }
296
297 static void
298 wxgtk_list_store_get_value (GtkTreeModel *tree_model,
299 GtkTreeIter *iter,
300 gint column,
301 GValue *value)
302 {
303 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
304 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model) );
305
306 wxDataViewListModel *model = list_store->model;
307 wxString mtype = model->GetColumnType( (unsigned int) column );
308 if (mtype == wxT("string"))
309 {
310 wxVariant variant;
311 g_value_init( value, G_TYPE_STRING );
312 model->GetValue( variant,
313 (unsigned int) column,
314 (unsigned int) iter->user_data );
315
316 // FIXME: we should support different encodings here
317 g_value_set_string( value, wxGTK_CONV_SYS(variant.GetString()) );
318 }
319 else
320 {
321 wxFAIL_MSG( _T("non-string columns not supported yet") );
322 }
323 }
324
325 static gboolean
326 wxgtk_list_store_iter_next (GtkTreeModel *tree_model,
327 GtkTreeIter *iter)
328 {
329 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
330 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
331
332 g_return_val_if_fail (list_store->stamp == iter->stamp, FALSE);
333
334 int n = (wxUIntPtr) iter->user_data;
335
336 if (n == -1)
337 return FALSE;
338
339 if (n >= (int) list_store->model->GetRowCount()-1)
340 return FALSE;
341
342 iter->user_data = (gpointer) ++n;
343
344 return TRUE;
345 }
346
347 static gboolean
348 wxgtk_list_store_iter_children (GtkTreeModel *tree_model,
349 GtkTreeIter *iter,
350 GtkTreeIter *parent)
351 {
352 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
353 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
354
355 // this is a list, nodes have no children
356 if (parent)
357 return FALSE;
358
359 iter->stamp = list_store->stamp;
360 iter->user_data = (gpointer) -1;
361
362 return TRUE;
363 }
364
365 static gboolean
366 wxgtk_list_store_iter_has_child (GtkTreeModel *tree_model,
367 GtkTreeIter *iter)
368 {
369 return FALSE;
370 }
371
372 static gint
373 wxgtk_list_store_iter_n_children (GtkTreeModel *tree_model,
374 GtkTreeIter *iter)
375 {
376 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), -1);
377 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
378
379 if (iter == NULL)
380 return (gint) list_store->model->GetRowCount();
381
382 g_return_val_if_fail (list_store->stamp == iter->stamp, -1);
383
384 return 0;
385 }
386
387 static gboolean
388 wxgtk_list_store_iter_nth_child (GtkTreeModel *tree_model,
389 GtkTreeIter *iter,
390 GtkTreeIter *parent,
391 gint n)
392 {
393 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model), FALSE);
394 GtkWxListStore *list_store = (GtkWxListStore *) tree_model;
395
396 if (parent)
397 return FALSE;
398
399 if (n < 0)
400 return FALSE;
401
402 if (n >= (gint) list_store->model->GetRowCount())
403 return FALSE;
404
405 iter->stamp = list_store->stamp;
406 iter->user_data = (gpointer) n;
407
408 return TRUE;
409 }
410
411 static gboolean
412 wxgtk_list_store_iter_parent (GtkTreeModel *tree_model,
413 GtkTreeIter *iter,
414 GtkTreeIter *child)
415 {
416 return FALSE;
417 }
418
419 //-----------------------------------------------------------------------------
420 // define new GTK+ class wxGtkRendererRenderer
421 //-----------------------------------------------------------------------------
422
423 extern "C" {
424
425 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
426 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
427 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
428 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
429 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
430 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
431
432 GType gtk_wx_cell_renderer_get_type (void);
433
434 typedef struct _GtkWxCellRenderer GtkWxCellRenderer;
435 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass;
436
437 struct _GtkWxCellRenderer
438 {
439 GtkCellRenderer parent;
440
441 /*< private >*/
442 wxDataViewCustomRenderer *cell;
443 guint32 last_click;
444 };
445
446 struct _GtkWxCellRendererClass
447 {
448 GtkCellRendererClass cell_parent_class;
449 };
450
451
452 static GtkCellRenderer *gtk_wx_cell_renderer_new (void);
453 static void gtk_wx_cell_renderer_init (
454 GtkWxCellRenderer *cell );
455 static void gtk_wx_cell_renderer_class_init(
456 GtkWxCellRendererClass *klass );
457 static void gtk_wx_cell_renderer_finalize (
458 GObject *object );
459 static void gtk_wx_cell_renderer_get_size (
460 GtkCellRenderer *cell,
461 GtkWidget *widget,
462 GdkRectangle *rectangle,
463 gint *x_offset,
464 gint *y_offset,
465 gint *width,
466 gint *height );
467 static void gtk_wx_cell_renderer_render (
468 GtkCellRenderer *cell,
469 GdkWindow *window,
470 GtkWidget *widget,
471 GdkRectangle *background_area,
472 GdkRectangle *cell_area,
473 GdkRectangle *expose_area,
474 GtkCellRendererState flags );
475 static gboolean gtk_wx_cell_renderer_activate(
476 GtkCellRenderer *cell,
477 GdkEvent *event,
478 GtkWidget *widget,
479 const gchar *path,
480 GdkRectangle *background_area,
481 GdkRectangle *cell_area,
482 GtkCellRendererState flags );
483 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
484 GtkCellRenderer *cell,
485 GdkEvent *event,
486 GtkWidget *widget,
487 const gchar *path,
488 GdkRectangle *background_area,
489 GdkRectangle *cell_area,
490 GtkCellRendererState flags );
491
492
493 static GObjectClass *cell_parent_class = NULL;
494
495 } // extern "C"
496
497 GType
498 gtk_wx_cell_renderer_get_type (void)
499 {
500 static GType cell_wx_type = 0;
501
502 if (!cell_wx_type)
503 {
504 const GTypeInfo cell_wx_info =
505 {
506 sizeof (GtkWxCellRendererClass),
507 NULL, /* base_init */
508 NULL, /* base_finalize */
509 (GClassInitFunc) gtk_wx_cell_renderer_class_init,
510 NULL, /* class_finalize */
511 NULL, /* class_data */
512 sizeof (GtkWxCellRenderer),
513 0, /* n_preallocs */
514 (GInstanceInitFunc) gtk_wx_cell_renderer_init,
515 };
516
517 cell_wx_type = g_type_register_static( GTK_TYPE_CELL_RENDERER,
518 "GtkWxCellRenderer", &cell_wx_info, (GTypeFlags)0 );
519 }
520
521 return cell_wx_type;
522 }
523
524 static void
525 gtk_wx_cell_renderer_init (GtkWxCellRenderer *cell)
526 {
527 cell->cell = NULL;
528 cell->last_click = 0;
529 }
530
531 static void
532 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass *klass)
533 {
534 GObjectClass *object_class = G_OBJECT_CLASS (klass);
535 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
536
537 cell_parent_class = (GObjectClass*) g_type_class_peek_parent (klass);
538
539 object_class->finalize = gtk_wx_cell_renderer_finalize;
540
541 cell_class->get_size = gtk_wx_cell_renderer_get_size;
542 cell_class->render = gtk_wx_cell_renderer_render;
543 cell_class->activate = gtk_wx_cell_renderer_activate;
544 cell_class->start_editing = gtk_wx_cell_renderer_start_editing;
545 }
546
547 static void
548 gtk_wx_cell_renderer_finalize (GObject *object)
549 {
550 /* must chain up */
551 (* G_OBJECT_CLASS (cell_parent_class)->finalize) (object);
552 }
553
554 GtkCellRenderer*
555 gtk_wx_cell_renderer_new (void)
556 {
557 return (GtkCellRenderer*) g_object_new (GTK_TYPE_WX_CELL_RENDERER, NULL);
558 }
559
560
561
562 static GtkCellEditable *gtk_wx_cell_renderer_start_editing(
563 GtkCellRenderer *renderer,
564 GdkEvent *event,
565 GtkWidget *widget,
566 const gchar *path,
567 GdkRectangle *background_area,
568 GdkRectangle *cell_area,
569 GtkCellRendererState flags )
570 {
571 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
572 wxDataViewCustomRenderer *cell = wxrenderer->cell;
573 if (!cell->HasEditorCtrl())
574 return NULL;
575
576 GdkRectangle rect;
577 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
578 &rect.x,
579 &rect.y,
580 &rect.width,
581 &rect.height);
582
583 rect.x += cell_area->x;
584 rect.y += cell_area->y;
585 // rect.width -= renderer->xpad * 2;
586 // rect.height -= renderer->ypad * 2;
587
588 // wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
589 wxRect renderrect( cell_area->x, cell_area->y, cell_area->width, cell_area->height );
590
591 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
592
593 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
594 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (treepath)[0];
595 gtk_tree_path_free( treepath );
596
597 cell->StartEditing( model_row, renderrect );
598
599 return NULL;
600 }
601
602 static void
603 gtk_wx_cell_renderer_get_size (GtkCellRenderer *renderer,
604 GtkWidget *widget,
605 GdkRectangle *cell_area,
606 gint *x_offset,
607 gint *y_offset,
608 gint *width,
609 gint *height)
610 {
611 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
612 wxDataViewCustomRenderer *cell = wxrenderer->cell;
613
614 wxSize size = cell->GetSize();
615
616 gint calc_width = (gint) renderer->xpad * 2 + size.x;
617 gint calc_height = (gint) renderer->ypad * 2 + size.y;
618
619 if (x_offset)
620 *x_offset = 0;
621 if (y_offset)
622 *y_offset = 0;
623
624 if (cell_area && size.x > 0 && size.y > 0)
625 {
626 if (x_offset)
627 {
628 *x_offset = (gint)((renderer->xalign *
629 (cell_area->width - calc_width - 2 * renderer->xpad)));
630 *x_offset = MAX (*x_offset, 0) + renderer->xpad;
631 }
632 if (y_offset)
633 {
634 *y_offset = (gint)((renderer->yalign *
635 (cell_area->height - calc_height - 2 * renderer->ypad)));
636 *y_offset = MAX (*y_offset, 0) + renderer->ypad;
637 }
638 }
639
640 if (width)
641 *width = calc_width;
642
643 if (height)
644 *height = calc_height;
645 }
646
647 static void
648 gtk_wx_cell_renderer_render (GtkCellRenderer *renderer,
649 GdkWindow *window,
650 GtkWidget *widget,
651 GdkRectangle *background_area,
652 GdkRectangle *cell_area,
653 GdkRectangle *expose_area,
654 GtkCellRendererState flags)
655
656 {
657 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
658 wxDataViewCustomRenderer *cell = wxrenderer->cell;
659
660 GdkRectangle rect;
661 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
662 &rect.x,
663 &rect.y,
664 &rect.width,
665 &rect.height);
666
667 rect.x += cell_area->x;
668 rect.y += cell_area->y;
669 rect.width -= renderer->xpad * 2;
670 rect.height -= renderer->ypad * 2;
671
672 GdkRectangle dummy;
673 if (gdk_rectangle_intersect (expose_area, &rect, &dummy))
674 {
675 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
676 wxWindowDC* dc = (wxWindowDC*) cell->GetDC();
677 if (dc->m_window == NULL)
678 {
679 dc->m_window = window;
680 dc->SetUpDC();
681 }
682
683 int state = 0;
684 if (flags & GTK_CELL_RENDERER_SELECTED)
685 state |= wxDATAVIEW_CELL_SELECTED;
686 if (flags & GTK_CELL_RENDERER_PRELIT)
687 state |= wxDATAVIEW_CELL_PRELIT;
688 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
689 state |= wxDATAVIEW_CELL_INSENSITIVE;
690 if (flags & GTK_CELL_RENDERER_INSENSITIVE)
691 state |= wxDATAVIEW_CELL_INSENSITIVE;
692 if (flags & GTK_CELL_RENDERER_FOCUSED)
693 state |= wxDATAVIEW_CELL_FOCUSED;
694 cell->Render( renderrect, dc, state );
695 }
696 }
697
698 static gboolean
699 gtk_wx_cell_renderer_activate(
700 GtkCellRenderer *renderer,
701 GdkEvent *event,
702 GtkWidget *widget,
703 const gchar *path,
704 GdkRectangle *background_area,
705 GdkRectangle *cell_area,
706 GtkCellRendererState flags )
707 {
708 GtkWxCellRenderer *wxrenderer = (GtkWxCellRenderer *) renderer;
709 wxDataViewCustomRenderer *cell = wxrenderer->cell;
710
711 GdkRectangle rect;
712 gtk_wx_cell_renderer_get_size (renderer, widget, cell_area,
713 &rect.x,
714 &rect.y,
715 &rect.width,
716 &rect.height);
717
718 rect.x += cell_area->x;
719 rect.y += cell_area->y;
720 rect.width -= renderer->xpad * 2;
721 rect.height -= renderer->ypad * 2;
722
723 wxRect renderrect( rect.x, rect.y, rect.width, rect.height );
724
725 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
726
727 GtkTreePath *treepath = gtk_tree_path_new_from_string( path );
728 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (treepath)[0];
729 gtk_tree_path_free( treepath );
730
731 unsigned int model_col = cell->GetOwner()->GetModelColumn();
732
733 if (!event)
734 {
735 bool ret = false;
736
737 // activated by <ENTER>
738 if (cell->Activate( renderrect, model, model_col, model_row ))
739 ret = true;
740
741 return ret;
742 }
743 else if (event->type == GDK_BUTTON_PRESS)
744 {
745 GdkEventButton *button_event = (GdkEventButton*) event;
746 wxPoint pt( ((int) button_event->x) - renderrect.x,
747 ((int) button_event->y) - renderrect.y );
748
749 bool ret = false;
750 if (button_event->button == 1)
751 {
752 if (cell->LeftClick( pt, renderrect, model, model_col, model_row ))
753 ret = true;
754 // TODO: query system double-click time
755 if (button_event->time - wxrenderer->last_click < 400)
756 if (cell->Activate( renderrect, model, model_col, model_row ))
757 ret = true;
758 }
759 if (button_event->button == 3)
760 {
761 if (cell->RightClick( pt, renderrect, model, model_col, model_row ))
762 ret = true;
763 }
764
765 wxrenderer->last_click = button_event->time;
766
767 return ret;
768 }
769
770 return false;
771 }
772
773 // ---------------------------------------------------------
774 // wxGtkDataViewListModelNotifier
775 // ---------------------------------------------------------
776
777 class wxGtkDataViewListModelNotifier: public wxDataViewListModelNotifier
778 {
779 public:
780 wxGtkDataViewListModelNotifier( GtkWxListStore* gtk_store, wxDataViewListModel *wx_model );
781 ~wxGtkDataViewListModelNotifier();
782
783 virtual bool RowAppended();
784 virtual bool RowPrepended();
785 virtual bool RowInserted( unsigned int before );
786 virtual bool RowDeleted( unsigned int row );
787 virtual bool RowChanged( unsigned int row );
788 virtual bool ValueChanged( unsigned int col, unsigned int row );
789 virtual bool RowsReordered( unsigned int *new_order );
790 virtual bool Cleared();
791
792 GtkWxListStore *m_gtk_store;
793 wxDataViewListModel *m_wx_model;
794 };
795
796 // ---------------------------------------------------------
797 // wxGtkDataViewListModelNotifier
798 // ---------------------------------------------------------
799
800 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
801 GtkWxListStore* gtk_store, wxDataViewListModel *wx_model )
802 {
803 m_gtk_store = gtk_store;
804 m_wx_model = wx_model;
805 }
806
807 wxGtkDataViewListModelNotifier::~wxGtkDataViewListModelNotifier()
808 {
809 m_wx_model = NULL;
810 m_gtk_store = NULL;
811 }
812
813 bool wxGtkDataViewListModelNotifier::RowAppended()
814 {
815 unsigned int pos = m_wx_model->GetRowCount()-1;
816
817 GtkTreeIter iter;
818 iter.stamp = m_gtk_store->stamp;
819 iter.user_data = (gpointer) pos;
820
821 GtkTreePath *path = gtk_tree_path_new ();
822 gtk_tree_path_append_index (path, (gint) pos);
823 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
824 gtk_tree_path_free (path);
825
826 return true;
827 }
828
829 bool wxGtkDataViewListModelNotifier::RowPrepended()
830 {
831 GtkTreeIter iter;
832 iter.stamp = m_gtk_store->stamp;
833 iter.user_data = (gpointer) 0;
834
835 GtkTreePath *path = gtk_tree_path_new ();
836 gtk_tree_path_append_index (path, (gint) 0);
837 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
838 gtk_tree_path_free (path);
839
840 return true;
841 }
842
843 bool wxGtkDataViewListModelNotifier::RowInserted( unsigned int before )
844 {
845 GtkTreeIter iter;
846 iter.stamp = m_gtk_store->stamp;
847 iter.user_data = (gpointer) before;
848
849 GtkTreePath *path = gtk_tree_path_new ();
850 gtk_tree_path_append_index (path, (gint) before);
851 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store), path, &iter);
852 gtk_tree_path_free (path);
853
854 return true;
855 }
856
857 bool wxGtkDataViewListModelNotifier::RowDeleted( unsigned int row )
858 {
859 GtkTreePath *path = gtk_tree_path_new ();
860 gtk_tree_path_append_index (path, (gint) row);
861 gtk_tree_model_row_deleted (GTK_TREE_MODEL (m_gtk_store), path);
862 gtk_tree_path_free (path);
863
864 return true;
865 }
866
867 bool wxGtkDataViewListModelNotifier::RowChanged( unsigned int row )
868 {
869 GtkTreeIter iter;
870 iter.stamp = m_gtk_store->stamp;
871 iter.user_data = (gpointer) row;
872 GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store), &iter);
873 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store), path, &iter);
874 gtk_tree_path_free (path);
875
876 return true;
877 }
878
879 bool wxGtkDataViewListModelNotifier::ValueChanged( unsigned int model_col, unsigned int model_row )
880 {
881 // This adds GTK+'s missing MVC logic for ValueChanged
882 wxObjectList::compatibility_iterator
883 node = GetOwner()->m_viewingColumns.GetFirst();
884 while (node)
885 {
886 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
887 if (viewing_column->m_modelColumn == model_col)
888 {
889 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
890 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(viewing_column->m_viewColumn->GetGtkHandle());
891
892 // Get cell area
893 GtkTreePath *path = gtk_tree_path_new();
894 gtk_tree_path_append_index( path, model_row );
895 GdkRectangle cell_area;
896 gtk_tree_view_get_cell_area( widget, path, column, &cell_area );
897 gtk_tree_path_free( path );
898
899 GtkAdjustment* hadjust = gtk_tree_view_get_hadjustment( widget );
900 double d = gtk_adjustment_get_value( hadjust );
901 int xdiff = (int) d;
902
903 int ydiff = column->button->allocation.height;
904 // Redraw
905 gtk_widget_queue_draw_area( GTK_WIDGET(widget),
906 cell_area.x - xdiff, ydiff + cell_area.y, cell_area.width, cell_area.height );
907 }
908
909 node = node->GetNext();
910 }
911
912 return true;
913 }
914
915 bool wxGtkDataViewListModelNotifier::RowsReordered( unsigned int *new_order )
916 {
917 // Assume sizeof(unsigned int)= == sizeof(gint)
918
919 GtkTreePath *path = gtk_tree_path_new ();
920 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store), path, NULL, (gint*)new_order);
921 gtk_tree_path_free (path);
922
923 // This adds GTK+'s missing MVC logic for RowsReordered
924 wxObjectList::compatibility_iterator
925 node = GetOwner()->m_viewingColumns.GetFirst();
926 while (node)
927 {
928 wxDataViewViewingColumn* viewing_column = (wxDataViewViewingColumn*) node->GetData();
929 GtkTreeView *widget = GTK_TREE_VIEW(viewing_column->m_viewColumn->GetOwner()->m_treeview);
930 // Doesn't work yet...
931 gtk_widget_queue_draw( GTK_WIDGET(widget) );
932
933 node = node->GetNext();
934 }
935
936 return true;
937 }
938
939 bool wxGtkDataViewListModelNotifier::Cleared()
940 {
941 return false;
942 }
943
944 // ---------------------------------------------------------
945 // wxDataViewRenderer
946 // ---------------------------------------------------------
947
948 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer, wxDataViewRendererBase)
949
950 wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, wxDataViewCellMode mode,
951 int align ) :
952 wxDataViewRendererBase( varianttype, mode, align )
953 {
954 m_renderer = NULL;
955
956 // NOTE: SetMode() and SetAlignment() needs to be called in the renderer's ctor,
957 // after the m_renderer pointer has been initialized
958 }
959
960 void wxDataViewRenderer::SetMode( wxDataViewCellMode mode )
961 {
962 GtkCellRendererMode gtkMode;
963 switch (mode)
964 {
965 case wxDATAVIEW_CELL_INERT:
966 gtkMode = GTK_CELL_RENDERER_MODE_INERT;
967 break;
968 case wxDATAVIEW_CELL_ACTIVATABLE:
969 gtkMode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
970 break;
971 case wxDATAVIEW_CELL_EDITABLE:
972 gtkMode = GTK_CELL_RENDERER_MODE_EDITABLE;
973 break;
974 }
975
976 GValue gvalue = { 0, };
977 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
978 g_value_set_enum( &gvalue, gtkMode );
979 g_object_set_property( G_OBJECT(m_renderer), "mode", &gvalue );
980 g_value_unset( &gvalue );
981 }
982
983 wxDataViewCellMode wxDataViewRenderer::GetMode() const
984 {
985 wxDataViewCellMode ret;
986
987 GValue gvalue;
988 g_object_get( G_OBJECT(m_renderer), "mode", &gvalue, NULL);
989
990 switch (g_value_get_enum(&gvalue))
991 {
992 case GTK_CELL_RENDERER_MODE_INERT:
993 ret = wxDATAVIEW_CELL_INERT;
994 break;
995 case GTK_CELL_RENDERER_MODE_ACTIVATABLE:
996 ret = wxDATAVIEW_CELL_ACTIVATABLE;
997 break;
998 case GTK_CELL_RENDERER_MODE_EDITABLE:
999 ret = wxDATAVIEW_CELL_EDITABLE;
1000 break;
1001 }
1002
1003 g_value_unset( &gvalue );
1004
1005 return ret;
1006 }
1007
1008 void wxDataViewRenderer::SetAlignment( int align )
1009 {
1010 // horizontal alignment:
1011
1012 gfloat xalign = 0.0;
1013 if (align & wxALIGN_RIGHT)
1014 xalign = 1.0;
1015 else if (align & wxALIGN_CENTER_HORIZONTAL)
1016 xalign = 0.5;
1017
1018 GValue gvalue = { 0, };
1019 g_value_init( &gvalue, G_TYPE_FLOAT );
1020 g_value_set_float( &gvalue, xalign );
1021 g_object_set_property( G_OBJECT(m_renderer), "xalign", &gvalue );
1022 g_value_unset( &gvalue );
1023
1024 // vertical alignment:
1025
1026 gfloat yalign = 0.0;
1027 if (align & wxALIGN_BOTTOM)
1028 yalign = 1.0;
1029 else if (align & wxALIGN_CENTER_VERTICAL)
1030 yalign = 0.5;
1031
1032 GValue gvalue2 = { 0, };
1033 g_value_init( &gvalue2, G_TYPE_FLOAT );
1034 g_value_set_float( &gvalue2, yalign );
1035 g_object_set_property( G_OBJECT(m_renderer), "yalign", &gvalue2 );
1036 g_value_unset( &gvalue2 );
1037 }
1038
1039 int wxDataViewRenderer::GetAlignment() const
1040 {
1041 int ret = 0;
1042 GValue gvalue;
1043
1044 // horizontal alignment:
1045
1046 g_object_get( G_OBJECT(m_renderer), "xalign", &gvalue, NULL );
1047 float xalign = g_value_get_float( &gvalue );
1048 if (xalign < 0.5)
1049 ret |= wxALIGN_LEFT;
1050 else if (xalign == 0.5)
1051 ret |= wxALIGN_CENTER_HORIZONTAL;
1052 else
1053 ret |= wxALIGN_RIGHT;
1054 g_value_unset( &gvalue );
1055
1056
1057 // vertical alignment:
1058
1059 g_object_get( G_OBJECT(m_renderer), "yalign", &gvalue, NULL );
1060 float yalign = g_value_get_float( &gvalue );
1061 if (yalign < 0.5)
1062 ret |= wxALIGN_TOP;
1063 else if (yalign == 0.5)
1064 ret |= wxALIGN_CENTER_VERTICAL;
1065 else
1066 ret |= wxALIGN_BOTTOM;
1067 g_value_unset( &gvalue );
1068
1069 return ret;
1070 }
1071
1072
1073
1074 // ---------------------------------------------------------
1075 // wxDataViewTextRenderer
1076 // ---------------------------------------------------------
1077
1078 extern "C" {
1079 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1080 gchar *arg1, gchar *arg2, gpointer user_data );
1081 }
1082
1083 static void wxGtkTextRendererEditedCallback( GtkCellRendererText *renderer,
1084 gchar *arg1, gchar *arg2, gpointer user_data )
1085 {
1086 wxDataViewTextRenderer *cell = (wxDataViewTextRenderer*) user_data;
1087
1088 wxString tmp = wxGTK_CONV_BACK_FONT(arg2, cell->GetOwner()->GetOwner()->GetFont());
1089 wxVariant value = tmp;
1090 if (!cell->Validate( value ))
1091 return;
1092
1093 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
1094
1095 GtkTreePath *path = gtk_tree_path_new_from_string( arg1 );
1096 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (path)[0];
1097 gtk_tree_path_free( path );
1098
1099 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1100
1101 model->SetValue( value, model_col, model_row );
1102 model->ValueChanged( model_col, model_row );
1103 }
1104
1105 IMPLEMENT_CLASS(wxDataViewTextRenderer, wxDataViewRenderer)
1106
1107 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1108 int align ) :
1109 wxDataViewRenderer( varianttype, mode, align )
1110 {
1111 m_renderer = (GtkWidget*) gtk_cell_renderer_text_new();
1112
1113 if (mode & wxDATAVIEW_CELL_EDITABLE)
1114 {
1115 GValue gvalue = { 0, };
1116 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1117 g_value_set_boolean( &gvalue, true );
1118 g_object_set_property( G_OBJECT(m_renderer), "editable", &gvalue );
1119 g_value_unset( &gvalue );
1120
1121 g_signal_connect_after( m_renderer, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback), this );
1122 }
1123
1124 SetMode(mode);
1125 SetAlignment(align);
1126 }
1127
1128 bool wxDataViewTextRenderer::SetValue( const wxVariant &value )
1129 {
1130 wxString tmp = value;
1131
1132 GValue gvalue = { 0, };
1133 g_value_init( &gvalue, G_TYPE_STRING );
1134 g_value_set_string( &gvalue, wxGTK_CONV_FONT( tmp, GetOwner()->GetOwner()->GetFont() ) );
1135 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1136 g_value_unset( &gvalue );
1137
1138 return true;
1139 }
1140
1141 bool wxDataViewTextRenderer::GetValue( wxVariant &value ) const
1142 {
1143 GValue gvalue = { 0, };
1144 g_value_init( &gvalue, G_TYPE_STRING );
1145 g_object_get_property( G_OBJECT(m_renderer), "text", &gvalue );
1146 wxString tmp = wxGTK_CONV_BACK_FONT( g_value_get_string( &gvalue ),
1147 wx_const_cast(wxDataViewTextRenderer*, this)->GetOwner()->GetOwner()->GetFont() );
1148 g_value_unset( &gvalue );
1149
1150 value = tmp;
1151
1152 return true;
1153 }
1154
1155 void wxDataViewTextRenderer::SetAlignment( int align )
1156 {
1157 wxDataViewRenderer::SetAlignment(align);
1158
1159 if (gtk_check_version(2,10,0))
1160 return;
1161
1162 // horizontal alignment:
1163 PangoAlignment pangoAlign = PANGO_ALIGN_LEFT;
1164 if (align & wxALIGN_RIGHT)
1165 pangoAlign = PANGO_ALIGN_RIGHT;
1166 else if (align & wxALIGN_CENTER_HORIZONTAL)
1167 pangoAlign = PANGO_ALIGN_CENTER;
1168
1169 GValue gvalue = { 0, };
1170 g_value_init( &gvalue, gtk_cell_renderer_mode_get_type() );
1171 g_value_set_enum( &gvalue, pangoAlign );
1172 g_object_set_property( G_OBJECT(m_renderer), "alignment", &gvalue );
1173 g_value_unset( &gvalue );
1174 }
1175
1176 // ---------------------------------------------------------
1177 // wxDataViewBitmapRenderer
1178 // ---------------------------------------------------------
1179
1180 IMPLEMENT_CLASS(wxDataViewBitmapRenderer, wxDataViewRenderer)
1181
1182 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString &varianttype, wxDataViewCellMode mode,
1183 int align ) :
1184 wxDataViewRenderer( varianttype, mode, align )
1185 {
1186 m_renderer = (GtkWidget*) gtk_cell_renderer_pixbuf_new();
1187
1188 SetMode(mode);
1189 SetAlignment(align);
1190 }
1191
1192 bool wxDataViewBitmapRenderer::SetValue( const wxVariant &value )
1193 {
1194 if (value.GetType() == wxT("wxBitmap"))
1195 {
1196 wxBitmap bitmap;
1197 bitmap << value;
1198
1199 // This may create a Pixbuf representation in the
1200 // wxBitmap object (and it will stay there)
1201 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1202
1203 GValue gvalue = { 0, };
1204 g_value_init( &gvalue, G_TYPE_OBJECT );
1205 g_value_set_object( &gvalue, pixbuf );
1206 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1207 g_value_unset( &gvalue );
1208
1209 return true;
1210 }
1211
1212 if (value.GetType() == wxT("wxIcon"))
1213 {
1214 wxIcon bitmap;
1215 bitmap << value;
1216
1217 // This may create a Pixbuf representation in the
1218 // wxBitmap object (and it will stay there)
1219 GdkPixbuf *pixbuf = bitmap.GetPixbuf();
1220
1221 GValue gvalue = { 0, };
1222 g_value_init( &gvalue, G_TYPE_OBJECT );
1223 g_value_set_object( &gvalue, pixbuf );
1224 g_object_set_property( G_OBJECT(m_renderer), "pixbuf", &gvalue );
1225 g_value_unset( &gvalue );
1226
1227 return true;
1228 }
1229
1230 return false;
1231 }
1232
1233 bool wxDataViewBitmapRenderer::GetValue( wxVariant &value ) const
1234 {
1235 return false;
1236 }
1237
1238 // ---------------------------------------------------------
1239 // wxDataViewToggleRenderer
1240 // ---------------------------------------------------------
1241
1242 extern "C" {
1243 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1244 gchar *path, gpointer user_data );
1245 }
1246
1247 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer,
1248 gchar *path, gpointer user_data )
1249 {
1250 wxDataViewToggleRenderer *cell = (wxDataViewToggleRenderer*) user_data;
1251
1252 // get old value
1253 GValue gvalue = { 0, };
1254 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1255 g_object_get_property( G_OBJECT(renderer), "active", &gvalue );
1256 bool tmp = g_value_get_boolean( &gvalue );
1257 g_value_unset( &gvalue );
1258 // invert it
1259 tmp = !tmp;
1260
1261 wxVariant value = tmp;
1262 if (!cell->Validate( value ))
1263 return;
1264
1265 wxDataViewListModel *model = cell->GetOwner()->GetOwner()->GetModel();
1266
1267 GtkTreePath *gtk_path = gtk_tree_path_new_from_string( path );
1268 unsigned int model_row = (unsigned int)gtk_tree_path_get_indices (gtk_path)[0];
1269 gtk_tree_path_free( gtk_path );
1270
1271 unsigned int model_col = cell->GetOwner()->GetModelColumn();
1272
1273 model->SetValue( value, model_col, model_row );
1274 model->ValueChanged( model_col, model_row );
1275 }
1276
1277 IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer)
1278
1279 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString &varianttype,
1280 wxDataViewCellMode mode, int align ) :
1281 wxDataViewRenderer( varianttype, mode, align )
1282 {
1283 m_renderer = (GtkWidget*) gtk_cell_renderer_toggle_new();
1284
1285 if (mode & wxDATAVIEW_CELL_ACTIVATABLE)
1286 {
1287 g_signal_connect_after( m_renderer, "toggled",
1288 G_CALLBACK(wxGtkToggleRendererToggledCallback), this );
1289 }
1290 else
1291 {
1292 GValue gvalue = { 0, };
1293 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1294 g_value_set_boolean( &gvalue, false );
1295 g_object_set_property( G_OBJECT(m_renderer), "activatable", &gvalue );
1296 g_value_unset( &gvalue );
1297 }
1298
1299 SetMode(mode);
1300 SetAlignment(align);
1301 }
1302
1303 bool wxDataViewToggleRenderer::SetValue( const wxVariant &value )
1304 {
1305 bool tmp = value;
1306
1307 GValue gvalue = { 0, };
1308 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1309 g_value_set_boolean( &gvalue, tmp );
1310 g_object_set_property( G_OBJECT(m_renderer), "active", &gvalue );
1311 g_value_unset( &gvalue );
1312
1313 return true;
1314 }
1315
1316 bool wxDataViewToggleRenderer::GetValue( wxVariant &value ) const
1317 {
1318 GValue gvalue = { 0, };
1319 g_value_init( &gvalue, G_TYPE_BOOLEAN );
1320 g_object_get_property( G_OBJECT(m_renderer), "active", &gvalue );
1321 bool tmp = g_value_get_boolean( &gvalue );
1322 g_value_unset( &gvalue );
1323
1324 value = tmp;
1325
1326 return true;
1327 }
1328
1329 // ---------------------------------------------------------
1330 // wxDataViewCustomRenderer
1331 // ---------------------------------------------------------
1332
1333 class wxDataViewCtrlDC: public wxWindowDC
1334 {
1335 public:
1336 wxDataViewCtrlDC( wxDataViewCtrl *window )
1337 {
1338 GtkWidget *widget = window->m_treeview;
1339 // Set later
1340 m_window = NULL;
1341
1342 m_context = window->GtkGetPangoDefaultContext();
1343 m_layout = pango_layout_new( m_context );
1344 m_fontdesc = pango_font_description_copy( widget->style->font_desc );
1345
1346 m_cmap = gtk_widget_get_colormap( widget ? widget : window->m_widget );
1347
1348 // Set m_window later
1349 // SetUpDC();
1350 // m_owner = window;
1351 }
1352 };
1353
1354 // ---------------------------------------------------------
1355 // wxDataViewCustomRenderer
1356 // ---------------------------------------------------------
1357
1358 IMPLEMENT_CLASS(wxDataViewCustomRenderer, wxDataViewRenderer)
1359
1360 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype,
1361 wxDataViewCellMode mode, int align,
1362 bool no_init ) :
1363 wxDataViewRenderer( varianttype, mode, align )
1364 {
1365 m_dc = NULL;
1366
1367 if (no_init)
1368 m_renderer = NULL;
1369 else
1370 Init(mode, align);
1371 }
1372
1373 bool wxDataViewCustomRenderer::Init(wxDataViewCellMode mode, int align)
1374 {
1375 GtkWxCellRenderer *renderer = (GtkWxCellRenderer *) gtk_wx_cell_renderer_new();
1376 renderer->cell = this;
1377
1378 m_renderer = (GtkWidget*) renderer;
1379
1380 SetMode(mode);
1381 SetAlignment(align);
1382
1383 return true;
1384 }
1385
1386 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
1387 {
1388 if (m_dc)
1389 delete m_dc;
1390 }
1391
1392 wxDC *wxDataViewCustomRenderer::GetDC()
1393 {
1394 if (m_dc == NULL)
1395 {
1396 if (GetOwner() == NULL)
1397 return NULL;
1398 if (GetOwner()->GetOwner() == NULL)
1399 return NULL;
1400 m_dc = new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1401 }
1402
1403 return m_dc;
1404 }
1405
1406 // ---------------------------------------------------------
1407 // wxDataViewProgressRenderer
1408 // ---------------------------------------------------------
1409
1410 IMPLEMENT_CLASS(wxDataViewProgressRenderer, wxDataViewCustomRenderer)
1411
1412 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString &label,
1413 const wxString &varianttype, wxDataViewCellMode mode, int align ) :
1414 wxDataViewCustomRenderer( varianttype, mode, align, true )
1415 {
1416 m_label = label;
1417 m_value = 0;
1418
1419 #ifdef __WXGTK26__
1420 if (!gtk_check_version(2,6,0))
1421 {
1422 m_renderer = (GtkWidget*) gtk_cell_renderer_progress_new();
1423
1424 GValue gvalue = { 0, };
1425 g_value_init( &gvalue, G_TYPE_STRING );
1426
1427 // FIXME: font encoding support
1428 g_value_set_string( &gvalue, wxGTK_CONV_SYS(m_label) );
1429 g_object_set_property( G_OBJECT(m_renderer), "text", &gvalue );
1430 g_value_unset( &gvalue );
1431
1432 SetMode(mode);
1433 SetAlignment(align);
1434 }
1435 else
1436 #endif
1437 {
1438 // Use custom cell code
1439 wxDataViewCustomRenderer::Init(mode, align);
1440 }
1441 }
1442
1443 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
1444 {
1445 }
1446
1447 bool wxDataViewProgressRenderer::SetValue( const wxVariant &value )
1448 {
1449 #ifdef __WXGTK26__
1450 if (!gtk_check_version(2,6,0))
1451 {
1452 gint tmp = (long) value;
1453 GValue gvalue = { 0, };
1454 g_value_init( &gvalue, G_TYPE_INT );
1455 g_value_set_int( &gvalue, tmp );
1456 g_object_set_property( G_OBJECT(m_renderer), "value", &gvalue );
1457 g_value_unset( &gvalue );
1458 }
1459 else
1460 #endif
1461 {
1462 m_value = (long) value;
1463
1464 if (m_value < 0) m_value = 0;
1465 if (m_value > 100) m_value = 100;
1466 }
1467
1468 return true;
1469 }
1470
1471 bool wxDataViewProgressRenderer::GetValue( wxVariant &value ) const
1472 {
1473 return false;
1474 }
1475
1476 bool wxDataViewProgressRenderer::Render( wxRect cell, wxDC *dc, int state )
1477 {
1478 double pct = (double)m_value / 100.0;
1479 wxRect bar = cell;
1480 bar.width = (int)(cell.width * pct);
1481 dc->SetPen( *wxTRANSPARENT_PEN );
1482 dc->SetBrush( *wxBLUE_BRUSH );
1483 dc->DrawRectangle( bar );
1484
1485 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1486 dc->SetPen( *wxBLACK_PEN );
1487 dc->DrawRectangle( cell );
1488
1489 return true;
1490 }
1491
1492 wxSize wxDataViewProgressRenderer::GetSize() const
1493 {
1494 return wxSize(40,12);
1495 }
1496
1497 // ---------------------------------------------------------
1498 // wxDataViewDateRenderer
1499 // ---------------------------------------------------------
1500
1501 class wxDataViewDateRendererPopupTransient: public wxPopupTransientWindow
1502 {
1503 public:
1504 wxDataViewDateRendererPopupTransient( wxWindow* parent, wxDateTime *value,
1505 wxDataViewListModel *model, unsigned int col, unsigned int row ) :
1506 wxPopupTransientWindow( parent, wxBORDER_SIMPLE )
1507 {
1508 m_model = model;
1509 m_col = col;
1510 m_row = row;
1511 m_cal = new wxCalendarCtrl( this, -1, *value );
1512 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
1513 sizer->Add( m_cal, 1, wxGROW );
1514 SetSizer( sizer );
1515 sizer->Fit( this );
1516 }
1517
1518 virtual void OnDismiss()
1519 {
1520 }
1521
1522 void OnCalendar( wxCalendarEvent &event );
1523
1524 wxCalendarCtrl *m_cal;
1525 wxDataViewListModel *m_model;
1526 unsigned int m_col;
1527 unsigned int m_row;
1528
1529 private:
1530 DECLARE_EVENT_TABLE()
1531 };
1532
1533 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient,wxPopupTransientWindow)
1534 EVT_CALENDAR( -1, wxDataViewDateRendererPopupTransient::OnCalendar )
1535 END_EVENT_TABLE()
1536
1537 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event )
1538 {
1539 wxDateTime date = event.GetDate();
1540 wxVariant value = date;
1541 m_model->SetValue( value, m_col, m_row );
1542 m_model->ValueChanged( m_col, m_row );
1543 DismissAndNotify();
1544 }
1545
1546 IMPLEMENT_CLASS(wxDataViewDateRenderer, wxDataViewCustomRenderer)
1547
1548 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString &varianttype,
1549 wxDataViewCellMode mode, int align ) :
1550 wxDataViewCustomRenderer( varianttype, mode, align )
1551 {
1552 SetMode(mode);
1553 SetAlignment(align);
1554 }
1555
1556 bool wxDataViewDateRenderer::SetValue( const wxVariant &value )
1557 {
1558 m_date = value.GetDateTime();
1559
1560 return true;
1561 }
1562
1563 bool wxDataViewDateRenderer::GetValue( wxVariant &value ) const
1564 {
1565 return false;
1566 }
1567
1568 bool wxDataViewDateRenderer::Render( wxRect cell, wxDC *dc, int state )
1569 {
1570 dc->SetFont( GetOwner()->GetOwner()->GetFont() );
1571 wxString tmp = m_date.FormatDate();
1572 dc->DrawText( tmp, cell.x, cell.y );
1573
1574 return true;
1575 }
1576
1577 wxSize wxDataViewDateRenderer::GetSize() const
1578 {
1579 wxString tmp = m_date.FormatDate();
1580 wxCoord x,y,d;
1581 GetView()->GetTextExtent( tmp, &x, &y, &d );
1582 return wxSize(x,y+d);
1583 }
1584
1585 bool wxDataViewDateRenderer::Activate( wxRect cell, wxDataViewListModel *model,
1586 unsigned int col, unsigned int row )
1587 {
1588 wxVariant variant;
1589 model->GetValue( variant, col, row );
1590 wxDateTime value = variant.GetDateTime();
1591
1592 wxDataViewDateRendererPopupTransient *popup = new wxDataViewDateRendererPopupTransient(
1593 GetOwner()->GetOwner()->GetParent(), &value, model, col, row );
1594 wxPoint pos = wxGetMousePosition();
1595 popup->Move( pos );
1596 popup->Layout();
1597 popup->Popup( popup->m_cal );
1598
1599 return true;
1600 }
1601
1602 // ---------------------------------------------------------
1603 // wxDataViewColumn
1604 // ---------------------------------------------------------
1605
1606
1607 static gboolean
1608 gtk_dataview_header_button_press_callback( GtkWidget *widget,
1609 GdkEventButton *gdk_event,
1610 wxDataViewColumn *column )
1611 {
1612 if (gdk_event->type != GDK_BUTTON_PRESS)
1613 return TRUE;
1614
1615 if (gdk_event->button == 1)
1616 {
1617 wxDataViewCtrl *dv = column->GetOwner();
1618 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, dv->GetId() );
1619 event.SetDataViewColumn( column );
1620 event.SetModel( dv->GetModel() );
1621 dv->GetEventHandler()->ProcessEvent( event );
1622 }
1623
1624 return TRUE;
1625 }
1626
1627 extern "C" {
1628 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1629 GtkCellRenderer *cell,
1630 GtkTreeModel *model,
1631 GtkTreeIter *iter,
1632 gpointer data );
1633 }
1634
1635
1636 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column,
1637 GtkCellRenderer *renderer,
1638 GtkTreeModel *model,
1639 GtkTreeIter *iter,
1640 gpointer data )
1641 {
1642 g_return_if_fail (GTK_IS_WX_LIST_STORE (model));
1643 GtkWxListStore *list_store = (GtkWxListStore *) model;
1644
1645 wxDataViewRenderer *cell = (wxDataViewRenderer*) data;
1646
1647 unsigned int model_row = (unsigned int) iter->user_data;
1648
1649 wxVariant value;
1650 list_store->model->GetValue( value, cell->GetOwner()->GetModelColumn(), model_row );
1651
1652 if (value.GetType() != cell->GetVariantType())
1653 wxLogError( wxT("Wrong type, required: %s but: %s"),
1654 value.GetType().c_str(),
1655 cell->GetVariantType().c_str() );
1656
1657 cell->SetValue( value );
1658 }
1659
1660 IMPLEMENT_CLASS(wxDataViewColumn, wxDataViewColumnBase)
1661
1662 wxDataViewColumn::wxDataViewColumn( const wxString &title, wxDataViewRenderer *cell,
1663 unsigned int model_column, int width,
1664 wxAlignment align, int flags ) :
1665 wxDataViewColumnBase( title, cell, model_column, width, align, flags )
1666 {
1667 Init( align, flags, width );
1668
1669 gtk_tree_view_column_set_clickable( GTK_TREE_VIEW_COLUMN(m_column), TRUE );
1670 SetTitle( title );
1671 }
1672
1673 wxDataViewColumn::wxDataViewColumn( const wxBitmap &bitmap, wxDataViewRenderer *cell,
1674 unsigned int model_column, int width,
1675 wxAlignment align, int flags ) :
1676 wxDataViewColumnBase( bitmap, cell, model_column, width, align, flags )
1677 {
1678 Init( align, flags, width );
1679
1680 SetBitmap( bitmap );
1681 }
1682
1683 void wxDataViewColumn::Init(wxAlignment align, int flags, int width)
1684 {
1685 m_isConnected = false;
1686
1687 GtkCellRenderer *renderer = (GtkCellRenderer *) GetRenderer()->GetGtkHandle();
1688 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1689 m_column = (GtkWidget*) column;
1690
1691 SetFlags( flags );
1692 SetAlignment( align );
1693
1694 // NOTE: we prefer not to call SetMinWidth(wxDVC_DEFAULT_MINWIDTH);
1695 // as GTK+ is smart and unless explicitely told, will set the minimal
1696 // width to the title's lenght, which is a better default
1697
1698 // the GTK_TREE_VIEW_COLUMN_FIXED is required by the "fixed height" mode
1699 // that we use for the wxDataViewCtrl
1700 gtk_tree_view_column_set_fixed_width( column, width < 0 ? wxDVC_DEFAULT_WIDTH : width );
1701 gtk_tree_view_column_set_sizing( column, GTK_TREE_VIEW_COLUMN_FIXED );
1702
1703 gtk_tree_view_column_pack_end( column, renderer, TRUE );
1704
1705 gtk_tree_view_column_set_cell_data_func( column, renderer,
1706 wxGtkTreeCellDataFunc, (gpointer) GetRenderer(), NULL );
1707 }
1708
1709 wxDataViewColumn::~wxDataViewColumn()
1710 {
1711 }
1712
1713 void wxDataViewColumn::OnInternalIdle()
1714 {
1715 if (m_isConnected)
1716 return;
1717
1718 if (GTK_WIDGET_REALIZED(GetOwner()->m_treeview))
1719 {
1720 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1721 if (column->button)
1722 {
1723 g_signal_connect(column->button, "button_press_event",
1724 G_CALLBACK (gtk_dataview_header_button_press_callback), this);
1725
1726 m_isConnected = true;
1727 }
1728 }
1729 }
1730
1731 void wxDataViewColumn::SetOwner( wxDataViewCtrl *owner )
1732 {
1733 wxDataViewColumnBase::SetOwner( owner );
1734
1735 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1736
1737 gtk_tree_view_column_set_title( column, wxGTK_CONV_FONT(GetTitle(), GetOwner()->GetFont() ) );
1738 }
1739
1740 void wxDataViewColumn::SetTitle( const wxString &title )
1741 {
1742 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1743
1744 if (m_isConnected)
1745 {
1746 // disconnect before column->button gets recreated
1747 g_signal_handlers_disconnect_by_func( column->button,
1748 (GtkWidget*) gtk_dataview_header_button_press_callback, this);
1749
1750 m_isConnected = false;
1751 }
1752
1753 // FIXME: can it really happen that we don't have the owner here??
1754 wxDataViewCtrl *ctrl = GetOwner();
1755 gtk_tree_view_column_set_title( column, ctrl ? wxGTK_CONV_FONT(title, ctrl->GetFont())
1756 : wxGTK_CONV_SYS(title) );
1757
1758 gtk_tree_view_column_set_widget( column, NULL );
1759 }
1760
1761 wxString wxDataViewColumn::GetTitle() const
1762 {
1763 const gchar *str = gtk_tree_view_column_get_title( GTK_TREE_VIEW_COLUMN(m_column) );
1764 return wxConvFileName->cMB2WX(str);
1765 }
1766
1767 void wxDataViewColumn::SetBitmap( const wxBitmap &bitmap )
1768 {
1769 wxDataViewColumnBase::SetBitmap( bitmap );
1770
1771 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1772 if (bitmap.Ok())
1773 {
1774 GtkImage *gtk_image = GTK_IMAGE( gtk_image_new() );
1775
1776 GdkBitmap *mask = (GdkBitmap *) NULL;
1777 if (bitmap.GetMask())
1778 mask = bitmap.GetMask()->GetBitmap();
1779
1780 if (bitmap.HasPixbuf())
1781 {
1782 gtk_image_set_from_pixbuf(GTK_IMAGE(gtk_image),
1783 bitmap.GetPixbuf());
1784 }
1785 else
1786 {
1787 gtk_image_set_from_pixmap(GTK_IMAGE(gtk_image),
1788 bitmap.GetPixmap(), mask);
1789 }
1790 gtk_widget_show( GTK_WIDGET(gtk_image) );
1791
1792 gtk_tree_view_column_set_widget( column, GTK_WIDGET(gtk_image) );
1793 }
1794 else
1795 {
1796 gtk_tree_view_column_set_widget( column, NULL );
1797 }
1798 }
1799
1800 void wxDataViewColumn::SetHidden( bool hidden )
1801 {
1802 gtk_tree_view_column_set_visible( GTK_TREE_VIEW_COLUMN(m_column), !hidden );
1803 }
1804
1805 void wxDataViewColumn::SetResizeable( bool resizeable )
1806 {
1807 gtk_tree_view_column_set_resizable( GTK_TREE_VIEW_COLUMN(m_column), resizeable );
1808 }
1809
1810 void wxDataViewColumn::SetAlignment( wxAlignment align )
1811 {
1812 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1813
1814 gfloat xalign = 0.0;
1815 if (align == wxALIGN_RIGHT)
1816 xalign = 1.0;
1817 if (align == wxALIGN_CENTER_HORIZONTAL ||
1818 align == wxALIGN_CENTER)
1819 xalign = 0.5;
1820
1821 gtk_tree_view_column_set_alignment( column, xalign );
1822 }
1823
1824 wxAlignment wxDataViewColumn::GetAlignment() const
1825 {
1826 gfloat xalign = gtk_tree_view_column_get_alignment( GTK_TREE_VIEW_COLUMN(m_column) );
1827
1828 if (xalign == 1.0)
1829 return wxALIGN_RIGHT;
1830 if (xalign == 0.5)
1831 return wxALIGN_CENTER_HORIZONTAL;
1832
1833 return wxALIGN_LEFT;
1834 }
1835
1836 void wxDataViewColumn::SetSortable( bool sortable )
1837 {
1838 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1839 gtk_tree_view_column_set_sort_indicator( column, sortable );
1840 }
1841
1842 bool wxDataViewColumn::IsSortable() const
1843 {
1844 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1845 return gtk_tree_view_column_get_sort_indicator( column );
1846 }
1847
1848 bool wxDataViewColumn::IsResizeable() const
1849 {
1850 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1851 return gtk_tree_view_column_get_resizable( column );
1852 }
1853
1854 bool wxDataViewColumn::IsHidden() const
1855 {
1856 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1857 return !gtk_tree_view_column_get_visible( column );
1858 }
1859
1860 void wxDataViewColumn::SetSortOrder( bool ascending )
1861 {
1862 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1863
1864 if (ascending)
1865 gtk_tree_view_column_set_sort_order( column, GTK_SORT_ASCENDING );
1866 else
1867 gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
1868 }
1869
1870 bool wxDataViewColumn::IsSortOrderAscending() const
1871 {
1872 GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
1873
1874 return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
1875 }
1876
1877 void wxDataViewColumn::SetMinWidth( int width )
1878 {
1879 gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
1880 }
1881
1882 int wxDataViewColumn::GetMinWidth() const
1883 {
1884 return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
1885 }
1886
1887 int wxDataViewColumn::GetWidth() const
1888 {
1889 return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
1890 }
1891
1892 void wxDataViewColumn::SetWidth( int width )
1893 {
1894 gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
1895 }
1896
1897
1898 //-----------------------------------------------------------------------------
1899 // wxDataViewCtrl signal callbacks
1900 //-----------------------------------------------------------------------------
1901
1902 static void
1903 wxdataview_selection_changed_callback( GtkTreeSelection* selection, wxDataViewCtrl *dv )
1904 {
1905 if (!GTK_WIDGET_REALIZED(dv->m_widget))
1906 return;
1907
1908 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_SELECTED, dv->GetId() );
1909 event.SetRow( dv->GetSelection() );
1910 event.SetModel( dv->GetModel() );
1911 dv->GetEventHandler()->ProcessEvent( event );
1912 }
1913
1914 static void
1915 wxdataview_row_activated_callback( GtkTreeView* treeview, GtkTreePath *path,
1916 GtkTreeViewColumn *column, wxDataViewCtrl *dv )
1917 {
1918 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED, dv->GetId() );
1919 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
1920 event.SetRow( row );
1921 event.SetModel( dv->GetModel() );
1922 dv->GetEventHandler()->ProcessEvent( event );
1923 }
1924
1925 //-----------------------------------------------------------------------------
1926 // wxDataViewCtrl
1927 //-----------------------------------------------------------------------------
1928
1929 //-----------------------------------------------------------------------------
1930 // InsertChild for wxDataViewCtrl
1931 //-----------------------------------------------------------------------------
1932
1933 static void wxInsertChildInDataViewCtrl( wxWindowGTK* parent, wxWindowGTK* child )
1934 {
1935 wxDataViewCtrl * dvc = (wxDataViewCtrl*) parent;
1936 GtkWidget *treeview = dvc->GtkGetTreeView();
1937
1938 // Insert widget in GtkTreeView
1939 if (GTK_WIDGET_REALIZED(treeview))
1940 gtk_widget_set_parent_window( child->m_widget,
1941 gtk_tree_view_get_bin_window( GTK_TREE_VIEW(treeview) ) );
1942 gtk_widget_set_parent( child->m_widget, treeview );
1943 }
1944
1945 static
1946 void gtk_dataviewctrl_size_callback( GtkWidget *WXUNUSED(widget),
1947 GtkAllocation *alloc,
1948 wxDataViewCtrl *win )
1949 {
1950
1951 wxWindowList::Node *node = win->GetChildren().GetFirst();
1952 while (node)
1953 {
1954 wxWindow *child = node->GetData();
1955
1956 GtkRequisition req;
1957 gtk_widget_size_request( child->m_widget, &req );
1958
1959 GtkAllocation alloc;
1960 alloc.x = child->m_x;
1961 alloc.y = child->m_y;
1962 alloc.width = child->m_width;
1963 alloc.height = child->m_height;
1964 gtk_widget_size_allocate( child->m_widget, &alloc );
1965
1966 node = node->GetNext();
1967 }
1968 }
1969
1970
1971
1972 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl, wxDataViewCtrlBase)
1973
1974 wxDataViewCtrl::~wxDataViewCtrl()
1975 {
1976 if (m_notifier)
1977 GetModel()->RemoveNotifier( m_notifier );
1978
1979 // remove the model from the GtkTreeView before it gets destroyed by the
1980 // wxDataViewCtrlBase's dtor
1981 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), NULL );
1982 }
1983
1984 void wxDataViewCtrl::Init()
1985 {
1986 m_notifier = NULL;
1987 }
1988
1989 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
1990 const wxPoint& pos, const wxSize& size,
1991 long style, const wxValidator& validator )
1992 {
1993 Init();
1994
1995 if (!PreCreation( parent, pos, size ) ||
1996 !CreateBase( parent, id, pos, size, style, validator ))
1997 {
1998 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1999 return false;
2000 }
2001
2002 m_insertCallback = wxInsertChildInDataViewCtrl;
2003
2004 m_widget = gtk_scrolled_window_new (NULL, NULL);
2005
2006 GtkScrolledWindowSetBorder(m_widget, style);
2007
2008 m_treeview = gtk_tree_view_new();
2009 gtk_container_add (GTK_CONTAINER (m_widget), m_treeview);
2010
2011 g_signal_connect (m_treeview, "size_allocate",
2012 G_CALLBACK (gtk_dataviewctrl_size_callback), this);
2013
2014 #ifdef __WXGTK26__
2015 if (!gtk_check_version(2,6,0))
2016 gtk_tree_view_set_fixed_height_mode( GTK_TREE_VIEW(m_treeview), TRUE );
2017 #endif
2018
2019 if (style & wxDV_MULTIPLE)
2020 {
2021 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2022 gtk_tree_selection_set_mode( selection, GTK_SELECTION_MULTIPLE );
2023 }
2024
2025 gtk_tree_view_set_headers_visible( GTK_TREE_VIEW(m_treeview), (style & wxDV_NO_HEADER) == 0 );
2026
2027 #ifdef __WXGTK210__
2028 if (!gtk_check_version(2,10,0))
2029 {
2030 GtkTreeViewGridLines grid = GTK_TREE_VIEW_GRID_LINES_NONE;
2031
2032 if ((style & wxDV_HORIZ_RULES) != 0 &&
2033 (style & wxDV_VERT_RULES) != 0)
2034 grid = GTK_TREE_VIEW_GRID_LINES_BOTH;
2035 else if (style & wxDV_VERT_RULES)
2036 grid = GTK_TREE_VIEW_GRID_LINES_VERTICAL;
2037 else if (style & wxDV_HORIZ_RULES)
2038 grid = GTK_TREE_VIEW_GRID_LINES_HORIZONTAL;
2039
2040 gtk_tree_view_set_grid_lines( GTK_TREE_VIEW(m_treeview), grid );
2041 }
2042 else
2043 #endif
2044 {
2045 gtk_tree_view_set_rules_hint( GTK_TREE_VIEW(m_treeview), (style & wxDV_HORIZ_RULES) != 0 );
2046 }
2047
2048 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget),
2049 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
2050 gtk_widget_show (m_treeview);
2051
2052 m_parent->DoAddChild( this );
2053
2054 PostCreation(size);
2055
2056 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2057 g_signal_connect_after (selection, "changed",
2058 G_CALLBACK (wxdataview_selection_changed_callback), this);
2059 g_signal_connect_after (m_treeview, "row_activated",
2060 G_CALLBACK (wxdataview_row_activated_callback), this);
2061
2062 return true;
2063 }
2064
2065 void wxDataViewCtrl::OnInternalIdle()
2066 {
2067 wxWindow::OnInternalIdle();
2068
2069 unsigned int cols = GetColumnCount();
2070 unsigned int i;
2071 for (i = 0; i < cols; i++)
2072 {
2073 wxDataViewColumn *col = GetColumn( i );
2074 col->OnInternalIdle();
2075 }
2076 }
2077
2078 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel *model )
2079 {
2080 if (!wxDataViewCtrlBase::AssociateModel( model ))
2081 return false;
2082
2083 GtkWxListStore *gtk_store = wxgtk_list_store_new();
2084 gtk_store->model = model;
2085
2086 m_notifier = new wxGtkDataViewListModelNotifier( gtk_store, model );
2087
2088 model->AddNotifier( m_notifier );
2089
2090 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview), GTK_TREE_MODEL(gtk_store) );
2091 g_object_unref( gtk_store );
2092
2093 return true;
2094 }
2095
2096 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn *col )
2097 {
2098 if (!wxDataViewCtrlBase::AppendColumn(col))
2099 return false;
2100
2101 GtkTreeViewColumn *column = (GtkTreeViewColumn *)col->GetGtkHandle();
2102
2103 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview), column );
2104
2105 return true;
2106 }
2107
2108 void wxDataViewCtrl::SetSelection( int row )
2109 {
2110 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2111
2112 if (row < 0)
2113 {
2114 gtk_tree_selection_unselect_all( selection );
2115 }
2116 else
2117 {
2118 GtkTreePath *path = gtk_tree_path_new ();
2119 gtk_tree_path_append_index( path, row );
2120
2121 gtk_tree_selection_select_path( selection, path );
2122
2123 gtk_tree_path_free( path );
2124 }
2125 }
2126
2127 void wxDataViewCtrl::Unselect( unsigned int row )
2128 {
2129 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2130
2131 GtkTreePath *path = gtk_tree_path_new ();
2132 gtk_tree_path_append_index( path, row );
2133
2134 gtk_tree_selection_unselect_path( selection, path );
2135
2136 gtk_tree_path_free( path );
2137 }
2138
2139 void wxDataViewCtrl::SetSelectionRange( unsigned int from, unsigned int to )
2140 {
2141 }
2142
2143 void wxDataViewCtrl::SetSelections( const wxArrayInt& aSelections)
2144 {
2145 }
2146
2147 bool wxDataViewCtrl::IsSelected( unsigned int row ) const
2148 {
2149 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2150
2151 GtkTreePath *path = gtk_tree_path_new ();
2152 gtk_tree_path_append_index( path, row );
2153
2154 gboolean ret = gtk_tree_selection_path_is_selected( selection, path );
2155
2156 gtk_tree_path_free( path );
2157
2158 return ret;
2159 }
2160
2161 int wxDataViewCtrl::GetSelection() const
2162 {
2163 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2164 if (HasFlag(wxDV_MULTIPLE))
2165 {
2166 GtkTreeModel *model;
2167 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
2168
2169 // do something
2170 if (list)
2171 {
2172 // list = g_list_nth( list, 0 ); should be a noop
2173 GtkTreePath *path = (GtkTreePath*) list->data;
2174
2175 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
2176
2177 // delete list
2178 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
2179 g_list_free( list );
2180
2181 return (int) row;
2182 }
2183 }
2184 else
2185 {
2186
2187 GtkTreeModel *model;
2188 GtkTreeIter iter;
2189 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
2190 if (has_selection)
2191 {
2192 unsigned int row = (wxUIntPtr) iter.user_data;
2193 return (int) row;
2194 }
2195 }
2196
2197 return -1;
2198 }
2199
2200 int wxDataViewCtrl::GetSelections(wxArrayInt& aSelections) const
2201 {
2202 aSelections.Clear();
2203
2204 GtkTreeSelection *selection = gtk_tree_view_get_selection( GTK_TREE_VIEW(m_treeview) );
2205 if (HasFlag(wxDV_MULTIPLE))
2206 {
2207 GtkTreeModel *model;
2208 GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
2209
2210 int count = 0;
2211 while (list)
2212 {
2213
2214 // list = g_list_nth( list, 0 ); should be a noop
2215 GtkTreePath *path = (GtkTreePath*) list->data;
2216
2217 unsigned int row = (unsigned int)gtk_tree_path_get_indices (path)[0];
2218
2219 aSelections.Add( (int) row );
2220
2221 list = g_list_next( list );
2222 }
2223
2224 // delete list
2225 g_list_foreach( list, (GFunc) gtk_tree_path_free, NULL );
2226 g_list_free( list );
2227
2228 return count;
2229 }
2230 else
2231 {
2232 GtkTreeModel *model;
2233 GtkTreeIter iter;
2234 gboolean has_selection = gtk_tree_selection_get_selected( selection, &model, &iter );
2235 if (has_selection)
2236 {
2237 unsigned int row = (wxUIntPtr) iter.user_data;
2238 aSelections.Add( (int) row );
2239 return 1;
2240 }
2241 }
2242
2243 return 0;
2244 }
2245
2246 // static
2247 wxVisualAttributes
2248 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
2249 {
2250 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new);
2251 }
2252
2253
2254 #endif
2255 // !wxUSE_GENERICDATAVIEWCTRL
2256
2257 #endif
2258 // wxUSE_DATAVIEWCTRL