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