]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
1) corrected bug in log.cpp which only affected wxGTK: some messages could
[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 // wxComboBox
26 //-----------------------------------------------------------------------------
27
28 //-----------------------------------------------------------------------------
29 // clicked
30
31 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
32 {
33 if (!combo->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
36 if (combo->m_alreadySent)
37 {
38 combo->m_alreadySent = FALSE;
39 return;
40 }
41
42 combo->m_alreadySent = TRUE;
43
44 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
45 event.SetInt( combo->GetSelection() );
46 wxString tmp( combo->GetStringSelection() );
47 event.SetString( WXSTRINGCAST(tmp) );
48 event.SetEventObject(combo);
49 combo->GetEventHandler()->ProcessEvent(event);
50 }
51
52 //-----------------------------------------------------------------------------
53 // size
54
55 /*
56 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
57 {
58 if (!win->HasVMT()) return FALSE;
59 if (g_blockEventsOnDrag) return FALSE;
60 if (!widget->button) return FALSE;
61
62 widget->button->allocation.x =
63 alloc->width - widget->button->allocation.width;
64
65 return FALSE;
66 }
67 */
68
69 //-----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
72
73 bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
74 const wxPoint& pos, const wxSize& size,
75 int n, const wxString choices[],
76 long style, const wxValidator& validator, const wxString& name )
77 {
78 m_alreadySent = FALSE;
79 m_needParent = TRUE;
80
81 PreCreation( parent, id, pos, size, style, name );
82
83 SetValidator( validator );
84
85 m_widget = gtk_combo_new();
86
87 wxSize newSize = size;
88 if (newSize.x == -1) newSize.x = 100;
89 if (newSize.y == -1) newSize.y = 26;
90 SetSize( newSize.x, newSize.y );
91
92 GtkWidget *list = GTK_COMBO(m_widget)->list;
93
94 for (int i = 0; i < n; i++)
95 {
96 GtkWidget *list_item;
97 list_item = gtk_list_item_new_with_label( choices[i] );
98
99 gtk_container_add( GTK_CONTAINER(list), list_item );
100
101 m_clientData.Append( (wxObject*)NULL );
102
103 gtk_widget_show( list_item );
104
105 gtk_signal_connect( GTK_OBJECT(list_item), "select",
106 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
107 }
108
109 PostCreation();
110
111 /*
112 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
113 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
114 */
115
116 if (!value.IsNull()) SetValue( value );
117
118 Show( TRUE );
119
120 return TRUE;
121 }
122
123 void wxComboBox::Clear(void)
124 {
125 GtkWidget *list = GTK_COMBO(m_widget)->list;
126 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
127
128 m_clientData.Clear();
129 }
130
131 void wxComboBox::Append( const wxString &item )
132 {
133 Append( item, (char*)NULL );
134 }
135
136 void wxComboBox::Append( const wxString &item, char *clientData )
137 {
138 GtkWidget *list = GTK_COMBO(m_widget)->list;
139
140 GtkWidget *list_item;
141 list_item = gtk_list_item_new_with_label( item );
142
143 gtk_signal_connect( GTK_OBJECT(list_item), "select",
144 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
145
146 gtk_container_add( GTK_CONTAINER(list), list_item );
147
148 gtk_widget_show( list_item );
149
150 m_clientData.Append( (wxObject*)clientData );
151 }
152
153 void wxComboBox::Delete( int n )
154 {
155 GtkWidget *list = GTK_COMBO(m_widget)->list;
156 gtk_list_clear_items( GTK_LIST(list), n, n );
157
158 wxNode *node = m_clientData.Nth( n );
159 if (!node)
160 {
161 wxFAIL_MSG( "wxComboBox: wrong index" );
162 }
163 else
164 m_clientData.DeleteNode( node );
165 }
166
167 int wxComboBox::FindString( const wxString &item )
168 {
169 GtkWidget *list = GTK_COMBO(m_widget)->list;
170
171 GList *child = GTK_LIST(list)->children;
172 int count = 0;
173 while (child)
174 {
175 GtkBin *bin = GTK_BIN( child->data );
176 GtkLabel *label = GTK_LABEL( bin->child );
177 if (item == label->label) return count;
178 count++;
179 child = child->next;
180 }
181
182 wxFAIL_MSG( "wxComboBox: string not found" );
183
184 return -1;
185 }
186
187 char* wxComboBox::GetClientData( int n )
188 {
189 wxNode *node = m_clientData.Nth( n );
190 if (node) return (char*)node->Data();
191
192 wxFAIL_MSG( "wxComboBox: wrong index" );
193
194 return NULL;
195 }
196
197 void wxComboBox::SetClientData( int n, char * clientData )
198 {
199 wxNode *node = m_clientData.Nth( n );
200 if (node) node->SetData( (wxObject*) clientData );
201
202 wxFAIL_MSG( "wxComboBox: wrong index" );
203 }
204
205 int wxComboBox::GetSelection(void) const
206 {
207 GtkWidget *list = GTK_COMBO(m_widget)->list;
208
209 GList *selection = GTK_LIST(list)->selection;
210 if (selection)
211 {
212 GList *child = GTK_LIST(list)->children;
213 int count = 0;
214 while (child)
215 {
216 if (child->data == selection->data) return count;
217 count++;
218 child = child->next;
219 }
220 }
221
222 wxFAIL_MSG( "wxComboBox: no selection" );
223
224 return -1;
225 }
226
227 wxString wxComboBox::GetString( int n ) const
228 {
229 GtkWidget *list = GTK_COMBO(m_widget)->list;
230
231 GList *child = g_list_nth( GTK_LIST(list)->children, n );
232 if (child)
233 {
234 GtkBin *bin = GTK_BIN( child->data );
235 GtkLabel *label = GTK_LABEL( bin->child );
236 return label->label;
237 }
238
239 wxFAIL_MSG( "wxComboBox: wrong index" );
240
241 return "";
242 }
243
244 wxString wxComboBox::GetStringSelection(void) const
245 {
246 GtkWidget *list = GTK_COMBO(m_widget)->list;
247
248 GList *selection = GTK_LIST(list)->selection;
249 if (selection)
250 {
251 GtkBin *bin = GTK_BIN( selection->data );
252 wxString tmp = GTK_LABEL( bin->child )->label;
253 return tmp;
254 }
255
256 wxFAIL_MSG( "wxComboBox: no selection" );
257
258 return "";
259 }
260
261 int wxComboBox::Number(void) const
262 {
263 GtkWidget *list = GTK_COMBO(m_widget)->list;
264
265 GList *child = GTK_LIST(list)->children;
266 int count = 0;
267 while (child) { count++; child = child->next; }
268 return count;
269 }
270
271 void wxComboBox::SetSelection( int n )
272 {
273 GtkWidget *list = GTK_COMBO(m_widget)->list;
274 gtk_list_select_item( GTK_LIST(list), n );
275 }
276
277 void wxComboBox::SetStringSelection( const wxString &string )
278 {
279 int res = FindString( string );
280 if (res == -1) return;
281 SetSelection( res );
282 }
283
284 wxString wxComboBox::GetValue(void) const
285 {
286 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
287 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
288 return tmp;
289 }
290
291 void wxComboBox::SetValue( const wxString& value )
292 {
293 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
294 wxString tmp = "";
295 if (!value.IsNull()) tmp = value;
296 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
297 }
298
299 void wxComboBox::Copy(void)
300 {
301 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
302 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
303 }
304
305 void wxComboBox::Cut(void)
306 {
307 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
308 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
309 }
310
311 void wxComboBox::Paste(void)
312 {
313 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
314 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
315 }
316
317 void wxComboBox::SetInsertionPoint( long pos )
318 {
319 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
320 int tmp = (int) pos;
321 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
322 }
323
324 void wxComboBox::SetInsertionPointEnd(void)
325 {
326 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
327 int pos = GTK_ENTRY(entry)->text_length;
328 SetInsertionPoint( pos-1 );
329 }
330
331 long wxComboBox::GetInsertionPoint(void) const
332 {
333 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
334 return (long) GTK_EDITABLE(entry)->current_pos;
335 }
336
337 long wxComboBox::GetLastPosition(void) const
338 {
339 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
340 int pos = GTK_ENTRY(entry)->text_length;
341 return (long) pos-1;
342 }
343
344 void wxComboBox::Replace( long from, long to, const wxString& value )
345 {
346 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
347 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
348 if (value.IsNull()) return;
349 gint pos = (gint)to;
350 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
351 }
352
353 void wxComboBox::Remove(long from, long to)
354 {
355 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
356 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
357 }
358
359 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
360 {
361 }
362
363 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
364 {
365 }
366
367