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