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"
26 #include "wx/arrstr.h"
30 #pragma message disable nosimpint
33 #include <Xm/PushBG.h>
35 #include <Xm/RowColumn.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 #define WIDTH_OVERHEAD 48
43 #define WIDTH_OVERHEAD_SUBTRACT 40
44 #define HEIGHT_OVERHEAD 15
46 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
48 void wxChoiceCallback (Widget w
, XtPointer clientData
,
59 m_buttonWidget
= (WXWidget
) 0;
60 m_menuWidget
= (WXWidget
) 0;
61 m_formWidget
= (WXWidget
) 0;
64 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
67 int n
, const wxString choices
[],
69 const wxValidator
& validator
,
72 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
75 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
77 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
78 xmRowColumnWidgetClass
, parentWidget
,
81 XmNpacking
, XmPACK_TIGHT
,
82 XmNorientation
, XmHORIZONTAL
,
83 XmNresizeWidth
, False
,
84 XmNresizeHeight
, False
,
87 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
90 * Create the popup menu
92 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
93 wxMOTIF_STR("choiceMenu"),
99 for (i
= 0; i
< n
; i
++)
109 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
110 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
111 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
112 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
113 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
114 wxMOTIF_STR("choiceButton"),
117 m_mainWidget
= m_buttonWidget
;
119 XtManageChild ((Widget
) m_buttonWidget
);
121 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
122 // Some time ago, I reported a problem with wxChoice-items under
123 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
124 // that I have found the code responsible for this behaviour.
125 #if XmVersion >= 1002
127 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
128 // in controls sample.
130 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
131 // XtUnmanageChild (optionLabel);
135 wxSize bestSize
= GetBestSize();
136 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
137 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
139 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
142 ChangeBackgroundColour();
144 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
145 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
150 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
153 const wxArrayString
& choices
,
155 const wxValidator
& validator
,
156 const wxString
& name
)
158 wxCArrayString
chs(choices
);
159 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
160 style
, validator
, name
);
163 wxChoice::~wxChoice()
165 // For some reason destroying the menuWidget
166 // can cause crashes on some machines. It will
167 // be deleted implicitly by deleting the parent form
169 // XtDestroyWidget (menuWidget);
173 DetachWidget(GetMainWidget()); // Removes event handlers
174 DetachWidget(m_formWidget
);
176 XtDestroyWidget((Widget
) m_formWidget
);
177 m_formWidget
= (WXWidget
) 0;
179 // Presumably the other widgets have been deleted now, via the form
180 m_mainWidget
= (WXWidget
) 0;
181 m_buttonWidget
= (WXWidget
) 0;
183 if ( HasClientObjectData() )
184 m_clientDataDict
.DestroyData();
187 static inline wxChar
* MYcopystring(const wxChar
* s
)
189 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
190 return wxStrcpy(copy
, s
);
193 int wxChoice::DoInsert(const wxString
& item
, unsigned int pos
)
195 #ifndef XmNpositionIndex
196 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
198 Widget w
= XtVaCreateManagedWidget (GetLabelText(item
),
200 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
202 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
204 #ifdef XmNpositionIndex
205 XmNpositionIndex
, pos
,
209 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
212 wxDoChangeFont( w
, m_font
);
214 m_widgetArray
.Insert(w
, pos
);
216 char mnem
= wxFindMnemonic (item
);
218 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
220 XtAddCallback (w
, XmNactivateCallback
,
221 (XtCallbackProc
) wxChoiceCallback
,
224 if (m_noStrings
== 0 && m_buttonWidget
)
226 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
227 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
228 wxXmString
text( item
);
229 XtVaSetValues (label
,
230 XmNlabelString
, text(),
233 // need to ditch wxStringList for wxArrayString
234 m_stringList
.Insert(pos
, MYcopystring(item
));
240 int wxChoice::DoAppend(const wxString
& item
)
242 return DoInsert(item
, GetCount());
245 void wxChoice::Delete(unsigned int n
)
247 Widget w
= (Widget
)m_widgetArray
[n
];
248 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
250 m_stringList
.Erase(m_stringList
.Item(n
));
251 m_widgetArray
.RemoveAt(size_t(n
));
252 m_clientDataDict
.Delete(n
, HasClientObjectData());
258 void wxChoice::Clear()
260 m_stringList
.Clear ();
262 for (i
= 0; i
< m_noStrings
; i
++)
264 XtRemoveCallback((Widget
) m_widgetArray
[i
],
265 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
267 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
268 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
270 m_widgetArray
.Clear();
272 XtVaSetValues ((Widget
) m_buttonWidget
,
273 XmNmenuHistory
, (Widget
) NULL
,
276 if ( HasClientObjectData() )
277 m_clientDataDict
.DestroyData();
282 int wxChoice::GetSelection() const
285 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
286 XtVaGetValues (label
,
287 XmNlabelString
, &text
,
289 wxXmString
freeMe(text
);
290 wxString s
= wxXmStringToString( text
);
295 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
296 node
; node
= node
->GetNext ())
298 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
311 void wxChoice::SetSelection(int n
)
315 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
319 Dimension selectionWidth
, selectionHeight
;
321 wxXmString
text( node
->GetData() );
322 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
323 // will remove "soon"
325 XtVaGetValues ((Widget
) m_widgetArray
[n
],
326 XmNwidth
, &selectionWidth
,
327 XmNheight
, &selectionHeight
,
330 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
331 XtVaSetValues (label
,
332 XmNlabelString
, text(),
335 XtVaSetValues ((Widget
) m_buttonWidget
,
336 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
337 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
340 m_inSetValue
= false;
343 wxString
wxChoice::GetString(unsigned int n
) const
345 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
347 return node
->GetData();
349 return wxEmptyString
;
352 void wxChoice::SetColumns(int n
)
356 short numColumns
= (short)n
;
359 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
360 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
361 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
364 int wxChoice::GetColumns(void) const
368 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
372 void wxChoice::SetFocus()
374 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
377 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
379 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
380 bool managed
= XtIsManaged((Widget
) m_formWidget
);
383 XtUnmanageChild ((Widget
) m_formWidget
);
385 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
386 actualHeight
= height
- HEIGHT_OVERHEAD
;
391 for (i
= 0; i
< m_noStrings
; i
++)
392 XtVaSetValues ((Widget
) m_widgetArray
[i
],
393 XmNwidth
, actualWidth
,
395 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
402 for (i
= 0; i
< m_noStrings
; i
++)
403 XtVaSetValues ((Widget
) m_widgetArray
[i
],
404 XmNheight
, actualHeight
,
407 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
412 XtManageChild ((Widget
) m_formWidget
);
413 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
415 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
418 void wxChoice::Command(wxCommandEvent
& event
)
420 SetSelection (event
.GetInt());
421 ProcessCommand (event
);
424 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
426 wxChoice
*item
= (wxChoice
*) clientData
;
429 if (item
->InSetValue())
432 int n
= item
->GetWidgets().Index(w
);
433 if (n
!= wxNOT_FOUND
)
435 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
436 event
.SetEventObject(item
);
438 event
.SetString( item
->GetStrings().Item(n
)->GetData() );
439 if ( item
->HasClientObjectData() )
440 event
.SetClientObject( item
->GetClientObject(n
) );
441 else if ( item
->HasClientUntypedData() )
442 event
.SetClientData( item
->GetClientData(n
) );
443 item
->ProcessCommand (event
);
448 void wxChoice::ChangeFont(bool keepOriginalSize
)
450 // Note that this causes the widget to be resized back
451 // to its original size! We therefore have to set the size
452 // back again. TODO: a better way in Motif?
455 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
456 int width
, height
, width1
, height1
;
457 GetSize(& width
, & height
);
459 WXString fontTag
= wxFont::GetFontTag();
461 XtVaSetValues ((Widget
) m_formWidget
,
462 fontTag
, m_font
.GetFontTypeC(dpy
),
464 XtVaSetValues ((Widget
) m_buttonWidget
,
465 fontTag
, m_font
.GetFontTypeC(dpy
),
468 for( unsigned int i
= 0; i
< m_noStrings
; ++i
)
469 XtVaSetValues( (Widget
)m_widgetArray
[i
],
470 fontTag
, m_font
.GetFontTypeC(dpy
),
473 GetSize(& width1
, & height1
);
474 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
476 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
481 void wxChoice::ChangeBackgroundColour()
483 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
484 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
485 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
487 for (i
= 0; i
< m_noStrings
; i
++)
488 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
491 void wxChoice::ChangeForegroundColour()
493 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
494 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
495 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
497 for (i
= 0; i
< m_noStrings
; i
++)
498 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
501 unsigned int wxChoice::GetCount() const
506 void wxChoice::DoSetItemClientData(unsigned int n
, void* clientData
)
508 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, false);
511 void* wxChoice::DoGetItemClientData(unsigned int n
) const
513 return (void*)m_clientDataDict
.Get(n
);
516 void wxChoice::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
518 // don't delete, wxItemContainer does that for us
519 m_clientDataDict
.Set(n
, clientData
, false);
522 wxClientData
* wxChoice::DoGetItemClientObject(unsigned int n
) const
524 return m_clientDataDict
.Get(n
);
527 void wxChoice::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
529 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
532 wxSize
wxChoice::GetItemsSize() const
534 int x
, y
, mx
= 0, my
= 0;
537 GetTextExtent( "|", &x
, &my
);
539 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
542 GetTextExtent( curr
->GetData(), &x
, &y
);
545 curr
= curr
->GetNext();
548 return wxSize( mx
, my
);
551 wxSize
wxChoice::DoGetBestSize() const
553 wxSize items
= GetItemsSize();
554 // FIXME arbitrary constants
555 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
556 items
.y
+ HEIGHT_OVERHEAD
);
559 #endif // wxUSE_CHOICE