]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Changed instal path for setup.h
[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 gtk_signal_connect( GTK_OBJECT(list_item), "select",
134 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
135
136 m_clientData.Append( (wxObject*)clientData );
137
138 gtk_container_add( GTK_CONTAINER(list), list_item );
139
140 gtk_widget_show( list_item );
141 }
142
143 void wxComboBox::Delete( int n )
144 {
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 );
158
159 wxNode *node = m_clientData.Nth( n );
160 if (!node)
161 {
162 wxFAIL_MSG( "wrong index" );
163 }
164 else
165 m_clientData.DeleteNode( node );
166 }
167
168 int 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;
181 }
182
183 wxFAIL_MSG( "wxComboBox: string not found" );
184
185 return -1;
186 }
187
188 char* wxComboBox::GetClientData( int n )
189 {
190 wxNode *node = m_clientData.Nth( n );
191 if (node) return (char*)node->Data();
192
193 wxFAIL_MSG( "wxComboBox: wrong index" );
194
195 return (char *) NULL;
196 }
197
198 void wxComboBox::SetClientData( int n, char * clientData )
199 {
200 wxNode *node = m_clientData.Nth( n );
201 if (node) node->SetData( (wxObject*) clientData );
202
203 wxFAIL_MSG( "wxComboBox: wrong index" );
204 }
205
206 int 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;
220 }
221 }
222
223 wxFAIL_MSG( "wxComboBox: no selection" );
224
225 return -1;
226 }
227
228 wxString wxComboBox::GetString( int n ) const
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;
238 }
239
240 wxFAIL_MSG( "wxComboBox: wrong index" );
241
242 return "";
243 }
244
245 wxString 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;
255 }
256
257 wxFAIL_MSG( "wxComboBox: no selection" );
258
259 return "";
260 }
261
262 int 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;
268 while (child) { count++; child = child->next; }
269 return count;
270 }
271
272 void wxComboBox::SetSelection( int n )
273 {
274 GtkWidget *list = GTK_COMBO(m_widget)->list;
275 gtk_list_select_item( GTK_LIST(list), n );
276 }
277
278 void wxComboBox::SetStringSelection( const wxString &string )
279 {
280 int res = FindString( string );
281 if (res == -1) return;
282 SetSelection( res );
283 }
284
285 wxString 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;
290 }
291
292 void 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 );
298 }
299
300 void wxComboBox::Copy(void)
301 {
302 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
303 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
304 }
305
306 void wxComboBox::Cut(void)
307 {
308 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
309 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
310 }
311
312 void wxComboBox::Paste(void)
313 {
314 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
315 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
316 }
317
318 void wxComboBox::SetInsertionPoint( long pos )
319 {
320 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
321 int tmp = (int) pos;
322 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
323 }
324
325 void wxComboBox::SetInsertionPointEnd(void)
326 {
327 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
328 int pos = GTK_ENTRY(entry)->text_length;
329 SetInsertionPoint( pos-1 );
330 }
331
332 long wxComboBox::GetInsertionPoint(void) const
333 {
334 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
335 return (long) GTK_EDITABLE(entry)->current_pos;
336 }
337
338 long 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;
343 }
344
345 void wxComboBox::Replace( long from, long to, const wxString& value )
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 );
352 }
353
354 void wxComboBox::Remove(long from, long to)
355 {
356 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
357 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
358 }
359
360 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
361 {
362 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
363 }
364
365 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
366 {
367 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
368 }
369
370 void 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 );
380 }
381
382 void 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 }
405
406 GtkWidget* wxComboBox::GetConnectWidget(void)
407 {
408 return GTK_COMBO(m_widget)->entry;
409 }
410
411 bool 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 }
416