]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/listbox.cpp
"const" added to wxAceelTable ctor
[wxWidgets.git] / src / gtk1 / listbox.cpp
... / ...
CommitLineData
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
25extern bool g_blockEventsOnDrag;
26
27//-----------------------------------------------------------------------------
28// wxListBox
29//-----------------------------------------------------------------------------
30
31static 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
62IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
63
64wxListBox::wxListBox(void)
65{
66 m_list = (GtkList *) NULL;
67}
68
69bool wxListBox::Create( wxWindow *parent, wxWindowID id,
70 const wxPoint &pos, const wxSize &size,
71 int n, const wxString choices[],
72 long style, const wxValidator& validator, const wxString &name )
73{
74 m_needParent = TRUE;
75
76 PreCreation( parent, id, pos, size, style, name );
77
78 SetValidator( validator );
79
80 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
81 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
82 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
83
84 m_list = GTK_LIST( gtk_list_new() );
85
86 // @@ what's the difference between BROWSE and SINGLE?
87 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
88 if ( style & wxLB_MULTIPLE )
89 mode = GTK_SELECTION_MULTIPLE;
90 else if ( style & wxLB_EXTENDED )
91 mode = GTK_SELECTION_EXTENDED;
92
93 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
94
95 gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
96 gtk_widget_show( GTK_WIDGET(m_list) );
97
98 for (int i = 0; i < n; i++)
99 {
100 GtkWidget *list_item;
101 list_item = gtk_list_item_new_with_label( choices[i] );
102
103 gtk_container_add( GTK_CONTAINER(m_list), list_item );
104
105 gtk_signal_connect( GTK_OBJECT(list_item), "select",
106 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
107
108 if ( style & wxLB_MULTIPLE )
109 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
110 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
111
112 m_clientData.Append( (wxObject*)NULL );
113
114 gtk_widget_show( list_item );
115 }
116
117 PostCreation();
118
119 gtk_widget_realize( GTK_WIDGET(m_list) );
120
121 Show( TRUE );
122
123 return TRUE;
124}
125
126void wxListBox::Append( const wxString &item )
127{
128 Append( item, (char*)NULL );
129}
130
131void wxListBox::Append( const wxString &item, char *clientData )
132{
133 GtkWidget *list_item = gtk_list_item_new_with_label( item );
134
135 if (m_hasOwnStyle)
136 {
137 GtkBin *bin = GTK_BIN( list_item );
138 gtk_widget_set_style( bin->child,
139 gtk_style_ref(
140 gtk_widget_get_style( m_widget ) ) );
141 }
142
143 gtk_signal_connect( GTK_OBJECT(list_item), "select",
144 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
145
146 if ( GetWindowStyleFlag() & wxLB_MULTIPLE )
147 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
148 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
149
150 m_clientData.Append( (wxObject*)clientData );
151
152 gtk_container_add( GTK_CONTAINER(m_list), list_item );
153
154 gtk_widget_show( list_item );
155}
156
157void wxListBox::Clear(void)
158{
159 gtk_list_clear_items( m_list, 0, Number() );
160
161 m_clientData.Clear();
162}
163
164void wxListBox::Delete( int n )
165{
166 GList *child = g_list_nth( m_list->children, n );
167
168 if (!child)
169 {
170 wxFAIL_MSG("wrong listbox index");
171 return;
172 }
173
174 GList *list = g_list_append( NULL, child->data );
175 gtk_list_remove_items( m_list, list );
176 g_list_free( list );
177
178 wxNode *node = m_clientData.Nth( n );
179 if (!node)
180 {
181 wxFAIL_MSG("wrong listbox index");
182 }
183 else
184 m_clientData.DeleteNode( node );
185}
186
187void wxListBox::Deselect( int n )
188{
189 gtk_list_unselect_item( m_list, n );
190}
191
192int wxListBox::FindString( const wxString &item ) const
193{
194 GList *child = m_list->children;
195 int count = 0;
196 while (child)
197 {
198 GtkBin *bin = GTK_BIN( child->data );
199 GtkLabel *label = GTK_LABEL( bin->child );
200 if (item == label->label) return count;
201 count++;
202 child = child->next;
203 }
204 wxFAIL_MSG("wrong listbox index");
205 return -1;
206}
207
208char *wxListBox::GetClientData( int n ) const
209{
210 wxNode *node = m_clientData.Nth( n );
211 if (node) return ((char*)node->Data());
212
213 wxFAIL_MSG("wrong listbox index");
214 return (char *) NULL;
215}
216
217int wxListBox::GetSelection(void) const
218{
219 GList *selection = m_list->selection;
220 if (selection)
221 {
222 GList *child = m_list->children;
223 int count = 0;
224 while (child)
225 {
226 if (child->data == selection->data) return count;
227 count++;
228 child = child->next;
229 }
230 }
231 wxFAIL_MSG("wrong listbox index");
232 return -1;
233}
234
235int wxListBox::GetSelections(wxArrayInt& aSelections) const
236{
237 // get the number of selected items first
238 GList *child = m_list->children;
239 int count = 0;
240 for ( child = m_list->children; child != NULL; child = child->next )
241 {
242 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
243 count++;
244 }
245
246 aSelections.Empty();
247
248 if ( count > 0 ) {
249 // now fill the list
250 aSelections.Alloc(count); // optimization attempt
251 int i = 0;
252 for ( child = m_list->children; child != NULL; child = child->next, i++ )
253 {
254 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
255 aSelections.Add(i);
256 }
257 }
258
259 return count;
260}
261
262wxString wxListBox::GetString( int n ) const
263{
264 GList *child = g_list_nth( m_list->children, n );
265 if (child)
266 {
267 GtkBin *bin = GTK_BIN( child->data );
268 GtkLabel *label = GTK_LABEL( bin->child );
269 return label->label;
270 }
271 wxFAIL_MSG("wrong listbox index");
272 return "";
273}
274
275wxString wxListBox::GetStringSelection(void) const
276{
277 GList *selection = m_list->selection;
278 if (selection)
279 {
280 GtkBin *bin = GTK_BIN( selection->data );
281 wxString tmp = GTK_LABEL( bin->child )->label;
282 return tmp;
283 }
284 wxFAIL_MSG("no listbox selection available");
285 return "";
286}
287
288int wxListBox::Number(void)
289{
290 GList *child = m_list->children;
291 int count = 0;
292 while (child) { count++; child = child->next; }
293 return count;
294}
295
296bool wxListBox::Selected( int n )
297{
298 GList *target = g_list_nth( m_list->children, n );
299 if (target)
300 {
301 GList *child = m_list->selection;
302 while (child)
303 {
304 if (child->data == target->data) return TRUE;
305 child = child->next;
306 }
307 }
308 wxFAIL_MSG("wrong listbox index");
309 return FALSE;
310}
311
312void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
313{
314 wxFAIL_MSG("wxListBox::Set not implemented");
315}
316
317void wxListBox::SetClientData( int n, char *clientData )
318{
319 wxNode *node = m_clientData.Nth( n );
320 if (node)
321 {
322 node->SetData( (wxObject*)clientData );
323 }
324 else
325 {
326 wxFAIL_MSG("wrong listbox index");
327 }
328}
329
330void wxListBox::SetFirstItem( int WXUNUSED(n) )
331{
332 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
333}
334
335void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
336{
337 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
338}
339
340void wxListBox::SetSelection( int n, bool select )
341{
342 if (select)
343 gtk_list_select_item( m_list, n );
344 else
345 gtk_list_unselect_item( m_list, n );
346}
347
348void wxListBox::SetString( int n, const wxString &string )
349{
350 GList *child = g_list_nth( m_list->children, n );
351 if (child)
352 {
353 GtkBin *bin = GTK_BIN( child->data );
354 GtkLabel *label = GTK_LABEL( bin->child );
355 gtk_label_set( label, string );
356 }
357 else
358 {
359 wxFAIL_MSG("wrong listbox index");
360 }
361}
362
363void wxListBox::SetStringSelection( const wxString &string, bool select )
364{
365 SetSelection( FindString(string), select );
366}
367
368int wxListBox::GetIndex( GtkWidget *item ) const
369{
370 if (item)
371 {
372 GList *child = m_list->children;
373 int count = 0;
374 while (child)
375 {
376 if (GTK_WIDGET(child->data) == item) return count;
377 count++;
378 child = child->next;
379 }
380 }
381 return -1;
382}
383
384GtkWidget *wxListBox::GetConnectWidget(void)
385{
386 return GTK_WIDGET(m_list);
387}
388
389void wxListBox::SetFont( const wxFont &font )
390{
391 wxWindow::SetFont( font );
392
393 GList *child = m_list->children;
394 while (child)
395 {
396 GtkBin *bin = (GtkBin*) child->data;
397 gtk_widget_set_style( bin->child,
398 gtk_style_ref(
399 gtk_widget_get_style( m_widget ) ) );
400 child = child->next;
401 }
402}
403
404