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