]> git.saurik.com Git - wxWidgets.git/blame - src/motif/choice.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[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;
31528cd3 59
4bb6408c 60 if (parent) parent->AddChild(this);
31528cd3 61
4bb6408c 62 if ( id == -1 )
2d120f83 63 m_windowId = (int)NewControlId();
4bb6408c 64 else
2d120f83 65 m_windowId = id;
31528cd3 66
0d57be45
JS
67 m_backgroundColour = parent->GetBackgroundColour();
68 m_foregroundColour = parent->GetForegroundColour();
da175b2c 69 m_font = parent->GetFont();
31528cd3 70
f97c9854 71 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3
VZ
72
73 m_formWidget = (WXWidget) XtVaCreateManagedWidget(name.c_str(),
2d120f83
JS
74 xmRowColumnWidgetClass, parentWidget,
75 XmNmarginHeight, 0,
76 XmNmarginWidth, 0,
77 XmNpacking, XmPACK_TIGHT,
78 XmNorientation, XmHORIZONTAL,
79 NULL);
31528cd3 80
f97c9854 81 XtVaSetValues ((Widget) m_formWidget, XmNspacing, 0, NULL);
31528cd3 82
2d120f83 83 /*
f97c9854
JS
84 * Create the popup menu
85 */
86 m_menuWidget = (WXWidget) XmCreatePulldownMenu ((Widget) m_formWidget, "choiceMenu", NULL, 0);
31528cd3 87
2d120f83 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 }
31528cd3 95
2d120f83 96 /*
f97c9854
JS
97 * Create button
98 */
99 Arg args[10];
100 Cardinal argcnt = 0;
31528cd3 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);
31528cd3 111
f97c9854 112 m_mainWidget = m_buttonWidget;
31528cd3 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);
31528cd3 131
4b5f3fe6 132 ChangeFont(FALSE);
9838df2c 133
f97c9854 134 AttachWidget (parent, m_buttonWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
31528cd3 135
0d57be45 136 ChangeBackgroundColour();
31528cd3 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;
31528cd3 150
8aa04e8b
JS
151 if (GetMainWidget())
152 {
153 DetachWidget(GetMainWidget()); // Removes event handlers
b412f9be 154 DetachWidget(m_formWidget);
31528cd3 155
8aa04e8b
JS
156 XtDestroyWidget((Widget) m_formWidget);
157 m_formWidget = (WXWidget) 0;
31528cd3 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{
31528cd3 167 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
f97c9854 168#if USE_GADGETS
2d120f83 169 xmPushButtonGadgetClass, (Widget) m_menuWidget,
f97c9854 170#else
2d120f83 171 xmPushButtonWidgetClass, (Widget) m_menuWidget,
f97c9854 172#endif
2d120f83 173 NULL);
31528cd3 174
2d120f83 175 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
31528cd3 176
da175b2c 177 if (m_font.Ok())
2d120f83 178 XtVaSetValues (w,
da175b2c 179 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
2d120f83 180 NULL);
31528cd3 181
2d120f83
JS
182 WXWidget *new_widgetList = new WXWidget[m_noStrings + 1];
183 int i;
184 if (m_widgetList)
185 for (i = 0; i < m_noStrings; i++)
186 new_widgetList[i] = m_widgetList[i];
31528cd3 187
2d120f83 188 new_widgetList[m_noStrings] = (WXWidget) w;
31528cd3 189
2d120f83
JS
190 if (m_widgetList)
191 delete[] m_widgetList;
192 m_widgetList = new_widgetList;
31528cd3 193
2d120f83
JS
194 char mnem = wxFindMnemonic ((char*) (const char*) item);
195 if (mnem != 0)
196 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
31528cd3 197
2d120f83 198 XtAddCallback (w, XmNactivateCallback, (XtCallbackProc) wxChoiceCallback, (XtPointer) this);
31528cd3 199
2d120f83
JS
200 if (m_noStrings == 0 && m_buttonWidget)
201 {
202 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
203 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
204 XmString text = XmStringCreateSimple ((char*) (const char*) item);
205 XtVaSetValues (label,
206 XmNlabelString, text,
207 NULL);
208 XmStringFree (text);
209 }
210 wxNode *node = m_stringList.Add (item);
211 XtVaSetValues (w, XmNuserData, node->Data (), NULL);
31528cd3 212
2d120f83 213 m_noStrings ++;
4bb6408c
JS
214}
215
f9e02ac7 216void wxChoice::Delete(int WXUNUSED(n))
4bb6408c 217{
f97c9854 218 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
31528cd3 219
f97c9854
JS
220 // What should we do -- remove the callback for this button widget,
221 // delete the m_stringList entry, delete the button widget, construct a new widget list
222 // (see Append)
31528cd3 223
4bb6408c
JS
224 // TODO
225 m_noStrings --;
226}
227
228void wxChoice::Clear()
229{
f97c9854
JS
230 m_stringList.Clear ();
231 int i;
232 for (i = 0; i < m_noStrings; i++)
233 {
234 XtUnmanageChild ((Widget) m_widgetList[i]);
235 XtDestroyWidget ((Widget) m_widgetList[i]);
236 }
237 if (m_noStrings)
238 delete[] m_widgetList;
239 m_widgetList = (WXWidget*) NULL;
240 if (m_buttonWidget)
241 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, (Widget) NULL, NULL);
4bb6408c
JS
242 m_noStrings = 0;
243}
244
245int wxChoice::GetSelection() const
246{
2d120f83
JS
247 XmString text;
248 char *s;
249 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
250 XtVaGetValues (label,
251 XmNlabelString, &text,
252 NULL);
31528cd3 253
2d120f83
JS
254 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
255 {
256 int i = 0;
257 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
f97c9854 258 {
2d120f83
JS
259 char *s1 = (char *) node->Data ();
260 if (s1 == s || strcmp (s1, s) == 0)
261 {
262 XmStringFree(text) ;
263 XtFree (s);
264 return i;
265 }
266 else
267 i++;
268 } // for()
31528cd3 269
2d120f83
JS
270 XmStringFree(text) ;
271 XtFree (s);
272 return -1;
273 }
274 XmStringFree(text) ;
275 return -1;
4bb6408c
JS
276}
277
278void wxChoice::SetSelection(int n)
279{
2d120f83 280 m_inSetValue = TRUE;
31528cd3 281
2d120f83
JS
282 wxNode *node = m_stringList.Nth (n);
283 if (node)
f97c9854 284 {
2d120f83 285 Dimension selectionWidth, selectionHeight;
31528cd3 286
2d120f83
JS
287 char *s = (char *) node->Data ();
288 XmString text = XmStringCreateSimple (s);
289 XtVaGetValues ((Widget) m_widgetList[n], XmNwidth, &selectionWidth, XmNheight, &selectionHeight, NULL);
290 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
291 XtVaSetValues (label,
292 XmNlabelString, text,
293 NULL);
294 XmStringFree (text);
295 XtVaSetValues ((Widget) m_buttonWidget,
296 XmNwidth, selectionWidth, XmNheight, selectionHeight,
297 XmNmenuHistory, (Widget) m_widgetList[n], NULL);
f97c9854 298 }
2d120f83 299 m_inSetValue = FALSE;
4bb6408c
JS
300}
301
302int wxChoice::FindString(const wxString& s) const
303{
f97c9854
JS
304 int i = 0;
305 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
306 {
307 char *s1 = (char *) node->Data ();
308 if (s == s1)
309 {
310 return i;
311 }
312 else
313 i++;
314 }
315 return -1;
4bb6408c
JS
316}
317
318wxString wxChoice::GetString(int n) const
319{
2d120f83
JS
320 wxNode *node = m_stringList.Nth (n);
321 if (node)
322 return wxString((char *) node->Data ());
323 else
324 return wxEmptyString;
f97c9854
JS
325}
326
327void wxChoice::SetColumns(int n)
328{
2d120f83 329 if (n<1) n = 1 ;
31528cd3 330
2d120f83
JS
331 short numColumns = n ;
332 Arg args[3];
31528cd3 333
2d120f83
JS
334 XtSetArg(args[0], XmNnumColumns, numColumns);
335 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
336 XtSetValues((Widget) m_menuWidget,args,2) ;
f97c9854
JS
337}
338
339int wxChoice::GetColumns(void) const
340{
2d120f83 341 short numColumns ;
31528cd3 342
2d120f83
JS
343 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
344 return numColumns ;
f97c9854
JS
345}
346
347void wxChoice::SetFocus()
348{
2d120f83 349 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
4bb6408c
JS
350}
351
bfc6fde4 352void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 353{
f97c9854
JS
354 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
355 bool managed = XtIsManaged((Widget) m_formWidget);
31528cd3 356
f97c9854
JS
357 if (managed)
358 XtUnmanageChild ((Widget) m_formWidget);
31528cd3 359
f97c9854 360 int actualWidth = width, actualHeight = height;
31528cd3 361
f97c9854
JS
362 if (width > -1)
363 {
364 int i;
365 for (i = 0; i < m_noStrings; i++)
366 XtVaSetValues ((Widget) m_widgetList[i], XmNwidth, actualWidth, NULL);
367 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
2d120f83 368 NULL);
f97c9854
JS
369 }
370 if (height > -1)
371 {
372 int i;
373 for (i = 0; i < m_noStrings; i++)
374 XtVaSetValues ((Widget) m_widgetList[i], XmNheight, actualHeight, NULL);
375 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
2d120f83 376 NULL);
f97c9854 377 }
31528cd3 378
f97c9854
JS
379 if (managed)
380 XtManageChild ((Widget) m_formWidget);
381 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 382
bfc6fde4 383 wxControl::DoSetSize (x, y, width, height, sizeFlags);
4bb6408c
JS
384}
385
386wxString wxChoice::GetStringSelection () const
387{
388 int sel = GetSelection ();
389 if (sel > -1)
390 return wxString(this->GetString (sel));
391 else
f97c9854 392 return wxEmptyString;
4bb6408c
JS
393}
394
395bool wxChoice::SetStringSelection (const wxString& s)
396{
397 int sel = FindString (s);
398 if (sel > -1)
2d120f83
JS
399 {
400 SetSelection (sel);
401 return TRUE;
402 }
4bb6408c
JS
403 else
404 return FALSE;
405}
406
407void wxChoice::Command(wxCommandEvent & event)
408{
409 SetSelection (event.GetInt());
410 ProcessCommand (event);
411}
412
f9e02ac7 413void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
f97c9854
JS
414{
415 wxChoice *item = (wxChoice *) clientData;
416 if (item)
417 {
a4294b78 418 if (item->InSetValue())
f97c9854 419 return;
31528cd3 420
f97c9854
JS
421 char *s = NULL;
422 XtVaGetValues (w, XmNuserData, &s, NULL);
423 if (s)
424 {
55acd85e
JS
425 wxCommandEvent event (wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
426 event.SetEventObject(item);
f97c9854 427 event.m_commandInt = item->FindString (s);
2d120f83 428 // event.m_commandString = s;
f97c9854
JS
429 item->ProcessCommand (event);
430 }
431 }
432}
433
4b5f3fe6 434void wxChoice::ChangeFont(bool keepOriginalSize)
0d57be45 435{
321db4b6
JS
436 // Note that this causes the widget to be resized back
437 // to its original size! We therefore have to set the size
438 // back again. TODO: a better way in Motif?
da175b2c 439 if (m_font.Ok())
321db4b6
JS
440 {
441 int width, height, width1, height1;
442 GetSize(& width, & height);
31528cd3 443
da175b2c 444 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
321db4b6
JS
445 XtVaSetValues ((Widget) m_mainWidget, XmNfontList, fontList, NULL);
446 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
31528cd3 447
4b5f3fe6 448 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
321db4b6
JS
449 int i;
450 for (i = 0; i < m_noStrings; i++)
2d120f83 451 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
4b5f3fe6 452 */
321db4b6 453 GetSize(& width1, & height1);
4b5f3fe6 454 if (keepOriginalSize && (width != width1 || height != height1))
321db4b6
JS
455 {
456 SetSize(-1, -1, width, height);
457 }
458 }
0d57be45
JS
459}
460
461void wxChoice::ChangeBackgroundColour()
462{
321db4b6
JS
463 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
464 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
465 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
466 int i;
467 for (i = 0; i < m_noStrings; i++)
468 DoChangeBackgroundColour(m_widgetList[i], m_backgroundColour);
0d57be45
JS
469}
470
471void wxChoice::ChangeForegroundColour()
472{
321db4b6
JS
473 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
474 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
475 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
476 int i;
477 for (i = 0; i < m_noStrings; i++)
478 DoChangeForegroundColour(m_widgetList[i], m_foregroundColour);
0d57be45 479}
6adaedf0
JS
480
481
482// These implement functions needed by wxControlWithItems.
483// Unfortunately, they're not all implemented yet.
484
485int wxChoice::GetCount() const
486{
487 return Number();
488}
489
490int wxChoice::DoAppend(const wxString& item)
491{
492 Append(item);
493 return GetCount() - 1;
494}
495
496// Just appends, doesn't yet insert
497void wxChoice::DoInsertItems(const wxArrayString& items, int WXUNUSED(pos))
498{
499 size_t nItems = items.GetCount();
500
501 for ( size_t n = 0; n < nItems; n++ )
502 {
503 Append( items[n]);
504 }
505}
506
507void wxChoice::DoSetItems(const wxArrayString& items, void **WXUNUSED(clientData))
508{
509 Clear();
510 size_t nItems = items.GetCount();
511
512 for ( size_t n = 0; n < nItems; n++ )
513 {
514 Append(items[n]);
515 }
516}
517
518void wxChoice::DoSetFirstItem(int WXUNUSED(n))
519{
520 wxFAIL_MSG( wxT("wxChoice::DoSetFirstItem not implemented") );
521}
522
523void wxChoice::DoSetItemClientData(int WXUNUSED(n), void* WXUNUSED(clientData))
524{
525 wxFAIL_MSG( wxT("wxChoice::DoSetItemClientData not implemented") );
526}
527
528void* wxChoice::DoGetItemClientData(int WXUNUSED(n)) const
529{
530 wxFAIL_MSG( wxT("wxChoice::DoGetItemClientData not implemented") );
531 return (void*) NULL;
532}
533
534void wxChoice::DoSetItemClientObject(int WXUNUSED(n), wxClientData* WXUNUSED(clientData))
535{
536 wxFAIL_MSG( wxT("wxChoice::DoSetItemClientObject not implemented") );
537}
538
539wxClientData* wxChoice::DoGetItemClientObject(int WXUNUSED(n)) const
540{
541 wxFAIL_MSG( wxT("wxChoice::DoGetItemClientObject not implemented") );
542 return (wxClientData*) NULL;
543}
544
545void wxChoice::Select(int n)
546{
547 SetSelection(n);
548}
549
550void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
551{
552 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
553}