]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
wxMSW fixes after wxFrameBase change - seems to work ok too by now
[wxWidgets.git] / src / gtk / choice.cpp
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 #if wxUSE_CHOICE
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "activate"
37 //-----------------------------------------------------------------------------
38
39 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 if (!choice->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
49 int n = choice->GetSelection();
50
51 event.SetInt( n );
52 event.SetString( choice->GetStringSelection() );
53 event.SetEventObject(choice);
54
55 if ( choice->HasClientObjectData() )
56 event.SetClientObject( choice->GetClientObject(n) );
57 else if ( choice->HasClientUntypedData() )
58 event.SetClientData( choice->GetClientData(n) );
59
60 choice->GetEventHandler()->ProcessEvent(event);
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxChoice
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
68
69 wxChoice::wxChoice()
70 {
71 m_strings = (wxSortedArrayString *)NULL;
72 }
73
74 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
75 const wxPoint &pos, const wxSize &size,
76 int n, const wxString choices[],
77 long style, const wxValidator& validator, const wxString &name )
78 {
79 m_needParent = TRUE;
80 #if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus = TRUE;
82 #endif
83
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
87 wxFAIL_MSG( wxT("wxChoice creation failed") );
88 return FALSE;
89 }
90
91 m_widget = gtk_option_menu_new();
92
93 wxSize newSize(size);
94 if (newSize.x == -1)
95 newSize.x = 80;
96 if (newSize.y == -1)
97 newSize.y = 26;
98 SetSize( newSize.x, newSize.y );
99
100 if ( style & wxCB_SORT )
101 {
102 // if our m_strings != NULL, DoAppend() will check for it and insert
103 // items in the correct order
104 m_strings = new wxSortedArrayString;
105 }
106
107 GtkWidget *menu = gtk_menu_new();
108
109 for (int i = 0; i < n; i++)
110 {
111 AppendHelper(menu, choices[i]);
112 }
113
114 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
115
116 m_parent->DoAddChild( this );
117
118 PostCreation();
119
120 SetBackgroundColour( parent->GetBackgroundColour() );
121 SetForegroundColour( parent->GetForegroundColour() );
122 SetFont( parent->GetFont() );
123
124 Show( TRUE );
125
126 return TRUE;
127 }
128
129 wxChoice::~wxChoice()
130 {
131 Clear();
132
133 delete m_strings;
134 }
135
136 int wxChoice::DoAppend( const wxString &item )
137 {
138 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
139
140 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
141
142 return AppendHelper(menu, item);
143 }
144
145 void wxChoice::DoSetItemClientData( int n, void* clientData )
146 {
147 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
148
149 wxNode *node = m_clientList.Nth( n );
150 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
151
152 node->SetData( (wxObject*) clientData );
153 }
154
155 void* wxChoice::DoGetItemClientData( int n ) const
156 {
157 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
158
159 wxNode *node = m_clientList.Nth( n );
160 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
161
162 return node->Data();
163 }
164
165 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
166 {
167 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
168
169 wxNode *node = m_clientList.Nth( n );
170 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
171
172 wxClientData *cd = (wxClientData*) node->Data();
173 delete cd;
174
175 node->SetData( (wxObject*) clientData );
176 }
177
178 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
179 {
180 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
181
182 wxNode *node = m_clientList.Nth( n );
183 wxCHECK_MSG( node, (wxClientData *)NULL,
184 wxT("invalid index in wxChoice::DoGetItemClientObject") );
185
186 return (wxClientData*) node->Data();
187 }
188
189 void wxChoice::Clear()
190 {
191 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
192
193 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
194 GtkWidget *menu = gtk_menu_new();
195 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
196
197 if ( HasClientObjectData() )
198 {
199 // destroy the data (due to Robert's idea of using wxList<wxObject>
200 // and not wxList<wxClientData> we can't just say
201 // m_clientList.DeleteContents(TRUE) - this would crash!
202 wxNode *node = m_clientList.First();
203 while ( node )
204 {
205 delete (wxClientData *)node->Data();
206 node = node->Next();
207 }
208 }
209 m_clientList.Clear();
210
211 if ( m_strings )
212 m_strings->Clear();
213 }
214
215 void wxChoice::Delete( int WXUNUSED(n) )
216 {
217 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
218 }
219
220 int wxChoice::FindString( const wxString &string ) const
221 {
222 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
223
224 // If you read this code once and you think you understand
225 // it, then you are very wrong. Robert Roebling.
226
227 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
228 int count = 0;
229 GList *child = menu_shell->children;
230 while (child)
231 {
232 GtkBin *bin = GTK_BIN( child->data );
233 GtkLabel *label = (GtkLabel *) NULL;
234 if (bin->child) label = GTK_LABEL(bin->child);
235 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
236
237 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
238
239 if (string == wxString(label->label,*wxConvCurrent))
240 return count;
241
242 child = child->next;
243 count++;
244 }
245
246 return -1;
247 }
248
249 int wxChoice::GetSelection() const
250 {
251 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
252
253 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
254 int count = 0;
255 GList *child = menu_shell->children;
256 while (child)
257 {
258 GtkBin *bin = GTK_BIN( child->data );
259 if (!bin->child) return count;
260 child = child->next;
261 count++;
262 }
263
264 return -1;
265 }
266
267 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
268 {
269 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
270
271 wxFAIL_MSG(wxT("not implemented"));
272 }
273
274 wxString wxChoice::GetString( int n ) const
275 {
276 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
277
278 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
279 int count = 0;
280 GList *child = menu_shell->children;
281 while (child)
282 {
283 GtkBin *bin = GTK_BIN( child->data );
284 if (count == n)
285 {
286 GtkLabel *label = (GtkLabel *) NULL;
287 if (bin->child) label = GTK_LABEL(bin->child);
288 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
289
290 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
291
292 return wxString(label->label,*wxConvCurrent);
293 }
294 child = child->next;
295 count++;
296 }
297
298 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
299
300 return wxT("");
301 }
302
303 int wxChoice::GetCount() const
304 {
305 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
306
307 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
308 int count = 0;
309 GList *child = menu_shell->children;
310 while (child)
311 {
312 count++;
313 child = child->next;
314 }
315 return count;
316 }
317
318 void wxChoice::SetSelection( int n )
319 {
320 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
321
322 int tmp = n;
323 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
324 }
325
326 void wxChoice::DisableEvents()
327 {
328 /*
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
330 GList *child = menu_shell->children;
331 while (child)
332 {
333 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
334 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
335
336 child = child->next;
337 }
338 */
339 }
340
341 void wxChoice::EnableEvents()
342 {
343 /*
344 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
345 GList *child = menu_shell->children;
346 while (child)
347 {
348 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
349 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
350
351 child = child->next;
352 }
353 */
354 }
355
356 void wxChoice::ApplyWidgetStyle()
357 {
358 SetWidgetStyle();
359
360 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
361
362 gtk_widget_set_style( m_widget, m_widgetStyle );
363 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
364
365 GList *child = menu_shell->children;
366 while (child)
367 {
368 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
369
370 GtkBin *bin = GTK_BIN( child->data );
371 GtkWidget *label = (GtkWidget *) NULL;
372 if (bin->child) label = bin->child;
373 if (!label) label = GTK_BUTTON(m_widget)->child;
374
375 gtk_widget_set_style( label, m_widgetStyle );
376
377 child = child->next;
378 }
379 }
380
381 size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item)
382 {
383 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
384
385 size_t index;
386 if ( m_strings )
387 {
388 // sorted control, need to insert at the correct index
389 index = m_strings->Add(item);
390
391 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
392
393 if ( index )
394 {
395 m_clientList.Insert( m_clientList.Item(index - 1),
396 (wxObject*) NULL );
397 }
398 else
399 {
400 m_clientList.Insert( (wxObject*) NULL );
401 }
402 }
403 else
404 {
405 // normal control, just append
406 gtk_menu_append( GTK_MENU(menu), menu_item );
407
408 m_clientList.Append( (wxObject*) NULL );
409
410 // don't call wxChoice::GetCount() from here because it doesn't work
411 // if we're called from ctor (and GtkMenuShell is still NULL)
412 index = m_clientList.GetCount() - 1;
413 }
414
415 if (GTK_WIDGET_REALIZED(m_widget))
416 {
417 gtk_widget_realize( menu_item );
418 gtk_widget_realize( GTK_BIN(menu_item)->child );
419
420 if (m_widgetStyle) ApplyWidgetStyle();
421 }
422
423 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
424 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
425
426 gtk_widget_show( menu_item );
427
428 // return the index of the item in the control
429 return index;
430 }
431
432 #endif