wxControl and wxChoice derive from the base classes under wxGTK too now
[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 #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_clientDataList.Append( (wxObject*) NULL );
96 m_clientObjectList.Append( (wxObject*) NULL );
97
98 GtkWidget *item = gtk_menu_item_new_with_label( choices[i].mbc_str() );
99 gtk_menu_append( GTK_MENU(menu), item );
100
101 gtk_widget_show( item );
102
103 gtk_signal_connect( GTK_OBJECT( item ), "activate",
104 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
105 }
106 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
107
108 m_parent->DoAddChild( this );
109
110 PostCreation();
111
112 SetBackgroundColour( parent->GetBackgroundColour() );
113 SetForegroundColour( parent->GetForegroundColour() );
114 SetFont( parent->GetFont() );
115
116 Show( TRUE );
117
118 return TRUE;
119 }
120
121 wxChoice::~wxChoice()
122 {
123 Clear();
124 }
125
126 int wxChoice::DoAppend( const wxString &item )
127 {
128 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
129
130 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
131 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
132
133 gtk_menu_append( GTK_MENU(menu), menu_item );
134
135 if (GTK_WIDGET_REALIZED(m_widget))
136 {
137 gtk_widget_realize( menu_item );
138 gtk_widget_realize( GTK_BIN(menu_item)->child );
139
140 if (m_widgetStyle) ApplyWidgetStyle();
141 }
142
143 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
144 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
145
146 gtk_widget_show( menu_item );
147
148 m_clientDataList.Append( (wxObject*) NULL );
149 m_clientObjectList.Append( (wxObject*) NULL );
150
151 // return the index of the item in the control
152 return GetCount();
153 }
154
155 void wxChoice::DoSetClientData( int n, void* clientData )
156 {
157 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
158
159 wxNode *node = m_clientDataList.Nth( n );
160 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientData") );
161
162 node->SetData( (wxObject*) clientData );
163 }
164
165 void* wxChoice::DoGetClientData( int n ) const
166 {
167 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") );
168
169 wxNode *node = m_clientDataList.Nth( n );
170 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetClientData") );
171
172 return node->Data();
173 }
174
175 void wxChoice::DoSetClientObject( int n, wxClientData* clientData )
176 {
177 wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") );
178
179 wxNode *node = m_clientObjectList.Nth( n );
180 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetClientObject") );
181
182 wxClientData *cd = (wxClientData*) node->Data();
183 delete cd;
184
185 node->SetData( (wxObject*) clientData );
186 }
187
188 wxClientData* wxChoice::DoGetClientObject( int n ) const
189 {
190 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid combobox") );
191
192 wxNode *node = m_clientObjectList.Nth( n );
193 wxCHECK_MSG( node, (wxClientData *)NULL,
194 wxT("invalid index in wxChoice::DoGetClientObject") );
195
196 return (wxClientData*) node->Data();
197 }
198
199 void wxChoice::Clear()
200 {
201 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
202
203 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
204 GtkWidget *menu = gtk_menu_new();
205 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
206
207 wxNode *node = m_clientObjectList.First();
208 while (node)
209 {
210 wxClientData *cd = (wxClientData*)node->Data();
211 if (cd) delete cd;
212 node = node->Next();
213 }
214 m_clientObjectList.Clear();
215
216 m_clientDataList.Clear();
217 }
218
219 void wxChoice::Delete( int WXUNUSED(n) )
220 {
221 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
222 }
223
224 int wxChoice::FindString( const wxString &string ) const
225 {
226 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
227
228 // If you read this code once and you think you understand
229 // it, then you are very wrong. Robert Roebling.
230
231 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
232 int count = 0;
233 GList *child = menu_shell->children;
234 while (child)
235 {
236 GtkBin *bin = GTK_BIN( child->data );
237 GtkLabel *label = (GtkLabel *) NULL;
238 if (bin->child) label = GTK_LABEL(bin->child);
239 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
240
241 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
242
243 if (string == wxString(label->label,*wxConvCurrent))
244 return count;
245
246 child = child->next;
247 count++;
248 }
249
250 return -1;
251 }
252
253 int wxChoice::GetSelection() const
254 {
255 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
256
257 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
258 int count = 0;
259 GList *child = menu_shell->children;
260 while (child)
261 {
262 GtkBin *bin = GTK_BIN( child->data );
263 if (!bin->child) return count;
264 child = child->next;
265 count++;
266 }
267
268 return -1;
269 }
270
271 wxString wxChoice::GetString( int n ) const
272 {
273 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
274
275 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
276 int count = 0;
277 GList *child = menu_shell->children;
278 while (child)
279 {
280 GtkBin *bin = GTK_BIN( child->data );
281 if (count == n)
282 {
283 GtkLabel *label = (GtkLabel *) NULL;
284 if (bin->child) label = GTK_LABEL(bin->child);
285 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
286
287 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
288
289 return wxString(label->label,*wxConvCurrent);
290 }
291 child = child->next;
292 count++;
293 }
294
295 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
296
297 return wxT("");
298 }
299
300 int wxChoice::GetCount() const
301 {
302 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
303
304 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
305 int count = 0;
306 GList *child = menu_shell->children;
307 while (child)
308 {
309 count++;
310 child = child->next;
311 }
312 return count;
313 }
314
315 void wxChoice::SetSelection( int n )
316 {
317 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
318
319 int tmp = n;
320 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
321 }
322
323 void wxChoice::DisableEvents()
324 {
325 /*
326 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
327 GList *child = menu_shell->children;
328 while (child)
329 {
330 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
331 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
332
333 child = child->next;
334 }
335 */
336 }
337
338 void wxChoice::EnableEvents()
339 {
340 /*
341 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
342 GList *child = menu_shell->children;
343 while (child)
344 {
345 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
346 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
347
348 child = child->next;
349 }
350 */
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
378 #endif