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"
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/win_gtk.h"
27 #include <gobject/gvaluecollector.h>
28 #include <gtk/gtktreemodel.h>
29 #include <gtk/gtktreednd.h>
31 #include <gdk/gdkkeysyms.h>
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern void wxapp_install_idle_handler();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern bool g_blockEventsOnDrag
;
52 //-----------------------------------------------------------------------------
53 // define new GTK+ class wxGtkListStore
54 //-----------------------------------------------------------------------------
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))
65 GType
gtk_wx_list_store_get_type (void);
67 typedef struct _GtkWxListStore GtkWxListStore
;
68 typedef struct _GtkWxListStoreClass GtkWxListStoreClass
;
70 struct _GtkWxListStore
76 wxDataViewListModel
*model
;
79 struct _GtkWxListStoreClass
81 GObjectClass list_parent_class
;
85 static GtkWxListStore
*wxgtk_list_store_new (void);
86 static void wxgtk_list_store_init (GtkWxListStore
*list_store
);
87 static void wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
);
88 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
);
89 static void wxgtk_list_store_finalize (GObject
*object
);
90 static GtkTreeModelFlags
wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
);
91 static gint
wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
);
92 static GType
wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
94 static gboolean
wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
97 static GtkTreePath
*wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
99 static void wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
103 static gboolean
wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
105 static gboolean
wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
107 GtkTreeIter
*parent
);
108 static gboolean
wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
110 static gint
wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
112 static gboolean
wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
116 static gboolean
wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
120 static GObjectClass
*list_parent_class
= NULL
;
123 gtk_wx_list_store_get_type (void)
125 static GType list_store_type
= 0;
127 if (!list_store_type
)
129 static const GTypeInfo list_store_info
=
131 sizeof (GtkWxListStoreClass
),
132 NULL
, /* base_init */
133 NULL
, /* base_finalize */
134 (GClassInitFunc
) wxgtk_list_store_class_init
,
135 NULL
, /* class_finalize */
136 NULL
, /* class_data */
137 sizeof (GtkWxListStore
),
139 (GInstanceInitFunc
) wxgtk_list_store_init
,
142 static const GInterfaceInfo tree_model_info
=
144 (GInterfaceInitFunc
) wxgtk_list_store_tree_model_init
,
149 list_store_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxListStore",
150 &list_store_info
, (GTypeFlags
)0 );
152 g_type_add_interface_static (list_store_type
,
157 return list_store_type
;
160 static GtkWxListStore
*
161 wxgtk_list_store_new(void)
163 GtkWxListStore
*retval
= (GtkWxListStore
*) g_object_new (GTK_TYPE_WX_LIST_STORE
, NULL
);
168 wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
)
170 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
171 GObjectClass
*object_class
= (GObjectClass
*) klass
;
172 object_class
->finalize
= wxgtk_list_store_finalize
;
176 wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
)
178 iface
->get_flags
= wxgtk_list_store_get_flags
;
179 iface
->get_n_columns
= wxgtk_list_store_get_n_columns
;
180 iface
->get_column_type
= wxgtk_list_store_get_column_type
;
181 iface
->get_iter
= wxgtk_list_store_get_iter
;
182 iface
->get_path
= wxgtk_list_store_get_path
;
183 iface
->get_value
= wxgtk_list_store_get_value
;
184 iface
->iter_next
= wxgtk_list_store_iter_next
;
185 iface
->iter_children
= wxgtk_list_store_iter_children
;
186 iface
->iter_has_child
= wxgtk_list_store_iter_has_child
;
187 iface
->iter_n_children
= wxgtk_list_store_iter_n_children
;
188 iface
->iter_nth_child
= wxgtk_list_store_iter_nth_child
;
189 iface
->iter_parent
= wxgtk_list_store_iter_parent
;
193 wxgtk_list_store_init (GtkWxListStore
*list_store
)
195 list_store
->model
= NULL
;
196 list_store
->stamp
= g_random_int();
200 wxgtk_list_store_finalize (GObject
*object
)
202 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
204 /* we need to sort out, which class deletes what */
205 /* delete list_store->model; */
208 (* list_parent_class
->finalize
) (object
);
213 //-----------------------------------------------------------------------------
214 // implement callbacks from wxGtkListStore class by letting
215 // them call the methods of wxWidgets' wxDataViewListModel
216 //-----------------------------------------------------------------------------
218 static GtkTreeModelFlags
219 wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
)
221 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), (GtkTreeModelFlags
)0 );
223 // GTK+ list store uses a linked list for storing the
224 // items and a pointer to a child is used as the member
225 // field of a GtkTreeIter. This means that the iter is
226 // valid in the GtkListStore as long as the child exists.
227 // We use the index of the row and since the index of a
228 // specific row will change if a row above is deleted,
229 // the iter does not persist
230 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY
;
234 wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
)
236 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
237 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), 0);
239 return list_store
->model
->GetNumberOfCols();
243 wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
246 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
247 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), G_TYPE_INVALID
);
249 GType gtype
= G_TYPE_INVALID
;
251 wxString wxtype
= list_store
->model
->GetColType( (size_t) index
);
253 if (wxtype
== wxT("string"))
254 gtype
= G_TYPE_STRING
;
260 wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
264 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
265 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
266 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
268 size_t i
= (size_t)gtk_tree_path_get_indices (path
)[0];
270 if (i
>= list_store
->model
->GetNumberOfRows())
273 iter
->stamp
= list_store
->stamp
;
274 // user_data is just the index
275 iter
->user_data
= (gpointer
) i
;
281 wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
284 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), NULL
);
285 g_return_val_if_fail (iter
->stamp
== GTK_WX_LIST_STORE (tree_model
)->stamp
, NULL
);
287 GtkTreePath
*retval
= gtk_tree_path_new ();
288 // user_data is just the index
289 int i
= (int) iter
->user_data
;
290 gtk_tree_path_append_index (retval
, i
);
295 wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
300 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
301 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model
) );
303 wxDataViewListModel
*model
= list_store
->model
;
304 wxString mtype
= model
->GetColType( (size_t) column
);
305 if (mtype
== wxT("string"))
307 g_value_init( value
, G_TYPE_STRING
);
308 wxVariant variant
= model
->GetValue( (size_t) column
, (size_t) iter
->user_data
);
309 g_value_set_string( value
, wxGTK_CONV(variant
.GetString()) );
316 GtkTreeDataList
*list
;
317 gint tmp_column
= column
;
319 g_return_if_fail (column
< GTK_LIST_STORE (tree_model
)->n_columns
);
320 g_return_if_fail (GTK_LIST_STORE (tree_model
)->stamp
== iter
->stamp
);
322 list
= G_SLIST (iter
->user_data
)->data
;
324 while (tmp_column
-- > 0 && list
)
328 g_value_init (value
, GTK_LIST_STORE (tree_model
)->column_headers
[column
]);
330 _gtk_tree_data_list_node_to_value (list
,
331 GTK_LIST_STORE (tree_model
)->column_headers
[column
],
338 wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
341 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
342 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
344 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, FALSE
);
346 int n
= (int) iter
->user_data
;
351 if (n
>= (int) list_store
->model
->GetNumberOfRows()-1)
354 iter
->user_data
= (gpointer
) ++n
;
360 wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
364 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
365 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
367 // this is a list, nodes have no children
371 iter
->stamp
= list_store
->stamp
;
372 iter
->user_data
= (gpointer
) -1;
378 wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
385 wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
388 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), -1);
389 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
392 return (gint
) list_store
->model
->GetNumberOfRows();
394 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, -1);
400 wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
405 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
406 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
414 if (n
>= (gint
) list_store
->model
->GetNumberOfRows())
417 iter
->stamp
= list_store
->stamp
;
418 iter
->user_data
= (gpointer
) n
;
424 wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
431 //-----------------------------------------------------------------------------
432 // define new GTK+ class wxGtkCellRenderer
433 //-----------------------------------------------------------------------------
437 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
438 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
439 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
440 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
441 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
442 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
444 GType
gtk_wx_cell_renderer_get_type (void);
446 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
447 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
449 struct _GtkWxCellRenderer
451 GtkCellRenderer parent
;
454 wxDataViewCustomCell
*cell
;
458 struct _GtkWxCellRendererClass
460 GtkCellRendererClass cell_parent_class
;
464 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
465 static void gtk_wx_cell_renderer_init (
466 GtkWxCellRenderer
*cell
);
467 static void gtk_wx_cell_renderer_class_init(
468 GtkWxCellRendererClass
*klass
);
469 static void gtk_wx_cell_renderer_finalize (
471 static void gtk_wx_cell_renderer_get_size (
472 GtkCellRenderer
*cell
,
474 GdkRectangle
*rectangle
,
479 static void gtk_wx_cell_renderer_render (
480 GtkCellRenderer
*cell
,
483 GdkRectangle
*background_area
,
484 GdkRectangle
*cell_area
,
485 GdkRectangle
*expose_area
,
486 GtkCellRendererState flags
);
487 static gboolean
gtk_wx_cell_renderer_activate(
488 GtkCellRenderer
*cell
,
492 GdkRectangle
*background_area
,
493 GdkRectangle
*cell_area
,
494 GtkCellRendererState flags
);
496 static GObjectClass
*cell_parent_class
= NULL
;
501 gtk_wx_cell_renderer_get_type (void)
503 static GType cell_wx_type
= 0;
507 static const GTypeInfo cell_wx_info
=
509 sizeof (GtkWxCellRendererClass
),
510 NULL
, /* base_init */
511 NULL
, /* base_finalize */
512 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
513 NULL
, /* class_finalize */
514 NULL
, /* class_data */
515 sizeof (GtkWxCellRenderer
),
517 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
520 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
521 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
528 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
531 cell
->last_click
= 0;
535 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
537 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
538 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
540 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
542 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
544 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
545 cell_class
->render
= gtk_wx_cell_renderer_render
;
546 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
550 gtk_wx_cell_renderer_finalize (GObject
*object
)
553 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
557 gtk_wx_cell_renderer_new (void)
559 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
563 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
565 GdkRectangle
*cell_area
,
571 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
572 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
574 wxSize size
= cell
->GetSize();
576 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
577 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
584 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
588 *x_offset
= (gint
)((renderer
->xalign
*
589 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
590 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
594 *y_offset
= (gint
)((renderer
->yalign
*
595 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
596 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
604 *height
= calc_height
;
608 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
611 GdkRectangle
*background_area
,
612 GdkRectangle
*cell_area
,
613 GdkRectangle
*expose_area
,
614 GtkCellRendererState flags
)
617 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
618 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
621 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
627 rect
.x
+= cell_area
->x
;
628 rect
.y
+= cell_area
->y
;
629 rect
.width
-= renderer
->xpad
* 2;
630 rect
.height
-= renderer
->ypad
* 2;
633 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
635 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
636 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
637 if (dc
->m_window
== NULL
)
639 dc
->m_window
= window
;
644 if (flags
& GTK_CELL_RENDERER_SELECTED
)
645 state
|= wxDATAVIEW_CELL_SELECTED
;
646 if (flags
& GTK_CELL_RENDERER_PRELIT
)
647 state
|= wxDATAVIEW_CELL_PRELIT
;
648 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
649 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
650 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
651 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
652 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
653 state
|= wxDATAVIEW_CELL_FOCUSED
;
654 cell
->Render( renderrect
, dc
, state
);
659 gtk_wx_cell_renderer_activate(
660 GtkCellRenderer
*renderer
,
664 GdkRectangle
*background_area
,
665 GdkRectangle
*cell_area
,
666 GtkCellRendererState flags
)
668 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
669 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
672 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
678 rect
.x
+= cell_area
->x
;
679 rect
.y
+= cell_area
->y
;
680 rect
.width
-= renderer
->xpad
* 2;
681 rect
.height
-= renderer
->ypad
* 2;
683 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
685 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
687 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
688 size_t model_row
= (size_t)gtk_tree_path_get_indices (treepath
)[0];
689 gtk_tree_path_free( treepath
);
691 size_t model_col
= cell
->GetOwner()->GetModelColumn();
693 if (event
->type
== GDK_BUTTON_PRESS
)
695 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
696 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
697 ((int) button_event
->y
) - renderrect
.y
);
700 if (button_event
->button
== 1)
702 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
704 // TODO: query system double-click time
705 if (button_event
->time
- wxrenderer
->last_click
< 400)
706 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
709 if (button_event
->button
== 3)
711 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
715 wxrenderer
->last_click
= button_event
->time
;
723 // ---------------------------------------------------------
724 // wxGtkDataViewListModelNotifier
725 // ---------------------------------------------------------
727 class wxGtkDataViewListModelNotifier
: public wxDataViewListModelNotifier
730 wxGtkDataViewListModelNotifier( GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
);
732 virtual bool RowAppended();
733 virtual bool RowPrepended();
734 virtual bool RowInserted( size_t before
);
735 virtual bool RowDeleted( size_t row
);
736 virtual bool RowChanged( size_t row
);
737 virtual bool ValueChanged( size_t col
, size_t row
);
738 virtual bool Cleared();
740 GtkWxListStore
*m_gtk_store
;
741 wxDataViewListModel
*m_wx_model
;
744 // ---------------------------------------------------------
745 // wxGtkDataViewListModelNotifier
746 // ---------------------------------------------------------
748 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
749 GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
)
751 m_gtk_store
= gtk_store
;
752 m_wx_model
= wx_model
;
755 bool wxGtkDataViewListModelNotifier::RowAppended()
757 size_t pos
= m_wx_model
->GetNumberOfRows()-1;
760 iter
.stamp
= m_gtk_store
->stamp
;
761 iter
.user_data
= (gpointer
) pos
;
763 GtkTreePath
*path
= gtk_tree_path_new ();
764 gtk_tree_path_append_index (path
, (gint
) pos
);
765 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
766 gtk_tree_path_free (path
);
771 bool wxGtkDataViewListModelNotifier::RowPrepended()
774 iter
.stamp
= m_gtk_store
->stamp
;
775 iter
.user_data
= (gpointer
) 0;
777 GtkTreePath
*path
= gtk_tree_path_new ();
778 gtk_tree_path_append_index (path
, (gint
) 0);
779 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
780 gtk_tree_path_free (path
);
785 bool wxGtkDataViewListModelNotifier::RowInserted( size_t before
)
790 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row
)
795 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row
)
798 iter
.stamp
= m_gtk_store
->stamp
;
799 iter
.user_data
= (gpointer
) row
;
800 GtkTreePath
*path
= gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store
), &iter
);
801 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
802 gtk_tree_path_free (path
);
807 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col
, size_t model_row
)
809 // This adds GTK+'s missing MVC logic for SetValue
810 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
813 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
814 if (viewing_column
->m_modelColumn
== model_col
)
816 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
817 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(viewing_column
->m_viewColumn
->GetGtkHandle());
820 GtkTreePath
*path
= gtk_tree_path_new();
821 gtk_tree_path_append_index( path
, model_row
);
822 GdkRectangle cell_area
;
823 gtk_tree_view_get_cell_area( widget
, path
, column
, &cell_area
);
824 gtk_tree_path_free( path
);
826 int ydiff
= column
->button
->allocation
.height
;
828 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
829 cell_area
.x
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
832 node
= node
->GetNext();
838 bool wxGtkDataViewListModelNotifier::Cleared()
843 // ---------------------------------------------------------
845 // ---------------------------------------------------------
847 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
849 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
850 wxDataViewCellBase( varianttype
, mode
)
855 // ---------------------------------------------------------
856 // wxDataViewTextCell
857 // ---------------------------------------------------------
860 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
861 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
864 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
865 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
867 wxDataViewTextCell
*cell
= (wxDataViewTextCell
*) user_data
;
869 wxString tmp
= wxGTK_CONV_BACK( arg2
);
870 wxVariant value
= tmp
;
871 if (!cell
->Validate( value
))
874 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
876 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
877 size_t model_row
= (size_t)gtk_tree_path_get_indices (path
)[0];
878 gtk_tree_path_free( path
);
880 size_t model_col
= cell
->GetOwner()->GetModelColumn();
882 model
->SetValue( value
, model_col
, model_row
);
883 model
->ValueChanged( model_col
, model_row
);
886 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCell
)
888 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
889 wxDataViewCell( varianttype
, mode
)
891 m_renderer
= (void*) gtk_cell_renderer_text_new();
893 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
895 GValue gvalue
= { 0, };
896 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
897 g_value_set_boolean( &gvalue
, true );
898 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
899 g_value_unset( &gvalue
);
901 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
905 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
907 wxString tmp
= value
;
909 GValue gvalue
= { 0, };
910 g_value_init( &gvalue
, G_TYPE_STRING
);
911 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
912 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
913 g_value_unset( &gvalue
);
918 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
920 GValue gvalue
= { 0, };
921 g_value_init( &gvalue
, G_TYPE_STRING
);
922 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
923 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
924 g_value_unset( &gvalue
);
931 // ---------------------------------------------------------
932 // wxDataViewToggleCell
933 // ---------------------------------------------------------
936 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
937 gchar
*path
, gpointer user_data
);
940 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
941 gchar
*path
, gpointer user_data
)
943 wxDataViewToggleCell
*cell
= (wxDataViewToggleCell
*) user_data
;
946 GValue gvalue
= { 0, };
947 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
948 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
949 bool tmp
= g_value_get_boolean( &gvalue
);
950 g_value_unset( &gvalue
);
954 wxVariant value
= tmp
;
955 if (!cell
->Validate( value
))
958 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
960 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
961 size_t model_row
= (size_t)gtk_tree_path_get_indices (gtk_path
)[0];
962 gtk_tree_path_free( gtk_path
);
964 size_t model_col
= cell
->GetOwner()->GetModelColumn();
966 model
->SetValue( value
, model_col
, model_row
);
967 model
->ValueChanged( model_col
, model_row
);
970 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCell
)
972 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
973 wxDataViewCellMode mode
) :
974 wxDataViewCell( varianttype
, mode
)
976 m_renderer
= (void*) gtk_cell_renderer_toggle_new();
978 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
980 g_signal_connect_after( m_renderer
, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
985 GValue gvalue
= { 0, };
986 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
987 g_value_set_boolean( &gvalue
, false );
988 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
989 g_value_unset( &gvalue
);
991 GValue gvalue2
= { 0, };
992 g_value_init( &gvalue2
, gtk_cell_renderer_mode_get_type() );
993 g_value_set_enum( &gvalue2
, GTK_CELL_RENDERER_MODE_INERT
);
994 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue2
);
995 g_value_unset( &gvalue2
);
1000 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
1004 GValue gvalue
= { 0, };
1005 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1006 g_value_set_boolean( &gvalue
, tmp
);
1007 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1008 g_value_unset( &gvalue
);
1013 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
1015 GValue gvalue
= { 0, };
1016 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1017 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1018 bool tmp
= g_value_get_boolean( &gvalue
);
1019 g_value_unset( &gvalue
);
1026 // ---------------------------------------------------------
1027 // wxDataViewCustomCell
1028 // ---------------------------------------------------------
1030 class wxDataViewCtrlDC
: public wxWindowDC
1033 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1035 GtkWidget
*widget
= window
->m_treeview
;
1039 m_context
= window
->GtkGetPangoDefaultContext();
1040 m_layout
= pango_layout_new( m_context
);
1041 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1043 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1045 // Set m_window later
1047 // m_owner = window;
1051 // ---------------------------------------------------------
1052 // wxDataViewCustomCell
1053 // ---------------------------------------------------------
1055 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
1057 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
1058 wxDataViewCellMode mode
, bool no_init
) :
1059 wxDataViewCell( varianttype
, mode
)
1069 bool wxDataViewCustomCell::Init()
1071 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1072 renderer
->cell
= this;
1074 m_renderer
= (void*) renderer
;
1076 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1078 GValue gvalue
= { 0, };
1079 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1080 g_value_set_enum( &gvalue
, GTK_CELL_RENDERER_MODE_ACTIVATABLE
);
1081 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1082 g_value_unset( &gvalue
);
1088 wxDataViewCustomCell::~wxDataViewCustomCell()
1094 wxDC
*wxDataViewCustomCell::GetDC()
1098 if (GetOwner() == NULL
)
1100 if (GetOwner()->GetOwner() == NULL
)
1102 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1108 // ---------------------------------------------------------
1109 // wxDataViewProgressCell
1110 // ---------------------------------------------------------
1112 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
1114 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
1115 const wxString
&varianttype
, wxDataViewCellMode mode
) :
1116 wxDataViewCustomCell( varianttype
, mode
, true )
1122 if (!gtk_check_version(2,6,0))
1124 m_renderer
= (void*) gtk_cell_renderer_progress_new();
1126 GValue gvalue
= { 0, };
1127 g_value_init( &gvalue
, G_TYPE_STRING
);
1128 g_value_set_boolean( &gvalue
, wxGTK_CONV(m_label
) );
1129 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1130 g_value_unset( &gvalue
);
1135 // Use custom cell code
1136 wxDataViewCustomCell::Init();
1140 wxDataViewProgressCell::~wxDataViewProgressCell()
1144 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
1147 if (!gtk_check_version(2,6,0))
1149 gint tmp
= (int) value
;
1150 GValue gvalue
= { 0, };
1151 g_value_init( &gvalue
, G_TYPE_INT
);
1152 g_value_set_boolean( &gvalue
, tmp
);
1153 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1154 g_value_unset( &gvalue
);
1159 m_value
= (long) value
;
1161 if (m_value
< 0) m_value
= 0;
1162 if (m_value
> 100) m_value
= 100;
1168 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1170 double pct
= (double)m_value
/ 100.0;
1172 bar
.width
= (int)(cell
.width
* pct
);
1173 dc
->SetPen( *wxTRANSPARENT_PEN
);
1174 dc
->SetBrush( *wxBLUE_BRUSH
);
1175 dc
->DrawRectangle( bar
);
1177 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1178 dc
->SetPen( *wxBLACK_PEN
);
1179 dc
->DrawRectangle( cell
);
1184 wxSize
wxDataViewProgressCell::GetSize()
1186 return wxSize(40,12);
1189 // ---------------------------------------------------------
1190 // wxDataViewDateCell
1191 // ---------------------------------------------------------
1193 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
1196 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1197 wxDataViewListModel
*model
, size_t col
, size_t row
) :
1198 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1203 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1204 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1205 sizer
->Add( m_cal
, 1, wxGROW
);
1210 virtual void OnDismiss()
1214 void OnCalendar( wxCalendarEvent
&event
);
1216 wxCalendarCtrl
*m_cal
;
1217 wxDataViewListModel
*m_model
;
1222 DECLARE_EVENT_TABLE()
1225 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
1226 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
1229 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1231 wxDateTime date
= event
.GetDate();
1232 wxVariant value
= date
;
1233 m_model
->SetValue( value
, m_col
, m_row
);
1234 m_model
->ValueChanged( m_col
, m_row
);
1238 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
1240 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
1241 wxDataViewCellMode mode
) :
1242 wxDataViewCustomCell( varianttype
, mode
)
1246 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
1248 m_date
= value
.GetDateTime();
1253 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1255 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1256 wxString tmp
= m_date
.FormatDate();
1257 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1262 wxSize
wxDataViewDateCell::GetSize()
1264 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
1265 wxString tmp
= m_date
.FormatDate();
1267 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1268 return wxSize(x
,y
+d
);
1271 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
1273 wxVariant variant
= model
->GetValue( col
, row
);
1274 wxDateTime value
= variant
.GetDateTime();
1276 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
1277 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1278 wxPoint pos
= wxGetMousePosition();
1281 popup
->Popup( popup
->m_cal
);
1286 // ---------------------------------------------------------
1288 // ---------------------------------------------------------
1291 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1292 GtkCellRenderer
*cell
,
1293 GtkTreeModel
*model
,
1299 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1300 GtkCellRenderer
*renderer
,
1301 GtkTreeModel
*model
,
1305 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1306 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1308 wxDataViewCell
*cell
= (wxDataViewCell
*) data
;
1310 size_t model_row
= (size_t) iter
->user_data
;
1312 wxVariant value
= list_store
->model
->GetValue(
1313 cell
->GetOwner()->GetModelColumn(), model_row
);
1315 if (value
.GetType() != cell
->GetVariantType())
1316 wxPrintf( wxT("Wrong type\n") );
1318 cell
->SetValue( value
);
1321 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1323 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
1324 size_t model_column
, int flags
) :
1325 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
1327 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) cell
->GetGtkHandle();
1329 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1331 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1333 gtk_tree_view_column_pack_start( column
, renderer
, TRUE
);
1335 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1336 wxGtkTreeCellDataFunc
, (gpointer
) cell
, NULL
);
1338 m_column
= (void*) column
;
1341 wxDataViewColumn::~wxDataViewColumn()
1345 void wxDataViewColumn::SetTitle( const wxString
&title
)
1347 wxDataViewColumnBase::SetTitle( title
);
1349 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)m_column
;
1350 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1353 //-----------------------------------------------------------------------------
1355 //-----------------------------------------------------------------------------
1357 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1359 wxDataViewCtrl::~wxDataViewCtrl()
1362 GetModel()->RemoveNotifier( m_notifier
);
1365 void wxDataViewCtrl::Init()
1370 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1371 const wxPoint
& pos
, const wxSize
& size
,
1372 long style
, const wxValidator
& validator
)
1376 m_needParent
= TRUE
;
1377 m_acceptsFocus
= TRUE
;
1379 if (!PreCreation( parent
, pos
, size
) ||
1380 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1382 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1386 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1387 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (m_widget
), GTK_SHADOW_IN
);
1389 m_treeview
= gtk_tree_view_new();
1390 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1392 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1393 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1394 gtk_widget_show (m_treeview
);
1396 m_parent
->DoAddChild( this );
1403 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1405 if (!wxDataViewCtrlBase::AssociateModel( model
))
1408 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1409 gtk_store
->model
= model
;
1411 m_notifier
= new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1413 model
->AddNotifier( m_notifier
);
1415 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1416 g_object_unref( gtk_store
);
1421 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1423 if (!wxDataViewCtrlBase::AppendColumn(col
))
1426 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1428 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1434 #endif // wxUSE_DATAVIEWCTRL