]>
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 | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
6a6d4eed | 16 | #include "wx/dynarray.h" |
c801d85f KB |
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, | |
debe6624 JS |
52 | int n, const wxString choices[], |
53 | long style, const wxString &name ) | |
c801d85f KB |
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, | |
debe6624 JS |
60 | int n, const wxString choices[], |
61 | long style, const wxString &name ) | |
c801d85f KB |
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() ); | |
6a6d4eed VZ |
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 ); | |
c801d85f KB |
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 | ||
e2414cbe RR |
95 | m_clientData.Append( (wxObject*)NULL ); |
96 | ||
c801d85f KB |
97 | gtk_widget_show( list_item ); |
98 | }; | |
99 | ||
100 | PostCreation(); | |
101 | ||
e3e65dac RR |
102 | gtk_widget_realize( GTK_WIDGET(m_list) ); |
103 | ||
c801d85f KB |
104 | Show( TRUE ); |
105 | ||
106 | return TRUE; | |
107 | }; | |
108 | ||
109 | void wxListBox::Append( const wxString &item ) | |
110 | { | |
111 | GtkWidget *list_item; | |
112 | list_item = gtk_list_item_new_with_label( item ); | |
113 | ||
114 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
115 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
116 | ||
117 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); | |
118 | ||
e2414cbe RR |
119 | m_clientData.Append( (wxObject*)NULL ); |
120 | ||
c801d85f KB |
121 | gtk_widget_show( list_item ); |
122 | }; | |
123 | ||
e2414cbe | 124 | void wxListBox::Append( const wxString &item, char *clientData ) |
c801d85f | 125 | { |
e2414cbe RR |
126 | GtkWidget *list_item; |
127 | list_item = gtk_list_item_new_with_label( item ); | |
128 | ||
129 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
130 | GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); | |
131 | ||
132 | gtk_container_add( GTK_CONTAINER(m_list), list_item ); | |
133 | ||
134 | m_clientData.Append( (wxObject*)clientData ); | |
135 | ||
136 | gtk_widget_show( list_item ); | |
c801d85f KB |
137 | }; |
138 | ||
139 | void wxListBox::Clear(void) | |
140 | { | |
141 | gtk_list_clear_items( m_list, 0, Number() ); | |
e2414cbe RR |
142 | |
143 | m_clientData.Clear(); | |
c801d85f KB |
144 | }; |
145 | ||
146 | void wxListBox::Delete( int n ) | |
147 | { | |
148 | gtk_list_clear_items( m_list, n, n ); | |
e2414cbe RR |
149 | |
150 | wxNode *node = m_clientData.Nth( n ); | |
151 | if (!node) | |
152 | { | |
153 | wxFAIL_MSG("wxListBox::Delete wrong index"); | |
154 | } | |
155 | else | |
156 | m_clientData.DeleteNode( node ); | |
c801d85f KB |
157 | }; |
158 | ||
159 | void wxListBox::Deselect( int n ) | |
160 | { | |
161 | gtk_list_unselect_item( m_list, n ); | |
162 | }; | |
163 | ||
164 | int wxListBox::FindString( const wxString &item ) const | |
165 | { | |
166 | GList *child = m_list->children; | |
167 | int count = 0; | |
168 | while (child) | |
169 | { | |
170 | GtkBin *bin = GTK_BIN( child->data ); | |
171 | GtkLabel *label = GTK_LABEL( bin->child ); | |
172 | if (item == label->label) return count; | |
173 | count++; | |
174 | child = child->next; | |
175 | }; | |
176 | return -1; | |
177 | }; | |
178 | ||
e2414cbe | 179 | char *wxListBox::GetClientData( int n ) const |
c801d85f | 180 | { |
e2414cbe RR |
181 | wxNode *node = m_clientData.Nth( n ); |
182 | if (node) return ((char*)node->Data()); | |
c801d85f KB |
183 | return NULL; |
184 | }; | |
185 | ||
186 | int wxListBox::GetSelection(void) const | |
187 | { | |
188 | GList *selection = m_list->selection; | |
189 | if (selection) | |
190 | { | |
191 | GList *child = m_list->children; | |
192 | int count = 0; | |
193 | while (child) | |
194 | { | |
195 | if (child->data == selection->data) return count; | |
196 | count++; | |
197 | child = child->next; | |
198 | }; | |
199 | }; | |
200 | return -1; | |
201 | }; | |
202 | ||
6a6d4eed | 203 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
c801d85f | 204 | { |
6a6d4eed VZ |
205 | // get the number of selected items first |
206 | GList *child = m_list->children; | |
207 | int count = 0; | |
208 | for ( child = m_list->children; child != NULL; child = child->next ) { | |
209 | if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED ) | |
210 | count++; | |
211 | } | |
212 | ||
213 | aSelections.Empty(); | |
214 | ||
215 | if ( count > 0 ) { | |
216 | // now fill the list | |
217 | aSelections.Alloc(count); // optimization attempt | |
218 | int i = 0; | |
219 | for ( child = m_list->children; child != NULL; child = child->next, i++ ) { | |
220 | if ( GTK_WIDGET(child->data)->state == GTK_STATE_SELECTED ) | |
221 | aSelections.Add(i); | |
222 | } | |
223 | } | |
224 | ||
225 | return count; | |
c801d85f KB |
226 | }; |
227 | ||
228 | wxString wxListBox::GetString( int n ) const | |
229 | { | |
230 | GList *child = g_list_nth( m_list->children, n ); | |
231 | if (child) | |
232 | { | |
233 | GtkBin *bin = GTK_BIN( child->data ); | |
234 | GtkLabel *label = GTK_LABEL( bin->child ); | |
235 | return label->label; | |
236 | }; | |
237 | return ""; | |
238 | }; | |
239 | ||
240 | wxString wxListBox::GetStringSelection(void) const | |
241 | { | |
242 | GList *selection = m_list->selection; | |
243 | if (selection) | |
244 | { | |
245 | GtkBin *bin = GTK_BIN( selection->data ); | |
246 | wxString tmp = GTK_LABEL( bin->child )->label; | |
247 | return tmp; | |
248 | }; | |
249 | return ""; | |
250 | }; | |
251 | ||
252 | int wxListBox::Number(void) | |
253 | { | |
254 | GList *child = m_list->children; | |
255 | int count = 0; | |
256 | while (child) { count++; child = child->next; }; | |
257 | return count; | |
258 | }; | |
259 | ||
debe6624 | 260 | bool wxListBox::Selected( int n ) |
c801d85f KB |
261 | { |
262 | GList *target = g_list_nth( m_list->children, n ); | |
263 | if (target) | |
264 | { | |
265 | GList *child = m_list->selection; | |
266 | while (child) | |
267 | { | |
268 | if (child->data == target->data) return TRUE; | |
269 | child = child->next; | |
270 | }; | |
271 | }; | |
272 | return FALSE; | |
273 | }; | |
274 | ||
debe6624 | 275 | void wxListBox::Set( int WXUNUSED(n), const wxString *WXUNUSED(choices) ) |
c801d85f KB |
276 | { |
277 | }; | |
278 | ||
e2414cbe | 279 | void wxListBox::SetClientData( int n, char *clientData ) |
c801d85f | 280 | { |
e2414cbe RR |
281 | wxNode *node = m_clientData.Nth( n ); |
282 | if (node) node->SetData( (wxObject*)clientData ); | |
c801d85f KB |
283 | }; |
284 | ||
285 | void wxListBox::SetFirstItem( int WXUNUSED(n) ) | |
286 | { | |
287 | }; | |
288 | ||
289 | void wxListBox::SetFirstItem( const wxString &WXUNUSED(item) ) | |
290 | { | |
291 | }; | |
292 | ||
debe6624 | 293 | void wxListBox::SetSelection( int n, bool select ) |
c801d85f KB |
294 | { |
295 | if (select) | |
296 | gtk_list_select_item( m_list, n ); | |
297 | else | |
298 | gtk_list_unselect_item( m_list, n ); | |
299 | }; | |
300 | ||
debe6624 | 301 | void wxListBox::SetString( int WXUNUSED(n), const wxString &WXUNUSED(string) ) |
c801d85f KB |
302 | { |
303 | }; | |
304 | ||
debe6624 | 305 | void wxListBox::SetStringSelection( const wxString &string, bool select ) |
c801d85f KB |
306 | { |
307 | SetSelection( FindString(string), select ); | |
308 | }; | |
309 | ||
310 | int wxListBox::GetIndex( GtkWidget *item ) const | |
311 | { | |
312 | if (item) | |
313 | { | |
314 | GList *child = m_list->children; | |
315 | int count = 0; | |
316 | while (child) | |
317 | { | |
318 | if (GTK_WIDGET(child->data) == item) return count; | |
319 | count++; | |
320 | child = child->next; | |
321 | }; | |
322 | }; | |
323 | return -1; | |
324 | }; | |
325 | ||
e3e65dac RR |
326 | GtkWidget *wxListBox::GetDropTargetWidget(void) |
327 | { | |
328 | return GTK_WIDGET(m_list); | |
329 | }; | |
330 | ||
331 | ||
c801d85f | 332 |