]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/choice.cpp
GSocket (Generic Socket) Mac OS X mach-o part by Brian Victor
[wxWidgets.git] / src / gtk1 / 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();
e8e24dfa 114 InheritAttributes();
db434467 115
df336b7f 116 SetBestSize(size);
db434467 117
fd0eed64 118 Show( TRUE );
29006414 119
fd0eed64 120 return TRUE;
6de97a3b 121}
29006414 122
fd0eed64
RR
123wxChoice::~wxChoice()
124{
f5e27805 125 Clear();
e01c8145
VZ
126
127 delete m_strings;
fd0eed64
RR
128}
129
9abe166a 130int wxChoice::DoAppend( const wxString &item )
fd0eed64 131{
2ee3ee1b 132 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
29006414 133
fd0eed64 134 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
29006414 135
243dbf1a
VZ
136 return GtkAddHelper(menu, GetCount(), item);
137}
138
139int 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);
fd0eed64 150}
f96aa4d9 151
6c8a980f 152void wxChoice::DoSetItemClientData( int n, void* clientData )
fd0eed64 153{
2ee3ee1b 154 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 155
222ed1d6 156 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 157 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
29006414 158
f5e27805 159 node->SetData( (wxObject*) clientData );
fd0eed64
RR
160}
161
6c8a980f 162void* wxChoice::DoGetItemClientData( int n ) const
fd0eed64 163{
2ee3ee1b 164 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
29006414 165
222ed1d6 166 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 167 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
29006414 168
b1d4dd7a 169 return node->GetData();
6de97a3b 170}
fd0eed64 171
6c8a980f 172void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
fd0eed64 173{
2ee3ee1b 174 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
29006414 175
222ed1d6 176 wxList::compatibility_iterator node = m_clientList.Item( n );
6c8a980f 177 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
29006414 178
edc973b1 179 // wxItemContainer already deletes data for us
29006414 180
fd0eed64
RR
181 node->SetData( (wxObject*) clientData );
182}
183
6c8a980f 184wxClientData* wxChoice::DoGetItemClientObject( int n ) const
fd0eed64 185{
2ee3ee1b 186 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
29006414 187
222ed1d6 188 wxList::compatibility_iterator node = m_clientList.Item( n );
9abe166a 189 wxCHECK_MSG( node, (wxClientData *)NULL,
6c8a980f 190 wxT("invalid index in wxChoice::DoGetItemClientObject") );
29006414 191
b1d4dd7a 192 return (wxClientData*) node->GetData();
fd0eed64 193}
29006414 194
fd0eed64 195void wxChoice::Clear()
c801d85f 196{
223d09f6 197 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 198
fd0eed64
RR
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 );
29006414 202
6c8a980f
VZ
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!
222ed1d6 208 wxList::compatibility_iterator node = m_clientList.GetFirst();
6c8a980f
VZ
209 while ( node )
210 {
b1d4dd7a
RL
211 delete (wxClientData *)node->GetData();
212 node = node->GetNext();
6c8a980f
VZ
213 }
214 }
d6538e2c 215 m_clientList.Clear();
2ee3ee1b
VZ
216
217 if ( m_strings )
218 m_strings->Clear();
6de97a3b 219}
c801d85f 220
645420d8 221void wxChoice::Delete( int n )
2f6407b9 222{
645420d8
VZ
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
e2380ce1 226 // in 2.0), hence this dumb implementation -- still better than nothing
645420d8
VZ
227 int i,
228 count = GetCount();
229
230 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
231
e2380ce1
VZ
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
645420d8 237 wxArrayString items;
e2380ce1 238 wxArrayPtrVoid itemsData;
645420d8
VZ
239 items.Alloc(count);
240 for ( i = 0; i < count; i++ )
241 {
242 if ( i != n )
e2380ce1 243 {
645420d8 244 items.Add(GetString(i));
e2380ce1
VZ
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;
645420d8
VZ
269 }
270
271 Clear();
272
273 for ( i = 0; i < count - 1; i++ )
274 {
275 Append(items[i]);
e2380ce1
VZ
276
277 if ( hasObjectData )
278 SetClientObject(i, (wxClientData *)itemsData[i]);
279 else if ( hasClientData )
f6bc4102 280 SetClientData(i, itemsData[i]);
645420d8 281 }
2f6407b9
RR
282}
283
c801d85f
KB
284int wxChoice::FindString( const wxString &string ) const
285{
223d09f6 286 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
fd0eed64
RR
287
288 // If you read this code once and you think you understand
289 // it, then you are very wrong. Robert Roebling.
29006414 290
fd0eed64
RR
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;
9e691f46
VZ
298 if (bin->child)
299 label = GTK_LABEL(bin->child);
300 if (!label)
301 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 302
223d09f6 303 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
e2380ce1 304
2e1d7104
RR
305#ifdef __WXGTK20__
306 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
307#else
308 wxString tmp( label->label );
309#endif
fab591c5 310 if (string == tmp)
9e691f46 311 return count;
29006414 312
9e691f46
VZ
313 child = child->next;
314 count++;
fd0eed64 315 }
29006414 316
fd0eed64 317 return -1;
6de97a3b 318}
c801d85f 319
9abe166a 320int wxChoice::GetSelection() const
c801d85f 321{
223d09f6 322 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
e2380ce1 323
2e1d7104 324#ifdef __WXGTK20__
fd0eed64 325
2e1d7104 326 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
e2380ce1 327
2e1d7104 328#else
fd0eed64
RR
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
330 int count = 0;
e2380ce1 331
fd0eed64
RR
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 }
29006414 340
fd0eed64 341 return -1;
2e1d7104 342#endif
6de97a3b 343}
c801d85f 344
a912c184 345void wxChoice::SetString( int n, const wxString& str )
6c8a980f
VZ
346{
347 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
348
34b5e560
RR
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 }
6c8a980f
VZ
372}
373
debe6624 374wxString wxChoice::GetString( int n ) const
c801d85f 375{
223d09f6 376 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
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)
c801d85f 382 {
fd0eed64
RR
383 GtkBin *bin = GTK_BIN( child->data );
384 if (count == n)
385 {
386 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
387 if (bin->child)
388 label = GTK_LABEL(bin->child);
389 if (!label)
390 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 391
223d09f6 392 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 393
2e1d7104
RR
394#ifdef __WXGTK20__
395 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
396#else
397 return wxString( label->label );
398#endif
fd0eed64
RR
399 }
400 child = child->next;
401 count++;
6de97a3b 402 }
29006414 403
223d09f6 404 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 405
223d09f6 406 return wxT("");
6de97a3b 407}
c801d85f 408
9abe166a 409int wxChoice::GetCount() const
c801d85f 410{
223d09f6 411 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
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;
6de97a3b 422}
c801d85f 423
debe6624 424void wxChoice::SetSelection( int n )
c801d85f 425{
223d09f6 426 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 427
fd0eed64
RR
428 int tmp = n;
429 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 430}
c801d85f 431
58614078 432void wxChoice::ApplyWidgetStyle()
868a2826 433{
fd0eed64 434 SetWidgetStyle();
29006414 435
fd0eed64 436 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 437
fd0eed64
RR
438 gtk_widget_set_style( m_widget, m_widgetStyle );
439 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 440
fd0eed64
RR
441 GList *child = menu_shell->children;
442 while (child)
443 {
444 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 445
fd0eed64
RR
446 GtkBin *bin = GTK_BIN( child->data );
447 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
448 if (bin->child)
449 label = bin->child;
450 if (!label)
451 label = BUTTON_CHILD(m_widget);
29006414 452
fd0eed64 453 gtk_widget_set_style( label, m_widgetStyle );
29006414 454
fd0eed64
RR
455 child = child->next;
456 }
f96aa4d9
RR
457}
458
243dbf1a 459int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 460{
243dbf1a
VZ
461 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
462
fab591c5 463 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
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 {
6c8a980f 480 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
481 }
482 }
483 else
484 {
243dbf1a
VZ
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
e01c8145 488 // normal control, just append
243dbf1a
VZ
489 if (pos == (int)m_clientList.GetCount())
490 {
e01c8145 491 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 492 m_clientList.Append( (wxObject*) NULL );
11e1c70d 493 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
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 }
e01c8145
VZ
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
f68586e5
VZ
520wxSize wxChoice::DoGetBestSize() const
521{
db434467 522 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
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 {
60d85ccb 529 int width;
29f54b9b
VZ
530 size_t count = GetCount();
531 for ( size_t n = 0; n < count; n++ )
532 {
60d85ccb 533 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
534 if ( width > ret.x )
535 ret.x = width;
536 }
537
df336b7f
VZ
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;
29f54b9b
VZ
549 }
550
551 // but not less than the minimal width
552 if ( ret.x < 80 )
553 ret.x = 80;
554
288059b2 555 ret.y = 16 + GetCharHeight();
9e691f46 556
db434467 557 return ret;
f68586e5
VZ
558}
559
2b904684
RR
560bool 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
df336b7f
VZ
570#endif // wxUSE_CHOICE
571