1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
23 #include "wx/choice.h"
27 #pragma message disable nosimpint
30 #include <Xm/PushBG.h>
32 #include <Xm/RowColumn.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 #define WIDTH_OVERHEAD 48
40 #define WIDTH_OVERHEAD_SUBTRACT 40
41 #define HEIGHT_OVERHEAD 15
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
45 void wxChoiceCallback (Widget w
, XtPointer clientData
,
58 m_buttonWidget
= (WXWidget
) 0;
59 m_menuWidget
= (WXWidget
) 0;
60 m_formWidget
= (WXWidget
) 0;
63 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
66 int n
, const wxString choices
[],
68 const wxValidator
& validator
,
71 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
74 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
76 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
77 xmRowColumnWidgetClass
, parentWidget
,
80 XmNpacking
, XmPACK_TIGHT
,
81 XmNorientation
, XmHORIZONTAL
,
82 XmNresizeWidth
, False
,
83 XmNresizeHeight
, False
,
86 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
89 * Create the popup menu
91 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
92 "choiceMenu", NULL
, 0);
97 for (i
= 0; i
< n
; i
++)
107 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
108 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
109 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
110 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
111 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
115 m_mainWidget
= m_buttonWidget
;
117 XtManageChild ((Widget
) m_buttonWidget
);
119 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
120 // Some time ago, I reported a problem with wxChoice-items under
121 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
122 // that I have found the code responsible for this behaviour.
123 #if XmVersion >= 1002
125 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
126 // in controls sample.
128 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
129 // XtUnmanageChild (optionLabel);
133 wxSize bestSize
= GetBestSize();
134 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
135 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
137 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
140 ChangeBackgroundColour();
142 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
143 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
148 wxChoice::~wxChoice()
150 // For some reason destroying the menuWidget
151 // can cause crashes on some machines. It will
152 // be deleted implicitly by deleting the parent form
154 // XtDestroyWidget (menuWidget);
158 DetachWidget(GetMainWidget()); // Removes event handlers
159 DetachWidget(m_formWidget
);
161 XtDestroyWidget((Widget
) m_formWidget
);
162 m_formWidget
= (WXWidget
) 0;
164 // Presumably the other widgets have been deleted now, via the form
165 m_mainWidget
= (WXWidget
) 0;
166 m_buttonWidget
= (WXWidget
) 0;
168 if ( HasClientObjectData() )
169 m_clientDataDict
.DestroyData();
172 int wxChoice::DoAppend(const wxString
& item
)
174 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
176 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
178 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
182 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
185 wxDoChangeFont( w
, m_font
);
187 m_widgetArray
.Add(w
);
189 char mnem
= wxFindMnemonic (item
);
191 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
193 XtAddCallback (w
, XmNactivateCallback
,
194 (XtCallbackProc
) wxChoiceCallback
,
197 if (m_noStrings
== 0 && m_buttonWidget
)
199 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
200 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
201 wxXmString
text( item
);
202 XtVaSetValues (label
,
203 XmNlabelString
, text(),
206 m_stringList
.Add(item
);
209 return GetCount() - 1;
212 void wxChoice::Delete(int n
)
214 Widget w
= (Widget
)m_widgetArray
[n
];
215 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
217 m_stringList
.DeleteNode(m_stringList
.Item(n
));
218 m_widgetArray
.RemoveAt(size_t(n
));
219 m_clientDataDict
.Delete(n
, HasClientObjectData());
225 void wxChoice::Clear()
227 m_stringList
.Clear ();
229 for (i
= 0; i
< m_noStrings
; i
++)
231 XtRemoveCallback((Widget
) m_widgetArray
[i
],
232 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
234 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
235 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
237 m_widgetArray
.Clear();
239 XtVaSetValues ((Widget
) m_buttonWidget
,
240 XmNmenuHistory
, (Widget
) NULL
,
243 if ( HasClientObjectData() )
244 m_clientDataDict
.DestroyData();
249 int wxChoice::GetSelection() const
252 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
253 XtVaGetValues (label
,
254 XmNlabelString
, &text
,
256 wxXmString
freeMe(text
);
257 wxString s
= wxXmStringToString( text
);
262 for (wxStringListNode
* node
= m_stringList
.GetFirst ();
263 node
; node
= node
->GetNext ())
265 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
278 void wxChoice::SetSelection(int n
)
282 wxStringListNode
*node
= m_stringList
.Item(n
);
286 Dimension selectionWidth
, selectionHeight
;
288 wxXmString
text( node
->GetData() );
289 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
290 // will remove "soon"
292 XtVaGetValues ((Widget
) m_widgetArray
[n
],
293 XmNwidth
, &selectionWidth
,
294 XmNheight
, &selectionHeight
,
297 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
298 XtVaSetValues (label
,
299 XmNlabelString
, text(),
302 XtVaSetValues ((Widget
) m_buttonWidget
,
303 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
304 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
307 m_inSetValue
= FALSE
;
310 int wxChoice::FindString(const wxString
& s
) const
313 for (wxStringListNode
* node
= m_stringList
.GetFirst();
314 node
; node
= node
->GetNext ())
316 if (s
== node
->GetData())
325 wxString
wxChoice::GetString(int n
) const
327 wxStringListNode
*node
= m_stringList
.Item(n
);
329 return node
->GetData();
331 return wxEmptyString
;
334 void wxChoice::SetColumns(int n
)
338 short numColumns
= n
;
341 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
342 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
343 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
346 int wxChoice::GetColumns(void) const
350 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
354 void wxChoice::SetFocus()
356 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
359 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
361 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
362 bool managed
= XtIsManaged((Widget
) m_formWidget
);
365 XtUnmanageChild ((Widget
) m_formWidget
);
367 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
368 actualHeight
= height
- HEIGHT_OVERHEAD
;
373 for (i
= 0; i
< m_noStrings
; i
++)
374 XtVaSetValues ((Widget
) m_widgetArray
[i
],
375 XmNwidth
, actualWidth
,
377 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
384 for (i
= 0; i
< m_noStrings
; i
++)
385 XtVaSetValues ((Widget
) m_widgetArray
[i
],
386 XmNheight
, actualHeight
,
389 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
394 XtManageChild ((Widget
) m_formWidget
);
395 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
397 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
400 void wxChoice::Command(wxCommandEvent
& event
)
402 SetSelection (event
.GetInt());
403 ProcessCommand (event
);
406 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
408 wxChoice
*item
= (wxChoice
*) clientData
;
411 if (item
->InSetValue())
414 int n
= item
->GetWidgets().Index(w
);
415 if (n
!= wxNOT_FOUND
)
417 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
418 event
.SetEventObject(item
);
419 event
.m_commandInt
= n
;
420 event
.m_commandString
= item
->GetStrings().Item(n
)->GetData();
421 if ( item
->HasClientObjectData() )
422 event
.SetClientObject( item
->GetClientObject(n
) );
423 else if ( item
->HasClientUntypedData() )
424 event
.SetClientData( item
->GetClientData(n
) );
425 item
->ProcessCommand (event
);
430 void wxChoice::ChangeFont(bool keepOriginalSize
)
432 // Note that this causes the widget to be resized back
433 // to its original size! We therefore have to set the size
434 // back again. TODO: a better way in Motif?
437 int width
, height
, width1
, height1
;
438 GetSize(& width
, & height
);
440 WXFontType fontType
=
441 m_font
.GetFontType(XtDisplay((Widget
) m_mainWidget
));
442 WXString fontTag
= wxFont::GetFontTag();
444 XtVaSetValues ((Widget
) m_formWidget
, fontTag
, fontType
, NULL
);
445 XtVaSetValues ((Widget
) m_buttonWidget
, fontTag
, fontType
, NULL
);
447 for( size_t i
= 0; i
< m_noStrings
; ++i
)
448 XtVaSetValues( (Widget
)m_widgetArray
[i
],
452 GetSize(& width1
, & height1
);
453 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
455 SetSize(-1, -1, width
, height
);
460 void wxChoice::ChangeBackgroundColour()
462 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
463 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
464 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
466 for (i
= 0; i
< m_noStrings
; i
++)
467 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
470 void wxChoice::ChangeForegroundColour()
472 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
473 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
474 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
476 for (i
= 0; i
< m_noStrings
; i
++)
477 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
480 int wxChoice::GetCount() const
485 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
487 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, FALSE
);
490 void* wxChoice::DoGetItemClientData(int n
) const
492 return (void*)m_clientDataDict
.Get(n
);
495 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
497 // don't delete, wxItemContainer does that for us
498 m_clientDataDict
.Set(n
, clientData
, FALSE
);
501 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
503 return m_clientDataDict
.Get(n
);
506 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
508 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
511 wxSize
wxChoice::GetItemsSize() const
513 int x
, y
, mx
= 0, my
= 0;
516 GetTextExtent( "|", &x
, &my
);
518 wxStringList::Node
* curr
= m_stringList
.GetFirst();
521 GetTextExtent( curr
->GetData(), &x
, &y
);
524 curr
= curr
->GetNext();
527 return wxSize( mx
, my
);
530 wxSize
wxChoice::DoGetBestSize() const
532 wxSize items
= GetItemsSize();
533 // FIXME arbitrary constants
534 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
535 items
.y
+ HEIGHT_OVERHEAD
);