]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: combobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "combobox.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/combobox.h" | |
15 | #include <wx/intl.h> | |
16 | ||
17 | //----------------------------------------------------------------------------- | |
18 | // data | |
19 | //----------------------------------------------------------------------------- | |
20 | ||
21 | extern bool g_blockEventsOnDrag; | |
22 | ||
23 | //----------------------------------------------------------------------------- | |
24 | // "select" | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
28 | { | |
29 | if (!combo->HasVMT()) return; | |
30 | if (g_blockEventsOnDrag) return; | |
31 | ||
32 | if (combo->m_alreadySent) | |
33 | { | |
34 | combo->m_alreadySent = FALSE; | |
35 | return; | |
36 | } | |
37 | ||
38 | combo->m_alreadySent = TRUE; | |
39 | ||
40 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId()); | |
41 | event.SetInt( combo->GetSelection() ); | |
42 | wxString tmp( combo->GetStringSelection() ); | |
43 | event.SetString( WXSTRINGCAST(tmp) ); | |
44 | event.SetEventObject(combo); | |
45 | combo->GetEventHandler()->ProcessEvent(event); | |
46 | } | |
47 | ||
48 | //----------------------------------------------------------------------------- | |
49 | // wxComboBox | |
50 | //----------------------------------------------------------------------------- | |
51 | ||
52 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
53 | ||
54 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) | |
55 | EVT_SIZE(wxComboBox::OnSize) | |
56 | END_EVENT_TABLE() | |
57 | ||
58 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, | |
59 | const wxPoint& pos, const wxSize& size, | |
60 | int n, const wxString choices[], | |
61 | long style, const wxValidator& validator, | |
62 | const wxString& name ) | |
63 | { | |
64 | m_alreadySent = FALSE; | |
65 | m_needParent = TRUE; | |
66 | ||
67 | PreCreation( parent, id, pos, size, style, name ); | |
68 | ||
69 | SetValidator( validator ); | |
70 | ||
71 | m_widget = gtk_combo_new(); | |
72 | ||
73 | wxSize newSize = size; | |
74 | if (newSize.x == -1) newSize.x = 100; | |
75 | if (newSize.y == -1) newSize.y = 26; | |
76 | SetSize( newSize.x, newSize.y ); | |
77 | ||
78 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
79 | ||
80 | for (int i = 0; i < n; i++) | |
81 | { | |
82 | GtkWidget *list_item = gtk_list_item_new_with_label( choices[i] ); | |
83 | ||
84 | m_clientDataList.Append( (wxObject*)NULL ); | |
85 | m_clientObjectList.Append( (wxObject*)NULL ); | |
86 | ||
87 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
88 | ||
89 | gtk_widget_realize( list_item ); | |
90 | gtk_widget_realize( GTK_BIN(list_item)->child ); | |
91 | ||
92 | gtk_widget_show( list_item ); | |
93 | ||
94 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
95 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
96 | } | |
97 | ||
98 | m_parent->AddChild( this ); | |
99 | ||
100 | (m_parent->m_insertCallback)( m_parent, this ); | |
101 | ||
102 | PostCreation(); | |
103 | ||
104 | ConnectWidget( GTK_COMBO(m_widget)->button ); | |
105 | ||
106 | if (!value.IsNull()) SetValue( value ); | |
107 | ||
108 | gtk_widget_realize( GTK_COMBO(m_widget)->list ); | |
109 | gtk_widget_realize( GTK_COMBO(m_widget)->entry ); | |
110 | gtk_widget_realize( GTK_COMBO(m_widget)->button ); | |
111 | ||
112 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
113 | SetForegroundColour( parent->GetForegroundColour() ); | |
114 | ||
115 | Show( TRUE ); | |
116 | ||
117 | return TRUE; | |
118 | } | |
119 | ||
120 | wxComboBox::~wxComboBox() | |
121 | { | |
122 | wxNode *node = m_clientDataList.First(); | |
123 | while (node) | |
124 | { | |
125 | wxClientData *cd = (wxClientData*)node->Data(); | |
126 | if (cd) delete cd; | |
127 | node = node->Next(); | |
128 | } | |
129 | m_clientDataList.Clear(); | |
130 | } | |
131 | ||
132 | void wxComboBox::AppendCommon( const wxString &item ) | |
133 | { | |
134 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
135 | ||
136 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
137 | ||
138 | GtkWidget *list_item = gtk_list_item_new_with_label( item ); | |
139 | ||
140 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
141 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
142 | ||
143 | gtk_container_add( GTK_CONTAINER(list), list_item ); | |
144 | ||
145 | if (m_widgetStyle) ApplyWidgetStyle(); | |
146 | ||
147 | gtk_widget_show( list_item ); | |
148 | } | |
149 | ||
150 | void wxComboBox::Append( const wxString &item ) | |
151 | { | |
152 | m_clientDataList.Append( (wxObject*) NULL ); | |
153 | m_clientObjectList.Append( (wxObject*) NULL ); | |
154 | ||
155 | AppendCommon( item ); | |
156 | } | |
157 | ||
158 | void wxComboBox::Append( const wxString &item, void *clientData ) | |
159 | { | |
160 | m_clientDataList.Append( (wxObject*) clientData ); | |
161 | m_clientObjectList.Append( (wxObject*)NULL ); | |
162 | ||
163 | AppendCommon( item ); | |
164 | } | |
165 | ||
166 | void wxComboBox::Append( const wxString &item, wxClientData *clientData ) | |
167 | { | |
168 | m_clientDataList.Append( (wxObject*) NULL ); | |
169 | m_clientObjectList.Append( (wxObject*) clientData ); | |
170 | ||
171 | AppendCommon( item ); | |
172 | } | |
173 | ||
174 | void wxComboBox::SetClientData( int n, void* clientData ) | |
175 | { | |
176 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
177 | ||
178 | wxNode *node = m_clientDataList.Nth( n ); | |
179 | if (!node) return; | |
180 | ||
181 | node->SetData( (wxObject*) clientData ); | |
182 | } | |
183 | ||
184 | void* wxComboBox::GetClientData( int n ) | |
185 | { | |
186 | wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" ); | |
187 | ||
188 | wxNode *node = m_clientDataList.Nth( n ); | |
189 | if (!node) return NULL; | |
190 | ||
191 | return node->Data(); | |
192 | } | |
193 | ||
194 | void wxComboBox::SetClientObject( int n, wxClientData* clientData ) | |
195 | { | |
196 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
197 | ||
198 | wxNode *node = m_clientObjectList.Nth( n ); | |
199 | if (!node) return; | |
200 | ||
201 | wxClientData *cd = (wxClientData*) node->Data(); | |
202 | if (cd) delete cd; | |
203 | ||
204 | node->SetData( (wxObject*) clientData ); | |
205 | } | |
206 | ||
207 | wxClientData* wxComboBox::GetClientObject( int n ) | |
208 | { | |
209 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" ); | |
210 | ||
211 | wxNode *node = m_clientDataList.Nth( n ); | |
212 | if (!node) return (wxClientData*) NULL; | |
213 | ||
214 | return (wxClientData*) node->Data(); | |
215 | } | |
216 | ||
217 | void wxComboBox::Clear() | |
218 | { | |
219 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
220 | ||
221 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
222 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
223 | ||
224 | wxNode *node = m_clientObjectList.First(); | |
225 | while (node) | |
226 | { | |
227 | wxClientData *cd = (wxClientData*)node->Data(); | |
228 | if (cd) delete cd; | |
229 | node = node->Next(); | |
230 | } | |
231 | m_clientObjectList.Clear(); | |
232 | ||
233 | m_clientDataList.Clear(); | |
234 | } | |
235 | ||
236 | void wxComboBox::Delete( int n ) | |
237 | { | |
238 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
239 | ||
240 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
241 | ||
242 | GList *child = g_list_nth( listbox->children, n ); | |
243 | ||
244 | if (!child) | |
245 | { | |
246 | wxFAIL_MSG("wrong index"); | |
247 | return; | |
248 | } | |
249 | ||
250 | GList *list = g_list_append( NULL, child->data ); | |
251 | gtk_list_remove_items( listbox, list ); | |
252 | g_list_free( list ); | |
253 | ||
254 | wxNode *node = m_clientObjectList.Nth( n ); | |
255 | if (node) | |
256 | { | |
257 | wxClientData *cd = (wxClientData*)node->Data(); | |
258 | if (cd) delete cd; | |
259 | m_clientObjectList.DeleteNode( node ); | |
260 | } | |
261 | ||
262 | node = m_clientDataList.Nth( n ); | |
263 | if (node) | |
264 | { | |
265 | m_clientDataList.DeleteNode( node ); | |
266 | } | |
267 | } | |
268 | ||
269 | int wxComboBox::FindString( const wxString &item ) | |
270 | { | |
271 | wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" ); | |
272 | ||
273 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
274 | ||
275 | GList *child = GTK_LIST(list)->children; | |
276 | int count = 0; | |
277 | while (child) | |
278 | { | |
279 | GtkBin *bin = GTK_BIN( child->data ); | |
280 | GtkLabel *label = GTK_LABEL( bin->child ); | |
281 | if (item == label->label) return count; | |
282 | count++; | |
283 | child = child->next; | |
284 | } | |
285 | ||
286 | wxFAIL_MSG( "wxComboBox: string not found" ); | |
287 | ||
288 | return -1; | |
289 | } | |
290 | ||
291 | int wxComboBox::GetSelection() const | |
292 | { | |
293 | wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" ); | |
294 | ||
295 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
296 | ||
297 | GList *selection = GTK_LIST(list)->selection; | |
298 | if (selection) | |
299 | { | |
300 | GList *child = GTK_LIST(list)->children; | |
301 | int count = 0; | |
302 | while (child) | |
303 | { | |
304 | if (child->data == selection->data) return count; | |
305 | count++; | |
306 | child = child->next; | |
307 | } | |
308 | } | |
309 | ||
310 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
311 | ||
312 | return -1; | |
313 | } | |
314 | ||
315 | wxString wxComboBox::GetString( int n ) const | |
316 | { | |
317 | wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" ); | |
318 | ||
319 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
320 | ||
321 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
322 | if (child) | |
323 | { | |
324 | GtkBin *bin = GTK_BIN( child->data ); | |
325 | GtkLabel *label = GTK_LABEL( bin->child ); | |
326 | return label->label; | |
327 | } | |
328 | ||
329 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
330 | ||
331 | return ""; | |
332 | } | |
333 | ||
334 | wxString wxComboBox::GetStringSelection() const | |
335 | { | |
336 | wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" ); | |
337 | ||
338 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
339 | ||
340 | GList *selection = GTK_LIST(list)->selection; | |
341 | if (selection) | |
342 | { | |
343 | GtkBin *bin = GTK_BIN( selection->data ); | |
344 | wxString tmp = GTK_LABEL( bin->child )->label; | |
345 | return tmp; | |
346 | } | |
347 | ||
348 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
349 | ||
350 | return ""; | |
351 | } | |
352 | ||
353 | int wxComboBox::Number() const | |
354 | { | |
355 | wxCHECK_MSG( m_widget != NULL, 0, "invalid combobox" ); | |
356 | ||
357 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
358 | ||
359 | GList *child = GTK_LIST(list)->children; | |
360 | int count = 0; | |
361 | while (child) { count++; child = child->next; } | |
362 | return count; | |
363 | } | |
364 | ||
365 | void wxComboBox::SetSelection( int n ) | |
366 | { | |
367 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
368 | ||
369 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
370 | gtk_list_select_item( GTK_LIST(list), n ); | |
371 | } | |
372 | ||
373 | void wxComboBox::SetStringSelection( const wxString &string ) | |
374 | { | |
375 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
376 | ||
377 | int res = FindString( string ); | |
378 | if (res == -1) return; | |
379 | SetSelection( res ); | |
380 | } | |
381 | ||
382 | wxString wxComboBox::GetValue() const | |
383 | { | |
384 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
385 | wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) ); | |
386 | return tmp; | |
387 | } | |
388 | ||
389 | void wxComboBox::SetValue( const wxString& value ) | |
390 | { | |
391 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
392 | ||
393 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
394 | wxString tmp = ""; | |
395 | if (!value.IsNull()) tmp = value; | |
396 | gtk_entry_set_text( GTK_ENTRY(entry), tmp ); | |
397 | } | |
398 | ||
399 | void wxComboBox::Copy() | |
400 | { | |
401 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
402 | ||
403 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
404 | #if (GTK_MINOR_VERSION == 1) | |
405 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) ); | |
406 | #else | |
407 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); | |
408 | #endif | |
409 | } | |
410 | ||
411 | void wxComboBox::Cut() | |
412 | { | |
413 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
414 | ||
415 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
416 | #if (GTK_MINOR_VERSION == 1) | |
417 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) ); | |
418 | #else | |
419 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); | |
420 | #endif | |
421 | } | |
422 | ||
423 | void wxComboBox::Paste() | |
424 | { | |
425 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
426 | ||
427 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
428 | #if (GTK_MINOR_VERSION == 1) | |
429 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) ); | |
430 | #else | |
431 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); | |
432 | #endif | |
433 | } | |
434 | ||
435 | void wxComboBox::SetInsertionPoint( long pos ) | |
436 | { | |
437 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
438 | ||
439 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
440 | int tmp = (int) pos; | |
441 | gtk_entry_set_position( GTK_ENTRY(entry), tmp ); | |
442 | } | |
443 | ||
444 | void wxComboBox::SetInsertionPointEnd() | |
445 | { | |
446 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
447 | ||
448 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
449 | int pos = GTK_ENTRY(entry)->text_length; | |
450 | SetInsertionPoint( pos-1 ); | |
451 | } | |
452 | ||
453 | long wxComboBox::GetInsertionPoint() const | |
454 | { | |
455 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
456 | return (long) GTK_EDITABLE(entry)->current_pos; | |
457 | } | |
458 | ||
459 | long wxComboBox::GetLastPosition() const | |
460 | { | |
461 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
462 | int pos = GTK_ENTRY(entry)->text_length; | |
463 | return (long) pos-1; | |
464 | } | |
465 | ||
466 | void wxComboBox::Replace( long from, long to, const wxString& value ) | |
467 | { | |
468 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
469 | ||
470 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
471 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
472 | if (value.IsNull()) return; | |
473 | gint pos = (gint)to; | |
474 | gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos ); | |
475 | } | |
476 | ||
477 | void wxComboBox::Remove(long from, long to) | |
478 | { | |
479 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
480 | ||
481 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
482 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
483 | } | |
484 | ||
485 | void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) ) | |
486 | { | |
487 | wxFAIL_MSG( "wxComboBox::SetSelection not implemented" ); | |
488 | } | |
489 | ||
490 | void wxComboBox::SetEditable( bool WXUNUSED(editable) ) | |
491 | { | |
492 | wxFAIL_MSG( "wxComboBox::SetEditable not implemented" ); | |
493 | } | |
494 | ||
495 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
496 | { | |
497 | wxControl::OnSize( event ); | |
498 | ||
499 | int w = 21; | |
500 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
501 | ||
502 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); | |
503 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
504 | } | |
505 | ||
506 | void wxComboBox::ApplyWidgetStyle() | |
507 | { | |
508 | SetWidgetStyle(); | |
509 | ||
510 | gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); | |
511 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); | |
512 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); | |
513 | ||
514 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); | |
515 | GList *child = list->children; | |
516 | while (child) | |
517 | { | |
518 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); | |
519 | ||
520 | GtkBin *bin = GTK_BIN(child->data); | |
521 | gtk_widget_set_style( bin->child, m_widgetStyle ); | |
522 | ||
523 | child = child->next; | |
524 | } | |
525 | } | |
526 | ||
527 | GtkWidget* wxComboBox::GetConnectWidget() | |
528 | { | |
529 | return GTK_COMBO(m_widget)->entry; | |
530 | } | |
531 | ||
532 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) | |
533 | { | |
534 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
535 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
536 | } |