]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | extern bool g_blockEventsOnDrag; | |
23 | ||
53010e52 | 24 | //----------------------------------------------------------------------------- |
e1e955e1 | 25 | // "select" |
47908e25 | 26 | //----------------------------------------------------------------------------- |
47908e25 RR |
27 | |
28 | static 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 RR |
48 | |
49 | //----------------------------------------------------------------------------- | |
50 | // size | |
51 | ||
52 | /* | |
53 | static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win ) | |
54 | { | |
55 | if (!win->HasVMT()) return FALSE; | |
56 | if (g_blockEventsOnDrag) return FALSE; | |
57 | if (!widget->button) return FALSE; | |
58 | ||
59 | widget->button->allocation.x = | |
60 | alloc->width - widget->button->allocation.width; | |
61 | ||
62 | return FALSE; | |
6de97a3b | 63 | } |
47908e25 | 64 | */ |
53010e52 | 65 | |
e1e955e1 RR |
66 | //----------------------------------------------------------------------------- |
67 | // wxComboBox | |
53010e52 RR |
68 | //----------------------------------------------------------------------------- |
69 | ||
70 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
71 | ||
debe6624 | 72 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value, |
53010e52 | 73 | const wxPoint& pos, const wxSize& size, |
debe6624 | 74 | int n, const wxString choices[], |
6de97a3b | 75 | long style, const wxValidator& validator, const wxString& name ) |
53010e52 | 76 | { |
47908e25 | 77 | m_alreadySent = FALSE; |
53010e52 RR |
78 | m_needParent = TRUE; |
79 | ||
80 | PreCreation( parent, id, pos, size, style, name ); | |
81 | ||
6de97a3b RR |
82 | SetValidator( validator ); |
83 | ||
53010e52 RR |
84 | m_widget = gtk_combo_new(); |
85 | ||
86 | wxSize newSize = size; | |
87 | if (newSize.x == -1) newSize.x = 100; | |
88 | if (newSize.y == -1) newSize.y = 26; | |
89 | SetSize( newSize.x, newSize.y ); | |
90 | ||
91 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
92 | ||
93 | for (int i = 0; i < n; i++) | |
94 | { | |
95 | GtkWidget *list_item; | |
96 | list_item = gtk_list_item_new_with_label( choices[i] ); | |
97 | ||
53010e52 RR |
98 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
99 | ||
47908e25 RR |
100 | m_clientData.Append( (wxObject*)NULL ); |
101 | ||
53010e52 | 102 | gtk_widget_show( list_item ); |
66bd6b93 RR |
103 | |
104 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
105 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
6de97a3b | 106 | } |
53010e52 RR |
107 | |
108 | PostCreation(); | |
109 | ||
47908e25 RR |
110 | /* |
111 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", | |
112 | GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this ); | |
113 | */ | |
114 | ||
53010e52 RR |
115 | if (!value.IsNull()) SetValue( value ); |
116 | ||
117 | Show( TRUE ); | |
118 | ||
119 | return TRUE; | |
6de97a3b | 120 | } |
53010e52 RR |
121 | |
122 | void wxComboBox::Clear(void) | |
123 | { | |
124 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
125 | gtk_list_clear_items( GTK_LIST(list), 0, Number() ); | |
47908e25 RR |
126 | |
127 | m_clientData.Clear(); | |
6de97a3b | 128 | } |
53010e52 RR |
129 | |
130 | void wxComboBox::Append( const wxString &item ) | |
47908e25 RR |
131 | { |
132 | Append( item, (char*)NULL ); | |
6de97a3b | 133 | } |
47908e25 RR |
134 | |
135 | void wxComboBox::Append( const wxString &item, char *clientData ) | |
53010e52 RR |
136 | { |
137 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
138 | ||
868a2826 RR |
139 | GtkWidget *list_item = gtk_list_item_new_with_label( item ); |
140 | ||
141 | if (m_hasOwnStyle) | |
142 | { | |
143 | GtkBin *bin = GTK_BIN( list_item ); | |
144 | gtk_widget_set_style( bin->child, | |
145 | gtk_style_ref( | |
146 | gtk_widget_get_style( m_widget ) ) ); | |
147 | } | |
53010e52 | 148 | |
47908e25 | 149 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
53010e52 RR |
150 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); |
151 | ||
2f6407b9 RR |
152 | m_clientData.Append( (wxObject*)clientData ); |
153 | ||
53010e52 RR |
154 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
155 | ||
156 | gtk_widget_show( list_item ); | |
6de97a3b | 157 | } |
53010e52 | 158 | |
debe6624 | 159 | void wxComboBox::Delete( int n ) |
53010e52 | 160 | { |
2f6407b9 RR |
161 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
162 | ||
163 | GList *child = g_list_nth( listbox->children, n ); | |
164 | ||
165 | if (!child) | |
166 | { | |
167 | wxFAIL_MSG("wrong index"); | |
168 | return; | |
169 | } | |
170 | ||
171 | GList *list = g_list_append( NULL, child->data ); | |
172 | gtk_list_remove_items( listbox, list ); | |
173 | g_list_free( list ); | |
47908e25 RR |
174 | |
175 | wxNode *node = m_clientData.Nth( n ); | |
176 | if (!node) | |
177 | { | |
2f6407b9 | 178 | wxFAIL_MSG( "wrong index" ); |
47908e25 RR |
179 | } |
180 | else | |
181 | m_clientData.DeleteNode( node ); | |
6de97a3b | 182 | } |
53010e52 RR |
183 | |
184 | int wxComboBox::FindString( const wxString &item ) | |
185 | { | |
186 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
187 | ||
188 | GList *child = GTK_LIST(list)->children; | |
189 | int count = 0; | |
190 | while (child) | |
191 | { | |
192 | GtkBin *bin = GTK_BIN( child->data ); | |
193 | GtkLabel *label = GTK_LABEL( bin->child ); | |
194 | if (item == label->label) return count; | |
195 | count++; | |
196 | child = child->next; | |
6de97a3b | 197 | } |
b6af8d80 RR |
198 | |
199 | wxFAIL_MSG( "wxComboBox: string not found" ); | |
200 | ||
53010e52 | 201 | return -1; |
6de97a3b | 202 | } |
53010e52 | 203 | |
debe6624 | 204 | char* wxComboBox::GetClientData( int n ) |
53010e52 RR |
205 | { |
206 | wxNode *node = m_clientData.Nth( n ); | |
207 | if (node) return (char*)node->Data(); | |
b6af8d80 RR |
208 | |
209 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
210 | ||
c67daf87 | 211 | return (char *) NULL; |
6de97a3b | 212 | } |
53010e52 | 213 | |
debe6624 | 214 | void wxComboBox::SetClientData( int n, char * clientData ) |
53010e52 RR |
215 | { |
216 | wxNode *node = m_clientData.Nth( n ); | |
217 | if (node) node->SetData( (wxObject*) clientData ); | |
b6af8d80 RR |
218 | |
219 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
6de97a3b | 220 | } |
53010e52 RR |
221 | |
222 | int wxComboBox::GetSelection(void) const | |
223 | { | |
224 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
225 | ||
226 | GList *selection = GTK_LIST(list)->selection; | |
227 | if (selection) | |
228 | { | |
229 | GList *child = GTK_LIST(list)->children; | |
230 | int count = 0; | |
231 | while (child) | |
232 | { | |
233 | if (child->data == selection->data) return count; | |
234 | count++; | |
235 | child = child->next; | |
6de97a3b RR |
236 | } |
237 | } | |
b6af8d80 RR |
238 | |
239 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
240 | ||
53010e52 | 241 | return -1; |
6de97a3b | 242 | } |
53010e52 | 243 | |
debe6624 | 244 | wxString wxComboBox::GetString( int n ) const |
53010e52 RR |
245 | { |
246 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
247 | ||
248 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
249 | if (child) | |
250 | { | |
251 | GtkBin *bin = GTK_BIN( child->data ); | |
252 | GtkLabel *label = GTK_LABEL( bin->child ); | |
253 | return label->label; | |
6de97a3b | 254 | } |
b6af8d80 RR |
255 | |
256 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
257 | ||
53010e52 | 258 | return ""; |
6de97a3b | 259 | } |
53010e52 RR |
260 | |
261 | wxString wxComboBox::GetStringSelection(void) const | |
262 | { | |
263 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
264 | ||
265 | GList *selection = GTK_LIST(list)->selection; | |
266 | if (selection) | |
267 | { | |
268 | GtkBin *bin = GTK_BIN( selection->data ); | |
269 | wxString tmp = GTK_LABEL( bin->child )->label; | |
270 | return tmp; | |
6de97a3b | 271 | } |
b6af8d80 RR |
272 | |
273 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
274 | ||
53010e52 | 275 | return ""; |
6de97a3b | 276 | } |
53010e52 RR |
277 | |
278 | int wxComboBox::Number(void) const | |
279 | { | |
280 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
281 | ||
282 | GList *child = GTK_LIST(list)->children; | |
283 | int count = 0; | |
6de97a3b | 284 | while (child) { count++; child = child->next; } |
53010e52 | 285 | return count; |
6de97a3b | 286 | } |
53010e52 | 287 | |
debe6624 | 288 | void wxComboBox::SetSelection( int n ) |
53010e52 RR |
289 | { |
290 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
291 | gtk_list_select_item( GTK_LIST(list), n ); | |
6de97a3b | 292 | } |
53010e52 | 293 | |
47908e25 RR |
294 | void wxComboBox::SetStringSelection( const wxString &string ) |
295 | { | |
296 | int res = FindString( string ); | |
297 | if (res == -1) return; | |
298 | SetSelection( res ); | |
6de97a3b | 299 | } |
47908e25 | 300 | |
53010e52 RR |
301 | wxString wxComboBox::GetValue(void) const |
302 | { | |
303 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
304 | wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) ); | |
305 | return tmp; | |
6de97a3b | 306 | } |
53010e52 RR |
307 | |
308 | void wxComboBox::SetValue( const wxString& value ) | |
309 | { | |
310 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
311 | wxString tmp = ""; | |
312 | if (!value.IsNull()) tmp = value; | |
313 | gtk_entry_set_text( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 314 | } |
53010e52 RR |
315 | |
316 | void wxComboBox::Copy(void) | |
317 | { | |
318 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
319 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 320 | } |
53010e52 RR |
321 | |
322 | void wxComboBox::Cut(void) | |
323 | { | |
324 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
325 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 326 | } |
53010e52 RR |
327 | |
328 | void wxComboBox::Paste(void) | |
329 | { | |
330 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
331 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 332 | } |
53010e52 | 333 | |
debe6624 | 334 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 RR |
335 | { |
336 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
337 | int tmp = (int) pos; | |
338 | gtk_entry_set_position( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 339 | } |
53010e52 RR |
340 | |
341 | void wxComboBox::SetInsertionPointEnd(void) | |
342 | { | |
343 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
344 | int pos = GTK_ENTRY(entry)->text_length; | |
345 | SetInsertionPoint( pos-1 ); | |
6de97a3b | 346 | } |
53010e52 RR |
347 | |
348 | long wxComboBox::GetInsertionPoint(void) const | |
349 | { | |
350 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
351 | return (long) GTK_EDITABLE(entry)->current_pos; | |
6de97a3b | 352 | } |
53010e52 RR |
353 | |
354 | long wxComboBox::GetLastPosition(void) const | |
355 | { | |
356 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
357 | int pos = GTK_ENTRY(entry)->text_length; | |
358 | return (long) pos-1; | |
6de97a3b | 359 | } |
53010e52 | 360 | |
debe6624 | 361 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 RR |
362 | { |
363 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
364 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
365 | if (value.IsNull()) return; | |
366 | gint pos = (gint)to; | |
367 | gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos ); | |
6de97a3b | 368 | } |
53010e52 | 369 | |
debe6624 | 370 | void wxComboBox::Remove(long from, long to) |
53010e52 RR |
371 | { |
372 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
373 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 374 | } |
53010e52 | 375 | |
debe6624 | 376 | void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) ) |
53010e52 | 377 | { |
6de97a3b | 378 | } |
53010e52 | 379 | |
debe6624 | 380 | void wxComboBox::SetEditable( bool WXUNUSED(editable) ) |
53010e52 | 381 | { |
6de97a3b | 382 | } |
53010e52 | 383 | |
868a2826 RR |
384 | void wxComboBox::SetFont( const wxFont &font ) |
385 | { | |
386 | wxWindow::SetFont( font ); | |
387 | ||
388 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
389 | ||
390 | gtk_widget_set_style( entry, | |
391 | gtk_style_ref( | |
392 | gtk_widget_get_style( m_widget ) ) ); | |
393 | ||
394 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
395 | ||
396 | GList *child = GTK_LIST(list)->children; | |
397 | while (child) | |
398 | { | |
399 | GtkBin *bin = (GtkBin*) child->data; | |
400 | gtk_widget_set_style( bin->child, | |
401 | gtk_style_ref( | |
402 | gtk_widget_get_style( m_widget ) ) ); | |
403 | ||
404 | child = child->next; | |
405 | } | |
406 | } | |
53010e52 | 407 |