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