]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
tab traversal now works better (using uninitialized variable was a bad idea :-)
[wxWidgets.git] / src / gtk1 / listbox.cpp
CommitLineData
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
25extern bool g_blockEventsOnDrag;
26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxListBox
29//-----------------------------------------------------------------------------
30
09cf7c58 31static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox )
c801d85f 32{
66bd6b93
RR
33 if (!listbox->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
c801d85f
KB
36 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
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
KB
59
60//-----------------------------------------------------------------------------
61
62IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
63
64wxListBox::wxListBox(void)
65{
c67daf87 66 m_list = (GtkList *) NULL;
6de97a3b 67}
c801d85f
KB
68
69bool wxListBox::Create( wxWindow *parent, wxWindowID id,
70 const wxPoint &pos, const wxSize &size,
debe6624 71 int n, const wxString choices[],
6de97a3b 72 long style, const wxValidator& validator, const wxString &name )
c801d85f
KB
73{
74 m_needParent = TRUE;
75
76 PreCreation( parent, id, pos, size, style, name );
77
6de97a3b
RR
78 SetValidator( validator );
79
c67daf87 80 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
c801d85f
KB
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() );
6a6d4eed
VZ
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 );
c801d85f
KB
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
66bd6b93
RR
103 gtk_container_add( GTK_CONTAINER(m_list), list_item );
104
c801d85f
KB
105 gtk_signal_connect( GTK_OBJECT(list_item), "select",
106 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
107
09cf7c58
RR
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
e2414cbe
RR
112 m_clientData.Append( (wxObject*)NULL );
113
c801d85f 114 gtk_widget_show( list_item );
6de97a3b 115 }
c801d85f
KB
116
117 PostCreation();
118
e3e65dac
RR
119 gtk_widget_realize( GTK_WIDGET(m_list) );
120
c801d85f
KB
121 Show( TRUE );
122
123 return TRUE;
6de97a3b 124}
c801d85f
KB
125
126void wxListBox::Append( const wxString &item )
127{
09cf7c58 128 Append( item, (char*)NULL );
6de97a3b 129}
c801d85f 130
e2414cbe 131void wxListBox::Append( const wxString &item, char *clientData )
c801d85f 132{
868a2826
RR
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 }
e2414cbe 142
09cf7c58 143 gtk_signal_connect( GTK_OBJECT(list_item), "select",
e2414cbe
RR
144 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
145
09cf7c58
RR
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
e2414cbe
RR
150 m_clientData.Append( (wxObject*)clientData );
151
2f6407b9
RR
152 gtk_container_add( GTK_CONTAINER(m_list), list_item );
153
e2414cbe 154 gtk_widget_show( list_item );
6de97a3b 155}
c801d85f
KB
156
157void wxListBox::Clear(void)
158{
159 gtk_list_clear_items( m_list, 0, Number() );
e2414cbe
RR
160
161 m_clientData.Clear();
6de97a3b 162}
c801d85f
KB
163
164void wxListBox::Delete( int n )
165{
30f82ea4
RR
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 );
e2414cbe
RR
177
178 wxNode *node = m_clientData.Nth( n );
179 if (!node)
180 {
30f82ea4 181 wxFAIL_MSG("wrong listbox index");
e2414cbe
RR
182 }
183 else
184 m_clientData.DeleteNode( node );
6de97a3b 185}
c801d85f
KB
186
187void wxListBox::Deselect( int n )
188{
189 gtk_list_unselect_item( m_list, n );
6de97a3b 190}
c801d85f
KB
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;
6de97a3b 203 }
30f82ea4 204 wxFAIL_MSG("wrong listbox index");
c801d85f 205 return -1;
6de97a3b 206}
c801d85f 207
e2414cbe 208char *wxListBox::GetClientData( int n ) const
c801d85f 209{
e2414cbe
RR
210 wxNode *node = m_clientData.Nth( n );
211 if (node) return ((char*)node->Data());
30f82ea4
RR
212
213 wxFAIL_MSG("wrong listbox index");
c67daf87 214 return (char *) NULL;
6de97a3b 215}
c801d85f
KB
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;
6de97a3b
RR
229 }
230 }
30f82ea4 231 wxFAIL_MSG("wrong listbox index");
c801d85f 232 return -1;
6de97a3b 233}
c801d85f 234
6a6d4eed 235int wxListBox::GetSelections(wxArrayInt& aSelections) const
c801d85f 236{
6a6d4eed
VZ
237 // get the number of selected items first
238 GList *child = m_list->children;
239 int count = 0;
868a2826
RR
240 for ( child = m_list->children; child != NULL; child = child->next )
241 {
6a6d4eed
VZ
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;
868a2826
RR
252 for ( child = m_list->children; child != NULL; child = child->next, i++ )
253 {
6a6d4eed
VZ
254 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
255 aSelections.Add(i);
256 }
257 }
258
259 return count;
6de97a3b 260}
c801d85f
KB
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;
6de97a3b 270 }
30f82ea4 271 wxFAIL_MSG("wrong listbox index");
c801d85f 272 return "";
6de97a3b 273}
c801d85f
KB
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;
6de97a3b 283 }
30f82ea4 284 wxFAIL_MSG("no listbox selection available");
c801d85f 285 return "";
6de97a3b 286}
c801d85f
KB
287
288int wxListBox::Number(void)
289{
290 GList *child = m_list->children;
291 int count = 0;
6de97a3b 292 while (child) { count++; child = child->next; }
c801d85f 293 return count;
6de97a3b 294}
c801d85f 295
debe6624 296bool wxListBox::Selected( int n )
c801d85f
KB
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;
6de97a3b
RR
306 }
307 }
30f82ea4 308 wxFAIL_MSG("wrong listbox index");
c801d85f 309 return FALSE;
6de97a3b 310}
c801d85f 311
debe6624 312void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f 313{
30f82ea4 314 wxFAIL_MSG("wxListBox::Set not implemented");
6de97a3b 315}
c801d85f 316
e2414cbe 317void wxListBox::SetClientData( int n, char *clientData )
c801d85f 318{
e2414cbe 319 wxNode *node = m_clientData.Nth( n );
30f82ea4
RR
320 if (node)
321 {
322 node->SetData( (wxObject*)clientData );
323 }
324 else
325 {
326 wxFAIL_MSG("wrong listbox index");
327 }
6de97a3b 328}
c801d85f
KB
329
330void wxListBox::SetFirstItem( int WXUNUSED(n) )
331{
30f82ea4 332 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 333}
c801d85f
KB
334
335void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
336{
30f82ea4 337 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 338}
c801d85f 339
debe6624 340void wxListBox::SetSelection( int n, bool select )
c801d85f
KB
341{
342 if (select)
343 gtk_list_select_item( m_list, n );
344 else
345 gtk_list_unselect_item( m_list, n );
6de97a3b 346}
c801d85f 347
09cf7c58 348void wxListBox::SetString( int n, const wxString &string )
c801d85f 349{
09cf7c58
RR
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 );
6de97a3b 356 }
30f82ea4
RR
357 else
358 {
359 wxFAIL_MSG("wrong listbox index");
360 }
6de97a3b 361}
c801d85f 362
debe6624 363void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f
KB
364{
365 SetSelection( FindString(string), select );
6de97a3b 366}
c801d85f
KB
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;
6de97a3b
RR
379 }
380 }
c801d85f 381 return -1;
6de97a3b 382}
c801d85f 383
30dea054 384GtkWidget *wxListBox::GetConnectWidget(void)
e3e65dac
RR
385{
386 return GTK_WIDGET(m_list);
6de97a3b 387}
e3e65dac 388
868a2826
RR
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}
e3e65dac 403
c801d85f 404