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