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