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