]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/choice.cpp
A little clarification
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #include "wx/arrstr.h"
21
22 #include "wx/gtk/private.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 GtkAddHelper(menu, i, 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 InheritAttributes();
115
116 SetBestSize(size);
117
118 Show( TRUE );
119
120 return TRUE;
121 }
122
123 wxChoice::~wxChoice()
124 {
125 Clear();
126
127 delete m_strings;
128 }
129
130 int wxChoice::DoAppend( const wxString &item )
131 {
132 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
133
134 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
135
136 return GtkAddHelper(menu, GetCount(), item);
137 }
138
139 int wxChoice::DoInsert( const wxString &item, int pos )
140 {
141 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
142 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
143
144 if (pos == GetCount())
145 return DoAppend(item);
146
147 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
148
149 return GtkAddHelper(menu, pos, item);
150 }
151
152 void wxChoice::DoSetItemClientData( int n, void* clientData )
153 {
154 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
155
156 wxList::compatibility_iterator node = m_clientList.Item( n );
157 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
158
159 node->SetData( (wxObject*) clientData );
160 }
161
162 void* wxChoice::DoGetItemClientData( int n ) const
163 {
164 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
165
166 wxList::compatibility_iterator node = m_clientList.Item( n );
167 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
168
169 return node->GetData();
170 }
171
172 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
173 {
174 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
175
176 wxList::compatibility_iterator node = m_clientList.Item( n );
177 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
178
179 // wxItemContainer already deletes data for us
180
181 node->SetData( (wxObject*) clientData );
182 }
183
184 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
185 {
186 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
187
188 wxList::compatibility_iterator node = m_clientList.Item( n );
189 wxCHECK_MSG( node, (wxClientData *)NULL,
190 wxT("invalid index in wxChoice::DoGetItemClientObject") );
191
192 return (wxClientData*) node->GetData();
193 }
194
195 void wxChoice::Clear()
196 {
197 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
198
199 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
200 GtkWidget *menu = gtk_menu_new();
201 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
202
203 if ( HasClientObjectData() )
204 {
205 // destroy the data (due to Robert's idea of using wxList<wxObject>
206 // and not wxList<wxClientData> we can't just say
207 // m_clientList.DeleteContents(TRUE) - this would crash!
208 wxList::compatibility_iterator node = m_clientList.GetFirst();
209 while ( node )
210 {
211 delete (wxClientData *)node->GetData();
212 node = node->GetNext();
213 }
214 }
215 m_clientList.Clear();
216
217 if ( m_strings )
218 m_strings->Clear();
219 }
220
221 void wxChoice::Delete( int n )
222 {
223 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
224
225 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
226 // in 2.0), hence this dumb implementation -- still better than nothing
227 int i,
228 count = GetCount();
229
230 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
231
232 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
233 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
234
235 wxList::compatibility_iterator node = m_clientList.GetFirst();
236
237 wxArrayString items;
238 wxArrayPtrVoid itemsData;
239 items.Alloc(count);
240 for ( i = 0; i < count; i++ )
241 {
242 if ( i != n )
243 {
244 items.Add(GetString(i));
245 if ( hasClientData )
246 {
247 // also save the client data
248 itemsData.Add(node->GetData());
249 }
250 }
251 else // need to delete the client object too
252 {
253 if ( hasObjectData )
254 {
255 delete (wxClientData *)node->GetData();
256 }
257 }
258
259 if ( hasClientData )
260 {
261 node = node->GetNext();
262 }
263 }
264
265 if ( hasObjectData )
266 {
267 // prevent Clear() from destroying all client data
268 m_clientDataItemsType = wxClientData_None;
269 }
270
271 Clear();
272
273 for ( i = 0; i < count - 1; i++ )
274 {
275 Append(items[i]);
276
277 if ( hasObjectData )
278 SetClientObject(i, (wxClientData *)itemsData[i]);
279 else if ( hasClientData )
280 SetClientData(i, itemsData[i]);
281 }
282 }
283
284 int wxChoice::FindString( const wxString &string ) const
285 {
286 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
287
288 // If you read this code once and you think you understand
289 // it, then you are very wrong. Robert Roebling.
290
291 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
292 int count = 0;
293 GList *child = menu_shell->children;
294 while (child)
295 {
296 GtkBin *bin = GTK_BIN( child->data );
297 GtkLabel *label = (GtkLabel *) NULL;
298 if (bin->child)
299 label = GTK_LABEL(bin->child);
300 if (!label)
301 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
302
303 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
304
305 #ifdef __WXGTK20__
306 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
307 #else
308 wxString tmp( label->label );
309 #endif
310 if (string == tmp)
311 return count;
312
313 child = child->next;
314 count++;
315 }
316
317 return -1;
318 }
319
320 int wxChoice::GetSelection() const
321 {
322 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
323
324 #ifdef __WXGTK20__
325
326 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
327
328 #else
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
330 int count = 0;
331
332 GList *child = menu_shell->children;
333 while (child)
334 {
335 GtkBin *bin = GTK_BIN( child->data );
336 if (!bin->child) return count;
337 child = child->next;
338 count++;
339 }
340
341 return -1;
342 #endif
343 }
344
345 void wxChoice::SetString( int n, const wxString& str )
346 {
347 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
348
349 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
350 int count = 0;
351 GList *child = menu_shell->children;
352 while (child)
353 {
354 GtkBin *bin = GTK_BIN( child->data );
355 if (count == n)
356 {
357 GtkLabel *label = (GtkLabel *) NULL;
358 if (bin->child)
359 label = GTK_LABEL(bin->child);
360 if (!label)
361 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
362
363 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
364
365 gtk_label_set_text( label, wxGTK_CONV( str ) );
366
367 return;
368 }
369 child = child->next;
370 count++;
371 }
372 }
373
374 wxString wxChoice::GetString( int n ) const
375 {
376 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
377
378 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
379 int count = 0;
380 GList *child = menu_shell->children;
381 while (child)
382 {
383 GtkBin *bin = GTK_BIN( child->data );
384 if (count == n)
385 {
386 GtkLabel *label = (GtkLabel *) NULL;
387 if (bin->child)
388 label = GTK_LABEL(bin->child);
389 if (!label)
390 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
391
392 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
393
394 #ifdef __WXGTK20__
395 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
396 #else
397 return wxString( label->label );
398 #endif
399 }
400 child = child->next;
401 count++;
402 }
403
404 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
405
406 return wxT("");
407 }
408
409 int wxChoice::GetCount() const
410 {
411 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
412
413 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
414 int count = 0;
415 GList *child = menu_shell->children;
416 while (child)
417 {
418 count++;
419 child = child->next;
420 }
421 return count;
422 }
423
424 void wxChoice::SetSelection( int n )
425 {
426 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
427
428 int tmp = n;
429 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
430 }
431
432 void wxChoice::ApplyWidgetStyle()
433 {
434 SetWidgetStyle();
435
436 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
437
438 gtk_widget_set_style( m_widget, m_widgetStyle );
439 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
440
441 GList *child = menu_shell->children;
442 while (child)
443 {
444 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
445
446 GtkBin *bin = GTK_BIN( child->data );
447 GtkWidget *label = (GtkWidget *) NULL;
448 if (bin->child)
449 label = bin->child;
450 if (!label)
451 label = BUTTON_CHILD(m_widget);
452
453 gtk_widget_set_style( label, m_widgetStyle );
454
455 child = child->next;
456 }
457 }
458
459 int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
460 {
461 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
462
463 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
464
465 size_t index;
466 if ( m_strings )
467 {
468 // sorted control, need to insert at the correct index
469 index = m_strings->Add(item);
470
471 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
472
473 if ( index )
474 {
475 m_clientList.Insert( m_clientList.Item(index - 1),
476 (wxObject*) NULL );
477 }
478 else
479 {
480 m_clientList.Insert( (wxObject*) NULL );
481 }
482 }
483 else
484 {
485 // don't call wxChoice::GetCount() from here because it doesn't work
486 // if we're called from ctor (and GtkMenuShell is still NULL)
487
488 // normal control, just append
489 if (pos == (int)m_clientList.GetCount())
490 {
491 gtk_menu_append( GTK_MENU(menu), menu_item );
492 m_clientList.Append( (wxObject*) NULL );
493 index = m_clientList.GetCount() - 1;
494 }
495 else
496 {
497 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
498 m_clientList.Insert( pos, (wxObject*) NULL );
499 index = pos;
500 }
501 }
502
503 if (GTK_WIDGET_REALIZED(m_widget))
504 {
505 gtk_widget_realize( menu_item );
506 gtk_widget_realize( GTK_BIN(menu_item)->child );
507
508 if (m_widgetStyle) ApplyWidgetStyle();
509 }
510
511 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
512 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
513
514 gtk_widget_show( menu_item );
515
516 // return the index of the item in the control
517 return index;
518 }
519
520 wxSize wxChoice::DoGetBestSize() const
521 {
522 wxSize ret( wxControl::DoGetBestSize() );
523
524 // we know better our horizontal extent: it depends on the longest string
525 // we have
526 ret.x = 0;
527 if ( m_widget )
528 {
529 int width;
530 size_t count = GetCount();
531 for ( size_t n = 0; n < count; n++ )
532 {
533 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
534 if ( width > ret.x )
535 ret.x = width;
536 }
537
538 // add extra for the choice "=" button
539
540 // VZ: I don't know how to get the right value, it seems to be in
541 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
542 // to it - maybe we can use gtk_option_menu_size_request() for this
543 // somehow?
544 //
545 // This default value works only for the default GTK+ theme (i.e.
546 // no theme at all) (FIXME)
547 static const int widthChoiceIndicator = 35;
548 ret.x += widthChoiceIndicator;
549 }
550
551 // but not less than the minimal width
552 if ( ret.x < 80 )
553 ret.x = 80;
554
555 ret.y = 16 + GetCharHeight();
556
557 return ret;
558 }
559
560 bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
561 {
562 #ifdef __WXGTK20__
563 return GTK_BUTTON(m_widget)->event_window;
564 #else
565 return (window == m_widget->window);
566 #endif
567 }
568
569
570 #endif // wxUSE_CHOICE
571