]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Moved wxCocoa specific doIdle method to application delegate object
[wxWidgets.git] / src / gtk / choice.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
29006414 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "choice.h"
13#endif
14
1e6feb95 15#include "wx/defs.h"
c801d85f 16
ce4169a4
RR
17#if wxUSE_CHOICE
18
1e6feb95 19#include "wx/choice.h"
2da2f941 20#include "wx/arrstr.h"
1e6feb95 21
9e691f46 22#include "wx/gtk/private.h"
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
35extern bool g_blockEventsOnDrag;
36
c801d85f 37//-----------------------------------------------------------------------------
e1e955e1 38// "activate"
c801d85f
KB
39//-----------------------------------------------------------------------------
40
66bd6b93 41static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice )
c801d85f 42{
e01c8145 43 if (g_isIdle)
4dcaf11a 44 wxapp_install_idle_handler();
acfd422a 45
a2053b27 46 if (!choice->m_hasVMT) return;
29006414 47
acfd422a 48 if (g_blockEventsOnDrag) return;
29006414 49
acfd422a 50 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() );
6c8a980f
VZ
51 int n = choice->GetSelection();
52
53 event.SetInt( n );
acfd422a
RR
54 event.SetString( choice->GetStringSelection() );
55 event.SetEventObject(choice);
6c8a980f
VZ
56
57 if ( choice->HasClientObjectData() )
58 event.SetClientObject( choice->GetClientObject(n) );
59 else if ( choice->HasClientUntypedData() )
60 event.SetClientData( choice->GetClientData(n) );
61
acfd422a 62 choice->GetEventHandler()->ProcessEvent(event);
6de97a3b 63}
c801d85f 64
e1e955e1
RR
65//-----------------------------------------------------------------------------
66// wxChoice
c801d85f
KB
67//-----------------------------------------------------------------------------
68
7f4dc78d 69IMPLEMENT_DYNAMIC_CLASS(wxChoice,wxControl)
c801d85f 70
fd0eed64 71wxChoice::wxChoice()
c801d85f 72{
e01c8145 73 m_strings = (wxSortedArrayString *)NULL;
6de97a3b 74}
c801d85f 75
debe6624 76bool wxChoice::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
77 const wxPoint &pos, const wxSize &size,
78 int n, const wxString choices[],
79 long style, const wxValidator& validator, const wxString &name )
c801d85f 80{
fd0eed64 81 m_needParent = TRUE;
034be888
RR
82#if (GTK_MINOR_VERSION > 0)
83 m_acceptsFocus = TRUE;
84#endif
29006414 85
4dcaf11a
RR
86 if (!PreCreation( parent, pos, size ) ||
87 !CreateBase( parent, id, pos, size, style, validator, name ))
88 {
223d09f6 89 wxFAIL_MSG( wxT("wxChoice creation failed") );
e01c8145 90 return FALSE;
4dcaf11a 91 }
6de97a3b 92
fd0eed64 93 m_widget = gtk_option_menu_new();
29006414 94
e01c8145
VZ
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
fd0eed64 102 GtkWidget *menu = gtk_menu_new();
29006414 103
fd0eed64
RR
104 for (int i = 0; i < n; i++)
105 {
243dbf1a 106 GtkAddHelper(menu, i, choices[i]);
fd0eed64 107 }
e01c8145 108
fd0eed64 109 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
29006414 110
f03fc89f 111 m_parent->DoAddChild( this );
29006414 112
fd0eed64 113 PostCreation();
29006414 114
db434467
RR
115 SetFont( parent->GetFont() );
116
df336b7f 117 SetBestSize(size);
db434467 118
fd0eed64
RR
119 SetBackgroundColour( parent->GetBackgroundColour() );
120 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 121
fd0eed64 122 Show( TRUE );
29006414 123
fd0eed64 124 return TRUE;
6de97a3b 125}
29006414 126
fd0eed64
RR
127wxChoice::~wxChoice()
128{
f5e27805 129 Clear();
e01c8145
VZ
130
131 delete m_strings;
fd0eed64
RR
132}
133
9abe166a 134int wxChoice::DoAppend( const wxString &item )
fd0eed64 135{
2ee3ee1b 136 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 137
fd0eed64 138 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 139
243dbf1a
VZ
140 return GtkAddHelper(menu, GetCount(), item);
141}
142
143int 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);
fd0eed64 154}
f96aa4d9 155
6c8a980f 156void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 157{
2ee3ee1b 158 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 159
222ed1d6 160 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 161 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 162
f5e27805 163 node->SetData( (wxObject*) clientData );
fd0eed64
RR
164}
165
6c8a980f 166void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 167{
2ee3ee1b 168 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 169
222ed1d6 170 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 171 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 172
b1d4dd7a 173 return node->GetData();
6de97a3b 174}
fd0eed64 175
6c8a980f 176void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 177{
2ee3ee1b 178 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 179
222ed1d6 180 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 181 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 182
edc973b1 183 // wxItemContainer already deletes data for us
29006414 184
fd0eed64
RR
185 node->SetData( (wxObject*) clientData );
186}
187
6c8a980f 188wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 189{
2ee3ee1b 190 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 191
222ed1d6 192 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 193 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 194 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 195
b1d4dd7a 196 return (wxClientData*) node->GetData();
fd0eed64 197}
29006414 198
fd0eed64 199void wxChoice::Clear()
c801d85f 200{
223d09f6 201 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 202
fd0eed64
RR
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 );
29006414 206
6c8a980f
VZ
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!
222ed1d6 212 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
213 while ( node )
214 {
b1d4dd7a
RL
215 delete (wxClientData *)node->GetData();
216 node = node->GetNext();
6c8a980f
VZ
217 }
218 }
d6538e2c 219 m_clientList.Clear();
2ee3ee1b
VZ
220
221 if ( m_strings )
222 m_strings->Clear();
6de97a3b 223}
c801d85f 224
645420d8 225void wxChoice::Delete( int n )
2f6407b9 226{
645420d8
VZ
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
e2380ce1 230 // in 2.0), hence this dumb implementation -- still better than nothing
645420d8
VZ
231 int i,
232 count = GetCount();
233
234 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
235
e2380ce1
VZ
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
645420d8 241 wxArrayString items;
e2380ce1 242 wxArrayPtrVoid itemsData;
645420d8
VZ
243 items.Alloc(count);
244 for ( i = 0; i < count; i++ )
245 {
246 if ( i != n )
e2380ce1 247 {
645420d8 248 items.Add(GetString(i));
e2380ce1
VZ
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;
645420d8
VZ
273 }
274
275 Clear();
276
277 for ( i = 0; i < count - 1; i++ )
278 {
279 Append(items[i]);
e2380ce1
VZ
280
281 if ( hasObjectData )
282 SetClientObject(i, (wxClientData *)itemsData[i]);
283 else if ( hasClientData )
f6bc4102 284 SetClientData(i, itemsData[i]);
645420d8 285 }
2f6407b9
RR
286}
287
c801d85f
KB
288int wxChoice::FindString( const wxString &string ) const
289{
223d09f6 290 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
291
292 // If you read this code once and you think you understand
293 // it, then you are very wrong. Robert Roebling.
29006414 294
fd0eed64
RR
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;
9e691f46
VZ
302 if (bin->child)
303 label = GTK_LABEL(bin->child);
304 if (!label)
305 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 306
223d09f6 307 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 308
2e1d7104
RR
309#ifdef __WXGTK20__
310 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
311#else
312 wxString tmp( label->label );
313#endif
fab591c5 314 if (string == tmp)
9e691f46 315 return count;
29006414 316
9e691f46
VZ
317 child = child->next;
318 count++;
fd0eed64 319 }
29006414 320
fd0eed64 321 return -1;
6de97a3b 322}
c801d85f 323
9abe166a 324int wxChoice::GetSelection() const
c801d85f 325{
223d09f6 326 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
e2380ce1 327
2e1d7104 328#ifdef __WXGTK20__
fd0eed64 329
2e1d7104 330 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
e2380ce1 331
2e1d7104 332#else
fd0eed64
RR
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
334 int count = 0;
e2380ce1 335
fd0eed64
RR
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 }
29006414 344
fd0eed64 345 return -1;
2e1d7104 346#endif
6de97a3b 347}
c801d85f 348
6c8a980f
VZ
349void 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
debe6624 356wxString wxChoice::GetString( int n ) const
c801d85f 357{
223d09f6 358 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
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)
c801d85f 364 {
fd0eed64
RR
365 GtkBin *bin = GTK_BIN( child->data );
366 if (count == n)
367 {
368 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
369 if (bin->child)
370 label = GTK_LABEL(bin->child);
371 if (!label)
372 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 373
223d09f6 374 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 375
2e1d7104
RR
376#ifdef __WXGTK20__
377 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
378#else
379 return wxString( label->label );
380#endif
fd0eed64
RR
381 }
382 child = child->next;
383 count++;
6de97a3b 384 }
29006414 385
223d09f6 386 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 387
223d09f6 388 return wxT("");
6de97a3b 389}
c801d85f 390
9abe166a 391int wxChoice::GetCount() const
c801d85f 392{
223d09f6 393 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
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;
6de97a3b 404}
c801d85f 405
debe6624 406void wxChoice::SetSelection( int n )
c801d85f 407{
223d09f6 408 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 409
fd0eed64
RR
410 int tmp = n;
411 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 412}
c801d85f 413
58614078 414void wxChoice::ApplyWidgetStyle()
868a2826 415{
fd0eed64 416 SetWidgetStyle();
29006414 417
fd0eed64 418 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 419
fd0eed64
RR
420 gtk_widget_set_style( m_widget, m_widgetStyle );
421 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 422
fd0eed64
RR
423 GList *child = menu_shell->children;
424 while (child)
425 {
426 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 427
fd0eed64
RR
428 GtkBin *bin = GTK_BIN( child->data );
429 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
430 if (bin->child)
431 label = bin->child;
432 if (!label)
433 label = BUTTON_CHILD(m_widget);
29006414 434
fd0eed64 435 gtk_widget_set_style( label, m_widgetStyle );
29006414 436
fd0eed64
RR
437 child = child->next;
438 }
f96aa4d9
RR
439}
440
243dbf1a 441int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 442{
243dbf1a
VZ
443 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
444
fab591c5 445 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
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 {
6c8a980f 462 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
463 }
464 }
465 else
466 {
243dbf1a
VZ
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
e01c8145 470 // normal control, just append
243dbf1a
VZ
471 if (pos == (int)m_clientList.GetCount())
472 {
e01c8145 473 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 474 m_clientList.Append( (wxObject*) NULL );
11e1c70d 475 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
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 }
e01c8145
VZ
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
f68586e5
VZ
502wxSize wxChoice::DoGetBestSize() const
503{
db434467 504 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
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 {
60d85ccb 511 int width;
29f54b9b
VZ
512 size_t count = GetCount();
513 for ( size_t n = 0; n < count; n++ )
514 {
60d85ccb 515 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
516 if ( width > ret.x )
517 ret.x = width;
518 }
519
df336b7f
VZ
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;
29f54b9b
VZ
531 }
532
533 // but not less than the minimal width
534 if ( ret.x < 80 )
535 ret.x = 80;
536
288059b2 537 ret.y = 16 + GetCharHeight();
9e691f46 538
db434467 539 return ret;
f68586e5
VZ
540}
541
2b904684
RR
542bool 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
df336b7f
VZ
552#endif // wxUSE_CHOICE
553