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