1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/choice.h"
21 #include <Xm/PushBG.h>
23 #include <Xm/RowColumn.h>
25 #include "wx/motif/private.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
31 void wxChoiceCallback (Widget w
, XtPointer clientData
,
37 m_buttonWidget
= (WXWidget
) 0;
38 m_menuWidget
= (WXWidget
) 0;
39 m_widgetList
= (WXWidget
*) 0;
40 m_formWidget
= (WXWidget
) 0;
43 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
46 int n
, const wxString choices
[],
48 const wxValidator
& validator
,
52 SetValidator(validator
);
54 m_windowStyle
= style
;
55 m_buttonWidget
= (WXWidget
) 0;
56 m_menuWidget
= (WXWidget
) 0;
57 m_widgetList
= (WXWidget
*) 0;
58 m_formWidget
= (WXWidget
) 0;
60 if (parent
) parent
->AddChild(this);
63 m_windowId
= (int)NewControlId();
67 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
69 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) name
,
70 xmRowColumnWidgetClass
, parentWidget
,
73 XmNpacking
, XmPACK_TIGHT
,
74 XmNorientation
, XmHORIZONTAL
,
77 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
80 * Create the popup menu
82 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
, "choiceMenu", NULL
, 0);
88 for (i
= 0; i
< n
; i
++)
98 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
);
100 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0);
102 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0);
104 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
);
106 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
, "choiceButton", args
, argcnt
);
108 m_mainWidget
= m_buttonWidget
;
110 XtManageChild ((Widget
) m_buttonWidget
);
112 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
113 // Some time ago, I reported a problem with wxChoice-items under
114 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
115 // that I have found the code responsible for this behaviour.
116 #if XmVersion >= 1002
118 Widget optionLabel
= XmOptionLabelGadget ((Widget
) m_buttonWidget
);
119 XtUnmanageChild (optionLabel
);
123 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
125 AttachWidget (parent
, m_buttonWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
127 SetFont(* parent
->GetFont());
128 ChangeColour(m_mainWidget
);
133 wxChoice::~wxChoice()
135 // For some reason destroying the menuWidget
136 // can cause crashes on some machines. It will
137 // be deleted implicitly by deleting the parent form
139 // XtDestroyWidget (menuWidget);
141 delete[] m_widgetList
;
143 DetachWidget(GetMainWidget()); // Removes event handlers
145 XtDestroyWidget((Widget
) m_formWidget
);
146 m_formWidget
= (WXWidget
) 0;
148 // Presumably the other widgets have been deleted now, via the form
149 m_mainWidget
= (WXWidget
) 0;
150 m_buttonWidget
= (WXWidget
) 0;
153 void wxChoice::Append(const wxString
& item
)
155 wxStripMenuCodes ((char *)(const char *)item
, wxBuffer
);
156 Widget w
= XtVaCreateManagedWidget (wxBuffer
,
158 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
160 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
164 if (m_windowFont
.Ok())
166 XmNfontList
, (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
169 WXWidget
*new_widgetList
= new WXWidget
[m_noStrings
+ 1];
171 for (i
= 0; i
< m_noStrings
; i
++)
172 new_widgetList
[i
] = m_widgetList
[i
];
173 new_widgetList
[m_noStrings
] = (WXWidget
) w
;
175 delete[] m_widgetList
;
176 m_widgetList
= new_widgetList
;
178 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
180 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
182 XtAddCallback (w
, XmNactivateCallback
, (XtCallbackProc
) wxChoiceCallback
, (XtPointer
) this);
184 if (m_noStrings
== 0 && m_buttonWidget
)
186 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
187 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
188 XmString text
= XmStringCreateSimple ((char*) (const char*) item
);
189 XtVaSetValues (label
,
190 XmNlabelString
, text
,
194 wxNode
*node
= m_stringList
.Add (item
);
195 XtVaSetValues (w
, XmNuserData
, node
->Data (), NULL
);
200 void wxChoice::Delete(int n
)
202 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
204 // What should we do -- remove the callback for this button widget,
205 // delete the m_stringList entry, delete the button widget, construct a new widget list
212 void wxChoice::Clear()
214 m_stringList
.Clear ();
216 for (i
= 0; i
< m_noStrings
; i
++)
218 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
219 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
222 delete[] m_widgetList
;
223 m_widgetList
= (WXWidget
*) NULL
;
225 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
229 int wxChoice::GetSelection() const
233 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
234 XtVaGetValues (label
,
235 XmNlabelString
, &text
,
238 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
241 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
243 char *s1
= (char *) node
->Data ();
244 if (s1
== s
|| strcmp (s1
, s
) == 0)
262 void wxChoice::SetSelection(int n
)
266 wxNode
*node
= m_stringList
.Nth (n
);
269 Dimension selectionWidth
, selectionHeight
;
271 char *s
= (char *) node
->Data ();
272 XmString text
= XmStringCreateSimple (s
);
273 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
274 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
275 XtVaSetValues (label
,
276 XmNlabelString
, text
,
279 XtVaSetValues ((Widget
) m_buttonWidget
,
280 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
281 XmNmenuHistory
, (Widget
) m_widgetList
[n
], NULL
);
283 m_inSetValue
= FALSE
;
286 int wxChoice::FindString(const wxString
& s
) const
289 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
291 char *s1
= (char *) node
->Data ();
302 wxString
wxChoice::GetString(int n
) const
304 wxNode
*node
= m_stringList
.Nth (n
);
306 return wxString((char *) node
->Data ());
308 return wxEmptyString
;
311 void wxChoice::SetColumns(int n
)
315 short numColumns
= n
;
318 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
319 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
320 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
323 int wxChoice::GetColumns(void) const
327 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
331 void wxChoice::SetFocus()
333 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
336 void wxChoice::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
338 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
339 bool managed
= XtIsManaged((Widget
) m_formWidget
);
342 XtUnmanageChild ((Widget
) m_formWidget
);
344 int actualWidth
= width
, actualHeight
= height
;
349 for (i
= 0; i
< m_noStrings
; i
++)
350 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
351 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
357 for (i
= 0; i
< m_noStrings
; i
++)
358 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
359 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
364 XtManageChild ((Widget
) m_formWidget
);
365 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
367 wxControl::SetSize (x
, y
, width
, height
, sizeFlags
);
370 wxString
wxChoice::GetStringSelection () const
372 int sel
= GetSelection ();
374 return wxString(this->GetString (sel
));
376 return wxEmptyString
;
379 bool wxChoice::SetStringSelection (const wxString
& s
)
381 int sel
= FindString (s
);
391 void wxChoice::Command(wxCommandEvent
& event
)
393 SetSelection (event
.GetInt());
394 ProcessCommand (event
);
397 void wxChoiceCallback (Widget w
, XtPointer clientData
,
400 wxChoice
*item
= (wxChoice
*) clientData
;
403 if (item
->InSetValue())
407 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
410 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
);
411 event
.m_commandInt
= item
->FindString (s
);
412 // event.m_commandString = s;
413 item
->ProcessCommand (event
);