]>
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 | |
354aa1e3 RR |
226 | if (ret) |
227 | { | |
228 | gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" ); | |
229 | return TRUE; | |
230 | } | |
ff8bfdbb | 231 | |
1144d24d RR |
232 | return FALSE; |
233 | } | |
234 | ||
c801d85f | 235 | //----------------------------------------------------------------------------- |
a60c99e6 | 236 | // "select" and "deselect" |
c801d85f KB |
237 | //----------------------------------------------------------------------------- |
238 | ||
0a07a7d8 RR |
239 | static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection ); |
240 | ||
241 | static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox ) | |
242 | { | |
243 | gtk_listitem_select_cb( widget, listbox, TRUE ); | |
244 | } | |
65045edd RR |
245 | |
246 | static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbox ) | |
247 | { | |
0a07a7d8 | 248 | gtk_listitem_select_cb( widget, listbox, FALSE ); |
65045edd RR |
249 | } |
250 | ||
9c9c3d7a VZ |
251 | static void gtk_listitem_select_cb( GtkWidget *widget, |
252 | wxListBox *listbox, | |
253 | bool is_selection ) | |
c801d85f | 254 | { |
acfd422a RR |
255 | if (g_isIdle) wxapp_install_idle_handler(); |
256 | ||
a2053b27 | 257 | if (!listbox->m_hasVMT) return; |
fd0eed64 | 258 | if (g_blockEventsOnDrag) return; |
8161b5b9 | 259 | |
bac95742 | 260 | if (listbox->m_blockEvent) return; |
9ef6ff8c | 261 | |
fd0eed64 | 262 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
6c8a980f | 263 | event.SetEventObject( listbox ); |
8161b5b9 | 264 | |
9c9c3d7a VZ |
265 | // indicate whether this is a selection or a deselection |
266 | event.SetExtraLong( is_selection ); | |
159b66c0 RR |
267 | |
268 | if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0) | |
269 | { | |
270 | int sel = listbox->GtkGetIndex( widget ); | |
271 | ||
272 | if (listbox->m_prevSelection != sel) | |
273 | gtk_list_unselect_item( listbox->m_list, listbox->m_prevSelection ); | |
274 | ||
275 | listbox->m_prevSelection = sel; | |
276 | } | |
dcf40a56 | 277 | |
09cf7c58 | 278 | wxArrayInt aSelections; |
2ee3ee1b | 279 | int n, count = listbox->GetSelections(aSelections); |
09cf7c58 RR |
280 | if ( count > 0 ) |
281 | { | |
2ee3ee1b | 282 | n = aSelections[0]; |
6c8a980f VZ |
283 | if ( listbox->HasClientObjectData() ) |
284 | event.SetClientObject( listbox->GetClientObject(n) ); | |
285 | else if ( listbox->HasClientUntypedData() ) | |
286 | event.SetClientData( listbox->GetClientData(n) ); | |
287 | event.SetString( listbox->GetString(n) ); | |
09cf7c58 RR |
288 | } |
289 | else | |
290 | { | |
2ee3ee1b | 291 | n = -1; |
09cf7c58 RR |
292 | } |
293 | ||
2ee3ee1b VZ |
294 | event.m_commandInt = n; |
295 | ||
159b66c0 RR |
296 | // No longer required with new code in wxLB_SINGLE |
297 | // listbox->GetEventHandler()->AddPendingEvent( event ); | |
298 | listbox->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b | 299 | } |
c801d85f | 300 | |
a60c99e6 RR |
301 | //----------------------------------------------------------------------------- |
302 | // wxListBox | |
c801d85f KB |
303 | //----------------------------------------------------------------------------- |
304 | ||
305 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) | |
306 | ||
2ee3ee1b VZ |
307 | // ---------------------------------------------------------------------------- |
308 | // construction | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
fd0eed64 | 311 | wxListBox::wxListBox() |
c801d85f | 312 | { |
fd0eed64 | 313 | m_list = (GtkList *) NULL; |
88ac883a | 314 | #if wxUSE_CHECKLISTBOX |
caaa4cfd | 315 | m_hasCheckBoxes = FALSE; |
88ac883a | 316 | #endif // wxUSE_CHECKLISTBOX |
6de97a3b | 317 | } |
c801d85f | 318 | |
584ad2a3 MB |
319 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
320 | const wxPoint &pos, const wxSize &size, | |
321 | const wxArrayString& choices, | |
322 | long style, const wxValidator& validator, | |
323 | const wxString &name ) | |
324 | { | |
325 | wxCArrayString chs(choices); | |
326 | ||
327 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
328 | style, validator, name ); | |
329 | } | |
330 | ||
dcf40a56 | 331 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
332 | const wxPoint &pos, const wxSize &size, |
333 | int n, const wxString choices[], | |
2ee3ee1b VZ |
334 | long style, const wxValidator& validator, |
335 | const wxString &name ) | |
c801d85f | 336 | { |
fd0eed64 | 337 | m_needParent = TRUE; |
b292e2f5 | 338 | m_acceptsFocus = TRUE; |
159b66c0 | 339 | m_prevSelection = 0; // or -1 ?? |
bac95742 | 340 | m_blockEvent = FALSE; |
dcf40a56 | 341 | |
4dcaf11a RR |
342 | if (!PreCreation( parent, pos, size ) || |
343 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
344 | { | |
223d09f6 | 345 | wxFAIL_MSG( wxT("wxListBox creation failed") ); |
2ee3ee1b | 346 | return FALSE; |
4dcaf11a | 347 | } |
6de97a3b | 348 | |
fd0eed64 | 349 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
034be888 RR |
350 | if (style & wxLB_ALWAYS_SB) |
351 | { | |
352 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
353 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); | |
354 | } | |
355 | else | |
356 | { | |
357 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
358 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
359 | } | |
ff8bfdbb | 360 | |
fd0eed64 | 361 | m_list = GTK_LIST( gtk_list_new() ); |
dcf40a56 | 362 | |
4a82abc8 | 363 | GtkSelectionMode mode; |
fd0eed64 | 364 | if (style & wxLB_MULTIPLE) |
4a82abc8 | 365 | { |
fd0eed64 | 366 | mode = GTK_SELECTION_MULTIPLE; |
4a82abc8 | 367 | } |
fd0eed64 | 368 | else if (style & wxLB_EXTENDED) |
4a82abc8 | 369 | { |
fd0eed64 | 370 | mode = GTK_SELECTION_EXTENDED; |
4a82abc8 RR |
371 | } |
372 | else | |
373 | { | |
374 | // if style was 0 set single mode | |
375 | m_windowStyle |= wxLB_SINGLE; | |
159b66c0 | 376 | mode = GTK_SELECTION_MULTIPLE; |
4a82abc8 | 377 | } |
6a6d4eed | 378 | |
fd0eed64 | 379 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); |
dcf40a56 | 380 | |
38c7b3d3 | 381 | gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); |
38c7b3d3 | 382 | |
e115e771 RR |
383 | /* make list scroll when moving the focus down using cursor keys */ |
384 | gtk_container_set_focus_vadjustment( | |
385 | GTK_CONTAINER(m_list), | |
386 | gtk_scrolled_window_get_vadjustment( | |
2ee3ee1b | 387 | GTK_SCROLLED_WINDOW(m_widget))); |
e115e771 | 388 | |
fd0eed64 | 389 | gtk_widget_show( GTK_WIDGET(m_list) ); |
dcf40a56 | 390 | |
2ee3ee1b VZ |
391 | if ( style & wxLB_SORT ) |
392 | { | |
393 | // this will change DoAppend() behaviour | |
394 | m_strings = new wxSortedArrayString; | |
395 | } | |
eb553cb2 VZ |
396 | else |
397 | { | |
398 | m_strings = (wxSortedArrayString *)NULL; | |
399 | } | |
2ee3ee1b | 400 | |
fd0eed64 RR |
401 | for (int i = 0; i < n; i++) |
402 | { | |
11e1c70d | 403 | // add one by one |
2ee3ee1b | 404 | DoAppend(choices[i]); |
fd0eed64 | 405 | } |
dcf40a56 | 406 | |
f03fc89f | 407 | m_parent->DoAddChild( this ); |
ff8bfdbb | 408 | |
abdeb9e7 RD |
409 | PostCreation(size); |
410 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
dcf40a56 | 411 | |
fd0eed64 | 412 | return TRUE; |
6de97a3b | 413 | } |
c801d85f | 414 | |
fd0eed64 | 415 | wxListBox::~wxListBox() |
c801d85f | 416 | { |
4a82abc8 RR |
417 | m_hasVMT = FALSE; |
418 | ||
caaa4cfd | 419 | Clear(); |
f96b15a3 | 420 | |
6981d3ec JS |
421 | if (m_strings) |
422 | delete m_strings; | |
6de97a3b | 423 | } |
c801d85f | 424 | |
8161b5b9 VZ |
425 | // ---------------------------------------------------------------------------- |
426 | // adding items | |
427 | // ---------------------------------------------------------------------------- | |
428 | ||
2ee3ee1b | 429 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
bb0ca8df | 430 | { |
223d09f6 | 431 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
bb0ca8df | 432 | |
11e1c70d RR |
433 | // VZ: notice that InsertItems knows nothing about sorting, so calling it |
434 | // from outside (and not from our own Append) is likely to break | |
435 | // everything | |
436 | ||
437 | // code elsewhere supposes we have as many items in m_clientList as items | |
2ee3ee1b | 438 | // in the listbox |
11e1c70d | 439 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
2ee3ee1b VZ |
440 | wxT("bug in client data management") ); |
441 | ||
9f884528 RD |
442 | InvalidateBestSize(); |
443 | ||
bb0ca8df VZ |
444 | GList *children = m_list->children; |
445 | int length = g_list_length(children); | |
9ef6ff8c | 446 | |
223d09f6 | 447 | wxCHECK_RET( pos <= length, wxT("invalid index in wxListBox::InsertItems") ); |
bb0ca8df | 448 | |
2ee3ee1b | 449 | size_t nItems = items.GetCount(); |
0a07a7d8 | 450 | int index; |
2ee3ee1b | 451 | |
0a07a7d8 | 452 | if (m_strings) |
bb0ca8df | 453 | { |
0a07a7d8 | 454 | for (size_t n = 0; n < nItems; n++) |
bb0ca8df | 455 | { |
0a07a7d8 | 456 | index = m_strings->Add( items[n] ); |
f96b15a3 | 457 | |
0a07a7d8 RR |
458 | if (index != GetCount()) |
459 | { | |
460 | GtkAddItem( items[n], index ); | |
222ed1d6 | 461 | wxList::compatibility_iterator node = m_clientList.Item( index ); |
0a07a7d8 RR |
462 | m_clientList.Insert( node, (wxObject*) NULL ); |
463 | } | |
464 | else | |
465 | { | |
466 | GtkAddItem( items[n] ); | |
467 | m_clientList.Append( (wxObject*) NULL ); | |
468 | } | |
bb0ca8df | 469 | } |
bb0ca8df | 470 | } |
11e1c70d | 471 | else |
bb0ca8df | 472 | { |
0a07a7d8 RR |
473 | if (pos == length) |
474 | { | |
475 | for ( size_t n = 0; n < nItems; n++ ) | |
476 | { | |
477 | GtkAddItem( items[n] ); | |
478 | ||
479 | m_clientList.Append((wxObject *)NULL); | |
480 | } | |
481 | } | |
482 | else | |
bb0ca8df | 483 | { |
222ed1d6 | 484 | wxList::compatibility_iterator node = m_clientList.Item( pos ); |
0a07a7d8 RR |
485 | for ( size_t n = 0; n < nItems; n++ ) |
486 | { | |
487 | GtkAddItem( items[n], pos+n ); | |
bb0ca8df | 488 | |
0a07a7d8 RR |
489 | m_clientList.Insert( node, (wxObject *)NULL ); |
490 | } | |
bb0ca8df VZ |
491 | } |
492 | } | |
2ee3ee1b | 493 | |
11e1c70d RR |
494 | wxASSERT_MSG( m_clientList.GetCount() == (size_t)GetCount(), |
495 | wxT("bug in client data management") ); | |
2ee3ee1b VZ |
496 | } |
497 | ||
498 | int wxListBox::DoAppend( const wxString& item ) | |
499 | { | |
9f884528 RD |
500 | InvalidateBestSize(); |
501 | ||
11e1c70d | 502 | if (m_strings) |
2ee3ee1b VZ |
503 | { |
504 | // need to determine the index | |
11e1c70d | 505 | int index = m_strings->Add( item ); |
9ef6ff8c VZ |
506 | |
507 | // only if not at the end anyway | |
508 | if (index != GetCount()) | |
509 | { | |
510 | GtkAddItem( item, index ); | |
511 | ||
222ed1d6 | 512 | wxList::compatibility_iterator node = m_clientList.Item( index ); |
11e1c70d | 513 | m_clientList.Insert( node, (wxObject *)NULL ); |
9ef6ff8c VZ |
514 | |
515 | return index; | |
516 | } | |
2ee3ee1b | 517 | } |
9ef6ff8c | 518 | |
11e1c70d | 519 | GtkAddItem(item); |
2ee3ee1b | 520 | |
11e1c70d | 521 | m_clientList.Append((wxObject *)NULL); |
2ee3ee1b | 522 | |
11e1c70d | 523 | return GetCount() - 1; |
bb0ca8df VZ |
524 | } |
525 | ||
11e1c70d | 526 | void wxListBox::GtkAddItem( const wxString &item, int pos ) |
c801d85f | 527 | { |
223d09f6 | 528 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
ff8bfdbb | 529 | |
caaa4cfd | 530 | GtkWidget *list_item; |
ff8bfdbb | 531 | |
bb0ca8df | 532 | wxString label(item); |
88ac883a | 533 | #if wxUSE_CHECKLISTBOX |
caaa4cfd RR |
534 | if (m_hasCheckBoxes) |
535 | { | |
d752a3c3 | 536 | label.Prepend(wxCHECKLBOX_STRING); |
caaa4cfd | 537 | } |
88ac883a | 538 | #endif // wxUSE_CHECKLISTBOX |
fc54776e | 539 | |
fab591c5 | 540 | list_item = gtk_list_item_new_with_label( wxGTK_CONV( label ) ); |
bb0ca8df | 541 | |
11e1c70d RR |
542 | GList *gitem_list = g_list_alloc (); |
543 | gitem_list->data = list_item; | |
9ef6ff8c | 544 | |
11e1c70d RR |
545 | if (pos == -1) |
546 | gtk_list_append_items( GTK_LIST (m_list), gitem_list ); | |
547 | else | |
548 | gtk_list_insert_items( GTK_LIST (m_list), gitem_list, pos ); | |
3502e687 | 549 | |
fd0eed64 RR |
550 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
551 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
dcf40a56 | 552 | |
159b66c0 | 553 | if (HasFlag(wxLB_MULTIPLE) || HasFlag(wxLB_EXTENDED)) |
fd0eed64 | 554 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", |
65045edd | 555 | GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); |
dcf40a56 | 556 | |
ff8bfdbb VZ |
557 | gtk_signal_connect( GTK_OBJECT(list_item), |
558 | "button_press_event", | |
559 | (GtkSignalFunc)gtk_listbox_button_press_callback, | |
560 | (gpointer) this ); | |
561 | ||
74505862 RR |
562 | gtk_signal_connect_after( GTK_OBJECT(list_item), |
563 | "button_release_event", | |
564 | (GtkSignalFunc)gtk_listbox_button_release_callback, | |
565 | (gpointer) this ); | |
566 | ||
354aa1e3 | 567 | gtk_signal_connect( GTK_OBJECT(list_item), |
bb0ca8df VZ |
568 | "key_press_event", |
569 | (GtkSignalFunc)gtk_listbox_key_press_callback, | |
570 | (gpointer)this ); | |
ff8bfdbb | 571 | |
fd0eed64 RR |
572 | ConnectWidget( list_item ); |
573 | ||
4349acd4 RR |
574 | gtk_widget_show( list_item ); |
575 | ||
2b07d713 RR |
576 | if (GTK_WIDGET_REALIZED(m_widget)) |
577 | { | |
578 | gtk_widget_realize( list_item ); | |
579 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
014b0d06 | 580 | |
11e1c70d | 581 | // Apply current widget style to the new list_item |
f40fdaa3 VS |
582 | GtkRcStyle *style = CreateWidgetStyle(); |
583 | if (style) | |
9ef6ff8c | 584 | { |
f40fdaa3 | 585 | gtk_widget_modify_style( GTK_WIDGET( list_item ), style ); |
91f49163 HH |
586 | GtkBin *bin = GTK_BIN( list_item ); |
587 | GtkWidget *label = GTK_WIDGET( bin->child ); | |
f40fdaa3 VS |
588 | gtk_widget_modify_style( label, style ); |
589 | gtk_rc_style_unref( style ); | |
91f49163 | 590 | } |
2b07d713 | 591 | |
291a8f20 | 592 | #if wxUSE_TOOLTIPS |
f03fc89f | 593 | if (m_tooltip) m_tooltip->Apply( this ); |
291a8f20 | 594 | #endif |
2b07d713 | 595 | } |
fd0eed64 RR |
596 | } |
597 | ||
2ee3ee1b VZ |
598 | void wxListBox::DoSetItems( const wxArrayString& items, |
599 | void **clientData) | |
fd0eed64 | 600 | { |
2ee3ee1b | 601 | Clear(); |
fd0eed64 | 602 | |
2ee3ee1b | 603 | DoInsertItems(items, 0); |
ff8bfdbb | 604 | |
2ee3ee1b VZ |
605 | if ( clientData ) |
606 | { | |
607 | size_t count = items.GetCount(); | |
608 | for ( size_t n = 0; n < count; n++ ) | |
609 | { | |
610 | SetClientData(n, clientData[n]); | |
611 | } | |
612 | } | |
fd0eed64 | 613 | } |
dcf40a56 | 614 | |
2ee3ee1b | 615 | // ---------------------------------------------------------------------------- |
8161b5b9 | 616 | // deleting items |
2ee3ee1b | 617 | // ---------------------------------------------------------------------------- |
ff8bfdbb | 618 | |
fd0eed64 RR |
619 | void wxListBox::Clear() |
620 | { | |
223d09f6 | 621 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
fc54776e | 622 | |
c4c92801 | 623 | gtk_list_clear_items( m_list, 0, GetCount() ); |
8161b5b9 | 624 | |
780bb874 RR |
625 | if ( GTK_LIST(m_list)->last_focus_child != NULL ) |
626 | { | |
627 | // This should be NULL, I think. | |
628 | GTK_LIST(m_list)->last_focus_child = NULL; | |
629 | } | |
dcf40a56 | 630 | |
6c8a980f VZ |
631 | if ( HasClientObjectData() ) |
632 | { | |
633 | // destroy the data (due to Robert's idea of using wxList<wxObject> | |
634 | // and not wxList<wxClientData> we can't just say | |
635 | // m_clientList.DeleteContents(TRUE) - this would crash! | |
222ed1d6 | 636 | wxList::compatibility_iterator node = m_clientList.GetFirst(); |
6c8a980f VZ |
637 | while ( node ) |
638 | { | |
b1d4dd7a RL |
639 | delete (wxClientData *)node->GetData(); |
640 | node = node->GetNext(); | |
6c8a980f VZ |
641 | } |
642 | } | |
11e1c70d | 643 | m_clientList.Clear(); |
ff8bfdbb | 644 | |
2ee3ee1b VZ |
645 | if ( m_strings ) |
646 | m_strings->Clear(); | |
6de97a3b | 647 | } |
c801d85f KB |
648 | |
649 | void wxListBox::Delete( int n ) | |
650 | { | |
223d09f6 | 651 | wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); |
fc54776e | 652 | |
fd0eed64 | 653 | GList *child = g_list_nth( m_list->children, n ); |
dcf40a56 | 654 | |
223d09f6 | 655 | wxCHECK_RET( child, wxT("wrong listbox index") ); |
dcf40a56 | 656 | |
bbe0af5b | 657 | GList *list = g_list_append( (GList*) NULL, child->data ); |
fd0eed64 RR |
658 | gtk_list_remove_items( m_list, list ); |
659 | g_list_free( list ); | |
dcf40a56 | 660 | |
222ed1d6 | 661 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
2ee3ee1b | 662 | if ( node ) |
fd0eed64 | 663 | { |
1e6feb95 | 664 | if ( m_clientDataItemsType == wxClientData_Object ) |
2ee3ee1b | 665 | { |
b1d4dd7a | 666 | wxClientData *cd = (wxClientData*)node->GetData(); |
2ee3ee1b VZ |
667 | delete cd; |
668 | } | |
669 | ||
222ed1d6 | 670 | m_clientList.Erase( node ); |
f5e27805 | 671 | } |
ff8bfdbb | 672 | |
2ee3ee1b | 673 | if ( m_strings ) |
ba8c1601 | 674 | m_strings->RemoveAt(n); |
2ee3ee1b VZ |
675 | } |
676 | ||
8161b5b9 VZ |
677 | // ---------------------------------------------------------------------------- |
678 | // client data | |
679 | // ---------------------------------------------------------------------------- | |
680 | ||
681 | void wxListBox::DoSetItemClientData( int n, void* clientData ) | |
682 | { | |
683 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); | |
684 | ||
222ed1d6 | 685 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
686 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); |
687 | ||
688 | node->SetData( (wxObject*) clientData ); | |
689 | } | |
690 | ||
691 | void* wxListBox::DoGetItemClientData( int n ) const | |
692 | { | |
693 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); | |
694 | ||
222ed1d6 | 695 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
696 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); |
697 | ||
b1d4dd7a | 698 | return node->GetData(); |
8161b5b9 VZ |
699 | } |
700 | ||
701 | void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) | |
702 | { | |
703 | wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); | |
704 | ||
222ed1d6 | 705 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
706 | wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); |
707 | ||
708 | // wxItemContainer already deletes data for us | |
709 | ||
710 | node->SetData( (wxObject*) clientData ); | |
711 | } | |
712 | ||
713 | wxClientData* wxListBox::DoGetItemClientObject( int n ) const | |
714 | { | |
715 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); | |
716 | ||
222ed1d6 | 717 | wxList::compatibility_iterator node = m_clientList.Item( n ); |
8161b5b9 VZ |
718 | wxCHECK_MSG( node, (wxClientData *)NULL, |
719 | wxT("invalid index in wxListBox::DoGetItemClientObject") ); | |
720 | ||
b1d4dd7a | 721 | return (wxClientData*) node->GetData(); |
8161b5b9 VZ |
722 | } |
723 | ||
2ee3ee1b VZ |
724 | // ---------------------------------------------------------------------------- |
725 | // string list access | |
726 | // ---------------------------------------------------------------------------- | |
727 | ||
8161b5b9 VZ |
728 | wxString wxListBox::GetRealLabel(GList *item) const |
729 | { | |
730 | GtkBin *bin = GTK_BIN( item->data ); | |
731 | GtkLabel *label = GTK_LABEL( bin->child ); | |
732 | ||
733 | wxString str; | |
734 | ||
735 | #ifdef __WXGTK20__ | |
736 | str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); | |
737 | #else | |
738 | str = wxString( label->label ); | |
739 | #endif | |
740 | ||
741 | #if wxUSE_CHECKLISTBOX | |
742 |