]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/treegtk.cpp
reSWIGged
[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
65571936 9// Licence: wxWindows licence
f4e325b3
DP
10/////////////////////////////////////////////////////////////////////////////
11
d9692df7
DP
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#endif
22
f4e325b3 23#include "wx/gtk/treectrl.h"
3096bd2f 24#include "wx/textctrl.h"
f4e325b3
DP
25#include "wx/log.h"
26
d9692df7 27#include <gtk/gtk.h>
f4e325b3 28
e21a5048
DP
29//static void wxConvertToGtkTreeItem(wxTreeCtrl *owner, wxTreeItem& info, GtkTreeItem **gtkItem);
30//static void wxConvertFromGtkTreeItem(wxTreeItem& info, GtkTreeItem *gtkItem);
e1dc4cc6 31
d9692df7 32static void gtk_treectrl_count_callback (GtkWidget *widget, gpointer data);
e895ec45 33static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data);
e1dc4cc6
DP
34static void gtk_treectrl_first_visible_callback(GtkWidget *widget, gpointer data);
35
d9692df7
DP
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);
f4e325b3 39
e21a5048
DP
40static void gtk_treeitem_expand_callback(GtkWidget *widget, wxTreeItemId *treeitem);
41static void gtk_treeitem_collapse_callback(GtkWidget *widget, wxTreeItemId *treeitem);
42static void gtk_treeitem_select_callback(GtkWidget *widget, wxTreeItemId *treeitem);
f4e325b3 43
e21a5048 44static void gtk_treeitem_expand_callback(GtkWidget *widget, wxTreeItemId *treeitem) {
d9692df7
DP
45 wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner");
46 if (owner == NULL)
befe54c6 47 return;
d9692df7 48
e21a5048
DP
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));
f4e325b3
DP
52};
53
e21a5048 54static void gtk_treeitem_collapse_callback(GtkWidget *widget, wxTreeItemId *treeitem) {
d9692df7
DP
55 wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner");
56 if (owner == NULL)
befe54c6 57 return;
d9692df7 58
e21a5048
DP
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));
f4e325b3
DP
62};
63
e21a5048 64static void gtk_treeitem_select_callback(GtkWidget *widget, wxTreeItemId *treeitem) {
d9692df7
DP
65 wxTreeCtrl *owner = (wxTreeCtrl *)gtk_object_get_data(GTK_OBJECT(widget), "owner");
66 if (owner == NULL)
67 return;
f4e325b3 68
e21a5048
DP
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));
f4e325b3
DP
72}
73
e21a5048
DP
74IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
75
f4e325b3 76
e21a5048 77void wxTreeCtrl::Init() {
d9692df7
DP
78 m_imageListNormal = NULL;
79 m_imageListState = NULL;
80 m_textCtrl = NULL;
d9692df7
DP
81}
82
e21a5048
DP
83bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos,
84 const wxSize& size, long style,
85 const wxValidator& validator, const wxString& name) {
86 Init();
d9692df7
DP
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;
978f38c2 107
e21a5048 108printf("precreate\n");
d9692df7
DP
109 PreCreation( parent, id, pos, size, style, name );
110
e21a5048
DP
111printf("1\n");
112
d9692df7 113 m_widget = gtk_scrolled_window_new(NULL, NULL);
e21a5048 114printf("2\n");
d9692df7 115 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_widget),
e21a5048 116 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
f4e325b3 117
e21a5048 118printf("3\n");
f4e325b3 119 m_tree = GTK_TREE(gtk_tree_new());
d9692df7 120
e21a5048 121printf("4\n");
d9692df7 122 gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_tree));
e21a5048 123printf("5\n");
f4e325b3 124 gtk_widget_show(GTK_WIDGET(m_tree));
befe54c6 125
d9692df7
DP
126 SetName(name);
127 SetValidator(validator);
128
6ca41e57 129printf("Robert's new insertion code :-)\n");
f03fc89f 130 m_parent->DoAddChild( this );
e21a5048 131printf("postcreate\n");
d9692df7
DP
132 PostCreation();
133
134 gtk_widget_realize(GTK_WIDGET(m_tree));
135
136 Show(TRUE);
978f38c2 137
d9692df7
DP
138 return TRUE;
139}
140
e21a5048
DP
141wxTreeCtrl::~wxTreeCtrl(void) {
142 if (m_textCtrl)
143 delete m_textCtrl;
f4e325b3
DP
144}
145
d9692df7 146// Attributes
e21a5048 147static void gtk_treectrl_count_callback (GtkWidget *widget, gpointer data) {
d9692df7 148 int count = (*((int *)data));
f4e325b3 149
d9692df7
DP
150 count++;
151 if (GTK_IS_CONTAINER(widget))
152 gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_count_callback, data);
f4e325b3
DP
153}
154
e21a5048 155size_t wxTreeCtrl::GetCount() const {
d9692df7
DP
156 int count = 0;
157
158 if (m_anchor != NULL)
e895ec45 159 gtk_treectrl_count_callback(GTK_WIDGET(m_anchor), &count);
d9692df7 160 return count;
f4e325b3
DP
161}
162
e21a5048 163unsigned int wxTreeCtrl::GetIndent() const {
d9692df7
DP
164 return m_tree->indent_value;
165}
befe54c6 166
e21a5048 167void wxTreeCtrl::SetIndent(unsigned int indent) {
d9692df7
DP
168 m_tree->indent_value = indent;
169}
170
e21a5048
DP
171wxImageList *wxTreeCtrl::GetImageList() const {
172 return m_imageListNormal;
d9692df7
DP
173}
174
e21a5048
DP
175wxImageList *wxTreeCtrl::GetStateImageList() const {
176 return m_imageListState;
d9692df7
DP
177}
178
e21a5048
DP
179void wxTreeCtrl::SetImageList(wxImageList *imageList) {
180 m_imageListNormal = imageList;
181}
d9692df7 182
e21a5048
DP
183void wxTreeCtrl::SetStateImageList(wxImageList *imageList) {
184 m_imageListState = imageList;
d9692df7
DP
185}
186
e21a5048
DP
187wxString wxTreeCtrl::GetItemText(const wxTreeItemId &item) const {
188 char *t;
d9692df7 189
e21a5048
DP
190 if (!item.IsOk())
191 return wxString("");
f4e325b3 192
e21a5048
DP
193 GtkLabel *l = GTK_LABEL(gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "w_label"));
194 gtk_label_get(l, &t);
195
196 return t;
197}
198
199int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const {
200 if (!item.IsOk())
201 return (-1);
202
203 return (int)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "image");
f4e325b3
DP
204}
205
e21a5048
DP
206int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const {
207 if (!item.IsOk())
208 return (-1);
f4e325b3 209
e21a5048
DP
210 return (int)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "selectedImage");
211}
f4e325b3 212
e21a5048
DP
213wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const {
214 if (!item.IsOk())
d9692df7 215 return NULL;
f4e325b3 216
e21a5048
DP
217 return (wxTreeItemData *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "data");
218}
f4e325b3 219
e21a5048
DP
220void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) {
221 if (!item.IsOk())
222 return;
f4e325b3 223
e21a5048 224 GtkLabel *l = GTK_LABEL(gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "w_label"));
a7c12d28 225 gtk_label_set_text(l, text);
e21a5048 226}
f4e325b3 227
e21a5048
DP
228void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image) {
229 if (!item.IsOk())
230 return;
231
232 gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "image", (void *)image);
d9692df7 233}
f4e325b3 234
e21a5048
DP
235void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image) {
236 if (!item.IsOk())
237 return;
238
239 gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "selectedImage", (void *)image);
d9692df7 240}
f4e325b3 241
e21a5048
DP
242void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) {
243 if (!item.IsOk())
244 return;
f4e325b3 245
e21a5048
DP
246 gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "data", data);
247}
f4e325b3 248
e21a5048 249bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const {
c9d9f242 250 return GTK_WIDGET_VISIBLE(GTK_TREE_ITEM((GtkTreeItem *)item));
d9692df7 251}
f4e325b3 252
e21a5048 253bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const {
e895ec45
DP
254 GtkTreeItem *p = (GtkTreeItem *)item;
255
256 if (p->subtree == NULL)
257 return wxFalse;
f4e325b3 258
e895ec45
DP
259 if (GTK_TREE(p->subtree)->children == NULL)
260 return wxFalse;
f4e325b3 261
e895ec45
DP
262 if (g_list_length(GTK_TREE(p->subtree)->children) == 0)
263 return wxFalse;
264
265 return wxTrue;
d9692df7 266}
f4e325b3 267
e21a5048
DP
268bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const {
269 return (((GtkTreeItem *)item)->expanded != 0);
270}
271
272bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const {
e895ec45
DP
273 GtkTreeItem *p = (GtkTreeItem *)item;
274 GtkWidget *parent = GTK_WIDGET(p)->parent;
275
276 if (!GTK_IS_TREE(parent))
277 return wxFalse;
278
279 if (g_list_index(GTK_TREE(parent)->children, p) == -1)
280 return wxFalse;
281
282 return wxTrue;
e21a5048
DP
283}
284
285wxTreeItemId wxTreeCtrl::GetRootItem() const {
286 return m_anchor;
287}
288
e895ec45
DP
289static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data) {
290 GtkTreeItem *p = (*((GtkTreeItem **)data));
291
292 GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree);
293
294 if (tree->selection != NULL) {
295 p = (GtkTreeItem *)tree->selection->data;
296 return;
297 }
298
299 if (GTK_IS_CONTAINER(widget))
300 gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_selected_callback, data);
301}
302
e21a5048 303wxTreeItemId wxTreeCtrl::GetSelection() const {
e895ec45 304 GtkTreeItem *p = NULL;
f4e325b3 305
e895ec45
DP
306 if (m_anchor == NULL)
307 return NULL;
d9692df7 308
e895ec45
DP
309 gtk_treectrl_first_selected_callback(GTK_WIDGET(m_anchor), &p);
310
311 return p;
d9692df7
DP
312}
313
99006e44 314wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const {
e21a5048
DP
315 if (item.IsOk())
316 return (GtkTreeItem *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "parent");
d9692df7 317
e21a5048
DP
318 return NULL;
319}
d9692df7 320
e21a5048 321wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const {
e895ec45
DP
322 GtkTreeItem *p = (GtkTreeItem *)item;
323 GtkWidget *parent = GTK_WIDGET(p)->parent;
324
325 if (!GTK_IS_TREE(parent))
326 return NULL;
327
328 cookie = 0;
329 return GTK_TREE_ITEM(g_list_first(GTK_TREE(parent)->children)->data);
d9692df7 330}
f4e325b3 331
e21a5048 332wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const {
e895ec45
DP
333 GtkTreeItem *p = (GtkTreeItem *)item;
334 GtkWidget *parent = GTK_WIDGET(p)->parent;
335
336 if (!GTK_IS_TREE(parent))
337 return NULL;
338
339 cookie++;
340 return GTK_TREE_ITEM(g_list_nth(GTK_TREE(parent)->children, cookie)->data);
e21a5048 341}
f4e325b3 342
978f38c2
VZ
343wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
344{
345 GtkTreeItem *p = (GtkTreeItem *)item;
346 GtkWidget *parent = GTK_WIDGET(p)->parent;
347
348 wxCHECK_MSG( GTK_IS_TREE(parent), NULL, "invalid tree item" );
349
350 return GTK_TREE_ITEM(g_list_last(GTK_TREE(parent)->children)->data);
351}
352
e21a5048 353wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const {
e895ec45
DP
354 GtkTreeItem *p = (GtkTreeItem *)item;
355 GtkWidget *parent = GTK_WIDGET(p)->parent;
356
357 if (!GTK_IS_TREE(parent))
358 return NULL;
359
360 if (g_list_index(GTK_TREE(parent)->children, p) == -1)
361 return NULL;
362
363 return GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data);
e21a5048 364}
f4e325b3 365
e21a5048 366wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const {
e895ec45
DP
367 GtkTreeItem *p = (GtkTreeItem *)item;
368 GtkWidget *parent = GTK_WIDGET(p)->parent;
369
370 if (!GTK_IS_TREE(parent))
371 return NULL;
372
373 if (g_list_index(GTK_TREE(parent)->children, p) == -1)
374 return NULL;
375
376 return GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data);
d9692df7
DP
377}
378
c9d9f242
DP
379static void gtk_treectrl_first_visible_callback(GtkWidget *widget, gpointer data) {
380 GtkTreeItem *p = (*((GtkTreeItem **)data));
381
382 GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree);
383
384 if (tree->children != NULL) {
385 guint len = g_list_length(tree->children);
386 for (guint i=0; i<len; i++) {
387 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(GTK_TREE_ITEM((GtkTreeItem *)g_list_nth(tree->children, i)->data)))) {
388 p = GTK_TREE_ITEM((GtkTreeItem *)g_list_nth(tree->children, i)->data);
389 return;
390 }
391 }
392 }
393
394 if (GTK_IS_CONTAINER(widget))
395 gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_visible_callback, data);
396}
397
e21a5048 398wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const {
c9d9f242
DP
399 GtkTreeItem *p = NULL;
400
401 if (m_anchor == NULL)
402 return NULL;
403
404 gtk_treectrl_first_visible_callback(GTK_WIDGET(m_anchor), &p);
405
406 return p;
d9692df7
DP
407}
408
e21a5048 409wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const {
c9d9f242
DP
410 GtkTreeItem *p = (GtkTreeItem *)item;
411 GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent);
412 GtkTreeItem *q;
413
414 if (!GTK_IS_TREE(parent))
415 return NULL;
416
417 if (parent->children == NULL)
418 return NULL;
419
420 q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data);
421
422 while (q != p) {
423 q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, q))->data);
424 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q)))
425 return q;
426 }
427
e21a5048
DP
428 return NULL;
429}
d9692df7 430
e21a5048 431wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const {
c9d9f242
DP
432 GtkTreeItem *p = (GtkTreeItem *)item;
433 GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent);
434 GtkTreeItem *q;
435
436 if (!GTK_IS_TREE(parent))
437 return NULL;
438
439 if (parent->children == NULL)
440 return NULL;
441
442 q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data);
443
444 while (q != p) {
445 q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, q))->data);
446 if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q)))
447 return q;
448 }
449
e21a5048
DP
450 return NULL;
451}
d9692df7 452
e21a5048
DP
453wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, int image,
454 int selectedImage, wxTreeItemData *data) {
455 return p_InsertItem(0, text, image, selectedImage, data);
456}
d9692df7 457
e21a5048
DP
458wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
459 const wxString& text, int image, int selectedImage,
460 wxTreeItemData *data) {
461#warning "Need to implement PrependItem"
462 return NULL;
463}
f4e325b3 464
e21a5048
DP
465wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
466 const wxTreeItemId& idPrevious, const wxString& text,
467 int image, int selectedImage, wxTreeItemData *data) {
468#warning "Need to implement InsertItem"
469 return NULL;
d9692df7 470}
f4e325b3 471
e21a5048
DP
472wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
473 const wxString& text, int image, int selectedImage,
978f38c2 474 wxTreeItemData *data) {
e21a5048
DP
475 return p_InsertItem(parent, text, image, selectedImage, data);
476}
f4e325b3 477
e21a5048
DP
478wxTreeItemId wxTreeCtrl::p_InsertItem(GtkTreeItem *p,
479 const wxString& text, int image, int selectedImage,
978f38c2 480 wxTreeItemData *data) {
e21a5048 481 GtkTreeItem *item;
d9692df7 482
e21a5048
DP
483printf("begin insert\n");
484
485 item = GTK_TREE_ITEM(gtk_tree_item_new());
486
487 GtkHBox *m_box = GTK_HBOX(gtk_hbox_new(FALSE, 0));
488 gtk_container_add (GTK_CONTAINER (item), GTK_WIDGET(m_box));
489
490 gtk_object_set_data(GTK_OBJECT(item), "w_box", m_box);
491
492 const wxBitmap *bmp;
493 const wxImageList *list;
494 if ((list = GetImageList(wxIMAGE_LIST_NORMAL)) != NULL)
49bf4e3e 495 if ((bmp = list->GetBitmapPtr(image)) != NULL)
e21a5048
DP
496 if (bmp->Ok()) {
497 GdkBitmap *mask = NULL;
498 if (bmp->GetMask())
499 mask = bmp->GetMask()->GetBitmap();
500 GtkPixmap *m_image_widget = GTK_PIXMAP(gtk_pixmap_new(bmp->GetPixmap(), mask));
501 gtk_misc_set_alignment (GTK_MISC (m_image_widget), 0.0, 0.5);
502 gtk_box_pack_start(GTK_BOX(m_box), GTK_WIDGET(m_image_widget), FALSE, FALSE, 0);
503 gtk_object_set_data(GTK_OBJECT(item), "w_image", (void *)m_image_widget);
504 gtk_object_set_data(GTK_OBJECT(item), "image", (void *)image);
505 gtk_widget_show (GTK_WIDGET(m_image_widget));
506 }
507 GtkLabel *m_label_widget = GTK_LABEL(gtk_label_new ((char *)(const char *)text));
508 gtk_misc_set_alignment (GTK_MISC (m_label_widget), 0.5, 0.5);
509 gtk_box_pack_start(GTK_BOX(m_box), GTK_WIDGET(m_label_widget), FALSE, FALSE, 0);
510 gtk_object_set_data(GTK_OBJECT(item), "w_label", m_label_widget);
511 gtk_widget_show (GTK_WIDGET(m_label_widget));
512
513 gtk_widget_show(GTK_WIDGET(m_box));
514
515 gtk_object_set_data(GTK_OBJECT(item), "owner", this);
e895ec45
DP
516 gtk_object_set_data(GTK_OBJECT(item), "data", data);
517 gtk_object_set_data(GTK_OBJECT(item), "parent", p);
e21a5048
DP
518
519 if (p != 0) {
520 if (p->subtree == NULL) {
521 GtkTree *tree = GTK_TREE(gtk_tree_new());
522 gtk_tree_item_set_subtree(GTK_TREE_ITEM(p), GTK_WIDGET(tree));
523 gtk_widget_show(GTK_WIDGET(tree));
524 p->expanded = 1;
525 }
526
527 gtk_container_add(GTK_CONTAINER(p->subtree), GTK_WIDGET(item));
528 } else {
529printf("Adding root\n");
530printf("m_tree = %p\n", m_tree);
531 m_anchor = item;
532 gtk_container_add(GTK_CONTAINER(m_tree), GTK_WIDGET(item));
d9692df7 533 }
f4e325b3 534
e21a5048 535 gtk_widget_show(GTK_WIDGET(item));
d9692df7 536
9fa72bd2
MR
537 g_signal_connect (item, "select",
538 G_CALLBACK (gtk_treeitem_select_callback), this);
539 g_signal_connect (item, "deselect",
540 G_CALLBACK (gtk_treeitem_select_callback), this);
541 g_signal_connect (item, "expand",
542 G_CALLBACK (gtk_treeitem_expand_callback), this);
543 g_signal_connect (item, "collapse",
544 G_CALLBACK (gtk_treeitem_collapse_callback), this);
e21a5048
DP
545
546 return item;
d9692df7
DP
547}
548
e21a5048
DP
549void wxTreeCtrl::Delete(const wxTreeItemId& item) {
550 if (!item.IsOk())
551 return;
d9692df7 552
e21a5048
DP
553 GtkTreeItem *parent = GTK_TREE_ITEM(GTK_WIDGET((GtkTreeItem *)item)->parent);
554 if (parent == NULL)
555 return;
d9692df7 556
e21a5048
DP
557 gtk_container_remove(GTK_CONTAINER(parent), GTK_WIDGET((GtkTreeItem *)item));
558
559 return;
d9692df7
DP
560}
561
e21a5048
DP
562void wxTreeCtrl::DeleteAllItems() {
563 gtk_tree_item_remove_subtree(m_anchor);
564}
d9692df7 565
e21a5048
DP
566void wxTreeCtrl::Expand(const wxTreeItemId& item) {
567 if (!item.IsOk())
568 return;
d9692df7 569
e21a5048 570 gtk_tree_item_expand(GTK_TREE_ITEM((GtkTreeItem *)item));
d9692df7
DP
571}
572
e21a5048
DP
573void wxTreeCtrl::Collapse(const wxTreeItemId& item) {
574 if (!item.IsOk())
575 return;
d9692df7 576
e21a5048
DP
577 gtk_tree_item_collapse(GTK_TREE_ITEM((GtkTreeItem *)item));
578}
d9692df7 579
e21a5048
DP
580void 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));
d9692df7
DP
586}
587
e21a5048
DP
588void wxTreeCtrl::Toggle(const wxTreeItemId& item) {
589 if (!item.IsOk())
590 return;
d9692df7 591
e21a5048
DP
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}
d9692df7 597
e1dc4cc6
DP
598static 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
e21a5048 615void wxTreeCtrl::Unselect() {
e1dc4cc6
DP
616 if (m_anchor == NULL)
617 return;
618
619 gtk_treectrl_unselect_callback(GTK_WIDGET(m_anchor), NULL);
d9692df7
DP
620}
621
e21a5048
DP
622void wxTreeCtrl::SelectItem(const wxTreeItemId& item) {
623 if (!item.IsOk())
624 return;
d9692df7 625
e21a5048
DP
626 gtk_tree_item_select((GtkTreeItem *)item);
627}
d9692df7 628
e21a5048
DP
629void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) {
630#warning "Need to implement EnsureVisible"
d9692df7
DP
631}
632
e21a5048
DP
633void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) {
634#warning "Need to implement ScrollTo"
635}
f4e325b3 636
e21a5048
DP
637wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
638 wxClassInfo* textControlClass) {
639 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
640#warning "Need to implement EditLabel"
e1dc4cc6
DP
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;
e21a5048 658}
d9692df7 659
e21a5048
DP
660wxTextCtrl* wxTreeCtrl::GetEditControl() const {
661 return m_textCtrl;
d9692df7
DP
662}
663
e21a5048
DP
664void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) {
665#warning "Need to implement EndEditLabel"
e1dc4cc6
DP
666/*
667 GtkLabel *m_label = (GtkLabel *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)m_editItem), "w_label");
a7c12d28 668 gtk_label_set_text(m_label, m_textCtrl->GetValue());
e1dc4cc6
DP
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*/
e21a5048 678}
befe54c6 679
e21a5048
DP
680void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) {
681 switch (action) {
682 case wxTREE_EXPAND_EXPAND:
683 Expand(item);
684 break;
d9692df7 685
e21a5048
DP
686 case wxTREE_EXPAND_COLLAPSE:
687 Collapse(item);
688 break;
c89a6106 689
e21a5048
DP
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 }
d9692df7 701}
f4e325b3 702
e21a5048
DP
703wxTreeItemId 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;
d9692df7 709}
f4e325b3 710
e21a5048
DP
711/* Old functions
712long wxTreeCtrl::GetChild(long item) const {
d9692df7 713 GtkTreeItem *p;
e21a5048 714 GtkTreeItem *next = NULL;
f4e325b3 715
d9692df7 716 p = findGtkTreeItem(item);
2e14a116 717 GList *list = gtk_container_get_children(GTK_CONTAINER(p));
73c902d6 718 next = GTK_TREE_ITEM(list->data);
f4e325b3 719
e21a5048
DP
720 if (next != NULL)
721 return (long)gtk_object_get_data(GTK_OBJECT(next), "id");
978f38c2 722
e21a5048
DP
723 return (-1);
724}
d9692df7 725
e21a5048
DP
726long wxTreeCtrl::GetFirstVisibleItem(void) const {
727 GtkTreeItem *next = NULL;
d9692df7 728
2e14a116 729 GList *list = gtk_container_get_children(GTK_CONTAINER(m_anchor));
73c902d6 730 next = GTK_TREE_ITEM(list->data);
e21a5048
DP
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");
978f38c2 735
e21a5048 736 return (-1);
d9692df7
DP
737}
738
e21a5048 739long wxTreeCtrl::GetNextVisibleItem(long item) const {
d9692df7 740 GtkTreeItem *p;
e21a5048 741 GtkTreeItem *next = NULL;
d9692df7
DP
742
743 p = findGtkTreeItem(item);
2e14a116 744 GList *list = gtk_container_get_children(GTK_CONTAINER(p));
73c902d6 745 next = GTK_TREE_ITEM(list->data);
e21a5048
DP
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");
978f38c2 750
e21a5048
DP
751 return (-1);
752}
753
754bool 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");
d9692df7 761 return FALSE;
e21a5048 762 }
d9692df7 763
e21a5048 764 wxConvertFromGtkTreeItem(info, p);
d9692df7
DP
765
766 return TRUE;
767}
768
e21a5048 769bool wxTreeCtrl::SetItem(wxTreeItem& info) {
d9692df7
DP
770 GtkTreeItem *p;
771
e21a5048 772 p = findGtkTreeItem(info.m_itemId);
d9692df7 773
e21a5048
DP
774 if (p == NULL) {
775 wxLogSysError("TreeCtrl::SetItem failed");
f4e325b3 776 return FALSE;
e21a5048 777 }
f4e325b3 778
e21a5048 779 wxConvertToGtkTreeItem(this, info, &p);
f4e325b3 780
e21a5048
DP
781 return TRUE;
782}
d9692df7 783
e21a5048
DP
784int wxTreeCtrl::GetItemState(long item, long stateMask) const {
785 wxTreeItem info;
f4e325b3 786
e21a5048
DP
787 info.m_mask = wxTREE_MASK_STATE ;
788 info.m_stateMask = stateMask;
789 info.m_itemId = item;
d9692df7 790
e21a5048
DP
791 if (!GetItem(info))
792 return 0;
793
794 return info.m_state;
795}
796
797bool 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/*
811bool 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));
f4e325b3
DP
819
820 return TRUE;
d9692df7 821}
e21a5048 822*/
f4e325b3 823
e21a5048
DP
824/*
825long wxTreeCtrl::InsertItem(long parent, wxTreeItem& info, long insertAfter) {
d9692df7
DP
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 }
f4e325b3 842
d9692df7
DP
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
9fa72bd2
MR
857 g_signal_connect (item, "select",
858 G_CALLBACK (gtk_treeitem_select_callback), this);
859 g_signal_connect (item, "deselect",
860 G_CALLBACK (gtk_treeitem_select_callback), this);
861 g_signal_connect (item, "expand",
862 G_CALLBACK (gtk_treeitem_expand_callback), this);
863 g_signal_connect (item, "collapse",
864 G_CALLBACK (gtk_treeitem_collapse_callback), this);
d9692df7
DP
865
866 return info.m_itemId;
f4e325b3
DP
867}
868
d9692df7
DP
869long wxTreeCtrl::InsertItem(long parent, const wxString& label, int image,
870 int selImage, long insertAfter) {
871
872 wxTreeItem info;
873 info.m_text = label;
874 info.m_mask = wxTREE_MASK_TEXT;
875 if (image > -1) {
876 info.m_mask |= wxTREE_MASK_IMAGE | wxTREE_MASK_SELECTED_IMAGE;
877 info.m_image = image;
878 if (selImage == -1)
879 info.m_selectedImage = image;
880 else
881 info.m_selectedImage = selImage;
882 }
f4e325b3 883
d9692df7 884 return InsertItem(parent, info, insertAfter);
f4e325b3 885}
d9692df7 886*/
f4e325b3 887
e895ec45
DP
888void wxTreeCtrl::SendMessage(wxEventType command, const wxTreeItemId& item) {
889 wxTreeEvent event(command, GetId());
d9692df7 890 event.SetEventObject(this);
e895ec45 891 event.m_item = item;
d9692df7
DP
892 ProcessEvent(event);
893}
f4e325b3 894
e895ec45
DP
895void wxTreeCtrl::SendExpanding(const wxTreeItemId& item) {
896 SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDING, item);
897}
898
e21a5048 899void wxTreeCtrl::SendExpanded(const wxTreeItemId& item) {
e895ec45 900 SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDED, item);
d9692df7 901}
f4e325b3 902
e21a5048 903void wxTreeCtrl::SendCollapsing(const wxTreeItemId& item) {
e895ec45 904 SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, item);
d9692df7 905}
f4e325b3 906
e21a5048 907void wxTreeCtrl::SendCollapsed(const wxTreeItemId& item) {
e895ec45 908 SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, item);
d9692df7 909}
f4e325b3 910
e21a5048 911void wxTreeCtrl::SendSelChanging(const wxTreeItemId& item) {
e895ec45 912 SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGED, item);
f4e325b3
DP
913}
914
e21a5048 915void wxTreeCtrl::SendSelChanged(const wxTreeItemId& item) {
e895ec45 916 SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGING, item);
d9692df7 917}
f4e325b3 918
d9692df7
DP
919// Tree event
920IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxCommandEvent)
921
e21a5048
DP
922wxTreeEvent::wxTreeEvent(wxEventType commandType, int id):
923 wxCommandEvent(commandType, id) {
d9692df7 924 m_code = 0;
e21a5048 925 m_itemOld = 0;
d9692df7 926}