]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
fixed notebook tabs refresh when changing them under wxGTK
[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 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
411 // we know better our horizontal extent: it depends on the longest string
412 // we have
413 ret.x = 0;
414 if ( m_widget )
415 {
416 GdkFont *font = m_font.GetInternalFont();
417
418 wxCoord width;
419 size_t count = GetCount();
420 for ( size_t n = 0; n < count; n++ )
421 {
422 width = (wxCoord)gdk_string_width(font, GetString(n).mbc_str());
423 if ( width > ret.x )
424 ret.x = width;
425 }
426
427 // for the choice "=" button
428 ret.x += 20;
429 }
430
431 // but not less than the minimal width
432 if ( ret.x < 80 )
433 ret.x = 80;
434
435 ret.y = 16 + gdk_char_height( m_widget->style->font, 'H' );
436 return ret;
437 }
438
439 #endif