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 void wxChoice::Append(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),
231 void wxChoice::Delete(int WXUNUSED(n
))
233 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
235 // What should we do -- remove the callback for this button widget,
236 // delete the m_stringList entry, delete the button widget, construct a new widget list
243 void wxChoice::Clear()
245 m_stringList
.Clear ();
247 for (i
= 0; i
< m_noStrings
; i
++)
249 XtUnmanageChild ((Widget
) m_widgetList
[i
]);
250 XtDestroyWidget ((Widget
) m_widgetList
[i
]);
253 delete[] m_widgetList
;
254 m_widgetList
= (WXWidget
*) NULL
;
256 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, (Widget
) NULL
, NULL
);
258 if ( HasClientObjectData() )
260 // destroy the data (due to Robert's idea of using wxList<wxObject>
261 // and not wxList<wxClientData> we can't just say
262 // m_clientList.DeleteContents(TRUE) - this would crash!
263 wxNode
*node
= m_clientList
.First();
266 delete (wxClientData
*)node
->Data();
270 m_clientList
.Clear();
275 int wxChoice::GetSelection() const
279 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
280 XtVaGetValues (label
,
281 XmNlabelString
, &text
,
284 if (XmStringGetLtoR (text
, XmSTRING_DEFAULT_CHARSET
, &s
))
287 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
289 char *s1
= (char *) node
->Data ();
290 if (s1
== s
|| strcmp (s1
, s
) == 0)
308 void wxChoice::SetSelection(int n
)
312 wxNode
*node
= m_stringList
.Nth (n
);
315 Dimension selectionWidth
, selectionHeight
;
317 char *s
= (char *) node
->Data ();
318 XmString text
= XmStringCreateSimple (s
);
319 XtVaGetValues ((Widget
) m_widgetList
[n
], XmNwidth
, &selectionWidth
, XmNheight
, &selectionHeight
, NULL
);
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_widgetList
[n
], NULL
);
329 m_inSetValue
= FALSE
;
332 int wxChoice::FindString(const wxString
& s
) const
335 for (wxNode
* node
= m_stringList
.First (); node
; node
= node
->Next ())
337 char *s1
= (char *) node
->Data ();
348 wxString
wxChoice::GetString(int n
) const
350 wxNode
*node
= m_stringList
.Nth (n
);
352 return wxString((char *) node
->Data ());
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
, actualHeight
= height
;
395 for (i
= 0; i
< m_noStrings
; i
++)
396 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNwidth
, actualWidth
, NULL
);
397 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
403 for (i
= 0; i
< m_noStrings
; i
++)
404 XtVaSetValues ((Widget
) m_widgetList
[i
], XmNheight
, actualHeight
, NULL
);
405 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
410 XtManageChild ((Widget
) m_formWidget
);
411 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
413 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
416 wxString
wxChoice::GetStringSelection () const
418 int sel
= GetSelection ();
420 return wxString(this->GetString (sel
));
422 return wxEmptyString
;
425 bool wxChoice::SetStringSelection (const wxString
& s
)
427 int sel
= FindString (s
);
437 void wxChoice::Command(wxCommandEvent
& event
)
439 SetSelection (event
.GetInt());
440 ProcessCommand (event
);
443 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
445 wxChoice
*item
= (wxChoice
*) clientData
;
448 if (item
->InSetValue())
452 XtVaGetValues (w
, XmNuserData
, &s
, NULL
);
455 wxCommandEvent
event (wxEVT_COMMAND_CHOICE_SELECTED
, item
->GetId());
456 event
.SetEventObject(item
);
457 event
.m_commandInt
= item
->FindString (s
);
458 // event.m_commandString = s;
459 item
->ProcessCommand (event
);
464 void wxChoice::ChangeFont(bool keepOriginalSize
)
466 // Note that this causes the widget to be resized back
467 // to its original size! We therefore have to set the size
468 // back again. TODO: a better way in Motif?
471 int width
, height
, width1
, height1
;
472 GetSize(& width
, & height
);
474 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay((Widget
) m_mainWidget
));
475 XtVaSetValues ((Widget
) m_mainWidget
, XmNfontList
, fontList
, NULL
);
476 XtVaSetValues ((Widget
) m_buttonWidget
, XmNfontList
, fontList
, NULL
);
478 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
480 for (i = 0; i < m_noStrings; i++)
481 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
483 GetSize(& width1
, & height1
);
484 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
486 SetSize(-1, -1, width
, height
);
491 void wxChoice::ChangeBackgroundColour()
493 DoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
494 DoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
495 DoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
497 for (i
= 0; i
< m_noStrings
; i
++)
498 DoChangeBackgroundColour(m_widgetList
[i
], m_backgroundColour
);
501 void wxChoice::ChangeForegroundColour()
503 DoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
504 DoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
505 DoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
507 for (i
= 0; i
< m_noStrings
; i
++)
508 DoChangeForegroundColour(m_widgetList
[i
], m_foregroundColour
);
512 // These implement functions needed by wxControlWithItems.
513 // Unfortunately, they're not all implemented yet.
515 int wxChoice::GetCount() const
520 int wxChoice::DoAppend(const wxString
& item
)
523 return GetCount() - 1;
526 // Just appends, doesn't yet insert
527 void wxChoice::DoInsertItems(const wxArrayString
& items
, int WXUNUSED(pos
))
529 size_t nItems
= items
.GetCount();
531 for ( size_t n
= 0; n
< nItems
; n
++ )
537 void wxChoice::DoSetItems(const wxArrayString
& items
, void **WXUNUSED(clientData
))
540 size_t nItems
= items
.GetCount();
542 for ( size_t n
= 0; n
< nItems
; n
++ )
548 void wxChoice::DoSetFirstItem(int WXUNUSED(n
))
550 wxFAIL_MSG( wxT("wxChoice::DoSetFirstItem not implemented") );
553 void wxChoice::DoSetItemClientData(int n
, void* clientData
)
555 wxNode
*node
= m_clientList
.Nth( n
);
556 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientData") );
558 node
->SetData( (wxObject
*) clientData
);
561 void* wxChoice::DoGetItemClientData(int n
) const
563 wxNode
*node
= m_clientList
.Nth( n
);
564 wxCHECK_MSG( node
, NULL
, wxT("invalid index in wxChoice::DoGetItemClientData") );
569 void wxChoice::DoSetItemClientObject(int n
, wxClientData
* clientData
)
571 wxNode
*node
= m_clientList
.Nth( n
);
572 wxCHECK_RET( node
, wxT("invalid index in wxChoice::DoSetItemClientObject") );
574 wxClientData
*cd
= (wxClientData
*) node
->Data();
577 node
->SetData( (wxObject
*) clientData
);
580 wxClientData
* wxChoice::DoGetItemClientObject(int n
) const
582 wxNode
*node
= m_clientList
.Nth( n
);
583 wxCHECK_MSG( node
, (wxClientData
*)NULL
,
584 wxT("invalid index in wxChoice::DoGetItemClientObject") );
586 return (wxClientData
*) node
->Data();
589 void wxChoice::Select(int n
)
594 void wxChoice::SetString(int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
596 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );