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
28 #include "wx/choice.h"
30 #include "wx/arrstr.h"
33 #pragma message disable nosimpint
36 #include <Xm/PushBG.h>
38 #include <Xm/RowColumn.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
45 #define WIDTH_OVERHEAD 48
46 #define WIDTH_OVERHEAD_SUBTRACT 40
47 #define HEIGHT_OVERHEAD 15
49 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
51 void wxChoiceCallback (Widget w
, XtPointer clientData
,
62 m_buttonWidget
= (WXWidget
) 0;
63 m_menuWidget
= (WXWidget
) 0;
64 m_formWidget
= (WXWidget
) 0;
67 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
70 int n
, const wxString choices
[],
72 const wxValidator
& validator
,
75 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
78 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
80 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
81 xmRowColumnWidgetClass
, parentWidget
,
84 XmNpacking
, XmPACK_TIGHT
,
85 XmNorientation
, XmHORIZONTAL
,
86 XmNresizeWidth
, False
,
87 XmNresizeHeight
, False
,
90 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
93 * Create the popup menu
95 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
96 "choiceMenu", NULL
, 0);
101 for (i
= 0; i
< n
; i
++)
111 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
112 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
113 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
114 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
115 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
119 m_mainWidget
= m_buttonWidget
;
121 XtManageChild ((Widget
) m_buttonWidget
);
123 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
124 // Some time ago, I reported a problem with wxChoice-items under
125 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
126 // that I have found the code responsible for this behaviour.
127 #if XmVersion >= 1002
129 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
130 // in controls sample.
132 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
133 // XtUnmanageChild (optionLabel);
137 wxSize bestSize
= GetBestSize();
138 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
139 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
141 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
144 ChangeBackgroundColour();
146 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
147 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
152 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
155 const wxArrayString
& choices
,
157 const wxValidator
& validator
,
158 const wxString
& name
)
160 wxCArrayString
chs(choices
);
161 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
162 style
, validator
, name
);
165 wxChoice::~wxChoice()
167 // For some reason destroying the menuWidget
168 // can cause crashes on some machines. It will
169 // be deleted implicitly by deleting the parent form
171 // XtDestroyWidget (menuWidget);
175 DetachWidget(GetMainWidget()); // Removes event handlers
176 DetachWidget(m_formWidget
);
178 XtDestroyWidget((Widget
) m_formWidget
);
179 m_formWidget
= (WXWidget
) 0;
181 // Presumably the other widgets have been deleted now, via the form
182 m_mainWidget
= (WXWidget
) 0;
183 m_buttonWidget
= (WXWidget
) 0;
185 if ( HasClientObjectData() )
186 m_clientDataDict
.DestroyData();
189 int wxChoice::DoAppend(const wxString
& item
)
191 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
193 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
195 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
199 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
202 wxDoChangeFont( w
, m_font
);
204 m_widgetArray
.Add(w
);
206 char mnem
= wxFindMnemonic (item
);
208 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
210 XtAddCallback (w
, XmNactivateCallback
,
211 (XtCallbackProc
) wxChoiceCallback
,
214 if (m_noStrings
== 0 && m_buttonWidget
)
216 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
217 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
218 wxXmString
text( item
);
219 XtVaSetValues (label
,
220 XmNlabelString
, text(),
223 m_stringList
.Add(item
);
226 return GetCount() - 1;
229 int wxChoice::DoInsert(const wxString
& item
, int pos
)
231 wxCHECK_MSG(false, -1, wxT("insert not implemented"));
233 // wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
234 // if (pos == GetCount()) return DoAppend(item);
237 void wxChoice::Delete(int n
)
239 Widget w
= (Widget
)m_widgetArray
[n
];
240 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
242 m_stringList
.Erase(m_stringList
.Item(n
));
243 m_widgetArray
.RemoveAt(size_t(n
));
244 m_clientDataDict
.Delete(n
, HasClientObjectData());
250 void wxChoice::Clear()
252 m_stringList
.Clear ();
254 for (i
= 0; i
< m_noStrings
; i
++)
256 XtRemoveCallback((Widget
) m_widgetArray
[i
],
257 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
259 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
260 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
262 m_widgetArray
.Clear();
264 XtVaSetValues ((Widget
) m_buttonWidget
,
265 XmNmenuHistory
, (Widget
) NULL
,
268 if ( HasClientObjectData() )
269 m_clientDataDict
.DestroyData();
274 int wxChoice::GetSelection() const
277 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
278 XtVaGetValues (label
,
279 XmNlabelString
, &text
,
281 wxXmString
freeMe(text
);
282 wxString s
= wxXmStringToString( text
);
287 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
288 node
; node
= node
->GetNext ())
290 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
303 void wxChoice::SetSelection(int n
)
307 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
311 Dimension selectionWidth
, selectionHeight
;
313 wxXmString
text( node
->GetData() );
314 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
315 // will remove "soon"
317 XtVaGetValues ((Widget
) m_widgetArray
[n
],
318 XmNwidth
, &selectionWidth
,
319 XmNheight
, &selectionHeight
,
322 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
323 XtVaSetValues (label
,
324 XmNlabelString
, text(),
327 XtVaSetValues ((Widget
) m_buttonWidget
,
328 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
329 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
332 m_inSetValue
= false;
335 int wxChoice::FindString(const wxString
& s
) const
338 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst();
339 node
; node
= node
->GetNext ())
341 if (s
== node
->GetData())
350 wxString
wxChoice::GetString(int n
) const
352 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
354 return node
->GetData();
356 return wxEmptyString
;
359 void wxChoice::SetColumns(int n
)
363 short numColumns
= n
;
366 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
367 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
368 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
371 int wxChoice::GetColumns(void) const
375 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
379 void wxChoice::SetFocus()
381 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
384 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
386 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
387 bool managed
= XtIsManaged((Widget
) m_formWidget
);
390 XtUnmanageChild ((Widget
) m_formWidget
);
392 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
393 actualHeight
= height
- HEIGHT_OVERHEAD
;
398 for (i
= 0; i
< m_noStrings
; i
++)
399 XtVaSetValues ((Widget
) m_widgetArray
[i
],
400 XmNwidth
, actualWidth
,
402 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
409 for (i
= 0; i
< m_noStrings
; i
++)
410 XtVaSetValues ((Widget
) m_widgetArray
[i
],
411 XmNheight
, actualHeight
,
414 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
419 XtManageChild ((Widget
) m_formWidget
);
420 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
422 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
425 void wxChoice::Command(wxCommandEvent
& event
)
427 SetSelection (event
.GetInt());
428 ProcessCommand (event
);
431 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
433 wxChoice
*item
= (wxChoice
*) clientData
;
436 if (item
->InSetValue())
439 int n
= item
->GetWidgets().Index(w
);
440 if (n
!= wxNOT_FOUND
)
442 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
443 event
.SetEventObject(item
);
445 event
.SetString( item
->GetStrings().Item(n
)->GetData() );
446 if ( item
->HasClientObjectData() )
447 event
.SetClientObject( item
->GetClientObject(n
) );
448 else if ( item
->HasClientUntypedData() )
449 event
.SetClientData( item
->GetClientData(n
) );
450 item
->ProcessCommand (event
);
455 void wxChoice::ChangeFont(bool keepOriginalSize
)
457 // Note that this causes the widget to be resized back
458 // to its original size! We therefore have to set the size
459 // back again. TODO: a better way in Motif?
462 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
463 int width
, height
, width1
, height1
;
464 GetSize(& width
, & height
);
466 WXString fontTag
= wxFont::GetFontTag();
468 XtVaSetValues ((Widget
) m_formWidget
,
469 fontTag
, m_font
.GetFontTypeC(dpy
),
471 XtVaSetValues ((Widget
) m_buttonWidget
,
472 fontTag
, m_font
.GetFontTypeC(dpy
),
475 for( size_t i
= 0; i
< m_noStrings
; ++i
)
476 XtVaSetValues( (Widget
)m_widgetArray
[i
],
477 fontTag
, m_font
.GetFontTypeC(dpy
),
480 GetSize(& width1
, & height1
);
481 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
483 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
488 void wxChoice::ChangeBackgroundColour()
490 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
491 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
492 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
494 for (i
= 0; i
< m_noStrings
; i
++)
495 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
498 void wxChoice::ChangeForegroundColour()
500 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
501 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
502 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
504 for (i
= 0; i
< m_noStrings
; i
++)
505 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
508 int wxChoice::GetCount() const
513 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
515 m_clientDataDict
.Set(n
, (wxClientData
*)clientData
, false);
518 void* wxChoice::DoGetItemClientData(int n
) const
520 return (void*)m_clientDataDict
.Get(n
);
523 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
525 // don't delete, wxItemContainer does that for us
526 m_clientDataDict
.Set(n
, clientData
, false);
529 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
531 return m_clientDataDict
.Get(n
);
534 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
536 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
539 wxSize
wxChoice::GetItemsSize() const
541 int x
, y
, mx
= 0, my
= 0;
544 GetTextExtent( "|", &x
, &my
);
546 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
549 GetTextExtent( curr
->GetData(), &x
, &y
);
552 curr
= curr
->GetNext();
555 return wxSize( mx
, my
);
558 wxSize
wxChoice::DoGetBestSize() const
560 wxSize items
= GetItemsSize();
561 // FIXME arbitrary constants
562 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
563 items
.y
+ HEIGHT_OVERHEAD
);
566 #endif // wxUSE_CHOICE