]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
a3622daa | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "listbox.h" | |
13 | #endif | |
14 | ||
6a6d4eed | 15 | #include "wx/dynarray.h" |
c801d85f | 16 | #include "wx/listbox.h" |
09cf7c58 | 17 | #include "wx/utils.h" |
caaa4cfd RR |
18 | #include "wx/intl.h" |
19 | #include "wx/checklst.h" | |
291a8f20 RR |
20 | |
21 | #if wxUSE_TOOLTIPS | |
b1170810 | 22 | #include "wx/tooltip.h" |
291a8f20 | 23 | #endif |
c801d85f | 24 | |
06cfab17 RR |
25 | #if wxUSE_DRAG_AND_DROP |
26 | #include "wx/dnd.h" | |
27 | #endif | |
28 | ||
83624f79 RR |
29 | #include "gdk/gdk.h" |
30 | #include "gtk/gtk.h" | |
31 | ||
38c7b3d3 RR |
32 | //------------------------------------------------------------------------- |
33 | // conditional compilation | |
34 | //------------------------------------------------------------------------- | |
35 | ||
36 | #if (GTK_MINOR_VERSION == 1) | |
37 | #if (GTK_MICRO_VERSION >= 5) | |
38 | #define NEW_GTK_SCROLL_CODE | |
39 | #endif | |
40 | #endif | |
41 | ||
66bd6b93 RR |
42 | //----------------------------------------------------------------------------- |
43 | // data | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | extern bool g_blockEventsOnDrag; | |
caaa4cfd RR |
47 | extern bool g_blockEventsOnScroll; |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // "button_press_event" | |
51 | //----------------------------------------------------------------------------- | |
52 | ||
ff8bfdbb | 53 | static gint |
caaa4cfd RR |
54 | gtk_listbox_button_press_callback( GtkWidget *widget, GdkEventButton *gdk_event, wxListBox *listbox ) |
55 | { | |
56 | if (g_blockEventsOnDrag) return FALSE; | |
57 | if (g_blockEventsOnScroll) return FALSE; | |
58 | ||
59 | if (!listbox->HasVMT()) return FALSE; | |
60 | ||
caaa4cfd | 61 | int sel = listbox->GetIndex( widget ); |
ff8bfdbb | 62 | |
4f22cf8d RR |
63 | if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS)) |
64 | { | |
65 | wxCheckListBox *clb = (wxCheckListBox *)listbox; | |
ff8bfdbb | 66 | |
4f22cf8d | 67 | clb->Check( sel, !clb->IsChecked(sel) ); |
ff8bfdbb | 68 | |
4f22cf8d RR |
69 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
70 | event.SetEventObject( listbox ); | |
71 | event.SetInt( sel ); | |
72 | listbox->GetEventHandler()->ProcessEvent( event ); | |
73 | } | |
ff8bfdbb | 74 | |
4f22cf8d RR |
75 | if (gdk_event->type == GDK_2BUTTON_PRESS) |
76 | { | |
77 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); | |
78 | event.SetEventObject( listbox ); | |
ff8bfdbb | 79 | |
5b077d48 RR |
80 | wxArrayInt aSelections; |
81 | int count = listbox->GetSelections(aSelections); | |
82 | if ( count > 0 ) | |
83 | { | |
84 | event.m_commandInt = aSelections[0] ; | |
85 | event.m_clientData = listbox->GetClientData( event.m_commandInt ); | |
86 | wxString str(listbox->GetString(event.m_commandInt)); | |
87 | if (str != "") event.m_commandString = copystring((char *)(const char *)str); | |
88 | } | |
89 | else | |
90 | { | |
91 | event.m_commandInt = -1 ; | |
92 | event.m_commandString = copystring("") ; | |
93 | } | |
94 | ||
4f22cf8d | 95 | listbox->GetEventHandler()->ProcessEvent( event ); |
ff8bfdbb | 96 | |
5b077d48 | 97 | if (event.m_commandString) delete[] event.m_commandString ; |
4f22cf8d RR |
98 | } |
99 | ||
caaa4cfd RR |
100 | return FALSE; |
101 | } | |
66bd6b93 | 102 | |
1144d24d RR |
103 | //----------------------------------------------------------------------------- |
104 | // "key_press_event" | |
105 | //----------------------------------------------------------------------------- | |
106 | ||
ff8bfdbb | 107 | static gint |
1144d24d RR |
108 | gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox ) |
109 | { | |
110 | if (g_blockEventsOnDrag) return FALSE; | |
111 | ||
112 | if (!listbox->HasVMT()) return FALSE; | |
113 | ||
114 | if (gdk_event->keyval != ' ') return FALSE; | |
ff8bfdbb | 115 | |
1144d24d | 116 | int sel = listbox->GetIndex( widget ); |
ff8bfdbb | 117 | |
1144d24d | 118 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
ff8bfdbb | 119 | |
1144d24d | 120 | clb->Check( sel, !clb->IsChecked(sel) ); |
ff8bfdbb | 121 | |
4f22cf8d RR |
122 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
123 | event.SetEventObject( listbox ); | |
124 | event.SetInt( sel ); | |
125 | listbox->GetEventHandler()->ProcessEvent( event ); | |
ff8bfdbb | 126 | |
1144d24d RR |
127 | return FALSE; |
128 | } | |
129 | ||
c801d85f | 130 | //----------------------------------------------------------------------------- |
a60c99e6 | 131 | // "select" and "deselect" |
c801d85f KB |
132 | //----------------------------------------------------------------------------- |
133 | ||
09cf7c58 | 134 | static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox ) |
c801d85f | 135 | { |
fd0eed64 RR |
136 | if (!listbox->HasVMT()) return; |
137 | if (g_blockEventsOnDrag) return; | |
dcf40a56 | 138 | |
fd0eed64 | 139 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
dcf40a56 | 140 | |
09cf7c58 RR |
141 | wxArrayInt aSelections; |
142 | int count = listbox->GetSelections(aSelections); | |
143 | if ( count > 0 ) | |
144 | { | |
fd0eed64 RR |
145 | event.m_commandInt = aSelections[0] ; |
146 | event.m_clientData = listbox->GetClientData( event.m_commandInt ); | |
147 | wxString str(listbox->GetString(event.m_commandInt)); | |
148 | if (str != "") event.m_commandString = copystring((char *)(const char *)str); | |
09cf7c58 RR |
149 | } |
150 | else | |
151 | { | |
fd0eed64 RR |
152 | event.m_commandInt = -1 ; |
153 | event.m_commandString = copystring("") ; | |
09cf7c58 RR |
154 | } |
155 | ||
fd0eed64 | 156 | event.SetEventObject( listbox ); |
a3622daa | 157 | |
fd0eed64 RR |
158 | listbox->GetEventHandler()->ProcessEvent( event ); |
159 | if (event.m_commandString) delete[] event.m_commandString ; | |
6de97a3b | 160 | } |
c801d85f | 161 | |
a60c99e6 RR |
162 | //----------------------------------------------------------------------------- |
163 | // wxListBox | |
c801d85f KB |
164 | //----------------------------------------------------------------------------- |
165 | ||
166 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) | |
167 | ||
fd0eed64 | 168 | wxListBox::wxListBox() |
c801d85f | 169 | { |
fd0eed64 | 170 | m_list = (GtkList *) NULL; |
caaa4cfd | 171 | m_hasCheckBoxes = FALSE; |
6de97a3b | 172 | } |
c801d85f | 173 | |
dcf40a56 | 174 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
175 | const wxPoint &pos, const wxSize &size, |
176 | int n, const wxString choices[], | |
177 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 178 | { |
fd0eed64 | 179 | m_needParent = TRUE; |
b292e2f5 | 180 | m_acceptsFocus = TRUE; |
dcf40a56 | 181 | |
fd0eed64 | 182 | PreCreation( parent, id, pos, size, style, name ); |
dcf40a56 | 183 | |
fd0eed64 | 184 | SetValidator( validator ); |
6de97a3b | 185 | |
fd0eed64 RR |
186 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
187 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
188 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
ff8bfdbb | 189 | |
fd0eed64 | 190 | m_list = GTK_LIST( gtk_list_new() ); |
dcf40a56 | 191 | |
fd0eed64 RR |
192 | GtkSelectionMode mode = GTK_SELECTION_BROWSE; |
193 | if (style & wxLB_MULTIPLE) | |
194 | mode = GTK_SELECTION_MULTIPLE; | |
195 | else if (style & wxLB_EXTENDED) | |
196 | mode = GTK_SELECTION_EXTENDED; | |
6a6d4eed | 197 | |
fd0eed64 | 198 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); |
dcf40a56 | 199 | |
38c7b3d3 RR |
200 | #ifdef NEW_GTK_SCROLL_CODE |
201 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); | |
202 | #else | |
fd0eed64 | 203 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) ); |
38c7b3d3 RR |
204 | #endif |
205 | ||
b292e2f5 RR |
206 | #ifdef __WXDEBUG__ |
207 | debug_focus_in( m_widget, "wxListBox::m_widget", name ); | |
ff8bfdbb | 208 | |
b292e2f5 RR |
209 | debug_focus_in( GTK_WIDGET(m_list), "wxListBox::m_list", name ); |
210 | ||
211 | GtkScrolledWindow *s_window = GTK_SCROLLED_WINDOW(m_widget); | |
212 | ||
213 | debug_focus_in( s_window->hscrollbar, "wxWindow::hsrcollbar", name ); | |
214 | debug_focus_in( s_window->vscrollbar, "wxWindow::vsrcollbar", name ); | |
215 | ||
216 | #ifdef NEW_GTK_SCROLL_CODE | |
217 | GtkViewport *viewport = GTK_VIEWPORT(s_window->child); | |
218 | #else | |
219 | GtkViewport *viewport = GTK_VIEWPORT(s_window->viewport); | |
220 | #endif | |
221 | ||
222 | debug_focus_in( GTK_WIDGET(viewport), "wxWindow::viewport", name ); | |
223 | #endif | |
224 | ||
fd0eed64 | 225 | gtk_widget_show( GTK_WIDGET(m_list) ); |
dcf40a56 | 226 | |
fd0eed64 RR |
227 | wxSize newSize = size; |
228 | if (newSize.x == -1) newSize.x = 100; | |
229 | if (newSize.y == -1) newSize.y = 110; | |
230 | SetSize( newSize.x, newSize.y ); | |
dcf40a56 | 231 | |
fd0eed64 RR |
232 | for (int i = 0; i < n; i++) |
233 | { | |
f5e27805 RR |
234 | m_clientDataList.Append( (wxObject*) NULL ); |
235 | m_clientObjectList.Append( (wxObject*) NULL ); | |
ff8bfdbb | 236 | |
fd0eed64 | 237 | GtkWidget *list_item; |
ff8bfdbb VZ |
238 | |
239 | if (m_hasCheckBoxes) | |
240 | { | |
caaa4cfd | 241 | wxString str = "[-] "; |
ff8bfdbb | 242 | str += choices[i]; |
caaa4cfd | 243 | list_item = gtk_list_item_new_with_label( str ); |
ff8bfdbb VZ |
244 | } |
245 | else | |
246 | { | |
caaa4cfd | 247 | list_item = gtk_list_item_new_with_label( choices[i] ); |
ff8bfdbb | 248 | } |
dcf40a56 | 249 | |
b292e2f5 RR |
250 | #ifdef __WXDEBUG__ |
251 | debug_focus_in( list_item, "wxListBox::list_item", name ); | |
ff8bfdbb VZ |
252 | #endif |
253 | ||
fd0eed64 | 254 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); |
dcf40a56 | 255 | |
fd0eed64 RR |
256 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
257 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
dcf40a56 | 258 | |
fd0eed64 RR |
259 | if (style & wxLB_MULTIPLE) |
260 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", | |
261 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
dcf40a56 | 262 | |
ff8bfdbb VZ |
263 | gtk_signal_connect( GTK_OBJECT(list_item), |
264 | "button_press_event", | |
265 | (GtkSignalFunc)gtk_listbox_button_press_callback, | |
266 | (gpointer) this ); | |
267 | ||
268 | if (m_hasCheckBoxes) | |
269 | { | |
270 | gtk_signal_connect( GTK_OBJECT(list_item), | |
271 | "key_press_event", | |
272 | (GtkSignalFunc)gtk_listbox_key_press_callback, | |
273 | (gpointer)this ); | |
274 | } | |
275 | ||
276 | ConnectWidget( list_item ); | |
277 | ||
fd0eed64 RR |
278 | gtk_widget_show( list_item ); |
279 | } | |
dcf40a56 | 280 | |
fd0eed64 | 281 | m_parent->AddChild( this ); |
6ca41e57 | 282 | |
fd0eed64 | 283 | (m_parent->m_insertCallback)( m_parent, this ); |
ff8bfdbb | 284 | |
fd0eed64 | 285 | PostCreation(); |
dcf40a56 | 286 | |
fd0eed64 | 287 | gtk_widget_realize( GTK_WIDGET(m_list) ); |
dcf40a56 | 288 | |
fd0eed64 RR |
289 | SetBackgroundColour( parent->GetBackgroundColour() ); |
290 | SetForegroundColour( parent->GetForegroundColour() ); | |
f96aa4d9 | 291 | |
fd0eed64 | 292 | Show( TRUE ); |
dcf40a56 | 293 | |
fd0eed64 | 294 | return TRUE; |
6de97a3b | 295 | } |
c801d85f | 296 | |
fd0eed64 | 297 | wxListBox::~wxListBox() |
c801d85f | 298 | { |
caaa4cfd | 299 | Clear(); |
6de97a3b | 300 | } |
c801d85f | 301 | |
fd0eed64 | 302 | void wxListBox::AppendCommon( const wxString &item ) |
c801d85f | 303 | { |
fd0eed64 | 304 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
ff8bfdbb | 305 | |
caaa4cfd | 306 | GtkWidget *list_item; |
ff8bfdbb | 307 | |
caaa4cfd RR |
308 | if (m_hasCheckBoxes) |
309 | { | |
310 | wxString str = "[-] "; | |
ff8bfdbb | 311 | str += item; |
caaa4cfd RR |
312 | list_item = gtk_list_item_new_with_label( str ); |
313 | } | |
314 | else | |
315 | { | |
316 | list_item = gtk_list_item_new_with_label( item ); | |
317 | } | |
fc54776e | 318 | |
3502e687 RR |
319 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); |
320 | ||
fd0eed64 RR |
321 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
322 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
dcf40a56 | 323 | |
fd0eed64 RR |
324 | if (GetWindowStyleFlag() & wxLB_MULTIPLE) |
325 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", | |
326 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
dcf40a56 | 327 | |
fd0eed64 | 328 | if (m_widgetStyle) ApplyWidgetStyle(); |
ff8bfdbb VZ |
329 | |
330 | gtk_signal_connect( GTK_OBJECT(list_item), | |
331 | "button_press_event", | |
332 | (GtkSignalFunc)gtk_listbox_button_press_callback, | |
333 | (gpointer) this ); | |
334 | ||
b666df2c RR |
335 | if (m_hasCheckBoxes) |
336 | { | |
ff8bfdbb VZ |
337 | gtk_signal_connect( GTK_OBJECT(list_item), |
338 | "key_press_event", | |
339 | (GtkSignalFunc)gtk_listbox_key_press_callback, | |
340 | (gpointer)this ); | |
b666df2c | 341 | } |
ff8bfdbb | 342 | |
fd0eed64 RR |
343 | gtk_widget_show( list_item ); |
344 | ||
345 | ConnectWidget( list_item ); | |
346 | ||
06cfab17 | 347 | #if wxUSE_DRAG_AND_DROP |
38c7b3d3 | 348 | #ifndef NEW_GTK_DND_CODE |
33a5bc52 | 349 | if (m_dropTarget) m_dropTarget->RegisterWidget( list_item ); |
38c7b3d3 | 350 | #endif |
ac57418f | 351 | #endif |
b1170810 | 352 | |
291a8f20 | 353 | #if wxUSE_TOOLTIPS |
301cd871 | 354 | if (m_toolTip) m_toolTip->Apply( this ); |
291a8f20 | 355 | #endif |
fd0eed64 RR |
356 | } |
357 | ||
358 | void wxListBox::Append( const wxString &item ) | |
359 | { | |
f5e27805 RR |
360 | m_clientDataList.Append( (wxObject*) NULL ); |
361 | m_clientObjectList.Append( (wxObject*) NULL ); | |
ff8bfdbb | 362 | |
fd0eed64 RR |
363 | AppendCommon( item ); |
364 | } | |
365 | ||
366 | void wxListBox::Append( const wxString &item, void *clientData ) | |
367 | { | |
f5e27805 RR |
368 | m_clientDataList.Append( (wxObject*) clientData ); |
369 | m_clientObjectList.Append( (wxObject*) NULL ); | |
ff8bfdbb | 370 | |
fd0eed64 RR |
371 | AppendCommon( item ); |
372 | } | |
dcf40a56 | 373 | |
fd0eed64 RR |
374 | void wxListBox::Append( const wxString &item, wxClientData *clientData ) |
375 | { | |
f5e27805 RR |
376 | m_clientObjectList.Append( (wxObject*) clientData ); |
377 | m_clientDataList.Append( (wxObject*) NULL ); | |
ff8bfdbb | 378 | |
fd0eed64 RR |
379 | AppendCommon( item ); |
380 | } | |
dcf40a56 | 381 | |
fd0eed64 RR |
382 | void wxListBox::SetClientData( int n, void* clientData ) |
383 | { | |
384 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
ff8bfdbb | 385 | |
fd0eed64 RR |
386 | wxNode *node = m_clientDataList.Nth( n ); |
387 | if (!node) return; | |
ff8bfdbb | 388 | |
f5e27805 | 389 | node->SetData( (wxObject*) clientData ); |
fd0eed64 | 390 | } |
dcf40a56 | 391 | |
fd0eed64 RR |
392 | void* wxListBox::GetClientData( int n ) |
393 | { | |
394 | wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" ); | |
ff8bfdbb | 395 | |
fd0eed64 RR |
396 | wxNode *node = m_clientDataList.Nth( n ); |
397 | if (!node) return NULL; | |
ff8bfdbb | 398 | |
f5e27805 | 399 | return node->Data(); |
fd0eed64 | 400 | } |
dcf40a56 | 401 | |
fd0eed64 RR |
402 | void wxListBox::SetClientObject( int n, wxClientData* clientData ) |
403 | { | |
404 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
ff8bfdbb | 405 | |
f5e27805 | 406 | wxNode *node = m_clientObjectList.Nth( n ); |
fd0eed64 | 407 | if (!node) return; |
ff8bfdbb | 408 | |
fd0eed64 RR |
409 | wxClientData *cd = (wxClientData*) node->Data(); |
410 | if (cd) delete cd; | |
ff8bfdbb | 411 | |
fd0eed64 | 412 | node->SetData( (wxObject*) clientData ); |
6de97a3b | 413 | } |
c801d85f | 414 | |
fd0eed64 | 415 | wxClientData* wxListBox::GetClientObject( int n ) |
c801d85f | 416 | { |
fd0eed64 | 417 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" ); |
ff8bfdbb | 418 | |
f5e27805 | 419 | wxNode *node = m_clientObjectList.Nth( n ); |
fd0eed64 | 420 | if (!node) return (wxClientData*) NULL; |
ff8bfdbb | 421 | |
fd0eed64 RR |
422 | return (wxClientData*) node->Data(); |
423 | } | |
424 | ||
425 | void wxListBox::Clear() | |
426 | { | |
427 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); | |
fc54776e | 428 | |
fd0eed64 | 429 | gtk_list_clear_items( m_list, 0, Number() ); |
dcf40a56 | 430 | |
f5e27805 | 431 | wxNode *node = m_clientObjectList.First(); |
fd0eed64 RR |
432 | while (node) |
433 | { | |
434 | wxClientData *cd = (wxClientData*)node->Data(); | |
435 | if (cd) delete cd; | |
436 | node = node->Next(); | |
437 | } | |
f5e27805 | 438 | m_clientObjectList.Clear(); |
ff8bfdbb | 439 | |
fd0eed64 | 440 | m_clientDataList.Clear(); |
6de97a3b | 441 | } |
c801d85f KB |
442 | |
443 | void wxListBox::Delete( int n ) | |
444 | { | |
fd0eed64 | 445 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
fc54776e | 446 | |
fd0eed64 | 447 | GList *child = g_list_nth( m_list->children, n ); |
dcf40a56 | 448 | |
caaa4cfd | 449 | wxCHECK_RET( child, "wrong listbox index" ); |
dcf40a56 | 450 | |
bbe0af5b | 451 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
452 | gtk_list_remove_items( m_list, list ); |
453 | g_list_free( list ); | |
dcf40a56 | 454 | |
f5e27805 RR |
455 | wxNode *node = m_clientObjectList.Nth( n ); |
456 | if (node) | |
fd0eed64 RR |
457 | { |
458 | wxClientData *cd = (wxClientData*)node->Data(); | |
459 | if (cd) delete cd; | |
f5e27805 RR |
460 | m_clientObjectList.DeleteNode( node ); |
461 | } | |
ff8bfdbb | 462 | |
f5e27805 RR |
463 | node = m_clientDataList.Nth( n ); |
464 | if (node) | |
465 | { | |
fd0eed64 RR |
466 | m_clientDataList.DeleteNode( node ); |
467 | } | |
6de97a3b | 468 | } |
c801d85f KB |
469 | |
470 | void wxListBox::Deselect( int n ) | |
471 | { | |
fd0eed64 | 472 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
fc54776e | 473 | |
fd0eed64 | 474 | gtk_list_unselect_item( m_list, n ); |
6de97a3b | 475 | } |
c801d85f KB |
476 | |
477 | int wxListBox::FindString( const wxString &item ) const | |
478 | { | |
fd0eed64 | 479 | wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" ); |
ff8bfdbb | 480 | |
fd0eed64 RR |
481 | GList *child = m_list->children; |
482 | int count = 0; | |
483 | while (child) | |
484 | { | |
485 | GtkBin *bin = GTK_BIN( child->data ); | |
486 | GtkLabel *label = GTK_LABEL( bin->child ); | |
ff8bfdbb VZ |
487 | |
488 | wxString str = label->label; | |
489 | if (m_hasCheckBoxes) str.Remove( 0, 4 ); | |
490 | ||
caaa4cfd | 491 | if (str == item) return count; |
ff8bfdbb | 492 | |
fd0eed64 RR |
493 | count++; |
494 | child = child->next; | |
495 | } | |
496 | ||
497 | // it's not an error if the string is not found -> no wxCHECK | |
dcf40a56 | 498 | |
c801d85f | 499 | return -1; |
6de97a3b | 500 | } |
c801d85f | 501 | |
fd0eed64 | 502 | int wxListBox::GetSelection() const |
c801d85f | 503 | { |
fd0eed64 | 504 | wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" ); |
ff8bfdbb | 505 | |
fd0eed64 RR |
506 | GList *child = m_list->children; |
507 | int count = 0; | |
508 | while (child) | |
509 | { | |
510 | if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count; | |
511 | count++; | |
512 | child = child->next; | |
513 | } | |
514 | return -1; | |
6de97a3b | 515 | } |
c801d85f | 516 | |
fd0eed64 | 517 | int wxListBox::GetSelections( wxArrayInt& aSelections ) const |
c801d85f | 518 | { |
fd0eed64 | 519 | wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" ); |
ff8bfdbb | 520 | |
fd0eed64 RR |
521 | // get the number of selected items first |
522 | GList *child = m_list->children; | |
523 | int count = 0; | |
524 | for (child = m_list->children; child != NULL; child = child->next) | |
525 | { | |
526 | if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) | |
527 | count++; | |
528 | } | |
c801d85f | 529 | |
fd0eed64 RR |
530 | aSelections.Empty(); |
531 | ||
ff8bfdbb | 532 | if (count > 0) |
868a2826 | 533 | { |
fd0eed64 RR |
534 | // now fill the list |
535 | aSelections.Alloc(count); // optimization attempt | |
536 | int i = 0; | |
537 | for (child = m_list->children; child != NULL; child = child->next, i++) | |
538 | { | |
539 | if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) | |
540 | aSelections.Add(i); | |
541 | } | |
6a6d4eed | 542 | } |
dcf40a56 | 543 | |
fd0eed64 | 544 | return count; |
6de97a3b | 545 | } |
c801d85f KB |
546 | |
547 | wxString wxListBox::GetString( int n ) const | |
548 | { | |
fd0eed64 | 549 | wxCHECK_MSG( m_list != NULL, "", "invalid listbox" ); |
ff8bfdbb | 550 | |
fd0eed64 RR |
551 | GList *child = g_list_nth( m_list->children, n ); |
552 | if (child) | |
553 | { | |
554 | GtkBin *bin = GTK_BIN( child->data ); | |
555 | GtkLabel *label = GTK_LABEL( bin->child ); | |
ff8bfdbb VZ |
556 | |
557 | wxString str = label->label; | |
558 | if (m_hasCheckBoxes) str.Remove( 0, 4 ); | |
559 | ||
caaa4cfd | 560 | return str; |
fd0eed64 RR |
561 | } |
562 | wxFAIL_MSG("wrong listbox index"); | |
563 | return ""; | |
6de97a3b | 564 | } |
c801d85f | 565 | |
fd0eed64 | 566 | wxString wxListBox::GetStringSelection() const |
c801d85f | 567 | { |
fd0eed64 | 568 | wxCHECK_MSG( m_list != NULL, "", "invalid listbox" ); |
ff8bfdbb | 569 | |
fd0eed64 RR |
570 | GList *selection = m_list->selection; |
571 | if (selection) | |
572 | { | |
573 | GtkBin *bin = GTK_BIN( selection->data ); | |
caaa4cfd | 574 | GtkLabel *label = GTK_LABEL( bin->child ); |
ff8bfdbb VZ |
575 | |
576 | wxString str = label->label; | |
577 | if (m_hasCheckBoxes) str.Remove( 0, 4 ); | |
578 | ||
caaa4cfd | 579 | return str; |
fd0eed64 | 580 | } |
ff8bfdbb | 581 | |
fd0eed64 RR |
582 | wxFAIL_MSG("no listbox selection available"); |
583 | return ""; | |
6de97a3b | 584 | } |
c801d85f | 585 | |
fd0eed64 | 586 | int wxListBox::Number() |
c801d85f | 587 | { |
fd0eed64 | 588 | wxCHECK_MSG( m_list != NULL, -1, "invalid listbox" ); |
ff8bfdbb | 589 | |
fd0eed64 RR |
590 | GList *child = m_list->children; |
591 | int count = 0; | |
592 | while (child) { count++; child = child->next; } | |
593 | return count; | |
6de97a3b | 594 | } |
c801d85f | 595 | |
debe6624 | 596 | bool wxListBox::Selected( int n ) |
c801d85f | 597 | { |
fd0eed64 | 598 | wxCHECK_MSG( m_list != NULL, FALSE, "invalid listbox" ); |
ff8bfdbb | 599 | |
fd0eed64 RR |
600 | GList *target = g_list_nth( m_list->children, n ); |
601 | if (target) | |
c801d85f | 602 | { |
fd0eed64 RR |
603 | GList *child = m_list->selection; |
604 | while (child) | |
605 | { | |
606 | if (child->data == target->data) return TRUE; | |
607 | child = child->next; | |
608 | } | |
6de97a3b | 609 | } |
fd0eed64 RR |
610 | wxFAIL_MSG("wrong listbox index"); |
611 | return FALSE; | |
6de97a3b | 612 | } |
c801d85f | 613 | |
debe6624 | 614 | void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) ) |
c801d85f | 615 | { |
fd0eed64 | 616 | wxFAIL_MSG("wxListBox::Set not implemented"); |
6de97a3b | 617 | } |
c801d85f KB |
618 | |
619 | void wxListBox::SetFirstItem( int WXUNUSED(n) ) | |
620 | { | |
fd0eed64 | 621 | wxFAIL_MSG("wxListBox::SetFirstItem not implemented"); |
6de97a3b | 622 | } |
c801d85f KB |
623 | |
624 | void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) ) | |
625 | { | |
fd0eed64 | 626 | wxFAIL_MSG("wxListBox::SetFirstItem not implemented"); |
6de97a3b | 627 | } |
c801d85f | 628 | |
debe6624 | 629 | void wxListBox::SetSelection( int n, bool select ) |
c801d85f | 630 | { |
fd0eed64 | 631 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
ff8bfdbb | 632 | |
fd0eed64 RR |
633 | if (select) |
634 | gtk_list_select_item( m_list, n ); | |
635 | else | |
636 | gtk_list_unselect_item( m_list, n ); | |
6de97a3b | 637 | } |
c801d85f | 638 | |
09cf7c58 | 639 | void wxListBox::SetString( int n, const wxString &string ) |
c801d85f | 640 | { |
fd0eed64 | 641 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
ff8bfdbb | 642 | |
fd0eed64 RR |
643 | GList *child = g_list_nth( m_list->children, n ); |
644 | if (child) | |
645 | { | |
646 | GtkBin *bin = GTK_BIN( child->data ); | |
647 | GtkLabel *label = GTK_LABEL( bin->child ); | |
ff8bfdbb VZ |
648 | |
649 | wxString str; | |
650 | if (m_hasCheckBoxes) str += "[-] "; | |
651 | str += string; | |
652 | ||
caaa4cfd | 653 | gtk_label_set( label, str ); |
fd0eed64 RR |
654 | } |
655 | else | |
656 | { | |
657 | wxFAIL_MSG("wrong listbox index"); | |
ff8bfdbb | 658 | } |
6de97a3b | 659 | } |
c801d85f | 660 | |
debe6624 | 661 | void wxListBox::SetStringSelection( const wxString &string, bool select ) |
c801d85f | 662 | { |
fd0eed64 | 663 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
ff8bfdbb | 664 | |
fd0eed64 | 665 | SetSelection( FindString(string), select ); |
6de97a3b | 666 | } |
c801d85f KB |
667 | |
668 | int wxListBox::GetIndex( GtkWidget *item ) const | |
669 | { | |
fd0eed64 | 670 | if (item) |
c801d85f | 671 | { |
fd0eed64 RR |
672 | GList *child = m_list->children; |
673 | int count = 0; | |
674 | while (child) | |
675 | { | |
676 | if (GTK_WIDGET(child->data) == item) return count; | |
677 | count++; | |
678 | child = child->next; | |
679 | } | |
6de97a3b | 680 | } |
fd0eed64 | 681 | return -1; |
6de97a3b | 682 | } |
c801d85f | 683 | |
ff8bfdbb | 684 | #if wxUSE_TOOLTIPS |
301cd871 | 685 | void wxListBox::ApplyToolTip( GtkTooltips *tips, const char *tip ) |
b1170810 | 686 | { |
b1170810 RR |
687 | GList *child = m_list->children; |
688 | while (child) | |
689 | { | |
301cd871 | 690 | gtk_tooltips_set_tip( tips, GTK_WIDGET( child->data ), tip, (gchar*) NULL ); |
b1170810 RR |
691 | child = child->next; |
692 | } | |
693 | } | |
ff8bfdbb | 694 | #endif // wxUSE_TOOLTIPS |
b1170810 | 695 | |
06cfab17 | 696 | #if wxUSE_DRAG_AND_DROP |
a60c99e6 RR |
697 | void wxListBox::SetDropTarget( wxDropTarget *dropTarget ) |
698 | { | |
fd0eed64 | 699 | wxCHECK_RET( m_list != NULL, "invalid listbox" ); |
ff8bfdbb | 700 | |
38c7b3d3 | 701 | #ifndef NEW_GTK_DND_CODE |
33a5bc52 | 702 | if (m_dropTarget) |
fd0eed64 | 703 | { |
33a5bc52 RR |
704 | GList *child = m_list->children; |
705 | while (child) | |
706 | { | |
ff8bfdbb | 707 | m_dropTarget->UnregisterWidget( GTK_WIDGET( child->data ) ); |
33a5bc52 RR |
708 | child = child->next; |
709 | } | |
fd0eed64 | 710 | } |
fed46e72 | 711 | #endif |
ff8bfdbb | 712 | |
38c7b3d3 | 713 | wxWindow::SetDropTarget( dropTarget ); |
fed46e72 RR |
714 | |
715 | #ifndef NEW_GTK_DND_CODE | |
33a5bc52 | 716 | if (m_dropTarget) |
fd0eed64 | 717 | { |
33a5bc52 RR |
718 | GList *child = m_list->children; |
719 | while (child) | |
720 | { | |
ff8bfdbb | 721 | m_dropTarget->RegisterWidget( GTK_WIDGET( child->data ) ); |
33a5bc52 RR |
722 | child = child->next; |
723 | } | |
fd0eed64 | 724 | } |
38c7b3d3 | 725 | #endif |
a60c99e6 | 726 | } |
ac57418f | 727 | #endif |
a60c99e6 | 728 | |
fd0eed64 | 729 | GtkWidget *wxListBox::GetConnectWidget() |
e3e65dac | 730 | { |
fd0eed64 | 731 | return GTK_WIDGET(m_list); |
6de97a3b | 732 | } |
e3e65dac | 733 | |
f96aa4d9 | 734 | bool wxListBox::IsOwnGtkWindow( GdkWindow *window ) |
868a2826 | 735 | { |
fd0eed64 | 736 | if (wxWindow::IsOwnGtkWindow( window )) return TRUE; |
a60c99e6 | 737 | |
fd0eed64 RR |
738 | GList *child = m_list->children; |
739 | while (child) | |
740 | { | |
741 | GtkWidget *bin = GTK_WIDGET( child->data ); | |
742 | if (bin->window == window) return TRUE; | |
743 | child = child->next; | |
744 | } | |
f96aa4d9 | 745 | |
fd0eed64 | 746 | return FALSE; |
868a2826 | 747 | } |
e3e65dac | 748 | |
58614078 | 749 | void wxListBox::ApplyWidgetStyle() |
c058d771 | 750 | { |
fd0eed64 | 751 | SetWidgetStyle(); |
ff8bfdbb | 752 | |
e380f72b RR |
753 | if (m_backgroundColour.Ok()) |
754 | { | |
755 | GdkWindow *window = GTK_WIDGET(m_list)->window; | |
756 | m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) ); | |
757 | gdk_window_set_background( window, m_backgroundColour.GetColor() ); | |
758 | gdk_window_clear( window ); | |
759 | } | |
ff8bfdbb | 760 | |
fd0eed64 RR |
761 | GList *child = m_list->children; |
762 | while (child) | |
763 | { | |
fd0eed64 | 764 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); |
ff8bfdbb | 765 | |
caaa4cfd RR |
766 | GtkBin *bin = GTK_BIN( child->data ); |
767 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
768 | gtk_widget_set_style( label, m_widgetStyle ); | |
ff8bfdbb | 769 | |
fd0eed64 RR |
770 | child = child->next; |
771 | } | |
68dda785 | 772 | } |
b1170810 | 773 |