1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dataview.cpp
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"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifndef wxUSE_GENERICDATAVIEWCTRL
19 #include "wx/stockitem.h"
20 #include "wx/dcclient.h"
21 #include "wx/calctrl.h"
22 #include "wx/popupwin.h"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/win_gtk.h"
29 #include <gobject/gvaluecollector.h>
30 #include <gtk/gtktreemodel.h>
31 #include <gtk/gtktreednd.h>
33 #include <gdk/gdkkeysyms.h>
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern bool g_blockEventsOnDrag
;
47 //-----------------------------------------------------------------------------
48 // define new GTK+ class wxGtkListStore
49 //-----------------------------------------------------------------------------
53 #define GTK_TYPE_WX_LIST_STORE (gtk_wx_list_store_get_type ())
54 #define GTK_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStore))
55 #define GTK_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
56 #define GTK_IS_WX_LIST_STORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_LIST_STORE))
57 #define GTK_IS_WX_LIST_STORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_LIST_STORE))
58 #define GTK_WX_LIST_STORE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_LIST_STORE, GtkWxListStoreClass))
60 GType
gtk_wx_list_store_get_type (void);
62 typedef struct _GtkWxListStore GtkWxListStore
;
63 typedef struct _GtkWxListStoreClass GtkWxListStoreClass
;
65 struct _GtkWxListStore
71 wxDataViewListModel
*model
;
74 struct _GtkWxListStoreClass
76 GObjectClass list_parent_class
;
79 static GtkWxListStore
*wxgtk_list_store_new (void);
80 static void wxgtk_list_store_init (GtkWxListStore
*list_store
);
81 static void wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
);
82 static void wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
);
83 static void wxgtk_list_store_finalize (GObject
*object
);
84 static GtkTreeModelFlags
wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
);
85 static gint
wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
);
86 static GType
wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
88 static gboolean
wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
91 static GtkTreePath
*wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
93 static void wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
97 static gboolean
wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
99 static gboolean
wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
101 GtkTreeIter
*parent
);
102 static gboolean
wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
104 static gint
wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
106 static gboolean
wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
110 static gboolean
wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
114 static GObjectClass
*list_parent_class
= NULL
;
117 gtk_wx_list_store_get_type (void)
119 static GType list_store_type
= 0;
121 if (!list_store_type
)
123 static const GTypeInfo list_store_info
=
125 sizeof (GtkWxListStoreClass
),
126 NULL
, /* base_init */
127 NULL
, /* base_finalize */
128 (GClassInitFunc
) wxgtk_list_store_class_init
,
129 NULL
, /* class_finalize */
130 NULL
, /* class_data */
131 sizeof (GtkWxListStore
),
133 (GInstanceInitFunc
) wxgtk_list_store_init
,
136 static const GInterfaceInfo tree_model_info
=
138 (GInterfaceInitFunc
) wxgtk_list_store_tree_model_init
,
143 list_store_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkWxListStore",
144 &list_store_info
, (GTypeFlags
)0 );
146 g_type_add_interface_static (list_store_type
,
151 return list_store_type
;
154 static GtkWxListStore
*
155 wxgtk_list_store_new(void)
157 GtkWxListStore
*retval
= (GtkWxListStore
*) g_object_new (GTK_TYPE_WX_LIST_STORE
, NULL
);
162 wxgtk_list_store_class_init (GtkWxListStoreClass
*klass
)
164 list_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
165 GObjectClass
*object_class
= (GObjectClass
*) klass
;
166 object_class
->finalize
= wxgtk_list_store_finalize
;
170 wxgtk_list_store_tree_model_init (GtkTreeModelIface
*iface
)
172 iface
->get_flags
= wxgtk_list_store_get_flags
;
173 iface
->get_n_columns
= wxgtk_list_store_get_n_columns
;
174 iface
->get_column_type
= wxgtk_list_store_get_column_type
;
175 iface
->get_iter
= wxgtk_list_store_get_iter
;
176 iface
->get_path
= wxgtk_list_store_get_path
;
177 iface
->get_value
= wxgtk_list_store_get_value
;
178 iface
->iter_next
= wxgtk_list_store_iter_next
;
179 iface
->iter_children
= wxgtk_list_store_iter_children
;
180 iface
->iter_has_child
= wxgtk_list_store_iter_has_child
;
181 iface
->iter_n_children
= wxgtk_list_store_iter_n_children
;
182 iface
->iter_nth_child
= wxgtk_list_store_iter_nth_child
;
183 iface
->iter_parent
= wxgtk_list_store_iter_parent
;
187 wxgtk_list_store_init (GtkWxListStore
*list_store
)
189 list_store
->model
= NULL
;
190 list_store
->stamp
= g_random_int();
194 wxgtk_list_store_finalize (GObject
*object
)
196 /* GtkWxListStore *list_store = GTK_WX_LIST_STORE (object); */
198 /* we need to sort out, which class deletes what */
199 /* delete list_store->model; */
202 (* list_parent_class
->finalize
) (object
);
207 //-----------------------------------------------------------------------------
208 // implement callbacks from wxGtkListStore class by letting
209 // them call the methods of wxWidgets' wxDataViewListModel
210 //-----------------------------------------------------------------------------
212 static GtkTreeModelFlags
213 wxgtk_list_store_get_flags (GtkTreeModel
*tree_model
)
215 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), (GtkTreeModelFlags
)0 );
217 // GTK+ list store uses a linked list for storing the
218 // items and a pointer to a child is used as the member
219 // field of a GtkTreeIter. This means that the iter is
220 // valid in the GtkListStore as long as the child exists.
221 // We use the index of the row and since the index of a
222 // specific row will change if a row above is deleted,
223 // the iter does not persist
224 return /* GTK_TREE_MODEL_ITERS_PERSIST | */ GTK_TREE_MODEL_LIST_ONLY
;
228 wxgtk_list_store_get_n_columns (GtkTreeModel
*tree_model
)
230 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
231 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), 0);
233 return list_store
->model
->GetNumberOfCols();
237 wxgtk_list_store_get_column_type (GtkTreeModel
*tree_model
,
240 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
241 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), G_TYPE_INVALID
);
243 GType gtype
= G_TYPE_INVALID
;
245 wxString wxtype
= list_store
->model
->GetColType( (size_t) index
);
247 if (wxtype
== wxT("string"))
248 gtype
= G_TYPE_STRING
;
254 wxgtk_list_store_get_iter (GtkTreeModel
*tree_model
,
258 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
259 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
260 g_return_val_if_fail (gtk_tree_path_get_depth (path
) > 0, FALSE
);
262 size_t i
= (size_t)gtk_tree_path_get_indices (path
)[0];
264 if (i
>= list_store
->model
->GetNumberOfRows())
267 iter
->stamp
= list_store
->stamp
;
268 // user_data is just the index
269 iter
->user_data
= (gpointer
) i
;
275 wxgtk_list_store_get_path (GtkTreeModel
*tree_model
,
278 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), NULL
);
279 g_return_val_if_fail (iter
->stamp
== GTK_WX_LIST_STORE (tree_model
)->stamp
, NULL
);
281 GtkTreePath
*retval
= gtk_tree_path_new ();
282 // user_data is just the index
283 int i
= (wxUIntPtr
) iter
->user_data
;
284 gtk_tree_path_append_index (retval
, i
);
289 wxgtk_list_store_get_value (GtkTreeModel
*tree_model
,
294 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
295 g_return_if_fail (GTK_IS_WX_LIST_STORE (tree_model
) );
297 wxDataViewListModel
*model
= list_store
->model
;
298 wxString mtype
= model
->GetColType( (size_t) column
);
299 if (mtype
== wxT("string"))
302 g_value_init( value
, G_TYPE_STRING
);
303 model
->GetValue( variant
, (size_t) column
, (size_t) iter
->user_data
);
304 g_value_set_string( value
, wxGTK_CONV(variant
.GetString()) );
311 GtkTreeDataList
*list
;
312 gint tmp_column
= column
;
314 g_return_if_fail (column
< GTK_LIST_STORE (tree_model
)->n_columns
);
315 g_return_if_fail (GTK_LIST_STORE (tree_model
)->stamp
== iter
->stamp
);
317 list
= G_SLIST (iter
->user_data
)->data
;
319 while (tmp_column
-- > 0 && list
)
323 g_value_init (value
, GTK_LIST_STORE (tree_model
)->column_headers
[column
]);
325 _gtk_tree_data_list_node_to_value (list
,
326 GTK_LIST_STORE (tree_model
)->column_headers
[column
],
333 wxgtk_list_store_iter_next (GtkTreeModel
*tree_model
,
336 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
337 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
339 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, FALSE
);
341 int n
= (wxUIntPtr
) iter
->user_data
;
346 if (n
>= (int) list_store
->model
->GetNumberOfRows()-1)
349 iter
->user_data
= (gpointer
) ++n
;
355 wxgtk_list_store_iter_children (GtkTreeModel
*tree_model
,
359 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
360 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
362 // this is a list, nodes have no children
366 iter
->stamp
= list_store
->stamp
;
367 iter
->user_data
= (gpointer
) -1;
373 wxgtk_list_store_iter_has_child (GtkTreeModel
*tree_model
,
380 wxgtk_list_store_iter_n_children (GtkTreeModel
*tree_model
,
383 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), -1);
384 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
387 return (gint
) list_store
->model
->GetNumberOfRows();
389 g_return_val_if_fail (list_store
->stamp
== iter
->stamp
, -1);
395 wxgtk_list_store_iter_nth_child (GtkTreeModel
*tree_model
,
400 g_return_val_if_fail (GTK_IS_WX_LIST_STORE (tree_model
), FALSE
);
401 GtkWxListStore
*list_store
= (GtkWxListStore
*) tree_model
;
409 if (n
>= (gint
) list_store
->model
->GetNumberOfRows())
412 iter
->stamp
= list_store
->stamp
;
413 iter
->user_data
= (gpointer
) n
;
419 wxgtk_list_store_iter_parent (GtkTreeModel
*tree_model
,
426 //-----------------------------------------------------------------------------
427 // define new GTK+ class wxGtkCellRenderer
428 //-----------------------------------------------------------------------------
432 #define GTK_TYPE_WX_CELL_RENDERER (gtk_wx_cell_renderer_get_type ())
433 #define GTK_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRenderer))
434 #define GTK_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
435 #define GTK_IS_WX_CELL_RENDERER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_WX_CELL_RENDERER))
436 #define GTK_IS_WX_CELL_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_WX_CELL_RENDERER))
437 #define GTK_WX_CELL_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_WX_CELL_RENDERER, GtkWxCellRendererClass))
439 GType
gtk_wx_cell_renderer_get_type (void);
441 typedef struct _GtkWxCellRenderer GtkWxCellRenderer
;
442 typedef struct _GtkWxCellRendererClass GtkWxCellRendererClass
;
444 struct _GtkWxCellRenderer
446 GtkCellRenderer parent
;
449 wxDataViewCustomCell
*cell
;
453 struct _GtkWxCellRendererClass
455 GtkCellRendererClass cell_parent_class
;
459 static GtkCellRenderer
*gtk_wx_cell_renderer_new (void);
460 static void gtk_wx_cell_renderer_init (
461 GtkWxCellRenderer
*cell
);
462 static void gtk_wx_cell_renderer_class_init(
463 GtkWxCellRendererClass
*klass
);
464 static void gtk_wx_cell_renderer_finalize (
466 static void gtk_wx_cell_renderer_get_size (
467 GtkCellRenderer
*cell
,
469 GdkRectangle
*rectangle
,
474 static void gtk_wx_cell_renderer_render (
475 GtkCellRenderer
*cell
,
478 GdkRectangle
*background_area
,
479 GdkRectangle
*cell_area
,
480 GdkRectangle
*expose_area
,
481 GtkCellRendererState flags
);
482 static gboolean
gtk_wx_cell_renderer_activate(
483 GtkCellRenderer
*cell
,
487 GdkRectangle
*background_area
,
488 GdkRectangle
*cell_area
,
489 GtkCellRendererState flags
);
491 static GObjectClass
*cell_parent_class
= NULL
;
496 gtk_wx_cell_renderer_get_type (void)
498 static GType cell_wx_type
= 0;
502 static const GTypeInfo cell_wx_info
=
504 sizeof (GtkWxCellRendererClass
),
505 NULL
, /* base_init */
506 NULL
, /* base_finalize */
507 (GClassInitFunc
) gtk_wx_cell_renderer_class_init
,
508 NULL
, /* class_finalize */
509 NULL
, /* class_data */
510 sizeof (GtkWxCellRenderer
),
512 (GInstanceInitFunc
) gtk_wx_cell_renderer_init
,
515 cell_wx_type
= g_type_register_static( GTK_TYPE_CELL_RENDERER
,
516 "GtkWxCellRenderer", &cell_wx_info
, (GTypeFlags
)0 );
523 gtk_wx_cell_renderer_init (GtkWxCellRenderer
*cell
)
526 cell
->last_click
= 0;
530 gtk_wx_cell_renderer_class_init (GtkWxCellRendererClass
*klass
)
532 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
533 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS (klass
);
535 cell_parent_class
= (GObjectClass
*) g_type_class_peek_parent (klass
);
537 object_class
->finalize
= gtk_wx_cell_renderer_finalize
;
539 cell_class
->get_size
= gtk_wx_cell_renderer_get_size
;
540 cell_class
->render
= gtk_wx_cell_renderer_render
;
541 cell_class
->activate
= gtk_wx_cell_renderer_activate
;
545 gtk_wx_cell_renderer_finalize (GObject
*object
)
548 (* G_OBJECT_CLASS (cell_parent_class
)->finalize
) (object
);
552 gtk_wx_cell_renderer_new (void)
554 return (GtkCellRenderer
*) g_object_new (GTK_TYPE_WX_CELL_RENDERER
, NULL
);
558 gtk_wx_cell_renderer_get_size (GtkCellRenderer
*renderer
,
560 GdkRectangle
*cell_area
,
566 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
567 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
569 wxSize size
= cell
->GetSize();
571 gint calc_width
= (gint
) renderer
->xpad
* 2 + size
.x
;
572 gint calc_height
= (gint
) renderer
->ypad
* 2 + size
.y
;
579 if (cell_area
&& size
.x
> 0 && size
.y
> 0)
583 *x_offset
= (gint
)((renderer
->xalign
*
584 (cell_area
->width
- calc_width
- 2 * renderer
->xpad
)));
585 *x_offset
= MAX (*x_offset
, 0) + renderer
->xpad
;
589 *y_offset
= (gint
)((renderer
->yalign
*
590 (cell_area
->height
- calc_height
- 2 * renderer
->ypad
)));
591 *y_offset
= MAX (*y_offset
, 0) + renderer
->ypad
;
599 *height
= calc_height
;
603 gtk_wx_cell_renderer_render (GtkCellRenderer
*renderer
,
606 GdkRectangle
*background_area
,
607 GdkRectangle
*cell_area
,
608 GdkRectangle
*expose_area
,
609 GtkCellRendererState flags
)
612 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
613 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
616 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
622 rect
.x
+= cell_area
->x
;
623 rect
.y
+= cell_area
->y
;
624 rect
.width
-= renderer
->xpad
* 2;
625 rect
.height
-= renderer
->ypad
* 2;
628 if (gdk_rectangle_intersect (expose_area
, &rect
, &dummy
))
630 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
631 wxWindowDC
* dc
= (wxWindowDC
*) cell
->GetDC();
632 if (dc
->m_window
== NULL
)
634 dc
->m_window
= window
;
639 if (flags
& GTK_CELL_RENDERER_SELECTED
)
640 state
|= wxDATAVIEW_CELL_SELECTED
;
641 if (flags
& GTK_CELL_RENDERER_PRELIT
)
642 state
|= wxDATAVIEW_CELL_PRELIT
;
643 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
644 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
645 if (flags
& GTK_CELL_RENDERER_INSENSITIVE
)
646 state
|= wxDATAVIEW_CELL_INSENSITIVE
;
647 if (flags
& GTK_CELL_RENDERER_FOCUSED
)
648 state
|= wxDATAVIEW_CELL_FOCUSED
;
649 cell
->Render( renderrect
, dc
, state
);
654 gtk_wx_cell_renderer_activate(
655 GtkCellRenderer
*renderer
,
659 GdkRectangle
*background_area
,
660 GdkRectangle
*cell_area
,
661 GtkCellRendererState flags
)
663 GtkWxCellRenderer
*wxrenderer
= (GtkWxCellRenderer
*) renderer
;
664 wxDataViewCustomCell
*cell
= wxrenderer
->cell
;
667 gtk_wx_cell_renderer_get_size (renderer
, widget
, cell_area
,
673 rect
.x
+= cell_area
->x
;
674 rect
.y
+= cell_area
->y
;
675 rect
.width
-= renderer
->xpad
* 2;
676 rect
.height
-= renderer
->ypad
* 2;
678 wxRect
renderrect( rect
.x
, rect
.y
, rect
.width
, rect
.height
);
680 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
682 GtkTreePath
*treepath
= gtk_tree_path_new_from_string( path
);
683 size_t model_row
= (size_t)gtk_tree_path_get_indices (treepath
)[0];
684 gtk_tree_path_free( treepath
);
686 size_t model_col
= cell
->GetOwner()->GetModelColumn();
688 if (event
->type
== GDK_BUTTON_PRESS
)
690 GdkEventButton
*button_event
= (GdkEventButton
*) event
;
691 wxPoint
pt( ((int) button_event
->x
) - renderrect
.x
,
692 ((int) button_event
->y
) - renderrect
.y
);
695 if (button_event
->button
== 1)
697 if (cell
->LeftClick( pt
, renderrect
, model
, model_col
, model_row
))
699 // TODO: query system double-click time
700 if (button_event
->time
- wxrenderer
->last_click
< 400)
701 if (cell
->Activate( renderrect
, model
, model_col
, model_row
))
704 if (button_event
->button
== 3)
706 if (cell
->RightClick( pt
, renderrect
, model
, model_col
, model_row
))
710 wxrenderer
->last_click
= button_event
->time
;
718 // ---------------------------------------------------------
719 // wxGtkDataViewListModelNotifier
720 // ---------------------------------------------------------
722 class wxGtkDataViewListModelNotifier
: public wxDataViewListModelNotifier
725 wxGtkDataViewListModelNotifier( GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
);
727 virtual bool RowAppended();
728 virtual bool RowPrepended();
729 virtual bool RowInserted( size_t before
);
730 virtual bool RowDeleted( size_t row
);
731 virtual bool RowChanged( size_t row
);
732 virtual bool ValueChanged( size_t col
, size_t row
);
733 virtual bool RowsReordered( size_t *new_order
);
734 virtual bool Cleared();
736 GtkWxListStore
*m_gtk_store
;
737 wxDataViewListModel
*m_wx_model
;
740 // ---------------------------------------------------------
741 // wxGtkDataViewListModelNotifier
742 // ---------------------------------------------------------
744 wxGtkDataViewListModelNotifier::wxGtkDataViewListModelNotifier(
745 GtkWxListStore
* gtk_store
, wxDataViewListModel
*wx_model
)
747 m_gtk_store
= gtk_store
;
748 m_wx_model
= wx_model
;
751 bool wxGtkDataViewListModelNotifier::RowAppended()
753 size_t pos
= m_wx_model
->GetNumberOfRows()-1;
756 iter
.stamp
= m_gtk_store
->stamp
;
757 iter
.user_data
= (gpointer
) pos
;
759 GtkTreePath
*path
= gtk_tree_path_new ();
760 gtk_tree_path_append_index (path
, (gint
) pos
);
761 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
762 gtk_tree_path_free (path
);
767 bool wxGtkDataViewListModelNotifier::RowPrepended()
770 iter
.stamp
= m_gtk_store
->stamp
;
771 iter
.user_data
= (gpointer
) 0;
773 GtkTreePath
*path
= gtk_tree_path_new ();
774 gtk_tree_path_append_index (path
, (gint
) 0);
775 gtk_tree_model_row_inserted (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
776 gtk_tree_path_free (path
);
781 bool wxGtkDataViewListModelNotifier::RowInserted( size_t before
)
786 bool wxGtkDataViewListModelNotifier::RowDeleted( size_t row
)
791 bool wxGtkDataViewListModelNotifier::RowChanged( size_t row
)
794 iter
.stamp
= m_gtk_store
->stamp
;
795 iter
.user_data
= (gpointer
) row
;
796 GtkTreePath
*path
= gtk_tree_model_get_path (GTK_TREE_MODEL (m_gtk_store
), &iter
);
797 gtk_tree_model_row_changed (GTK_TREE_MODEL (m_gtk_store
), path
, &iter
);
798 gtk_tree_path_free (path
);
803 bool wxGtkDataViewListModelNotifier::ValueChanged( size_t model_col
, size_t model_row
)
805 // This adds GTK+'s missing MVC logic for ValueChanged
806 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
809 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
810 if (viewing_column
->m_modelColumn
== model_col
)
812 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
813 GtkTreeViewColumn
*column
= GTK_TREE_VIEW_COLUMN(viewing_column
->m_viewColumn
->GetGtkHandle());
816 GtkTreePath
*path
= gtk_tree_path_new();
817 gtk_tree_path_append_index( path
, model_row
);
818 GdkRectangle cell_area
;
819 gtk_tree_view_get_cell_area( widget
, path
, column
, &cell_area
);
820 gtk_tree_path_free( path
);
822 int ydiff
= column
->button
->allocation
.height
;
824 gtk_widget_queue_draw_area( GTK_WIDGET(widget
),
825 cell_area
.x
, ydiff
+ cell_area
.y
, cell_area
.width
, cell_area
.height
);
828 node
= node
->GetNext();
834 bool wxGtkDataViewListModelNotifier::RowsReordered( size_t *new_order
)
836 // Assume sizeof(size_t)= == sizeof(gint)
838 GtkTreePath
*path
= gtk_tree_path_new ();
839 gtk_tree_model_rows_reordered (GTK_TREE_MODEL (m_gtk_store
), path
, NULL
, (gint
*)new_order
);
840 gtk_tree_path_free (path
);
842 // This adds GTK+'s missing MVC logic for RowsReordered
843 wxNode
*node
= GetOwner()->m_viewingColumns
.GetFirst();
846 wxDataViewViewingColumn
* viewing_column
= (wxDataViewViewingColumn
*) node
->GetData();
847 GtkTreeView
*widget
= GTK_TREE_VIEW(viewing_column
->m_viewColumn
->GetOwner()->m_treeview
);
848 // Doesn't work yet...
849 gtk_widget_queue_draw( GTK_WIDGET(widget
) );
851 node
= node
->GetNext();
857 bool wxGtkDataViewListModelNotifier::Cleared()
862 // ---------------------------------------------------------
864 // ---------------------------------------------------------
866 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
868 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
869 wxDataViewCellBase( varianttype
, mode
)
874 // ---------------------------------------------------------
875 // wxDataViewTextCell
876 // ---------------------------------------------------------
879 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
880 gchar
*arg1
, gchar
*arg2
, gpointer user_data
);
883 static void wxGtkTextRendererEditedCallback( GtkCellRendererText
*renderer
,
884 gchar
*arg1
, gchar
*arg2
, gpointer user_data
)
886 wxDataViewTextCell
*cell
= (wxDataViewTextCell
*) user_data
;
888 wxString tmp
= wxGTK_CONV_BACK( arg2
);
889 wxVariant value
= tmp
;
890 if (!cell
->Validate( value
))
893 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
895 GtkTreePath
*path
= gtk_tree_path_new_from_string( arg1
);
896 size_t model_row
= (size_t)gtk_tree_path_get_indices (path
)[0];
897 gtk_tree_path_free( path
);
899 size_t model_col
= cell
->GetOwner()->GetModelColumn();
901 model
->SetValue( value
, model_col
, model_row
);
902 model
->ValueChanged( model_col
, model_row
);
905 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCell
)
907 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
908 wxDataViewCell( varianttype
, mode
)
910 m_renderer
= (void*) gtk_cell_renderer_text_new();
912 if (m_mode
& wxDATAVIEW_CELL_EDITABLE
)
914 GValue gvalue
= { 0, };
915 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
916 g_value_set_boolean( &gvalue
, true );
917 g_object_set_property( G_OBJECT(m_renderer
), "editable", &gvalue
);
918 g_value_unset( &gvalue
);
920 g_signal_connect_after( m_renderer
, "edited", G_CALLBACK(wxGtkTextRendererEditedCallback
), this );
924 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
926 wxString tmp
= value
;
928 GValue gvalue
= { 0, };
929 g_value_init( &gvalue
, G_TYPE_STRING
);
930 g_value_set_string( &gvalue
, wxGTK_CONV( tmp
) );
931 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
932 g_value_unset( &gvalue
);
937 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
939 GValue gvalue
= { 0, };
940 g_value_init( &gvalue
, G_TYPE_STRING
);
941 g_object_get_property( G_OBJECT(m_renderer
), "text", &gvalue
);
942 wxString tmp
= wxGTK_CONV_BACK( g_value_get_string( &gvalue
) );
943 g_value_unset( &gvalue
);
950 // ---------------------------------------------------------
951 // wxDataViewToggleCell
952 // ---------------------------------------------------------
955 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
956 gchar
*path
, gpointer user_data
);
959 static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle
*renderer
,
960 gchar
*path
, gpointer user_data
)
962 wxDataViewToggleCell
*cell
= (wxDataViewToggleCell
*) user_data
;
965 GValue gvalue
= { 0, };
966 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
967 g_object_get_property( G_OBJECT(renderer
), "active", &gvalue
);
968 bool tmp
= g_value_get_boolean( &gvalue
);
969 g_value_unset( &gvalue
);
973 wxVariant value
= tmp
;
974 if (!cell
->Validate( value
))
977 wxDataViewListModel
*model
= cell
->GetOwner()->GetOwner()->GetModel();
979 GtkTreePath
*gtk_path
= gtk_tree_path_new_from_string( path
);
980 size_t model_row
= (size_t)gtk_tree_path_get_indices (gtk_path
)[0];
981 gtk_tree_path_free( gtk_path
);
983 size_t model_col
= cell
->GetOwner()->GetModelColumn();
985 model
->SetValue( value
, model_col
, model_row
);
986 model
->ValueChanged( model_col
, model_row
);
989 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCell
)
991 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
992 wxDataViewCellMode mode
) :
993 wxDataViewCell( varianttype
, mode
)
995 m_renderer
= (void*) gtk_cell_renderer_toggle_new();
997 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
999 g_signal_connect_after( m_renderer
, "toggled", G_CALLBACK(wxGtkToggleRendererToggledCallback
), this );
1004 GValue gvalue
= { 0, };
1005 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1006 g_value_set_boolean( &gvalue
, false );
1007 g_object_set_property( G_OBJECT(m_renderer
), "activatable", &gvalue
);
1008 g_value_unset( &gvalue
);
1010 GValue gvalue2
= { 0, };
1011 g_value_init( &gvalue2
, gtk_cell_renderer_mode_get_type() );
1012 g_value_set_enum( &gvalue2
, GTK_CELL_RENDERER_MODE_INERT
);
1013 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue2
);
1014 g_value_unset( &gvalue2
);
1019 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
1023 GValue gvalue
= { 0, };
1024 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1025 g_value_set_boolean( &gvalue
, tmp
);
1026 g_object_set_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1027 g_value_unset( &gvalue
);
1032 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
1034 GValue gvalue
= { 0, };
1035 g_value_init( &gvalue
, G_TYPE_BOOLEAN
);
1036 g_object_get_property( G_OBJECT(m_renderer
), "active", &gvalue
);
1037 bool tmp
= g_value_get_boolean( &gvalue
);
1038 g_value_unset( &gvalue
);
1045 // ---------------------------------------------------------
1046 // wxDataViewCustomCell
1047 // ---------------------------------------------------------
1049 class wxDataViewCtrlDC
: public wxWindowDC
1052 wxDataViewCtrlDC( wxDataViewCtrl
*window
)
1054 GtkWidget
*widget
= window
->m_treeview
;
1058 m_context
= window
->GtkGetPangoDefaultContext();
1059 m_layout
= pango_layout_new( m_context
);
1060 m_fontdesc
= pango_font_description_copy( widget
->style
->font_desc
);
1062 m_cmap
= gtk_widget_get_colormap( widget
? widget
: window
->m_widget
);
1064 // Set m_window later
1066 // m_owner = window;
1070 // ---------------------------------------------------------
1071 // wxDataViewCustomCell
1072 // ---------------------------------------------------------
1074 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
1076 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
1077 wxDataViewCellMode mode
, bool no_init
) :
1078 wxDataViewCell( varianttype
, mode
)
1088 bool wxDataViewCustomCell::Init()
1090 GtkWxCellRenderer
*renderer
= (GtkWxCellRenderer
*) gtk_wx_cell_renderer_new();
1091 renderer
->cell
= this;
1093 m_renderer
= (void*) renderer
;
1095 if (m_mode
& wxDATAVIEW_CELL_ACTIVATABLE
)
1097 GValue gvalue
= { 0, };
1098 g_value_init( &gvalue
, gtk_cell_renderer_mode_get_type() );
1099 g_value_set_enum( &gvalue
, GTK_CELL_RENDERER_MODE_ACTIVATABLE
);
1100 g_object_set_property( G_OBJECT(m_renderer
), "mode", &gvalue
);
1101 g_value_unset( &gvalue
);
1107 wxDataViewCustomCell::~wxDataViewCustomCell()
1113 wxDC
*wxDataViewCustomCell::GetDC()
1117 if (GetOwner() == NULL
)
1119 if (GetOwner()->GetOwner() == NULL
)
1121 m_dc
= new wxDataViewCtrlDC( GetOwner()->GetOwner() );
1127 // ---------------------------------------------------------
1128 // wxDataViewProgressCell
1129 // ---------------------------------------------------------
1131 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
1133 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
1134 const wxString
&varianttype
, wxDataViewCellMode mode
) :
1135 wxDataViewCustomCell( varianttype
, mode
, true )
1141 if (!gtk_check_version(2,6,0))
1143 m_renderer
= (void*) gtk_cell_renderer_progress_new();
1145 GValue gvalue
= { 0, };
1146 g_value_init( &gvalue
, G_TYPE_STRING
);
1147 g_value_set_boolean( &gvalue
, wxGTK_CONV(m_label
) );
1148 g_object_set_property( G_OBJECT(m_renderer
), "text", &gvalue
);
1149 g_value_unset( &gvalue
);
1154 // Use custom cell code
1155 wxDataViewCustomCell::Init();
1159 wxDataViewProgressCell::~wxDataViewProgressCell()
1163 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
1166 if (!gtk_check_version(2,6,0))
1168 gint tmp
= (int) value
;
1169 GValue gvalue
= { 0, };
1170 g_value_init( &gvalue
, G_TYPE_INT
);
1171 g_value_set_boolean( &gvalue
, tmp
);
1172 g_object_set_property( G_OBJECT(m_renderer
), "value", &gvalue
);
1173 g_value_unset( &gvalue
);
1178 m_value
= (long) value
;
1180 if (m_value
< 0) m_value
= 0;
1181 if (m_value
> 100) m_value
= 100;
1187 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1189 double pct
= (double)m_value
/ 100.0;
1191 bar
.width
= (int)(cell
.width
* pct
);
1192 dc
->SetPen( *wxTRANSPARENT_PEN
);
1193 dc
->SetBrush( *wxBLUE_BRUSH
);
1194 dc
->DrawRectangle( bar
);
1196 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1197 dc
->SetPen( *wxBLACK_PEN
);
1198 dc
->DrawRectangle( cell
);
1203 wxSize
wxDataViewProgressCell::GetSize()
1205 return wxSize(40,12);
1208 // ---------------------------------------------------------
1209 // wxDataViewDateCell
1210 // ---------------------------------------------------------
1212 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
1215 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
1216 wxDataViewListModel
*model
, size_t col
, size_t row
) :
1217 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
1222 m_cal
= new wxCalendarCtrl( this, -1, *value
);
1223 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
1224 sizer
->Add( m_cal
, 1, wxGROW
);
1229 virtual void OnDismiss()
1233 void OnCalendar( wxCalendarEvent
&event
);
1235 wxCalendarCtrl
*m_cal
;
1236 wxDataViewListModel
*m_model
;
1241 DECLARE_EVENT_TABLE()
1244 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
1245 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
1248 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1250 wxDateTime date
= event
.GetDate();
1251 wxVariant value
= date
;
1252 m_model
->SetValue( value
, m_col
, m_row
);
1253 m_model
->ValueChanged( m_col
, m_row
);
1257 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
1259 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
1260 wxDataViewCellMode mode
) :
1261 wxDataViewCustomCell( varianttype
, mode
)
1265 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
1267 m_date
= value
.GetDateTime();
1272 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
1274 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
1275 wxString tmp
= m_date
.FormatDate();
1276 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
1281 wxSize
wxDataViewDateCell::GetSize()
1283 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
1284 wxString tmp
= m_date
.FormatDate();
1286 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1287 return wxSize(x
,y
+d
);
1290 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
1293 model
->GetValue( variant
, col
, row
);
1294 wxDateTime value
= variant
.GetDateTime();
1296 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
1297 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
1298 wxPoint pos
= wxGetMousePosition();
1301 popup
->Popup( popup
->m_cal
);
1306 // ---------------------------------------------------------
1308 // ---------------------------------------------------------
1311 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1312 GtkCellRenderer
*cell
,
1313 GtkTreeModel
*model
,
1319 static void wxGtkTreeCellDataFunc( GtkTreeViewColumn
*column
,
1320 GtkCellRenderer
*renderer
,
1321 GtkTreeModel
*model
,
1325 g_return_if_fail (GTK_IS_WX_LIST_STORE (model
));
1326 GtkWxListStore
*list_store
= (GtkWxListStore
*) model
;
1328 wxDataViewCell
*cell
= (wxDataViewCell
*) data
;
1330 size_t model_row
= (size_t) iter
->user_data
;
1333 list_store
->model
->GetValue( value
, cell
->GetOwner()->GetModelColumn(), model_row
);
1335 if (value
.GetType() != cell
->GetVariantType())
1336 wxLogError( wxT("Wrong type\n") );
1338 cell
->SetValue( value
);
1341 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1343 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
1344 size_t model_column
, int flags
) :
1345 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
1347 GtkCellRenderer
*renderer
= (GtkCellRenderer
*) cell
->GetGtkHandle();
1349 GtkTreeViewColumn
*column
= gtk_tree_view_column_new();
1351 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1353 gtk_tree_view_column_pack_start( column
, renderer
, TRUE
);
1355 gtk_tree_view_column_set_cell_data_func( column
, renderer
,
1356 wxGtkTreeCellDataFunc
, (gpointer
) cell
, NULL
);
1358 m_column
= (void*) column
;
1361 wxDataViewColumn::~wxDataViewColumn()
1365 void wxDataViewColumn::SetTitle( const wxString
&title
)
1367 wxDataViewColumnBase::SetTitle( title
);
1369 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)m_column
;
1370 gtk_tree_view_column_set_title( column
, wxGTK_CONV(title
) );
1373 //-----------------------------------------------------------------------------
1375 //-----------------------------------------------------------------------------
1377 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1379 wxDataViewCtrl::~wxDataViewCtrl()
1382 GetModel()->RemoveNotifier( m_notifier
);
1385 void wxDataViewCtrl::Init()
1390 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1391 const wxPoint
& pos
, const wxSize
& size
,
1392 long style
, const wxValidator
& validator
)
1396 m_needParent
= true;
1397 m_acceptsFocus
= true;
1399 if (!PreCreation( parent
, pos
, size
) ||
1400 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
1402 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
1406 m_widget
= gtk_scrolled_window_new (NULL
, NULL
);
1408 GtkScrolledWindowSetBorder(m_widget
, style
);
1410 m_treeview
= gtk_tree_view_new();
1411 gtk_container_add (GTK_CONTAINER (m_widget
), m_treeview
);
1413 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_widget
),
1414 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
1415 gtk_widget_show (m_treeview
);
1417 m_parent
->DoAddChild( this );
1424 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1426 if (!wxDataViewCtrlBase::AssociateModel( model
))
1429 GtkWxListStore
*gtk_store
= wxgtk_list_store_new();
1430 gtk_store
->model
= model
;
1432 m_notifier
= new wxGtkDataViewListModelNotifier( gtk_store
, model
);
1434 model
->AddNotifier( m_notifier
);
1436 gtk_tree_view_set_model( GTK_TREE_VIEW(m_treeview
), GTK_TREE_MODEL(gtk_store
) );
1437 g_object_unref( gtk_store
);
1442 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1444 if (!wxDataViewCtrlBase::AppendColumn(col
))
1447 GtkTreeViewColumn
*column
= (GtkTreeViewColumn
*)col
->GetGtkHandle();
1449 gtk_tree_view_append_column( GTK_TREE_VIEW(m_treeview
), column
);
1455 // !wxUSE_GENERICDATAVIEWCTRL
1458 // wxUSE_DATAVIEWCTRL