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