]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: listbox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
12 | #pragma implementation "listbox.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #include "wx/defs.h" | |
19 | ||
20 | #if wxUSE_LISTBOX | |
21 | ||
22 | #include "wx/listbox.h" | |
23 | #include "wx/dynarray.h" | |
24 | #include "wx/arrstr.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/intl.h" | |
27 | #include "wx/checklst.h" | |
28 | #include "wx/settings.h" | |
29 | #include "wx/gtk/private.h" | |
30 | ||
31 | #if wxUSE_TOOLTIPS | |
32 | #include "wx/tooltip.h" | |
33 | #endif | |
34 | ||
35 | #include <gdk/gdk.h> | |
36 | #include <gtk/gtk.h> | |
37 | #include <gdk/gdkkeysyms.h> | |
38 | ||
39 | //----------------------------------------------------------------------------- | |
40 | // idle system | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern void wxapp_install_idle_handler(); | |
44 | extern bool g_isIdle; | |
45 | ||
46 | //----------------------------------------------------------------------------- | |
47 | // data | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | extern bool g_blockEventsOnDrag; | |
51 | extern bool g_blockEventsOnScroll; | |
52 | extern wxCursor g_globalCursor; | |
53 | extern wxWindowGTK *g_delayedFocus; | |
54 | extern wxWindowGTK *g_focusWindow; | |
55 | extern wxWindowGTK *g_focusWindowLast; | |
56 | ||
57 | static bool g_hasDoubleClicked = FALSE; | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // idle callback for SetFirstItem | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | struct wxlistbox_idle_struct | |
64 | { | |
65 | wxListBox *m_listbox; | |
66 | int m_item; | |
67 | gint m_tag; | |
68 | }; | |
69 | ||
70 | extern "C" gint wxlistbox_idle_callback( gpointer gdata ) | |
71 | { | |
72 | wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata; | |
73 | gdk_threads_enter(); | |
74 | ||
75 | gtk_idle_remove( data->m_tag ); | |
76 | ||
77 | // check that the items haven't been deleted from the listbox since we had | |
78 | // installed this callback | |
79 | wxListBox *lbox = data->m_listbox; | |
80 | if ( data->m_item < lbox->GetCount() ) | |
81 | { | |
82 | lbox->SetFirstItem( data->m_item ); | |
83 | } | |
84 | ||
85 | delete data; | |
86 | ||
87 | gdk_threads_leave(); | |
88 | ||
89 | return TRUE; | |
90 | } | |
91 | ||
92 | //----------------------------------------------------------------------------- | |
93 | // "focus_in_event" | |
94 | //----------------------------------------------------------------------------- | |
95 | ||
96 | static gint gtk_listitem_focus_in_callback( GtkWidget *widget, | |
97 | GdkEvent *WXUNUSED(event), | |
98 | wxWindow *win ) | |
99 | { | |
100 | if (g_isIdle) | |
101 | wxapp_install_idle_handler(); | |
102 | ||
103 | g_focusWindowLast = | |
104 | g_focusWindow = win; | |
105 | ||
106 | // does the window itself think that it has the focus? | |
107 | if ( !win->m_hasFocus ) | |
108 | { | |
109 | // not yet, notify it | |
110 | win->m_hasFocus = TRUE; | |
111 | ||
112 | wxChildFocusEvent eventChildFocus(win); | |
113 | (void)win->GetEventHandler()->ProcessEvent(eventChildFocus); | |
114 | ||
115 | wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId()); | |
116 | eventFocus.SetEventObject(win); | |
117 | ||
118 | (void)win->GetEventHandler()->ProcessEvent(eventFocus); | |
119 | } | |
120 | ||
121 | return FALSE; | |
122 | } | |
123 | ||
124 | //----------------------------------------------------------------------------- | |
125 | // "focus_out_event" | |
126 | //----------------------------------------------------------------------------- | |
127 | ||
128 | static gint gtk_listitem_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win ) | |
129 | { | |
130 | if (g_isIdle) | |
131 | wxapp_install_idle_handler(); | |
132 | ||
133 | g_focusWindow = (wxWindowGTK *)NULL; | |
134 | ||
135 | // don't send the window a kill focus event if it thinks that it doesn't | |
136 | // have focus already | |
137 | if ( win->m_hasFocus ) | |
138 | { | |
139 | win->m_hasFocus = FALSE; | |
140 | ||
141 | wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); | |
142 | event.SetEventObject( win ); | |
143 | ||
144 | // even if we did process the event in wx code, still let GTK itself | |
145 | // process it too as otherwise bad things happen, especially in GTK2 | |
146 | // where the text control simply aborts the program if it doesn't get | |
147 | // the matching focus out event | |
148 | (void)win->GetEventHandler()->ProcessEvent( event ); | |
149 | } | |
150 | ||
151 | return FALSE; | |
152 | } | |
153 | ||
154 | //----------------------------------------------------------------------------- | |
155 | // "button_release_event" | |
156 | //----------------------------------------------------------------------------- | |
157 | ||
158 | /* we would normally emit a wxEVT_COMMAND_LISTBOX_DOUBLECLICKED event once | |
159 | a GDK_2BUTTON_PRESS occurs, but this has the particular problem of the | |
160 | listbox keeping the focus until it receives a GDK_BUTTON_RELEASE event. | |
161 | this can lead to race conditions so that we emit the dclick event | |
162 | after the GDK_BUTTON_RELEASE event after the GDK_2BUTTON_PRESS event */ | |
163 | ||
164 | static gint | |
165 | gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget), | |
166 | GdkEventButton * WXUNUSED(gdk_event), | |
167 | wxListBox *listbox ) | |
168 | { | |
169 | if (g_isIdle) wxapp_install_idle_handler(); | |
170 | ||
171 | if (g_blockEventsOnDrag) return FALSE; | |
172 | if (g_blockEventsOnScroll) return FALSE; | |
173 | ||
174 | if (!listbox->m_hasVMT) return FALSE; | |
175 | ||
176 | if (!g_hasDoubleClicked) return FALSE; | |
177 | ||
178 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); | |
179 | event.SetEventObject( listbox ); | |
180 | ||
181 | wxArrayInt aSelections; | |
182 | int n, count = listbox->GetSelections(aSelections); | |
183 | if ( count > 0 ) | |
184 | { | |
185 | n = aSelections[0]; | |
186 | if ( listbox->HasClientObjectData() ) | |
187 | event.SetClientObject( listbox->GetClientObject(n) ); | |
188 | else if ( listbox->HasClientUntypedData() ) | |
189 | event.SetClientData( listbox->GetClientData(n) ); | |
190 | event.SetString( listbox->GetString(n) ); | |
191 | } | |
192 | else | |
193 | { | |
194 | n = -1; | |
195 | } | |
196 | ||
197 | event.m_commandInt = n; | |
198 | ||
199 | listbox->GetEventHandler()->ProcessEvent( event ); | |
200 | ||
201 | return FALSE; | |
202 | } | |
203 | ||
204 | //----------------------------------------------------------------------------- | |
205 | // "button_press_event" | |
206 | //----------------------------------------------------------------------------- | |
207 | ||
208 | static gint | |
209 | gtk_listbox_button_press_callback( GtkWidget *widget, | |
210 | GdkEventButton *gdk_event, | |
211 | wxListBox *listbox ) | |
212 | { | |
213 | if (g_isIdle) wxapp_install_idle_handler(); | |
214 | ||
215 | if (g_blockEventsOnDrag) return FALSE; | |
216 | if (g_blockEventsOnScroll) return FALSE; | |
217 | ||
218 | if (!listbox->m_hasVMT) return FALSE; | |
219 | ||
220 | int sel = listbox->GtkGetIndex( widget ); | |
221 | ||
222 | #if wxUSE_CHECKLISTBOX | |
223 | if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS)) | |
224 | { | |
225 | wxCheckListBox *clb = (wxCheckListBox *)listbox; | |
226 | ||
227 | clb->Check( sel, !clb->IsChecked(sel) ); | |
228 | ||
229 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); | |
230 | event.SetEventObject( listbox ); | |
231 | event.SetInt( sel ); | |
232 | listbox->GetEventHandler()->ProcessEvent( event ); | |
233 | } | |
234 | #endif // wxUSE_CHECKLISTBOX | |
235 | ||
236 | /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */ | |
237 | g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS); | |
238 | ||
239 | return FALSE; | |
240 | } | |
241 | ||
242 | //----------------------------------------------------------------------------- | |
243 | // "key_press_event" | |
244 | //----------------------------------------------------------------------------- | |
245 | ||
246 | static gint | |
247 | gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox ) | |
248 | { | |
249 | if (g_isIdle) | |
250 | wxapp_install_idle_handler(); | |
251 | ||
252 | if (g_blockEventsOnDrag) | |
253 | return FALSE; | |
254 | ||
255 | bool ret = FALSE; | |
256 | ||
257 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) | |
258 | { | |
259 | wxNavigationKeyEvent new_event; | |
260 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ | |
261 | new_event.SetDirection( (gdk_event->keyval == GDK_Tab) ); | |
262 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
263 | new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); | |
264 | new_event.SetCurrentFocus( listbox ); | |
265 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); | |
266 | } | |
267 | ||
268 | if ((gdk_event->keyval == GDK_Return) && (!ret)) | |
269 | { | |
270 | // eat return in all modes | |
271 | ret = TRUE; | |
272 | } | |
273 | ||
274 | #if wxUSE_CHECKLISTBOX | |
275 | if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret)) | |
276 | { | |
277 | int sel = listbox->GtkGetIndex( widget ); | |
278 | ||
279 | wxCheckListBox *clb = (wxCheckListBox *)listbox; | |
280 | ||
281 | clb->Check( sel, !clb->IsChecked(sel) ); | |
282 | ||
283 | wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); | |
284 | new_event.SetEventObject( listbox ); | |
285 | new_event.SetInt( sel ); | |
286 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); | |
287 | } | |
288 | #endif // wxUSE_CHECKLISTBOX | |
289 | ||
290 | // Check or uncheck item with SPACE | |
291 | if ((gdk_event->keyval == ' ') && (!ret) && | |
292 | (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || | |
293 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) | |
294 | { | |
295 | int sel = listbox->GtkGetIndex( widget ); | |
296 | ||
297 | if (sel != -1) | |
298 | { | |
299 | ret = TRUE; | |
300 | ||
301 | if (listbox->IsSelected( sel )) | |
302 | gtk_list_unselect_item( listbox->m_list, sel ); | |
303 | else | |
304 | gtk_list_select_item( listbox->m_list, sel ); | |
305 | ||
306 | wxCommandEvent new_event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); | |
307 | new_event.SetEventObject( listbox ); | |
308 | wxArrayInt aSelections; | |
309 | int n, count = listbox->GetSelections(aSelections); | |
310 | if ( count > 0 ) | |
311 | { | |
312 | n = aSelections[0]; | |
313 | if ( listbox->HasClientObjectData() ) | |
314 | new_event.SetClientObject( listbox->GetClientObject(n) ); | |
315 | else if ( listbox->HasClientUntypedData() ) | |
316 | new_event.SetClientData( listbox->GetClientData(n) ); | |
317 | new_event.SetString( listbox->GetString(n) ); | |
318 | } | |
319 | else | |
320 | { | |
321 | n = -1; | |
322 | } | |
323 | new_event.m_commandInt = n; | |
324 | listbox->GetEventHandler()->ProcessEvent( new_event ); | |
325 | } | |
326 | } | |
327 | ||
328 | if (ret) | |
329 | { | |
330 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); | |
331 | return TRUE; | |
332 | } | |
333 | ||
334 | return FALSE; | |
335 | } | |
336 | ||
337 | //----------------------------------------------------------------------------- | |
338 | // "select" and "deselect" | |
339 | //----------------------------------------------------------------------------- | |
340 | ||
341 | static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection ); | |
342 | ||
343 | static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox ) | |
344 | { | |
345 | gtk_listitem_select_cb( widget, listbox, TRUE ); | |
346 | } | |
347 | ||
348 | static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox ) | |
349 | { | |
350 | gtk_listitem_select_cb( widget, listbox, FALSE ); | |
351 | } | |
352 | ||
353 | static void gtk_listitem_select_cb( GtkWidget *widget, | |
354 | wxListBox *listbox, | |
355 | bool is_selection ) | |
356 | { | |
357 | if (g_isIdle) wxapp_install_idle_handler(); | |
358 | ||
359 | if (!listbox->m_hasVMT) return; | |
360 | if (g_blockEventsOnDrag) return; | |
361 | ||
362 | if (listbox->m_blockEvent) return; | |
363 | ||
364 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); | |
365 | event.SetEventObject( listbox ); | |
366 | ||
367 | // indicate whether this is a selection or a deselection | |
368 | event.SetExtraLong( is_selection ); | |
369 | ||
370 | if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0) | |
371 | { | |
372 | int sel = listbox->GtkGetIndex( widget ); | |
373 | ||
374 | if (listbox->m_prevSelection != sel) | |
375 | gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection ); | |
376 | ||
377 | listbox->m_prevSelection = sel; | |
378 | } | |
379 | ||
380 | wxArrayInt aSelections; | |
381 | int n, count = listbox->GetSelections(aSelections); | |
382 | if ( count > 0 ) | |
383 | { | |
384 | n = aSelections[0]; | |
385 | if ( listbox->HasClientObjectData() ) | |
386 | event.SetClientObject( listbox->GetClientObject(n) ); | |
387 | else if ( listbox->HasClientUntypedData() ) | |
388 | event.SetClientData( listbox->GetClientData(n) ); | |
389 | event.SetString( listbox->GetString(n) ); | |
390 | } | |
391 | else | |
392 | { | |
393 | n = -1; | |
394 | } | |
395 | ||
396 | event.m_commandInt = n; | |
397 | ||
398 | // No longer required with new code in wxLB_SINGLE | |
399 | // listbox->GetEventHandler()->AddPendingEvent( event ); | |
400 | listbox->GetEventHandler()->ProcessEvent( event ); | |
401 | } | |
402 | ||
403 | //----------------------------------------------------------------------------- | |
404 | // wxListBox | |
405 | //----------------------------------------------------------------------------- | |
406 | ||
407 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) | |
408 | ||
409 | // ---------------------------------------------------------------------------- | |
410 | // construction | |
411 | // ---------------------------------------------------------------------------- | |
412 | ||
413 | wxListBox::wxListBox() | |
414 | { | |
415 | m_list = (GtkList *) NULL; | |
416 | #if wxUSE_CHECKLISTBOX | |
417 | m_hasCheckBoxes = FALSE; | |
418 | #endif // wxUSE_CHECKLISTBOX | |
419 | } | |
420 | ||
421 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, | |
422 | const wxPoint &pos, const wxSize &size, | |
423 | const wxArrayString& choices, | |
424 | long style, const wxValidator& validator, | |
425 | const wxString &name ) | |
426 | { | |
427 | wxCArrayString chs(choices); | |
428 | ||
429 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
430 | style, validator, name ); | |
431 | } | |
432 | ||
433 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, | |
434 | const wxPoint &pos, const wxSize &size, | |
435 | int n, const wxString choices[], | |
436 | long style, const wxValidator& validator, | |
437 | const wxString &name ) | |
438 | { | |
439 | m_needParent = TRUE; | |
440 | m_acceptsFocus = TRUE; | |
441 | m_prevSelection = 0; // or -1 ?? | |
442 | m_blockEvent = FALSE; | |
443 | ||
444 | if (!PreCreation( parent, pos, size ) || | |
445 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
446 | { | |
447 | wxFAIL_MSG( wxT("wxListBox creation failed") ); | |
448 | return FALSE; | |
449 | } | |
450 | ||
451 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); | |
452 | if (style & wxLB_ALWAYS_SB) | |
453 | { | |
454 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
455 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); | |
456 | } | |
457 | else | |
458 | { | |
459 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
460 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
461 | } | |
462 | ||
463 | m_list = GTK_LIST( gtk_list_new() ); | |
464 | ||
465 | GtkSelectionMode mode; | |
466 | if (style & wxLB_MULTIPLE) | |
467 | { | |
468 | mode = GTK_SELECTION_MULTIPLE; | |
469 | } | |
470 | else if (style & wxLB_EXTENDED) | |
471 | { | |
472 | mode = GTK_SELECTION_EXTENDED; | |
473 | } | |
474 | else | |
475 | { | |
476 | // if style was 0 set single mode | |
477 | m_windowStyle |= wxLB_SINGLE; | |
478 | mode = GTK_SELECTION_MULTIPLE; | |
479 | } | |
480 | ||
481 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); | |
482 | ||
483 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); | |
484 | ||
485 | /* make list scroll when moving the focus down using cursor keys */ | |
486 | gtk_container_set_focus_vadjustment( | |
487 | GTK_CONTAINER(m_list), | |
488 | gtk_scrolled_window_get_vadjustment( | |
489 | GTK_SCROLLED_WINDOW(m_widget))); | |
490 | ||
491 | gtk_widget_show( GTK_WIDGET(m_list) ); | |
492 | ||
493 | if ( style & wxLB_SORT ) | |
494 | { | |
495 | // this will change DoAppend() behaviour | |
496 | m_strings = new wxSortedArrayString; | |
497 | } | |
498 | else | |
499 | { | |
500 | m_strings = (wxSortedArrayString *)NULL; | |
501 | } | |
502 | ||
503 | for (int i = 0; i < n; i++) | |
504 | { | |
505 | // add one by one | |
506 | DoAppend(choices[i]); | |
507 | } | |
508 | ||
509 | m_parent->DoAddChild( this ); | |
510 | ||
511 | PostCreation(size); | |
512 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
513 | ||
514 | return TRUE; | |
515 | } | |
516 | ||
517 | wxListBox::~wxListBox() | |
518 | { | |
519 | m_hasVMT = FALSE; | |
520 | ||
521 | Clear(); | |
522 | ||
523 | if (m_strings) | |
524 | delete m_strings; | |
525 | } | |
526 | ||
527 | // ---------------------------------------------------------------------------- | |
528 | // adding items | |
529 | // ---------------------------------------------------------------------------- | |
530 | ||
531 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
532 | { | |
533 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); | |
534 | ||
535 | // VZ: notice that InsertItems knows nothing about sorting, so calling it | |
536 | // from outside (and not from our own Append) is likely to break | |
537 | // everything | |
538 | ||
539 | // code elsewhere supposes we have as many items in m_clientList as items | |
540 | // in the listbox | |
541 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), | |
542 | wxT("bug in client data management") ); | |
543 | ||
544 | InvalidateBestSize(); | |
545 | ||
546 | GList *children = m_list->children; | |
547 | int length = g_list_length(children); | |
548 | ||
549 | wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") ); | |
550 | ||
551 | size_t nItems = items.GetCount(); | |
552 | int index; | |
553 | ||
554 | if (m_strings) | |
555 | { | |
556 | for (size_t n = 0; n < nItems; n++) | |
557 | { | |
558 | index = m_strings->Add( items[n] ); | |
559 | ||
560 | if (index != GetCount()) | |
561 | { | |
562 | GtkAddItem( items[n], index ); | |
563 | wxList::compatibility_iterator node = m_clientList.Item( index ); | |
564 | m_clientList.Insert( node, (wxObject*) NULL ); | |
565 | } | |
566 | else | |
567 | { | |
568 | GtkAddItem( items[n] ); | |
569 | m_clientList.Append( (wxObject*) NULL ); | |
570 | } | |
571 | } | |
572 | } | |
573 | else | |
574 | { | |
575 | if (pos == length) | |
576 | { | |
577 | for ( size_t n = 0; n < nItems; n++ ) | |
578 | { | |
579 | GtkAddItem( items[n] ); | |
580 | ||
581 | m_clientList.Append((wxObject *)NULL); | |
582 | } | |
583 | } | |
584 | else | |
585 | { | |
586 | wxList::compatibility_iterator node = m_clientList.Item( pos ); | |
587 | for ( size_t n = 0; n < nItems; n++ ) | |
588 | { | |
589 | GtkAddItem( items[n], pos+n ); | |
590 | ||
591 | m_clientList.Insert( node, (wxObject *)NULL ); | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), | |
597 | wxT("bug in client data management") ); | |
598 | } | |
599 | ||
600 | int wxListBox::DoAppend( const wxString& item ) | |
601 | { | |
602 | InvalidateBestSize(); | |
603 | ||
604 | if (m_strings) | |
605 | { | |
606 | // need to determine the index | |
607 | int index = m_strings->Add( item ); | |
608 | ||
609 | // only if not at the end anyway | |
610 | if (index != GetCount()) | |
611 | { | |
612 | GtkAddItem( item, index ); | |
613 | ||
614 | wxList::compatibility_iterator node = m_clientList.Item( index ); | |
615 | m_clientList.Insert( node, (wxObject *)NULL ); | |
616 | ||
617 | return index; | |
618 | } | |
619 | } | |
620 | ||
621 | GtkAddItem(item); | |
622 | ||
623 | m_clientList.Append((wxObject *)NULL); | |
624 | ||
625 | return GetCount() - 1; | |
626 | } | |
627 | ||
628 | void wxListBox::GtkAddItem( const wxString &item, int pos ) | |
629 | { | |
630 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); | |
631 | ||
632 | GtkWidget *list_item; | |
633 | ||
634 | wxString label(item); | |
635 | #if wxUSE_CHECKLISTBOX | |
636 | if (m_hasCheckBoxes) | |
637 | { | |
638 | label.Prepend(wxCHECKLBOX_STRING); | |
639 | } | |
640 | #endif // wxUSE_CHECKLISTBOX | |
641 | ||
642 | list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) ); | |
643 | ||
644 | GList *gitem_list = g_list_alloc (); | |
645 | gitem_list->data = list_item; | |
646 | ||
647 | if (pos == -1) | |
648 | gtk_list_append_items( GTK_LIST (m_list), gitem_list ); | |
649 | else | |
650 | gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos ); | |
651 | ||
652 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
653 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
654 | ||
655 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) | |
656 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", | |
657 | GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); | |
658 | ||
659 | gtk_signal_connect( GTK_OBJECT(list_item), | |
660 | "button_press_event", | |
661 | (GtkSignalFunc)gtk_listbox_button_press_callback, | |
662 | (gpointer) this ); | |
663 | ||
664 | gtk_signal_connect_after( GTK_OBJECT(list_item), | |
665 | "button_release_event", | |
666 | (GtkSignalFunc)gtk_listbox_button_release_callback, | |
667 | (gpointer) this ); | |
668 | ||
669 | gtk_signal_connect( GTK_OBJECT(list_item), | |
670 | "key_press_event", | |
671 | (GtkSignalFunc)gtk_listbox_key_press_callback, | |
672 | (gpointer)this ); | |
673 | ||
674 | ||
675 | gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event", | |
676 | GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this ); | |
677 | ||
678 | gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event", | |
679 | GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this ); | |
680 | ||
681 | ||
682 | ConnectWidget( list_item ); | |
683 | ||
684 | gtk_widget_show( list_item ); | |
685 | ||
686 | if (GTK_WIDGET_REALIZED(m_widget)) | |
687 | { | |
688 | gtk_widget_realize( list_item ); | |
689 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
690 | ||
691 | #if wxUSE_TOOLTIPS | |
692 | if (m_tooltip) m_tooltip->Apply( this ); | |
693 | #endif | |
694 | } | |
695 | ||
696 | // Apply current widget style to the new list_item | |
697 | GtkRcStyle *style = CreateWidgetStyle(); | |
698 | if (style) | |
699 | { | |
700 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); | |
701 | GtkBin *bin = GTK_BIN( list_item ); | |
702 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
703 | gtk_widget_modify_style( label, style ); | |
704 | gtk_rc_style_unref( style ); | |
705 | } | |
706 | } | |
707 | ||
708 | void wxListBox::DoSetItems( const wxArrayString& items, | |
709 | void **clientData) | |
710 | { | |
711 | Clear(); | |
712 | ||
713 | DoInsertItems(items, 0); | |
714 | ||
715 | if ( clientData ) | |
716 | { | |
717 | size_t count = items.GetCount(); | |
718 | for ( size_t n = 0; n < count; n++ ) | |
719 | { | |
720 | SetClientData(n, clientData[n]); | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | // ---------------------------------------------------------------------------- | |
726 | // deleting items | |
727 | // ---------------------------------------------------------------------------- | |
728 | ||
729 | void wxListBox::Clear() | |
730 | { | |
731 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); | |
732 | ||
733 | gtk_list_clear_items( m_list, 0, GetCount() ); | |
734 | ||
735 | if ( GTK_LIST(m_list)->last_focus_child != NULL ) | |
736 | { | |
737 | // This should be NULL, I think. | |
738 | GTK_LIST(m_list)->last_focus_child = NULL; | |
739 | } | |
740 | ||
741 | if ( HasClientObjectData() ) | |
742 | { | |
743 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
744 | // and not wxList<wxClientData> we can't just say | |
745 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
746 | wxList::compatibility_iterator node = m_clientList.GetFirst(); | |
747 | while ( node ) | |
748 | { | |
749 | delete (wxClientData *)node->GetData(); | |
750 | node = node->GetNext(); | |
751 | } | |
752 | } | |
753 | m_clientList.Clear(); | |
754 | ||
755 | if ( m_strings ) | |
756 | m_strings->Clear(); | |
757 | } | |
758 | ||
759 | void wxListBox::Delete( int n ) | |
760 | { | |
761 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); | |
762 | ||
763 | GList *child = g_list_nth( m_list->children, n ); | |
764 | ||
765 | wxCHECK_RET( child, wxT("wrong listbox index") ); | |
766 | ||
767 | GList *list = g_list_append( (GList*) NULL, child->data ); | |
768 | gtk_list_remove_items( m_list, list ); | |
769 | g_list_free( list ); | |
770 | ||
771 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
772 | if ( node ) | |
773 | { | |
774 | if ( m_clientDataItemsType == wxClientData_Object ) | |
775 | { | |
776 | wxClientData *cd = (wxClientData*)node->GetData(); | |
777 | delete cd; | |
778 | } | |
779 | ||
780 | m_clientList.Erase( node ); | |
781 | } | |
782 | ||
783 | if ( m_strings ) | |
784 | m_strings->RemoveAt(n); | |
785 | } | |
786 | ||
787 | // ---------------------------------------------------------------------------- | |
788 | // client data | |
789 | // ---------------------------------------------------------------------------- | |
790 | ||
791 | void wxListBox::DoSetItemClientData( int n, void* clientData ) | |
792 | { | |
793 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); | |
794 | ||
795 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
796 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); | |
797 | ||
798 | node->SetData( (wxObject*) clientData ); | |
799 | } | |
800 | ||
801 | void* wxListBox::DoGetItemClientData( int n ) const | |
802 | { | |
803 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); | |
804 | ||
805 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
806 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); | |
807 | ||
808 | return node->GetData(); | |
809 | } | |
810 | ||
811 | void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) | |
812 | { | |
813 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); | |
814 | ||
815 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
816 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); | |
817 | ||
818 | // wxItemContainer already deletes data for us | |
819 | ||
820 | node->SetData( (wxObject*) clientData ); | |
821 | } | |
822 | ||
823 | wxClientData* wxListBox::DoGetItemClientObject( int n ) const | |
824 | { | |
825 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); | |
826 | ||
827 | wxList::compatibility_iterator node = m_clientList.Item( n ); | |
828 | wxCHECK_MSG( node, (wxClientData *)NULL, | |
829 | wxT("invalid index in wxListBox::DoGetItemClientObject") ); | |
830 | ||
831 | return (wxClientData*) node->GetData(); | |
832 | } | |
833 | ||
834 | // ---------------------------------------------------------------------------- | |
835 | // string list access | |
836 | // ---------------------------------------------------------------------------- | |
837 | ||
838 | wxString wxListBox::GetRealLabel(GList *item) const | |
839 | { | |
840 | GtkBin *bin = GTK_BIN( item->data ); | |
841 | GtkLabel *label = GTK_LABEL( bin->child ); | |
842 | ||
843 | wxString str; | |
844 | ||
845 | #ifdef __WXGTK20__ | |
846 | str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); | |
847 | #else | |
848 | str = wxString( label->label ); | |
849 | #endif | |
850 | ||
851 | #if wxUSE_CHECKLISTBOX | |
852 |