Fixed Fontdialog
[wxWidgets.git] / src / gtk / 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 // data
21 //-----------------------------------------------------------------------------
22
23 extern bool g_blockEventsOnDrag;
24
25 //-----------------------------------------------------------------------------
26 // wxListBox
27 //-----------------------------------------------------------------------------
28
29 static void gtk_listitem_select_callback( GtkWidget *widget, wxListBox *listbox )
30 {
31 if (!listbox->HasVMT()) return;
32 if (g_blockEventsOnDrag) return;
33
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) );
42 event.SetEventObject( listbox );
43
44 listbox->GetEventHandler()->ProcessEvent( event );
45 };
46
47 //-----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
50
51 wxListBox::wxListBox(void)
52 {
53 m_list = NULL;
54 };
55
56 wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
57 const wxPoint &pos, const wxSize &size,
58 int n, const wxString choices[],
59 long style, const wxString &name )
60 {
61 Create( parent, id, pos, size, n, choices, style, name );
62 };
63
64 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
65 const wxPoint &pos, const wxSize &size,
66 int n, const wxString choices[],
67 long style, const wxString &name )
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() );
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 );
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
96 gtk_container_add( GTK_CONTAINER(m_list), list_item );
97
98 gtk_signal_connect( GTK_OBJECT(list_item), "select",
99 GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
100
101 m_clientData.Append( (wxObject*)NULL );
102
103 gtk_widget_show( list_item );
104 };
105
106 PostCreation();
107
108 gtk_widget_realize( GTK_WIDGET(m_list) );
109
110 Show( TRUE );
111
112 return TRUE;
113 };
114
115 void 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
125 m_clientData.Append( (wxObject*)NULL );
126
127 gtk_widget_show( list_item );
128 };
129
130 void wxListBox::Append( const wxString &item, char *clientData )
131 {
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 );
143 };
144
145 void wxListBox::Clear(void)
146 {
147 gtk_list_clear_items( m_list, 0, Number() );
148
149 m_clientData.Clear();
150 };
151
152 void wxListBox::Delete( int n )
153 {
154 gtk_list_clear_items( m_list, n, n );
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 );
163 };
164
165 void wxListBox::Deselect( int n )
166 {
167 gtk_list_unselect_item( m_list, n );
168 };
169
170 int 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
185 char *wxListBox::GetClientData( int n ) const
186 {
187 wxNode *node = m_clientData.Nth( n );
188 if (node) return ((char*)node->Data());
189 return NULL;
190 };
191
192 int 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
209 int wxListBox::GetSelections(wxArrayInt& aSelections) const
210 {
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;
232 };
233
234 wxString 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
246 wxString 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
258 int 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
266 bool wxListBox::Selected( int n )
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
281 void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) )
282 {
283 };
284
285 void wxListBox::SetClientData( int n, char *clientData )
286 {
287 wxNode *node = m_clientData.Nth( n );
288 if (node) node->SetData( (wxObject*)clientData );
289 };
290
291 void wxListBox::SetFirstItem( int WXUNUSED(n) )
292 {
293 };
294
295 void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) )
296 {
297 };
298
299 void wxListBox::SetSelection( int n, bool select )
300 {
301 if (select)
302 gtk_list_select_item( m_list, n );
303 else
304 gtk_list_unselect_item( m_list, n );
305 };
306
307 void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) )
308 {
309 };
310
311 void wxListBox::SetStringSelection( const wxString &string, bool select )
312 {
313 SetSelection( FindString(string), select );
314 };
315
316 int 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
332 GtkWidget *wxListBox::GetDropTargetWidget(void)
333 {
334 return GTK_WIDGET(m_list);
335 };
336
337
338