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