]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
no message
[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
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;
75ed1d15
GL
303#if (GTK_MINOR_VERSION == 1)
304 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
305#else
53010e52 306 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 307#endif
6de97a3b 308}
53010e52
RR
309
310void wxComboBox::Cut(void)
311{
312 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15
GL
313#if (GTK_MINOR_VERSION == 1)
314 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
315#else
53010e52 316 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 317#endif
6de97a3b 318}
53010e52
RR
319
320void wxComboBox::Paste(void)
321{
322 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15
GL
323#if (GTK_MINOR_VERSION == 1)
324 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
325#else
53010e52 326 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 327#endif
6de97a3b 328}
53010e52 329
debe6624 330void wxComboBox::SetInsertionPoint( long pos )
53010e52
RR
331{
332 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
333 int tmp = (int) pos;
334 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
6de97a3b 335}
53010e52
RR
336
337void wxComboBox::SetInsertionPointEnd(void)
338{
339 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
340 int pos = GTK_ENTRY(entry)->text_length;
341 SetInsertionPoint( pos-1 );
6de97a3b 342}
53010e52
RR
343
344long wxComboBox::GetInsertionPoint(void) const
345{
346 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
347 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 348}
53010e52
RR
349
350long wxComboBox::GetLastPosition(void) const
351{
352 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
353 int pos = GTK_ENTRY(entry)->text_length;
354 return (long) pos-1;
6de97a3b 355}
53010e52 356
debe6624 357void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52
RR
358{
359 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
360 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
361 if (value.IsNull()) return;
362 gint pos = (gint)to;
363 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
6de97a3b 364}
53010e52 365
debe6624 366void wxComboBox::Remove(long from, long to)
53010e52
RR
367{
368 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
369 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 370}
53010e52 371
debe6624 372void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52 373{
b4071e91 374 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
6de97a3b 375}
53010e52 376
debe6624 377void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52 378{
b4071e91
RR
379 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
380}
381
382void wxComboBox::OnSize( wxSizeEvent &event )
383{
384 wxControl::OnSize( event );
385
386 int w = 22;
387
388 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
389
390 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
391 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
6de97a3b 392}
53010e52 393
868a2826
RR
394void wxComboBox::SetFont( const wxFont &font )
395{
396 wxWindow::SetFont( font );
397
398 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
399
400 gtk_widget_set_style( entry,
401 gtk_style_ref(
402 gtk_widget_get_style( m_widget ) ) );
403
404 GtkWidget *list = GTK_COMBO(m_widget)->list;
405
406 GList *child = GTK_LIST(list)->children;
407 while (child)
408 {
409 GtkBin *bin = (GtkBin*) child->data;
410 gtk_widget_set_style( bin->child,
411 gtk_style_ref(
412 gtk_widget_get_style( m_widget ) ) );
413
414 child = child->next;
415 }
416}
b4071e91 417
97b3455a
RR
418GtkWidget* wxComboBox::GetConnectWidget(void)
419{
420 return GTK_COMBO(m_widget)->entry;
421}
422
b4071e91
RR
423bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
424{
425 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
426 (window == GTK_COMBO(m_widget)->button->window ) );
427}
97b3455a 428