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