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