]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/listbox.cpp
Shared libs now get installed correctly on Linux.
[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{
e2414cbe
RR
133 GtkWidget *list_item;
134 list_item = gtk_list_item_new_with_label( item );
135
09cf7c58 136 gtk_signal_connect( GTK_OBJECT(list_item), "select",
e2414cbe
RR
137 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
138
09cf7c58
RR
139 if ( GetWindowStyleFlag() & wxLB_MULTIPLE )
140 gtk_signal_connect( GTK_OBJECT(list_item), "deselect",
141 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
142
e2414cbe
RR
143 m_clientData.Append( (wxObject*)clientData );
144
2f6407b9
RR
145 gtk_container_add( GTK_CONTAINER(m_list), list_item );
146
e2414cbe 147 gtk_widget_show( list_item );
6de97a3b 148}
c801d85f
KB
149
150void wxListBox::Clear(void)
151{
152 gtk_list_clear_items( m_list, 0, Number() );
e2414cbe
RR
153
154 m_clientData.Clear();
6de97a3b 155}
c801d85f
KB
156
157void wxListBox::Delete( int n )
158{
30f82ea4
RR
159 GList *child = g_list_nth( m_list->children, n );
160
161 if (!child)
162 {
163 wxFAIL_MSG("wrong listbox index");
164 return;
165 }
166
167 GList *list = g_list_append( NULL, child->data );
168 gtk_list_remove_items( m_list, list );
169 g_list_free( list );
e2414cbe
RR
170
171 wxNode *node = m_clientData.Nth( n );
172 if (!node)
173 {
30f82ea4 174 wxFAIL_MSG("wrong listbox index");
e2414cbe
RR
175 }
176 else
177 m_clientData.DeleteNode( node );
6de97a3b 178}
c801d85f
KB
179
180void wxListBox::Deselect( int n )
181{
182 gtk_list_unselect_item( m_list, n );
6de97a3b 183}
c801d85f
KB
184
185int wxListBox::FindString( const wxString &item ) const
186{
187 GList *child = m_list->children;
188 int count = 0;
189 while (child)
190 {
191 GtkBin *bin = GTK_BIN( child->data );
192 GtkLabel *label = GTK_LABEL( bin->child );
193 if (item == label->label) return count;
194 count++;
195 child = child->next;
6de97a3b 196 }
30f82ea4 197 wxFAIL_MSG("wrong listbox index");
c801d85f 198 return -1;
6de97a3b 199}
c801d85f 200
e2414cbe 201char *wxListBox::GetClientData( int n ) const
c801d85f 202{
e2414cbe
RR
203 wxNode *node = m_clientData.Nth( n );
204 if (node) return ((char*)node->Data());
30f82ea4
RR
205
206 wxFAIL_MSG("wrong listbox index");
c67daf87 207 return (char *) NULL;
6de97a3b 208}
c801d85f
KB
209
210int wxListBox::GetSelection(void) const
211{
212 GList *selection = m_list->selection;
213 if (selection)
214 {
215 GList *child = m_list->children;
216 int count = 0;
217 while (child)
218 {
219 if (child->data == selection->data) return count;
220 count++;
221 child = child->next;
6de97a3b
RR
222 }
223 }
30f82ea4 224 wxFAIL_MSG("wrong listbox index");
c801d85f 225 return -1;
6de97a3b 226}
c801d85f 227
6a6d4eed 228int wxListBox::GetSelections(wxArrayInt& aSelections) const
c801d85f 229{
6a6d4eed
VZ
230 // get the number of selected items first
231 GList *child = m_list->children;
232 int count = 0;
233 for ( child = m_list->children; child != NULL; child = child->next ) {
234 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
235 count++;
236 }
237
238 aSelections.Empty();
239
240 if ( count > 0 ) {
241 // now fill the list
242 aSelections.Alloc(count); // optimization attempt
243 int i = 0;
244 for ( child = m_list->children; child != NULL; child = child->next, i++ ) {
245 if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED )
246 aSelections.Add(i);
247 }
248 }
249
250 return count;
6de97a3b 251}
c801d85f
KB
252
253wxString wxListBox::GetString( int n ) const
254{
255 GList *child = g_list_nth( m_list->children, n );
256 if (child)
257 {
258 GtkBin *bin = GTK_BIN( child->data );
259 GtkLabel *label = GTK_LABEL( bin->child );
260 return label->label;
6de97a3b 261 }
30f82ea4 262 wxFAIL_MSG("wrong listbox index");
c801d85f 263 return "";
6de97a3b 264}
c801d85f
KB
265
266wxString wxListBox::GetStringSelection(void) const
267{
268 GList *selection = m_list->selection;
269 if (selection)
270 {
271 GtkBin *bin = GTK_BIN( selection->data );
272 wxString tmp = GTK_LABEL( bin->child )->label;
273 return tmp;
6de97a3b 274 }
30f82ea4 275 wxFAIL_MSG("no listbox selection available");
c801d85f 276 return "";
6de97a3b 277}
c801d85f
KB
278
279int wxListBox::Number(void)
280{
281 GList *child = m_list->children;
282 int count = 0;
6de97a3b 283 while (child) { count++; child = child->next; }
c801d85f 284 return count;
6de97a3b 285}
c801d85f 286
debe6624 287bool wxListBox::Selected( int n )
c801d85f
KB
288{
289 GList *target = g_list_nth( m_list->children, n );
290 if (target)
291 {
292 GList *child = m_list->selection;
293 while (child)
294 {
295 if (child->data == target->data) return TRUE;
296 child = child->next;
6de97a3b
RR
297 }
298 }
30f82ea4 299 wxFAIL_MSG("wrong listbox index");
c801d85f 300 return FALSE;
6de97a3b 301}
c801d85f 302
debe6624 303void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
c801d85f 304{
30f82ea4 305 wxFAIL_MSG("wxListBox::Set not implemented");
6de97a3b 306}
c801d85f 307
e2414cbe 308void wxListBox::SetClientData( int n, char *clientData )
c801d85f 309{
e2414cbe 310 wxNode *node = m_clientData.Nth( n );
30f82ea4
RR
311 if (node)
312 {
313 node->SetData( (wxObject*)clientData );
314 }
315 else
316 {
317 wxFAIL_MSG("wrong listbox index");
318 }
6de97a3b 319}
c801d85f
KB
320
321void wxListBox::SetFirstItem( int WXUNUSED(n) )
322{
30f82ea4 323 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 324}
c801d85f
KB
325
326void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
327{
30f82ea4 328 wxFAIL_MSG("wxListBox::SetFirstItem not implemented");
6de97a3b 329}
c801d85f 330
debe6624 331void wxListBox::SetSelection( int n, bool select )
c801d85f
KB
332{
333 if (select)
334 gtk_list_select_item( m_list, n );
335 else
336 gtk_list_unselect_item( m_list, n );
6de97a3b 337}
c801d85f 338
09cf7c58 339void wxListBox::SetString( int n, const wxString &string )
c801d85f 340{
09cf7c58
RR
341 GList *child = g_list_nth( m_list->children, n );
342 if (child)
343 {
344 GtkBin *bin = GTK_BIN( child->data );
345 GtkLabel *label = GTK_LABEL( bin->child );
346 gtk_label_set( label, string );
6de97a3b 347 }
30f82ea4
RR
348 else
349 {
350 wxFAIL_MSG("wrong listbox index");
351 }
6de97a3b 352}
c801d85f 353
debe6624 354void wxListBox::SetStringSelection( const wxString &string, bool select )
c801d85f
KB
355{
356 SetSelection( FindString(string), select );
6de97a3b 357}
c801d85f
KB
358
359int wxListBox::GetIndex( GtkWidget *item ) const
360{
361 if (item)
362 {
363 GList *child = m_list->children;
364 int count = 0;
365 while (child)
366 {
367 if (GTK_WIDGET(child->data) == item) return count;
368 count++;
369 child = child->next;
6de97a3b
RR
370 }
371 }
c801d85f 372 return -1;
6de97a3b 373}
c801d85f 374
30dea054 375GtkWidget *wxListBox::GetConnectWidget(void)
e3e65dac
RR
376{
377 return GTK_WIDGET(m_list);
6de97a3b 378}
e3e65dac
RR
379
380
c801d85f 381