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"
25 #include "wx/arrstr.h"
28 #pragma message disable nosimpint
31 #include <Xm/PushBG.h>
33 #include <Xm/RowColumn.h>
35 #pragma message enable nosimpint
38 #include "wx/motif/private.h"
40 #define WIDTH_OVERHEAD 48
41 #define WIDTH_OVERHEAD_SUBTRACT 40
42 #define HEIGHT_OVERHEAD 15
44 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
46 void wxChoiceCallback (Widget w
, XtPointer clientData
,
57 m_buttonWidget
= (WXWidget
) 0;
58 m_menuWidget
= (WXWidget
) 0;
59 m_formWidget
= (WXWidget
) 0;
62 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
65 int n
, const wxString choices
[],
67 const wxValidator
& validator
,
70 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
73 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
75 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
76 xmRowColumnWidgetClass
, parentWidget
,
79 XmNpacking
, XmPACK_TIGHT
,
80 XmNorientation
, XmHORIZONTAL
,
81 XmNresizeWidth
, False
,
82 XmNresizeHeight
, False
,
85 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
88 * Create the popup menu
90 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
91 "choiceMenu", NULL
, 0);
96 for (i
= 0; i
< n
; i
++)
106 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
107 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
108 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
109 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
110 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
114 m_mainWidget
= m_buttonWidget
;
116 XtManageChild ((Widget
) m_buttonWidget
);
118 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
119 // Some time ago, I reported a problem with wxChoice-items under
120 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
121 // that I have found the code responsible for this behaviour.
122 #if XmVersion >= 1002
124 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
125 // in controls sample.
127 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
128 // XtUnmanageChild (optionLabel);
132 wxSize bestSize
= GetBestSize();
133 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
134 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
136 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
139 ChangeBackgroundColour();
141 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
142 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
147 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
150 const wxArrayString
& choices
,
152 const wxValidator
& validator
,
153 const wxString
& name
)
155 wxCArrayString
chs(choices
);
156 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
157 style
, validator
, name
);
160 wxChoice::~wxChoice()
162 // For some reason destroying the menuWidget
163 // can cause crashes on some machines. It will
164 // be deleted implicitly by deleting the parent form
166 // XtDestroyWidget (menuWidget);
170 DetachWidget(GetMainWidget()); // Removes event handlers
171 DetachWidget(m_formWidget
);
173 XtDestroyWidget((Widget
) m_formWidget
);
174 m_formWidget
= (WXWidget
) 0;
176 // Presumably the other widgets have been deleted now, via the form
177 m_mainWidget
= (WXWidget
) 0;
178 m_buttonWidget
= (WXWidget
) 0;
180 if ( HasClientObjectData() )
181 m_clientDataDict
.DestroyData();
184 int wxChoice::DoAppend(const wxString
& item
)
186 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
188 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
190 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
194 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
197 wxDoChangeFont( w
, m_font
);
199 m_widgetArray
.Add(w
);
201 char mnem
= wxFindMnemonic (item
);
203 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
205 XtAddCallback (w
, XmNactivateCallback
,
206 (XtCallbackProc
) wxChoiceCallback
,
209 if (m_noStrings
== 0 && m_buttonWidget
)
211 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
212 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
213 wxXmString
text( item
);
214 XtVaSetValues (label
,
215 XmNlabelString
, text(),
218 m_stringList
.Add(item
);
221 return GetCount() - 1;
224 int wxChoice::DoInsert(const wxString
& item
, int pos
)
226 wxCHECK_MSG(FALSE
, -1, wxT("insert not implemented"));
228 // wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
229 // if (pos == GetCount()) return DoAppend(item);
232 void wxChoice::Delete(int n
)
234 Widget w
= (Widget
)m_widgetArray
[n
];
235 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
237 m_stringList
.Erase(m_stringList
.Item(n
));
238 m_widgetArray
.RemoveAt(size_t(n
));
239 m_clientDataDict
.Delete(n
, HasClientObjectData());
245 void wxChoice::Clear()
247 m_stringList
.Clear ();
249 for (i
= 0; i
< m_noStrings
; i
++)
251 XtRemoveCallback((Widget
) m_widgetArray
[i
],
252 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
254 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
255 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
257 m_widgetArray
.Clear();
259 XtVaSetValues ((Widget
) m_buttonWidget
,
260 XmNmenuHistory
, (Widget
) NULL
,
263 if ( HasClientObjectData() )
264 m_clientDataDict
.DestroyData();
269 int wxChoice::GetSelection() const
272 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
273 XtVaGetValues (label
,
274 XmNlabelString
, &text
,
276 wxXmString
freeMe(text
);
277 wxString s
= wxXmStringToString( text
);
282 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
283 node
; node
= node
->GetNext ())
285 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
298 void wxChoice::SetSelection(int n
)
302 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
306 Dimension selectionWidth
, selectionHeight
;
308 wxXmString
text( node
->GetData() );
309 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
310 // will remove "soon"
312 XtVaGetValues ((Widget
) m_widgetArray
[n
],
313 XmNwidth
, &selectionWidth
,
314 XmNheight
, &selectionHeight
,
317 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
318 XtVaSetValues (label
,
319 XmNlabelString
, text(),
322 XtVaSetValues ((Widget
) m_buttonWidget
,
323 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
324 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
327 m_inSetValue
= FALSE
;
330 int wxChoice::FindString(const wxString
& s
) const
333 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst();
334 node
; node
= node
->GetNext ())
336 if (s
== node
->GetData())
345 wxString
wxChoice::GetString(int n
) const
347 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
349 return node
->GetData();
351 return wxEmptyString
;
354 void wxChoice::SetColumns(int n
)
358 short numColumns
= n
;
361 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
362 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
363 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
366 int wxChoice::GetColumns(void) const
370 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
374 void wxChoice::SetFocus()
376 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
379 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
381 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
382 bool managed
= XtIsManaged((Widget
) m_formWidget
);
385 XtUnmanageChild ((Widget
) m_formWidget
);
387 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
388 actualHeight
= height
- HEIGHT_OVERHEAD
;
393 for (i
= 0; i
< m_noStrings
; i
++)
394 XtVaSetValues ((Widget
) m_widgetArray
[i
],
395 XmNwidth
, actualWidth
,
397 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
404 for (i
= 0; i
< m_noStrings
; i
++)
405 XtVaSetValues ((Widget
) m_widgetArray
[i
],
406 XmNheight
, actualHeight
,
409 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
414 XtManageChild ((Widget
) m_formWidget
);
415 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
417 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
420 void wxChoice::Command(wxCommandEvent
& event
)
422 SetSelection (event
.GetInt());
423 ProcessCommand (event
);
426 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
428 wxChoice
*item
= (wxChoice
*) clientData
;
431 if (item
->InSetValue())
434 int n
= item
->GetWidgets().Index(w
);
435 if (n
!= wxNOT_FOUND
)
437 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
438 event
.SetEventObject(item
);
439 event
.m_commandInt
= n
;
440 event
.m_commandString
= item
->GetStrings().Item(n
)->GetData();
441 if ( item
->HasClientObjectData() )
442 event
.SetClientObject( item
->GetClientObject(n
) );
443 else if ( item
->HasClientUntypedData() )
444 event
.SetClientData( item
->GetClientData(n
) );
445 item
->ProcessCommand (event
);
450 void wxChoice::ChangeFont(bool keepOriginalSize
)
452 // Note that this causes the widget to be resized back
453 // to its original size! We therefore have to set the size
454 // back again. TODO: a better way in Motif?
457 int width
, height
, width1
, height1
;
458 GetSize(& width
, & height
);
460 WXFontType fontType
=
461 m_font
.GetFontType(XtDisplay((Widget
) m_mainWidget
));
462 WXString fontTag
= wxFont::GetFontTag();
464 XtVaSetValues ((Widget
) m_formWidget
, fontTag
, fontType
, NULL
);
465 XtVaSetValues ((Widget
) m_buttonWidget
, fontTag
, fontType
, NULL
);
467 for( size_t i
= 0; i
< m_noStrings
; ++i
)
468 XtVaSetValues( (Widget
)m_widgetArray
[i
],
472 GetSize(& width1
, & height1
);
473 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
475 SetSize(-1, -1, width
, height
);
480 void wxChoice::ChangeBackgroundColour()
482 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
483 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
484 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
486 for (i
= 0; i
< m_noStrings
; i
++)
487 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
490 void wxChoice::ChangeForegroundColour()
492 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
493 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
494 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
496 for (i
= 0; i
< m_noStrings
; i
++)
497 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
500 int wxChoice::GetCount() const
505 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
507 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, FALSE
);
510 void* wxChoice::DoGetItemClientData(int n
) const
512 return (void*)m_clientDataDict
.Get(n
);
515 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
517 // don't delete, wxItemContainer does that for us
518 m_clientDataDict
.Set(n
, clientData
, FALSE
);
521 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
523 return m_clientDataDict
.Get(n
);
526 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
528 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
531 wxSize
wxChoice::GetItemsSize() const
533 int x
, y
, mx
= 0, my
= 0;
536 GetTextExtent( "|", &x
, &my
);
538 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
541 GetTextExtent( curr
->GetData(), &x
, &y
);
544 curr
= curr
->GetNext();
547 return wxSize( mx
, my
);
550 wxSize
wxChoice::DoGetBestSize() const
552 wxSize items
= GetItemsSize();
553 // FIXME arbitrary constants
554 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
555 items
.y
+ HEIGHT_OVERHEAD
);