]>
Commit | Line | Data |
---|---|---|
53010e52 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: combobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
01111366 RR |
5 | // Id: $id$ |
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 | { | |
81 | GtkWidget *list_item; | |
82 | list_item = gtk_list_item_new_with_label( choices[i] ); | |
83 | ||
53010e52 RR |
84 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
85 | ||
47908e25 RR |
86 | m_clientData.Append( (wxObject*)NULL ); |
87 | ||
53010e52 | 88 | gtk_widget_show( list_item ); |
66bd6b93 RR |
89 | |
90 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
91 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
6de97a3b | 92 | } |
53010e52 RR |
93 | |
94 | PostCreation(); | |
95 | ||
b4071e91 RR |
96 | ConnectWidget( GTK_COMBO(m_widget)->button ); |
97 | ||
53010e52 RR |
98 | if (!value.IsNull()) SetValue( value ); |
99 | ||
100 | Show( TRUE ); | |
101 | ||
102 | return TRUE; | |
6de97a3b | 103 | } |
53010e52 RR |
104 | |
105 | void wxComboBox::Clear(void) | |
106 | { | |
107 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
108 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
47908e25 RR |
109 | |
110 | m_clientData.Clear(); | |
6de97a3b | 111 | } |
53010e52 RR |
112 | |
113 | void wxComboBox::Append( const wxString &item ) | |
47908e25 RR |
114 | { |
115 | Append( item, (char*)NULL ); | |
6de97a3b | 116 | } |
47908e25 RR |
117 | |
118 | void wxComboBox::Append( const wxString &item, char *clientData ) | |
53010e52 RR |
119 | { |
120 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
121 | ||
868a2826 RR |
122 | GtkWidget *list_item = gtk_list_item_new_with_label( item ); |
123 | ||
124 | if (m_hasOwnStyle) | |
125 | { | |
126 | GtkBin *bin = GTK_BIN( list_item ); | |
127 | gtk_widget_set_style( bin->child, | |
128 | gtk_style_ref( | |
129 | gtk_widget_get_style( m_widget ) ) ); | |
130 | } | |
53010e52 | 131 | |
fc54776e RR |
132 | if (m_backgroundColour != wxNullColour) |
133 | { | |
134 | GtkBin *bin = GTK_BIN( list_item ); | |
135 | SetBackgroundColourHelper( bin->child->window ); | |
136 | } | |
137 | ||
47908e25 | 138 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
53010e52 RR |
139 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); |
140 | ||
2f6407b9 RR |
141 | m_clientData.Append( (wxObject*)clientData ); |
142 | ||
53010e52 RR |
143 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
144 | ||
145 | gtk_widget_show( list_item ); | |
6de97a3b | 146 | } |
53010e52 | 147 | |
debe6624 | 148 | void wxComboBox::Delete( int n ) |
53010e52 | 149 | { |
2f6407b9 RR |
150 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
151 | ||
152 | GList *child = g_list_nth( listbox->children, n ); | |
153 | ||
154 | if (!child) | |
155 | { | |
156 | wxFAIL_MSG("wrong index"); | |
157 | return; | |
158 | } | |
159 | ||
160 | GList *list = g_list_append( NULL, child->data ); | |
161 | gtk_list_remove_items( listbox, list ); | |
162 | g_list_free( list ); | |
47908e25 RR |
163 | |
164 | wxNode *node = m_clientData.Nth( n ); | |
165 | if (!node) | |
166 | { | |
2f6407b9 | 167 | wxFAIL_MSG( "wrong index" ); |
47908e25 RR |
168 | } |
169 | else | |
170 | m_clientData.DeleteNode( node ); | |
6de97a3b | 171 | } |
53010e52 RR |
172 | |
173 | int wxComboBox::FindString( const wxString &item ) | |
174 | { | |
175 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
176 | ||
177 | GList *child = GTK_LIST(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 | } |
b6af8d80 RR |
187 | |
188 | wxFAIL_MSG( "wxComboBox: string not found" ); | |
189 | ||
53010e52 | 190 | return -1; |
6de97a3b | 191 | } |
53010e52 | 192 | |
debe6624 | 193 | char* wxComboBox::GetClientData( int n ) |
53010e52 RR |
194 | { |
195 | wxNode *node = m_clientData.Nth( n ); | |
196 | if (node) return (char*)node->Data(); | |
b6af8d80 RR |
197 | |
198 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
199 | ||
c67daf87 | 200 | return (char *) NULL; |
6de97a3b | 201 | } |
53010e52 | 202 | |
debe6624 | 203 | void wxComboBox::SetClientData( int n, char * clientData ) |
53010e52 RR |
204 | { |
205 | wxNode *node = m_clientData.Nth( n ); | |
206 | if (node) node->SetData( (wxObject*) clientData ); | |
b6af8d80 RR |
207 | |
208 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
6de97a3b | 209 | } |
53010e52 RR |
210 | |
211 | int wxComboBox::GetSelection(void) const | |
212 | { | |
213 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
214 | ||
215 | GList *selection = GTK_LIST(list)->selection; | |
216 | if (selection) | |
217 | { | |
218 | GList *child = GTK_LIST(list)->children; | |
219 | int count = 0; | |
220 | while (child) | |
221 | { | |
222 | if (child->data == selection->data) return count; | |
223 | count++; | |
224 | child = child->next; | |
6de97a3b RR |
225 | } |
226 | } | |
b6af8d80 RR |
227 | |
228 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
229 | ||
53010e52 | 230 | return -1; |
6de97a3b | 231 | } |
53010e52 | 232 | |
debe6624 | 233 | wxString wxComboBox::GetString( int n ) const |
53010e52 RR |
234 | { |
235 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
236 | ||
237 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
238 | if (child) | |
239 | { | |
240 | GtkBin *bin = GTK_BIN( child->data ); | |
241 | GtkLabel *label = GTK_LABEL( bin->child ); | |
242 | return label->label; | |
6de97a3b | 243 | } |
b6af8d80 RR |
244 | |
245 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
246 | ||
53010e52 | 247 | return ""; |
6de97a3b | 248 | } |
53010e52 RR |
249 | |
250 | wxString wxComboBox::GetStringSelection(void) const | |
251 | { | |
252 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
253 | ||
254 | GList *selection = GTK_LIST(list)->selection; | |
255 | if (selection) | |
256 | { | |
257 | GtkBin *bin = GTK_BIN( selection->data ); | |
258 | wxString tmp = GTK_LABEL( bin->child )->label; | |
259 | return tmp; | |
6de97a3b | 260 | } |
b6af8d80 RR |
261 | |
262 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
263 | ||
53010e52 | 264 | return ""; |
6de97a3b | 265 | } |
53010e52 RR |
266 | |
267 | int wxComboBox::Number(void) const | |
268 | { | |
269 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
270 | ||
271 | GList *child = GTK_LIST(list)->children; | |
272 | int count = 0; | |
6de97a3b | 273 | while (child) { count++; child = child->next; } |
53010e52 | 274 | return count; |
6de97a3b | 275 | } |
53010e52 | 276 | |
debe6624 | 277 | void wxComboBox::SetSelection( int n ) |
53010e52 RR |
278 | { |
279 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
280 | gtk_list_select_item( GTK_LIST(list), n ); | |
6de97a3b | 281 | } |
53010e52 | 282 | |
47908e25 RR |
283 | void wxComboBox::SetStringSelection( const wxString &string ) |
284 | { | |
285 | int res = FindString( string ); | |
286 | if (res == -1) return; | |
287 | SetSelection( res ); | |
6de97a3b | 288 | } |
47908e25 | 289 | |
53010e52 RR |
290 | wxString wxComboBox::GetValue(void) const |
291 | { | |
292 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
293 | wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) ); | |
294 | return tmp; | |
6de97a3b | 295 | } |
53010e52 RR |
296 | |
297 | void wxComboBox::SetValue( const wxString& value ) | |
298 | { | |
299 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
300 | wxString tmp = ""; | |
301 | if (!value.IsNull()) tmp = value; | |
302 | gtk_entry_set_text( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 303 | } |
53010e52 RR |
304 | |
305 | void wxComboBox::Copy(void) | |
306 | { | |
307 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
75ed1d15 GL |
308 | #if (GTK_MINOR_VERSION == 1) |
309 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) ); | |
310 | #else | |
53010e52 | 311 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); |
75ed1d15 | 312 | #endif |
6de97a3b | 313 | } |
53010e52 RR |
314 | |
315 | void wxComboBox::Cut(void) | |
316 | { | |
317 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
75ed1d15 GL |
318 | #if (GTK_MINOR_VERSION == 1) |
319 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) ); | |
320 | #else | |
53010e52 | 321 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); |
75ed1d15 | 322 | #endif |
6de97a3b | 323 | } |
53010e52 RR |
324 | |
325 | void wxComboBox::Paste(void) | |
326 | { | |
327 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
75ed1d15 GL |
328 | #if (GTK_MINOR_VERSION == 1) |
329 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) ); | |
330 | #else | |
53010e52 | 331 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); |
75ed1d15 | 332 | #endif |
6de97a3b | 333 | } |
53010e52 | 334 | |
debe6624 | 335 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 RR |
336 | { |
337 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
338 | int tmp = (int) pos; | |
339 | gtk_entry_set_position( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 340 | } |
53010e52 RR |
341 | |
342 | void wxComboBox::SetInsertionPointEnd(void) | |
343 | { | |
344 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
345 | int pos = GTK_ENTRY(entry)->text_length; | |
346 | SetInsertionPoint( pos-1 ); | |
6de97a3b | 347 | } |
53010e52 RR |
348 | |
349 | long wxComboBox::GetInsertionPoint(void) const | |
350 | { | |
351 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
352 | return (long) GTK_EDITABLE(entry)->current_pos; | |
6de97a3b | 353 | } |
53010e52 RR |
354 | |
355 | long wxComboBox::GetLastPosition(void) const | |
356 | { | |
357 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
358 | int pos = GTK_ENTRY(entry)->text_length; | |
359 | return (long) pos-1; | |
6de97a3b | 360 | } |
53010e52 | 361 | |
debe6624 | 362 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 RR |
363 | { |
364 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
365 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
366 | if (value.IsNull()) return; | |
367 | gint pos = (gint)to; | |
368 | gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos ); | |
6de97a3b | 369 | } |
53010e52 | 370 | |
debe6624 | 371 | void wxComboBox::Remove(long from, long to) |
53010e52 RR |
372 | { |
373 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
374 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 375 | } |
53010e52 | 376 | |
debe6624 | 377 | void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) ) |
53010e52 | 378 | { |
b4071e91 | 379 | wxFAIL_MSG( "wxComboBox::SetSelection not implemented" ); |
6de97a3b | 380 | } |
53010e52 | 381 | |
debe6624 | 382 | void wxComboBox::SetEditable( bool WXUNUSED(editable) ) |
53010e52 | 383 | { |
b4071e91 RR |
384 | wxFAIL_MSG( "wxComboBox::SetEditable not implemented" ); |
385 | } | |
386 | ||
387 | void wxComboBox::OnSize( wxSizeEvent &event ) | |
388 | { | |
389 | wxControl::OnSize( event ); | |
390 | ||
391 | int w = 22; | |
392 | ||
393 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); | |
394 | ||
395 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); | |
396 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); | |
6de97a3b | 397 | } |
53010e52 | 398 | |
868a2826 RR |
399 | void wxComboBox::SetFont( const wxFont &font ) |
400 | { | |
401 | wxWindow::SetFont( font ); | |
402 | ||
403 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
404 | ||
405 | gtk_widget_set_style( entry, | |
406 | gtk_style_ref( | |
407 | gtk_widget_get_style( m_widget ) ) ); | |
408 | ||
409 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
410 | ||
411 | GList *child = GTK_LIST(list)->children; | |
412 | while (child) | |
413 | { | |
414 | GtkBin *bin = (GtkBin*) child->data; | |
415 | gtk_widget_set_style( bin->child, | |
416 | gtk_style_ref( | |
417 | gtk_widget_get_style( m_widget ) ) ); | |
418 | ||
419 | child = child->next; | |
420 | } | |
421 | } | |
b4071e91 | 422 | |
97b3455a RR |
423 | GtkWidget* wxComboBox::GetConnectWidget(void) |
424 | { | |
425 | return GTK_COMBO(m_widget)->entry; | |
426 | } | |
427 | ||
b4071e91 RR |
428 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
429 | { | |
430 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || | |
431 | (window == GTK_COMBO(m_widget)->button->window ) ); | |
432 | } | |
97b3455a | 433 | |
fc54776e RR |
434 | void wxComboBox::SetBackgroundColour( const wxColour &colour ) |
435 | { | |
436 | wxWindow::SetBackgroundColour( colour ); | |
437 | ||
438 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
439 | ||
440 | GList *child = GTK_LIST(list)->children; | |
441 | while (child) | |
442 | { | |
443 | GtkBin *bin = (GtkBin*) child->data; | |
444 | SetBackgroundColourHelper( bin->child->window ); | |
445 | child = child->next; | |
446 | } | |
447 | } | |
448 |