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"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
21 #define XtParent XTPARENT
26 #include "wx/choice.h"
28 #include "wx/arrstr.h"
31 #pragma message disable nosimpint
34 #include <Xm/PushBG.h>
36 #include <Xm/RowColumn.h>
38 #pragma message enable nosimpint
41 #include "wx/motif/private.h"
43 #define WIDTH_OVERHEAD 48
44 #define WIDTH_OVERHEAD_SUBTRACT 40
45 #define HEIGHT_OVERHEAD 15
47 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
49 void wxChoiceCallback (Widget w
, XtPointer clientData
,
60 m_buttonWidget
= (WXWidget
) 0;
61 m_menuWidget
= (WXWidget
) 0;
62 m_formWidget
= (WXWidget
) 0;
65 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
68 int n
, const wxString choices
[],
70 const wxValidator
& validator
,
73 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
76 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
78 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
79 xmRowColumnWidgetClass
, parentWidget
,
82 XmNpacking
, XmPACK_TIGHT
,
83 XmNorientation
, XmHORIZONTAL
,
84 XmNresizeWidth
, False
,
85 XmNresizeHeight
, False
,
88 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
91 * Create the popup menu
93 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
94 "choiceMenu", NULL
, 0);
99 for (i
= 0; i
< n
; i
++)
109 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
110 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
111 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
112 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
113 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
117 m_mainWidget
= m_buttonWidget
;
119 XtManageChild ((Widget
) m_buttonWidget
);
121 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
122 // Some time ago, I reported a problem with wxChoice-items under
123 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
124 // that I have found the code responsible for this behaviour.
125 #if XmVersion >= 1002
127 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
128 // in controls sample.
130 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
131 // XtUnmanageChild (optionLabel);
135 wxSize bestSize
= GetBestSize();
136 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
137 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
139 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
142 ChangeBackgroundColour();
144 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
145 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
150 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
153 const wxArrayString
& choices
,
155 const wxValidator
& validator
,
156 const wxString
& name
)
158 wxCArrayString
chs(choices
);
159 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
160 style
, validator
, name
);
163 wxChoice::~wxChoice()
165 // For some reason destroying the menuWidget
166 // can cause crashes on some machines. It will
167 // be deleted implicitly by deleting the parent form
169 // XtDestroyWidget (menuWidget);
173 DetachWidget(GetMainWidget()); // Removes event handlers
174 DetachWidget(m_formWidget
);
176 XtDestroyWidget((Widget
) m_formWidget
);
177 m_formWidget
= (WXWidget
) 0;
179 // Presumably the other widgets have been deleted now, via the form
180 m_mainWidget
= (WXWidget
) 0;
181 m_buttonWidget
= (WXWidget
) 0;
183 if ( HasClientObjectData() )
184 m_clientDataDict
.DestroyData();
187 int wxChoice::DoAppend(const wxString
& item
)
189 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
191 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
193 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
197 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
200 wxDoChangeFont( w
, m_font
);
202 m_widgetArray
.Add(w
);
204 char mnem
= wxFindMnemonic (item
);
206 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
208 XtAddCallback (w
, XmNactivateCallback
,
209 (XtCallbackProc
) wxChoiceCallback
,
212 if (m_noStrings
== 0 && m_buttonWidget
)
214 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
215 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
216 wxXmString
text( item
);
217 XtVaSetValues (label
,
218 XmNlabelString
, text(),
221 m_stringList
.Add(item
);
224 return GetCount() - 1;
227 int wxChoice::DoInsert(const wxString
& item
, int pos
)
229 wxCHECK_MSG(FALSE
, -1, wxT("insert not implemented"));
231 // wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
232 // if (pos == GetCount()) return DoAppend(item);
235 void wxChoice::Delete(int n
)
237 Widget w
= (Widget
)m_widgetArray
[n
];
238 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
240 m_stringList
.Erase(m_stringList
.Item(n
));
241 m_widgetArray
.RemoveAt(size_t(n
));
242 m_clientDataDict
.Delete(n
, HasClientObjectData());
248 void wxChoice::Clear()
250 m_stringList
.Clear ();
252 for (i
= 0; i
< m_noStrings
; i
++)
254 XtRemoveCallback((Widget
) m_widgetArray
[i
],
255 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
257 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
258 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
260 m_widgetArray
.Clear();
262 XtVaSetValues ((Widget
) m_buttonWidget
,
263 XmNmenuHistory
, (Widget
) NULL
,
266 if ( HasClientObjectData() )
267 m_clientDataDict
.DestroyData();
272 int wxChoice::GetSelection() const
275 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
276 XtVaGetValues (label
,
277 XmNlabelString
, &text
,
279 wxXmString
freeMe(text
);
280 wxString s
= wxXmStringToString( text
);
285 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
286 node
; node
= node
->GetNext ())
288 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
301 void wxChoice::SetSelection(int n
)
305 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
309 Dimension selectionWidth
, selectionHeight
;
311 wxXmString
text( node
->GetData() );
312 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
313 // will remove "soon"
315 XtVaGetValues ((Widget
) m_widgetArray
[n
],
316 XmNwidth
, &selectionWidth
,
317 XmNheight
, &selectionHeight
,
320 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
321 XtVaSetValues (label
,
322 XmNlabelString
, text(),
325 XtVaSetValues ((Widget
) m_buttonWidget
,
326 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
327 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
330 m_inSetValue
= FALSE
;
333 int wxChoice::FindString(const wxString
& s
) const
336 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst();
337 node
; node
= node
->GetNext ())
339 if (s
== node
->GetData())
348 wxString
wxChoice::GetString(int n
) const
350 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
352 return node
->GetData();
354 return wxEmptyString
;
357 void wxChoice::SetColumns(int n
)
361 short numColumns
= n
;
364 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
365 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
366 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
369 int wxChoice::GetColumns(void) const
373 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
377 void wxChoice::SetFocus()
379 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
382 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
384 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
385 bool managed
= XtIsManaged((Widget
) m_formWidget
);
388 XtUnmanageChild ((Widget
) m_formWidget
);
390 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
391 actualHeight
= height
- HEIGHT_OVERHEAD
;
396 for (i
= 0; i
< m_noStrings
; i
++)
397 XtVaSetValues ((Widget
) m_widgetArray
[i
],
398 XmNwidth
, actualWidth
,
400 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
407 for (i
= 0; i
< m_noStrings
; i
++)
408 XtVaSetValues ((Widget
) m_widgetArray
[i
],
409 XmNheight
, actualHeight
,
412 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
417 XtManageChild ((Widget
) m_formWidget
);
418 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
420 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
423 void wxChoice::Command(wxCommandEvent
& event
)
425 SetSelection (event
.GetInt());
426 ProcessCommand (event
);
429 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
431 wxChoice
*item
= (wxChoice
*) clientData
;
434 if (item
->InSetValue())
437 int n
= item
->GetWidgets().Index(w
);
438 if (n
!= wxNOT_FOUND
)
440 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
441 event
.SetEventObject(item
);
442 event
.m_commandInt
= n
;
443 event
.m_commandString
= item
->GetStrings().Item(n
)->GetData();
444 if ( item
->HasClientObjectData() )
445 event
.SetClientObject( item
->GetClientObject(n
) );
446 else if ( item
->HasClientUntypedData() )
447 event
.SetClientData( item
->GetClientData(n
) );
448 item
->ProcessCommand (event
);
453 void wxChoice::ChangeFont(bool keepOriginalSize
)
455 // Note that this causes the widget to be resized back
456 // to its original size! We therefore have to set the size
457 // back again. TODO: a better way in Motif?
460 int width
, height
, width1
, height1
;
461 GetSize(& width
, & height
);
463 WXFontType fontType
=
464 m_font
.GetFontType(XtDisplay((Widget
) m_mainWidget
));
465 WXString fontTag
= wxFont::GetFontTag();
467 XtVaSetValues ((Widget
) m_formWidget
, fontTag
, fontType
, NULL
);
468 XtVaSetValues ((Widget
) m_buttonWidget
, fontTag
, fontType
, NULL
);
470 for( size_t i
= 0; i
< m_noStrings
; ++i
)
471 XtVaSetValues( (Widget
)m_widgetArray
[i
],
475 GetSize(& width1
, & height1
);
476 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
478 SetSize(-1, -1, width
, height
);
483 void wxChoice::ChangeBackgroundColour()
485 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
486 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
487 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
489 for (i
= 0; i
< m_noStrings
; i
++)
490 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
493 void wxChoice::ChangeForegroundColour()
495 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
496 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
497 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
499 for (i
= 0; i
< m_noStrings
; i
++)
500 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
503 int wxChoice::GetCount() const
508 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
510 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, FALSE
);
513 void* wxChoice::DoGetItemClientData(int n
) const
515 return (void*)m_clientDataDict
.Get(n
);
518 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
520 // don't delete, wxItemContainer does that for us
521 m_clientDataDict
.Set(n
, clientData
, FALSE
);
524 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
526 return m_clientDataDict
.Get(n
);
529 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
531 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
534 wxSize
wxChoice::GetItemsSize() const
536 int x
, y
, mx
= 0, my
= 0;
539 GetTextExtent( "|", &x
, &my
);
541 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
544 GetTextExtent( curr
->GetData(), &x
, &y
);
547 curr
= curr
->GetNext();
550 return wxSize( mx
, my
);
553 wxSize
wxChoice::DoGetBestSize() const
555 wxSize items
= GetItemsSize();
556 // FIXME arbitrary constants
557 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
558 items
.y
+ HEIGHT_OVERHEAD
);