1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl GTK+2 implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #if wxUSE_DATAVIEWCTRL
17 #include "wx/dataview.h"
18 #include "wx/stockitem.h"
19 #include "wx/dcclient.h"
20 #include "wx/calctrl.h"
21 #include "wx/popupwin.h"
25 #include "wx/gtk/private.h"
26 #include "wx/gtk/win_gtk.h"
28 #include <gobject/gvaluecollector.h>
29 #include <gtk/gtktreemodel.h>
30 #include <gtk/gtktreednd.h>
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern void wxapp_install_idle_handler();
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 extern bool g_blockEventsOnDrag
;
53 //-----------------------------------------------------------------------------
54 // define new GTK+ class wxGtkListStore
55 //-----------------------------------------------------------------------------
59 #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ())
60 #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore))
61 #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
62 #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE))
63 #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE))
64 #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
66 GType
gtk_wx_list_store_get_type (void);
68 typedef struct _GtkWxListStore GtkWxListStore
;
69 typedef struct _GtkWxListStoreClass GtkWxListStoreClass
;
71 struct _GtkWxListStore
77 wxDataViewListModel
*model
;
80 struct _GtkWxListStoreClass
82 GObjectClass list_parent_class
;
86 static GtkWxListStore
*wxgtk_list_store_new (void);
87 static void wxgtk_list_store_init (GtkWxListStore
*list_store
);
88 static void wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
);
89 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
);
90 static void wxgtk_list_store_finalize (GObject
*object
);
91 static GtkTreeModelFlags
wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
);
92 static gint
wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
);
93 static GType
wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
95 static gboolean
wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
98 static GtkTreePath
*wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
100 static void wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
104 static gboolean
wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
106 static gboolean
wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
108 GtkTreeIter
*parent
);
109 static gboolean
wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
111 static gint
wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
113 static gboolean
wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
117 static gboolean
wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
121 static GObjectClass
*list_parent_class
= NULL
;
124 gtk_wx_list_store_get_type (void)
126 static GType list_store_type
= 0;
128 if (!list_store_type
)
130 static const GTypeInfo list_store_info
=
132 sizeof (GtkWxListStoreClass
),
133 NULL
, /* base_init */
134 NULL
, /* base_finalize */
135 (GClassInitFunc
) wxgtk_list_store_class_init
,
136 NULL
, /* class_finalize */
137 NULL
, /* class_data */
138 sizeof (GtkWxListStore
),
140 (GInstanceInitFunc
) wxgtk_list_store_init
,
143 static const GInterfaceInfo tree_model_info
=
145 (GInterfaceInitFunc
) wxgtk_list_store_tree_model_init
,
150 list_store_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxListStore",
151 &list_store_info
, (GTypeFlags
)0 );
153 g_type_add_interface_static (list_store_type
,
158 return list_store_type
;
161 static GtkWxListStore
*
162 wxgtk_list_store_new(void)
164 GtkWxListStore
*retval
= (GtkWxListStore
*) g_object_new (GTK_TYPE_WX_LIST_STORE
, NULL
);
169 wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
)
171 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
172 GObjectClass
*object_class
= (GObjectClass
*) klass
;
173 object_class
->finalize
= wxgtk_list_store_finalize
;
177 wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
)
179 iface
->get_flags
= wxgtk_list_store_get_flags
;
180 iface
->get_n_columns
= wxgtk_list_store_get_n_columns
;
181 iface
->get_column_type
= wxgtk_list_store_get_column_type
;
182 iface
->get_iter
= wxgtk_list_store_get_iter
;
183 iface
->get_path
= wxgtk_list_store_get_path
;
184 iface
->get_value
= wxgtk_list_store_get_value
;
185 iface
->iter_next
= wxgtk_list_store_iter_next
;
186 iface
->iter_children
= wxgtk_list_store_iter_children
;
187 iface
->iter_has_child
= wxgtk_list_store_iter_has_child
;
188 iface
->iter_n_children
= wxgtk_list_store_iter_n_children
;
189 iface
->iter_nth_child
= wxgtk_list_store_iter_nth_child
;
190 iface
->iter_parent
= wxgtk_list_store_iter_parent
;
194 wxgtk_list_store_init (GtkWxListStore
*list_store
)
196 list_store
->model
= NULL
;
197 list_store
->stamp
= g_random_int();
201 wxgtk_list_store_finalize (GObject
*object
)
203 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
205 /* we need to sort out, which class deletes what */
206 /* delete list_store->model; */
209 (* list_parent_class
->finalize
) (object
);
214 //-----------------------------------------------------------------------------
215 // implement callbacks from wxGtkListStore class by letting
216 // them call the methods of wxWidgets' wxDataViewListModel
217 //-----------------------------------------------------------------------------
219 static GtkTreeModelFlags
220 wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
)
222 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), (GtkTreeModelFlags
)0 );
224 // GTK+ list store uses a linked list for storing the
225 // items and a pointer to a child is used as the member
226 // field of a GtkTreeIter. This means that the iter is
227 // valid in the GtkListStore as long as the child exists.
228 // We use the index of the row and since the index of a
229 // specific row will change if a row above is deleted,
230 // the iter does not persist
231 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY
;
235 wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
)
237 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
238 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), 0);
240 return list_store
->model
->GetNumberOfCols();
244 wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
247 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
248 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), G_TYPE_INVALID
);
250 GType gtype
= G_TYPE_INVALID
;
252 wxString wxtype
= list_store
->model
->GetColType( (size_t) index
);
254 if (wxtype
== wxT("string"))
255 gtype
= G_TYPE_STRING
;
261 wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
265 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
266 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
267 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
269 size_t i
= (size_t)gtk_tree_path_get_indices (path
)[0];
271 if (i
>= list_store
->model
->GetNumberOfRows())
274 iter
->stamp
= list_store
->stamp
;
275 // user_data is just the index
276 iter
->user_data
= (gpointer
) i
;
282 wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
285 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), NULL
);
286 g_return_val_if_fail (iter
->stamp
== GTK_WX_LIST_STORE (tree_model
)->stamp
, NULL
);
288 GtkTreePath
*retval
= gtk_tree_path_new ();
289 // user_data is just the index
290 int i
= (int) iter
->user_data
;
291 gtk_tree_path_append_index (retval
, i
);
296 wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
301 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
302 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model
) );
304 wxDataViewListModel
*model
= list_store
->model
;
305 wxString mtype
= model
->GetColType( (size_t) column
);
306 if (mtype
== wxT("string"))
309 g_value_init( value
, G_TYPE_STRING
);
310 model
->GetValue( variant
, (size_t) column
, (size_t) iter
->user_data
);
311 g_value_set_string( value
, wxGTK_CONV(variant
.GetString()) );
318 GtkTreeDataList
*list
;
319 gint tmp_column
= column
;
321 g_return_if_fail (column
< GTK_LIST_STORE (tree_model
)->n_columns
);
322 g_return_if_fail (GTK_LIST_STORE (tree_model
)->stamp
== iter
->stamp
);
324 list
= G_SLIST (iter
->user_data
)->data
;
326 while (tmp_column
-- > 0 && list
)
330 g_value_init (value
, GTK_LIST_STORE (tree_model
)->column_headers
[column
]);
332 _gtk_tree_data_list_node_to_value (list
,
333 GTK_LIST_STORE (tree_model
)->column_headers
[column
],
340 wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
343 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
344 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
346 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, FALSE
);
348 int n
= (int) iter
->user_data
;
353 if (n
>= (int) list_store
->model
->GetNumberOfRows()-1)
356 iter
->user_data
= (gpointer
) ++n
;
362 wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
366 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
367 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
369 // this is a list, nodes have no children
373 iter
->stamp
= list_store
->stamp
;
374 iter
->user_data
= (gpointer
) -1;
380 wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
387 wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
390 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), -1);
391 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
394 return (gint
) list_store
->model
->GetNumberOfRows();
396 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, -1);
402 wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
407 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
408 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
416 if (n
>= (gint
) list_store
->model
->GetNumberOfRows())
419 iter
->stamp
= list_store
->stamp
;
420 iter
->user_data
= (gpointer
) n
;
426 wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
433 //-----------------------------------------------------------------------------
434 // define new GTK+ class wxGtkCellRenderer
435 //-----------------------------------------------------------------------------
439 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
440 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
441 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
442 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
443 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
444 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
446 GType
gtk_wx_cell_renderer_get_type (void);
448 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
449 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
451 struct _GtkWxCellRenderer
453 GtkCellRenderer parent
;
456 wxDataViewCustomCell
*cell
;
460 struct _GtkWxCellRendererClass
462 GtkCellRendererClass cell_parent_class
;
466 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
467 static void gtk_wx_cell_renderer_init (
468 GtkWxCellRenderer
*cell
);
469 static void gtk_wx_cell_renderer_class_init(
470 GtkWxCellRendererClass
*klass
);
471 static void gtk_wx_cell_renderer_finalize (
473 static void gtk_wx_cell_renderer_get_size (
474 GtkCellRenderer
*cell
,
476 GdkRectangle
*rectangle
,
481 static void gtk_wx_cell_renderer_render (
482 GtkCellRenderer
*cell
,
485 GdkRectangle
*background_area
,
486 GdkRectangle
*cell_area
,
487 GdkRectangle
*expose_area
,
488 GtkCellRendererState flags
);
489 static gboolean
gtk_wx_cell_renderer_activate(
490 GtkCellRenderer
*cell
,
494 GdkRectangle
*background_area
,
495 GdkRectangle
*cell_area
,
496 GtkCellRendererState flags
);
498 static GObjectClass
*cell_parent_class
= NULL
;
503 gtk_wx_cell_renderer_get_type (void)
505 static GType cell_wx_type
= 0;
509 static const GTypeInfo cell_wx_info
=
511 sizeof (GtkWxCellRendererClass
),
512 NULL
, /* base_init */
513 NULL
, /* base_finalize */
514 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
515 NULL
, /* class_finalize */
516 NULL
, /* class_data */
517 sizeof (GtkWxCellRenderer
),
519 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
522 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
523 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
530 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
533 cell
->last_click
= 0;
537 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
539 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
540 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
542 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
544 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
546 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
547 cell_class
->render
= gtk_wx_cell_renderer_render
;
548 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
552 gtk_wx_cell_renderer_finalize (GObject
*object
)
555 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
559 gtk_wx_cell_renderer_new (void)
561 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
565 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
567 GdkRectangle
*cell_area
,
573 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
574 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
576 wxSize size
= cell
->GetSize();
578 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
579 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
586 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
590 *x_offset
= (gint
)((renderer
->xalign
*
591 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
592 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
596 *y_offset
= (gint
)((renderer
->yalign
*
597 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
598 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
606 *height
= calc_height
;
610 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
613 GdkRectangle
*background_area
,
614 GdkRectangle
*cell_area
,
615 GdkRectangle
*expose_area
,
616 GtkCellRendererState flags
)
619 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
620 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
623 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
629 rect
.x
+= cell_area
->x
;
630 rect
.y
+= cell_area
->y
;
631 rect
.width
-= renderer
->xpad
* 2;
632 rect
.height
-= renderer
->ypad
* 2;
635 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
637 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
638 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
639 if (dc
->m_window
== NULL
)
641 dc
->m_window
= window
;
646 if (flags
& GTK_CELL_RENDERER_SELECTED
)
647 state
|= wxDATAVIEW_CELL_SELECTED
;
648 if (flags
& GTK_CELL_RENDERER_PRELIT
)
649 state
|= wxDATAVIEW_CELL_PRELIT
;
650 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
651 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
652 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
653 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
654 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
655 state
|= wxDATAVIEW_CELL_FOCUSED
;
656 cell
->Render( renderrect
, dc
, state
);
661 gtk_wx_cell_renderer_activate(
662 GtkCellRenderer
*renderer
,
666 GdkRectangle
*background_area
,
667 GdkRectangle
*cell_area
,
668 GtkCellRendererState flags
)
670 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
671 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
674 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
680 rect
.x
+= cell_area
->x
;
681 rect
.y
+= cell_area
->y
;
682 rect
.width
-= renderer
->xpad
* 2;
683 rect
.height
-= renderer
->ypad
* 2;
685 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
687 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
689 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
690 size_t model_row
= (size_t)gtk_tree_path_get_indices (treepath
)[0];
691 gtk_tree_path_free( treepath
);
693 size_t model_col
= cell
->GetOwner()->GetModelColumn();
695 if (event
->type
== GDK_BUTTON_PRESS
)
697 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
698 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
699 ((int) button_event
->y
) - renderrect
.y
);
702 if (button_event
->button
== 1)
704 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
706 // TODO: query system double-click time
707 if (button_event
->time
- wxrenderer
->last_click
< 400)
708 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
711 if (button_event
->button
== 3)
713 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
717 wxrenderer
->last_click
= button_event
->time
;
725 // ---------------------------------------------------------
726 // wxGtkDataViewListModelNotifier
727 // ---------------------------------------------------------
729 class wxGtkDataViewListModelNotifier
: public wxDataViewListModelNotifier
732 wxGtkDataViewListModelNotifier( GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
);
734 virtual bool RowAppended();
735 virtual bool RowPrepended();
736 virtual bool RowInserted( size_t before
);
737 virtual bool RowDeleted( size_t row
);
738 virtual bool RowChanged( size_t row
);
739 virtual bool ValueChanged( size_t col
, size_t row
);
740 virtual bool RowsReordered( size_t *new_order
);
741 virtual bool Cleared();
743 GtkWxListStore
*m_gtk_store
;
744 wxDataViewListModel
*m_wx_model
;
747 // ---------------------------------------------------------
748 // wxGtkDataViewListModelNotifier
749 // ---------------------------------------------------------
751 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
752 GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
)
754 m_gtk_store
= gtk_store
;
755 m_wx_model
= wx_model
;
758 bool wxGtkDataViewListModelNotifier::RowAppended()
760 size_t pos
= m_wx_model
->GetNumberOfRows()-1;
763 iter
.stamp
= m_gtk_store
->stamp
;
764 iter
.user_data
= (gpointer
) pos
;
766 GtkTreePath
*path
= gtk_tree_path_new ();
767 gtk_tree_path_append_index (path
, (gint
) pos
);
768 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
769 gtk_tree_path_free (path
);
774 bool wxGtkDataViewListModelNotifier::RowPrepended()
777 iter
.stamp
= m_gtk_store
->stamp
;
778 iter
.user_data
= (gpointer
) 0;
780 GtkTreePath
*path
= gtk_tree_path_new ();
781 gtk_tree_path_append_index (path
, (gint
) 0);
782 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
783 gtk_tree_path_free (path
);
788 bool wxGtkDataViewListModelNotifier::RowInserted( size_t before
)
793 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row
)
798 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row
)
801 iter
.stamp
= m_gtk_store
->stamp
;
802 iter
.user_data
= (gpointer
) row
;
803 GtkTreePath
*path
= gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store
), &iter
);
804 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
805 gtk_tree_path_free (path
);
810 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col
, size_t model_row
)
812 // This adds GTK+'s missing MVC logic for ValueChanged
813 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
816 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
817 if (viewing_column
->m_modelColumn
== model_col
)
819 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
820 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(viewing_column
->m_viewColumn
->GetGtkHandle());
823 GtkTreePath
*path
= gtk_tree_path_new();
824 gtk_tree_path_append_index( path
, model_row
);
825 GdkRectangle cell_area
;
826 gtk_tree_view_get_cell_area( widget
, path
, column
, &cell_area
);
827 gtk_tree_path_free( path
);
829 int ydiff
= column
->button
->allocation
.height
;
831 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
832 cell_area
.x
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
835 node
= node
->GetNext();
841 bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order
)
843 // Assume sizeof(size_t)= == sizeof(gint)
845 GtkTreePath
*path
= gtk_tree_path_new ();
846 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store
), path
, NULL
, (gint
*)new_order
);
847 gtk_tree_path_free (path
);
849 // This adds GTK+'s missing MVC logic for RowsReordered
850 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
853 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
854 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
855 // Doesn't work yet...
856 gtk_widget_queue_draw( GTK_WIDGET(widget
) );
858 node
= node
->GetNext();
864 bool wxGtkDataViewListModelNotifier::Cleared()
869 // ---------------------------------------------------------
871 // ---------------------------------------------------------
873 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
875 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
876 wxDataViewCellBase( varianttype
, mode
)
881 // ---------------------------------------------------------
882 // wxDataViewTextCell
883 // ---------------------------------------------------------
886 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
887 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
890 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
891 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
893 wxDataViewTextCell
*cell
= (wxDataViewTextCell
*) user_data
;
895 wxString tmp
= wxGTK_CONV_BACK( arg2
);
896 wxVariant value
= tmp
;
897 if (!cell
->Validate( value
))
900 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
902 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
903 size_t model_row
= (size_t)gtk_tree_path_get_indices (path
)[0];
904 gtk_tree_path_free( path
);
906 size_t model_col
= cell
->GetOwner()->GetModelColumn();
908 model
->SetValue( value
, model_col
, model_row
);
909 model
->ValueChanged( model_col
, model_row
);
912 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCell
)
914 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
915 wxDataViewCell( varianttype
, mode
)
917 m_renderer
= (void*) gtk_cell_renderer_text_new();
919 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
921 GValue gvalue
= { 0, };
922 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
923 g_value_set_boolean( &gvalue
, true );
924 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
925 g_value_unset( &gvalue
);
927 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
931 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
933 wxString tmp
= value
;
935 GValue gvalue
= { 0, };
936 g_value_init( &gvalue
, G_TYPE_STRING
);
937 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
938 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
939 g_value_unset( &gvalue
);
944 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
946 GValue gvalue
= { 0, };
947 g_value_init( &gvalue
, G_TYPE_STRING
);
948 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
949 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
950 g_value_unset( &gvalue
);
957 // ---------------------------------------------------------
958 // wxDataViewToggleCell
959 // ---------------------------------------------------------
962 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
963 gchar
*path
, gpointer user_data
);
966 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
967 gchar
*path
, gpointer user_data
)
969 wxDataViewToggleCell
*cell
= (wxDataViewToggleCell
*) user_data
;
972 GValue gvalue
= { 0, };
973 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
974 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
975 bool tmp
= g_value_get_boolean( &gvalue
);
976 g_value_unset( &gvalue
);
980 wxVariant value
= tmp
;
981 if (!cell
->Validate( value
))
984 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
986 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
987 size_t model_row
= (size_t)gtk_tree_path_get_indices (gtk_path
)[0];
988 gtk_tree_path_free( gtk_path
);
990 size_t model_col
= cell
->GetOwner()->GetModelColumn();
992 model
->SetValue( value
, model_col
, model_row
);
993 model
->ValueChanged( model_col
, model_row
);
996 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCell
)
998 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
999 wxDataViewCellMode mode
) :
1000 wxDataViewCell( varianttype
, mode
)
1002 m_renderer
= (void*) gtk_cell_renderer_toggle_new();
1004 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
1006 g_signal_connect_after( m_renderer
, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1011 GValue gvalue
= { 0, };
1012 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1013 g_value_set_boolean( &gvalue
, false );
1014 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1015 g_value_unset( &gvalue
);
1017 GValue gvalue2
= { 0, };
1018 g_value_init( &gvalue2
, gtk_cell_renderer_mode_get_type() );
1019 g_value_set_enum( &gvalue2
, GTK_CELL_RENDERER_MODE_INERT
);
1020 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue2
);
1021 g_value_unset( &gvalue2
);
1026 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
1030 GValue gvalue
= { 0, };
1031 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1032 g_value_set_boolean( &gvalue
, tmp
);
1033 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1034 g_value_unset( &gvalue
);
1039 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
1041 GValue gvalue
= { 0, };
1042 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1043 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1044 bool tmp
= g_value_get_boolean( &gvalue
);
1045 g_value_unset( &gvalue
);
1052 // ---------------------------------------------------------
1053 // wxDataViewCustomCell
1054 // ---------------------------------------------------------
1056 class wxDataViewCtrlDC
: public wxWindowDC
1059 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1061 GtkWidget
*widget
= window
->m_treeview
;
1065 m_context
= window
->GtkGetPangoDefaultContext();
1066 m_layout
= pango_layout_new( m_context
);
1067 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1069 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1071 // Set m_window later
1073 // m_owner = window;
1077 // ---------------------------------------------------------
1078 // wxDataViewCustomCell
1079 // ---------------------------------------------------------
1081 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
1083 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
1084 wxDataViewCellMode mode
, bool no_init
) :
1085 wxDataViewCell( varianttype
, mode
)
1095 bool wxDataViewCustomCell::Init()
1097 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1098 renderer
->cell
= this;
1100 m_renderer
= (void*) renderer
;
1102 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1104 GValue gvalue
= { 0, };
1105 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1106 g_value_set_enum( &gvalue
, GTK_CELL_RENDERER_MODE_ACTIVATABLE
);
1107 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1108 g_value_unset( &gvalue
);
1114 wxDataViewCustomCell::~wxDataViewCustomCell()
1120 wxDC
*wxDataViewCustomCell::GetDC()
1124 if (GetOwner() == NULL
)
1126 if (GetOwner()->GetOwner() == NULL
)
1128 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1134 // ---------------------------------------------------------
1135 // wxDataViewProgressCell
1136 // ---------------------------------------------------------
1138 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
1140 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
1141 const wxString
&varianttype
, wxDataViewCellMode mode
) :
1142 wxDataViewCustomCell( varianttype
, mode
, true )
1148 if (!gtk_check_version(2,6,0))
1150 m_renderer
= (void*) gtk_cell_renderer_progress_new();
1152 GValue gvalue
= { 0, };
1153 g_value_init( &gvalue
, G_TYPE_STRING
);
1154 g_value_set_boolean( &gvalue
, wxGTK_CONV(m_label
) );
1155 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1156 g_value_unset( &gvalue
);
1161 // Use custom cell code
1162 wxDataViewCustomCell::Init();
1166 wxDataViewProgressCell::~wxDataViewProgressCell()
1170 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
1173 if (!gtk_check_version(2,6,0))
1175 gint tmp
= (int) value
;
1176 GValue gvalue
= { 0, };
1177 g_value_init( &gvalue
, G_TYPE_INT
);
1178 g_value_set_boolean( &gvalue
, tmp
);
1179 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1180 g_value_unset( &gvalue
);
1185 m_value
= (long) value
;
1187 if (m_value
< 0) m_value
= 0;
1188 if (m_value
> 100) m_value
= 100;
1194 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1196 double pct
= (double)m_value
/ 100.0;
1198 bar
.width
= (int)(cell
.width
* pct
);
1199 dc
->SetPen( *wxTRANSPARENT_PEN
);
1200 dc
->SetBrush( *wxBLUE_BRUSH
);
1201 dc
->DrawRectangle( bar
);
1203 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1204 dc
->SetPen( *wxBLACK_PEN
);
1205 dc
->DrawRectangle( cell
);
1210 wxSize
wxDataViewProgressCell::GetSize()
1212 return wxSize(40,12);
1215 // ---------------------------------------------------------
1216 // wxDataViewDateCell
1217 // ---------------------------------------------------------
1219 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
1222 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1223 wxDataViewListModel
*model
, size_t col
, size_t row
) :
1224 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1229 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1230 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1231 sizer
->Add( m_cal
, 1, wxGROW
);
1236 virtual void OnDismiss()
1240 void OnCalendar( wxCalendarEvent
&event
);
1242 wxCalendarCtrl
*m_cal
;
1243 wxDataViewListModel
*m_model
;
1248 DECLARE_EVENT_TABLE()
1251 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
1252 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
1255 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1257 wxDateTime date
= event
.GetDate();
1258 wxVariant value
= date
;
1259 m_model
->SetValue( value
, m_col
, m_row
);
1260 m_model
->ValueChanged( m_col
, m_row
);
1264 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
1266 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
1267 wxDataViewCellMode mode
) :
1268 wxDataViewCustomCell( varianttype
, mode
)
1272 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
1274 m_date
= value
.GetDateTime();
1279 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1281 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1282 wxString tmp
= m_date
.FormatDate();
1283 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1288 wxSize
wxDataViewDateCell::GetSize()
1290 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
1291 wxString tmp
= m_date
.FormatDate();
1293 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1294 return wxSize(x
,y
+d
);
1297 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
1300 model
->GetValue( variant
, col
, row
);
1301 wxDateTime value
= variant
.GetDateTime();
1303 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
1304 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1305 wxPoint pos
= wxGetMousePosition();
1308 popup
->Popup( popup
->m_cal
);
1313 // ---------------------------------------------------------
1315 // ---------------------------------------------------------
1318 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1319 GtkCellRenderer
*cell
,
1320 GtkTreeModel
*model
,
1326 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1327 GtkCellRenderer
*renderer
,
1328 GtkTreeModel
*model
,
1332 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1333 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1335 wxDataViewCell
*cell
= (wxDataViewCell
*) data
;
1337 size_t model_row
= (size_t) iter
->user_data
;
1340 list_store
->model
->GetValue( value
, cell
->GetOwner()->GetModelColumn(), model_row
);
1342 if (value
.GetType() != cell
->GetVariantType())
1343 wxLogError( wxT("Wrong type\n") );
1345 cell
->SetValue( value
);
1348 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1350 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
1351 size_t model_column
, int flags
) :
1352 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
1354 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) cell
->GetGtkHandle();
1356 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1358 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1360 gtk_tree_view_column_pack_start( column
, renderer
, TRUE
);
1362 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1363 wxGtkTreeCellDataFunc
, (gpointer
) cell
, NULL
);
1365 m_column
= (void*) column
;
1368 wxDataViewColumn::~wxDataViewColumn()
1372 void wxDataViewColumn::SetTitle( const wxString
&title
)
1374 wxDataViewColumnBase::SetTitle( title
);
1376 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)m_column
;
1377 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1380 //-----------------------------------------------------------------------------
1382 //-----------------------------------------------------------------------------
1384 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1386 wxDataViewCtrl::~wxDataViewCtrl()
1389 GetModel()->RemoveNotifier( m_notifier
);
1392 void wxDataViewCtrl::Init()
1397 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1398 const wxPoint
& pos
, const wxSize
& size
,
1399 long style
, const wxValidator
& validator
)
1403 m_needParent
= TRUE
;
1404 m_acceptsFocus
= TRUE
;
1406 if (!PreCreation( parent
, pos
, size
) ||
1407 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1409 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1413 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1414 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget
), GTK_SHADOW_IN
);
1416 m_treeview
= gtk_tree_view_new();
1417 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1419 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1420 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1421 gtk_widget_show (m_treeview
);
1423 m_parent
->DoAddChild( this );
1430 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1432 if (!wxDataViewCtrlBase::AssociateModel( model
))
1435 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1436 gtk_store
->model
= model
;
1438 m_notifier
= new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1440 model
->AddNotifier( m_notifier
);
1442 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1443 g_object_unref( gtk_store
);
1448 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1450 if (!wxDataViewCtrlBase::AppendColumn(col
))
1453 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1455 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1461 #endif // wxUSE_DATAVIEWCTRL