]>
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 | ||
139 | GtkWidget *list_item; | |
140 | list_item = gtk_list_item_new_with_label( item ); | |
141 | ||
47908e25 | 142 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
53010e52 RR |
143 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); |
144 | ||
2f6407b9 RR |
145 | m_clientData.Append( (wxObject*)clientData ); |
146 | ||
53010e52 RR |
147 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
148 | ||
149 | gtk_widget_show( list_item ); | |
6de97a3b | 150 | } |
53010e52 | 151 | |
debe6624 | 152 | void wxComboBox::Delete( int n ) |
53010e52 | 153 | { |
2f6407b9 RR |
154 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
155 | ||
156 | GList *child = g_list_nth( listbox->children, n ); | |
157 | ||
158 | if (!child) | |
159 | { | |
160 | wxFAIL_MSG("wrong index"); | |
161 | return; | |
162 | } | |
163 | ||
164 | GList *list = g_list_append( NULL, child->data ); | |
165 | gtk_list_remove_items( listbox, list ); | |
166 | g_list_free( list ); | |
47908e25 RR |
167 | |
168 | wxNode *node = m_clientData.Nth( n ); | |
169 | if (!node) | |
170 | { | |
2f6407b9 | 171 | wxFAIL_MSG( "wrong index" ); |
47908e25 RR |
172 | } |
173 | else | |
174 | m_clientData.DeleteNode( node ); | |
6de97a3b | 175 | } |
53010e52 RR |
176 | |
177 | int wxComboBox::FindString( const wxString &item ) | |
178 | { | |
179 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
180 | ||
181 | GList *child = GTK_LIST(list)->children; | |
182 | int count = 0; | |
183 | while (child) | |
184 | { | |
185 | GtkBin *bin = GTK_BIN( child->data ); | |
186 | GtkLabel *label = GTK_LABEL( bin->child ); | |
187 | if (item == label->label) return count; | |
188 | count++; | |
189 | child = child->next; | |
6de97a3b | 190 | } |
b6af8d80 RR |
191 | |
192 | wxFAIL_MSG( "wxComboBox: string not found" ); | |
193 | ||
53010e52 | 194 | return -1; |
6de97a3b | 195 | } |
53010e52 | 196 | |
debe6624 | 197 | char* wxComboBox::GetClientData( int n ) |
53010e52 RR |
198 | { |
199 | wxNode *node = m_clientData.Nth( n ); | |
200 | if (node) return (char*)node->Data(); | |
b6af8d80 RR |
201 | |
202 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
203 | ||
c67daf87 | 204 | return (char *) NULL; |
6de97a3b | 205 | } |
53010e52 | 206 | |
debe6624 | 207 | void wxComboBox::SetClientData( int n, char * clientData ) |
53010e52 RR |
208 | { |
209 | wxNode *node = m_clientData.Nth( n ); | |
210 | if (node) node->SetData( (wxObject*) clientData ); | |
b6af8d80 RR |
211 | |
212 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
6de97a3b | 213 | } |
53010e52 RR |
214 | |
215 | int wxComboBox::GetSelection(void) const | |
216 | { | |
217 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
218 | ||
219 | GList *selection = GTK_LIST(list)->selection; | |
220 | if (selection) | |
221 | { | |
222 | GList *child = GTK_LIST(list)->children; | |
223 | int count = 0; | |
224 | while (child) | |
225 | { | |
226 | if (child->data == selection->data) return count; | |
227 | count++; | |
228 | child = child->next; | |
6de97a3b RR |
229 | } |
230 | } | |
b6af8d80 RR |
231 | |
232 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
233 | ||
53010e52 | 234 | return -1; |
6de97a3b | 235 | } |
53010e52 | 236 | |
debe6624 | 237 | wxString wxComboBox::GetString( int n ) const |
53010e52 RR |
238 | { |
239 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
240 | ||
241 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
242 | if (child) | |
243 | { | |
244 | GtkBin *bin = GTK_BIN( child->data ); | |
245 | GtkLabel *label = GTK_LABEL( bin->child ); | |
246 | return label->label; | |
6de97a3b | 247 | } |
b6af8d80 RR |
248 | |
249 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
250 | ||
53010e52 | 251 | return ""; |
6de97a3b | 252 | } |
53010e52 RR |
253 | |
254 | wxString wxComboBox::GetStringSelection(void) const | |
255 | { | |
256 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
257 | ||
258 | GList *selection = GTK_LIST(list)->selection; | |
259 | if (selection) | |
260 | { | |
261 | GtkBin *bin = GTK_BIN( selection->data ); | |
262 | wxString tmp = GTK_LABEL( bin->child )->label; | |
263 | return tmp; | |
6de97a3b | 264 | } |
b6af8d80 RR |
265 | |
266 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
267 | ||
53010e52 | 268 | return ""; |
6de97a3b | 269 | } |
53010e52 RR |
270 | |
271 | int wxComboBox::Number(void) const | |
272 | { | |
273 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
274 | ||
275 | GList *child = GTK_LIST(list)->children; | |
276 | int count = 0; | |
6de97a3b | 277 | while (child) { count++; child = child->next; } |
53010e52 | 278 | return count; |
6de97a3b | 279 | } |
53010e52 | 280 | |
debe6624 | 281 | void wxComboBox::SetSelection( int n ) |
53010e52 RR |
282 | { |
283 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
284 | gtk_list_select_item( GTK_LIST(list), n ); | |
6de97a3b | 285 | } |
53010e52 | 286 | |
47908e25 RR |
287 | void wxComboBox::SetStringSelection( const wxString &string ) |
288 | { | |
289 | int res = FindString( string ); | |
290 | if (res == -1) return; | |
291 | SetSelection( res ); | |
6de97a3b | 292 | } |
47908e25 | 293 | |
53010e52 RR |
294 | wxString wxComboBox::GetValue(void) const |
295 | { | |
296 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
297 | wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) ); | |
298 | return tmp; | |
6de97a3b | 299 | } |
53010e52 RR |
300 | |
301 | void wxComboBox::SetValue( const wxString& value ) | |
302 | { | |
303 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
304 | wxString tmp = ""; | |
305 | if (!value.IsNull()) tmp = value; | |
306 | gtk_entry_set_text( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 307 | } |
53010e52 RR |
308 | |
309 | void wxComboBox::Copy(void) | |
310 | { | |
311 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
312 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 313 | } |
53010e52 RR |
314 | |
315 | void wxComboBox::Cut(void) | |
316 | { | |
317 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
318 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 319 | } |
53010e52 RR |
320 | |
321 | void wxComboBox::Paste(void) | |
322 | { | |
323 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
324 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 325 | } |
53010e52 | 326 | |
debe6624 | 327 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 RR |
328 | { |
329 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
330 | int tmp = (int) pos; | |
331 | gtk_entry_set_position( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 332 | } |
53010e52 RR |
333 | |
334 | void wxComboBox::SetInsertionPointEnd(void) | |
335 | { | |
336 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
337 | int pos = GTK_ENTRY(entry)->text_length; | |
338 | SetInsertionPoint( pos-1 ); | |
6de97a3b | 339 | } |
53010e52 RR |
340 | |
341 | long wxComboBox::GetInsertionPoint(void) const | |
342 | { | |
343 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
344 | return (long) GTK_EDITABLE(entry)->current_pos; | |
6de97a3b | 345 | } |
53010e52 RR |
346 | |
347 | long wxComboBox::GetLastPosition(void) const | |
348 | { | |
349 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
350 | int pos = GTK_ENTRY(entry)->text_length; | |
351 | return (long) pos-1; | |
6de97a3b | 352 | } |
53010e52 | 353 | |
debe6624 | 354 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 RR |
355 | { |
356 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
357 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
358 | if (value.IsNull()) return; | |
359 | gint pos = (gint)to; | |
360 | gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos ); | |
6de97a3b | 361 | } |
53010e52 | 362 | |
debe6624 | 363 | void wxComboBox::Remove(long from, long to) |
53010e52 RR |
364 | { |
365 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
366 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 367 | } |
53010e52 | 368 | |
debe6624 | 369 | void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) ) |
53010e52 | 370 | { |
6de97a3b | 371 | } |
53010e52 | 372 | |
debe6624 | 373 | void wxComboBox::SetEditable( bool WXUNUSED(editable) ) |
53010e52 | 374 | { |
6de97a3b | 375 | } |
53010e52 RR |
376 | |
377 |