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