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