1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "choice.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
21 #define XtParent XTPARENT
28 #include "wx/choice.h"
30 #include "wx/arrstr.h"
33 #pragma message disable nosimpint
36 #include <Xm/PushBG.h>
38 #include <Xm/RowColumn.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
45 #define WIDTH_OVERHEAD 48
46 #define WIDTH_OVERHEAD_SUBTRACT 40
47 #define HEIGHT_OVERHEAD 15
49 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
51 void wxChoiceCallback (Widget w
, XtPointer clientData
,
62 m_buttonWidget
= (WXWidget
) 0;
63 m_menuWidget
= (WXWidget
) 0;
64 m_formWidget
= (WXWidget
) 0;
67 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
70 int n
, const wxString choices
[],
72 const wxValidator
& validator
,
75 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
78 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
80 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
81 xmRowColumnWidgetClass
, parentWidget
,
84 XmNpacking
, XmPACK_TIGHT
,
85 XmNorientation
, XmHORIZONTAL
,
86 XmNresizeWidth
, False
,
87 XmNresizeHeight
, False
,
90 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
93 * Create the popup menu
95 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
96 "choiceMenu", NULL
, 0);
101 for (i
= 0; i
< n
; i
++)
111 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
112 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
113 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
114 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
115 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
119 m_mainWidget
= m_buttonWidget
;
121 XtManageChild ((Widget
) m_buttonWidget
);
123 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
124 // Some time ago, I reported a problem with wxChoice-items under
125 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
126 // that I have found the code responsible for this behaviour.
127 #if XmVersion >= 1002
129 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
130 // in controls sample.
132 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
133 // XtUnmanageChild (optionLabel);
137 wxSize bestSize
= GetBestSize();
138 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
139 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
141 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
144 ChangeBackgroundColour();
146 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
147 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
152 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
155 const wxArrayString
& choices
,
157 const wxValidator
& validator
,
158 const wxString
& name
)
160 wxCArrayString
chs(choices
);
161 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
162 style
, validator
, name
);
165 wxChoice::~wxChoice()
167 // For some reason destroying the menuWidget
168 // can cause crashes on some machines. It will
169 // be deleted implicitly by deleting the parent form
171 // XtDestroyWidget (menuWidget);
175 DetachWidget(GetMainWidget()); // Removes event handlers
176 DetachWidget(m_formWidget
);
178 XtDestroyWidget((Widget
) m_formWidget
);
179 m_formWidget
= (WXWidget
) 0;
181 // Presumably the other widgets have been deleted now, via the form
182 m_mainWidget
= (WXWidget
) 0;
183 m_buttonWidget
= (WXWidget
) 0;
185 if ( HasClientObjectData() )
186 m_clientDataDict
.DestroyData();
189 int wxChoice::DoAppend(const wxString
& item
)
191 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
193 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
195 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
199 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
202 wxDoChangeFont( w
, m_font
);
204 m_widgetArray
.Add(w
);
206 char mnem
= wxFindMnemonic (item
);
208 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
210 XtAddCallback (w
, XmNactivateCallback
,
211 (XtCallbackProc
) wxChoiceCallback
,
214 if (m_noStrings
== 0 && m_buttonWidget
)
216 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
217 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
218 wxXmString
text( item
);
219 XtVaSetValues (label
,
220 XmNlabelString
, text(),
223 m_stringList
.Add(item
);
226 return GetCount() - 1;
229 int wxChoice::DoInsert(const wxString
& item
, int pos
)
231 wxCHECK_MSG(false, -1, wxT("insert not implemented"));
233 // wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
234 // if (pos == GetCount()) return DoAppend(item);
237 void wxChoice::Delete(int n
)
239 Widget w
= (Widget
)m_widgetArray
[n
];
240 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
242 m_stringList
.Erase(m_stringList
.Item(n
));
243 m_widgetArray
.RemoveAt(size_t(n
));
244 m_clientDataDict
.Delete(n
, HasClientObjectData());
250 void wxChoice::Clear()
252 m_stringList
.Clear ();
254 for (i
= 0; i
< m_noStrings
; i
++)
256 XtRemoveCallback((Widget
) m_widgetArray
[i
],
257 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
259 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
260 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
262 m_widgetArray
.Clear();
264 XtVaSetValues ((Widget
) m_buttonWidget
,
265 XmNmenuHistory
, (Widget
) NULL
,
268 if ( HasClientObjectData() )
269 m_clientDataDict
.DestroyData();
274 int wxChoice::GetSelection() const
277 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
278 XtVaGetValues (label
,
279 XmNlabelString
, &text
,
281 wxXmString
freeMe(text
);
282 wxString s
= wxXmStringToString( text
);
287 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
288 node
; node
= node
->GetNext ())
290 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
303 void wxChoice::SetSelection(int n
)
307 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
311 Dimension selectionWidth
, selectionHeight
;
313 wxXmString
text( node
->GetData() );
314 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
315 // will remove "soon"
317 XtVaGetValues ((Widget
) m_widgetArray
[n
],
318 XmNwidth
, &selectionWidth
,
319 XmNheight
, &selectionHeight
,
322 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
323 XtVaSetValues (label
,
324 XmNlabelString
, text(),
327 XtVaSetValues ((Widget
) m_buttonWidget
,
328 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
329 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
332 m_inSetValue
= false;
335 int wxChoice::FindString(const wxString
& s
) const
338 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst();
339 node
; node
= node
->GetNext ())
341 if (s
== node
->GetData())
350 wxString
wxChoice::GetString(int n
) const
352 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
354 return node
->GetData();
356 return wxEmptyString
;
359 void wxChoice::SetColumns(int n
)
363 short numColumns
= n
;
366 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
367 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
368 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
371 int wxChoice::GetColumns(void) const
375 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
379 void wxChoice::SetFocus()
381 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
384 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
386 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
387 bool managed
= XtIsManaged((Widget
) m_formWidget
);
390 XtUnmanageChild ((Widget
) m_formWidget
);
392 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
393 actualHeight
= height
- HEIGHT_OVERHEAD
;
398 for (i
= 0; i
< m_noStrings
; i
++)
399 XtVaSetValues ((Widget
) m_widgetArray
[i
],
400 XmNwidth
, actualWidth
,
402 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
409 for (i
= 0; i
< m_noStrings
; i
++)
410 XtVaSetValues ((Widget
) m_widgetArray
[i
],
411 XmNheight
, actualHeight
,
414 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
419 XtManageChild ((Widget
) m_formWidget
);
420 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
422 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
425 void wxChoice::Command(wxCommandEvent
& event
)
427 SetSelection (event
.GetInt());
428 ProcessCommand (event
);
431 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
433 wxChoice
*item
= (wxChoice
*) clientData
;
436 if (item
->InSetValue())
439 int n
= item
->GetWidgets().Index(w
);
440 if (n
!= wxNOT_FOUND
)
442 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
443 event
.SetEventObject(item
);
445 event
.SetString( item
->GetStrings().Item(n
)->GetData() );
446 if ( item
->HasClientObjectData() )
447 event
.SetClientObject( item
->GetClientObject(n
) );
448 else if ( item
->HasClientUntypedData() )
449 event
.SetClientData( item
->GetClientData(n
) );
450 item
->ProcessCommand (event
);
455 void wxChoice::ChangeFont(bool keepOriginalSize
)
457 // Note that this causes the widget to be resized back
458 // to its original size! We therefore have to set the size
459 // back again. TODO: a better way in Motif?
462 int width
, height
, width1
, height1
;
463 GetSize(& width
, & height
);
465 WXFontType fontType
=
466 m_font
.GetFontType(XtDisplay((Widget
) m_mainWidget
));
467 WXString fontTag
= wxFont::GetFontTag();
469 XtVaSetValues ((Widget
) m_formWidget
, fontTag
, fontType
, NULL
);
470 XtVaSetValues ((Widget
) m_buttonWidget
, fontTag
, fontType
, NULL
);
472 for( size_t i
= 0; i
< m_noStrings
; ++i
)
473 XtVaSetValues( (Widget
)m_widgetArray
[i
],
477 GetSize(& width1
, & height1
);
478 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
480 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
485 void wxChoice::ChangeBackgroundColour()
487 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
488 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
489 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
491 for (i
= 0; i
< m_noStrings
; i
++)
492 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
495 void wxChoice::ChangeForegroundColour()
497 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
498 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
499 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
501 for (i
= 0; i
< m_noStrings
; i
++)
502 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
505 int wxChoice::GetCount() const
510 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
512 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, false);
515 void* wxChoice::DoGetItemClientData(int n
) const
517 return (void*)m_clientDataDict
.Get(n
);
520 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
522 // don't delete, wxItemContainer does that for us
523 m_clientDataDict
.Set(n
, clientData
, false);
526 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
528 return m_clientDataDict
.Get(n
);
531 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
533 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
536 wxSize
wxChoice::GetItemsSize() const
538 int x
, y
, mx
= 0, my
= 0;
541 GetTextExtent( "|", &x
, &my
);
543 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
546 GetTextExtent( curr
->GetData(), &x
, &y
);
549 curr
= curr
->GetNext();
552 return wxSize( mx
, my
);
555 wxSize
wxChoice::DoGetBestSize() const
557 wxSize items
= GetItemsSize();
558 // FIXME arbitrary constants
559 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
560 items
.y
+ HEIGHT_OVERHEAD
);
563 #endif // wxUSE_CHOICE