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