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