]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Added wxDC:DrawPolygone
[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: 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
180 wxFAIL_MSG( "wxComboBox: string not found" );
181
182 return -1;
183 };
184
185 char* wxComboBox::GetClientData( int n )
186 {
187 wxNode *node = m_clientData.Nth( n );
188 if (node) return (char*)node->Data();
189
190 wxFAIL_MSG( "wxComboBox: wrong index" );
191
192 return NULL;
193 };
194
195 void wxComboBox::SetClientData( int n, char * clientData )
196 {
197 wxNode *node = m_clientData.Nth( n );
198 if (node) node->SetData( (wxObject*) clientData );
199
200 wxFAIL_MSG( "wxComboBox: wrong index" );
201 };
202
203 int wxComboBox::GetSelection(void) const
204 {
205 GtkWidget *list = GTK_COMBO(m_widget)->list;
206
207 GList *selection = GTK_LIST(list)->selection;
208 if (selection)
209 {
210 GList *child = GTK_LIST(list)->children;
211 int count = 0;
212 while (child)
213 {
214 if (child->data == selection->data) return count;
215 count++;
216 child = child->next;
217 };
218 };
219
220 wxFAIL_MSG( "wxComboBox: no selection" );
221
222 return -1;
223 };
224
225 wxString wxComboBox::GetString( int n ) const
226 {
227 GtkWidget *list = GTK_COMBO(m_widget)->list;
228
229 GList *child = g_list_nth( GTK_LIST(list)->children, n );
230 if (child)
231 {
232 GtkBin *bin = GTK_BIN( child->data );
233 GtkLabel *label = GTK_LABEL( bin->child );
234 return label->label;
235 };
236
237 wxFAIL_MSG( "wxComboBox: wrong index" );
238
239 return "";
240 };
241
242 wxString wxComboBox::GetStringSelection(void) const
243 {
244 GtkWidget *list = GTK_COMBO(m_widget)->list;
245
246 GList *selection = GTK_LIST(list)->selection;
247 if (selection)
248 {
249 GtkBin *bin = GTK_BIN( selection->data );
250 wxString tmp = GTK_LABEL( bin->child )->label;
251 return tmp;
252 };
253
254 wxFAIL_MSG( "wxComboBox: no selection" );
255
256 return "";
257 };
258
259 int wxComboBox::Number(void) const
260 {
261 GtkWidget *list = GTK_COMBO(m_widget)->list;
262
263 GList *child = GTK_LIST(list)->children;
264 int count = 0;
265 while (child) { count++; child = child->next; };
266 return count;
267 };
268
269 void wxComboBox::SetSelection( int n )
270 {
271 GtkWidget *list = GTK_COMBO(m_widget)->list;
272 gtk_list_select_item( GTK_LIST(list), n );
273 };
274
275 void wxComboBox::SetStringSelection( const wxString &string )
276 {
277 int res = FindString( string );
278 if (res == -1) return;
279 SetSelection( res );
280 };
281
282 wxString wxComboBox::GetValue(void) const
283 {
284 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
285 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
286 return tmp;
287 };
288
289 void wxComboBox::SetValue( const wxString& value )
290 {
291 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
292 wxString tmp = "";
293 if (!value.IsNull()) tmp = value;
294 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
295 };
296
297 void wxComboBox::Copy(void)
298 {
299 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
300 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
301 };
302
303 void wxComboBox::Cut(void)
304 {
305 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
306 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
307 };
308
309 void wxComboBox::Paste(void)
310 {
311 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
312 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
313 };
314
315 void wxComboBox::SetInsertionPoint( long pos )
316 {
317 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
318 int tmp = (int) pos;
319 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
320 };
321
322 void wxComboBox::SetInsertionPointEnd(void)
323 {
324 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
325 int pos = GTK_ENTRY(entry)->text_length;
326 SetInsertionPoint( pos-1 );
327 };
328
329 long wxComboBox::GetInsertionPoint(void) const
330 {
331 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
332 return (long) GTK_EDITABLE(entry)->current_pos;
333 };
334
335 long wxComboBox::GetLastPosition(void) const
336 {
337 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
338 int pos = GTK_ENTRY(entry)->text_length;
339 return (long) pos-1;
340 };
341
342 void wxComboBox::Replace( long from, long to, const wxString& value )
343 {
344 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
345 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
346 if (value.IsNull()) return;
347 gint pos = (gint)to;
348 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
349 };
350
351 void wxComboBox::Remove(long from, long to)
352 {
353 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
354 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
355 };
356
357 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
358 {
359 };
360
361 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
362 {
363 };
364
365