fudged the extra width in GetBestSize() to fit better
[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/defs.h"
16
17 #if wxUSE_CHOICE
18
19 #include "wx/choice.h"
20
21 #include <gdk/gdk.h>
22 #include <gtk/gtk.h>
23
24 //-----------------------------------------------------------------------------
25 // idle system
26 //-----------------------------------------------------------------------------
27
28 extern void wxapp_install_idle_handler();
29 extern bool g_isIdle;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern bool g_blockEventsOnDrag;
36
37 //-----------------------------------------------------------------------------
38 // "activate"
39 //-----------------------------------------------------------------------------
40
41 static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
42 {
43 if (g_isIdle)
44 wxapp_install_idle_handler();
45
46 if (!choice->m_hasVMT) return;
47
48 if (g_blockEventsOnDrag) return;
49
50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
62 choice->GetEventHandler()->ProcessEvent(event);
63 }
64
65 //-----------------------------------------------------------------------------
66 // wxChoice
67 //-----------------------------------------------------------------------------
68
69 IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
70
71 wxChoice::wxChoice()
72 {
73 m_strings = (wxSortedArrayString *)NULL;
74 }
75
76 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
77 const wxPoint &pos, const wxSize &size,
78 int n, const wxString choices[],
79 long style, const wxValidator& validator, const wxString &name )
80 {
81 m_needParent = TRUE;
82 #if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus = TRUE;
84 #endif
85
86 if (!PreCreation( parent, pos, size ) ||
87 !CreateBase( parent, id, pos, size, style, validator, name ))
88 {
89 wxFAIL_MSG( wxT("wxChoice creation failed") );
90 return FALSE;
91 }
92
93 m_widget = gtk_option_menu_new();
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 GtkAppendHelper(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 SetFont( parent->GetFont() );
116
117 SetBestSize(size);
118
119 SetBackgroundColour( parent->GetBackgroundColour() );
120 SetForegroundColour( parent->GetForegroundColour() );
121
122 Show( TRUE );
123
124 return TRUE;
125 }
126
127 wxChoice::~wxChoice()
128 {
129 Clear();
130
131 delete m_strings;
132 }
133
134 int wxChoice::DoAppend( const wxString &item )
135 {
136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
137
138 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
139
140 return GtkAppendHelper(menu, item);
141 }
142
143 void wxChoice::DoSetItemClientData( int n, void* clientData )
144 {
145 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
146
147 wxNode *node = m_clientList.Nth( n );
148 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
149
150 node->SetData( (wxObject*) clientData );
151 }
152
153 void* wxChoice::DoGetItemClientData( int n ) const
154 {
155 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
156
157 wxNode *node = m_clientList.Nth( n );
158 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
159
160 return node->Data();
161 }
162
163 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
164 {
165 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
166
167 wxNode *node = m_clientList.Nth( n );
168 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
169
170 wxClientData *cd = (wxClientData*) node->Data();
171 delete cd;
172
173 node->SetData( (wxObject*) clientData );
174 }
175
176 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
177 {
178 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
179
180 wxNode *node = m_clientList.Nth( n );
181 wxCHECK_MSG( node, (wxClientData *)NULL,
182 wxT("invalid index in wxChoice::DoGetItemClientObject") );
183
184 return (wxClientData*) node->Data();
185 }
186
187 void wxChoice::Clear()
188 {
189 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
190
191 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
192 GtkWidget *menu = gtk_menu_new();
193 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
194
195 if ( HasClientObjectData() )
196 {
197 // destroy the data (due to Robert's idea of using wxList<wxObject>
198 // and not wxList<wxClientData> we can't just say
199 // m_clientList.DeleteContents(TRUE) - this would crash!
200 wxNode *node = m_clientList.First();
201 while ( node )
202 {
203 delete (wxClientData *)node->Data();
204 node = node->Next();
205 }
206 }
207 m_clientList.Clear();
208
209 if ( m_strings )
210 m_strings->Clear();
211 }
212
213 void wxChoice::Delete( int WXUNUSED(n) )
214 {
215 wxFAIL_MSG( wxT("wxChoice:Delete not implemented") );
216 }
217
218 int wxChoice::FindString( const wxString &string ) const
219 {
220 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
221
222 // If you read this code once and you think you understand
223 // it, then you are very wrong. Robert Roebling.
224
225 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
226 int count = 0;
227 GList *child = menu_shell->children;
228 while (child)
229 {
230 GtkBin *bin = GTK_BIN( child->data );
231 GtkLabel *label = (GtkLabel *) NULL;
232 if (bin->child) label = GTK_LABEL(bin->child);
233 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
234
235 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
236
237 if (string == wxString(label->label,*wxConvCurrent))
238 return count;
239
240 child = child->next;
241 count++;
242 }
243
244 return -1;
245 }
246
247 int wxChoice::GetSelection() const
248 {
249 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
250
251 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
252 int count = 0;
253 GList *child = menu_shell->children;
254 while (child)
255 {
256 GtkBin *bin = GTK_BIN( child->data );
257 if (!bin->child) return count;
258 child = child->next;
259 count++;
260 }
261
262 return -1;
263 }
264
265 void wxChoice::SetString( int WXUNUSED(n), const wxString& WXUNUSED(string) )
266 {
267 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
268
269 wxFAIL_MSG(wxT("not implemented"));
270 }
271
272 wxString wxChoice::GetString( int n ) const
273 {
274 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
275
276 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
277 int count = 0;
278 GList *child = menu_shell->children;
279 while (child)
280 {
281 GtkBin *bin = GTK_BIN( child->data );
282 if (count == n)
283 {
284 GtkLabel *label = (GtkLabel *) NULL;
285 if (bin->child) label = GTK_LABEL(bin->child);
286 if (!label) label = GTK_LABEL( GTK_BUTTON(m_widget)->child );
287
288 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
289
290 return wxString(label->label,*wxConvCurrent);
291 }
292 child = child->next;
293 count++;
294 }
295
296 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
297
298 return wxT("");
299 }
300
301 int wxChoice::GetCount() const
302 {
303 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
304
305 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
306 int count = 0;
307 GList *child = menu_shell->children;
308 while (child)
309 {
310 count++;
311 child = child->next;
312 }
313 return count;
314 }
315
316 void wxChoice::SetSelection( int n )
317 {
318 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
319
320 int tmp = n;
321 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
322 }
323
324 void wxChoice::ApplyWidgetStyle()
325 {
326 SetWidgetStyle();
327
328 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
329
330 gtk_widget_set_style( m_widget, m_widgetStyle );
331 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
332
333 GList *child = menu_shell->children;
334 while (child)
335 {
336 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
337
338 GtkBin *bin = GTK_BIN( child->data );
339 GtkWidget *label = (GtkWidget *) NULL;
340 if (bin->child) label = bin->child;
341 if (!label) label = GTK_BUTTON(m_widget)->child;
342
343 gtk_widget_set_style( label, m_widgetStyle );
344
345 child = child->next;
346 }
347 }
348
349 size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item)
350 {
351 GtkWidget *menu_item = gtk_menu_item_new_with_label( item.mbc_str() );
352
353 size_t index;
354 if ( m_strings )
355 {
356 // sorted control, need to insert at the correct index
357 index = m_strings->Add(item);
358
359 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
360
361 if ( index )
362 {
363 m_clientList.Insert( m_clientList.Item(index - 1),
364 (wxObject*) NULL );
365 }
366 else
367 {
368 m_clientList.Insert( (wxObject*) NULL );
369 }
370 }
371 else
372 {
373 // normal control, just append
374 gtk_menu_append( GTK_MENU(menu), menu_item );
375
376 m_clientList.Append( (wxObject*) NULL );
377
378 // don't call wxChoice::GetCount() from here because it doesn't work
379 // if we're called from ctor (and GtkMenuShell is still NULL)
380 index = m_clientList.GetCount() - 1;
381 }
382
383 if (GTK_WIDGET_REALIZED(m_widget))
384 {
385 gtk_widget_realize( menu_item );
386 gtk_widget_realize( GTK_BIN(menu_item)->child );
387
388 if (m_widgetStyle) ApplyWidgetStyle();
389 }
390
391 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
392 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
393
394 gtk_widget_show( menu_item );
395
396 // return the index of the item in the control
397 return index;
398 }
399
400 wxSize wxChoice::DoGetBestSize() const
401 {
402 wxSize ret( wxControl::DoGetBestSize() );
403
404 // we know better our horizontal extent: it depends on the longest string
405 // we have
406 ret.x = 0;
407 if ( m_widget )
408 {
409 GdkFont *font = m_font.GetInternalFont();
410
411 wxCoord width;
412 size_t count = GetCount();
413 for ( size_t n = 0; n < count; n++ )
414 {
415 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
416 if ( width > ret.x )
417 ret.x = width;
418 }
419
420 // add extra for the choice "=" button
421
422 // VZ: I don't know how to get the right value, it seems to be in
423 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
424 // to it - maybe we can use gtk_option_menu_size_request() for this
425 // somehow?
426 //
427 // This default value works only for the default GTK+ theme (i.e.
428 // no theme at all) (FIXME)
429 static const int widthChoiceIndicator = 35;
430 ret.x += widthChoiceIndicator;
431 }
432
433 // but not less than the minimal width
434 if ( ret.x < 80 )
435 ret.x = 80;
436
437 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
438 return ret;
439 }
440
441 #endif // wxUSE_CHOICE
442