wxWindow::GetBestSize() added
[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 int n = choice->GetSelection();
50
51 event.SetInt( n );
52 event.SetString( choice->GetStringSelection() );
53 event.SetEventObject(choice);
54
55 if ( choice->HasClientObjectData() )
56 event.SetClientObject( choice->GetClientObject(n) );
57 else if ( choice->HasClientUntypedData() )
58 event.SetClientData( choice->GetClientData(n) );
59
60 choice->GetEventHandler()->ProcessEvent(event);
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxChoice
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
68
69 wxChoice::wxChoice()
70 {
71 m_strings = (wxSortedArrayString *)NULL;
72 }
73
74 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
75 const wxPoint &pos, const wxSize &size,
76 int n, const wxString choices[],
77 long style, const wxValidator& validator, const wxString &name )
78 {
79 m_needParent = TRUE;
80 #if (GTK_MINOR_VERSION > 0)
81 m_acceptsFocus = TRUE;
82 #endif
83
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
87 wxFAIL_MSG( wxT("wxChoice creation failed") );
88 return FALSE;
89 }
90
91 m_widget = gtk_option_menu_new();
92
93 SetSizeOrDefault( size );
94
95 if ( style & wxCB_SORT )
96 {
97 // if our m_strings != NULL, DoAppend() will check for it and insert
98 // items in the correct order
99 m_strings = new wxSortedArrayString;
100 }
101
102 GtkWidget *menu = gtk_menu_new();
103
104 for (int i = 0; i < n; i++)
105 {
106 AppendHelper(menu, choices[i]);
107 }
108
109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
110
111 m_parent->DoAddChild( this );
112
113 PostCreation();
114
115 SetBackgroundColour( parent->GetBackgroundColour() );
116 SetForegroundColour( parent->GetForegroundColour() );
117 SetFont( parent->GetFont() );
118
119 Show( TRUE );
120
121 return TRUE;
122 }
123
124 wxChoice::~wxChoice()
125 {
126 Clear();
127
128 delete m_strings;
129 }
130
131 int wxChoice::DoAppend( const wxString &item )
132 {
133 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
134
135 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
136
137 return AppendHelper(menu, item);
138 }
139
140 void wxChoice::DoSetItemClientData( int n, void* clientData )
141 {
142 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
143
144 wxNode *node = m_clientList.Nth( n );
145 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
146
147 node->SetData( (wxObject*) clientData );
148 }
149
150 void* wxChoice::DoGetItemClientData( int n ) const
151 {
152 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
153
154 wxNode *node = m_clientList.Nth( n );
155 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
156
157 return node->Data();
158 }
159
160 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
161 {
162 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
163
164 wxNode *node = m_clientList.Nth( n );
165 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
166
167 wxClientData *cd = (wxClientData*) node->Data();
168 delete cd;
169
170 node->SetData( (wxObject*) clientData );
171 }
172
173 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
174 {
175 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
176
177 wxNode *node = m_clientList.Nth( n );
178 wxCHECK_MSG( node, (wxClientData *)NULL,
179 wxT("invalid index in wxChoice::DoGetItemClientObject") );
180
181 return (wxClientData*) node->Data();
182 }
183
184 void wxChoice::Clear()
185 {
186 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
187
188 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
189 GtkWidget *menu = gtk_menu_new();
190 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
191
192 if ( HasClientObjectData() )
193 {
194 // destroy the data (due to Robert's idea of using wxList<wxObject>
195 // and not wxList<wxClientData> we can't just say
196 // m_clientList.DeleteContents(TRUE) - this would crash!
197 wxNode *node = m_clientList.First();
198 while ( node )
199 {
200 delete (wxClientData *)node->Data();
201 node = node->Next();
202 }
203 }
204 m_clientList.Clear();
205
206 if ( m_strings )
207 m_strings->Clear();
208 }
209
210 void wxChoice::Delete( int WXUNUSED(n) )
211 {
212 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
213 }
214
215 int wxChoice::FindString( const wxString &string ) const
216 {
217 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
218
219 // If you read this code once and you think you understand
220 // it, then you are very wrong. Robert Roebling.
221
222 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
223 int count = 0;
224 GList *child = menu_shell->children;
225 while (child)
226 {
227 GtkBin *bin = GTK_BIN( child->data );
228 GtkLabel *label = (GtkLabel *) NULL;
229 if (bin->child) label = GTK_LABEL(bin->child);
230 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
231
232 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
233
234 if (string == wxString(label->label,*wxConvCurrent))
235 return count;
236
237 child = child->next;
238 count++;
239 }
240
241 return -1;
242 }
243
244 int wxChoice::GetSelection() const
245 {
246 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
247
248 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
249 int count = 0;
250 GList *child = menu_shell->children;
251 while (child)
252 {
253 GtkBin *bin = GTK_BIN( child->data );
254 if (!bin->child) return count;
255 child = child->next;
256 count++;
257 }
258
259 return -1;
260 }
261
262 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
263 {
264 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
265
266 wxFAIL_MSG(wxT("not implemented"));
267 }
268
269 wxString wxChoice::GetString( int n ) const
270 {
271 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
272
273 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
274 int count = 0;
275 GList *child = menu_shell->children;
276 while (child)
277 {
278 GtkBin *bin = GTK_BIN( child->data );
279 if (count == n)
280 {
281 GtkLabel *label = (GtkLabel *) NULL;
282 if (bin->child) label = GTK_LABEL(bin->child);
283 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
284
285 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
286
287 return wxString(label->label,*wxConvCurrent);
288 }
289 child = child->next;
290 count++;
291 }
292
293 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
294
295 return wxT("");
296 }
297
298 int wxChoice::GetCount() const
299 {
300 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
301
302 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
303 int count = 0;
304 GList *child = menu_shell->children;
305 while (child)
306 {
307 count++;
308 child = child->next;
309 }
310 return count;
311 }
312
313 void wxChoice::SetSelection( int n )
314 {
315 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
316
317 int tmp = n;
318 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
319 }
320
321 void wxChoice::DisableEvents()
322 {
323 /*
324 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
325 GList *child = menu_shell->children;
326 while (child)
327 {
328 gtk_signal_disconnect_by_func( GTK_OBJECT( child->data ),
329 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
330
331 child = child->next;
332 }
333 */
334 }
335
336 void wxChoice::EnableEvents()
337 {
338 /*
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
340 GList *child = menu_shell->children;
341 while (child)
342 {
343 gtk_signal_connect( GTK_OBJECT( child->data ), "activate",
344 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
345
346 child = child->next;
347 }
348 */
349 }
350
351 void wxChoice::ApplyWidgetStyle()
352 {
353 SetWidgetStyle();
354
355 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
356
357 gtk_widget_set_style( m_widget, m_widgetStyle );
358 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
359
360 GList *child = menu_shell->children;
361 while (child)
362 {
363 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
364
365 GtkBin *bin = GTK_BIN( child->data );
366 GtkWidget *label = (GtkWidget *) NULL;
367 if (bin->child) label = bin->child;
368 if (!label) label = GTK_BUTTON(m_widget)->child;
369
370 gtk_widget_set_style( label, m_widgetStyle );
371
372 child = child->next;
373 }
374 }
375
376 size_t wxChoice::AppendHelper(GtkWidget *menu, const wxString& item)
377 {
378 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
379
380 size_t index;
381 if ( m_strings )
382 {
383 // sorted control, need to insert at the correct index
384 index = m_strings->Add(item);
385
386 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
387
388 if ( index )
389 {
390 m_clientList.Insert( m_clientList.Item(index - 1),
391 (wxObject*) NULL );
392 }
393 else
394 {
395 m_clientList.Insert( (wxObject*) NULL );
396 }
397 }
398 else
399 {
400 // normal control, just append
401 gtk_menu_append( GTK_MENU(menu), menu_item );
402
403 m_clientList.Append( (wxObject*) NULL );
404
405 // don't call wxChoice::GetCount() from here because it doesn't work
406 // if we're called from ctor (and GtkMenuShell is still NULL)
407 index = m_clientList.GetCount() - 1;
408 }
409
410 if (GTK_WIDGET_REALIZED(m_widget))
411 {
412 gtk_widget_realize( menu_item );
413 gtk_widget_realize( GTK_BIN(menu_item)->child );
414
415 if (m_widgetStyle) ApplyWidgetStyle();
416 }
417
418 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
419 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
420
421 gtk_widget_show( menu_item );
422
423 // return the index of the item in the control
424 return index;
425 }
426
427 wxSize wxChoice::DoGetBestSize() const
428 {
429 return wxSize(80, 26);
430 }
431
432 #endif