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