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