]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/combobox.cpp
Removed /install/gtk/configure from cvs
[wxWidgets.git] / src / gtk1 / combobox.cpp
CommitLineData
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"
16
47908e25
RR
17//-----------------------------------------------------------------------------
18// data
19//-----------------------------------------------------------------------------
20
21extern bool g_blockEventsOnDrag;
22
53010e52
RR
23//-----------------------------------------------------------------------------
24// wxComboBox
25//-----------------------------------------------------------------------------
26
47908e25
RR
27//-----------------------------------------------------------------------------
28// clicked
29
30static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 31{
66bd6b93
RR
32 if (!combo->HasVMT()) return;
33 if (g_blockEventsOnDrag) return;
34
47908e25
RR
35 if (combo->m_alreadySent)
36 {
37 combo->m_alreadySent = FALSE;
38 return;
39 }
40
41 combo->m_alreadySent = TRUE;
42
53010e52
RR
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);
47908e25
RR
48 combo->GetEventHandler()->ProcessEvent(event);
49};
50
51//-----------------------------------------------------------------------------
52// size
53
54/*
55static 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;
53010e52 65};
47908e25 66*/
53010e52
RR
67
68//-----------------------------------------------------------------------------
69
70IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
71
debe6624 72bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
53010e52 73 const wxPoint& pos, const wxSize& size,
debe6624
JS
74 int n, const wxString choices[],
75 long style, 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
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
53010e52
RR
96 gtk_container_add( GTK_CONTAINER(list), list_item );
97
47908e25
RR
98 m_clientData.Append( (wxObject*)NULL );
99
53010e52 100 gtk_widget_show( list_item );
66bd6b93
RR
101
102 gtk_signal_connect( GTK_OBJECT(list_item), "select",
103 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
53010e52
RR
104 };
105
106 PostCreation();
107
47908e25
RR
108/*
109 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
110 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
111*/
112
53010e52
RR
113 if (!value.IsNull()) SetValue( value );
114
115 Show( TRUE );
116
117 return TRUE;
118};
119
120void wxComboBox::Clear(void)
121{
122 GtkWidget *list = GTK_COMBO(m_widget)->list;
123 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
47908e25
RR
124
125 m_clientData.Clear();
53010e52
RR
126};
127
128void wxComboBox::Append( const wxString &item )
47908e25
RR
129{
130 Append( item, (char*)NULL );
131};
132
133void wxComboBox::Append( const wxString &item, char *clientData )
53010e52
RR
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
47908e25 140 gtk_signal_connect( GTK_OBJECT(list_item), "select",
53010e52
RR
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 );
47908e25
RR
146
147 m_clientData.Append( (wxObject*)clientData );
53010e52
RR
148};
149
debe6624 150void wxComboBox::Delete( int n )
53010e52
RR
151{
152 GtkWidget *list = GTK_COMBO(m_widget)->list;
153 gtk_list_clear_items( GTK_LIST(list), n, n );
47908e25
RR
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 );
53010e52
RR
162};
163
164int 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
debe6624 181char* wxComboBox::GetClientData( int n )
53010e52
RR
182{
183 wxNode *node = m_clientData.Nth( n );
184 if (node) return (char*)node->Data();
185 return NULL;
186};
187
debe6624 188void wxComboBox::SetClientData( int n, char * clientData )
53010e52
RR
189{
190 wxNode *node = m_clientData.Nth( n );
191 if (node) node->SetData( (wxObject*) clientData );
192};
193
194int 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
debe6624 213wxString wxComboBox::GetString( int n ) const
53010e52
RR
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
227wxString 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
241int 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
debe6624 251void wxComboBox::SetSelection( int n )
53010e52
RR
252{
253 GtkWidget *list = GTK_COMBO(m_widget)->list;
254 gtk_list_select_item( GTK_LIST(list), n );
255};
256
47908e25
RR
257void wxComboBox::SetStringSelection( const wxString &string )
258{
259 int res = FindString( string );
260 if (res == -1) return;
261 SetSelection( res );
262};
263
53010e52
RR
264wxString 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
271void 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
279void wxComboBox::Copy(void)
280{
281 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
282 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
283};
284
285void wxComboBox::Cut(void)
286{
287 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
288 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
289};
290
291void wxComboBox::Paste(void)
292{
293 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
294 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
295};
296
debe6624 297void wxComboBox::SetInsertionPoint( long pos )
53010e52
RR
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
304void 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
311long wxComboBox::GetInsertionPoint(void) const
312{
313 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
314 return (long) GTK_EDITABLE(entry)->current_pos;
315};
316
317long 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
debe6624 324void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52
RR
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
debe6624 333void wxComboBox::Remove(long from, long to)
53010e52
RR
334{
335 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
336 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
337};
338
debe6624 339void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52
RR
340{
341};
342
debe6624 343void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52
RR
344{
345};
346
347