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