]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
Prealpha, prebeta of new wxTreeCtrl for GTK. It is possible to AddRoot,
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
53010e52
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "combobox.h"
13#endif
14
15#include "wx/combobox.h"
1a5a8367 16#include <wx/intl.h>
53010e52 17
47908e25
RR
18//-----------------------------------------------------------------------------
19// data
20//-----------------------------------------------------------------------------
21
22extern bool g_blockEventsOnDrag;
23
53010e52 24//-----------------------------------------------------------------------------
e1e955e1 25// "select"
47908e25 26//-----------------------------------------------------------------------------
47908e25
RR
27
28static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 29{
66bd6b93
RR
30 if (!combo->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
47908e25
RR
33 if (combo->m_alreadySent)
34 {
35 combo->m_alreadySent = FALSE;
36 return;
37 }
38
39 combo->m_alreadySent = TRUE;
40
53010e52
RR
41 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
42 event.SetInt( combo->GetSelection() );
43 wxString tmp( combo->GetStringSelection() );
44 event.SetString( WXSTRINGCAST(tmp) );
45 event.SetEventObject(combo);
47908e25 46 combo->GetEventHandler()->ProcessEvent(event);
6de97a3b 47}
47908e25 48
e1e955e1
RR
49//-----------------------------------------------------------------------------
50// wxComboBox
53010e52
RR
51//-----------------------------------------------------------------------------
52
53IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
54
b4071e91
RR
55BEGIN_EVENT_TABLE(wxComboBox, wxControl)
56 EVT_SIZE(wxComboBox::OnSize)
57END_EVENT_TABLE()
58
debe6624 59bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
53010e52 60 const wxPoint& pos, const wxSize& size,
debe6624 61 int n, const wxString choices[],
6de97a3b 62 long style, const wxValidator& validator, const wxString& name )
53010e52 63{
47908e25 64 m_alreadySent = FALSE;
53010e52
RR
65 m_needParent = TRUE;
66
67 PreCreation( parent, id, pos, size, style, name );
68
6de97a3b
RR
69 SetValidator( validator );
70
53010e52
RR
71 m_widget = gtk_combo_new();
72
73 wxSize newSize = size;
74 if (newSize.x == -1) newSize.x = 100;
75 if (newSize.y == -1) newSize.y = 26;
76 SetSize( newSize.x, newSize.y );
77
78 GtkWidget *list = GTK_COMBO(m_widget)->list;
79
80 for (int i = 0; i < n; i++)
81 {
82 GtkWidget *list_item;
83 list_item = gtk_list_item_new_with_label( choices[i] );
84
53010e52
RR
85 gtk_container_add( GTK_CONTAINER(list), list_item );
86
47908e25
RR
87 m_clientData.Append( (wxObject*)NULL );
88
53010e52 89 gtk_widget_show( list_item );
66bd6b93
RR
90
91 gtk_signal_connect( GTK_OBJECT(list_item), "select",
92 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
6de97a3b 93 }
53010e52
RR
94
95 PostCreation();
96
b4071e91
RR
97 ConnectWidget( GTK_COMBO(m_widget)->button );
98
53010e52
RR
99 if (!value.IsNull()) SetValue( value );
100
101 Show( TRUE );
102
103 return TRUE;
6de97a3b 104}
53010e52
RR
105
106void wxComboBox::Clear(void)
107{
108 GtkWidget *list = GTK_COMBO(m_widget)->list;
109 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
47908e25
RR
110
111 m_clientData.Clear();
6de97a3b 112}
53010e52
RR
113
114void wxComboBox::Append( const wxString &item )
47908e25
RR
115{
116 Append( item, (char*)NULL );
6de97a3b 117}
47908e25
RR
118
119void wxComboBox::Append( const wxString &item, char *clientData )
53010e52
RR
120{
121 GtkWidget *list = GTK_COMBO(m_widget)->list;
122
868a2826
RR
123 GtkWidget *list_item = gtk_list_item_new_with_label( item );
124
125 if (m_hasOwnStyle)
126 {
127 GtkBin *bin = GTK_BIN( list_item );
128 gtk_widget_set_style( bin->child,
129 gtk_style_ref(
130 gtk_widget_get_style( m_widget ) ) );
131 }
53010e52 132
fc54776e
RR
133 if (m_backgroundColour != wxNullColour)
134 {
135 GtkBin *bin = GTK_BIN( list_item );
136 SetBackgroundColourHelper( bin->child->window );
137 }
138
47908e25 139 gtk_signal_connect( GTK_OBJECT(list_item), "select",
53010e52
RR
140 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
141
2f6407b9
RR
142 m_clientData.Append( (wxObject*)clientData );
143
53010e52
RR
144 gtk_container_add( GTK_CONTAINER(list), list_item );
145
146 gtk_widget_show( list_item );
6de97a3b 147}
53010e52 148
debe6624 149void wxComboBox::Delete( int n )
53010e52 150{
2f6407b9
RR
151 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
152
153 GList *child = g_list_nth( listbox->children, n );
154
155 if (!child)
156 {
157 wxFAIL_MSG("wrong index");
158 return;
159 }
160
161 GList *list = g_list_append( NULL, child->data );
162 gtk_list_remove_items( listbox, list );
163 g_list_free( list );
47908e25
RR
164
165 wxNode *node = m_clientData.Nth( n );
166 if (!node)
167 {
2f6407b9 168 wxFAIL_MSG( "wrong index" );
47908e25
RR
169 }
170 else
171 m_clientData.DeleteNode( node );
6de97a3b 172}
53010e52
RR
173
174int wxComboBox::FindString( const wxString &item )
175{
176 GtkWidget *list = GTK_COMBO(m_widget)->list;
177
178 GList *child = GTK_LIST(list)->children;
179 int count = 0;
180 while (child)
181 {
182 GtkBin *bin = GTK_BIN( child->data );
183 GtkLabel *label = GTK_LABEL( bin->child );
184 if (item == label->label) return count;
185 count++;
186 child = child->next;
6de97a3b 187 }
b6af8d80
RR
188
189 wxFAIL_MSG( "wxComboBox: string not found" );
190
53010e52 191 return -1;
6de97a3b 192}
53010e52 193
debe6624 194char* wxComboBox::GetClientData( int n )
53010e52
RR
195{
196 wxNode *node = m_clientData.Nth( n );
197 if (node) return (char*)node->Data();
b6af8d80
RR
198
199 wxFAIL_MSG( "wxComboBox: wrong index" );
200
c67daf87 201 return (char *) NULL;
6de97a3b 202}
53010e52 203
debe6624 204void wxComboBox::SetClientData( int n, char * clientData )
53010e52
RR
205{
206 wxNode *node = m_clientData.Nth( n );
207 if (node) node->SetData( (wxObject*) clientData );
b6af8d80
RR
208
209 wxFAIL_MSG( "wxComboBox: wrong index" );
6de97a3b 210}
53010e52
RR
211
212int wxComboBox::GetSelection(void) const
213{
214 GtkWidget *list = GTK_COMBO(m_widget)->list;
215
216 GList *selection = GTK_LIST(list)->selection;
217 if (selection)
218 {
219 GList *child = GTK_LIST(list)->children;
220 int count = 0;
221 while (child)
222 {
223 if (child->data == selection->data) return count;
224 count++;
225 child = child->next;
6de97a3b
RR
226 }
227 }
b6af8d80
RR
228
229 wxFAIL_MSG( "wxComboBox: no selection" );
230
53010e52 231 return -1;
6de97a3b 232}
53010e52 233
debe6624 234wxString wxComboBox::GetString( int n ) const
53010e52
RR
235{
236 GtkWidget *list = GTK_COMBO(m_widget)->list;
237
238 GList *child = g_list_nth( GTK_LIST(list)->children, n );
239 if (child)
240 {
241 GtkBin *bin = GTK_BIN( child->data );
242 GtkLabel *label = GTK_LABEL( bin->child );
243 return label->label;
6de97a3b 244 }
b6af8d80
RR
245
246 wxFAIL_MSG( "wxComboBox: wrong index" );
247
53010e52 248 return "";
6de97a3b 249}
53010e52
RR
250
251wxString wxComboBox::GetStringSelection(void) const
252{
253 GtkWidget *list = GTK_COMBO(m_widget)->list;
254
255 GList *selection = GTK_LIST(list)->selection;
256 if (selection)
257 {
258 GtkBin *bin = GTK_BIN( selection->data );
259 wxString tmp = GTK_LABEL( bin->child )->label;
260 return tmp;
6de97a3b 261 }
b6af8d80
RR
262
263 wxFAIL_MSG( "wxComboBox: no selection" );
264
53010e52 265 return "";
6de97a3b 266}
53010e52
RR
267
268int wxComboBox::Number(void) const
269{
270 GtkWidget *list = GTK_COMBO(m_widget)->list;
271
272 GList *child = GTK_LIST(list)->children;
273 int count = 0;
6de97a3b 274 while (child) { count++; child = child->next; }
53010e52 275 return count;
6de97a3b 276}
53010e52 277
debe6624 278void wxComboBox::SetSelection( int n )
53010e52
RR
279{
280 GtkWidget *list = GTK_COMBO(m_widget)->list;
281 gtk_list_select_item( GTK_LIST(list), n );
6de97a3b 282}
53010e52 283
47908e25
RR
284void wxComboBox::SetStringSelection( const wxString &string )
285{
286 int res = FindString( string );
287 if (res == -1) return;
288 SetSelection( res );
6de97a3b 289}
47908e25 290
53010e52
RR
291wxString wxComboBox::GetValue(void) const
292{
293 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
294 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
295 return tmp;
6de97a3b 296}
53010e52
RR
297
298void wxComboBox::SetValue( const wxString& value )
299{
300 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
301 wxString tmp = "";
302 if (!value.IsNull()) tmp = value;
303 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
6de97a3b 304}
53010e52
RR
305
306void wxComboBox::Copy(void)
307{
308 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15
GL
309#if (GTK_MINOR_VERSION == 1)
310 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
311#else
53010e52 312 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 313#endif
6de97a3b 314}
53010e52
RR
315
316void wxComboBox::Cut(void)
317{
318 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15
GL
319#if (GTK_MINOR_VERSION == 1)
320 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
321#else
53010e52 322 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 323#endif
6de97a3b 324}
53010e52
RR
325
326void wxComboBox::Paste(void)
327{
328 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15
GL
329#if (GTK_MINOR_VERSION == 1)
330 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
331#else
53010e52 332 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 333#endif
6de97a3b 334}
53010e52 335
debe6624 336void wxComboBox::SetInsertionPoint( long pos )
53010e52
RR
337{
338 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
339 int tmp = (int) pos;
340 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
6de97a3b 341}
53010e52
RR
342
343void wxComboBox::SetInsertionPointEnd(void)
344{
345 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
346 int pos = GTK_ENTRY(entry)->text_length;
347 SetInsertionPoint( pos-1 );
6de97a3b 348}
53010e52
RR
349
350long wxComboBox::GetInsertionPoint(void) const
351{
352 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
353 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 354}
53010e52
RR
355
356long wxComboBox::GetLastPosition(void) const
357{
358 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
359 int pos = GTK_ENTRY(entry)->text_length;
360 return (long) pos-1;
6de97a3b 361}
53010e52 362
debe6624 363void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52
RR
364{
365 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
366 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
367 if (value.IsNull()) return;
368 gint pos = (gint)to;
369 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
6de97a3b 370}
53010e52 371
debe6624 372void wxComboBox::Remove(long from, long to)
53010e52
RR
373{
374 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
375 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 376}
53010e52 377
debe6624 378void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52 379{
b4071e91 380 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
6de97a3b 381}
53010e52 382
debe6624 383void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52 384{
b4071e91
RR
385 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
386}
387
388void wxComboBox::OnSize( wxSizeEvent &event )
389{
390 wxControl::OnSize( event );
391
392 int w = 22;
393
394 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
395
396 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
397 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
6de97a3b 398}
53010e52 399
868a2826
RR
400void wxComboBox::SetFont( const wxFont &font )
401{
402 wxWindow::SetFont( font );
403
404 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
405
406 gtk_widget_set_style( entry,
407 gtk_style_ref(
408 gtk_widget_get_style( m_widget ) ) );
409
410 GtkWidget *list = GTK_COMBO(m_widget)->list;
411
412 GList *child = GTK_LIST(list)->children;
413 while (child)
414 {
415 GtkBin *bin = (GtkBin*) child->data;
416 gtk_widget_set_style( bin->child,
417 gtk_style_ref(
418 gtk_widget_get_style( m_widget ) ) );
419
420 child = child->next;
421 }
422}
b4071e91 423
97b3455a
RR
424GtkWidget* wxComboBox::GetConnectWidget(void)
425{
426 return GTK_COMBO(m_widget)->entry;
427}
428
b4071e91
RR
429bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
430{
431 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
432 (window == GTK_COMBO(m_widget)->button->window ) );
433}
97b3455a 434
fc54776e
RR
435void wxComboBox::SetBackgroundColour( const wxColour &colour )
436{
437 wxWindow::SetBackgroundColour( colour );
438
439 GtkWidget *list = GTK_COMBO(m_widget)->list;
440
441 GList *child = GTK_LIST(list)->children;
442 while (child)
443 {
444 GtkBin *bin = (GtkBin*) child->data;
445 SetBackgroundColourHelper( bin->child->window );
446 child = child->next;
447 }
448}
449