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