]>
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 RR |
24 | //----------------------------------------------------------------------------- |
25 | // wxComboBox | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
47908e25 RR |
28 | //----------------------------------------------------------------------------- |
29 | // clicked | |
30 | ||
31 | static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) | |
53010e52 | 32 | { |
66bd6b93 RR |
33 | if (!combo->HasVMT()) return; |
34 | if (g_blockEventsOnDrag) return; | |
35 | ||
47908e25 RR |
36 | if (combo->m_alreadySent) |
37 | { | |
38 | combo->m_alreadySent = FALSE; | |
39 | return; | |
40 | } | |
41 | ||
42 | combo->m_alreadySent = TRUE; | |
43 | ||
53010e52 RR |
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); | |
47908e25 | 49 | combo->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 50 | } |
47908e25 RR |
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; | |
6de97a3b | 66 | } |
47908e25 | 67 | */ |
53010e52 RR |
68 | |
69 | //----------------------------------------------------------------------------- | |
70 | ||
71 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) | |
72 | ||
debe6624 | 73 | bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value, |
53010e52 | 74 | const wxPoint& pos, const wxSize& size, |
debe6624 | 75 | int n, const wxString choices[], |
6de97a3b | 76 | long style, const wxValidator& validator, const wxString& name ) |
53010e52 | 77 | { |
47908e25 | 78 | m_alreadySent = FALSE; |
53010e52 RR |
79 | m_needParent = TRUE; |
80 | ||
81 | PreCreation( parent, id, pos, size, style, name ); | |
82 | ||
6de97a3b RR |
83 | SetValidator( validator ); |
84 | ||
53010e52 RR |
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 | ||
53010e52 RR |
99 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
100 | ||
47908e25 RR |
101 | m_clientData.Append( (wxObject*)NULL ); |
102 | ||
53010e52 | 103 | gtk_widget_show( list_item ); |
66bd6b93 RR |
104 | |
105 | gtk_signal_connect( GTK_OBJECT(list_item), "select", | |
106 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); | |
6de97a3b | 107 | } |
53010e52 RR |
108 | |
109 | PostCreation(); | |
110 | ||
47908e25 RR |
111 | /* |
112 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", | |
113 | GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this ); | |
114 | */ | |
115 | ||
53010e52 RR |
116 | if (!value.IsNull()) SetValue( value ); |
117 | ||
118 | Show( TRUE ); | |
119 | ||
120 | return TRUE; | |
6de97a3b | 121 | } |
53010e52 RR |
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() ); | |
47908e25 RR |
127 | |
128 | m_clientData.Clear(); | |
6de97a3b | 129 | } |
53010e52 RR |
130 | |
131 | void wxComboBox::Append( const wxString &item ) | |
47908e25 RR |
132 | { |
133 | Append( item, (char*)NULL ); | |
6de97a3b | 134 | } |
47908e25 RR |
135 | |
136 | void wxComboBox::Append( const wxString &item, char *clientData ) | |
53010e52 RR |
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 | ||
47908e25 | 143 | gtk_signal_connect( GTK_OBJECT(list_item), "select", |
53010e52 RR |
144 | GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this ); |
145 | ||
2f6407b9 RR |
146 | m_clientData.Append( (wxObject*)clientData ); |
147 | ||
53010e52 RR |
148 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
149 | ||
150 | gtk_widget_show( list_item ); | |
6de97a3b | 151 | } |
53010e52 | 152 | |
debe6624 | 153 | void wxComboBox::Delete( int n ) |
53010e52 | 154 | { |
2f6407b9 RR |
155 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
156 | ||
157 | GList *child = g_list_nth( listbox->children, n ); | |
158 | ||
159 | if (!child) | |
160 | { | |
161 | wxFAIL_MSG("wrong index"); | |
162 | return; | |
163 | } | |
164 | ||
165 | GList *list = g_list_append( NULL, child->data ); | |
166 | gtk_list_remove_items( listbox, list ); | |
167 | g_list_free( list ); | |
47908e25 RR |
168 | |
169 | wxNode *node = m_clientData.Nth( n ); | |
170 | if (!node) | |
171 | { | |
2f6407b9 | 172 | wxFAIL_MSG( "wrong index" ); |
47908e25 RR |
173 | } |
174 | else | |
175 | m_clientData.DeleteNode( node ); | |
6de97a3b | 176 | } |
53010e52 RR |
177 | |
178 | int wxComboBox::FindString( const wxString &item ) | |
179 | { | |
180 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
181 | ||
182 | GList *child = GTK_LIST(list)->children; | |
183 | int count = 0; | |
184 | while (child) | |
185 | { | |
186 | GtkBin *bin = GTK_BIN( child->data ); | |
187 | GtkLabel *label = GTK_LABEL( bin->child ); | |
188 | if (item == label->label) return count; | |
189 | count++; | |
190 | child = child->next; | |
6de97a3b | 191 | } |
b6af8d80 RR |
192 | |
193 | wxFAIL_MSG( "wxComboBox: string not found" ); | |
194 | ||
53010e52 | 195 | return -1; |
6de97a3b | 196 | } |
53010e52 | 197 | |
debe6624 | 198 | char* wxComboBox::GetClientData( int n ) |
53010e52 RR |
199 | { |
200 | wxNode *node = m_clientData.Nth( n ); | |
201 | if (node) return (char*)node->Data(); | |
b6af8d80 RR |
202 | |
203 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
204 | ||
c67daf87 | 205 | return (char *) NULL; |
6de97a3b | 206 | } |
53010e52 | 207 | |
debe6624 | 208 | void wxComboBox::SetClientData( int n, char * clientData ) |
53010e52 RR |
209 | { |
210 | wxNode *node = m_clientData.Nth( n ); | |
211 | if (node) node->SetData( (wxObject*) clientData ); | |
b6af8d80 RR |
212 | |
213 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
6de97a3b | 214 | } |
53010e52 RR |
215 | |
216 | int wxComboBox::GetSelection(void) const | |
217 | { | |
218 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
219 | ||
220 | GList *selection = GTK_LIST(list)->selection; | |
221 | if (selection) | |
222 | { | |
223 | GList *child = GTK_LIST(list)->children; | |
224 | int count = 0; | |
225 | while (child) | |
226 | { | |
227 | if (child->data == selection->data) return count; | |
228 | count++; | |
229 | child = child->next; | |
6de97a3b RR |
230 | } |
231 | } | |
b6af8d80 RR |
232 | |
233 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
234 | ||
53010e52 | 235 | return -1; |
6de97a3b | 236 | } |
53010e52 | 237 | |
debe6624 | 238 | wxString wxComboBox::GetString( int n ) const |
53010e52 RR |
239 | { |
240 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
241 | ||
242 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); | |
243 | if (child) | |
244 | { | |
245 | GtkBin *bin = GTK_BIN( child->data ); | |
246 | GtkLabel *label = GTK_LABEL( bin->child ); | |
247 | return label->label; | |
6de97a3b | 248 | } |
b6af8d80 RR |
249 | |
250 | wxFAIL_MSG( "wxComboBox: wrong index" ); | |
251 | ||
53010e52 | 252 | return ""; |
6de97a3b | 253 | } |
53010e52 RR |
254 | |
255 | wxString wxComboBox::GetStringSelection(void) const | |
256 | { | |
257 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
258 | ||
259 | GList *selection = GTK_LIST(list)->selection; | |
260 | if (selection) | |
261 | { | |
262 | GtkBin *bin = GTK_BIN( selection->data ); | |
263 | wxString tmp = GTK_LABEL( bin->child )->label; | |
264 | return tmp; | |
6de97a3b | 265 | } |
b6af8d80 RR |
266 | |
267 | wxFAIL_MSG( "wxComboBox: no selection" ); | |
268 | ||
53010e52 | 269 | return ""; |
6de97a3b | 270 | } |
53010e52 RR |
271 | |
272 | int wxComboBox::Number(void) const | |
273 | { | |
274 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
275 | ||
276 | GList *child = GTK_LIST(list)->children; | |
277 | int count = 0; | |
6de97a3b | 278 | while (child) { count++; child = child->next; } |
53010e52 | 279 | return count; |
6de97a3b | 280 | } |
53010e52 | 281 | |
debe6624 | 282 | void wxComboBox::SetSelection( int n ) |
53010e52 RR |
283 | { |
284 | GtkWidget *list = GTK_COMBO(m_widget)->list; | |
285 | gtk_list_select_item( GTK_LIST(list), n ); | |
6de97a3b | 286 | } |
53010e52 | 287 | |
47908e25 RR |
288 | void wxComboBox::SetStringSelection( const wxString &string ) |
289 | { | |
290 | int res = FindString( string ); | |
291 | if (res == -1) return; | |
292 | SetSelection( res ); | |
6de97a3b | 293 | } |
47908e25 | 294 | |
53010e52 RR |
295 | wxString wxComboBox::GetValue(void) const |
296 | { | |
297 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
298 | wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) ); | |
299 | return tmp; | |
6de97a3b | 300 | } |
53010e52 RR |
301 | |
302 | void wxComboBox::SetValue( const wxString& value ) | |
303 | { | |
304 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
305 | wxString tmp = ""; | |
306 | if (!value.IsNull()) tmp = value; | |
307 | gtk_entry_set_text( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 308 | } |
53010e52 RR |
309 | |
310 | void wxComboBox::Copy(void) | |
311 | { | |
312 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
313 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 314 | } |
53010e52 RR |
315 | |
316 | void wxComboBox::Cut(void) | |
317 | { | |
318 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
319 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 320 | } |
53010e52 RR |
321 | |
322 | void wxComboBox::Paste(void) | |
323 | { | |
324 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
325 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 ); | |
6de97a3b | 326 | } |
53010e52 | 327 | |
debe6624 | 328 | void wxComboBox::SetInsertionPoint( long pos ) |
53010e52 RR |
329 | { |
330 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
331 | int tmp = (int) pos; | |
332 | gtk_entry_set_position( GTK_ENTRY(entry), tmp ); | |
6de97a3b | 333 | } |
53010e52 RR |
334 | |
335 | void wxComboBox::SetInsertionPointEnd(void) | |
336 | { | |
337 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
338 | int pos = GTK_ENTRY(entry)->text_length; | |
339 | SetInsertionPoint( pos-1 ); | |
6de97a3b | 340 | } |
53010e52 RR |
341 | |
342 | long wxComboBox::GetInsertionPoint(void) const | |
343 | { | |
344 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
345 | return (long) GTK_EDITABLE(entry)->current_pos; | |
6de97a3b | 346 | } |
53010e52 RR |
347 | |
348 | long wxComboBox::GetLastPosition(void) const | |
349 | { | |
350 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
351 | int pos = GTK_ENTRY(entry)->text_length; | |
352 | return (long) pos-1; | |
6de97a3b | 353 | } |
53010e52 | 354 | |
debe6624 | 355 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
53010e52 RR |
356 | { |
357 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
358 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
359 | if (value.IsNull()) return; | |
360 | gint pos = (gint)to; | |
361 | gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos ); | |
6de97a3b | 362 | } |
53010e52 | 363 | |
debe6624 | 364 | void wxComboBox::Remove(long from, long to) |
53010e52 RR |
365 | { |
366 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; | |
367 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); | |
6de97a3b | 368 | } |
53010e52 | 369 | |
debe6624 | 370 | void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) ) |
53010e52 | 371 | { |
6de97a3b | 372 | } |
53010e52 | 373 | |
debe6624 | 374 | void wxComboBox::SetEditable( bool WXUNUSED(editable) ) |
53010e52 | 375 | { |
6de97a3b | 376 | } |
53010e52 RR |
377 | |
378 |