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