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