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