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