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