]> git.saurik.com Git - wxWidgets.git/blame - src/motif/choice.cpp
wxMenu compile fix
[wxWidgets.git] / src / motif / choice.cpp
CommitLineData
4bb6408c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose: wxChoice
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "choice.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/choice.h"
f97c9854
JS
18#include "wx/utils.h"
19
20#include <Xm/Xm.h>
21#include <Xm/PushBG.h>
22#include <Xm/PushB.h>
23#include <Xm/RowColumn.h>
24
25#include "wx/motif/private.h"
4bb6408c
JS
26
27#if !USE_SHARED_LIBRARY
28IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
29#endif
30
f97c9854 31void wxChoiceCallback (Widget w, XtPointer clientData,
2d120f83 32 XtPointer ptr);
f97c9854
JS
33
34wxChoice::wxChoice()
35{
36 m_noStrings = 0;
37 m_buttonWidget = (WXWidget) 0;
38 m_menuWidget = (WXWidget) 0;
39 m_widgetList = (WXWidget*) 0;
40 m_formWidget = (WXWidget) 0;
f97c9854
JS
41}
42
4bb6408c 43bool wxChoice::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
44 const wxPoint& pos,
45 const wxSize& size,
46 int n, const wxString choices[],
47 long style,
48 const wxValidator& validator,
49 const wxString& name)
4bb6408c
JS
50{
51 SetName(name);
52 SetValidator(validator);
ea57084d 53 m_noStrings = 0; // Starts off with none, incremented in Append
4bb6408c 54 m_windowStyle = style;
f97c9854
JS
55 m_buttonWidget = (WXWidget) 0;
56 m_menuWidget = (WXWidget) 0;
57 m_widgetList = (WXWidget*) 0;
58 m_formWidget = (WXWidget) 0;
2d120f83 59
4bb6408c 60 if (parent) parent->AddChild(this);
2d120f83 61
4bb6408c 62 if ( id == -1 )
2d120f83 63 m_windowId = (int)NewControlId();
4bb6408c 64 else
2d120f83
JS
65 m_windowId = id;
66
0d57be45
JS
67 m_backgroundColour = parent->GetBackgroundColour();
68 m_foregroundColour = parent->GetForegroundColour();
da175b2c 69 m_font = parent->GetFont();
2d120f83 70
f97c9854 71 Widget parentWidget = (Widget) parent->GetClientWidget();
2d120f83 72
f97c9854 73 m_formWidget = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) name,
2d120f83
JS
74 xmRowColumnWidgetClass, parentWidget,
75 XmNmarginHeight, 0,
76 XmNmarginWidth, 0,
77 XmNpacking, XmPACK_TIGHT,
78 XmNorientation, XmHORIZONTAL,
79 NULL);
80
f97c9854 81 XtVaSetValues ((Widget) m_formWidget, XmNspacing, 0, NULL);
2d120f83
JS
82
83 /*
f97c9854
JS
84 * Create the popup menu
85 */
86 m_menuWidget = (WXWidget) XmCreatePulldownMenu ((Widget) m_formWidget, "choiceMenu", NULL, 0);
2d120f83
JS
87
88 // int i;
f97c9854
JS
89 if (n > 0)
90 {
91 int i;
92 for (i = 0; i < n; i++)
93 Append (choices[i]);
94 }
2d120f83
JS
95
96 /*
f97c9854
JS
97 * Create button
98 */
99 Arg args[10];
100 Cardinal argcnt = 0;
2d120f83 101
f97c9854
JS
102 XtSetArg (args[argcnt], XmNsubMenuId, (Widget) m_menuWidget);
103 argcnt++;
104 XtSetArg (args[argcnt], XmNmarginWidth, 0);
105 argcnt++;
106 XtSetArg (args[argcnt], XmNmarginHeight, 0);
107 argcnt++;
108 XtSetArg (args[argcnt], XmNpacking, XmPACK_TIGHT);
109 argcnt++;
110 m_buttonWidget = (WXWidget) XmCreateOptionMenu ((Widget) m_formWidget, "choiceButton", args, argcnt);
2d120f83 111
f97c9854 112 m_mainWidget = m_buttonWidget;
2d120f83 113
f97c9854 114 XtManageChild ((Widget) m_buttonWidget);
9838df2c 115
f97c9854
JS
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
121#if XmVersion < 2000
9838df2c
JS
122 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
123 // in controls sample.
dfe1eee3
VZ
124 //
125 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
126 // XtUnmanageChild (optionLabel);
f97c9854
JS
127#endif
128#endif
9838df2c 129
f97c9854 130 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
2d120f83 131
4b5f3fe6 132 ChangeFont(FALSE);
9838df2c 133
f97c9854 134 AttachWidget (parent, m_buttonWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
2d120f83 135
0d57be45 136 ChangeBackgroundColour();
2d120f83 137
f97c9854
JS
138 return TRUE;
139}
140
141wxChoice::~wxChoice()
142{
2d120f83
JS
143 // For some reason destroying the menuWidget
144 // can cause crashes on some machines. It will
145 // be deleted implicitly by deleting the parent form
146 // anyway.
147 // XtDestroyWidget (menuWidget);
f97c9854
JS
148 if (m_widgetList)
149 delete[] m_widgetList;
2d120f83 150
8aa04e8b
JS
151 if (GetMainWidget())
152 {
153 DetachWidget(GetMainWidget()); // Removes event handlers
b412f9be 154 DetachWidget(m_formWidget);
2d120f83 155
8aa04e8b
JS
156 XtDestroyWidget((Widget) m_formWidget);
157 m_formWidget = (WXWidget) 0;
2d120f83 158
8aa04e8b
JS
159 // Presumably the other widgets have been deleted now, via the form
160 m_mainWidget = (WXWidget) 0;
161 m_buttonWidget = (WXWidget) 0;
162 }
4bb6408c
JS
163}
164
165void wxChoice::Append(const wxString& item)
166{
2d120f83
JS
167 wxStripMenuCodes ((char *)(const char *)item, wxBuffer);
168 Widget w = XtVaCreateManagedWidget (wxBuffer,
f97c9854 169#if USE_GADGETS
2d120f83 170 xmPushButtonGadgetClass, (Widget) m_menuWidget,
f97c9854 171#else
2d120f83 172 xmPushButtonWidgetClass, (Widget) m_menuWidget,
f97c9854 173#endif
2d120f83
JS
174 NULL);
175
176 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
177
da175b2c 178 if (m_font.Ok())
2d120f83 179 XtVaSetValues (w,
da175b2c 180 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
2d120f83
JS
181 NULL);
182
183 WXWidget *new_widgetList = new WXWidget[m_noStrings + 1];
184 int i;
185 if (m_widgetList)
186 for (i = 0; i < m_noStrings; i++)
187 new_widgetList[i] = m_widgetList[i];
188
189 new_widgetList[m_noStrings] = (WXWidget) w;
190
191 if (m_widgetList)
192 delete[] m_widgetList;
193 m_widgetList = new_widgetList;
194
195 char mnem = wxFindMnemonic ((char*) (const char*) item);
196 if (mnem != 0)
197 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
198
199 XtAddCallback (w, XmNactivateCallback, (XtCallbackProc) wxChoiceCallback, (XtPointer) this);
200
201 if (m_noStrings == 0 && m_buttonWidget)
202 {
203 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
204 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
205 XmString text = XmStringCreateSimple ((char*) (const char*) item);
206 XtVaSetValues (label,
207 XmNlabelString, text,
208 NULL);
209 XmStringFree (text);
210 }
211 wxNode *node = m_stringList.Add (item);
212 XtVaSetValues (w, XmNuserData, node->Data (), NULL);
213
214 m_noStrings ++;
4bb6408c
JS
215}
216
f9e02ac7 217void wxChoice::Delete(int WXUNUSED(n))
4bb6408c 218{
f97c9854 219 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
2d120f83 220
f97c9854
JS
221 // What should we do -- remove the callback for this button widget,
222 // delete the m_stringList entry, delete the button widget, construct a new widget list
223 // (see Append)
2d120f83 224
4bb6408c
JS
225 // TODO
226 m_noStrings --;
227}
228
229void wxChoice::Clear()
230{
f97c9854
JS
231 m_stringList.Clear ();
232 int i;
233 for (i = 0; i < m_noStrings; i++)
234 {
235 XtUnmanageChild ((Widget) m_widgetList[i]);
236 XtDestroyWidget ((Widget) m_widgetList[i]);
237 }
238 if (m_noStrings)
239 delete[] m_widgetList;
240 m_widgetList = (WXWidget*) NULL;
241 if (m_buttonWidget)
242 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, (Widget) NULL, NULL);
4bb6408c
JS
243 m_noStrings = 0;
244}
245
246int wxChoice::GetSelection() const
247{
2d120f83
JS
248 XmString text;
249 char *s;
250 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
251 XtVaGetValues (label,
252 XmNlabelString, &text,
253 NULL);
254
255 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
256 {
257 int i = 0;
258 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
f97c9854 259 {
2d120f83
JS
260 char *s1 = (char *) node->Data ();
261 if (s1 == s || strcmp (s1, s) == 0)
262 {
263 XmStringFree(text) ;
264 XtFree (s);
265 return i;
266 }
267 else
268 i++;
269 } // for()
270
271 XmStringFree(text) ;
272 XtFree (s);
273 return -1;
274 }
275 XmStringFree(text) ;
276 return -1;
4bb6408c
JS
277}
278
279void wxChoice::SetSelection(int n)
280{
2d120f83
JS
281 m_inSetValue = TRUE;
282
283 wxNode *node = m_stringList.Nth (n);
284 if (node)
f97c9854 285 {
2d120f83
JS
286 Dimension selectionWidth, selectionHeight;
287
288 char *s = (char *) node->Data ();
289 XmString text = XmStringCreateSimple (s);
290 XtVaGetValues ((Widget) m_widgetList[n], XmNwidth, &selectionWidth, XmNheight, &selectionHeight, NULL);
291 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
292 XtVaSetValues (label,
293 XmNlabelString, text,
294 NULL);
295 XmStringFree (text);
296 XtVaSetValues ((Widget) m_buttonWidget,
297 XmNwidth, selectionWidth, XmNheight, selectionHeight,
298 XmNmenuHistory, (Widget) m_widgetList[n], NULL);
f97c9854 299 }
2d120f83 300 m_inSetValue = FALSE;
4bb6408c
JS
301}
302
303int wxChoice::FindString(const wxString& s) const
304{
f97c9854
JS
305 int i = 0;
306 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
307 {
308 char *s1 = (char *) node->Data ();
309 if (s == s1)
310 {
311 return i;
312 }
313 else
314 i++;
315 }
316 return -1;
4bb6408c
JS
317}
318
319wxString wxChoice::GetString(int n) const
320{
2d120f83
JS
321 wxNode *node = m_stringList.Nth (n);
322 if (node)
323 return wxString((char *) node->Data ());
324 else
325 return wxEmptyString;
f97c9854
JS
326}
327
328void wxChoice::SetColumns(int n)
329{
2d120f83
JS
330 if (n<1) n = 1 ;
331
332 short numColumns = n ;
333 Arg args[3];
334
335 XtSetArg(args[0], XmNnumColumns, numColumns);
336 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
337 XtSetValues((Widget) m_menuWidget,args,2) ;
f97c9854
JS
338}
339
340int wxChoice::GetColumns(void) const
341{
2d120f83
JS
342 short numColumns ;
343
344 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
345 return numColumns ;
f97c9854
JS
346}
347
348void wxChoice::SetFocus()
349{
2d120f83 350 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
4bb6408c
JS
351}
352
bfc6fde4 353void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 354{
f97c9854
JS
355 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
356 bool managed = XtIsManaged((Widget) m_formWidget);
2d120f83 357
f97c9854
JS
358 if (managed)
359 XtUnmanageChild ((Widget) m_formWidget);
2d120f83 360
f97c9854 361 int actualWidth = width, actualHeight = height;
2d120f83 362
f97c9854
JS
363 if (width > -1)
364 {
365 int i;
366 for (i = 0; i < m_noStrings; i++)
367 XtVaSetValues ((Widget) m_widgetList[i], XmNwidth, actualWidth, NULL);
368 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
2d120f83 369 NULL);
f97c9854
JS
370 }
371 if (height > -1)
372 {
373 int i;
374 for (i = 0; i < m_noStrings; i++)
375 XtVaSetValues ((Widget) m_widgetList[i], XmNheight, actualHeight, NULL);
376 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
2d120f83 377 NULL);
f97c9854 378 }
2d120f83 379
f97c9854
JS
380 if (managed)
381 XtManageChild ((Widget) m_formWidget);
382 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
2d120f83 383
bfc6fde4 384 wxControl::DoSetSize (x, y, width, height, sizeFlags);
4bb6408c
JS
385}
386
387wxString wxChoice::GetStringSelection () const
388{
389 int sel = GetSelection ();
390 if (sel > -1)
391 return wxString(this->GetString (sel));
392 else
f97c9854 393 return wxEmptyString;
4bb6408c
JS
394}
395
396bool wxChoice::SetStringSelection (const wxString& s)
397{
398 int sel = FindString (s);
399 if (sel > -1)
2d120f83
JS
400 {
401 SetSelection (sel);
402 return TRUE;
403 }
4bb6408c
JS
404 else
405 return FALSE;
406}
407
408void wxChoice::Command(wxCommandEvent & event)
409{
410 SetSelection (event.GetInt());
411 ProcessCommand (event);
412}
413
f9e02ac7 414void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
f97c9854
JS
415{
416 wxChoice *item = (wxChoice *) clientData;
417 if (item)
418 {
a4294b78 419 if (item->InSetValue())
f97c9854
JS
420 return;
421
422 char *s = NULL;
423 XtVaGetValues (w, XmNuserData, &s, NULL);
424 if (s)
425 {
55acd85e
JS
426 wxCommandEvent event (wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
427 event.SetEventObject(item);
f97c9854 428 event.m_commandInt = item->FindString (s);
2d120f83 429 // event.m_commandString = s;
f97c9854
JS
430 item->ProcessCommand (event);
431 }
432 }
433}
434
4b5f3fe6 435void wxChoice::ChangeFont(bool keepOriginalSize)
0d57be45 436{
321db4b6
JS
437 // Note that this causes the widget to be resized back
438 // to its original size! We therefore have to set the size
439 // back again. TODO: a better way in Motif?
da175b2c 440 if (m_font.Ok())
321db4b6
JS
441 {
442 int width, height, width1, height1;
443 GetSize(& width, & height);
2d120f83 444
da175b2c 445 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
321db4b6
JS
446 XtVaSetValues ((Widget) m_mainWidget, XmNfontList, fontList, NULL);
447 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
2d120f83 448
4b5f3fe6 449 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
321db4b6
JS
450 int i;
451 for (i = 0; i < m_noStrings; i++)
2d120f83 452 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
4b5f3fe6 453 */
321db4b6 454 GetSize(& width1, & height1);
4b5f3fe6 455 if (keepOriginalSize && (width != width1 || height != height1))
321db4b6
JS
456 {
457 SetSize(-1, -1, width, height);
458 }
459 }
0d57be45
JS
460}
461
462void wxChoice::ChangeBackgroundColour()
463{
321db4b6
JS
464 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
465 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
466 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
467 int i;
468 for (i = 0; i < m_noStrings; i++)
469 DoChangeBackgroundColour(m_widgetList[i], m_backgroundColour);
0d57be45
JS
470}
471
472void wxChoice::ChangeForegroundColour()
473{
321db4b6
JS
474 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
475 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
476 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
477 int i;
478 for (i = 0; i < m_noStrings; i++)
479 DoChangeForegroundColour(m_widgetList[i], m_foregroundColour);
0d57be45 480}