]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
SetBackground for ListBox and others
[wxWidgets.git] / src / gtk / combobox.cpp
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"
16 #include <wx/intl.h>
17
18 //-----------------------------------------------------------------------------
19 // data
20 //-----------------------------------------------------------------------------
21
22 extern bool g_blockEventsOnDrag;
23
24 //-----------------------------------------------------------------------------
25 // "select"
26 //-----------------------------------------------------------------------------
27
28 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
29 {
30 if (!combo->HasVMT()) return;
31 if (g_blockEventsOnDrag) return;
32
33 if (combo->m_alreadySent)
34 {
35 combo->m_alreadySent = FALSE;
36 return;
37 }
38
39 combo->m_alreadySent = TRUE;
40
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);
46 combo->GetEventHandler()->ProcessEvent(event);
47 }
48
49 //-----------------------------------------------------------------------------
50 // wxComboBox
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
54
55 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
56 EVT_SIZE(wxComboBox::OnSize)
57 END_EVENT_TABLE()
58
59 bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
60 const wxPoint& pos, const wxSize& size,
61 int n, const wxString choices[],
62 long style, const wxValidator& validator, const wxString& name )
63 {
64 m_alreadySent = FALSE;
65 m_needParent = TRUE;
66
67 PreCreation( parent, id, pos, size, style, name );
68
69 SetValidator( validator );
70
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
85 gtk_container_add( GTK_CONTAINER(list), list_item );
86
87 m_clientData.Append( (wxObject*)NULL );
88
89 gtk_widget_show( list_item );
90
91 gtk_signal_connect( GTK_OBJECT(list_item), "select",
92 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
93 }
94
95 PostCreation();
96
97 ConnectWidget( GTK_COMBO(m_widget)->button );
98
99 if (!value.IsNull()) SetValue( value );
100
101 Show( TRUE );
102
103 return TRUE;
104 }
105
106 void wxComboBox::Clear(void)
107 {
108 GtkWidget *list = GTK_COMBO(m_widget)->list;
109 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
110
111 m_clientData.Clear();
112 }
113
114 void wxComboBox::Append( const wxString &item )
115 {
116 Append( item, (char*)NULL );
117 }
118
119 void wxComboBox::Append( const wxString &item, char *clientData )
120 {
121 GtkWidget *list = GTK_COMBO(m_widget)->list;
122
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 }
132
133 if (m_backgroundColour != wxNullColour)
134 {
135 GtkBin *bin = GTK_BIN( list_item );
136 SetBackgroundColourHelper( bin->child->window );
137 }
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 gtk_widget_show( list_item );
147 }
148
149 void wxComboBox::Delete( int n )
150 {
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 );
164
165 wxNode *node = m_clientData.Nth( n );
166 if (!node)
167 {
168 wxFAIL_MSG( "wrong index" );
169 }
170 else
171 m_clientData.DeleteNode( node );
172 }
173
174 int 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;
187 }
188
189 wxFAIL_MSG( "wxComboBox: string not found" );
190
191 return -1;
192 }
193
194 char* wxComboBox::GetClientData( int n )
195 {
196 wxNode *node = m_clientData.Nth( n );
197 if (node) return (char*)node->Data();
198
199 wxFAIL_MSG( "wxComboBox: wrong index" );
200
201 return (char *) NULL;
202 }
203
204 void wxComboBox::SetClientData( int n, char * clientData )
205 {
206 wxNode *node = m_clientData.Nth( n );
207 if (node) node->SetData( (wxObject*) clientData );
208
209 wxFAIL_MSG( "wxComboBox: wrong index" );
210 }
211
212 int 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;
226 }
227 }
228
229 wxFAIL_MSG( "wxComboBox: no selection" );
230
231 return -1;
232 }
233
234 wxString wxComboBox::GetString( int n ) const
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;
244 }
245
246 wxFAIL_MSG( "wxComboBox: wrong index" );
247
248 return "";
249 }
250
251 wxString 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;
261 }
262
263 wxFAIL_MSG( "wxComboBox: no selection" );
264
265 return "";
266 }
267
268 int 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;
274 while (child) { count++; child = child->next; }
275 return count;
276 }
277
278 void wxComboBox::SetSelection( int n )
279 {
280 GtkWidget *list = GTK_COMBO(m_widget)->list;
281 gtk_list_select_item( GTK_LIST(list), n );
282 }
283
284 void wxComboBox::SetStringSelection( const wxString &string )
285 {
286 int res = FindString( string );
287 if (res == -1) return;
288 SetSelection( res );
289 }
290
291 wxString 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;
296 }
297
298 void 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 );
304 }
305
306 void wxComboBox::Copy(void)
307 {
308 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
309 #if (GTK_MINOR_VERSION == 1)
310 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
311 #else
312 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
313 #endif
314 }
315
316 void wxComboBox::Cut(void)
317 {
318 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
319 #if (GTK_MINOR_VERSION == 1)
320 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
321 #else
322 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
323 #endif
324 }
325
326 void wxComboBox::Paste(void)
327 {
328 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
329 #if (GTK_MINOR_VERSION == 1)
330 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
331 #else
332 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
333 #endif
334 }
335
336 void wxComboBox::SetInsertionPoint( long pos )
337 {
338 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
339 int tmp = (int) pos;
340 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
341 }
342
343 void wxComboBox::SetInsertionPointEnd(void)
344 {
345 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
346 int pos = GTK_ENTRY(entry)->text_length;
347 SetInsertionPoint( pos-1 );
348 }
349
350 long wxComboBox::GetInsertionPoint(void) const
351 {
352 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
353 return (long) GTK_EDITABLE(entry)->current_pos;
354 }
355
356 long 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;
361 }
362
363 void wxComboBox::Replace( long from, long to, const wxString& value )
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 );
370 }
371
372 void wxComboBox::Remove(long from, long to)
373 {
374 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
375 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
376 }
377
378 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
379 {
380 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
381 }
382
383 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
384 {
385 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
386 }
387
388 void 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 );
398 }
399
400 void 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 }
423
424 GtkWidget* wxComboBox::GetConnectWidget(void)
425 {
426 return GTK_COMBO(m_widget)->entry;
427 }
428
429 bool 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 }
434
435 void 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