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