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