]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choice.cpp
fixed SaveDIB() crash with BW images (patch 1045884)
[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 const wxArrayString& choices,
79 long style, const wxValidator& validator,
80 const wxString &name )
81 {
82 wxCArrayString chs(choices);
83
84 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
85 style, validator, name );
86 }
87
88 bool wxChoice::Create( wxWindow *parent, wxWindowID id,
89 const wxPoint &pos, const wxSize &size,
90 int n, const wxString choices[],
91 long style, const wxValidator& validator, const wxString &name )
92 {
93 m_needParent = TRUE;
94 #if (GTK_MINOR_VERSION > 0)
95 m_acceptsFocus = TRUE;
96 #endif
97
98 if (!PreCreation( parent, pos, size ) ||
99 !CreateBase( parent, id, pos, size, style, validator, name ))
100 {
101 wxFAIL_MSG( wxT("wxChoice creation failed") );
102 return FALSE;
103 }
104
105 m_widget = gtk_option_menu_new();
106
107 if ( style & wxCB_SORT )
108 {
109 // if our m_strings != NULL, DoAppend() will check for it and insert
110 // items in the correct order
111 m_strings = new wxSortedArrayString;
112 }
113
114 // begin with no selection
115 m_selection_hack = wxNOT_FOUND;
116
117 GtkWidget *menu = gtk_menu_new();
118
119 for (int i = 0; i < n; i++)
120 {
121 GtkAddHelper(menu, i, choices[i]);
122 }
123
124 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
125
126 m_parent->DoAddChild( this );
127
128 PostCreation(size);
129 SetBestSize(size); // need this too because this is a wxControlWithItems
130
131 return TRUE;
132 }
133
134 wxChoice::~wxChoice()
135 {
136 Clear();
137
138 delete m_strings;
139 }
140
141 int wxChoice::DoAppend( const wxString &item )
142 {
143 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
144
145 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
146
147 return GtkAddHelper(menu, GetCount(), item);
148 }
149
150 int wxChoice::DoInsert( const wxString &item, int pos )
151 {
152 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") );
153 wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
154
155 if (pos == GetCount())
156 return DoAppend(item);
157
158 GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) );
159
160 // if the item to insert is at or before the selection, and the selection is valid
161 if ((pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
162 {
163 // move the selection forward one
164 m_selection_hack++;
165 }
166
167 return GtkAddHelper(menu, pos, item);
168 }
169
170 void wxChoice::DoSetItemClientData( int n, void* clientData )
171 {
172 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
173
174 wxList::compatibility_iterator node = m_clientList.Item( n );
175 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
176
177 node->SetData( (wxObject*) clientData );
178 }
179
180 void* wxChoice::DoGetItemClientData( int n ) const
181 {
182 wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid choice control") );
183
184 wxList::compatibility_iterator node = m_clientList.Item( n );
185 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
186
187 return node->GetData();
188 }
189
190 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
191 {
192 wxCHECK_RET( m_widget != NULL, wxT("invalid choice control") );
193
194 wxList::compatibility_iterator node = m_clientList.Item( n );
195 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
196
197 // wxItemContainer already deletes data for us
198
199 node->SetData( (wxObject*) clientData );
200 }
201
202 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
203 {
204 wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid choice control") );
205
206 wxList::compatibility_iterator node = m_clientList.Item( n );
207 wxCHECK_MSG( node, (wxClientData *)NULL,
208 wxT("invalid index in wxChoice::DoGetItemClientObject") );
209
210 return (wxClientData*) node->GetData();
211 }
212
213 void wxChoice::Clear()
214 {
215 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
216
217 gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) );
218 GtkWidget *menu = gtk_menu_new();
219 gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu );
220
221 if ( HasClientObjectData() )
222 {
223 // destroy the data (due to Robert's idea of using wxList<wxObject>
224 // and not wxList<wxClientData> we can't just say
225 // m_clientList.DeleteContents(TRUE) - this would crash!
226 wxList::compatibility_iterator node = m_clientList.GetFirst();
227 while ( node )
228 {
229 delete (wxClientData *)node->GetData();
230 node = node->GetNext();
231 }
232 }
233 m_clientList.Clear();
234
235 if ( m_strings )
236 m_strings->Clear();
237
238 // begin with no selection
239 m_selection_hack = wxNOT_FOUND;
240 }
241
242 void wxChoice::Delete( int n )
243 {
244 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
245
246 // VZ: apparently GTK+ doesn't have a built-in function to do it (not even
247 // in 2.0), hence this dumb implementation -- still better than nothing
248 int i,
249 count = GetCount();
250
251 wxCHECK_RET( n >= 0 && n < count, _T("invalid index in wxChoice::Delete") );
252
253 // if the item to delete is before the selection, and the selection is valid
254 if ((n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND))
255 {
256 // move the selection back one
257 m_selection_hack--;
258 }
259 else if (n == m_selection_hack)
260 {
261 // invalidate the selection
262 m_selection_hack = wxNOT_FOUND;
263 }
264
265 const bool hasClientData = m_clientDataItemsType != wxClientData_None;
266 const bool hasObjectData = m_clientDataItemsType == wxClientData_Object;
267
268 wxList::compatibility_iterator node = m_clientList.GetFirst();
269
270 wxArrayString items;
271 wxArrayPtrVoid itemsData;
272 items.Alloc(count);
273 for ( i = 0; i < count; i++ )
274 {
275 if ( i != n )
276 {
277 items.Add(GetString(i));
278 if ( hasClientData )
279 {
280 // also save the client data
281 itemsData.Add(node->GetData());
282 }
283 }
284 else // need to delete the client object too
285 {
286 if ( hasObjectData )
287 {
288 delete (wxClientData *)node->GetData();
289 }
290 }
291
292 if ( hasClientData )
293 {
294 node = node->GetNext();
295 }
296 }
297
298 if ( hasObjectData )
299 {
300 // prevent Clear() from destroying all client data
301 m_clientDataItemsType = wxClientData_None;
302 }
303
304 Clear();
305
306 for ( i = 0; i < count - 1; i++ )
307 {
308 Append(items[i]);
309
310 if ( hasObjectData )
311 SetClientObject(i, (wxClientData *)itemsData[i]);
312 else if ( hasClientData )
313 SetClientData(i, itemsData[i]);
314 }
315 }
316
317 int wxChoice::FindString( const wxString &string ) const
318 {
319 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
320
321 // If you read this code once and you think you understand
322 // it, then you are very wrong. Robert Roebling.
323
324 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
325 int count = 0;
326 GList *child = menu_shell->children;
327 while (child)
328 {
329 GtkBin *bin = GTK_BIN( child->data );
330 GtkLabel *label = (GtkLabel *) NULL;
331 if (bin->child)
332 label = GTK_LABEL(bin->child);
333 if (!label)
334 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
335
336 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
337
338 #ifdef __WXGTK20__
339 wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
340 #else
341 wxString tmp( label->label );
342 #endif
343 if (string == tmp)
344 return count;
345
346 child = child->next;
347 count++;
348 }
349
350 return -1;
351 }
352
353 int wxChoice::GetSelection() const
354 {
355 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice") );
356
357 // this has the same (if not better) behaviour as the following commented code
358 return m_selection_hack;
359
360 /*
361 #ifdef __WXGTK20__
362
363 return gtk_option_menu_get_history( GTK_OPTION_MENU(m_widget) );
364
365 #else
366 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
367 int count = 0;
368
369 GList *child = menu_shell->children;
370 while (child)
371 {
372 GtkBin *bin = GTK_BIN( child->data );
373 if (!bin->child) return count;
374 child = child->next;
375 count++;
376 }
377
378 return -1;
379 #endif
380 */
381 }
382
383 void wxChoice::SetString( int n, const wxString& str )
384 {
385 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
386
387 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
388 int count = 0;
389 GList *child = menu_shell->children;
390 while (child)
391 {
392 GtkBin *bin = GTK_BIN( child->data );
393 if (count == n)
394 {
395 GtkLabel *label = (GtkLabel *) NULL;
396 if (bin->child)
397 label = GTK_LABEL(bin->child);
398 if (!label)
399 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
400
401 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
402
403 gtk_label_set_text( label, wxGTK_CONV( str ) );
404
405 return;
406 }
407 child = child->next;
408 count++;
409 }
410 }
411
412 wxString wxChoice::GetString( int n ) const
413 {
414 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid choice") );
415
416 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
417 int count = 0;
418 GList *child = menu_shell->children;
419 while (child)
420 {
421 GtkBin *bin = GTK_BIN( child->data );
422 if (count == n)
423 {
424 GtkLabel *label = (GtkLabel *) NULL;
425 if (bin->child)
426 label = GTK_LABEL(bin->child);
427 if (!label)
428 label = GTK_LABEL( BUTTON_CHILD(m_widget) );
429
430 wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") );
431
432 #ifdef __WXGTK20__
433 return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) );
434 #else
435 return wxString( label->label );
436 #endif
437 }
438 child = child->next;
439 count++;
440 }
441
442 wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") );
443
444 return wxT("");
445 }
446
447 int wxChoice::GetCount() const
448 {
449 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") );
450
451 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
452 int count = 0;
453 GList *child = menu_shell->children;
454 while (child)
455 {
456 count++;
457 child = child->next;
458 }
459 return count;
460 }
461
462 void wxChoice::SetSelection( int n )
463 {
464 wxCHECK_RET( m_widget != NULL, wxT("invalid choice") );
465
466 int tmp = n;
467 gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp );
468
469 // set the local selection variable manually
470 if ((n >= 0) && (n < GetCount()))
471 {
472 // a valid selection has been made
473 m_selection_hack = n;
474 }
475 else if ((n == wxNOT_FOUND) || (GetCount() == 0))
476 {
477 // invalidates the selection if there are no items
478 // or if it is specifically set to wxNOT_FOUND
479 m_selection_hack = wxNOT_FOUND;
480 }
481 else
482 {
483 // this selects the first item by default if the selection is out of bounds
484 m_selection_hack = 0;
485 }
486 }
487
488 void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style)
489 {
490 GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) );
491
492 gtk_widget_modify_style( m_widget, style );
493 gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style );
494
495 GList *child = menu_shell->children;
496 while (child)
497 {
498 gtk_widget_modify_style( GTK_WIDGET( child->data ), style );
499
500 GtkBin *bin = GTK_BIN( child->data );
501 GtkWidget *label = (GtkWidget *) NULL;
502 if (bin->child)
503 label = bin->child;
504 if (!label)
505 label = BUTTON_CHILD(m_widget);
506
507 gtk_widget_modify_style( label, style );
508
509 child = child->next;
510 }
511 }
512
513 int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item)
514 {
515 wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index"));
516
517 GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) );
518
519 size_t index;
520 if ( m_strings )
521 {
522 // sorted control, need to insert at the correct index
523 index = m_strings->Add(item);
524
525 gtk_menu_insert( GTK_MENU(menu), menu_item, index );
526
527 if ( index )
528 {
529 m_clientList.Insert( m_clientList.Item(index - 1),
530 (wxObject*) NULL );
531 }
532 else
533 {
534 m_clientList.Insert( (wxObject*) NULL );
535 }
536 }
537 else
538 {
539 // don't call wxChoice::GetCount() from here because it doesn't work
540 // if we're called from ctor (and GtkMenuShell is still NULL)
541
542 // normal control, just append
543 if (pos == (int)m_clientList.GetCount())
544 {
545 gtk_menu_append( GTK_MENU(menu), menu_item );
546 m_clientList.Append( (wxObject*) NULL );
547 index = m_clientList.GetCount() - 1;
548 }
549 else
550 {
551 gtk_menu_insert( GTK_MENU(menu), menu_item, pos );
552 m_clientList.Insert( pos, (wxObject*) NULL );
553 index = pos;
554 }
555 }
556
557 if (GTK_WIDGET_REALIZED(m_widget))
558 {
559 gtk_widget_realize( menu_item );
560 gtk_widget_realize( GTK_BIN(menu_item)->child );
561
562 ApplyWidgetStyle();
563 }
564
565 // The best size of a wxChoice should probably
566 // be changed everytime the control has been
567 // changed, but at least after adding an item
568 // it has to change. Adapted from Matt Ownby.
569 InvalidateBestSize();
570
571 gtk_signal_connect( GTK_OBJECT( menu_item ), "activate",
572 GTK_SIGNAL_FUNC(gtk_choice_clicked_callback), (gpointer*)this );
573
574 gtk_widget_show( menu_item );
575
576 // return the index of the item in the control
577 return index;
578 }
579
580 wxSize wxChoice::DoGetBestSize() const
581 {
582 wxSize ret( wxControl::DoGetBestSize() );
583
584 // we know better our horizontal extent: it depends on the longest string
585 // we have
586 ret.x = 0;
587 if ( m_widget )
588 {
589 int width;
590 size_t count = GetCount();
591 for ( size_t n = 0; n < count; n++ )
592 {
593 GetTextExtent( GetString(n), &width, NULL, NULL, NULL );
594 if ( width > ret.x )
595 ret.x = width;
596 }
597
598 // add extra for the choice "=" button
599
600 // VZ: I don't know how to get the right value, it seems to be in
601 // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get
602 // to it - maybe we can use gtk_option_menu_size_request() for this
603 // somehow?
604 //
605 // This default value works only for the default GTK+ theme (i.e.
606 // no theme at all) (FIXME)
607 static const int widthChoiceIndicator = 35;
608 ret.x += widthChoiceIndicator;
609 }
610
611 // but not less than the minimal width
612 if ( ret.x < 80 )
613 ret.x = 80;
614
615 // If this request_size is called with no entries then
616 // the returned height is wrong. Give it a reasonable
617 // default value.
618 if (ret.y <= 18)
619 ret.y = 8 + GetCharHeight();
620
621 CacheBestSize(ret);
622 return ret;
623 }
624
625 bool wxChoice::IsOwnGtkWindow( GdkWindow *window )
626 {
627 #ifdef __WXGTK20__
628 return GTK_BUTTON(m_widget)->event_window;
629 #else
630 return (window == m_widget->window);
631 #endif
632 }
633
634 // static
635 wxVisualAttributes
636 wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
637 {
638 return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new);
639 }
640
641
642 #endif // wxUSE_CHOICE
643