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"
18 #define XtDisplay XTDISPLAY
19 #define XtParent XTPARENT
22 #include "wx/choice.h"
26 #include "wx/arrstr.h"
30 #pragma message disable nosimpint
33 #include <Xm/PushBG.h>
35 #include <Xm/RowColumn.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 #define WIDTH_OVERHEAD 48
43 #define WIDTH_OVERHEAD_SUBTRACT 40
44 #define HEIGHT_OVERHEAD 15
46 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
48 void wxChoiceCallback (Widget w
, XtPointer clientData
,
59 m_buttonWidget
= (WXWidget
) 0;
60 m_menuWidget
= (WXWidget
) 0;
61 m_formWidget
= (WXWidget
) 0;
64 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
67 int n
, const wxString choices
[],
69 const wxValidator
& validator
,
72 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 wxMOTIF_STR("choiceMenu"),
100 for (i
= 0; i
< n
; i
++)
110 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
); ++argcnt
;
111 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0); ++argcnt
;
112 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0); ++argcnt
;
113 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
); ++argcnt
;
114 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
,
115 wxMOTIF_STR("choiceButton"),
118 m_mainWidget
= m_buttonWidget
;
120 XtManageChild ((Widget
) m_buttonWidget
);
122 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
123 // Some time ago, I reported a problem with wxChoice-items under
124 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
125 // that I have found the code responsible for this behaviour.
126 #if XmVersion >= 1002
128 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
129 // in controls sample.
131 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
132 // XtUnmanageChild (optionLabel);
136 wxSize bestSize
= GetBestSize();
137 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
138 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
140 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
143 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
144 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
149 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
152 const wxArrayString
& choices
,
154 const wxValidator
& validator
,
155 const wxString
& name
)
157 wxCArrayString
chs(choices
);
158 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
159 style
, validator
, name
);
162 wxChoice::~wxChoice()
164 // For some reason destroying the menuWidget
165 // can cause crashes on some machines. It will
166 // be deleted implicitly by deleting the parent form
168 // XtDestroyWidget (menuWidget);
172 DetachWidget(GetMainWidget()); // Removes event handlers
173 DetachWidget(m_formWidget
);
175 XtDestroyWidget((Widget
) m_formWidget
);
176 m_formWidget
= (WXWidget
) 0;
178 // Presumably the other widgets have been deleted now, via the form
179 m_mainWidget
= (WXWidget
) 0;
180 m_buttonWidget
= (WXWidget
) 0;
184 static inline wxChar
* MYcopystring(const wxChar
* s
)
186 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
187 return wxStrcpy(copy
, s
);
190 // TODO auto-sorting is not supported by the code
191 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
193 void **clientData
, wxClientDataType type
)
195 #ifndef XmNpositionIndex
196 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
199 const unsigned int numItems
= items
.GetCount();
200 AllocClientData(numItems
);
201 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
203 Widget w
= XtVaCreateManagedWidget (GetLabelText(items
[i
]),
205 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
207 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
209 #ifdef XmNpositionIndex
210 XmNpositionIndex
, pos
,
214 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
217 wxDoChangeFont( w
, m_font
);
219 m_widgetArray
.Insert(w
, pos
);
221 char mnem
= wxFindMnemonic (items
[i
]);
223 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
225 XtAddCallback (w
, XmNactivateCallback
,
226 (XtCallbackProc
) wxChoiceCallback
,
229 if (m_noStrings
== 0 && m_buttonWidget
)
231 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
232 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
233 wxXmString
text( items
[i
] );
234 XtVaSetValues (label
,
235 XmNlabelString
, text(),
238 // need to ditch wxStringList for wxArrayString
239 m_stringList
.Insert(pos
, MYcopystring(items
[i
]));
242 InsertNewItemClientData(pos
, clientData
, i
, type
);
248 void wxChoice::DoDeleteOneItem(unsigned int n
)
250 Widget w
= (Widget
)m_widgetArray
[n
];
251 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
253 m_stringList
.Erase(m_stringList
.Item(n
));
254 m_widgetArray
.RemoveAt(size_t(n
));
255 wxChoiceBase::DoDeleteOneItem(n
);
261 void wxChoice::DoClear()
263 m_stringList
.Clear ();
265 for (i
= 0; i
< m_noStrings
; i
++)
267 XtRemoveCallback((Widget
) m_widgetArray
[i
],
268 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
270 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
271 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
273 m_widgetArray
.Clear();
275 XtVaSetValues ((Widget
) m_buttonWidget
,
276 XmNmenuHistory
, (Widget
) NULL
,
279 wxChoiceBase::DoClear();
284 int wxChoice::GetSelection() const
287 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
288 XtVaGetValues (label
,
289 XmNlabelString
, &text
,
291 wxXmString
freeMe(text
);
292 wxString s
= wxXmStringToString( text
);
297 for (wxStringList::compatibility_iterator node
= m_stringList
.GetFirst ();
298 node
; node
= node
->GetNext ())
300 if (wxStrcmp(node
->GetData(), s
.c_str()) == 0)
313 void wxChoice::SetSelection(int n
)
317 wxStringList::compatibility_iterator node
= m_stringList
.Item(n
);
321 Dimension selectionWidth
, selectionHeight
;
323 wxXmString
text( node
->GetData() );
324 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
325 // will remove "soon"
327 XtVaGetValues ((Widget
) m_widgetArray
[n
],
328 XmNwidth
, &selectionWidth
,
329 XmNheight
, &selectionHeight
,
332 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
333 XtVaSetValues (label
,
334 XmNlabelString
, text(),
337 XtVaSetValues ((Widget
) m_buttonWidget
,
338 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
339 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
342 m_inSetValue
= false;
345 wxString
wxChoice::GetString(unsigned 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
= (short)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
);
440 event
.SetString( 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?
455 if (m_mainWidget
&& m_font
.Ok())
457 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
458 int width
, height
, width1
, height1
;
459 GetSize(& width
, & height
);
461 WXString fontTag
= wxFont::GetFontTag();
463 XtVaSetValues ((Widget
) m_formWidget
,
464 fontTag
, m_font
.GetFontTypeC(dpy
),
466 XtVaSetValues ((Widget
) m_buttonWidget
,
467 fontTag
, m_font
.GetFontTypeC(dpy
),
470 for( unsigned int i
= 0; i
< m_noStrings
; ++i
)
471 XtVaSetValues( (Widget
)m_widgetArray
[i
],
472 fontTag
, m_font
.GetFontTypeC(dpy
),
475 GetSize(& width1
, & height1
);
476 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
478 SetSize(wxDefaultCoord
, wxDefaultCoord
, 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 unsigned int wxChoice::GetCount() const
508 void wxChoice::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
510 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
513 wxSize
wxChoice::GetItemsSize() const
515 int x
, y
, mx
= 0, my
= 0;
518 GetTextExtent( "|", &x
, &my
);
520 wxStringList::compatibility_iterator curr
= m_stringList
.GetFirst();
523 GetTextExtent( curr
->GetData(), &x
, &y
);
526 curr
= curr
->GetNext();
529 return wxSize( mx
, my
);
532 wxSize
wxChoice::DoGetBestSize() const
534 wxSize items
= GetItemsSize();
535 // FIXME arbitrary constants
536 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
537 items
.y
+ HEIGHT_OVERHEAD
);
540 #endif // wxUSE_CHOICE