]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
3cbab641 | 2 | // Name: src/gtk1/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 | ||
dcf924a3 RR |
13 | #if wxUSE_LISTBOX |
14 | ||
88a7a4e1 WS |
15 | #include "wx/listbox.h" |
16 | ||
ad9835c9 WS |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/dynarray.h" | |
88a7a4e1 | 19 | #include "wx/intl.h" |
de6185e2 | 20 | #include "wx/utils.h" |
9eddec69 | 21 | #include "wx/settings.h" |
e6e83933 | 22 | #include "wx/checklst.h" |
aaa6d89a | 23 | #include "wx/arrstr.h" |
ad9835c9 WS |
24 | #endif |
25 | ||
3cbab641 | 26 | #include "wx/gtk1/private.h" |
291a8f20 RR |
27 | |
28 | #if wxUSE_TOOLTIPS | |
88a7a4e1 | 29 | #include "wx/tooltip.h" |
291a8f20 | 30 | #endif |
c801d85f | 31 | |
4a82abc8 | 32 | #include <gdk/gdk.h> |
16c1f79c RR |
33 | #include <gtk/gtk.h> |
34 | #include <gdk/gdkkeysyms.h> | |
83624f79 | 35 | |
acfd422a RR |
36 | //----------------------------------------------------------------------------- |
37 | // idle system | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern void wxapp_install_idle_handler(); | |
41 | extern bool g_isIdle; | |
42 | ||
66bd6b93 RR |
43 | //----------------------------------------------------------------------------- |
44 | // data | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
d7fa7eaa RR |
47 | extern bool g_blockEventsOnDrag; |
48 | extern bool g_blockEventsOnScroll; | |
49 | extern wxCursor g_globalCursor; | |
50 | extern wxWindowGTK *g_delayedFocus; | |
c9433ea9 RR |
51 | extern wxWindowGTK *g_focusWindow; |
52 | extern wxWindowGTK *g_focusWindowLast; | |
caaa4cfd | 53 | |
caf6e6de | 54 | static bool g_hasDoubleClicked = false; |
7a30ee8f | 55 | |
4a82abc8 RR |
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 | ||
865bb325 VZ |
67 | extern "C" { |
68 | static gint wxlistbox_idle_callback( gpointer gdata ) | |
4a82abc8 RR |
69 | { |
70 | wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata; | |
71 | gdk_threads_enter(); | |
72 | ||
73 | gtk_idle_remove( data->m_tag ); | |
f96b15a3 | 74 | |
992295c4 VZ |
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; | |
8228b893 | 78 | if ( data->m_item < (int)lbox->GetCount() ) |
992295c4 VZ |
79 | { |
80 | lbox->SetFirstItem( data->m_item ); | |
81 | } | |
f96b15a3 | 82 | |
4a82abc8 | 83 | delete data; |
f96b15a3 | 84 | |
4a82abc8 RR |
85 | gdk_threads_leave(); |
86 | ||
87 | return TRUE; | |
88 | } | |
865bb325 | 89 | } |
4a82abc8 | 90 | |
c9433ea9 RR |
91 | //----------------------------------------------------------------------------- |
92 | // "focus_in_event" | |
93 | //----------------------------------------------------------------------------- | |
94 | ||
865bb325 | 95 | extern "C" { |
c9433ea9 RR |
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 | |
caf6e6de | 110 | win->m_hasFocus = true; |
17a1ebd1 | 111 | |
c9433ea9 RR |
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 | } | |
865bb325 | 123 | } |
c9433ea9 RR |
124 | |
125 | //----------------------------------------------------------------------------- | |
126 | // "focus_out_event" | |
127 | //----------------------------------------------------------------------------- | |
128 | ||
865bb325 | 129 | extern "C" { |
c9433ea9 RR |
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 | { | |
caf6e6de | 141 | win->m_hasFocus = false; |
c9433ea9 RR |
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 | } | |
865bb325 | 155 | } |
c9433ea9 | 156 | |
caaa4cfd | 157 | //----------------------------------------------------------------------------- |
7a30ee8f | 158 | // "button_release_event" |
caaa4cfd RR |
159 | //----------------------------------------------------------------------------- |
160 | ||
7a30ee8f RR |
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 | ||
865bb325 | 167 | extern "C" { |
ff8bfdbb | 168 | static gint |
014b0d06 VZ |
169 | gtk_listbox_button_release_callback( GtkWidget * WXUNUSED(widget), |
170 | GdkEventButton * WXUNUSED(gdk_event), | |
171 | wxListBox *listbox ) | |
caaa4cfd | 172 | { |
acfd422a RR |
173 | if (g_isIdle) wxapp_install_idle_handler(); |
174 | ||
caaa4cfd RR |
175 | if (g_blockEventsOnDrag) return FALSE; |
176 | if (g_blockEventsOnScroll) return FALSE; | |
177 | ||
a2053b27 | 178 | if (!listbox->m_hasVMT) return FALSE; |
caaa4cfd | 179 | |
7a30ee8f | 180 | if (!g_hasDoubleClicked) return FALSE; |
ff8bfdbb | 181 | |
6c8a980f VZ |
182 | wxCommandEvent event( wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); |
183 | event.SetEventObject( listbox ); | |
ff8bfdbb | 184 | |
6c8a980f VZ |
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 | } | |
5b077d48 | 200 | |
687706f5 | 201 | event.SetInt(n); |
6c8a980f VZ |
202 | |
203 | listbox->GetEventHandler()->ProcessEvent( event ); | |
ff8bfdbb | 204 | |
7a30ee8f RR |
205 | return FALSE; |
206 | } | |
865bb325 | 207 | } |
7a30ee8f RR |
208 | |
209 | //----------------------------------------------------------------------------- | |
210 | // "button_press_event" | |
211 | //----------------------------------------------------------------------------- | |
212 | ||
865bb325 | 213 | extern "C" { |
7a30ee8f | 214 | static gint |
014b0d06 VZ |
215 | gtk_listbox_button_press_callback( GtkWidget *widget, |
216 | GdkEventButton *gdk_event, | |
217 | wxListBox *listbox ) | |
7a30ee8f RR |
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 | ||
11e1c70d | 226 | int sel = listbox->GtkGetIndex( widget ); |
7a30ee8f | 227 | |
88ac883a | 228 | #if wxUSE_CHECKLISTBOX |
7a30ee8f RR |
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 ); | |
4f22cf8d | 239 | } |
88ac883a | 240 | #endif // wxUSE_CHECKLISTBOX |
014b0d06 | 241 | |
4fcfa27c RR |
242 | if ((gdk_event->state == 0) && |
243 | (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || | |
244 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) | |
245 | { | |
caf6e6de | 246 | listbox->m_blockEvent = true; |
376c0148 RR |
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 ); | |
17a1ebd1 | 252 | |
caf6e6de | 253 | listbox->m_blockEvent = false; |
17a1ebd1 | 254 | |
376c0148 | 255 | return false; |
4fcfa27c RR |
256 | } |
257 | ||
7a30ee8f RR |
258 | /* emit wxEVT_COMMAND_LISTBOX_DOUBLECLICKED later */ |
259 | g_hasDoubleClicked = (gdk_event->type == GDK_2BUTTON_PRESS); | |
4f22cf8d | 260 | |
caaa4cfd RR |
261 | return FALSE; |
262 | } | |
865bb325 | 263 | } |
66bd6b93 | 264 | |
1144d24d RR |
265 | //----------------------------------------------------------------------------- |
266 | // "key_press_event" | |
267 | //----------------------------------------------------------------------------- | |
268 | ||
865bb325 | 269 | extern "C" { |
ff8bfdbb | 270 | static gint |
1144d24d RR |
271 | gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxListBox *listbox ) |
272 | { | |
9ef6ff8c | 273 | if (g_isIdle) |
354aa1e3 | 274 | wxapp_install_idle_handler(); |
acfd422a | 275 | |
9ef6ff8c | 276 | if (g_blockEventsOnDrag) |
354aa1e3 | 277 | return FALSE; |
1144d24d | 278 | |
caf6e6de | 279 | bool ret = false; |
1144d24d | 280 | |
354aa1e3 RR |
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 | } | |
9ef6ff8c | 291 | |
4a82abc8 RR |
292 | if ((gdk_event->keyval == GDK_Return) && (!ret)) |
293 | { | |
294 | // eat return in all modes | |
caf6e6de | 295 | ret = true; |
4a82abc8 | 296 | } |
f96b15a3 | 297 | |
354aa1e3 | 298 | #if wxUSE_CHECKLISTBOX |
1e8d2f69 | 299 | if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret)) |
354aa1e3 RR |
300 | { |
301 | int sel = listbox->GtkGetIndex( widget ); | |
ff8bfdbb | 302 | |
354aa1e3 | 303 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
ff8bfdbb | 304 | |
354aa1e3 | 305 | clb->Check( sel, !clb->IsChecked(sel) ); |
ff8bfdbb | 306 | |
354aa1e3 RR |
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 | |
ff8bfdbb | 313 | |
fec195b2 | 314 | // Check or uncheck item with SPACE |
17a1ebd1 | 315 | if ((gdk_event->keyval == ' ') && (!ret) && |
fec195b2 RR |
316 | (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || |
317 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) | |
318 | { | |
319 | int sel = listbox->GtkGetIndex( widget ); | |
17a1ebd1 | 320 | |
fec195b2 RR |
321 | if (sel != -1) |
322 | { | |
caf6e6de | 323 | ret = true; |
17a1ebd1 | 324 | |
fec195b2 RR |
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 ); | |
17a1ebd1 | 329 | |
fec195b2 RR |
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 | } | |
687706f5 | 347 | new_event.SetInt(n); |
fec195b2 RR |
348 | listbox->GetEventHandler()->ProcessEvent( new_event ); |
349 | } | |
350 | } | |
17a1ebd1 | 351 | |
354aa1e3 RR |
352 | if (ret) |
353 | { | |
354 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); | |
355 | return TRUE; | |
356 | } | |
ff8bfdbb | 357 | |
1144d24d RR |
358 | return FALSE; |
359 | } | |
865bb325 | 360 | } |
1144d24d | 361 | |
c801d85f | 362 | //----------------------------------------------------------------------------- |
a60c99e6 | 363 | // "select" and "deselect" |
c801d85f KB |
364 | //----------------------------------------------------------------------------- |
365 | ||
9c9c3d7a VZ |
366 | static void gtk_listitem_select_cb( GtkWidget *widget, |
367 | wxListBox *listbox, | |
368 | bool is_selection ) | |
c801d85f | 369 | { |
acfd422a RR |
370 | if (g_isIdle) wxapp_install_idle_handler(); |
371 | ||
a2053b27 | 372 | if (!listbox->m_hasVMT) return; |
fd0eed64 | 373 | if (g_blockEventsOnDrag) return; |
8161b5b9 | 374 | |
bac95742 | 375 | if (listbox->m_blockEvent) return; |
9ef6ff8c | 376 | |
fd0eed64 | 377 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
6c8a980f | 378 | event.SetEventObject( listbox ); |
8161b5b9 | 379 | |
9c9c3d7a VZ |
380 | // indicate whether this is a selection or a deselection |
381 | event.SetExtraLong( is_selection ); | |
159b66c0 RR |
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 | } | |
dcf40a56 | 392 | |
09cf7c58 | 393 | wxArrayInt aSelections; |
2ee3ee1b | 394 | int n, count = listbox->GetSelections(aSelections); |
09cf7c58 RR |
395 | if ( count > 0 ) |
396 | { | |
2ee3ee1b | 397 | n = aSelections[0]; |
6c8a980f VZ |
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) ); | |
09cf7c58 RR |
403 | } |
404 | else | |
405 | { | |
2ee3ee1b | 406 | n = -1; |
09cf7c58 RR |
407 | } |
408 | ||
687706f5 | 409 | event.SetInt(n); |
2ee3ee1b | 410 | |
159b66c0 RR |
411 | // No longer required with new code in wxLB_SINGLE |
412 | // listbox->GetEventHandler()->AddPendingEvent( event ); | |
413 | listbox->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 414 | } |
c801d85f | 415 | |
865bb325 VZ |
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 | ||
a60c99e6 RR |
430 | //----------------------------------------------------------------------------- |
431 | // wxListBox | |
c801d85f KB |
432 | //----------------------------------------------------------------------------- |
433 | ||
865bb325 | 434 | extern "C" { |
f2e93537 RR |
435 | static gint |
436 | gtk_listbox_realized_callback( GtkWidget *m_widget, wxListBox *win ) | |
437 | { | |
438 | if (g_isIdle) | |
439 | wxapp_install_idle_handler(); | |
17a1ebd1 | 440 | |
f2e93537 RR |
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) ); | |
17a1ebd1 | 444 | |
f2e93537 RR |
445 | return false; |
446 | } | |
865bb325 | 447 | } |
f2e93537 RR |
448 | |
449 | //----------------------------------------------------------------------------- | |
450 | // wxListBox | |
451 | //----------------------------------------------------------------------------- | |
452 | ||
b1294ada | 453 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) |
c801d85f | 454 | |
2ee3ee1b VZ |
455 | // ---------------------------------------------------------------------------- |
456 | // construction | |
457 | // ---------------------------------------------------------------------------- | |
458 | ||
fd0eed64 | 459 | wxListBox::wxListBox() |
c801d85f | 460 | { |
fd0eed64 | 461 | m_list = (GtkList *) NULL; |
88ac883a | 462 | #if wxUSE_CHECKLISTBOX |
caf6e6de | 463 | m_hasCheckBoxes = false; |
88ac883a | 464 | #endif // wxUSE_CHECKLISTBOX |
6de97a3b | 465 | } |
c801d85f | 466 | |
584ad2a3 MB |
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 | ||
dcf40a56 | 479 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
480 | const wxPoint &pos, const wxSize &size, |
481 | int n, const wxString choices[], | |
2ee3ee1b VZ |
482 | long style, const wxValidator& validator, |
483 | const wxString &name ) | |
c801d85f | 484 | { |
caf6e6de WS |
485 | m_needParent = true; |
486 | m_acceptsFocus = true; | |
159b66c0 | 487 | m_prevSelection = 0; // or -1 ?? |
caf6e6de | 488 | m_blockEvent = false; |
dcf40a56 | 489 | |
4dcaf11a RR |
490 | if (!PreCreation( parent, pos, size ) || |
491 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
492 | { | |
223d09f6 | 493 | wxFAIL_MSG( wxT("wxListBox creation failed") ); |
caf6e6de | 494 | return false; |
4dcaf11a | 495 | } |
6de97a3b | 496 | |
fd0eed64 | 497 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
034be888 RR |
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 | } | |
ff8bfdbb | 508 | |
fd0eed64 | 509 | m_list = GTK_LIST( gtk_list_new() ); |
dcf40a56 | 510 | |
4a82abc8 | 511 | GtkSelectionMode mode; |
fd0eed64 | 512 | if (style & wxLB_MULTIPLE) |
4a82abc8 | 513 | { |
fd0eed64 | 514 | mode = GTK_SELECTION_MULTIPLE; |
4a82abc8 | 515 | } |
fd0eed64 | 516 | else if (style & wxLB_EXTENDED) |
4a82abc8 | 517 | { |
fd0eed64 | 518 | mode = GTK_SELECTION_EXTENDED; |
4a82abc8 RR |
519 | } |
520 | else | |
521 | { | |
522 | // if style was 0 set single mode | |
523 | m_windowStyle |= wxLB_SINGLE; | |
4fcfa27c | 524 | mode = GTK_SELECTION_SINGLE; |
4a82abc8 | 525 | } |
6a6d4eed | 526 | |
fd0eed64 | 527 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); |
dcf40a56 | 528 | |
38c7b3d3 | 529 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); |
38c7b3d3 | 530 | |
e115e771 RR |
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( | |
2ee3ee1b | 535 | GTK_SCROLLED_WINDOW(m_widget))); |
e115e771 | 536 | |
fd0eed64 | 537 | gtk_widget_show( GTK_WIDGET(m_list) ); |
dcf40a56 | 538 | |
f2e93537 RR |
539 | gtk_signal_connect( GTK_OBJECT(m_list), "realize", |
540 | GTK_SIGNAL_FUNC(gtk_listbox_realized_callback), (gpointer) this ); | |
17a1ebd1 | 541 | |
2ee3ee1b VZ |
542 | if ( style & wxLB_SORT ) |
543 | { | |
a236aa20 | 544 | // this will change Append() behaviour |
2ee3ee1b VZ |
545 | m_strings = new wxSortedArrayString; |
546 | } | |
eb553cb2 VZ |
547 | else |
548 | { | |
549 | m_strings = (wxSortedArrayString *)NULL; | |
550 | } | |
2ee3ee1b | 551 | |
a236aa20 | 552 | Append(n, choices); |
dcf40a56 | 553 | |
f03fc89f | 554 | m_parent->DoAddChild( this ); |
ff8bfdbb | 555 | |
abdeb9e7 | 556 | PostCreation(size); |
170acdc9 | 557 | SetInitialSize(size); // need this too because this is a wxControlWithItems |
dcf40a56 | 558 | |
caf6e6de | 559 | return true; |
6de97a3b | 560 | } |
c801d85f | 561 | |
fd0eed64 | 562 | wxListBox::~wxListBox() |
c801d85f | 563 | { |
caf6e6de | 564 | m_hasVMT = false; |
4a82abc8 | 565 | |
caaa4cfd | 566 | Clear(); |
f96b15a3 | 567 | |
6981d3ec JS |
568 | if (m_strings) |
569 | delete m_strings; | |
6de97a3b | 570 | } |
c801d85f | 571 | |
8161b5b9 VZ |
572 | // ---------------------------------------------------------------------------- |
573 | // adding items | |
574 | // ---------------------------------------------------------------------------- | |
575 | ||
a236aa20 VZ |
576 | int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, |
577 | unsigned int pos, | |
578 | void **clientData, | |
579 | wxClientDataType type) | |
bb0ca8df | 580 | { |
a236aa20 | 581 | wxCHECK_MSG( m_list != NULL, wxNOT_FOUND, wxT("invalid listbox") ); |
bb0ca8df | 582 | |
a236aa20 VZ |
583 | const unsigned count = GetCount(); |
584 | wxCHECK_MSG( pos <= count, wxNOT_FOUND, | |
585 | wxT("invalid index in wxListBox::InsertItems") ); | |
11e1c70d RR |
586 | |
587 | // code elsewhere supposes we have as many items in m_clientList as items | |
2ee3ee1b | 588 | // in the listbox |
a236aa20 VZ |
589 | wxASSERT_MSG( m_clientList.GetCount() == count, |
590 | wxT("bug in client data management") ); | |
2ee3ee1b | 591 | |
9f884528 RD |
592 | InvalidateBestSize(); |
593 | ||
a236aa20 | 594 | const unsigned numItems = items.GetCount(); |
9ef6ff8c | 595 | |
a236aa20 | 596 | for ( unsigned int n = 0; n < numItems; ++n, ++pos ) |
bb0ca8df | 597 | { |
a236aa20 | 598 | const wxString& item = items[n]; |
f96b15a3 | 599 | |
a236aa20 VZ |
600 | const unsigned idx = m_strings ? m_strings->Add(item) |
601 | : pos; | |
0a07a7d8 | 602 | |
934960d1 | 603 | GtkAddItem(item, idx == GetCount() ? (unsigned) -1 : idx); |
bb0ca8df | 604 | |
a236aa20 VZ |
605 | m_clientList.Insert(idx, NULL); |
606 | ||
607 | AssignNewItemClientData(idx, clientData, n, type); | |
bb0ca8df | 608 | } |
2ee3ee1b | 609 | |
8228b893 | 610 | wxASSERT_MSG( m_clientList.GetCount() == GetCount(), |
11e1c70d | 611 | wxT("bug in client data management") ); |
9ef6ff8c | 612 | |
a236aa20 | 613 | return pos - 1; |
bb0ca8df VZ |
614 | } |
615 | ||
11e1c70d | 616 | void wxListBox::GtkAddItem( const wxString &item, int pos ) |
c801d85f | 617 | { |
223d09f6 | 618 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
ff8bfdbb | 619 | |
caaa4cfd | 620 | GtkWidget *list_item; |
ff8bfdbb | 621 | |
bb0ca8df | 622 | wxString label(item); |
88ac883a | 623 | #if wxUSE_CHECKLISTBOX |
caaa4cfd RR |
624 | if (m_hasCheckBoxes) |
625 | { | |
d752a3c3 | 626 | label.Prepend(wxCHECKLBOX_STRING); |
caaa4cfd | 627 | } |
88ac883a | 628 | #endif // wxUSE_CHECKLISTBOX |
fc54776e | 629 | |
fab591c5 | 630 | list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) ); |
bb0ca8df | 631 | |
11e1c70d RR |
632 | GList *gitem_list = g_list_alloc (); |
633 | gitem_list->data = list_item; | |
9ef6ff8c | 634 | |
11e1c70d RR |
635 | if (pos == -1) |
636 | gtk_list_append_items( GTK_LIST (m_list), gitem_list ); | |
637 | else | |
638 | gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos ); | |
3502e687 | 639 | |
c4526184 | 640 | gtk_signal_connect_after( GTK_OBJECT(list_item), "select", |
fd0eed64 | 641 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
dcf40a56 | 642 | |
159b66c0 | 643 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
c4526184 | 644 | gtk_signal_connect_after( GTK_OBJECT(list_item), "deselect", |
65045edd | 645 | GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); |
dcf40a56 | 646 | |
ff8bfdbb VZ |
647 | gtk_signal_connect( GTK_OBJECT(list_item), |
648 | "button_press_event", | |
649 | (GtkSignalFunc)gtk_listbox_button_press_callback, | |
650 | (gpointer) this ); | |
651 | ||
74505862 RR |
652 | gtk_signal_connect_after( GTK_OBJECT(list_item), |
653 | "button_release_event", | |
654 | (GtkSignalFunc)gtk_listbox_button_release_callback, | |
655 | (gpointer) this ); | |
656 | ||
354aa1e3 | 657 | gtk_signal_connect( GTK_OBJECT(list_item), |
bb0ca8df VZ |
658 | "key_press_event", |
659 | (GtkSignalFunc)gtk_listbox_key_press_callback, | |
660 | (gpointer)this ); | |
ff8bfdbb | 661 | |
c9433ea9 RR |
662 | |
663 | gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event", | |
664 | GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this ); | |
665 | ||
666 | gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event", | |
667 | GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this ); | |
668 | ||
fd0eed64 RR |
669 | ConnectWidget( list_item ); |
670 | ||
2b07d713 RR |
671 | if (GTK_WIDGET_REALIZED(m_widget)) |
672 | { | |
f2e93537 | 673 | gtk_widget_show( list_item ); |
17a1ebd1 | 674 | |
2b07d713 RR |
675 | gtk_widget_realize( list_item ); |
676 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
014b0d06 | 677 | |
291a8f20 | 678 | #if wxUSE_TOOLTIPS |
f03fc89f | 679 | if (m_tooltip) m_tooltip->Apply( this ); |
291a8f20 | 680 | #endif |
2b07d713 | 681 | } |
631a05be RL |
682 | |
683 | // Apply current widget style to the new list_item | |
684 | GtkRcStyle *style = CreateWidgetStyle(); | |
685 | if (style) | |
686 | { | |
687 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); | |
688 | GtkBin *bin = GTK_BIN( list_item ); | |
17a1ebd1 | 689 | gtk_widget_modify_style( GTK_WIDGET( bin->child ), style ); |
631a05be RL |
690 | gtk_rc_style_unref( style ); |
691 | } | |
fd0eed64 RR |
692 | } |
693 | ||
2ee3ee1b | 694 | // ---------------------------------------------------------------------------- |
8161b5b9 | 695 | // deleting items |
2ee3ee1b | 696 | // ---------------------------------------------------------------------------- |
ff8bfdbb | 697 | |
a236aa20 | 698 | void wxListBox::DoClear() |
fd0eed64 | 699 | { |
223d09f6 | 700 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
fc54776e | 701 | |
8228b893 | 702 | gtk_list_clear_items( m_list, 0, (int)GetCount() ); |
8161b5b9 | 703 | |
780bb874 RR |
704 | if ( GTK_LIST(m_list)->last_focus_child != NULL ) |
705 | { | |
706 | // This should be NULL, I think. | |
707 | GTK_LIST(m_list)->last_focus_child = NULL; | |
708 | } | |
dcf40a56 | 709 | |
11e1c70d | 710 | m_clientList.Clear(); |
ff8bfdbb | 711 | |
2ee3ee1b VZ |
712 | if ( m_strings ) |
713 | m_strings->Clear(); | |
6de97a3b | 714 | } |
c801d85f | 715 | |
a236aa20 | 716 | void wxListBox::DoDeleteOneItem(unsigned int n) |
c801d85f | 717 | { |
223d09f6 | 718 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
fc54776e | 719 | |
fd0eed64 | 720 | GList *child = g_list_nth( m_list->children, n ); |
dcf40a56 | 721 | |
223d09f6 | 722 | wxCHECK_RET( child, wxT("wrong listbox index") ); |
dcf40a56 | 723 | |
bbe0af5b | 724 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
725 | gtk_list_remove_items( m_list, list ); |
726 | g_list_free( list ); | |
dcf40a56 | 727 | |
222ed1d6 | 728 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
2ee3ee1b | 729 | if ( node ) |
fd0eed64 | 730 | { |
222ed1d6 | 731 | m_clientList.Erase( node ); |
f5e27805 | 732 | } |
ff8bfdbb | 733 | |
2ee3ee1b | 734 | if ( m_strings ) |
ba8c1601 | 735 | m_strings->RemoveAt(n); |
2ee3ee1b VZ |
736 | } |
737 | ||
8161b5b9 VZ |
738 | // ---------------------------------------------------------------------------- |
739 | // client data | |
740 | // ---------------------------------------------------------------------------- | |
741 | ||
aa61d352 | 742 | void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) |
8161b5b9 VZ |
743 | { |
744 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); | |
745 | ||
222ed1d6 | 746 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
747 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); |
748 | ||
749 | node->SetData( (wxObject*) clientData ); | |
750 | } | |
751 | ||
aa61d352 | 752 | void* wxListBox::DoGetItemClientData(unsigned int n) const |
8161b5b9 VZ |
753 | { |
754 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); | |
755 | ||
222ed1d6 | 756 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
757 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); |
758 | ||
b1d4dd7a | 759 | return node->GetData(); |
8161b5b9 VZ |
760 | } |
761 | ||
2ee3ee1b VZ |
762 | // ---------------------------------------------------------------------------- |
763 | // string list access | |
764 | // ---------------------------------------------------------------------------- | |
765 | ||
8161b5b9 VZ |
766 | wxString wxListBox::GetRealLabel(GList *item) const |
767 | { | |
768 | GtkBin *bin = GTK_BIN( item->data ); | |
769 | GtkLabel *label = GTK_LABEL( bin->child ); | |
770 | ||
771 | wxString str; | |
772 | ||
8161b5b9 | 773 | str = wxString( label->label ); |
8161b5b9 VZ |
774 | |
775 | #if wxUSE_CHECKLISTBOX | |
776 |