1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
23 #include "wx/choice.h"
27 #pragma message disable nosimpint
30 #include <Xm/PushBG.h>
32 #include <Xm/RowColumn.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
41 void wxChoiceCallback (Widget w
, XtPointer clientData
,
54 m_buttonWidget
= (WXWidget
) 0;
55 m_menuWidget
= (WXWidget
) 0;
56 m_formWidget
= (WXWidget
) 0;
59 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
62 int n
, const wxString choices
[],
64 const wxValidator
& validator
,
67 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
70 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
72 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
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
,
86 "choiceMenu", NULL
, 0);
91 for (i
= 0; i
< n
; i
++)
101 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
102 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
103 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
104 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
105 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
109 m_mainWidget
= m_buttonWidget
;
111 XtManageChild ((Widget
) m_buttonWidget
);
113 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
114 // Some time ago, I reported a problem with wxChoice-items under
115 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
116 // that I have found the code responsible for this behaviour.
117 #if XmVersion >= 1002
119 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
120 // in controls sample.
122 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
123 // XtUnmanageChild (optionLabel);
127 wxSize bestSize
= GetBestSize();
128 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
129 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
131 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
135 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
136 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
138 ChangeBackgroundColour();
143 wxChoice::~wxChoice()
145 // For some reason destroying the menuWidget
146 // can cause crashes on some machines. It will
147 // be deleted implicitly by deleting the parent form
149 // XtDestroyWidget (menuWidget);
153 DetachWidget(GetMainWidget()); // Removes event handlers
154 DetachWidget(m_formWidget
);
156 XtDestroyWidget((Widget
) m_formWidget
);
157 m_formWidget
= (WXWidget
) 0;
159 // Presumably the other widgets have been deleted now, via the form
160 m_mainWidget
= (WXWidget
) 0;
161 m_buttonWidget
= (WXWidget
) 0;
163 if ( HasClientObjectData() )
164 m_clientDataDict
.DestroyData();
167 int wxChoice::DoAppend(const wxString
& item
)
169 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
171 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
173 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
177 DoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
181 XmNfontList
, (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
184 m_widgetArray
.Add(w
);
186 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
188 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
190 XtAddCallback (w
, XmNactivateCallback
,
191 (XtCallbackProc
) wxChoiceCallback
,
194 if (m_noStrings
== 0 && m_buttonWidget
)
196 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
197 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
198 wxXmString
text( item
);
199 XtVaSetValues (label
,
200 XmNlabelString
, text(),
203 m_stringList
.Add(item
);
206 return GetCount() - 1;
209 void wxChoice::Delete(int n
)
211 Widget w
= (Widget
)m_widgetArray
[n
];
212 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
214 m_stringList
.DeleteNode(m_stringList
.Item(n
));
215 m_widgetArray
.RemoveAt(size_t(n
));
216 m_clientDataDict
.Delete(n
, HasClientObjectData());
222 void wxChoice::Clear()
224 m_stringList
.Clear ();
226 for (i
= 0; i
< m_noStrings
; i
++)
228 XtRemoveCallback((Widget
) m_widgetArray
[i
],
229 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
231 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
232 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
234 m_widgetArray
.Clear();
236 XtVaSetValues ((Widget
) m_buttonWidget
,
237 XmNmenuHistory
, (Widget
) NULL
,
240 if ( HasClientObjectData() )
241 m_clientDataDict
.DestroyData();
246 int wxChoice::GetSelection() const
250 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
251 XtVaGetValues (label
,
252 XmNlabelString
, &text
,
255 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
258 for (wxStringListNode
* node
= m_stringList
.GetFirst ();
259 node
; node
= node
->GetNext ())
261 if (strcmp(node
->GetData(), s
) == 0)
279 void wxChoice::SetSelection(int n
)
283 wxStringListNode
*node
= m_stringList
.Item(n
);
287 Dimension selectionWidth
, selectionHeight
;
289 wxXmString
text( (char*)node
->Data() );
290 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
291 // will remove "soon"
293 XtVaGetValues ((Widget
) m_widgetArray
[n
],
294 XmNwidth
, &selectionWidth
,
295 XmNheight
, &selectionHeight
,
298 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
299 XtVaSetValues (label
,
300 XmNlabelString
, text(),
303 XtVaSetValues ((Widget
) m_buttonWidget
,
304 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
305 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
308 m_inSetValue
= FALSE
;
311 int wxChoice::FindString(const wxString
& s
) const
314 for (wxStringListNode
* node
= m_stringList
.GetFirst();
315 node
; node
= node
->GetNext ())
317 if (s
== node
->GetData())
326 wxString
wxChoice::GetString(int n
) const
328 wxStringListNode
*node
= m_stringList
.Item(n
);
330 return node
->GetData();
332 return wxEmptyString
;
335 void wxChoice::SetColumns(int n
)
339 short numColumns
= n
;
342 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
343 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
344 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
347 int wxChoice::GetColumns(void) const
351 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
355 void wxChoice::SetFocus()
357 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
360 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
362 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
363 bool managed
= XtIsManaged((Widget
) m_formWidget
);
366 XtUnmanageChild ((Widget
) m_formWidget
);
368 int actualWidth
= width
, actualHeight
= height
;
373 for (i
= 0; i
< m_noStrings
; i
++)
374 XtVaSetValues ((Widget
) m_widgetArray
[i
],
375 XmNwidth
, actualWidth
,
377 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
383 for (i
= 0; i
< m_noStrings
; i
++)
384 XtVaSetValues ((Widget
) m_widgetArray
[i
],
385 XmNheight
, actualHeight
,
387 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
392 XtManageChild ((Widget
) m_formWidget
);
393 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
395 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
398 void wxChoice::Command(wxCommandEvent
& event
)
400 SetSelection (event
.GetInt());
401 ProcessCommand (event
);
404 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
406 wxChoice
*item
= (wxChoice
*) clientData
;
409 if (item
->InSetValue())
412 int n
= item
->GetWidgets().Index(w
);
413 if (n
!= wxNOT_FOUND
)
415 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
416 event
.SetEventObject(item
);
417 event
.m_commandInt
= n
;
418 event
.m_commandString
= item
->GetStrings().Item(n
)->GetData();
419 if ( item
->HasClientObjectData() )
420 event
.SetClientObject( item
->GetClientObject(n
) );
421 else if ( item
->HasClientUntypedData() )
422 event
.SetClientData( item
->GetClientData(n
) );
423 item
->ProcessCommand (event
);
428 void wxChoice::ChangeFont(bool keepOriginalSize
)
430 // Note that this causes the widget to be resized back
431 // to its original size! We therefore have to set the size
432 // back again. TODO: a better way in Motif?
435 int width
, height
, width1
, height1
;
436 GetSize(& width
, & height
);
438 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_mainWidget
));
439 XtVaSetValues ((Widget
) m_formWidget
, XmNfontList
, fontList
, NULL
);
440 XtVaSetValues ((Widget
) m_buttonWidget
, XmNfontList
, fontList
, NULL
);
442 for( size_t i
= 0; i
< m_noStrings
; ++i
)
443 XtVaSetValues( (Widget
)m_widgetArray
[i
],
444 XmNfontList
, fontList
,
447 GetSize(& width1
, & height1
);
448 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
450 SetSize(-1, -1, width
, height
);
455 void wxChoice::ChangeBackgroundColour()
457 DoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
458 DoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
459 DoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
461 for (i
= 0; i
< m_noStrings
; i
++)
462 DoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
465 void wxChoice::ChangeForegroundColour()
467 DoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
468 DoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
469 DoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
471 for (i
= 0; i
< m_noStrings
; i
++)
472 DoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
475 int wxChoice::GetCount() const
480 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
482 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, FALSE
);
485 void* wxChoice::DoGetItemClientData(int n
) const
487 return (void*)m_clientDataDict
.Get(n
);
490 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
492 // don't delete, wxItemContainer does that for us
493 m_clientDataDict
.Set(n
, clientData
, FALSE
);
496 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
498 return m_clientDataDict
.Get(n
);
501 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
503 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
506 wxSize
wxChoice::GetItemsSize() const
508 int x
, y
, mx
= 0, my
= 0;
511 GetTextExtent( "|", &x
, &my
);
513 wxStringList::Node
* curr
= m_stringList
.GetFirst();
516 GetTextExtent( curr
->GetData(), &x
, &y
);
519 curr
= curr
->GetNext();
522 return wxSize( mx
, my
);
525 wxSize
wxChoice::DoGetBestSize() const
527 wxSize items
= GetItemsSize();
528 // FIXME arbitrary constants
529 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),