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 m_backgroundColour
= parent
->GetBackgroundColour();
68 m_foregroundColour
= parent
->GetForegroundColour();
70 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
72 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) name
,
73 xmRowColumnWidgetClass
, parentWidget
,
76 XmNpacking
, XmPACK_TIGHT
,
77 XmNorientation
, XmHORIZONTAL
,
80 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
83 * Create the popup menu
85 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
, "choiceMenu", NULL
, 0);
91 for (i
= 0; i
< n
; i
++)
101 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
);
103 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0);
105 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0);
107 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
);
109 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
, "choiceButton", args
, argcnt
);
111 m_mainWidget
= m_buttonWidget
;
113 XtManageChild ((Widget
) m_buttonWidget
);
115 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
116 // Some time ago, I reported a problem with wxChoice-items under
117 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
118 // that I have found the code responsible for this behaviour.
119 #if XmVersion >= 1002
121 Widget optionLabel
= XmOptionLabelGadget ((Widget
) m_buttonWidget
);
122 XtUnmanageChild (optionLabel
);
126 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
128 AttachWidget (parent
, m_buttonWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
130 SetFont(* parent
->GetFont());
131 ChangeBackgroundColour();
136 wxChoice
::~wxChoice()
138 // For some reason destroying the menuWidget
139 // can cause crashes on some machines. It will
140 // be deleted implicitly by deleting the parent form
142 // XtDestroyWidget (menuWidget);
144 delete[] m_widgetList
;
146 DetachWidget(GetMainWidget()); // Removes event handlers
148 XtDestroyWidget((Widget
) m_formWidget
);
149 m_formWidget
= (WXWidget
) 0;
151 // Presumably the other widgets have been deleted now, via the form
152 m_mainWidget
= (WXWidget
) 0;
153 m_buttonWidget
= (WXWidget
) 0;
156 void wxChoice
::Append(const wxString
& item
)
158 wxStripMenuCodes ((char *)(const char *)item
, wxBuffer
);
159 Widget w
= XtVaCreateManagedWidget (wxBuffer
,
161 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
163 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
167 if (m_windowFont
.Ok())
169 XmNfontList
, (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
172 WXWidget
*new_widgetList
= new WXWidget
[m_noStrings
+ 1];
174 for (i
= 0; i
< m_noStrings
; i
++)
175 new_widgetList
[i
] = m_widgetList
[i
];
176 new_widgetList
[m_noStrings
] = (WXWidget
) w
;
178 delete[] m_widgetList
;
179 m_widgetList
= new_widgetList
;
181 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
183 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
185 XtAddCallback (w
, XmNactivateCallback
, (XtCallbackProc
) wxChoiceCallback
, (XtPointer
) this);
187 if (m_noStrings
== 0 && m_buttonWidget
)
189 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
190 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
191 XmString text
= XmStringCreateSimple ((char*) (const char*) item
);
192 XtVaSetValues (label
,
193 XmNlabelString
, text
,
197 wxNode
*node
= m_stringList
.Add (item
);
198 XtVaSetValues (w
, XmNuserData
, node
->Data (), NULL
);
203 void wxChoice
::Delete(int n
)
205 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
207 // What should we do -- remove the callback for this button widget,
208 // delete the m_stringList entry, delete the button widget, construct a new widget list
215 void wxChoice
::Clear()
217 m_stringList
.Clear ();
219 for (i
= 0; i
< m_noStrings
; i
++)
221 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
222 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
225 delete[] m_widgetList
;
226 m_widgetList
= (WXWidget
*) NULL
;
228 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
232 int wxChoice
::GetSelection() const
236 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
237 XtVaGetValues (label
,
238 XmNlabelString
, &text
,
241 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
244 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
246 char *s1
= (char *) node
->Data ();
247 if (s1
== s
|| strcmp (s1
, s
) == 0)
265 void wxChoice
::SetSelection(int n
)
269 wxNode
*node
= m_stringList
.Nth (n
);
272 Dimension selectionWidth
, selectionHeight
;
274 char *s
= (char *) node
->Data ();
275 XmString text
= XmStringCreateSimple (s
);
276 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
277 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
278 XtVaSetValues (label
,
279 XmNlabelString
, text
,
282 XtVaSetValues ((Widget
) m_buttonWidget
,
283 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
284 XmNmenuHistory
, (Widget
) m_widgetList
[n
], NULL
);
286 m_inSetValue
= FALSE
;
289 int wxChoice
::FindString(const wxString
& s
) const
292 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
294 char *s1
= (char *) node
->Data ();
305 wxString wxChoice
::GetString(int n
) const
307 wxNode
*node
= m_stringList
.Nth (n
);
309 return wxString((char *) node
->Data ());
311 return wxEmptyString
;
314 void wxChoice
::SetColumns(int n
)
318 short numColumns
= n
;
321 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
322 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
323 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
326 int wxChoice
::GetColumns(void) const
330 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
334 void wxChoice
::SetFocus()
336 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
339 void wxChoice
::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
341 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
342 bool managed
= XtIsManaged((Widget
) m_formWidget
);
345 XtUnmanageChild ((Widget
) m_formWidget
);
347 int actualWidth
= width
, actualHeight
= height
;
352 for (i
= 0; i
< m_noStrings
; i
++)
353 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
354 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
360 for (i
= 0; i
< m_noStrings
; i
++)
361 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
362 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
367 XtManageChild ((Widget
) m_formWidget
);
368 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
370 wxControl
::SetSize (x
, y
, width
, height
, sizeFlags
);
373 wxString wxChoice
::GetStringSelection () const
375 int sel
= GetSelection ();
377 return wxString(this->GetString (sel
));
379 return wxEmptyString
;
382 bool wxChoice
::SetStringSelection (const wxString
& s
)
384 int sel
= FindString (s
);
394 void wxChoice
::Command(wxCommandEvent
& event
)
396 SetSelection (event
.GetInt());
397 ProcessCommand (event
);
400 void wxChoiceCallback (Widget w
, XtPointer clientData
,
403 wxChoice
*item
= (wxChoice
*) clientData
;
406 if (item
->InSetValue())
410 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
413 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
);
414 event
.m_commandInt
= item
->FindString (s
);
415 // event.m_commandString = s;
416 item
->ProcessCommand (event
);
421 void wxChoice
::ChangeFont()
426 void wxChoice
::ChangeBackgroundColour()
431 void wxChoice
::ChangeForegroundColour()