1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/choice.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/choice.h"
21 #include "wx/arrstr.h"
25 #pragma message disable nosimpint
28 #include <Xm/PushBG.h>
30 #include <Xm/RowColumn.h>
32 #pragma message enable nosimpint
35 #include "wx/motif/private.h"
37 #define WIDTH_OVERHEAD 48
38 #define WIDTH_OVERHEAD_SUBTRACT 40
39 #define HEIGHT_OVERHEAD 15
41 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControlWithItems
)
43 void wxChoiceCallback (Widget w
, XtPointer clientData
,
53 m_buttonWidget
= (WXWidget
) 0;
54 m_menuWidget
= (WXWidget
) 0;
55 m_formWidget
= (WXWidget
) 0;
58 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
61 int n
, const wxString choices
[],
63 const wxValidator
& validator
,
66 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
,
78 XmNresizeWidth
, False
,
79 XmNresizeHeight
, False
,
82 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
85 * Create the popup menu
87 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
88 wxMOTIF_STR("choiceMenu"),
94 for (i
= 0; i
< n
; i
++)
104 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
105 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
106 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
107 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
108 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
109 wxMOTIF_STR("choiceButton"),
112 m_mainWidget
= m_buttonWidget
;
114 XtManageChild ((Widget
) m_buttonWidget
);
116 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
117 // Some time ago, I reported a problem with wxChoice-items under
118 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
119 // that I have found the code responsible for this behaviour.
120 #if XmVersion >= 1002
122 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
123 // in controls sample.
125 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
126 // XtUnmanageChild (optionLabel);
130 wxSize bestSize
= GetBestSize();
131 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
132 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
134 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
137 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
138 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
143 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
146 const wxArrayString
& choices
,
148 const wxValidator
& validator
,
149 const wxString
& name
)
151 wxCArrayString
chs(choices
);
152 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
153 style
, validator
, name
);
156 wxChoice::~wxChoice()
158 // For some reason destroying the menuWidget
159 // can cause crashes on some machines. It will
160 // be deleted implicitly by deleting the parent form
162 // XtDestroyWidget (menuWidget);
166 DetachWidget(GetMainWidget()); // Removes event handlers
167 DetachWidget(m_formWidget
);
169 XtDestroyWidget((Widget
) m_formWidget
);
170 m_formWidget
= (WXWidget
) 0;
172 // Presumably the other widgets have been deleted now, via the form
173 m_mainWidget
= (WXWidget
) 0;
174 m_buttonWidget
= (WXWidget
) 0;
178 static inline wxChar
* MYcopystring(const wxChar
* s
)
180 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
181 return wxStrcpy(copy
, s
);
184 // TODO auto-sorting is not supported by the code
185 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
187 void **clientData
, wxClientDataType type
)
189 #ifndef XmNpositionIndex
190 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
193 const unsigned int numItems
= items
.GetCount();
194 AllocClientData(numItems
);
195 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
197 Widget w
= XtVaCreateManagedWidget (GetLabelText(items
[i
]),
199 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
201 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
203 #ifdef XmNpositionIndex
204 XmNpositionIndex
, pos
,
208 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
211 wxDoChangeFont( w
, m_font
);
213 m_widgetArray
.Insert(w
, pos
);
215 char mnem
= wxFindMnemonic (items
[i
]);
217 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
219 XtAddCallback (w
, XmNactivateCallback
,
220 (XtCallbackProc
) wxChoiceCallback
,
223 if (m_stringArray
.GetCount() == 0 && m_buttonWidget
)
225 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
226 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
227 wxXmString
text( items
[i
] );
228 XtVaSetValues (label
,
229 XmNlabelString
, text(),
233 m_stringArray
.Insert(items
[i
], pos
);
235 InsertNewItemClientData(pos
, clientData
, i
, type
);
241 void wxChoice::DoDeleteOneItem(unsigned int n
)
243 Widget w
= (Widget
)m_widgetArray
[n
];
244 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
247 m_stringArray
.RemoveAt(size_t(n
));
248 m_widgetArray
.RemoveAt(size_t(n
));
249 wxChoiceBase::DoDeleteOneItem(n
);
254 void wxChoice::DoClear()
256 m_stringArray
.Clear();
259 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
261 XtRemoveCallback((Widget
) m_widgetArray
[i
],
262 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
264 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
265 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
268 m_widgetArray
.Clear();
270 XtVaSetValues ((Widget
) m_buttonWidget
,
271 XmNmenuHistory
, (Widget
) NULL
,
274 wxChoiceBase::DoClear();
277 int wxChoice::GetSelection() const
280 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
281 XtVaGetValues (label
,
282 XmNlabelString
, &text
,
284 wxXmString
freeMe(text
);
285 wxString s
= wxXmStringToString( text
);
289 for (size_t i
=0; i
<m_stringArray
.GetCount(); i
++)
290 if (m_stringArray
[i
] == s
)
299 void wxChoice::SetSelection(int n
)
304 Dimension selectionWidth
, selectionHeight
;
306 wxXmString
text( m_stringArray
[n
] );
307 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
308 // will remove "soon"
310 XtVaGetValues ((Widget
) m_widgetArray
[n
],
311 XmNwidth
, &selectionWidth
,
312 XmNheight
, &selectionHeight
,
315 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
316 XtVaSetValues (label
,
317 XmNlabelString
, text(),
320 XtVaSetValues ((Widget
) m_buttonWidget
,
321 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
322 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
325 m_inSetValue
= false;
328 wxString
wxChoice::GetString(unsigned int n
) const
330 return m_stringArray
[n
];
333 void wxChoice::SetColumns(int n
)
337 short numColumns
= (short)n
;
340 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
341 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
342 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
345 int wxChoice::GetColumns(void) const
349 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
353 void wxChoice::SetFocus()
355 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
358 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
360 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
361 bool managed
= XtIsManaged((Widget
) m_formWidget
);
364 XtUnmanageChild ((Widget
) m_formWidget
);
366 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
367 actualHeight
= height
- HEIGHT_OVERHEAD
;
372 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
373 XtVaSetValues ((Widget
) m_widgetArray
[i
],
374 XmNwidth
, actualWidth
,
376 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
383 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
384 XtVaSetValues ((Widget
) m_widgetArray
[i
],
385 XmNheight
, actualHeight
,
388 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
393 XtManageChild ((Widget
) m_formWidget
);
394 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
396 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
399 void wxChoice::Command(wxCommandEvent
& event
)
401 SetSelection (event
.GetInt());
402 ProcessCommand (event
);
405 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
407 wxChoice
*item
= (wxChoice
*) clientData
;
410 if (item
->InSetValue())
413 int n
= item
->GetWidgets().Index(w
);
414 if (n
!= wxNOT_FOUND
)
416 wxCommandEvent
event(wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
417 event
.SetEventObject(item
);
419 event
.SetString( item
->GetStrings().Item(n
) );
420 if ( item
->HasClientObjectData() )
421 event
.SetClientObject( item
->GetClientObject(n
) );
422 else if ( item
->HasClientUntypedData() )
423 event
.SetClientData( item
->GetClientData(n
) );
424 item
->ProcessCommand (event
);
429 void wxChoice::ChangeFont(bool keepOriginalSize
)
431 // Note that this causes the widget to be resized back
432 // to its original size! We therefore have to set the size
433 // back again. TODO: a better way in Motif?
434 if (m_mainWidget
&& m_font
.Ok())
436 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
437 int width
, height
, width1
, height1
;
438 GetSize(& width
, & height
);
440 WXString fontTag
= wxFont::GetFontTag();
442 XtVaSetValues ((Widget
) m_formWidget
,
443 fontTag
, m_font
.GetFontTypeC(dpy
),
445 XtVaSetValues ((Widget
) m_buttonWidget
,
446 fontTag
, m_font
.GetFontTypeC(dpy
),
449 for( unsigned int i
= 0; i
< m_stringArray
.GetCount(); ++i
)
450 XtVaSetValues( (Widget
)m_widgetArray
[i
],
451 fontTag
, m_font
.GetFontTypeC(dpy
),
454 GetSize(& width1
, & height1
);
455 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
457 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
462 void wxChoice::ChangeBackgroundColour()
464 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
465 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
466 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
468 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
469 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
472 void wxChoice::ChangeForegroundColour()
474 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
475 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
476 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
478 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
479 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
482 unsigned int wxChoice::GetCount() const
484 return m_stringArray
.GetCount();
487 void wxChoice::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
489 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
492 wxSize
wxChoice::GetItemsSize() const
494 int x
, y
, mx
= 0, my
= 0;
497 GetTextExtent( "|", &x
, &my
);
499 for (size_t i
=0; i
<m_stringArray
.GetCount(); i
++)
501 GetTextExtent( m_stringArray
[i
], &x
, &y
);
506 return wxSize( mx
, my
);
509 wxSize
wxChoice::DoGetBestSize() const
511 wxSize items
= GetItemsSize();
512 // FIXME arbitrary constants
513 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
514 items
.y
+ HEIGHT_OVERHEAD
);
517 #endif // wxUSE_CHOICE