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
);
53 m_noStrings
= 0; // Starts off with none, incremented in Append
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();
69 m_windowFont
= parent
->GetFont();
71 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
73 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) name
,
74 xmRowColumnWidgetClass
, parentWidget
,
77 XmNpacking
, XmPACK_TIGHT
,
78 XmNorientation
, XmHORIZONTAL
,
81 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
84 * Create the popup menu
86 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
, "choiceMenu", NULL
, 0);
92 for (i
= 0; i
< n
; i
++)
102 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
);
104 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0);
106 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0);
108 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
);
110 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
, "choiceButton", args
, argcnt
);
112 m_mainWidget
= m_buttonWidget
;
114 XtManageChild ((Widget
) m_buttonWidget
);
116 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
117 // Some time ago, I reported a problem with wxChoice-items under
118 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
119 // that I have found the code responsible for this behaviour.
120 #if XmVersion >= 1002
122 Widget optionLabel
= XmOptionLabelGadget ((Widget
) m_buttonWidget
);
123 XtUnmanageChild (optionLabel
);
127 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
131 AttachWidget (parent
, m_buttonWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
133 ChangeBackgroundColour();
138 wxChoice::~wxChoice()
140 // For some reason destroying the menuWidget
141 // can cause crashes on some machines. It will
142 // be deleted implicitly by deleting the parent form
144 // XtDestroyWidget (menuWidget);
146 delete[] m_widgetList
;
148 DetachWidget(GetMainWidget()); // Removes event handlers
150 XtDestroyWidget((Widget
) m_formWidget
);
151 m_formWidget
= (WXWidget
) 0;
153 // Presumably the other widgets have been deleted now, via the form
154 m_mainWidget
= (WXWidget
) 0;
155 m_buttonWidget
= (WXWidget
) 0;
158 void wxChoice::Append(const wxString
& item
)
160 wxStripMenuCodes ((char *)(const char *)item
, wxBuffer
);
161 Widget w
= XtVaCreateManagedWidget (wxBuffer
,
163 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
165 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
169 DoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
171 if (m_windowFont
.Ok())
173 XmNfontList
, (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
176 WXWidget
*new_widgetList
= new WXWidget
[m_noStrings
+ 1];
179 for (i
= 0; i
< m_noStrings
; i
++)
180 new_widgetList
[i
] = m_widgetList
[i
];
182 new_widgetList
[m_noStrings
] = (WXWidget
) w
;
185 delete[] m_widgetList
;
186 m_widgetList
= new_widgetList
;
188 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
190 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
192 XtAddCallback (w
, XmNactivateCallback
, (XtCallbackProc
) wxChoiceCallback
, (XtPointer
) this);
194 if (m_noStrings
== 0 && m_buttonWidget
)
196 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
197 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
198 XmString text
= XmStringCreateSimple ((char*) (const char*) item
);
199 XtVaSetValues (label
,
200 XmNlabelString
, text
,
204 wxNode
*node
= m_stringList
.Add (item
);
205 XtVaSetValues (w
, XmNuserData
, node
->Data (), NULL
);
210 void wxChoice::Delete(int WXUNUSED(n
))
212 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
214 // What should we do -- remove the callback for this button widget,
215 // delete the m_stringList entry, delete the button widget, construct a new widget list
222 void wxChoice::Clear()
224 m_stringList
.Clear ();
226 for (i
= 0; i
< m_noStrings
; i
++)
228 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
229 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
232 delete[] m_widgetList
;
233 m_widgetList
= (WXWidget
*) NULL
;
235 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
239 int wxChoice::GetSelection() const
243 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
244 XtVaGetValues (label
,
245 XmNlabelString
, &text
,
248 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
251 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
253 char *s1
= (char *) node
->Data ();
254 if (s1
== s
|| strcmp (s1
, s
) == 0)
272 void wxChoice::SetSelection(int n
)
276 wxNode
*node
= m_stringList
.Nth (n
);
279 Dimension selectionWidth
, selectionHeight
;
281 char *s
= (char *) node
->Data ();
282 XmString text
= XmStringCreateSimple (s
);
283 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
284 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
285 XtVaSetValues (label
,
286 XmNlabelString
, text
,
289 XtVaSetValues ((Widget
) m_buttonWidget
,
290 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
291 XmNmenuHistory
, (Widget
) m_widgetList
[n
], NULL
);
293 m_inSetValue
= FALSE
;
296 int wxChoice::FindString(const wxString
& s
) const
299 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
301 char *s1
= (char *) node
->Data ();
312 wxString
wxChoice::GetString(int n
) const
314 wxNode
*node
= m_stringList
.Nth (n
);
316 return wxString((char *) node
->Data ());
318 return wxEmptyString
;
321 void wxChoice::SetColumns(int n
)
325 short numColumns
= n
;
328 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
329 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
330 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
333 int wxChoice::GetColumns(void) const
337 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
341 void wxChoice::SetFocus()
343 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
346 void wxChoice::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
348 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
349 bool managed
= XtIsManaged((Widget
) m_formWidget
);
352 XtUnmanageChild ((Widget
) m_formWidget
);
354 int actualWidth
= width
, actualHeight
= height
;
359 for (i
= 0; i
< m_noStrings
; i
++)
360 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
361 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
367 for (i
= 0; i
< m_noStrings
; i
++)
368 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
369 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
374 XtManageChild ((Widget
) m_formWidget
);
375 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
377 wxControl::SetSize (x
, y
, width
, height
, sizeFlags
);
380 wxString
wxChoice::GetStringSelection () const
382 int sel
= GetSelection ();
384 return wxString(this->GetString (sel
));
386 return wxEmptyString
;
389 bool wxChoice::SetStringSelection (const wxString
& s
)
391 int sel
= FindString (s
);
401 void wxChoice::Command(wxCommandEvent
& event
)
403 SetSelection (event
.GetInt());
404 ProcessCommand (event
);
407 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
409 wxChoice
*item
= (wxChoice
*) clientData
;
412 if (item
->InSetValue())
416 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
419 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
);
420 event
.m_commandInt
= item
->FindString (s
);
421 // event.m_commandString = s;
422 item
->ProcessCommand (event
);
427 void wxChoice::ChangeFont(bool keepOriginalSize
)
429 // Note that this causes the widget to be resized back
430 // to its original size! We therefore have to set the size
431 // back again. TODO: a better way in Motif?
432 if (m_windowFont
.Ok())
434 int width
, height
, width1
, height1
;
435 GetSize(& width
, & height
);
437 XmFontList fontList
= (XmFontList
) m_windowFont
.GetFontList(1.0, XtDisplay((Widget
) m_mainWidget
));
438 XtVaSetValues ((Widget
) m_mainWidget
, XmNfontList
, fontList
, NULL
);
439 XtVaSetValues ((Widget
) m_buttonWidget
, XmNfontList
, fontList
, NULL
);
441 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
443 for (i = 0; i < m_noStrings; i++)
444 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
446 GetSize(& width1
, & height1
);
447 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
449 SetSize(-1, -1, width
, height
);
454 void wxChoice::ChangeBackgroundColour()
456 DoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
457 DoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
458 DoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
460 for (i
= 0; i
< m_noStrings
; i
++)
461 DoChangeBackgroundColour(m_widgetList
[i
], m_backgroundColour
);
464 void wxChoice::ChangeForegroundColour()
466 DoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
467 DoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
468 DoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
470 for (i
= 0; i
< m_noStrings
; i
++)
471 DoChangeForegroundColour(m_widgetList
[i
], m_foregroundColour
);