]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: choice.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/choice.h" | |
16 | ||
17 | #include "gdk/gdk.h" | |
18 | #include "gtk/gtk.h" | |
19 | ||
20 | //----------------------------------------------------------------------------- | |
21 | // data | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern bool g_blockEventsOnDrag; | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // "activate" | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) | |
31 | { | |
32 | if (!choice->HasVMT()) return; | |
33 | if (g_blockEventsOnDrag) return; | |
34 | ||
35 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); | |
36 | event.SetInt( choice->GetSelection() ); | |
37 | wxString tmp( choice->GetStringSelection() ); | |
38 | event.SetString( WXSTRINGCAST(tmp) ); | |
39 | event.SetEventObject(choice); | |
40 | choice->GetEventHandler()->ProcessEvent(event); | |
41 | } | |
42 | ||
43 | //----------------------------------------------------------------------------- | |
44 | // wxChoice | |
45 | //----------------------------------------------------------------------------- | |
46 | ||
47 | IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl) | |
48 | ||
49 | wxChoice::wxChoice() | |
50 | { | |
51 | } | |
52 | ||
53 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, | |
54 | const wxPoint &pos, const wxSize &size, | |
55 | int n, const wxString choices[], | |
56 | long style, const wxValidator& validator, const wxString &name ) | |
57 | { | |
58 | m_needParent = TRUE; | |
59 | ||
60 | PreCreation( parent, id, pos, size, style, name ); | |
61 | ||
62 | SetValidator( validator ); | |
63 | ||
64 | m_widget = gtk_option_menu_new(); | |
65 | ||
66 | wxSize newSize = size; | |
67 | if (newSize.x == -1) newSize.x = 80; | |
68 | if (newSize.y == -1) newSize.y = 26; | |
69 | SetSize( newSize.x, newSize.y ); | |
70 | ||
71 | GtkWidget *menu = gtk_menu_new(); | |
72 | ||
73 | for (int i = 0; i < n; i++) | |
74 | { | |
75 | m_clientDataList.Append( (wxObject*) NULL ); | |
76 | m_clientObjectList.Append( (wxObject*) NULL ); | |
77 | ||
78 | GtkWidget *item = gtk_menu_item_new_with_label( choices[i] ); | |
79 | gtk_menu_append( GTK_MENU(menu), item ); | |
80 | ||
81 | gtk_widget_realize( item ); | |
82 | gtk_widget_realize( GTK_BIN(item)->child ); | |
83 | ||
84 | gtk_widget_show( item ); | |
85 | ||
86 | gtk_signal_connect( GTK_OBJECT( item ), "activate", | |
87 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
88 | } | |
89 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
90 | ||
91 | m_parent->AddChild( this ); | |
92 | ||
93 | (m_parent->m_insertCallback)( m_parent, this ); | |
94 | ||
95 | PostCreation(); | |
96 | ||
97 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
98 | SetForegroundColour( parent->GetForegroundColour() ); | |
99 | SetFont( parent->GetFont() ); | |
100 | ||
101 | Show( TRUE ); | |
102 | ||
103 | return TRUE; | |
104 | } | |
105 | ||
106 | wxChoice::~wxChoice() | |
107 | { | |
108 | Clear(); | |
109 | } | |
110 | ||
111 | void wxChoice::AppendCommon( const wxString &item ) | |
112 | { | |
113 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
114 | ||
115 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); | |
116 | GtkWidget *menu_item = gtk_menu_item_new_with_label( item ); | |
117 | ||
118 | gtk_menu_append( GTK_MENU(menu), menu_item ); | |
119 | ||
120 | gtk_widget_realize( menu_item ); | |
121 | gtk_widget_realize( GTK_BIN(menu_item)->child ); | |
122 | ||
123 | if (m_widgetStyle) ApplyWidgetStyle(); | |
124 | ||
125 | gtk_signal_connect( GTK_OBJECT( menu_item ), "activate", | |
126 | GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this ); | |
127 | ||
128 | gtk_widget_show( menu_item ); | |
129 | } | |
130 | ||
131 | void wxChoice::Append( const wxString &item ) | |
132 | { | |
133 | m_clientDataList.Append( (wxObject*) NULL ); | |
134 | m_clientObjectList.Append( (wxObject*) NULL ); | |
135 | ||
136 | AppendCommon( item ); | |
137 | } | |
138 | ||
139 | void wxChoice::Append( const wxString &item, void *clientData ) | |
140 | { | |
141 | m_clientDataList.Append( (wxObject*) clientData ); | |
142 | m_clientObjectList.Append( (wxObject*) NULL ); | |
143 | ||
144 | AppendCommon( item ); | |
145 | } | |
146 | ||
147 | void wxChoice::Append( const wxString &item, wxClientData *clientData ) | |
148 | { | |
149 | m_clientObjectList.Append( (wxObject*) clientData ); | |
150 | m_clientDataList.Append( (wxObject*) NULL ); | |
151 | ||
152 | AppendCommon( item ); | |
153 | } | |
154 | ||
155 | void wxChoice::SetClientData( int n, void* clientData ) | |
156 | { | |
157 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
158 | ||
159 | wxNode *node = m_clientDataList.Nth( n ); | |
160 | if (!node) return; | |
161 | ||
162 | node->SetData( (wxObject*) clientData ); | |
163 | } | |
164 | ||
165 | void* wxChoice::GetClientData( int n ) | |
166 | { | |
167 | wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" ); | |
168 | ||
169 | wxNode *node = m_clientDataList.Nth( n ); | |
170 | if (!node) return NULL; | |
171 | ||
172 | return node->Data(); | |
173 | } | |
174 | ||
175 | void wxChoice::SetClientObject( int n, wxClientData* clientData ) | |
176 | { | |
177 | wxCHECK_RET( m_widget != NULL, "invalid combobox" ); | |
178 | ||
179 | wxNode *node = m_clientObjectList.Nth( n ); | |
180 | if (!node) return; | |
181 | ||
182 | wxClientData *cd = (wxClientData*) node->Data(); | |
183 | if (cd) delete cd; | |
184 | ||
185 | node->SetData( (wxObject*) clientData ); | |
186 | } | |
187 | ||
188 | wxClientData* wxChoice::GetClientObject( int n ) | |
189 | { | |
190 | wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, "invalid combobox" ); | |
191 | ||
192 | wxNode *node = m_clientObjectList.Nth( n ); | |
193 | if (!node) return (wxClientData*) NULL; | |
194 | ||
195 | return (wxClientData*) node->Data(); | |
196 | } | |
197 | ||
198 | void wxChoice::Clear() | |
199 | { | |
200 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
201 | ||
202 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); | |
203 | GtkWidget *menu = gtk_menu_new(); | |
204 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); | |
205 | ||
206 | wxNode *node = m_clientObjectList.First(); | |
207 | while (node) | |
208 | { | |
209 | wxClientData *cd = (wxClientData*)node->Data(); | |
210 | if (cd) delete cd; | |
211 | node = node->Next(); | |
212 | } | |
213 | m_clientObjectList.Clear(); | |
214 | ||
215 | m_clientDataList.Clear(); | |
216 | } | |
217 | ||
218 | void wxChoice::Delete( int WXUNUSED(n) ) | |
219 | { | |
220 | wxFAIL_MSG( "wxChoice:Delete not implemented" ); | |
221 | } | |
222 | ||
223 | int wxChoice::FindString( const wxString &string ) const | |
224 | { | |
225 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); | |
226 | ||
227 | // If you read this code once and you think you understand | |
228 | // it, then you are very wrong. Robert Roebling. | |
229 | ||
230 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
231 | int count = 0; | |
232 | GList *child = menu_shell->children; | |
233 | while (child) | |
234 | { | |
235 | GtkBin *bin = GTK_BIN( child->data ); | |
236 | GtkLabel *label = (GtkLabel *) NULL; | |
237 | if (bin->child) label = GTK_LABEL(bin->child); | |
238 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
239 | ||
240 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
241 | ||
242 | if (string == label->label) return count; | |
243 | child = child->next; | |
244 | count++; | |
245 | } | |
246 | ||
247 | wxFAIL_MSG( "wxChoice: string not found" ); | |
248 | ||
249 | return -1; | |
250 | } | |
251 | ||
252 | int wxChoice::GetColumns() const | |
253 | { | |
254 | return 1; | |
255 | } | |
256 | ||
257 | int wxChoice::GetSelection() | |
258 | { | |
259 | wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" ); | |
260 | ||
261 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
262 | int count = 0; | |
263 | GList *child = menu_shell->children; | |
264 | while (child) | |
265 | { | |
266 | GtkBin *bin = GTK_BIN( child->data ); | |
267 | if (!bin->child) return count; | |
268 | child = child->next; | |
269 | count++; | |
270 | } | |
271 | ||
272 | wxFAIL_MSG( "wxChoice: no selection" ); | |
273 | ||
274 | return -1; | |
275 | } | |
276 | ||
277 | wxString wxChoice::GetString( int n ) const | |
278 | { | |
279 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); | |
280 | ||
281 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
282 | int count = 0; | |
283 | GList *child = menu_shell->children; | |
284 | while (child) | |
285 | { | |
286 | GtkBin *bin = GTK_BIN( child->data ); | |
287 | if (count == n) | |
288 | { | |
289 | GtkLabel *label = (GtkLabel *) NULL; | |
290 | if (bin->child) label = GTK_LABEL(bin->child); | |
291 | if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
292 | ||
293 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
294 | ||
295 | return label->label; | |
296 | } | |
297 | child = child->next; | |
298 | count++; | |
299 | } | |
300 | ||
301 | wxFAIL_MSG( "wxChoice: string not found" ); | |
302 | ||
303 | return ""; | |
304 | } | |
305 | ||
306 | wxString wxChoice::GetStringSelection() const | |
307 | { | |
308 | wxCHECK_MSG( m_widget != NULL, "", "invalid choice" ); | |
309 | ||
310 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child ); | |
311 | ||
312 | wxASSERT_MSG( label != NULL , "wxChoice: invalid label" ); | |
313 | ||
314 | return label->label; | |
315 | } | |
316 | ||
317 | int wxChoice::Number() const | |
318 | { | |
319 | wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" ); | |
320 | ||
321 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
322 | int count = 0; | |
323 | GList *child = menu_shell->children; | |
324 | while (child) | |
325 | { | |
326 | count++; | |
327 | child = child->next; | |
328 | } | |
329 | return count; | |
330 | } | |
331 | ||
332 | void wxChoice::SetColumns( int WXUNUSED(n) ) | |
333 | { | |
334 | } | |
335 | ||
336 | void wxChoice::SetSelection( int n ) | |
337 | { | |
338 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
339 | ||
340 | int tmp = n; | |
341 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); | |
342 | ||
343 | gtk_choice_clicked_callback( (GtkWidget *) NULL, this ); | |
344 | } | |
345 | ||
346 | void wxChoice::SetStringSelection( const wxString &string ) | |
347 | { | |
348 | wxCHECK_RET( m_widget != NULL, "invalid choice" ); | |
349 | ||
350 | int n = FindString( string ); | |
351 | if (n != -1) SetSelection( n ); | |
352 | } | |
353 | ||
354 | void wxChoice::ApplyWidgetStyle() | |
355 | { | |
356 | SetWidgetStyle(); | |
357 | ||
358 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); | |
359 | ||
360 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
361 | gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle ); | |
362 | ||
363 | GList *child = menu_shell->children; | |
364 | while (child) | |
365 | { | |
366 | gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle ); | |
367 | ||
368 | GtkBin *bin = GTK_BIN( child->data ); | |
369 | GtkWidget *label = (GtkWidget *) NULL; | |
370 | if (bin->child) label = bin->child; | |
371 | if (!label) label = GTK_BUTTON(m_widget)->child; | |
372 | ||
373 | gtk_widget_set_style( label, m_widgetStyle ); | |
374 | ||
375 | child = child->next; | |
376 | } | |
377 | } | |
378 |