1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/choice.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
17 #define XtParent XTPARENT
24 #include "wx/choice.h"
26 #include "wx/arrstr.h"
29 #pragma message disable nosimpint
32 #include <Xm/PushBG.h>
34 #include <Xm/RowColumn.h>
36 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
41 #define WIDTH_OVERHEAD 48
42 #define WIDTH_OVERHEAD_SUBTRACT 40
43 #define HEIGHT_OVERHEAD 15
45 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
47 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 wxMOTIF_STR("choiceMenu"),
98 for (i
= 0; i
< n
; i
++)
108 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
109 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
110 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
111 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
112 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
113 wxMOTIF_STR("choiceButton"),
116 m_mainWidget
= m_buttonWidget
;
118 XtManageChild ((Widget
) m_buttonWidget
);
120 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
121 // Some time ago, I reported a problem with wxChoice-items under
122 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
123 // that I have found the code responsible for this behaviour.
124 #if XmVersion >= 1002
126 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
127 // in controls sample.
129 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
130 // XtUnmanageChild (optionLabel);
134 wxSize bestSize
= GetBestSize();
135 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
136 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
138 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
141 ChangeBackgroundColour();
143 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
144 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
149 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
152 const wxArrayString
& choices
,
154 const wxValidator
& validator
,
155 const wxString
& name
)
157 wxCArrayString
chs(choices
);
158 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
159 style
, validator
, name
);
162 wxChoice::~wxChoice()
164 // For some reason destroying the menuWidget
165 // can cause crashes on some machines. It will
166 // be deleted implicitly by deleting the parent form
168 // XtDestroyWidget (menuWidget);
172 DetachWidget(GetMainWidget()); // Removes event handlers
173 DetachWidget(m_formWidget
);
175 XtDestroyWidget((Widget
) m_formWidget
);
176 m_formWidget
= (WXWidget
) 0;
178 // Presumably the other widgets have been deleted now, via the form
179 m_mainWidget
= (WXWidget
) 0;
180 m_buttonWidget
= (WXWidget
) 0;
182 if ( HasClientObjectData() )
183 m_clientDataDict
.DestroyData();
186 static inline wxChar
* MYcopystring(const wxChar
* s
)
188 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
189 return wxStrcpy(copy
, s
);
192 int wxChoice::DoInsert(const wxString
& item
, int pos
)
194 #ifndef XmNpositionIndex
195 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
197 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
199 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
201 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
203 #ifdef XmNpositionIndex
204 XmNpositionIndex
, pos
,
208 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
211 wxDoChangeFont( w
, m_font
);
213 m_widgetArray
.Insert(w
, pos
);
215 char mnem
= wxFindMnemonic (item
);
217 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
219 XtAddCallback (w
, XmNactivateCallback
,
220 (XtCallbackProc
) wxChoiceCallback
,
223 if (m_noStrings
== 0 && m_buttonWidget
)
225 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
226 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
227 wxXmString
text( item
);
228 XtVaSetValues (label
,
229 XmNlabelString
, text(),
232 // need to ditch wxStringList for wxArrayString
233 m_stringList
.Insert(pos
, MYcopystring(item
));
239 int wxChoice::DoAppend(const wxString
& item
)
241 return DoInsert(item
, GetCount());
244 void wxChoice::Delete(int n
)
246 Widget w
= (Widget
)m_widgetArray
[n
];
247 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
249 m_stringList
.Erase(m_stringList
.Item(n
));
250 m_widgetArray
.RemoveAt(size_t(n
));
251 m_clientDataDict
.Delete(n
, HasClientObjectData());
257 void wxChoice::Clear()
259 m_stringList
.Clear ();
261 for (i
= 0; i
< m_noStrings
; i
++)
263 XtRemoveCallback((Widget
) m_widgetArray
[i
],
264 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
266 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
267 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
269 m_widgetArray
.Clear();
271 XtVaSetValues ((Widget
) m_buttonWidget
,
272 XmNmenuHistory
, (Widget
) NULL
,
275 if ( HasClientObjectData() )
276 m_clientDataDict
.DestroyData();
281 int wxChoice::GetSelection() const
284 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
285 XtVaGetValues (label
,
286 XmNlabelString
, &text
,
288 wxXmString
freeMe(text
);
289 wxString s
= wxXmStringToString( text
);
294 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
295 node
; node
= node
->GetNext ())
297 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
310 void wxChoice::SetSelection(int n
)
314 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
318 Dimension selectionWidth
, selectionHeight
;
320 wxXmString
text( node
->GetData() );
321 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
322 // will remove "soon"
324 XtVaGetValues ((Widget
) m_widgetArray
[n
],
325 XmNwidth
, &selectionWidth
,
326 XmNheight
, &selectionHeight
,
329 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
330 XtVaSetValues (label
,
331 XmNlabelString
, text(),
334 XtVaSetValues ((Widget
) m_buttonWidget
,
335 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
336 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
339 m_inSetValue
= false;
342 wxString
wxChoice::GetString(int n
) const
344 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
346 return node
->GetData();
348 return wxEmptyString
;
351 void wxChoice::SetColumns(int n
)
355 short numColumns
= n
;
358 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
359 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
360 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
363 int wxChoice::GetColumns(void) const
367 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
371 void wxChoice::SetFocus()
373 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
376 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
378 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
379 bool managed
= XtIsManaged((Widget
) m_formWidget
);
382 XtUnmanageChild ((Widget
) m_formWidget
);
384 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
385 actualHeight
= height
- HEIGHT_OVERHEAD
;
390 for (i
= 0; i
< m_noStrings
; i
++)
391 XtVaSetValues ((Widget
) m_widgetArray
[i
],
392 XmNwidth
, actualWidth
,
394 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
401 for (i
= 0; i
< m_noStrings
; i
++)
402 XtVaSetValues ((Widget
) m_widgetArray
[i
],
403 XmNheight
, actualHeight
,
406 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
411 XtManageChild ((Widget
) m_formWidget
);
412 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
414 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
417 void wxChoice::Command(wxCommandEvent
& event
)
419 SetSelection (event
.GetInt());
420 ProcessCommand (event
);
423 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
425 wxChoice
*item
= (wxChoice
*) clientData
;
428 if (item
->InSetValue())
431 int n
= item
->GetWidgets().Index(w
);
432 if (n
!= wxNOT_FOUND
)
434 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
435 event
.SetEventObject(item
);
437 event
.SetString( item
->GetStrings().Item(n
)->GetData() );
438 if ( item
->HasClientObjectData() )
439 event
.SetClientObject( item
->GetClientObject(n
) );
440 else if ( item
->HasClientUntypedData() )
441 event
.SetClientData( item
->GetClientData(n
) );
442 item
->ProcessCommand (event
);
447 void wxChoice::ChangeFont(bool keepOriginalSize
)
449 // Note that this causes the widget to be resized back
450 // to its original size! We therefore have to set the size
451 // back again. TODO: a better way in Motif?
454 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
455 int width
, height
, width1
, height1
;
456 GetSize(& width
, & height
);
458 WXString fontTag
= wxFont::GetFontTag();
460 XtVaSetValues ((Widget
) m_formWidget
,
461 fontTag
, m_font
.GetFontTypeC(dpy
),
463 XtVaSetValues ((Widget
) m_buttonWidget
,
464 fontTag
, m_font
.GetFontTypeC(dpy
),
467 for( size_t i
= 0; i
< m_noStrings
; ++i
)
468 XtVaSetValues( (Widget
)m_widgetArray
[i
],
469 fontTag
, m_font
.GetFontTypeC(dpy
),
472 GetSize(& width1
, & height1
);
473 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
475 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
480 void wxChoice::ChangeBackgroundColour()
482 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
483 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
484 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
486 for (i
= 0; i
< m_noStrings
; i
++)
487 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
490 void wxChoice::ChangeForegroundColour()
492 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
493 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
494 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
496 for (i
= 0; i
< m_noStrings
; i
++)
497 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
500 int wxChoice::GetCount() const
505 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
507 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, false);
510 void* wxChoice::DoGetItemClientData(int n
) const
512 return (void*)m_clientDataDict
.Get(n
);
515 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
517 // don't delete, wxItemContainer does that for us
518 m_clientDataDict
.Set(n
, clientData
, false);
521 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
523 return m_clientDataDict
.Get(n
);
526 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
528 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
531 wxSize
wxChoice::GetItemsSize() const
533 int x
, y
, mx
= 0, my
= 0;
536 GetTextExtent( "|", &x
, &my
);
538 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
541 GetTextExtent( curr
->GetData(), &x
, &y
);
544 curr
= curr
->GetNext();
547 return wxSize( mx
, my
);
550 wxSize
wxChoice::DoGetBestSize() const
552 wxSize items
= GetItemsSize();
553 // FIXME arbitrary constants
554 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
555 items
.y
+ HEIGHT_OVERHEAD
);
558 #endif // wxUSE_CHOICE