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 DoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
169 if (m_windowFont
.Ok())
171 XmNfontList
, (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
174 WXWidget
*new_widgetList
= new WXWidget
[m_noStrings
+ 1];
176 for (i
= 0; i
< m_noStrings
; i
++)
177 new_widgetList
[i
] = m_widgetList
[i
];
178 new_widgetList
[m_noStrings
] = (WXWidget
) w
;
180 delete[] m_widgetList
;
181 m_widgetList
= new_widgetList
;
183 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
185 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
187 XtAddCallback (w
, XmNactivateCallback
, (XtCallbackProc
) wxChoiceCallback
, (XtPointer
) this);
189 if (m_noStrings
== 0 && m_buttonWidget
)
191 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
192 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
193 XmString text
= XmStringCreateSimple ((char*) (const char*) item
);
194 XtVaSetValues (label
,
195 XmNlabelString
, text
,
199 wxNode
*node
= m_stringList
.Add (item
);
200 XtVaSetValues (w
, XmNuserData
, node
->Data (), NULL
);
205 void wxChoice::Delete(int n
)
207 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
209 // What should we do -- remove the callback for this button widget,
210 // delete the m_stringList entry, delete the button widget, construct a new widget list
217 void wxChoice::Clear()
219 m_stringList
.Clear ();
221 for (i
= 0; i
< m_noStrings
; i
++)
223 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
224 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
227 delete[] m_widgetList
;
228 m_widgetList
= (WXWidget
*) NULL
;
230 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
234 int wxChoice::GetSelection() const
238 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
239 XtVaGetValues (label
,
240 XmNlabelString
, &text
,
243 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
246 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
248 char *s1
= (char *) node
->Data ();
249 if (s1
== s
|| strcmp (s1
, s
) == 0)
267 void wxChoice::SetSelection(int n
)
271 wxNode
*node
= m_stringList
.Nth (n
);
274 Dimension selectionWidth
, selectionHeight
;
276 char *s
= (char *) node
->Data ();
277 XmString text
= XmStringCreateSimple (s
);
278 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
279 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
280 XtVaSetValues (label
,
281 XmNlabelString
, text
,
284 XtVaSetValues ((Widget
) m_buttonWidget
,
285 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
286 XmNmenuHistory
, (Widget
) m_widgetList
[n
], NULL
);
288 m_inSetValue
= FALSE
;
291 int wxChoice::FindString(const wxString
& s
) const
294 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
296 char *s1
= (char *) node
->Data ();
307 wxString
wxChoice::GetString(int n
) const
309 wxNode
*node
= m_stringList
.Nth (n
);
311 return wxString((char *) node
->Data ());
313 return wxEmptyString
;
316 void wxChoice::SetColumns(int n
)
320 short numColumns
= n
;
323 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
324 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
325 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
328 int wxChoice::GetColumns(void) const
332 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
336 void wxChoice::SetFocus()
338 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
341 void wxChoice::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
343 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
344 bool managed
= XtIsManaged((Widget
) m_formWidget
);
347 XtUnmanageChild ((Widget
) m_formWidget
);
349 int actualWidth
= width
, actualHeight
= height
;
354 for (i
= 0; i
< m_noStrings
; i
++)
355 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
356 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
362 for (i
= 0; i
< m_noStrings
; i
++)
363 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
364 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
369 XtManageChild ((Widget
) m_formWidget
);
370 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
372 wxControl::SetSize (x
, y
, width
, height
, sizeFlags
);
375 wxString
wxChoice::GetStringSelection () const
377 int sel
= GetSelection ();
379 return wxString(this->GetString (sel
));
381 return wxEmptyString
;
384 bool wxChoice::SetStringSelection (const wxString
& s
)
386 int sel
= FindString (s
);
396 void wxChoice::Command(wxCommandEvent
& event
)
398 SetSelection (event
.GetInt());
399 ProcessCommand (event
);
402 void wxChoiceCallback (Widget w
, XtPointer clientData
,
405 wxChoice
*item
= (wxChoice
*) clientData
;
408 if (item
->InSetValue())
412 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
415 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
);
416 event
.m_commandInt
= item
->FindString (s
);
417 // event.m_commandString = s;
418 item
->ProcessCommand (event
);
423 void wxChoice::ChangeFont()
425 // Note that this causes the widget to be resized back
426 // to its original size! We therefore have to set the size
427 // back again. TODO: a better way in Motif?
428 if (m_windowFont
.Ok())
430 int width
, height
, width1
, height1
;
431 GetSize(& width
, & height
);
433 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_mainWidget
));
434 XtVaSetValues ((Widget
) m_mainWidget
, XmNfontList
, fontList
, NULL
);
435 XtVaSetValues ((Widget
) m_buttonWidget
, XmNfontList
, fontList
, NULL
);
438 for (i
= 0; i
< m_noStrings
; i
++)
439 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNfontList
, fontList
, NULL
);
440 GetSize(& width1
, & height1
);
441 if (width
!= width1
|| height
!= height1
)
443 SetSize(-1, -1, width
, height
);
448 void wxChoice::ChangeBackgroundColour()
450 DoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
451 DoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
452 DoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
454 for (i
= 0; i
< m_noStrings
; i
++)
455 DoChangeBackgroundColour(m_widgetList
[i
], m_backgroundColour
);
458 void wxChoice::ChangeForegroundColour()
460 DoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
461 DoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
462 DoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
464 for (i
= 0; i
< m_noStrings
; i
++)
465 DoChangeForegroundColour(m_widgetList
[i
], m_foregroundColour
);