]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/combobox.cpp
wxTempFile bug corrected: the temp file is now created in the same dir
[wxWidgets.git] / src / gtk1 / 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 // wxComboBox
19 //-----------------------------------------------------------------------------
20
21 void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
22 {
23 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
24 event.SetInt( combo->GetSelection() );
25 wxString tmp( combo->GetStringSelection() );
26 event.SetString( WXSTRINGCAST(tmp) );
27 event.SetEventObject(combo);
28 combo->ProcessEvent(event);
29 };
30
31 //-----------------------------------------------------------------------------
32
33 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
34
35 bool wxComboBox::Create(wxWindow *parent, const wxWindowID id, const wxString& value,
36 const wxPoint& pos, const wxSize& size,
37 const int n, const wxString choices[],
38 const long style, const wxString& name )
39 {
40 m_needParent = TRUE;
41
42 PreCreation( parent, id, pos, size, style, name );
43
44 m_widget = gtk_combo_new();
45
46 wxSize newSize = size;
47 if (newSize.x == -1) newSize.x = 100;
48 if (newSize.y == -1) newSize.y = 26;
49 SetSize( newSize.x, newSize.y );
50
51 GtkWidget *list = GTK_COMBO(m_widget)->list;
52
53 for (int i = 0; i < n; i++)
54 {
55 GtkWidget *list_item;
56 list_item = gtk_list_item_new_with_label( choices[i] );
57
58 gtk_signal_connect( GTK_OBJECT(list_item), "select",
59 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
60
61 gtk_container_add( GTK_CONTAINER(list), list_item );
62
63 gtk_widget_show( list_item );
64 };
65
66 PostCreation();
67
68 if (!value.IsNull()) SetValue( value );
69
70 Show( TRUE );
71
72 return TRUE;
73 };
74
75 void wxComboBox::Clear(void)
76 {
77 GtkWidget *list = GTK_COMBO(m_widget)->list;
78 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
79 };
80
81 void wxComboBox::Append( const wxString &item )
82 {
83 GtkWidget *list = GTK_COMBO(m_widget)->list;
84
85 GtkWidget *list_item;
86 list_item = gtk_list_item_new_with_label( item );
87
88 gtk_signal_connect( GTK_OBJECT(list_item), "select",
89 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
90
91 gtk_container_add( GTK_CONTAINER(list), list_item );
92
93 gtk_widget_show( list_item );
94 };
95
96 void wxComboBox::Append( const wxString &WXUNUSED(item), char* WXUNUSED(clientData) )
97 {
98 };
99
100 void wxComboBox::Delete( const int n )
101 {
102 GtkWidget *list = GTK_COMBO(m_widget)->list;
103 gtk_list_clear_items( GTK_LIST(list), n, n );
104 };
105
106 int wxComboBox::FindString( const wxString &item )
107 {
108 GtkWidget *list = GTK_COMBO(m_widget)->list;
109
110 GList *child = GTK_LIST(list)->children;
111 int count = 0;
112 while (child)
113 {
114 GtkBin *bin = GTK_BIN( child->data );
115 GtkLabel *label = GTK_LABEL( bin->child );
116 if (item == label->label) return count;
117 count++;
118 child = child->next;
119 };
120 return -1;
121 };
122
123 char* wxComboBox::GetClientData( const int n )
124 {
125 wxNode *node = m_clientData.Nth( n );
126 if (node) return (char*)node->Data();
127 return NULL;
128 };
129
130 void wxComboBox::SetClientData( const int n, char * clientData )
131 {
132 wxNode *node = m_clientData.Nth( n );
133 if (node) node->SetData( (wxObject*) clientData );
134 };
135
136 int wxComboBox::GetSelection(void) const
137 {
138 GtkWidget *list = GTK_COMBO(m_widget)->list;
139
140 GList *selection = GTK_LIST(list)->selection;
141 if (selection)
142 {
143 GList *child = GTK_LIST(list)->children;
144 int count = 0;
145 while (child)
146 {
147 if (child->data == selection->data) return count;
148 count++;
149 child = child->next;
150 };
151 };
152 return -1;
153 };
154
155 wxString wxComboBox::GetString( const int n ) const
156 {
157 GtkWidget *list = GTK_COMBO(m_widget)->list;
158
159 GList *child = g_list_nth( GTK_LIST(list)->children, n );
160 if (child)
161 {
162 GtkBin *bin = GTK_BIN( child->data );
163 GtkLabel *label = GTK_LABEL( bin->child );
164 return label->label;
165 };
166 return "";
167 };
168
169 wxString wxComboBox::GetStringSelection(void) const
170 {
171 GtkWidget *list = GTK_COMBO(m_widget)->list;
172
173 GList *selection = GTK_LIST(list)->selection;
174 if (selection)
175 {
176 GtkBin *bin = GTK_BIN( selection->data );
177 wxString tmp = GTK_LABEL( bin->child )->label;
178 return tmp;
179 };
180 return "";
181 };
182
183 int wxComboBox::Number(void) const
184 {
185 GtkWidget *list = GTK_COMBO(m_widget)->list;
186
187 GList *child = GTK_LIST(list)->children;
188 int count = 0;
189 while (child) { count++; child = child->next; };
190 return count;
191 };
192
193 void wxComboBox::SetSelection( const int n )
194 {
195 GtkWidget *list = GTK_COMBO(m_widget)->list;
196 gtk_list_select_item( GTK_LIST(list), n );
197 };
198
199 wxString wxComboBox::GetValue(void) const
200 {
201 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
202 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
203 return tmp;
204 };
205
206 void wxComboBox::SetValue( const wxString& value )
207 {
208 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
209 wxString tmp = "";
210 if (!value.IsNull()) tmp = value;
211 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
212 };
213
214 void wxComboBox::Copy(void)
215 {
216 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
217 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
218 };
219
220 void wxComboBox::Cut(void)
221 {
222 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
223 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
224 };
225
226 void wxComboBox::Paste(void)
227 {
228 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
229 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
230 };
231
232 void wxComboBox::SetInsertionPoint( const long pos )
233 {
234 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
235 int tmp = (int) pos;
236 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
237 };
238
239 void wxComboBox::SetInsertionPointEnd(void)
240 {
241 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
242 int pos = GTK_ENTRY(entry)->text_length;
243 SetInsertionPoint( pos-1 );
244 };
245
246 long wxComboBox::GetInsertionPoint(void) const
247 {
248 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
249 return (long) GTK_EDITABLE(entry)->current_pos;
250 };
251
252 long wxComboBox::GetLastPosition(void) const
253 {
254 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
255 int pos = GTK_ENTRY(entry)->text_length;
256 return (long) pos-1;
257 };
258
259 void wxComboBox::Replace( const long from, const long to, const wxString& value )
260 {
261 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
262 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
263 if (value.IsNull()) return;
264 gint pos = (gint)to;
265 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
266 };
267
268 void wxComboBox::Remove(const long from, const long to)
269 {
270 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
271 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
272 };
273
274 void wxComboBox::SetSelection( const long WXUNUSED(from), const long WXUNUSED(to) )
275 {
276 };
277
278 void wxComboBox::SetEditable( const bool WXUNUSED(editable) )
279 {
280 };
281
282