1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "choice.h"
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
23 #include "wx/choice.h"
27 #pragma message disable nosimpint
30 #include <Xm/PushBG.h>
32 #include <Xm/RowColumn.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxChoice
, wxControl
)
41 void wxChoiceCallback (Widget w
, XtPointer clientData
,
47 m_buttonWidget
= (WXWidget
) 0;
48 m_menuWidget
= (WXWidget
) 0;
49 m_widgetList
= (WXWidget
*) 0;
50 m_formWidget
= (WXWidget
) 0;
53 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
56 int n
, const wxString choices
[],
58 const wxValidator
& validator
,
62 SetValidator(validator
);
63 m_noStrings
= 0; // Starts off with none, incremented in Append
64 m_windowStyle
= style
;
65 m_buttonWidget
= (WXWidget
) 0;
66 m_menuWidget
= (WXWidget
) 0;
67 m_widgetList
= (WXWidget
*) 0;
68 m_formWidget
= (WXWidget
) 0;
70 if (parent
) parent
->AddChild(this);
73 m_windowId
= (int)NewControlId();
77 m_backgroundColour
= parent
->GetBackgroundColour();
78 m_foregroundColour
= parent
->GetForegroundColour();
79 m_font
= parent
->GetFont();
81 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
83 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
84 xmRowColumnWidgetClass
, parentWidget
,
87 XmNpacking
, XmPACK_TIGHT
,
88 XmNorientation
, XmHORIZONTAL
,
91 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
94 * Create the popup menu
96 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
, "choiceMenu", NULL
, 0);
102 for (i
= 0; i
< n
; i
++)
112 XtSetArg (args
[argcnt
], XmNsubMenuId
, (Widget
) m_menuWidget
);
114 XtSetArg (args
[argcnt
], XmNmarginWidth
, 0);
116 XtSetArg (args
[argcnt
], XmNmarginHeight
, 0);
118 XtSetArg (args
[argcnt
], XmNpacking
, XmPACK_TIGHT
);
120 m_buttonWidget
= (WXWidget
) XmCreateOptionMenu ((Widget
) m_formWidget
, "choiceButton", args
, argcnt
);
122 m_mainWidget
= m_buttonWidget
;
124 XtManageChild ((Widget
) m_buttonWidget
);
126 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
127 // Some time ago, I reported a problem with wxChoice-items under
128 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
129 // that I have found the code responsible for this behaviour.
130 #if XmVersion >= 1002
132 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
133 // in controls sample.
135 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
136 // XtUnmanageChild (optionLabel);
140 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
144 AttachWidget (parent
, m_buttonWidget
, m_formWidget
, pos
.x
, pos
.y
, size
.x
, size
.y
);
146 ChangeBackgroundColour();
151 wxChoice::~wxChoice()
153 // For some reason destroying the menuWidget
154 // can cause crashes on some machines. It will
155 // be deleted implicitly by deleting the parent form
157 // XtDestroyWidget (menuWidget);
159 delete[] m_widgetList
;
163 DetachWidget(GetMainWidget()); // Removes event handlers
164 DetachWidget(m_formWidget
);
166 XtDestroyWidget((Widget
) m_formWidget
);
167 m_formWidget
= (WXWidget
) 0;
169 // Presumably the other widgets have been deleted now, via the form
170 m_mainWidget
= (WXWidget
) 0;
171 m_buttonWidget
= (WXWidget
) 0;
175 int wxChoice::DoAppend(const wxString
& item
)
177 Widget w
= XtVaCreateManagedWidget (wxStripMenuCodes(item
),
179 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
181 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
185 DoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
189 XmNfontList
, (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_formWidget
)),
192 WXWidget
*new_widgetList
= new WXWidget
[m_noStrings
+ 1];
195 for (i
= 0; i
< m_noStrings
; i
++)
196 new_widgetList
[i
] = m_widgetList
[i
];
198 new_widgetList
[m_noStrings
] = (WXWidget
) w
;
201 delete[] m_widgetList
;
202 m_widgetList
= new_widgetList
;
204 char mnem
= wxFindMnemonic ((char*) (const char*) item
);
206 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
208 XtAddCallback (w
, XmNactivateCallback
, (XtCallbackProc
) wxChoiceCallback
, (XtPointer
) this);
210 if (m_noStrings
== 0 && m_buttonWidget
)
212 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
213 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
214 XmString text
= XmStringCreateSimple ((char*) (const char*) item
);
215 XtVaSetValues (label
,
216 XmNlabelString
, text
,
220 wxNode
*node
= m_stringList
.Add (item
);
221 XtVaSetValues (w
, XmNuserData
, node
->Data (), NULL
);
223 if (m_noStrings
== 0)
224 m_clientList
.Append((wxObject
*) NULL
);
226 m_clientList
.Insert( m_clientList
.Item(m_noStrings
-1),
233 void wxChoice::Delete(int WXUNUSED(n
))
235 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
237 // What should we do -- remove the callback for this button widget,
238 // delete the m_stringList entry, delete the button widget, construct a new widget list
245 void wxChoice::Clear()
247 m_stringList
.Clear ();
249 for (i
= 0; i
< m_noStrings
; i
++)
251 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
252 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
255 delete[] m_widgetList
;
256 m_widgetList
= (WXWidget
*) NULL
;
258 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
260 if ( HasClientObjectData() )
262 // destroy the data (due to Robert's idea of using wxList<wxObject>
263 // and not wxList<wxClientData> we can't just say
264 // m_clientList.DeleteContents(TRUE) - this would crash!
265 wxNode
*node
= m_clientList
.First();
268 delete (wxClientData
*)node
->Data();
272 m_clientList
.Clear();
277 int wxChoice::GetSelection() const
281 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
282 XtVaGetValues (label
,
283 XmNlabelString
, &text
,
286 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
289 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
291 char *s1
= (char *) node
->Data ();
292 if (s1
== s
|| strcmp (s1
, s
) == 0)
310 void wxChoice::SetSelection(int n
)
314 wxNode
*node
= m_stringList
.Nth (n
);
317 Dimension selectionWidth
, selectionHeight
;
319 char *s
= (char *) node
->Data ();
320 XmString text
= XmStringCreateSimple (s
);
321 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
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_widgetList
[n
], NULL
);
331 m_inSetValue
= FALSE
;
334 int wxChoice::FindString(const wxString
& s
) const
337 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
339 char *s1
= (char *) node
->Data ();
350 wxString
wxChoice::GetString(int n
) const
352 wxNode
*node
= m_stringList
.Nth (n
);
354 return wxString((char *) node
->Data ());
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
, actualHeight
= height
;
397 for (i
= 0; i
< m_noStrings
; i
++)
398 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
399 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
405 for (i
= 0; i
< m_noStrings
; i
++)
406 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
407 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
412 XtManageChild ((Widget
) m_formWidget
);
413 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
415 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
418 wxString
wxChoice::GetStringSelection () const
420 int sel
= GetSelection ();
422 return wxString(this->GetString (sel
));
424 return wxEmptyString
;
427 bool wxChoice::SetStringSelection (const wxString
& s
)
429 int sel
= FindString (s
);
439 void wxChoice::Command(wxCommandEvent
& event
)
441 SetSelection (event
.GetInt());
442 ProcessCommand (event
);
445 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
447 wxChoice
*item
= (wxChoice
*) clientData
;
450 if (item
->InSetValue())
454 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
457 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
458 event
.SetEventObject(item
);
459 event
.m_commandInt
= item
->FindString (s
);
460 // event.m_commandString = s;
461 item
->ProcessCommand (event
);
466 void wxChoice::ChangeFont(bool keepOriginalSize
)
468 // Note that this causes the widget to be resized back
469 // to its original size! We therefore have to set the size
470 // back again. TODO: a better way in Motif?
473 int width
, height
, width1
, height1
;
474 GetSize(& width
, & height
);
476 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_mainWidget
));
477 XtVaSetValues ((Widget
) m_mainWidget
, XmNfontList
, fontList
, NULL
);
478 XtVaSetValues ((Widget
) m_buttonWidget
, XmNfontList
, fontList
, NULL
);
480 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
482 for (i = 0; i < m_noStrings; i++)
483 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
485 GetSize(& width1
, & height1
);
486 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
488 SetSize(-1, -1, width
, height
);
493 void wxChoice::ChangeBackgroundColour()
495 DoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
496 DoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
497 DoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
499 for (i
= 0; i
< m_noStrings
; i
++)
500 DoChangeBackgroundColour(m_widgetList
[i
], m_backgroundColour
);
503 void wxChoice::ChangeForegroundColour()
505 DoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
506 DoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
507 DoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
509 for (i
= 0; i
< m_noStrings
; i
++)
510 DoChangeForegroundColour(m_widgetList
[i
], m_foregroundColour
);
514 // These implement functions needed by wxControlWithItems.
515 // Unfortunately, they're not all implemented yet.
517 int wxChoice::GetCount() const
523 int wxChoice::DoAppend(const wxString& item)
526 return GetCount() - 1;
530 // Just appends, doesn't yet insert
531 void wxChoice::DoInsertItems(const wxArrayString
& items
, int WXUNUSED(pos
))
533 size_t nItems
= items
.GetCount();
535 for ( size_t n
= 0; n
< nItems
; n
++ )
541 void wxChoice::DoSetItems(const wxArrayString
& items
, void **WXUNUSED(clientData
))
544 size_t nItems
= items
.GetCount();
546 for ( size_t n
= 0; n
< nItems
; n
++ )
552 void wxChoice::DoSetFirstItem(int WXUNUSED(n
))
554 wxFAIL_MSG( wxT("wxChoice::DoSetFirstItem not implemented") );
557 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
559 wxNode
*node
= m_clientList
.Nth( n
);
560 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
562 node
->SetData( (wxObject
*) clientData
);
565 void* wxChoice::DoGetItemClientData(int n
) const
567 wxNode
*node
= m_clientList
.Nth( n
);
568 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
573 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
575 wxNode
*node
= m_clientList
.Nth( n
);
576 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
578 wxClientData
*cd
= (wxClientData
*) node
->Data();
581 node
->SetData( (wxObject
*) clientData
);
584 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
586 wxNode
*node
= m_clientList
.Nth( n
);
587 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
588 wxT("invalid index in wxChoice::DoGetItemClientObject") );
590 return (wxClientData
*) node
->Data();
593 void wxChoice::Select(int n
)
598 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
600 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );