]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
DP:
[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 wxString& name )
77 {
78 m_alreadySent = FALSE;
79 m_needParent = TRUE;
80
81 PreCreation( parent, id, pos, size, style, name );
82
83 m_widget = gtk_combo_new();
84
85 wxSize newSize = size;
86 if (newSize.x == -1) newSize.x = 100;
87 if (newSize.y == -1) newSize.y = 26;
88 SetSize( newSize.x, newSize.y );
89
90 GtkWidget *list = GTK_COMBO(m_widget)->list;
91
92 for (int i = 0; i < n; i++)
93 {
94 GtkWidget *list_item;
95 list_item = gtk_list_item_new_with_label( choices[i] );
96
97 gtk_container_add( GTK_CONTAINER(list), list_item );
98
99 m_clientData.Append( (wxObject*)NULL );
100
101 gtk_widget_show( list_item );
102
103 gtk_signal_connect( GTK_OBJECT(list_item), "select",
104 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
105 };
106
107 PostCreation();
108
109 /*
110 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
111 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
112 */
113
114 if (!value.IsNull()) SetValue( value );
115
116 Show( TRUE );
117
118 return TRUE;
119 };
120
121 void wxComboBox::Clear(void)
122 {
123 GtkWidget *list = GTK_COMBO(m_widget)->list;
124 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
125
126 m_clientData.Clear();
127 };
128
129 void wxComboBox::Append( const wxString &item )
130 {
131 Append( item, (char*)NULL );
132 };
133
134 void wxComboBox::Append( const wxString &item, char *clientData )
135 {
136 GtkWidget *list = GTK_COMBO(m_widget)->list;
137
138 GtkWidget *list_item;
139 list_item = gtk_list_item_new_with_label( item );
140
141 gtk_signal_connect( GTK_OBJECT(list_item), "select",
142 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
143
144 gtk_container_add( GTK_CONTAINER(list), list_item );
145
146 gtk_widget_show( list_item );
147
148 m_clientData.Append( (wxObject*)clientData );
149 };
150
151 void wxComboBox::Delete( int n )
152 {
153 GtkWidget *list = GTK_COMBO(m_widget)->list;
154 gtk_list_clear_items( GTK_LIST(list), n, n );
155
156 wxNode *node = m_clientData.Nth( n );
157 if (!node)
158 {
159 wxFAIL_MSG(_("wxComboBox::Delete wrong index"));
160 }
161 else
162 m_clientData.DeleteNode( node );
163 };
164
165 int wxComboBox::FindString( const wxString &item )
166 {
167 GtkWidget *list = GTK_COMBO(m_widget)->list;
168
169 GList *child = GTK_LIST(list)->children;
170 int count = 0;
171 while (child)
172 {
173 GtkBin *bin = GTK_BIN( child->data );
174 GtkLabel *label = GTK_LABEL( bin->child );
175 if (item == label->label) return count;
176 count++;
177 child = child->next;
178 };
179 return -1;
180 };
181
182 char* wxComboBox::GetClientData( int n )
183 {
184 wxNode *node = m_clientData.Nth( n );
185 if (node) return (char*)node->Data();
186 return NULL;
187 };
188
189 void wxComboBox::SetClientData( int n, char * clientData )
190 {
191 wxNode *node = m_clientData.Nth( n );
192 if (node) node->SetData( (wxObject*) clientData );
193 };
194
195 int wxComboBox::GetSelection(void) const
196 {
197 GtkWidget *list = GTK_COMBO(m_widget)->list;
198
199 GList *selection = GTK_LIST(list)->selection;
200 if (selection)
201 {
202 GList *child = GTK_LIST(list)->children;
203 int count = 0;
204 while (child)
205 {
206 if (child->data == selection->data) return count;
207 count++;
208 child = child->next;
209 };
210 };
211 return -1;
212 };
213
214 wxString wxComboBox::GetString( int n ) const
215 {
216 GtkWidget *list = GTK_COMBO(m_widget)->list;
217
218 GList *child = g_list_nth( GTK_LIST(list)->children, n );
219 if (child)
220 {
221 GtkBin *bin = GTK_BIN( child->data );
222 GtkLabel *label = GTK_LABEL( bin->child );
223 return label->label;
224 };
225 return "";
226 };
227
228 wxString wxComboBox::GetStringSelection(void) const
229 {
230 GtkWidget *list = GTK_COMBO(m_widget)->list;
231
232 GList *selection = GTK_LIST(list)->selection;
233 if (selection)
234 {
235 GtkBin *bin = GTK_BIN( selection->data );
236 wxString tmp = GTK_LABEL( bin->child )->label;
237 return tmp;
238 };
239 return "";
240 };
241
242 int wxComboBox::Number(void) const
243 {
244 GtkWidget *list = GTK_COMBO(m_widget)->list;
245
246 GList *child = GTK_LIST(list)->children;
247 int count = 0;
248 while (child) { count++; child = child->next; };
249 return count;
250 };
251
252 void wxComboBox::SetSelection( int n )
253 {
254 GtkWidget *list = GTK_COMBO(m_widget)->list;
255 gtk_list_select_item( GTK_LIST(list), n );
256 };
257
258 void wxComboBox::SetStringSelection( const wxString &string )
259 {
260 int res = FindString( string );
261 if (res == -1) return;
262 SetSelection( res );
263 };
264
265 wxString wxComboBox::GetValue(void) const
266 {
267 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
268 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
269 return tmp;
270 };
271
272 void wxComboBox::SetValue( const wxString& value )
273 {
274 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
275 wxString tmp = "";
276 if (!value.IsNull()) tmp = value;
277 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
278 };
279
280 void wxComboBox::Copy(void)
281 {
282 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
283 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
284 };
285
286 void wxComboBox::Cut(void)
287 {
288 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
289 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
290 };
291
292 void wxComboBox::Paste(void)
293 {
294 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
295 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
296 };
297
298 void wxComboBox::SetInsertionPoint( long pos )
299 {
300 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
301 int tmp = (int) pos;
302 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
303 };
304
305 void wxComboBox::SetInsertionPointEnd(void)
306 {
307 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
308 int pos = GTK_ENTRY(entry)->text_length;
309 SetInsertionPoint( pos-1 );
310 };
311
312 long wxComboBox::GetInsertionPoint(void) const
313 {
314 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
315 return (long) GTK_EDITABLE(entry)->current_pos;
316 };
317
318 long wxComboBox::GetLastPosition(void) const
319 {
320 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
321 int pos = GTK_ENTRY(entry)->text_length;
322 return (long) pos-1;
323 };
324
325 void wxComboBox::Replace( long from, long to, const wxString& value )
326 {
327 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
328 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
329 if (value.IsNull()) return;
330 gint pos = (gint)to;
331 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
332 };
333
334 void wxComboBox::Remove(long from, long to)
335 {
336 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
337 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
338 };
339
340 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
341 {
342 };
343
344 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
345 {
346 };
347
348