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