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