1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #define WIDTH_OVERHEAD 48
40 #define WIDTH_OVERHEAD_SUBTRACT 40
41 #define HEIGHT_OVERHEAD 15
43 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
45 void wxChoiceCallback (Widget w
, XtPointer clientData
,
56 m_buttonWidget
= (WXWidget
) 0;
57 m_menuWidget
= (WXWidget
) 0;
58 m_formWidget
= (WXWidget
) 0;
61 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
64 int n
, const wxString choices
[],
66 const wxValidator
& validator
,
69 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
72 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
74 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
75 xmRowColumnWidgetClass
, parentWidget
,
78 XmNpacking
, XmPACK_TIGHT
,
79 XmNorientation
, XmHORIZONTAL
,
80 XmNresizeWidth
, False
,
81 XmNresizeHeight
, False
,
84 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
87 * Create the popup menu
89 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
90 "choiceMenu", NULL
, 0);
95 for (i
= 0; i
< n
; i
++)
105 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
106 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
107 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
108 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
109 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
113 m_mainWidget
= m_buttonWidget
;
115 XtManageChild ((Widget
) m_buttonWidget
);
117 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
118 // Some time ago, I reported a problem with wxChoice-items under
119 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
120 // that I have found the code responsible for this behaviour.
121 #if XmVersion >= 1002
123 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
124 // in controls sample.
126 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
127 // XtUnmanageChild (optionLabel);
131 wxSize bestSize
= GetBestSize();
132 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
133 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
135 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
138 ChangeBackgroundColour();
140 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
141 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
146 wxChoice::~wxChoice()
148 // For some reason destroying the menuWidget
149 // can cause crashes on some machines. It will
150 // be deleted implicitly by deleting the parent form
152 // XtDestroyWidget (menuWidget);
156 DetachWidget(GetMainWidget()); // Removes event handlers
157 DetachWidget(m_formWidget
);
159 XtDestroyWidget((Widget
) m_formWidget
);
160 m_formWidget
= (WXWidget
) 0;
162 // Presumably the other widgets have been deleted now, via the form
163 m_mainWidget
= (WXWidget
) 0;
164 m_buttonWidget
= (WXWidget
) 0;
166 if ( HasClientObjectData() )
167 m_clientDataDict
.DestroyData();
170 int wxChoice::DoAppend(const wxString
& item
)
172 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
174 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
176 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
180 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
183 wxDoChangeFont( w
, m_font
);
185 m_widgetArray
.Add(w
);
187 char mnem
= wxFindMnemonic (item
);
189 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
191 XtAddCallback (w
, XmNactivateCallback
,
192 (XtCallbackProc
) wxChoiceCallback
,
195 if (m_noStrings
== 0 && m_buttonWidget
)
197 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
198 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
199 wxXmString
text( item
);
200 XtVaSetValues (label
,
201 XmNlabelString
, text(),
204 m_stringList
.Add(item
);
207 return GetCount() - 1;
210 int wxChoice::DoInsert(const wxString
& item
, int pos
)
212 wxCHECK_MSG(FALSE
, -1, wxT("insert not implemented"));
214 // wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
215 // if (pos == GetCount()) return DoAppend(item);
218 void wxChoice::Delete(int n
)
220 Widget w
= (Widget
)m_widgetArray
[n
];
221 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
223 m_stringList
.Erase(m_stringList
.Item(n
));
224 m_widgetArray
.RemoveAt(size_t(n
));
225 m_clientDataDict
.Delete(n
, HasClientObjectData());
231 void wxChoice::Clear()
233 m_stringList
.Clear ();
235 for (i
= 0; i
< m_noStrings
; i
++)
237 XtRemoveCallback((Widget
) m_widgetArray
[i
],
238 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
240 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
241 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
243 m_widgetArray
.Clear();
245 XtVaSetValues ((Widget
) m_buttonWidget
,
246 XmNmenuHistory
, (Widget
) NULL
,
249 if ( HasClientObjectData() )
250 m_clientDataDict
.DestroyData();
255 int wxChoice::GetSelection() const
258 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
259 XtVaGetValues (label
,
260 XmNlabelString
, &text
,
262 wxXmString
freeMe(text
);
263 wxString s
= wxXmStringToString( text
);
268 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
269 node
; node
= node
->GetNext ())
271 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
284 void wxChoice::SetSelection(int n
)
288 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
292 Dimension selectionWidth
, selectionHeight
;
294 wxXmString
text( node
->GetData() );
295 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
296 // will remove "soon"
298 XtVaGetValues ((Widget
) m_widgetArray
[n
],
299 XmNwidth
, &selectionWidth
,
300 XmNheight
, &selectionHeight
,
303 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
304 XtVaSetValues (label
,
305 XmNlabelString
, text(),
308 XtVaSetValues ((Widget
) m_buttonWidget
,
309 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
310 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
313 m_inSetValue
= FALSE
;
316 int wxChoice::FindString(const wxString
& s
) const
319 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst();
320 node
; node
= node
->GetNext ())
322 if (s
== node
->GetData())
331 wxString
wxChoice::GetString(int n
) const
333 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
335 return node
->GetData();
337 return wxEmptyString
;
340 void wxChoice::SetColumns(int n
)
344 short numColumns
= n
;
347 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
348 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
349 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
352 int wxChoice::GetColumns(void) const
356 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
360 void wxChoice::SetFocus()
362 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
365 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
367 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
368 bool managed
= XtIsManaged((Widget
) m_formWidget
);
371 XtUnmanageChild ((Widget
) m_formWidget
);
373 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
374 actualHeight
= height
- HEIGHT_OVERHEAD
;
379 for (i
= 0; i
< m_noStrings
; i
++)
380 XtVaSetValues ((Widget
) m_widgetArray
[i
],
381 XmNwidth
, actualWidth
,
383 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
390 for (i
= 0; i
< m_noStrings
; i
++)
391 XtVaSetValues ((Widget
) m_widgetArray
[i
],
392 XmNheight
, actualHeight
,
395 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
400 XtManageChild ((Widget
) m_formWidget
);
401 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
403 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
406 void wxChoice::Command(wxCommandEvent
& event
)
408 SetSelection (event
.GetInt());
409 ProcessCommand (event
);
412 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
414 wxChoice
*item
= (wxChoice
*) clientData
;
417 if (item
->InSetValue())
420 int n
= item
->GetWidgets().Index(w
);
421 if (n
!= wxNOT_FOUND
)
423 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
424 event
.SetEventObject(item
);
425 event
.m_commandInt
= n
;
426 event
.m_commandString
= item
->GetStrings().Item(n
)->GetData();
427 if ( item
->HasClientObjectData() )
428 event
.SetClientObject( item
->GetClientObject(n
) );
429 else if ( item
->HasClientUntypedData() )
430 event
.SetClientData( item
->GetClientData(n
) );
431 item
->ProcessCommand (event
);
436 void wxChoice::ChangeFont(bool keepOriginalSize
)
438 // Note that this causes the widget to be resized back
439 // to its original size! We therefore have to set the size
440 // back again. TODO: a better way in Motif?
443 int width
, height
, width1
, height1
;
444 GetSize(& width
, & height
);
446 WXFontType fontType
=
447 m_font
.GetFontType(XtDisplay((Widget
) m_mainWidget
));
448 WXString fontTag
= wxFont::GetFontTag();
450 XtVaSetValues ((Widget
) m_formWidget
, fontTag
, fontType
, NULL
);
451 XtVaSetValues ((Widget
) m_buttonWidget
, fontTag
, fontType
, NULL
);
453 for( size_t i
= 0; i
< m_noStrings
; ++i
)
454 XtVaSetValues( (Widget
)m_widgetArray
[i
],
458 GetSize(& width1
, & height1
);
459 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
461 SetSize(-1, -1, width
, height
);
466 void wxChoice::ChangeBackgroundColour()
468 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
469 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
470 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
472 for (i
= 0; i
< m_noStrings
; i
++)
473 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
476 void wxChoice::ChangeForegroundColour()
478 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
479 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
480 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
482 for (i
= 0; i
< m_noStrings
; i
++)
483 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
486 int wxChoice::GetCount() const
491 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
493 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, FALSE
);
496 void* wxChoice::DoGetItemClientData(int n
) const
498 return (void*)m_clientDataDict
.Get(n
);
501 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
503 // don't delete, wxItemContainer does that for us
504 m_clientDataDict
.Set(n
, clientData
, FALSE
);
507 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
509 return m_clientDataDict
.Get(n
);
512 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
514 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
517 wxSize
wxChoice::GetItemsSize() const
519 int x
, y
, mx
= 0, my
= 0;
522 GetTextExtent( "|", &x
, &my
);
524 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
527 GetTextExtent( curr
->GetData(), &x
, &y
);
530 curr
= curr
->GetNext();
533 return wxSize( mx
, my
);
536 wxSize
wxChoice::DoGetBestSize() const
538 wxSize items
= GetItemsSize();
539 // FIXME arbitrary constants
540 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
541 items
.y
+ HEIGHT_OVERHEAD
);