]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/choice.cpp
Fixed update rectangle passed to WinInvalidateRect in wxWindowOS2::Refresh().
[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
a912c184 349void wxChoice::SetString( int n, const wxString& str )
6c8a980f
VZ
350{
351 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
352
34b5e560
RR
353 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
354 int count = 0;
355 GList *child = menu_shell->children;
356 while (child)
357 {
358 GtkBin *bin = GTK_BIN( child->data );
359 if (count == n)
360 {
361 GtkLabel *label = (GtkLabel *) NULL;
362 if (bin->child)
363 label = GTK_LABEL(bin->child);
364 if (!label)
365 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
366
367 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
368
369 gtk_label_set_text( label, wxGTK_CONV( str ) );
370
371 return;
372 }
373 child = child->next;
374 count++;
375 }
6c8a980f
VZ
376}
377
debe6624 378wxString wxChoice::GetString( int n ) const
c801d85f 379{
223d09f6 380 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
fd0eed64
RR
381
382 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
383 int count = 0;
384 GList *child = menu_shell->children;
385 while (child)
c801d85f 386 {
fd0eed64
RR
387 GtkBin *bin = GTK_BIN( child->data );
388 if (count == n)
389 {
390 GtkLabel *label = (GtkLabel *) NULL;
9e691f46
VZ
391 if (bin->child)
392 label = GTK_LABEL(bin->child);
393 if (!label)
394 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
29006414 395
223d09f6 396 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
29006414 397
2e1d7104
RR
398#ifdef __WXGTK20__
399 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
400#else
401 return wxString( label->label );
402#endif
fd0eed64
RR
403 }
404 child = child->next;
405 count++;
6de97a3b 406 }
29006414 407
223d09f6 408 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
29006414 409
223d09f6 410 return wxT("");
6de97a3b 411}
c801d85f 412
9abe166a 413int wxChoice::GetCount() const
c801d85f 414{
223d09f6 415 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
fd0eed64
RR
416
417 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
418 int count = 0;
419 GList *child = menu_shell->children;
420 while (child)
421 {
422 count++;
423 child = child->next;
424 }
425 return count;
6de97a3b 426}
c801d85f 427
debe6624 428void wxChoice::SetSelection( int n )
c801d85f 429{
223d09f6 430 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
f96aa4d9 431
fd0eed64
RR
432 int tmp = n;
433 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
6de97a3b 434}
c801d85f 435
58614078 436void wxChoice::ApplyWidgetStyle()
868a2826 437{
fd0eed64 438 SetWidgetStyle();
29006414 439
fd0eed64 440 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
29006414 441
fd0eed64
RR
442 gtk_widget_set_style( m_widget, m_widgetStyle );
443 gtk_widget_set_style( GTK_WIDGET( menu_shell ), m_widgetStyle );
29006414 444
fd0eed64
RR
445 GList *child = menu_shell->children;
446 while (child)
447 {
448 gtk_widget_set_style( GTK_WIDGET( child->data ), m_widgetStyle );
29006414 449
fd0eed64
RR
450 GtkBin *bin = GTK_BIN( child->data );
451 GtkWidget *label = (GtkWidget *) NULL;
9e691f46
VZ
452 if (bin->child)
453 label = bin->child;
454 if (!label)
455 label = BUTTON_CHILD(m_widget);
29006414 456
fd0eed64 457 gtk_widget_set_style( label, m_widgetStyle );
29006414 458
fd0eed64
RR
459 child = child->next;
460 }
f96aa4d9
RR
461}
462
243dbf1a 463int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
e01c8145 464{
243dbf1a
VZ
465 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
466
fab591c5 467 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
e01c8145
VZ
468
469 size_t index;
470 if ( m_strings )
471 {
472 // sorted control, need to insert at the correct index
473 index = m_strings->Add(item);
474
475 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
476
477 if ( index )
478 {
479 m_clientList.Insert( m_clientList.Item(index - 1),
480 (wxObject*) NULL );
481 }
482 else
483 {
6c8a980f 484 m_clientList.Insert( (wxObject*) NULL );
e01c8145
VZ
485 }
486 }
487 else
488 {
243dbf1a
VZ
489 // don't call wxChoice::GetCount() from here because it doesn't work
490 // if we're called from ctor (and GtkMenuShell is still NULL)
491
e01c8145 492 // normal control, just append
243dbf1a
VZ
493 if (pos == (int)m_clientList.GetCount())
494 {
e01c8145 495 gtk_menu_append( GTK_MENU(menu), menu_item );
e01c8145 496 m_clientList.Append( (wxObject*) NULL );
11e1c70d 497 index = m_clientList.GetCount() - 1;
243dbf1a
VZ
498 }
499 else
500 {
501 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
502 m_clientList.Insert( pos, (wxObject*) NULL );
503 index = pos;
504 }
e01c8145
VZ
505 }
506
507 if (GTK_WIDGET_REALIZED(m_widget))
508 {
509 gtk_widget_realize( menu_item );
510 gtk_widget_realize( GTK_BIN(menu_item)->child );
511
512 if (m_widgetStyle) ApplyWidgetStyle();
513 }
514
515 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
516 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
517
518 gtk_widget_show( menu_item );
519
520 // return the index of the item in the control
521 return index;
522}
523
f68586e5
VZ
524wxSize wxChoice::DoGetBestSize() const
525{
db434467 526 wxSize ret( wxControl::DoGetBestSize() );
29f54b9b
VZ
527
528 // we know better our horizontal extent: it depends on the longest string
529 // we have
530 ret.x = 0;
531 if ( m_widget )
532 {
60d85ccb 533 int width;
29f54b9b
VZ
534 size_t count = GetCount();
535 for ( size_t n = 0; n < count; n++ )
536 {
60d85ccb 537 GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font );
29f54b9b
VZ
538 if ( width > ret.x )
539 ret.x = width;
540 }
541
df336b7f
VZ
542 // add extra for the choice "=" button
543
544 // VZ: I don't know how to get the right value, it seems to be in
545 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
546 // to it - maybe we can use gtk_option_menu_size_request() for this
547 // somehow?
548 //
549 // This default value works only for the default GTK+ theme (i.e.
550 // no theme at all) (FIXME)
551 static const int widthChoiceIndicator = 35;
552 ret.x += widthChoiceIndicator;
29f54b9b
VZ
553 }
554
555 // but not less than the minimal width
556 if ( ret.x < 80 )
557 ret.x = 80;
558
288059b2 559 ret.y = 16 + GetCharHeight();
9e691f46 560
db434467 561 return ret;
f68586e5
VZ
562}
563
2b904684
RR
564bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
565{
566#ifdef __WXGTK20__
567 return GTK_BUTTON(m_widget)->event_window;
568#else
569 return (window == m_widget->window);
570#endif
571}
572
573
df336b7f
VZ
574#endif // wxUSE_CHOICE
575