]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
Added wxMenu::UpdateUI to wxMSW, wxGTK, wxMotif, wxStubs; rearranged/debugged
[wxWidgets.git] / src / gtk1 / 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 #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
100 Show( TRUE );
101
102 return TRUE;
103 }
104
105 wxChoice::~wxChoice()
106 {
107 Clear();
108 }
109
110 void wxChoice::AppendCommon( const wxString &item )
111 {
112 wxCHECK_RET( m_widget != NULL, "invalid choice" );
113
114 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
115 GtkWidget *menu_item = gtk_menu_item_new_with_label( item );
116
117 gtk_menu_append( GTK_MENU(menu), menu_item );
118
119 gtk_widget_realize( menu_item );
120 gtk_widget_realize( GTK_BIN(menu_item)->child );
121
122 if (m_widgetStyle) ApplyWidgetStyle();
123
124 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
125 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
126
127 gtk_widget_show( menu_item );
128 }
129
130 void wxChoice::Append( const wxString &item )
131 {
132 m_clientDataList.Append( (wxObject*) NULL );
133 m_clientObjectList.Append( (wxObject*) NULL );
134
135 AppendCommon( item );
136 }
137
138 void wxChoice::Append( const wxString &item, void *clientData )
139 {
140 m_clientDataList.Append( (wxObject*) clientData );
141 m_clientObjectList.Append( (wxObject*) NULL );
142
143 AppendCommon( item );
144 }
145
146 void wxChoice::Append( const wxString &item, wxClientData *clientData )
147 {
148 m_clientObjectList.Append( (wxObject*) clientData );
149 m_clientDataList.Append( (wxObject*) NULL );
150
151 AppendCommon( item );
152 }
153
154 void wxChoice::SetClientData( int n, void* clientData )
155 {
156 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
157
158 wxNode *node = m_clientDataList.Nth( n );
159 if (!node) return;
160
161 node->SetData( (wxObject*) clientData );
162 }
163
164 void* wxChoice::GetClientData( int n )
165 {
166 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
167
168 wxNode *node = m_clientDataList.Nth( n );
169 if (!node) return NULL;
170
171 return node->Data();
172 }
173
174 void wxChoice::SetClientObject( int n, wxClientData* clientData )
175 {
176 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
177
178 wxNode *node = m_clientObjectList.Nth( n );
179 if (!node) return;
180
181 wxClientData *cd = (wxClientData*) node->Data();
182 if (cd) delete cd;
183
184 node->SetData( (wxObject*) clientData );
185 }
186
187 wxClientData* wxChoice::GetClientObject( int n )
188 {
189 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, "invalid combobox" );
190
191 wxNode *node = m_clientObjectList.Nth( n );
192 if (!node) return (wxClientData*) NULL;
193
194 return (wxClientData*) node->Data();
195 }
196
197 void wxChoice::Clear()
198 {
199 wxCHECK_RET( m_widget != NULL, "invalid choice" );
200
201 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
202 GtkWidget *menu = gtk_menu_new();
203 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
204
205 wxNode *node = m_clientObjectList.First();
206 while (node)
207 {
208 wxClientData *cd = (wxClientData*)node->Data();
209 if (cd) delete cd;
210 node = node->Next();
211 }
212 m_clientObjectList.Clear();
213
214 m_clientDataList.Clear();
215 }
216
217 void wxChoice::Delete( int WXUNUSED(n) )
218 {
219 wxFAIL_MSG( "wxChoice:Delete not implemented" );
220 }
221
222 int wxChoice::FindString( const wxString &string ) const
223 {
224 wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" );
225
226 // If you read this code once and you think you understand
227 // it, then you are very wrong. Robert Roebling.
228
229 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
230 int count = 0;
231 GList *child = menu_shell->children;
232 while (child)
233 {
234 GtkBin *bin = GTK_BIN( child->data );
235 GtkLabel *label = (GtkLabel *) NULL;
236 if (bin->child) label = GTK_LABEL(bin->child);
237 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
238
239 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
240
241 if (string == label->label) return count;
242 child = child->next;
243 count++;
244 }
245
246 wxFAIL_MSG( "wxChoice: string not found" );
247
248 return -1;
249 }
250
251 int wxChoice::GetColumns() const
252 {
253 return 1;
254 }
255
256 int wxChoice::GetSelection()
257 {
258 wxCHECK_MSG( m_widget != NULL, -1, "invalid choice" );
259
260 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
261 int count = 0;
262 GList *child = menu_shell->children;
263 while (child)
264 {
265 GtkBin *bin = GTK_BIN( child->data );
266 if (!bin->child) return count;
267 child = child->next;
268 count++;
269 }
270
271 wxFAIL_MSG( "wxChoice: no selection" );
272
273 return -1;
274 }
275
276 wxString wxChoice::GetString( int n ) const
277 {
278 wxCHECK_MSG( m_widget != NULL, "", "invalid choice" );
279
280 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
281 int count = 0;
282 GList *child = menu_shell->children;
283 while (child)
284 {
285 GtkBin *bin = GTK_BIN( child->data );
286 if (count == n)
287 {
288 GtkLabel *label = (GtkLabel *) NULL;
289 if (bin->child) label = GTK_LABEL(bin->child);
290 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
291
292 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
293
294 return label->label;
295 }
296 child = child->next;
297 count++;
298 }
299
300 wxFAIL_MSG( "wxChoice: string not found" );
301
302 return "";
303 }
304
305 wxString wxChoice::GetStringSelection() const
306 {
307 wxCHECK_MSG( m_widget != NULL, "", "invalid choice" );
308
309 GtkLabel *label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
310
311 wxASSERT_MSG( label != NULL , "wxChoice: invalid label" );
312
313 return label->label;
314 }
315
316 int wxChoice::Number() const
317 {
318 wxCHECK_MSG( m_widget != NULL, 0, "invalid choice" );
319
320 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
321 int count = 0;
322 GList *child = menu_shell->children;
323 while (child)
324 {
325 count++;
326 child = child->next;
327 }
328 return count;
329 }
330
331 void wxChoice::SetColumns( int WXUNUSED(n) )
332 {
333 }
334
335 void wxChoice::SetSelection( int n )
336 {
337 wxCHECK_RET( m_widget != NULL, "invalid choice" );
338
339 int tmp = n;
340 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
341
342 gtk_choice_clicked_callback( (GtkWidget *) NULL, this );
343 }
344
345 void wxChoice::SetStringSelection( const wxString &string )
346 {
347 wxCHECK_RET( m_widget != NULL, "invalid choice" );
348
349 int n = FindString( string );
350 if (n != -1) SetSelection( n );
351 }
352
353 void wxChoice::ApplyWidgetStyle()
354 {
355 SetWidgetStyle();
356
357 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
358
359 gtk_widget_set_style( m_widget, m_widgetStyle );
360 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
361
362 GList *child = menu_shell->children;
363 while (child)
364 {
365 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
366
367 GtkBin *bin = GTK_BIN( child->data );
368 GtkWidget *label = (GtkWidget *) NULL;
369 if (bin->child) label = bin->child;
370 if (!label) label = GTK_BUTTON(m_widget)->child;
371
372 gtk_widget_set_style( label, m_widgetStyle );
373
374 child = child->next;
375 }
376 }
377