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