]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
a3622daa | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
6a6d4eed | 16 | #include "wx/dynarray.h" |
c801d85f | 17 | #include "wx/listbox.h" |
09cf7c58 | 18 | #include "wx/utils.h" |
1a5a8367 | 19 | #include <wx/intl.h> |
c801d85f | 20 | |
66bd6b93 RR |
21 | //----------------------------------------------------------------------------- |
22 | // data | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern bool g_blockEventsOnDrag; | |
26 | ||
c801d85f | 27 | //----------------------------------------------------------------------------- |
a60c99e6 | 28 | // "select" and "deselect" |
c801d85f KB |
29 | //----------------------------------------------------------------------------- |
30 | ||
09cf7c58 | 31 | static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox ) |
c801d85f | 32 | { |
66bd6b93 RR |
33 | if (!listbox->HasVMT()) return; |
34 | if (g_blockEventsOnDrag) return; | |
dcf40a56 | 35 | |
c801d85f | 36 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
dcf40a56 | 37 | |
09cf7c58 RR |
38 | wxArrayInt aSelections; |
39 | int count = listbox->GetSelections(aSelections); | |
40 | if ( count > 0 ) | |
41 | { | |
42 | event.m_commandInt = aSelections[0] ; | |
43 | event.m_clientData = listbox->GetClientData(event.m_commandInt); | |
44 | wxString str(listbox->GetString(event.m_commandInt)); | |
45 | if (str != "") | |
46 | event.m_commandString = copystring((char *)(const char *)str); | |
47 | } | |
48 | else | |
49 | { | |
50 | event.m_commandInt = -1 ; | |
51 | event.m_commandString = copystring("") ; | |
52 | } | |
53 | ||
c801d85f | 54 | event.SetEventObject( listbox ); |
a3622daa | 55 | |
47908e25 | 56 | listbox->GetEventHandler()->ProcessEvent( event ); |
09cf7c58 | 57 | if (event.m_commandString) delete[] event.m_commandString ; |
6de97a3b | 58 | } |
c801d85f | 59 | |
a60c99e6 RR |
60 | //----------------------------------------------------------------------------- |
61 | // wxListBox | |
c801d85f KB |
62 | //----------------------------------------------------------------------------- |
63 | ||
64 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) | |
65 | ||
66 | wxListBox::wxListBox(void) | |
67 | { | |
c67daf87 | 68 | m_list = (GtkList *) NULL; |
6de97a3b | 69 | } |
c801d85f | 70 | |
dcf40a56 VZ |
71 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
72 | const wxPoint &pos, const wxSize &size, | |
debe6624 | 73 | int n, const wxString choices[], |
6de97a3b | 74 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f KB |
75 | { |
76 | m_needParent = TRUE; | |
dcf40a56 | 77 | |
c801d85f | 78 | PreCreation( parent, id, pos, size, style, name ); |
dcf40a56 | 79 | |
6de97a3b RR |
80 | SetValidator( validator ); |
81 | ||
c67daf87 | 82 | m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL ); |
c801d85f KB |
83 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), |
84 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
dcf40a56 | 85 | |
c801d85f | 86 | m_list = GTK_LIST( gtk_list_new() ); |
dcf40a56 | 87 | |
d84eb083 | 88 | GtkSelectionMode mode = GTK_SELECTION_BROWSE; |
a60c99e6 | 89 | if (style & wxLB_MULTIPLE) |
6a6d4eed | 90 | mode = GTK_SELECTION_MULTIPLE; |
a60c99e6 | 91 | else if (style & wxLB_EXTENDED) |
6a6d4eed VZ |
92 | mode = GTK_SELECTION_EXTENDED; |
93 | ||
94 | gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); | |
dcf40a56 | 95 | |
c801d85f KB |
96 | gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) ); |
97 | gtk_widget_show( GTK_WIDGET(m_list) ); | |
dcf40a56 | 98 | |
d84eb083 RR |
99 | wxSize newSize = size; |
100 | if (newSize.x == -1) newSize.x = 100; | |
101 | if (newSize.y == -1) newSize.y = 110; | |
102 | SetSize( newSize.x, newSize.y ); | |
dcf40a56 | 103 | |
c801d85f KB |
104 | for (int i = 0; i < n; i++) |
105 | { | |
106 | GtkWidget *list_item; | |
dcf40a56 VZ |
107 | list_item = gtk_list_item_new_with_label( choices[i] ); |
108 | ||
66bd6b93 | 109 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); |
dcf40a56 VZ |
110 | |
111 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
c801d85f | 112 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
dcf40a56 | 113 | |
a60c99e6 | 114 | if (style & wxLB_MULTIPLE) |
dcf40a56 | 115 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", |
09cf7c58 | 116 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
dcf40a56 | 117 | |
e2414cbe | 118 | m_clientData.Append( (wxObject*)NULL ); |
dcf40a56 | 119 | |
c801d85f | 120 | gtk_widget_show( list_item ); |
6de97a3b | 121 | } |
dcf40a56 | 122 | |
c801d85f | 123 | PostCreation(); |
dcf40a56 | 124 | |
e3e65dac | 125 | gtk_widget_realize( GTK_WIDGET(m_list) ); |
dcf40a56 | 126 | |
c801d85f | 127 | Show( TRUE ); |
dcf40a56 | 128 | |
c801d85f | 129 | return TRUE; |
6de97a3b | 130 | } |
c801d85f KB |
131 | |
132 | void wxListBox::Append( const wxString &item ) | |
133 | { | |
09cf7c58 | 134 | Append( item, (char*)NULL ); |
6de97a3b | 135 | } |
c801d85f | 136 | |
e2414cbe | 137 | void wxListBox::Append( const wxString &item, char *clientData ) |
c801d85f | 138 | { |
dcf40a56 VZ |
139 | GtkWidget *list_item = gtk_list_item_new_with_label( item ); |
140 | ||
868a2826 RR |
141 | if (m_hasOwnStyle) |
142 | { | |
143 | GtkBin *bin = GTK_BIN( list_item ); | |
dcf40a56 | 144 | gtk_widget_set_style( bin->child, |
868a2826 | 145 | gtk_style_ref( |
dcf40a56 | 146 | gtk_widget_get_style( m_widget ) ) ); |
868a2826 | 147 | } |
dcf40a56 VZ |
148 | |
149 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
e2414cbe | 150 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
dcf40a56 | 151 | |
a60c99e6 | 152 | if (GetWindowStyleFlag() & wxLB_MULTIPLE) |
dcf40a56 | 153 | gtk_signal_connect( GTK_OBJECT(list_item), "deselect", |
09cf7c58 | 154 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); |
dcf40a56 | 155 | |
e2414cbe | 156 | m_clientData.Append( (wxObject*)clientData ); |
dcf40a56 | 157 | |
2f6407b9 | 158 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); |
dcf40a56 | 159 | |
e2414cbe | 160 | gtk_widget_show( list_item ); |
dcf40a56 | 161 | |
a60c99e6 | 162 | ConnectWidget( list_item ); |
dcf40a56 | 163 | |
a60c99e6 | 164 | ConnectDnDWidget( list_item ); |
dcf40a56 | 165 | |
6de97a3b | 166 | } |
c801d85f KB |
167 | |
168 | void wxListBox::Clear(void) | |
169 | { | |
170 | gtk_list_clear_items( m_list, 0, Number() ); | |
dcf40a56 | 171 | |
e2414cbe | 172 | m_clientData.Clear(); |
6de97a3b | 173 | } |
c801d85f KB |
174 | |
175 | void wxListBox::Delete( int n ) | |
176 | { | |
30f82ea4 | 177 | GList *child = g_list_nth( m_list->children, n ); |
dcf40a56 | 178 | |
30f82ea4 RR |
179 | if (!child) |
180 | { | |
181 | wxFAIL_MSG("wrong listbox index"); | |
182 | return; | |
183 | } | |
dcf40a56 | 184 | |
30f82ea4 RR |
185 | GList *list = g_list_append( NULL, child->data ); |
186 | gtk_list_remove_items( m_list, list ); | |
187 | g_list_free( list ); | |
dcf40a56 | 188 | |
e2414cbe RR |
189 | wxNode *node = m_clientData.Nth( n ); |
190 | if (!node) | |
191 | { | |
30f82ea4 | 192 | wxFAIL_MSG("wrong listbox index"); |
e2414cbe RR |
193 | } |
194 | else | |
195 | m_clientData.DeleteNode( node ); | |
6de97a3b | 196 | } |
c801d85f KB |
197 | |
198 | void wxListBox::Deselect( int n ) | |
199 | { | |
200 | gtk_list_unselect_item( m_list, n ); | |
6de97a3b | 201 | } |
c801d85f KB |
202 | |
203 | int wxListBox::FindString( const wxString &item ) const | |
204 | { | |
205 | GList *child = m_list->children; | |
206 | int count = 0; | |
207 | while (child) | |
208 | { | |
209 | GtkBin *bin = GTK_BIN( child->data ); | |
210 | GtkLabel *label = GTK_LABEL( bin->child ); | |
211 | if (item == label->label) return count; | |
212 | count++; | |
213 | child = child->next; | |
6de97a3b | 214 | } |
dcf40a56 VZ |
215 | |
216 | // it's not an error if the string is not found - this function may be used to | |
217 | // test for existence of the string in the listbox, so don't give any | |
218 | // errors/assert failures. | |
219 | ||
c801d85f | 220 | return -1; |
6de97a3b | 221 | } |
c801d85f | 222 | |
e2414cbe | 223 | char *wxListBox::GetClientData( int n ) const |
c801d85f | 224 | { |
e2414cbe RR |
225 | wxNode *node = m_clientData.Nth( n ); |
226 | if (node) return ((char*)node->Data()); | |
dcf40a56 | 227 | |
30f82ea4 | 228 | wxFAIL_MSG("wrong listbox index"); |
c67daf87 | 229 | return (char *) NULL; |
6de97a3b | 230 | } |
c801d85f KB |
231 | |
232 | int wxListBox::GetSelection(void) const | |
233 | { | |
d84eb083 RR |
234 | GList *child = m_list->children; |
235 | int count = 0; | |
236 | while (child) | |
c801d85f | 237 | { |
d84eb083 RR |
238 | if (GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED) return count; |
239 | count++; | |
240 | child = child->next; | |
6de97a3b | 241 | } |
c801d85f | 242 | return -1; |
6de97a3b | 243 | } |
c801d85f | 244 | |
6a6d4eed | 245 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
c801d85f | 246 | { |
6a6d4eed VZ |
247 | // get the number of selected items first |
248 | GList *child = m_list->children; | |
249 | int count = 0; | |
dcf40a56 | 250 | for ( child = m_list->children; child != NULL; child = child->next ) |
868a2826 | 251 | { |
dcf40a56 | 252 | if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED ) |
6a6d4eed VZ |
253 | count++; |
254 | } | |
255 | ||
256 | aSelections.Empty(); | |
dcf40a56 | 257 | |
6a6d4eed VZ |
258 | if ( count > 0 ) { |
259 | // now fill the list | |
260 | aSelections.Alloc(count); // optimization attempt | |
261 | int i = 0; | |
dcf40a56 | 262 | for ( child = m_list->children; child != NULL; child = child->next, i++ ) |
868a2826 | 263 | { |
dcf40a56 | 264 | if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED ) |
6a6d4eed VZ |
265 | aSelections.Add(i); |
266 | } | |
267 | } | |
dcf40a56 | 268 | |
6a6d4eed | 269 | return count; |
6de97a3b | 270 | } |
c801d85f KB |
271 | |
272 | wxString wxListBox::GetString( int n ) const | |
273 | { | |
274 | GList *child = g_list_nth( m_list->children, n ); | |
275 | if (child) | |
276 | { | |
277 | GtkBin *bin = GTK_BIN( child->data ); | |
278 | GtkLabel *label = GTK_LABEL( bin->child ); | |
279 | return label->label; | |
6de97a3b | 280 | } |
30f82ea4 | 281 | wxFAIL_MSG("wrong listbox index"); |
c801d85f | 282 | return ""; |
6de97a3b | 283 | } |
c801d85f KB |
284 | |
285 | wxString wxListBox::GetStringSelection(void) const | |
286 | { | |
287 | GList *selection = m_list->selection; | |
288 | if (selection) | |
289 | { | |
290 | GtkBin *bin = GTK_BIN( selection->data ); | |
291 | wxString tmp = GTK_LABEL( bin->child )->label; | |
292 | return tmp; | |
6de97a3b | 293 | } |
30f82ea4 | 294 | wxFAIL_MSG("no listbox selection available"); |
c801d85f | 295 | return ""; |
6de97a3b | 296 | } |
c801d85f KB |
297 | |
298 | int wxListBox::Number(void) | |
299 | { | |
300 | GList *child = m_list->children; | |
301 | int count = 0; | |
6de97a3b | 302 | while (child) { count++; child = child->next; } |
c801d85f | 303 | return count; |
6de97a3b | 304 | } |
c801d85f | 305 | |
debe6624 | 306 | bool wxListBox::Selected( int n ) |
c801d85f KB |
307 | { |
308 | GList *target = g_list_nth( m_list->children, n ); | |
309 | if (target) | |
310 | { | |
311 | GList *child = m_list->selection; | |
312 | while (child) | |
313 | { | |
314 | if (child->data == target->data) return TRUE; | |
315 | child = child->next; | |
6de97a3b RR |
316 | } |
317 | } | |
30f82ea4 | 318 | wxFAIL_MSG("wrong listbox index"); |
c801d85f | 319 | return FALSE; |
6de97a3b | 320 | } |
c801d85f | 321 | |
debe6624 | 322 | void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) ) |
c801d85f | 323 | { |
30f82ea4 | 324 | wxFAIL_MSG("wxListBox::Set not implemented"); |
6de97a3b | 325 | } |
c801d85f | 326 | |
e2414cbe | 327 | void wxListBox::SetClientData( int n, char *clientData ) |
c801d85f | 328 | { |
e2414cbe | 329 | wxNode *node = m_clientData.Nth( n ); |
30f82ea4 | 330 | if (node) |
dcf40a56 | 331 | { |
30f82ea4 RR |
332 | node->SetData( (wxObject*)clientData ); |
333 | } | |
334 | else | |
335 | { | |
336 | wxFAIL_MSG("wrong listbox index"); | |
337 | } | |
6de97a3b | 338 | } |
c801d85f KB |
339 | |
340 | void wxListBox::SetFirstItem( int WXUNUSED(n) ) | |
341 | { | |
30f82ea4 | 342 | wxFAIL_MSG("wxListBox::SetFirstItem not implemented"); |
6de97a3b | 343 | } |
c801d85f KB |
344 | |
345 | void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) ) | |
346 | { | |
30f82ea4 | 347 | wxFAIL_MSG("wxListBox::SetFirstItem not implemented"); |
6de97a3b | 348 | } |
c801d85f | 349 | |
debe6624 | 350 | void wxListBox::SetSelection( int n, bool select ) |
c801d85f KB |
351 | { |
352 | if (select) | |
353 | gtk_list_select_item( m_list, n ); | |
354 | else | |
355 | gtk_list_unselect_item( m_list, n ); | |
6de97a3b | 356 | } |
c801d85f | 357 | |
09cf7c58 | 358 | void wxListBox::SetString( int n, const wxString &string ) |
c801d85f | 359 | { |
09cf7c58 RR |
360 | GList *child = g_list_nth( m_list->children, n ); |
361 | if (child) | |
362 | { | |
363 | GtkBin *bin = GTK_BIN( child->data ); | |
364 | GtkLabel *label = GTK_LABEL( bin->child ); | |
365 | gtk_label_set( label, string ); | |
6de97a3b | 366 | } |
30f82ea4 RR |
367 | else |
368 | { | |
369 | wxFAIL_MSG("wrong listbox index"); | |
370 | } | |
6de97a3b | 371 | } |
c801d85f | 372 | |
debe6624 | 373 | void wxListBox::SetStringSelection( const wxString &string, bool select ) |
c801d85f KB |
374 | { |
375 | SetSelection( FindString(string), select ); | |
6de97a3b | 376 | } |
c801d85f KB |
377 | |
378 | int wxListBox::GetIndex( GtkWidget *item ) const | |
379 | { | |
380 | if (item) | |
381 | { | |
382 | GList *child = m_list->children; | |
383 | int count = 0; | |
384 | while (child) | |
385 | { | |
386 | if (GTK_WIDGET(child->data) == item) return count; | |
387 | count++; | |
388 | child = child->next; | |
6de97a3b RR |
389 | } |
390 | } | |
c801d85f | 391 | return -1; |
6de97a3b | 392 | } |
c801d85f | 393 | |
a60c99e6 RR |
394 | void wxListBox::SetDropTarget( wxDropTarget *dropTarget ) |
395 | { | |
396 | GList *child = m_list->children; | |
397 | while (child) | |
398 | { | |
399 | DisconnectDnDWidget( GTK_WIDGET( child->data ) ); | |
400 | child = child->next; | |
401 | } | |
dcf40a56 | 402 | |
a60c99e6 | 403 | wxWindow::SetDropTarget( dropTarget ); |
dcf40a56 | 404 | |
a60c99e6 RR |
405 | child = m_list->children; |
406 | while (child) | |
407 | { | |
408 | ConnectDnDWidget( GTK_WIDGET( child->data ) ); | |
409 | child = child->next; | |
410 | } | |
411 | } | |
412 | ||
30dea054 | 413 | GtkWidget *wxListBox::GetConnectWidget(void) |
e3e65dac RR |
414 | { |
415 | return GTK_WIDGET(m_list); | |
6de97a3b | 416 | } |
e3e65dac | 417 | |
868a2826 RR |
418 | void wxListBox::SetFont( const wxFont &font ) |
419 | { | |
420 | wxWindow::SetFont( font ); | |
a60c99e6 | 421 | |
868a2826 RR |
422 | GList *child = m_list->children; |
423 | while (child) | |
424 | { | |
425 | GtkBin *bin = (GtkBin*) child->data; | |
dcf40a56 | 426 | gtk_widget_set_style( bin->child, |
868a2826 | 427 | gtk_style_ref( |
dcf40a56 | 428 | gtk_widget_get_style( m_widget ) ) ); |
868a2826 RR |
429 | child = child->next; |
430 | } | |
431 | } | |
e3e65dac | 432 | |
c058d771 RR |
433 | bool wxListBox::IsOwnGtkWindow( GdkWindow *window ) |
434 | { | |
435 | if (wxWindow::IsOwnGtkWindow( window )) return TRUE; | |
dcf40a56 | 436 | |
c058d771 RR |
437 | GList *child = m_list->children; |
438 | while (child) | |
439 | { | |
440 | GtkBin *bin = (GtkBin*) child->data; | |
441 | if (bin->child->window == window) return TRUE; | |
442 | child = child->next; | |
443 | } | |
dcf40a56 | 444 | |
c058d771 RR |
445 | return FALSE; |
446 | } | |
447 | ||
c801d85f | 448 |