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"
18 #define XtDisplay XTDISPLAY
19 #define XtParent XTPARENT
22 #include "wx/choice.h"
24 #include "wx/arrstr.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
,
56 m_buttonWidget
= (WXWidget
) 0;
57 m_menuWidget
= (WXWidget
) 0;
58 m_formWidget
= (WXWidget
) 0;
61 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
64 int n
, const wxString choices
[],
66 const wxValidator
& validator
,
69 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
72 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
74 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
75 xmRowColumnWidgetClass
, parentWidget
,
78 XmNpacking
, XmPACK_TIGHT
,
79 XmNorientation
, XmHORIZONTAL
,
80 XmNresizeWidth
, False
,
81 XmNresizeHeight
, False
,
84 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
87 * Create the popup menu
89 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
90 wxMOTIF_STR("choiceMenu"),
96 for (i
= 0; i
< n
; i
++)
106 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
107 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
108 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
109 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
110 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
111 wxMOTIF_STR("choiceButton"),
114 m_mainWidget
= m_buttonWidget
;
116 XtManageChild ((Widget
) m_buttonWidget
);
118 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
119 // Some time ago, I reported a problem with wxChoice-items under
120 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
121 // that I have found the code responsible for this behaviour.
122 #if XmVersion >= 1002
124 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
125 // in controls sample.
127 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
128 // XtUnmanageChild (optionLabel);
132 wxSize bestSize
= GetBestSize();
133 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
134 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
136 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
139 ChangeBackgroundColour();
141 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
142 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
147 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
150 const wxArrayString
& choices
,
152 const wxValidator
& validator
,
153 const wxString
& name
)
155 wxCArrayString
chs(choices
);
156 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
157 style
, validator
, name
);
160 wxChoice::~wxChoice()
162 // For some reason destroying the menuWidget
163 // can cause crashes on some machines. It will
164 // be deleted implicitly by deleting the parent form
166 // XtDestroyWidget (menuWidget);
170 DetachWidget(GetMainWidget()); // Removes event handlers
171 DetachWidget(m_formWidget
);
173 XtDestroyWidget((Widget
) m_formWidget
);
174 m_formWidget
= (WXWidget
) 0;
176 // Presumably the other widgets have been deleted now, via the form
177 m_mainWidget
= (WXWidget
) 0;
178 m_buttonWidget
= (WXWidget
) 0;
180 if ( HasClientObjectData() )
181 m_clientDataDict
.DestroyData();
184 static inline wxChar
* MYcopystring(const wxChar
* s
)
186 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
187 return wxStrcpy(copy
, s
);
190 int wxChoice::DoInsert(const wxString
& item
, unsigned int pos
)
192 #ifndef XmNpositionIndex
193 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
195 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
197 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
199 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
201 #ifdef XmNpositionIndex
202 XmNpositionIndex
, pos
,
206 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
209 wxDoChangeFont( w
, m_font
);
211 m_widgetArray
.Insert(w
, pos
);
213 char mnem
= wxFindMnemonic (item
);
215 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
217 XtAddCallback (w
, XmNactivateCallback
,
218 (XtCallbackProc
) wxChoiceCallback
,
221 if (m_noStrings
== 0 && m_buttonWidget
)
223 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
224 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
225 wxXmString
text( item
);
226 XtVaSetValues (label
,
227 XmNlabelString
, text(),
230 // need to ditch wxStringList for wxArrayString
231 m_stringList
.Insert(pos
, MYcopystring(item
));
237 int wxChoice::DoAppend(const wxString
& item
)
239 return DoInsert(item
, GetCount());
242 void wxChoice::Delete(unsigned int n
)
244 Widget w
= (Widget
)m_widgetArray
[n
];
245 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
247 m_stringList
.Erase(m_stringList
.Item(n
));
248 m_widgetArray
.RemoveAt(size_t(n
));
249 m_clientDataDict
.Delete(n
, HasClientObjectData());
255 void wxChoice::Clear()
257 m_stringList
.Clear ();
259 for (i
= 0; i
< m_noStrings
; i
++)
261 XtRemoveCallback((Widget
) m_widgetArray
[i
],
262 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
264 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
265 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
267 m_widgetArray
.Clear();
269 XtVaSetValues ((Widget
) m_buttonWidget
,
270 XmNmenuHistory
, (Widget
) NULL
,
273 if ( HasClientObjectData() )
274 m_clientDataDict
.DestroyData();
279 int wxChoice::GetSelection() const
282 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
283 XtVaGetValues (label
,
284 XmNlabelString
, &text
,
286 wxXmString
freeMe(text
);
287 wxString s
= wxXmStringToString( text
);
292 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
293 node
; node
= node
->GetNext ())
295 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
308 void wxChoice::SetSelection(int n
)
312 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
316 Dimension selectionWidth
, selectionHeight
;
318 wxXmString
text( node
->GetData() );
319 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
320 // will remove "soon"
322 XtVaGetValues ((Widget
) m_widgetArray
[n
],
323 XmNwidth
, &selectionWidth
,
324 XmNheight
, &selectionHeight
,
327 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
328 XtVaSetValues (label
,
329 XmNlabelString
, text(),
332 XtVaSetValues ((Widget
) m_buttonWidget
,
333 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
334 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
337 m_inSetValue
= false;
340 wxString
wxChoice::GetString(unsigned int n
) const
342 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
344 return node
->GetData();
346 return wxEmptyString
;
349 void wxChoice::SetColumns(int n
)
353 short numColumns
= (short)n
;
356 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
357 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
358 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
361 int wxChoice::GetColumns(void) const
365 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
369 void wxChoice::SetFocus()
371 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
374 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
376 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
377 bool managed
= XtIsManaged((Widget
) m_formWidget
);
380 XtUnmanageChild ((Widget
) m_formWidget
);
382 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
383 actualHeight
= height
- HEIGHT_OVERHEAD
;
388 for (i
= 0; i
< m_noStrings
; i
++)
389 XtVaSetValues ((Widget
) m_widgetArray
[i
],
390 XmNwidth
, actualWidth
,
392 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
399 for (i
= 0; i
< m_noStrings
; i
++)
400 XtVaSetValues ((Widget
) m_widgetArray
[i
],
401 XmNheight
, actualHeight
,
404 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
409 XtManageChild ((Widget
) m_formWidget
);
410 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
412 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
415 void wxChoice::Command(wxCommandEvent
& event
)
417 SetSelection (event
.GetInt());
418 ProcessCommand (event
);
421 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
423 wxChoice
*item
= (wxChoice
*) clientData
;
426 if (item
->InSetValue())
429 int n
= item
->GetWidgets().Index(w
);
430 if (n
!= wxNOT_FOUND
)
432 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
433 event
.SetEventObject(item
);
435 event
.SetString( item
->GetStrings().Item(n
)->GetData() );
436 if ( item
->HasClientObjectData() )
437 event
.SetClientObject( item
->GetClientObject(n
) );
438 else if ( item
->HasClientUntypedData() )
439 event
.SetClientData( item
->GetClientData(n
) );
440 item
->ProcessCommand (event
);
445 void wxChoice::ChangeFont(bool keepOriginalSize
)
447 // Note that this causes the widget to be resized back
448 // to its original size! We therefore have to set the size
449 // back again. TODO: a better way in Motif?
452 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
453 int width
, height
, width1
, height1
;
454 GetSize(& width
, & height
);
456 WXString fontTag
= wxFont::GetFontTag();
458 XtVaSetValues ((Widget
) m_formWidget
,
459 fontTag
, m_font
.GetFontTypeC(dpy
),
461 XtVaSetValues ((Widget
) m_buttonWidget
,
462 fontTag
, m_font
.GetFontTypeC(dpy
),
465 for( unsigned int i
= 0; i
< m_noStrings
; ++i
)
466 XtVaSetValues( (Widget
)m_widgetArray
[i
],
467 fontTag
, m_font
.GetFontTypeC(dpy
),
470 GetSize(& width1
, & height1
);
471 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
473 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
478 void wxChoice::ChangeBackgroundColour()
480 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
481 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
482 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
484 for (i
= 0; i
< m_noStrings
; i
++)
485 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
488 void wxChoice::ChangeForegroundColour()
490 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
491 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
492 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
494 for (i
= 0; i
< m_noStrings
; i
++)
495 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
498 unsigned int wxChoice::GetCount() const
503 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
505 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, false);
508 void* wxChoice::DoGetItemClientData(unsigned int n
) const
510 return (void*)m_clientDataDict
.Get(n
);
513 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
515 // don't delete, wxItemContainer does that for us
516 m_clientDataDict
.Set(n
, clientData
, false);
519 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
521 return m_clientDataDict
.Get(n
);
524 void wxChoice::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
526 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
529 wxSize
wxChoice::GetItemsSize() const
531 int x
, y
, mx
= 0, my
= 0;
534 GetTextExtent( "|", &x
, &my
);
536 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
539 GetTextExtent( curr
->GetData(), &x
, &y
);
542 curr
= curr
->GetNext();
545 return wxSize( mx
, my
);
548 wxSize
wxChoice::DoGetBestSize() const
550 wxSize items
= GetItemsSize();
551 // FIXME arbitrary constants
552 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
553 items
.y
+ HEIGHT_OVERHEAD
);
556 #endif // wxUSE_CHOICE