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