]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/utils.h" | |
19 | ||
20 | //----------------------------------------------------------------------------- | |
21 | // data | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern bool g_blockEventsOnDrag; | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // wxListBox | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | static void gtk_listitem_select_callback( GtkWidget *WXUNUSED(widget), wxListBox *listbox ) | |
31 | { | |
32 | if (!listbox->HasVMT()) return; | |
33 | if (g_blockEventsOnDrag) return; | |
34 | ||
35 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); | |
36 | ||
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 | ||
53 | event.SetEventObject( listbox ); | |
54 | listbox->GetEventHandler()->ProcessEvent( event ); | |
55 | if (event.m_commandString) delete[] event.m_commandString ; | |
56 | }; | |
57 | ||
58 | //----------------------------------------------------------------------------- | |
59 | ||
60 | IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl) | |
61 | ||
62 | wxListBox::wxListBox(void) | |
63 | { | |
64 | m_list = NULL; | |
65 | }; | |
66 | ||
67 | wxListBox::wxListBox( wxWindow *parent, wxWindowID id, | |
68 | const wxPoint &pos, const wxSize &size, | |
69 | int n, const wxString choices[], | |
70 | long style, const wxString &name ) | |
71 | { | |
72 | Create( parent, id, pos, size, n, choices, style, name ); | |
73 | }; | |
74 | ||
75 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, | |
76 | const wxPoint &pos, const wxSize &size, | |
77 | int n, const wxString choices[], | |
78 | long style, const wxString &name ) | |
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() ); | |
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 ); | |
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 | ||
107 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); | |
108 | ||
109 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
110 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
111 | ||
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 | ||
116 | m_clientData.Append( (wxObject*)NULL ); | |
117 | ||
118 | gtk_widget_show( list_item ); | |
119 | }; | |
120 | ||
121 | PostCreation(); | |
122 | ||
123 | gtk_widget_realize( GTK_WIDGET(m_list) ); | |
124 | ||
125 | Show( TRUE ); | |
126 | ||
127 | return TRUE; | |
128 | }; | |
129 | ||
130 | void wxListBox::Append( const wxString &item ) | |
131 | { | |
132 | Append( item, (char*)NULL ); | |
133 | }; | |
134 | ||
135 | void wxListBox::Append( const wxString &item, char *clientData ) | |
136 | { | |
137 | GtkWidget *list_item; | |
138 | list_item = gtk_list_item_new_with_label( item ); | |
139 | ||
140 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
141 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
142 | ||
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 | ||
147 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); | |
148 | ||
149 | m_clientData.Append( (wxObject*)clientData ); | |
150 | ||
151 | gtk_widget_show( list_item ); | |
152 | }; | |
153 | ||
154 | void wxListBox::Clear(void) | |
155 | { | |
156 | gtk_list_clear_items( m_list, 0, Number() ); | |
157 | ||
158 | m_clientData.Clear(); | |
159 | }; | |
160 | ||
161 | void wxListBox::Delete( int n ) | |
162 | { | |
163 | gtk_list_clear_items( m_list, n, n ); | |
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 ); | |
172 | }; | |
173 | ||
174 | void wxListBox::Deselect( int n ) | |
175 | { | |
176 | gtk_list_unselect_item( m_list, n ); | |
177 | }; | |
178 | ||
179 | int 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 | ||
194 | char *wxListBox::GetClientData( int n ) const | |
195 | { | |
196 | wxNode *node = m_clientData.Nth( n ); | |
197 | if (node) return ((char*)node->Data()); | |
198 | return NULL; | |
199 | }; | |
200 | ||
201 | int 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 | ||
218 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
219 | { | |
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; | |
241 | }; | |
242 | ||
243 | wxString 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 | ||
255 | wxString 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 | ||
267 | int 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 | ||
275 | bool wxListBox::Selected( int n ) | |
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 | ||
290 | void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) ) | |
291 | { | |
292 | }; | |
293 | ||
294 | void wxListBox::SetClientData( int n, char *clientData ) | |
295 | { | |
296 | wxNode *node = m_clientData.Nth( n ); | |
297 | if (node) node->SetData( (wxObject*)clientData ); | |
298 | }; | |
299 | ||
300 | void wxListBox::SetFirstItem( int WXUNUSED(n) ) | |
301 | { | |
302 | }; | |
303 | ||
304 | void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) ) | |
305 | { | |
306 | }; | |
307 | ||
308 | void wxListBox::SetSelection( int n, bool select ) | |
309 | { | |
310 | if (select) | |
311 | gtk_list_select_item( m_list, n ); | |
312 | else | |
313 | gtk_list_unselect_item( m_list, n ); | |
314 | }; | |
315 | ||
316 | void wxListBox::SetString( int n, const wxString &string ) | |
317 | { | |
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 | }; | |
325 | }; | |
326 | ||
327 | void wxListBox::SetStringSelection( const wxString &string, bool select ) | |
328 | { | |
329 | SetSelection( FindString(string), select ); | |
330 | }; | |
331 | ||
332 | int 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 | ||
348 | GtkWidget *wxListBox::GetDropTargetWidget(void) | |
349 | { | |
350 | return GTK_WIDGET(m_list); | |
351 | }; | |
352 | ||
353 | ||
354 |