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