]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
Version 0.4 of wxPython for MSW.
[wxWidgets.git] / src / gtk1 / 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
47908e25 133 gtk_signal_connect( GTK_OBJECT(list_item), "select",
53010e52
RR
134 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
135
2f6407b9
RR
136 m_clientData.Append( (wxObject*)clientData );
137
53010e52
RR
138 gtk_container_add( GTK_CONTAINER(list), list_item );
139
140 gtk_widget_show( list_item );
6de97a3b 141}
53010e52 142
debe6624 143void wxComboBox::Delete( int n )
53010e52 144{
2f6407b9
RR
145 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
146
147 GList *child = g_list_nth( listbox->children, n );
148
149 if (!child)
150 {
151 wxFAIL_MSG("wrong index");
152 return;
153 }
154
155 GList *list = g_list_append( NULL, child->data );
156 gtk_list_remove_items( listbox, list );
157 g_list_free( list );
47908e25
RR
158
159 wxNode *node = m_clientData.Nth( n );
160 if (!node)
161 {
2f6407b9 162 wxFAIL_MSG( "wrong index" );
47908e25
RR
163 }
164 else
165 m_clientData.DeleteNode( node );
6de97a3b 166}
53010e52
RR
167
168int wxComboBox::FindString( const wxString &item )
169{
170 GtkWidget *list = GTK_COMBO(m_widget)->list;
171
172 GList *child = GTK_LIST(list)->children;
173 int count = 0;
174 while (child)
175 {
176 GtkBin *bin = GTK_BIN( child->data );
177 GtkLabel *label = GTK_LABEL( bin->child );
178 if (item == label->label) return count;
179 count++;
180 child = child->next;
6de97a3b 181 }
b6af8d80
RR
182
183 wxFAIL_MSG( "wxComboBox: string not found" );
184
53010e52 185 return -1;
6de97a3b 186}
53010e52 187
debe6624 188char* wxComboBox::GetClientData( int n )
53010e52
RR
189{
190 wxNode *node = m_clientData.Nth( n );
191 if (node) return (char*)node->Data();
b6af8d80
RR
192
193 wxFAIL_MSG( "wxComboBox: wrong index" );
194
c67daf87 195 return (char *) NULL;
6de97a3b 196}
53010e52 197
debe6624 198void wxComboBox::SetClientData( int n, char * clientData )
53010e52
RR
199{
200 wxNode *node = m_clientData.Nth( n );
201 if (node) node->SetData( (wxObject*) clientData );
b6af8d80
RR
202
203 wxFAIL_MSG( "wxComboBox: wrong index" );
6de97a3b 204}
53010e52
RR
205
206int wxComboBox::GetSelection(void) const
207{
208 GtkWidget *list = GTK_COMBO(m_widget)->list;
209
210 GList *selection = GTK_LIST(list)->selection;
211 if (selection)
212 {
213 GList *child = GTK_LIST(list)->children;
214 int count = 0;
215 while (child)
216 {
217 if (child->data == selection->data) return count;
218 count++;
219 child = child->next;
6de97a3b
RR
220 }
221 }
b6af8d80
RR
222
223 wxFAIL_MSG( "wxComboBox: no selection" );
224
53010e52 225 return -1;
6de97a3b 226}
53010e52 227
debe6624 228wxString wxComboBox::GetString( int n ) const
53010e52
RR
229{
230 GtkWidget *list = GTK_COMBO(m_widget)->list;
231
232 GList *child = g_list_nth( GTK_LIST(list)->children, n );
233 if (child)
234 {
235 GtkBin *bin = GTK_BIN( child->data );
236 GtkLabel *label = GTK_LABEL( bin->child );
237 return label->label;
6de97a3b 238 }
b6af8d80
RR
239
240 wxFAIL_MSG( "wxComboBox: wrong index" );
241
53010e52 242 return "";
6de97a3b 243}
53010e52
RR
244
245wxString wxComboBox::GetStringSelection(void) const
246{
247 GtkWidget *list = GTK_COMBO(m_widget)->list;
248
249 GList *selection = GTK_LIST(list)->selection;
250 if (selection)
251 {
252 GtkBin *bin = GTK_BIN( selection->data );
253 wxString tmp = GTK_LABEL( bin->child )->label;
254 return tmp;
6de97a3b 255 }
b6af8d80
RR
256
257 wxFAIL_MSG( "wxComboBox: no selection" );
258
53010e52 259 return "";
6de97a3b 260}
53010e52
RR
261
262int wxComboBox::Number(void) const
263{
264 GtkWidget *list = GTK_COMBO(m_widget)->list;
265
266 GList *child = GTK_LIST(list)->children;
267 int count = 0;
6de97a3b 268 while (child) { count++; child = child->next; }
53010e52 269 return count;
6de97a3b 270}
53010e52 271
debe6624 272void wxComboBox::SetSelection( int n )
53010e52
RR
273{
274 GtkWidget *list = GTK_COMBO(m_widget)->list;
275 gtk_list_select_item( GTK_LIST(list), n );
6de97a3b 276}
53010e52 277
47908e25
RR
278void wxComboBox::SetStringSelection( const wxString &string )
279{
280 int res = FindString( string );
281 if (res == -1) return;
282 SetSelection( res );
6de97a3b 283}
47908e25 284
53010e52
RR
285wxString wxComboBox::GetValue(void) const
286{
287 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
288 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
289 return tmp;
6de97a3b 290}
53010e52
RR
291
292void wxComboBox::SetValue( const wxString& value )
293{
294 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
295 wxString tmp = "";
296 if (!value.IsNull()) tmp = value;
297 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
6de97a3b 298}
53010e52
RR
299
300void wxComboBox::Copy(void)
301{
302 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
303 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 304}
53010e52
RR
305
306void wxComboBox::Cut(void)
307{
308 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
309 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 310}
53010e52
RR
311
312void wxComboBox::Paste(void)
313{
314 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
315 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
6de97a3b 316}
53010e52 317
debe6624 318void wxComboBox::SetInsertionPoint( long pos )
53010e52
RR
319{
320 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
321 int tmp = (int) pos;
322 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
6de97a3b 323}
53010e52
RR
324
325void wxComboBox::SetInsertionPointEnd(void)
326{
327 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
328 int pos = GTK_ENTRY(entry)->text_length;
329 SetInsertionPoint( pos-1 );
6de97a3b 330}
53010e52
RR
331
332long wxComboBox::GetInsertionPoint(void) const
333{
334 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
335 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 336}
53010e52
RR
337
338long wxComboBox::GetLastPosition(void) const
339{
340 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
341 int pos = GTK_ENTRY(entry)->text_length;
342 return (long) pos-1;
6de97a3b 343}
53010e52 344
debe6624 345void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52
RR
346{
347 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
348 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
349 if (value.IsNull()) return;
350 gint pos = (gint)to;
351 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
6de97a3b 352}
53010e52 353
debe6624 354void wxComboBox::Remove(long from, long to)
53010e52
RR
355{
356 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
357 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 358}
53010e52 359
debe6624 360void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52 361{
b4071e91 362 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
6de97a3b 363}
53010e52 364
debe6624 365void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52 366{
b4071e91
RR
367 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
368}
369
370void wxComboBox::OnSize( wxSizeEvent &event )
371{
372 wxControl::OnSize( event );
373
374 int w = 22;
375
376 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
377
378 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
379 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
6de97a3b 380}
53010e52 381
868a2826
RR
382void wxComboBox::SetFont( const wxFont &font )
383{
384 wxWindow::SetFont( font );
385
386 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
387
388 gtk_widget_set_style( entry,
389 gtk_style_ref(
390 gtk_widget_get_style( m_widget ) ) );
391
392 GtkWidget *list = GTK_COMBO(m_widget)->list;
393
394 GList *child = GTK_LIST(list)->children;
395 while (child)
396 {
397 GtkBin *bin = (GtkBin*) child->data;
398 gtk_widget_set_style( bin->child,
399 gtk_style_ref(
400 gtk_widget_get_style( m_widget ) ) );
401
402 child = child->next;
403 }
404}
b4071e91 405
97b3455a
RR
406GtkWidget* wxComboBox::GetConnectWidget(void)
407{
408 return GTK_COMBO(m_widget)->entry;
409}
410
b4071e91
RR
411bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
412{
413 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
414 (window == GTK_COMBO(m_widget)->button->window ) );
415}
97b3455a 416