]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
* Fixes and new features in wxObject*Stream
[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
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "listbox.h"
14#endif
15
6a6d4eed 16#include "wx/dynarray.h"
c801d85f
KB
17#include "wx/listbox.h"
18
66bd6b93
RR
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
23extern bool g_blockEventsOnDrag;
24
c801d85f
KB
25//-----------------------------------------------------------------------------
26// wxListBox
27//-----------------------------------------------------------------------------
28
66bd6b93 29static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
c801d85f 30{
66bd6b93
RR
31 if (!listbox->HasVMT()) return;
32 if (g_blockEventsOnDrag) return;
33
c801d85f
KB
34 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
35
36 event.SetInt( listbox->GetIndex( widget ) );
37
38 GtkBin *bin = GTK_BIN( widget );
39 GtkLabel *label = GTK_LABEL( bin->child );
40 wxString tmp( label->label );
41 event.SetString( WXSTRINGCAST(tmp) );
c801d85f
KB
42 event.SetEventObject( listbox );
43
47908e25 44 listbox->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
45};
46
47//-----------------------------------------------------------------------------
48
49IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
50
51wxListBox::wxListBox(void)
52{
53 m_list = NULL;
54};
55
56wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
57 const wxPoint &pos, const wxSize &size,
debe6624
JS
58 int n, const wxString choices[],
59 long style, const wxString &name )
c801d85f
KB
60{
61 Create( parent, id, pos, size, n, choices, style, name );
62};
63
64bool wxListBox::Create( wxWindow *parent, wxWindowID id,
65 const wxPoint &pos, const wxSize &size,
debe6624
JS
66 int n, const wxString choices[],
67 long style, const wxString &name )
c801d85f
KB
68{
69 m_needParent = TRUE;
70
71 PreCreation( parent, id, pos, size, style, name );
72
73 m_widget = gtk_scrolled_window_new( NULL, NULL );
74 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
75 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
76
77 m_list = GTK_LIST( gtk_list_new() );
6a6d4eed
VZ
78
79 // @@ what's the difference between BROWSE and SINGLE?
80 GtkSelectionMode mode = GTK_SELECTION_BROWSE;
81 if ( style & wxLB_MULTIPLE )
82 mode = GTK_SELECTION_MULTIPLE;
83 else if ( style & wxLB_EXTENDED )
84 mode = GTK_SELECTION_EXTENDED;
85
86 gtk_list_set_selection_mode( GTK_LIST(m_list), mode );
c801d85f
KB
87
88 gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
89 gtk_widget_show( GTK_WIDGET(m_list) );
90
91 for (int i = 0; i < n; i++)
92 {
93 GtkWidget *list_item;
94 list_item = gtk_list_item_new_with_label( choices[i] );
95
66bd6b93
RR
96 gtk_container_add( GTK_CONTAINER(m_list), list_item );
97
c801d85f
KB
98 gtk_signal_connect( GTK_OBJECT(list_item), "select",
99 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
100
e2414cbe
RR
101 m_clientData.Append( (wxObject*)NULL );
102
c801d85f
KB
103 gtk_widget_show( list_item );
104 };
105
106 PostCreation();
107
e3e65dac
RR
108 gtk_widget_realize( GTK_WIDGET(m_list) );
109
c801d85f
KB
110 Show( TRUE );
111
112 return TRUE;
113};
114
115void wxListBox::Append( const wxString &item )
116{
117 GtkWidget *list_item;
118 list_item = gtk_list_item_new_with_label( item );
119
120 gtk_signal_connect( GTK_OBJECT(list_item), "select",
121 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
122
123 gtk_container_add( GTK_CONTAINER(m_list), list_item );
124
e2414cbe
RR
125 m_clientData.Append( (wxObject*)NULL );
126
c801d85f
KB
127 gtk_widget_show( list_item );
128};
129
e2414cbe 130void wxListBox::Append( const wxString &item, char *clientData )
c801d85f 131{
e2414cbe
RR
132 GtkWidget *list_item;
133 list_item = gtk_list_item_new_with_label( item );
134
135 gtk_signal_connect( GTK_OBJECT(list_item), "select",
136 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
137
138 gtk_container_add( GTK_CONTAINER(m_list), list_item );
139
140 m_clientData.Append( (wxObject*)clientData );
141
142 gtk_widget_show( list_item );
c801d85f
KB
143};
144
145void wxListBox::Clear(void)
146{
147 gtk_list_clear_items( m_list, 0, Number() );
e2414cbe
RR
148
149 m_clientData.Clear();
c801d85f
KB
150};
151
152void wxListBox::Delete( int n )
153{
154 gtk_list_clear_items( m_list, n, n );
e2414cbe
RR
155
156 wxNode *node = m_clientData.Nth( n );
157 if (!node)
158 {
159 wxFAIL_MSG("wxListBox::Delete wrong index");
160 }
161 else
162 m_clientData.DeleteNode( node );
c801d85f
KB
163};
164
165void wxListBox::Deselect( int n )
166{
167 gtk_list_unselect_item( m_list, n );
168};
169
170int wxListBox::FindString( const wxString &item ) const
171{
172 GList *child = m_list->children;
173 int count = 0;
174 while (child)
175 {
176 GtkBin *bin = GTK_BIN( child->data );
177 GtkLabel *label = GTK_LABEL( bin->child );
178 if (item == label->label) return count;
179 count++;
180 child = child->next;
181 };
182 return -1;
183};
184
e2414cbe 185char *wxListBox::GetClientData( int n ) const
c801d85f 186{
e2414cbe
RR
187 wxNode *node = m_clientData.Nth( n );
188 if (node) return ((char*)node->Data());
c801d85f
KB
189 return NULL;
190};
191
192int wxListBox::GetSelection(void) const
193{
194 GList *selection = m_list->selection;
195 if (selection)
196 {
197 GList *child = m_list->children;
198 int count = 0;
199 while (child)
200 {
201 if (child->data == selection->data) return count;
202 count++;
203 child = child->next;
204 };
205 };
206 return -1;
207};
208
6a6d4eed 209int wxListBox::GetSelections(wxArrayInt& aSelections) const
c801d85f 210{
6a6d4eed
VZ
211 // get the number of selected items first
212 GList *child = m_list->children;
213 int count = 0;
214 for ( child = m_list->children; child != NULL; child = child->next ) {
215 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
216 count++;
217 }
218
219 aSelections.Empty();
220
221 if ( count > 0 ) {
222 // now fill the list
223 aSelections.Alloc(count); // optimization attempt
224 int i = 0;
225 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
226 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
227 aSelections.Add(i);
228 }
229 }
230
231 return count;
c801d85f
KB
232};
233
234wxString wxListBox::GetString( int n ) const
235{
236 GList *child = g_list_nth( m_list->children, n );
237 if (child)
238 {
239 GtkBin *bin = GTK_BIN( child->data );
240 GtkLabel *label = GTK_LABEL( bin->child );
241 return label->label;
242 };
243 return "";
244};
245
246wxString wxListBox::GetStringSelection(void) const
247{
248 GList *selection = m_list->selection;
249 if (selection)
250 {
251 GtkBin *bin = GTK_BIN( selection->data );
252 wxString tmp = GTK_LABEL( bin->child )->label;
253 return tmp;
254 };
255 return "";
256};
257
258int wxListBox::Number(void)
259{
260 GList *child = m_list->children;
261 int count = 0;
262 while (child) { count++; child = child->next; };
263 return count;
264};
265
debe6624 266bool wxListBox::Selected( int n )
c801d85f
KB
267{
268 GList *target = g_list_nth( m_list->children, n );
269 if (target)
270 {
271 GList *child = m_list->selection;
272 while (child)
273 {
274 if (child->data == target->data) return TRUE;
275 child = child->next;
276 };
277 };
278 return FALSE;
279};
280
debe6624 281void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f
KB
282{
283};
284
e2414cbe 285void wxListBox::SetClientData( int n, char *clientData )
c801d85f 286{
e2414cbe
RR
287 wxNode *node = m_clientData.Nth( n );
288 if (node) node->SetData( (wxObject*)clientData );
c801d85f
KB
289};
290
291void wxListBox::SetFirstItem( int WXUNUSED(n) )
292{
293};
294
295void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
296{
297};
298
debe6624 299void wxListBox::SetSelection( int n, bool select )
c801d85f
KB
300{
301 if (select)
302 gtk_list_select_item( m_list, n );
303 else
304 gtk_list_unselect_item( m_list, n );
305};
306
debe6624 307void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) )
c801d85f
KB
308{
309};
310
debe6624 311void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f
KB
312{
313 SetSelection( FindString(string), select );
314};
315
316int wxListBox::GetIndex( GtkWidget *item ) const
317{
318 if (item)
319 {
320 GList *child = m_list->children;
321 int count = 0;
322 while (child)
323 {
324 if (GTK_WIDGET(child->data) == item) return count;
325 count++;
326 child = child->next;
327 };
328 };
329 return -1;
330};
331
e3e65dac
RR
332GtkWidget *wxListBox::GetDropTargetWidget(void)
333{
334 return GTK_WIDGET(m_list);
335};
336
337
c801d85f 338