]>
Commit | Line | Data |
---|---|---|
f4e325b3 DP |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treectrl.cpp | |
d9692df7 | 3 | // Purpose: wxTreeCtrl |
f4e325b3 | 4 | // Author: Denis Pershin |
d9692df7 | 5 | // Modified by: |
f4e325b3 | 6 | // Created: 07/05/98 |
d9692df7 DP |
7 | // RCS-ID: $Id$ |
8 | // Copyright: (c) Denis Pershin | |
e21a5048 | 9 | // Licence: wxWindows licence |
f4e325b3 DP |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
e8cd8b1b | 13 | #pragma implementation "treectrl.h" |
f4e325b3 DP |
14 | #endif |
15 | ||
d9692df7 DP |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
f4e325b3 | 27 | #include "wx/gtk/treectrl.h" |
e21a5048 | 28 | #include <wx/textctrl.h> |
f4e325b3 DP |
29 | #include "wx/log.h" |
30 | ||
d9692df7 | 31 | #include <gtk/gtk.h> |
f4e325b3 | 32 | |
e21a5048 DP |
33 | //static void wxConvertToGtkTreeItem(wxTreeCtrl *owner, wxTreeItem& info, GtkTreeItem **gtkItem); |
34 | //static void wxConvertFromGtkTreeItem(wxTreeItem& info, GtkTreeItem *gtkItem); | |
d9692df7 | 35 | static void gtk_treectrl_count_callback (GtkWidget *widget, gpointer data); |
e895ec45 | 36 | static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data); |
d9692df7 DP |
37 | // static void gtk_treectrl_next_callback (GtkWidget *widget, gpointer data); |
38 | // static void gtk_treectrl_next_visible_callback (GtkWidget *widget, gpointer data); | |
39 | // static void gtk_treectrl_next_selected_callback (GtkWidget *widget, gpointer data); | |
f4e325b3 | 40 | |
e21a5048 DP |
41 | static void gtk_treeitem_expand_callback(GtkWidget *widget, wxTreeItemId *treeitem); |
42 | static void gtk_treeitem_collapse_callback(GtkWidget *widget, wxTreeItemId *treeitem); | |
43 | static void gtk_treeitem_select_callback(GtkWidget *widget, wxTreeItemId *treeitem); | |
f4e325b3 | 44 | |
e21a5048 | 45 | static void gtk_treeitem_expand_callback(GtkWidget *widget, wxTreeItemId *treeitem) { |
d9692df7 DP |
46 | wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner"); |
47 | if (owner == NULL) | |
befe54c6 | 48 | return; |
d9692df7 | 49 | |
e21a5048 DP |
50 | // long id = (long)gtk_object_get_data(GTK_OBJECT(widget), "id"); |
51 | owner->SendExpanding(GTK_TREE_ITEM(widget)); | |
52 | owner->SendExpanded(GTK_TREE_ITEM(widget)); | |
f4e325b3 DP |
53 | }; |
54 | ||
e21a5048 | 55 | static void gtk_treeitem_collapse_callback(GtkWidget *widget, wxTreeItemId *treeitem) { |
d9692df7 DP |
56 | wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner"); |
57 | if (owner == NULL) | |
befe54c6 | 58 | return; |
d9692df7 | 59 | |
e21a5048 DP |
60 | // long id = (long)gtk_object_get_data(GTK_OBJECT(widget), "id"); |
61 | owner->SendCollapsing(GTK_TREE_ITEM(widget)); | |
62 | owner->SendCollapsed(GTK_TREE_ITEM(widget)); | |
f4e325b3 DP |
63 | }; |
64 | ||
e21a5048 | 65 | static void gtk_treeitem_select_callback(GtkWidget *widget, wxTreeItemId *treeitem) { |
d9692df7 DP |
66 | wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner"); |
67 | if (owner == NULL) | |
68 | return; | |
f4e325b3 | 69 | |
e21a5048 DP |
70 | // long id = (long)gtk_object_get_data(GTK_OBJECT(widget), "id"); |
71 | owner->SendSelChanging(GTK_TREE_ITEM(widget)); | |
72 | owner->SendSelChanged(GTK_TREE_ITEM(widget)); | |
f4e325b3 DP |
73 | } |
74 | ||
d9692df7 | 75 | #if !USE_SHARED_LIBRARY |
e21a5048 DP |
76 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
77 | ||
d9692df7 | 78 | #endif |
f4e325b3 | 79 | |
e21a5048 | 80 | void wxTreeCtrl::Init() { |
d9692df7 DP |
81 | m_imageListNormal = NULL; |
82 | m_imageListState = NULL; | |
83 | m_textCtrl = NULL; | |
d9692df7 DP |
84 | } |
85 | ||
e21a5048 DP |
86 | bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, |
87 | const wxSize& size, long style, | |
88 | const wxValidator& validator, const wxString& name) { | |
89 | Init(); | |
d9692df7 DP |
90 | |
91 | int x = pos.x; | |
92 | int y = pos.y; | |
93 | int width = size.x; | |
94 | int height = size.y; | |
95 | ||
96 | m_windowStyle = style; | |
97 | ||
98 | SetParent(parent); | |
99 | ||
100 | if (width <= 0) | |
101 | width = 100; | |
102 | if (height <= 0) | |
103 | height = 30; | |
104 | if (x < 0) | |
105 | x = 0; | |
106 | if (y < 0) | |
107 | y = 0; | |
108 | ||
109 | m_needParent = TRUE; | |
110 | ||
e21a5048 | 111 | printf("precreate\n"); |
d9692df7 DP |
112 | PreCreation( parent, id, pos, size, style, name ); |
113 | ||
e21a5048 DP |
114 | printf("1\n"); |
115 | ||
d9692df7 | 116 | m_widget = gtk_scrolled_window_new(NULL, NULL); |
e21a5048 | 117 | printf("2\n"); |
d9692df7 | 118 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_widget), |
e21a5048 | 119 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
f4e325b3 | 120 | |
e21a5048 | 121 | printf("3\n"); |
f4e325b3 | 122 | m_tree = GTK_TREE(gtk_tree_new()); |
d9692df7 | 123 | |
e21a5048 | 124 | printf("4\n"); |
d9692df7 | 125 | gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_tree)); |
e21a5048 DP |
126 | printf("5\n"); |
127 | // gtk_widget_set_parent(GTK_WIDGET(m_tree), m_widget); | |
128 | printf("6\n"); | |
f4e325b3 | 129 | gtk_widget_show(GTK_WIDGET(m_tree)); |
befe54c6 | 130 | |
d9692df7 DP |
131 | SetName(name); |
132 | SetValidator(validator); | |
133 | ||
e21a5048 | 134 | printf("postcreate\n"); |
d9692df7 DP |
135 | PostCreation(); |
136 | ||
137 | gtk_widget_realize(GTK_WIDGET(m_tree)); | |
138 | ||
139 | Show(TRUE); | |
140 | ||
141 | return TRUE; | |
142 | } | |
143 | ||
e21a5048 DP |
144 | wxTreeCtrl::~wxTreeCtrl(void) { |
145 | if (m_textCtrl) | |
146 | delete m_textCtrl; | |
f4e325b3 DP |
147 | } |
148 | ||
d9692df7 | 149 | // Attributes |
e21a5048 | 150 | static void gtk_treectrl_count_callback (GtkWidget *widget, gpointer data) { |
d9692df7 | 151 | int count = (*((int *)data)); |
f4e325b3 | 152 | |
d9692df7 DP |
153 | count++; |
154 | if (GTK_IS_CONTAINER(widget)) | |
155 | gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_count_callback, data); | |
f4e325b3 DP |
156 | } |
157 | ||
e21a5048 | 158 | size_t wxTreeCtrl::GetCount() const { |
d9692df7 DP |
159 | int count = 0; |
160 | ||
161 | if (m_anchor != NULL) | |
e895ec45 | 162 | gtk_treectrl_count_callback(GTK_WIDGET(m_anchor), &count); |
d9692df7 | 163 | return count; |
f4e325b3 DP |
164 | } |
165 | ||
e21a5048 | 166 | unsigned int wxTreeCtrl::GetIndent() const { |
d9692df7 DP |
167 | return m_tree->indent_value; |
168 | } | |
befe54c6 | 169 | |
e21a5048 | 170 | void wxTreeCtrl::SetIndent(unsigned int indent) { |
d9692df7 DP |
171 | m_tree->indent_value = indent; |
172 | } | |
173 | ||
e21a5048 DP |
174 | wxImageList *wxTreeCtrl::GetImageList() const { |
175 | return m_imageListNormal; | |
d9692df7 DP |
176 | } |
177 | ||
e21a5048 DP |
178 | wxImageList *wxTreeCtrl::GetStateImageList() const { |
179 | return m_imageListState; | |
d9692df7 DP |
180 | } |
181 | ||
e21a5048 DP |
182 | void wxTreeCtrl::SetImageList(wxImageList *imageList) { |
183 | m_imageListNormal = imageList; | |
184 | } | |
d9692df7 | 185 | |
e21a5048 DP |
186 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) { |
187 | m_imageListState = imageList; | |
d9692df7 DP |
188 | } |
189 | ||
e21a5048 DP |
190 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId &item) const { |
191 | char *t; | |
d9692df7 | 192 | |
e21a5048 DP |
193 | if (!item.IsOk()) |
194 | return wxString(""); | |
f4e325b3 | 195 | |
e21a5048 DP |
196 | GtkLabel *l = GTK_LABEL(gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "w_label")); |
197 | gtk_label_get(l, &t); | |
198 | ||
199 | return t; | |
200 | } | |
201 | ||
202 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const { | |
203 | if (!item.IsOk()) | |
204 | return (-1); | |
205 | ||
206 | return (int)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "image"); | |
f4e325b3 DP |
207 | } |
208 | ||
e21a5048 DP |
209 | int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const { |
210 | if (!item.IsOk()) | |
211 | return (-1); | |
f4e325b3 | 212 | |
e21a5048 DP |
213 | return (int)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "selectedImage"); |
214 | } | |
f4e325b3 | 215 | |
e21a5048 DP |
216 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const { |
217 | if (!item.IsOk()) | |
d9692df7 | 218 | return NULL; |
f4e325b3 | 219 | |
e21a5048 DP |
220 | return (wxTreeItemData *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "data"); |
221 | } | |
f4e325b3 | 222 | |
e21a5048 DP |
223 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) { |
224 | if (!item.IsOk()) | |
225 | return; | |
f4e325b3 | 226 | |
e21a5048 DP |
227 | GtkLabel *l = GTK_LABEL(gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "w_label")); |
228 | gtk_label_set(l, text); | |
229 | } | |
f4e325b3 | 230 | |
e21a5048 DP |
231 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) { |
232 | if (!item.IsOk()) | |
233 | return; | |
234 | ||
235 | gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "image", (void *)image); | |
d9692df7 | 236 | } |
f4e325b3 | 237 | |
e21a5048 DP |
238 | void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) { |
239 | if (!item.IsOk()) | |
240 | return; | |
241 | ||
242 | gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "selectedImage", (void *)image); | |
d9692df7 | 243 | } |
f4e325b3 | 244 | |
e21a5048 DP |
245 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) { |
246 | if (!item.IsOk()) | |
247 | return; | |
f4e325b3 | 248 | |
e21a5048 DP |
249 | gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "data", data); |
250 | } | |
f4e325b3 | 251 | |
e21a5048 | 252 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const { |
c9d9f242 | 253 | return GTK_WIDGET_VISIBLE(GTK_TREE_ITEM((GtkTreeItem *)item)); |
d9692df7 | 254 | } |
f4e325b3 | 255 | |
e21a5048 | 256 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const { |
e895ec45 DP |
257 | GtkTreeItem *p = (GtkTreeItem *)item; |
258 | ||
259 | if (p->subtree == NULL) | |
260 | return wxFalse; | |
f4e325b3 | 261 | |
e895ec45 DP |
262 | if (GTK_TREE(p->subtree)->children == NULL) |
263 | return wxFalse; | |
f4e325b3 | 264 | |
e895ec45 DP |
265 | if (g_list_length(GTK_TREE(p->subtree)->children) == 0) |
266 | return wxFalse; | |
267 | ||
268 | return wxTrue; | |
d9692df7 | 269 | } |
f4e325b3 | 270 | |
e21a5048 DP |
271 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const { |
272 | return (((GtkTreeItem *)item)->expanded != 0); | |
273 | } | |
274 | ||
275 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const { | |
e895ec45 DP |
276 | GtkTreeItem *p = (GtkTreeItem *)item; |
277 | GtkWidget *parent = GTK_WIDGET(p)->parent; | |
278 | ||
279 | if (!GTK_IS_TREE(parent)) | |
280 | return wxFalse; | |
281 | ||
282 | if (g_list_index(GTK_TREE(parent)->children, p) == -1) | |
283 | return wxFalse; | |
284 | ||
285 | return wxTrue; | |
e21a5048 DP |
286 | } |
287 | ||
288 | wxTreeItemId wxTreeCtrl::GetRootItem() const { | |
289 | return m_anchor; | |
290 | } | |
291 | ||
e895ec45 DP |
292 | static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data) { |
293 | GtkTreeItem *p = (*((GtkTreeItem **)data)); | |
294 | ||
295 | GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree); | |
296 | ||
297 | if (tree->selection != NULL) { | |
298 | p = (GtkTreeItem *)tree->selection->data; | |
299 | return; | |
300 | } | |
301 | ||
302 | if (GTK_IS_CONTAINER(widget)) | |
303 | gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_selected_callback, data); | |
304 | } | |
305 | ||
e21a5048 | 306 | wxTreeItemId wxTreeCtrl::GetSelection() const { |
e895ec45 | 307 | GtkTreeItem *p = NULL; |
f4e325b3 | 308 | |
e895ec45 DP |
309 | if (m_anchor == NULL) |
310 | return NULL; | |
d9692df7 | 311 | |
e895ec45 DP |
312 | gtk_treectrl_first_selected_callback(GTK_WIDGET(m_anchor), &p); |
313 | ||
314 | return p; | |
d9692df7 DP |
315 | } |
316 | ||
e21a5048 | 317 | wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const { |
e21a5048 DP |
318 | if (item.IsOk()) |
319 | return (GtkTreeItem *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "parent"); | |
d9692df7 | 320 | |
e21a5048 DP |
321 | return NULL; |
322 | } | |
d9692df7 | 323 | |
e21a5048 | 324 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const { |
e895ec45 DP |
325 | GtkTreeItem *p = (GtkTreeItem *)item; |
326 | GtkWidget *parent = GTK_WIDGET(p)->parent; | |
327 | ||
328 | if (!GTK_IS_TREE(parent)) | |
329 | return NULL; | |
330 | ||
331 | cookie = 0; | |
332 | return GTK_TREE_ITEM(g_list_first(GTK_TREE(parent)->children)->data); | |
d9692df7 | 333 | } |
f4e325b3 | 334 | |
e21a5048 | 335 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const { |
e895ec45 DP |
336 | GtkTreeItem *p = (GtkTreeItem *)item; |
337 | GtkWidget *parent = GTK_WIDGET(p)->parent; | |
338 | ||
339 | if (!GTK_IS_TREE(parent)) | |
340 | return NULL; | |
341 | ||
342 | cookie++; | |
343 | return GTK_TREE_ITEM(g_list_nth(GTK_TREE(parent)->children, cookie)->data); | |
e21a5048 | 344 | } |
f4e325b3 | 345 | |
e21a5048 | 346 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const { |
e895ec45 DP |
347 | GtkTreeItem *p = (GtkTreeItem *)item; |
348 | GtkWidget *parent = GTK_WIDGET(p)->parent; | |
349 | ||
350 | if (!GTK_IS_TREE(parent)) | |
351 | return NULL; | |
352 | ||
353 | if (g_list_index(GTK_TREE(parent)->children, p) == -1) | |
354 | return NULL; | |
355 | ||
356 | return GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data); | |
e21a5048 | 357 | } |
f4e325b3 | 358 | |
e21a5048 | 359 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const { |
e895ec45 DP |
360 | GtkTreeItem *p = (GtkTreeItem *)item; |
361 | GtkWidget *parent = GTK_WIDGET(p)->parent; | |
362 | ||
363 | if (!GTK_IS_TREE(parent)) | |
364 | return NULL; | |
365 | ||
366 | if (g_list_index(GTK_TREE(parent)->children, p) == -1) | |
367 | return NULL; | |
368 | ||
369 | return GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data); | |
d9692df7 DP |
370 | } |
371 | ||
c9d9f242 DP |
372 | static void gtk_treectrl_first_visible_callback(GtkWidget *widget, gpointer data) { |
373 | GtkTreeItem *p = (*((GtkTreeItem **)data)); | |
374 | ||
375 | GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree); | |
376 | ||
377 | if (tree->children != NULL) { | |
378 | guint len = g_list_length(tree->children); | |
379 | for (guint i=0; i<len; i++) { | |
380 | if (GTK_WIDGET_VISIBLE(GTK_WIDGET(GTK_TREE_ITEM((GtkTreeItem *)g_list_nth(tree->children, i)->data)))) { | |
381 | p = GTK_TREE_ITEM((GtkTreeItem *)g_list_nth(tree->children, i)->data); | |
382 | return; | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
387 | if (GTK_IS_CONTAINER(widget)) | |
388 | gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_visible_callback, data); | |
389 | } | |
390 | ||
e21a5048 | 391 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const { |
c9d9f242 DP |
392 | GtkTreeItem *p = NULL; |
393 | ||
394 | if (m_anchor == NULL) | |
395 | return NULL; | |
396 | ||
397 | gtk_treectrl_first_visible_callback(GTK_WIDGET(m_anchor), &p); | |
398 | ||
399 | return p; | |
d9692df7 DP |
400 | } |
401 | ||
e21a5048 | 402 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const { |
c9d9f242 DP |
403 | GtkTreeItem *p = (GtkTreeItem *)item; |
404 | GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent); | |
405 | GtkTreeItem *q; | |
406 | ||
407 | if (!GTK_IS_TREE(parent)) | |
408 | return NULL; | |
409 | ||
410 | if (parent->children == NULL) | |
411 | return NULL; | |
412 | ||
413 | q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data); | |
414 | ||
415 | while (q != p) { | |
416 | q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, q))->data); | |
417 | if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q))) | |
418 | return q; | |
419 | } | |
420 | ||
e21a5048 DP |
421 | return NULL; |
422 | } | |
d9692df7 | 423 | |
e21a5048 | 424 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const { |
c9d9f242 DP |
425 | GtkTreeItem *p = (GtkTreeItem *)item; |
426 | GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent); | |
427 | GtkTreeItem *q; | |
428 | ||
429 | if (!GTK_IS_TREE(parent)) | |
430 | return NULL; | |
431 | ||
432 | if (parent->children == NULL) | |
433 | return NULL; | |
434 | ||
435 | q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data); | |
436 | ||
437 | while (q != p) { | |
438 | q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, q))->data); | |
439 | if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q))) | |
440 | return q; | |
441 | } | |
442 | ||
e21a5048 DP |
443 | return NULL; |
444 | } | |
d9692df7 | 445 | |
e21a5048 DP |
446 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, int image, |
447 | int selectedImage, wxTreeItemData *data) { | |
448 | return p_InsertItem(0, text, image, selectedImage, data); | |
449 | } | |
d9692df7 | 450 | |
e21a5048 DP |
451 | wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent, |
452 | const wxString& text, int image, int selectedImage, | |
453 | wxTreeItemData *data) { | |
454 | #warning "Need to implement PrependItem" | |
455 | return NULL; | |
456 | } | |
f4e325b3 | 457 | |
e21a5048 DP |
458 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
459 | const wxTreeItemId& idPrevious, const wxString& text, | |
460 | int image, int selectedImage, wxTreeItemData *data) { | |
461 | #warning "Need to implement InsertItem" | |
462 | return NULL; | |
d9692df7 | 463 | } |
f4e325b3 | 464 | |
e21a5048 DP |
465 | wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, |
466 | const wxString& text, int image, int selectedImage, | |
467 | wxTreeItemData *data) { | |
468 | return p_InsertItem(parent, text, image, selectedImage, data); | |
469 | } | |
f4e325b3 | 470 | |
e21a5048 DP |
471 | wxTreeItemId wxTreeCtrl::p_InsertItem(GtkTreeItem *p, |
472 | const wxString& text, int image, int selectedImage, | |
473 | wxTreeItemData *data) { | |
474 | GtkTreeItem *item; | |
d9692df7 | 475 | |
e21a5048 DP |
476 | printf("begin insert\n"); |
477 | ||
478 | item = GTK_TREE_ITEM(gtk_tree_item_new()); | |
479 | ||
480 | GtkHBox *m_box = GTK_HBOX(gtk_hbox_new(FALSE, 0)); | |
481 | gtk_container_add (GTK_CONTAINER (item), GTK_WIDGET(m_box)); | |
482 | ||
483 | gtk_object_set_data(GTK_OBJECT(item), "w_box", m_box); | |
484 | ||
485 | const wxBitmap *bmp; | |
486 | const wxImageList *list; | |
487 | if ((list = GetImageList(wxIMAGE_LIST_NORMAL)) != NULL) | |
488 | if ((bmp = list->GetBitmap(image)) != NULL) | |
489 | if (bmp->Ok()) { | |
490 | GdkBitmap *mask = NULL; | |
491 | if (bmp->GetMask()) | |
492 | mask = bmp->GetMask()->GetBitmap(); | |
493 | GtkPixmap *m_image_widget = GTK_PIXMAP(gtk_pixmap_new(bmp->GetPixmap(), mask)); | |
494 | gtk_misc_set_alignment (GTK_MISC (m_image_widget), 0.0, 0.5); | |
495 | gtk_box_pack_start(GTK_BOX(m_box), GTK_WIDGET(m_image_widget), FALSE, FALSE, 0); | |
496 | gtk_object_set_data(GTK_OBJECT(item), "w_image", (void *)m_image_widget); | |
497 | gtk_object_set_data(GTK_OBJECT(item), "image", (void *)image); | |
498 | gtk_widget_show (GTK_WIDGET(m_image_widget)); | |
499 | } | |
500 | GtkLabel *m_label_widget = GTK_LABEL(gtk_label_new ((char *)(const char *)text)); | |
501 | gtk_misc_set_alignment (GTK_MISC (m_label_widget), 0.5, 0.5); | |
502 | gtk_box_pack_start(GTK_BOX(m_box), GTK_WIDGET(m_label_widget), FALSE, FALSE, 0); | |
503 | gtk_object_set_data(GTK_OBJECT(item), "w_label", m_label_widget); | |
504 | gtk_widget_show (GTK_WIDGET(m_label_widget)); | |
505 | ||
506 | gtk_widget_show(GTK_WIDGET(m_box)); | |
507 | ||
508 | gtk_object_set_data(GTK_OBJECT(item), "owner", this); | |
e895ec45 DP |
509 | gtk_object_set_data(GTK_OBJECT(item), "data", data); |
510 | gtk_object_set_data(GTK_OBJECT(item), "parent", p); | |
e21a5048 DP |
511 | |
512 | if (p != 0) { | |
513 | if (p->subtree == NULL) { | |
514 | GtkTree *tree = GTK_TREE(gtk_tree_new()); | |
515 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(p), GTK_WIDGET(tree)); | |
516 | gtk_widget_show(GTK_WIDGET(tree)); | |
517 | p->expanded = 1; | |
518 | } | |
519 | ||
520 | gtk_container_add(GTK_CONTAINER(p->subtree), GTK_WIDGET(item)); | |
521 | } else { | |
522 | printf("Adding root\n"); | |
523 | printf("m_tree = %p\n", m_tree); | |
524 | m_anchor = item; | |
525 | gtk_container_add(GTK_CONTAINER(m_tree), GTK_WIDGET(item)); | |
d9692df7 | 526 | } |
f4e325b3 | 527 | |
e21a5048 | 528 | gtk_widget_show(GTK_WIDGET(item)); |
d9692df7 | 529 | |
e21a5048 DP |
530 | gtk_signal_connect(GTK_OBJECT(item), "select", |
531 | GTK_SIGNAL_FUNC(gtk_treeitem_select_callback), (gpointer)this ); | |
d9692df7 | 532 | |
e21a5048 DP |
533 | gtk_signal_connect(GTK_OBJECT(item), "deselect", |
534 | GTK_SIGNAL_FUNC(gtk_treeitem_select_callback), (gpointer)this ); | |
d9692df7 | 535 | |
e21a5048 DP |
536 | gtk_signal_connect(GTK_OBJECT(item), "expand", |
537 | GTK_SIGNAL_FUNC(gtk_treeitem_expand_callback), (gpointer)this ); | |
538 | gtk_signal_connect(GTK_OBJECT(item), "collapse", | |
539 | GTK_SIGNAL_FUNC(gtk_treeitem_collapse_callback), (gpointer)this ); | |
540 | ||
541 | return item; | |
d9692df7 DP |
542 | } |
543 | ||
e21a5048 DP |
544 | void wxTreeCtrl::Delete(const wxTreeItemId& item) { |
545 | if (!item.IsOk()) | |
546 | return; | |
d9692df7 | 547 | |
e21a5048 DP |
548 | GtkTreeItem *parent = GTK_TREE_ITEM(GTK_WIDGET((GtkTreeItem *)item)->parent); |
549 | if (parent == NULL) | |
550 | return; | |
d9692df7 | 551 | |
e21a5048 DP |
552 | gtk_container_remove(GTK_CONTAINER(parent), GTK_WIDGET((GtkTreeItem *)item)); |
553 | ||
554 | return; | |
d9692df7 DP |
555 | } |
556 | ||
e21a5048 DP |
557 | void wxTreeCtrl::DeleteAllItems() { |
558 | gtk_tree_item_remove_subtree(m_anchor); | |
559 | } | |
d9692df7 | 560 | |
e21a5048 DP |
561 | void wxTreeCtrl::Expand(const wxTreeItemId& item) { |
562 | if (!item.IsOk()) | |
563 | return; | |
d9692df7 | 564 | |
e21a5048 | 565 | gtk_tree_item_expand(GTK_TREE_ITEM((GtkTreeItem *)item)); |
d9692df7 DP |
566 | } |
567 | ||
e21a5048 DP |
568 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) { |
569 | if (!item.IsOk()) | |
570 | return; | |
d9692df7 | 571 | |
e21a5048 DP |
572 | gtk_tree_item_collapse(GTK_TREE_ITEM((GtkTreeItem *)item)); |
573 | } | |
d9692df7 | 574 | |
e21a5048 DP |
575 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) { |
576 | if (!item.IsOk()) | |
577 | return; | |
578 | ||
579 | gtk_tree_item_collapse(GTK_TREE_ITEM((GtkTreeItem *)item)); | |
580 | gtk_tree_item_remove_subtree(GTK_TREE_ITEM((GtkTreeItem *)item)); | |
d9692df7 DP |
581 | } |
582 | ||
e21a5048 DP |
583 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) { |
584 | if (!item.IsOk()) | |
585 | return; | |
d9692df7 | 586 | |
e21a5048 DP |
587 | if (((GtkTreeItem *)item)->expanded) |
588 | gtk_tree_item_collapse(GTK_TREE_ITEM((GtkTreeItem *)item)); | |
589 | else | |
590 | gtk_tree_item_expand(GTK_TREE_ITEM((GtkTreeItem *)item)); | |
591 | } | |
d9692df7 | 592 | |
e21a5048 DP |
593 | void wxTreeCtrl::Unselect() { |
594 | #warning "Need to implement Unselect" | |
d9692df7 DP |
595 | } |
596 | ||
e21a5048 DP |
597 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item) { |
598 | if (!item.IsOk()) | |
599 | return; | |
d9692df7 | 600 | |
e21a5048 DP |
601 | gtk_tree_item_select((GtkTreeItem *)item); |
602 | } | |
d9692df7 | 603 | |
e21a5048 DP |
604 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) { |
605 | #warning "Need to implement EnsureVisible" | |
d9692df7 DP |
606 | } |
607 | ||
e21a5048 DP |
608 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) { |
609 | #warning "Need to implement ScrollTo" | |
610 | } | |
f4e325b3 | 611 | |
e21a5048 DP |
612 | wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
613 | wxClassInfo* textControlClass) { | |
614 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
615 | #warning "Need to implement EditLabel" | |
616 | return m_textCtrl; | |
617 | } | |
d9692df7 | 618 | |
e21a5048 DP |
619 | wxTextCtrl* wxTreeCtrl::GetEditControl() const { |
620 | return m_textCtrl; | |
d9692df7 DP |
621 | } |
622 | ||
e21a5048 DP |
623 | void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) { |
624 | #warning "Need to implement EndEditLabel" | |
625 | } | |
befe54c6 | 626 | |
e21a5048 DP |
627 | void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) { |
628 | switch (action) { | |
629 | case wxTREE_EXPAND_EXPAND: | |
630 | Expand(item); | |
631 | break; | |
d9692df7 | 632 | |
e21a5048 DP |
633 | case wxTREE_EXPAND_COLLAPSE: |
634 | Collapse(item); | |
635 | break; | |
c89a6106 | 636 | |
e21a5048 DP |
637 | case wxTREE_EXPAND_COLLAPSE_RESET: |
638 | CollapseAndReset(item); | |
639 | break; | |
640 | ||
641 | case wxTREE_EXPAND_TOGGLE: | |
642 | Toggle(item); | |
643 | break; | |
644 | ||
645 | default: | |
646 | wxFAIL_MSG("unknown action in wxTreeCtrl::ExpandItem"); | |
647 | } | |
d9692df7 | 648 | } |
f4e325b3 | 649 | |
e21a5048 DP |
650 | wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, |
651 | const wxString& text, int image, int selImage, | |
652 | long insertAfter) { | |
653 | // InsertItem(parent, insertAfter, text, image, selImage); | |
654 | #warning "Need to implement InsertItem" | |
655 | return NULL; | |
d9692df7 | 656 | } |
f4e325b3 | 657 | |
e21a5048 DP |
658 | /* Old functions |
659 | long wxTreeCtrl::GetChild(long item) const { | |
d9692df7 | 660 | GtkTreeItem *p; |
e21a5048 | 661 | GtkTreeItem *next = NULL; |
f4e325b3 | 662 | |
d9692df7 | 663 | p = findGtkTreeItem(item); |
e21a5048 DP |
664 | GList *list = gtk_container_children(GTK_CONTAINER(p)); |
665 | next = GTK_TREE_ITEM(list->data);; | |
f4e325b3 | 666 | |
e21a5048 DP |
667 | if (next != NULL) |
668 | return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); | |
669 | ||
670 | return (-1); | |
671 | } | |
d9692df7 | 672 | |
e21a5048 DP |
673 | long wxTreeCtrl::GetFirstVisibleItem(void) const { |
674 | GtkTreeItem *next = NULL; | |
d9692df7 | 675 | |
e21a5048 DP |
676 | GList *list = gtk_container_children(GTK_CONTAINER(m_anchor)); |
677 | next = GTK_TREE_ITEM(list->data);; | |
678 | // gtk_container_foreach(GTK_CONTAINER(m_anchor), gtk_treectrl_next_visible_callback, &next); | |
679 | ||
680 | if (next != NULL) | |
681 | return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); | |
682 | ||
683 | return (-1); | |
d9692df7 DP |
684 | } |
685 | ||
e21a5048 | 686 | long wxTreeCtrl::GetNextVisibleItem(long item) const { |
d9692df7 | 687 | GtkTreeItem *p; |
e21a5048 | 688 | GtkTreeItem *next = NULL; |
d9692df7 DP |
689 | |
690 | p = findGtkTreeItem(item); | |
e21a5048 DP |
691 | GList *list = gtk_container_children(GTK_CONTAINER(p)); |
692 | next = GTK_TREE_ITEM(list->data);; | |
693 | // gtk_container_foreach(GTK_CONTAINER(p), gtk_treectrl_next_visible_callback, &next); | |
694 | ||
695 | if (next != NULL) | |
696 | return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); | |
697 | ||
698 | return (-1); | |
699 | } | |
700 | ||
701 | bool wxTreeCtrl::GetItem(wxTreeItem& info) const { | |
702 | GtkTreeItem *p; | |
703 | ||
704 | p = findGtkTreeItem(info.m_itemId); | |
705 | ||
706 | if (p == NULL) { | |
707 | wxLogSysError("TreeCtrl::GetItem failed"); | |
d9692df7 | 708 | return FALSE; |
e21a5048 | 709 | } |
d9692df7 | 710 | |
e21a5048 | 711 | wxConvertFromGtkTreeItem(info, p); |
d9692df7 DP |
712 | |
713 | return TRUE; | |
714 | } | |
715 | ||
e21a5048 | 716 | bool wxTreeCtrl::SetItem(wxTreeItem& info) { |
d9692df7 DP |
717 | GtkTreeItem *p; |
718 | ||
e21a5048 | 719 | p = findGtkTreeItem(info.m_itemId); |
d9692df7 | 720 | |
e21a5048 DP |
721 | if (p == NULL) { |
722 | wxLogSysError("TreeCtrl::SetItem failed"); | |
f4e325b3 | 723 | return FALSE; |
e21a5048 | 724 | } |
f4e325b3 | 725 | |
e21a5048 | 726 | wxConvertToGtkTreeItem(this, info, &p); |
f4e325b3 | 727 | |
e21a5048 DP |
728 | return TRUE; |
729 | } | |
d9692df7 | 730 | |
e21a5048 DP |
731 | int wxTreeCtrl::GetItemState(long item, long stateMask) const { |
732 | wxTreeItem info; | |
f4e325b3 | 733 | |
e21a5048 DP |
734 | info.m_mask = wxTREE_MASK_STATE ; |
735 | info.m_stateMask = stateMask; | |
736 | info.m_itemId = item; | |
d9692df7 | 737 | |
e21a5048 DP |
738 | if (!GetItem(info)) |
739 | return 0; | |
740 | ||
741 | return info.m_state; | |
742 | } | |
743 | ||
744 | bool wxTreeCtrl::SetItemState(long item, long state, long stateMask) { | |
745 | wxTreeItem info; | |
746 | ||
747 | info.m_mask = wxTREE_MASK_STATE ; | |
748 | info.m_state = state; | |
749 | info.m_stateMask = stateMask; | |
750 | info.m_itemId = item; | |
751 | ||
752 | return SetItem(info); | |
753 | } | |
754 | */ | |
755 | ||
756 | // Operations | |
757 | /* | |
758 | bool wxTreeCtrl::DeleteChildren(long item) { | |
759 | GtkTreeItem *p; | |
760 | ||
761 | p = findGtkTreeItem(item); | |
762 | if (p == NULL) | |
763 | return FALSE; | |
764 | ||
765 | gtk_tree_item_remove_subtree(GTK_TREE_ITEM(p)); | |
f4e325b3 DP |
766 | |
767 | return TRUE; | |
d9692df7 | 768 | } |
e21a5048 | 769 | */ |
f4e325b3 | 770 | |
e21a5048 DP |
771 | /* |
772 | long wxTreeCtrl::InsertItem(long parent, wxTreeItem& info, long insertAfter) { | |
d9692df7 DP |
773 | GtkTreeItem *p; |
774 | GtkTreeItem *item = NULL; | |
775 | ||
776 | info.m_itemId = m_curitemId; | |
777 | m_curitemId++; | |
778 | ||
779 | wxConvertToGtkTreeItem(this, info, &item); | |
780 | ||
781 | if (parent != 0) { | |
782 | p = findGtkTreeItem(parent); | |
783 | if (p->subtree == NULL) { | |
784 | GtkTree *tree = GTK_TREE(gtk_tree_new()); | |
785 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(p), GTK_WIDGET(tree)); | |
786 | gtk_widget_show(GTK_WIDGET(tree)); | |
787 | p->expanded = 1; | |
788 | } | |
f4e325b3 | 789 | |
d9692df7 DP |
790 | gtk_container_add(GTK_CONTAINER(p->subtree), GTK_WIDGET(item)); |
791 | } else { | |
792 | m_anchor = item; | |
793 | gtk_container_add(GTK_CONTAINER(m_tree), GTK_WIDGET(item)); | |
794 | } | |
795 | ||
796 | if ((info.m_mask & wxTREE_MASK_CHILDREN) != 0) { | |
797 | GtkTree *tree = GTK_TREE(gtk_tree_new()); | |
798 | gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), GTK_WIDGET(tree)); | |
799 | gtk_widget_show(GTK_WIDGET(tree)); | |
800 | } | |
801 | ||
802 | gtk_widget_show(GTK_WIDGET(item)); | |
803 | ||
804 | gtk_signal_connect(GTK_OBJECT(item), "select", | |
805 | GTK_SIGNAL_FUNC(gtk_treeitem_select_callback), (gpointer)this ); | |
806 | ||
807 | gtk_signal_connect(GTK_OBJECT(item), "deselect", | |
808 | GTK_SIGNAL_FUNC(gtk_treeitem_select_callback), (gpointer)this ); | |
809 | ||
810 | gtk_signal_connect(GTK_OBJECT(item), "expand", | |
811 | GTK_SIGNAL_FUNC(gtk_treeitem_expand_callback), (gpointer)this ); | |
812 | gtk_signal_connect(GTK_OBJECT(item), "collapse", | |
813 | GTK_SIGNAL_FUNC(gtk_treeitem_collapse_callback), (gpointer)this ); | |
814 | ||
815 | return info.m_itemId; | |
f4e325b3 DP |
816 | } |
817 | ||
d9692df7 DP |
818 | long wxTreeCtrl::InsertItem(long parent, const wxString& label, int image, |
819 | int selImage, long insertAfter) { | |
820 | ||
821 | wxTreeItem info; | |
822 | info.m_text = label; | |
823 | info.m_mask = wxTREE_MASK_TEXT; | |
824 | if (image > -1) { | |
825 | info.m_mask |= wxTREE_MASK_IMAGE | wxTREE_MASK_SELECTED_IMAGE; | |
826 | info.m_image = image; | |
827 | if (selImage == -1) | |
828 | info.m_selectedImage = image; | |
829 | else | |
830 | info.m_selectedImage = selImage; | |
831 | } | |
f4e325b3 | 832 | |
d9692df7 | 833 | return InsertItem(parent, info, insertAfter); |
f4e325b3 | 834 | } |
d9692df7 | 835 | */ |
f4e325b3 | 836 | |
e895ec45 DP |
837 | void wxTreeCtrl::SendMessage(wxEventType command, const wxTreeItemId& item) { |
838 | wxTreeEvent event(command, GetId()); | |
d9692df7 | 839 | event.SetEventObject(this); |
e895ec45 | 840 | event.m_item = item; |
d9692df7 DP |
841 | ProcessEvent(event); |
842 | } | |
f4e325b3 | 843 | |
e895ec45 DP |
844 | void wxTreeCtrl::SendExpanding(const wxTreeItemId& item) { |
845 | SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDING, item); | |
846 | } | |
847 | ||
e21a5048 | 848 | void wxTreeCtrl::SendExpanded(const wxTreeItemId& item) { |
e895ec45 | 849 | SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDED, item); |
d9692df7 | 850 | } |
f4e325b3 | 851 | |
e21a5048 | 852 | void wxTreeCtrl::SendCollapsing(const wxTreeItemId& item) { |
e895ec45 | 853 | SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, item); |
d9692df7 | 854 | } |
f4e325b3 | 855 | |
e21a5048 | 856 | void wxTreeCtrl::SendCollapsed(const wxTreeItemId& item) { |
e895ec45 | 857 | SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, item); |
d9692df7 | 858 | } |
f4e325b3 | 859 | |
e21a5048 | 860 | void wxTreeCtrl::SendSelChanging(const wxTreeItemId& item) { |
e895ec45 | 861 | SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGED, item); |
f4e325b3 DP |
862 | } |
863 | ||
e21a5048 | 864 | void wxTreeCtrl::SendSelChanged(const wxTreeItemId& item) { |
e895ec45 | 865 | SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGING, item); |
d9692df7 | 866 | } |
f4e325b3 | 867 | |
d9692df7 DP |
868 | // Tree event |
869 | IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxCommandEvent) | |
870 | ||
e21a5048 DP |
871 | wxTreeEvent::wxTreeEvent(wxEventType commandType, int id): |
872 | wxCommandEvent(commandType, id) { | |
d9692df7 | 873 | m_code = 0; |
e21a5048 | 874 | m_itemOld = 0; |
d9692df7 | 875 | } |