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