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