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