]> git.saurik.com Git - wxWidgets.git/blame - src/motif/choice.cpp
more informative error messages
[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
bcd055ae 16#ifdef __VMS
4dff3400
JJ
17#define XtDisplay XTDISPLAY
18#define XtParent XTPARENT
19#endif
20
f6045f99
GD
21#include "wx/defs.h"
22
4bb6408c 23#include "wx/choice.h"
f97c9854
JS
24#include "wx/utils.h"
25
338dd992
JJ
26#ifdef __VMS__
27#pragma message disable nosimpint
28#endif
f97c9854
JS
29#include <Xm/Xm.h>
30#include <Xm/PushBG.h>
31#include <Xm/PushB.h>
32#include <Xm/RowColumn.h>
338dd992
JJ
33#ifdef __VMS__
34#pragma message enable nosimpint
35#endif
f97c9854
JS
36
37#include "wx/motif/private.h"
4bb6408c 38
4bb6408c 39IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
4bb6408c 40
f97c9854 41void wxChoiceCallback (Widget w, XtPointer clientData,
2d120f83 42 XtPointer ptr);
f97c9854
JS
43
44wxChoice::wxChoice()
45{
ec75d791
MB
46 Init();
47}
48
49void wxChoice::Init()
50{
51 InitBase();
52
f97c9854
JS
53 m_noStrings = 0;
54 m_buttonWidget = (WXWidget) 0;
55 m_menuWidget = (WXWidget) 0;
f97c9854 56 m_formWidget = (WXWidget) 0;
f97c9854
JS
57}
58
4bb6408c 59bool wxChoice::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
60 const wxPoint& pos,
61 const wxSize& size,
62 int n, const wxString choices[],
63 long style,
64 const wxValidator& validator,
65 const wxString& name)
4bb6408c 66{
ec75d791
MB
67 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
68 return FALSE;
31528cd3 69
f97c9854 70 Widget parentWidget = (Widget) parent->GetClientWidget();
31528cd3
VZ
71
72 m_formWidget = (WXWidget) XtVaCreateManagedWidget(name.c_str(),
2d120f83
JS
73 xmRowColumnWidgetClass, parentWidget,
74 XmNmarginHeight, 0,
75 XmNmarginWidth, 0,
76 XmNpacking, XmPACK_TIGHT,
77 XmNorientation, XmHORIZONTAL,
78 NULL);
31528cd3 79
f97c9854 80 XtVaSetValues ((Widget) m_formWidget, XmNspacing, 0, NULL);
31528cd3 81
2d120f83 82 /*
f97c9854
JS
83 * Create the popup menu
84 */
ec75d791
MB
85 m_menuWidget = (WXWidget) XmCreatePulldownMenu ((Widget) m_formWidget,
86 "choiceMenu", NULL, 0);
31528cd3 87
f97c9854
JS
88 if (n > 0)
89 {
90 int i;
91 for (i = 0; i < n; i++)
92 Append (choices[i]);
93 }
31528cd3 94
2d120f83 95 /*
f97c9854
JS
96 * Create button
97 */
98 Arg args[10];
99 Cardinal argcnt = 0;
31528cd3 100
ec75d791
MB
101 XtSetArg (args[argcnt], XmNsubMenuId, (Widget) m_menuWidget); ++argcnt;
102 XtSetArg (args[argcnt], XmNmarginWidth, 0); ++argcnt;
103 XtSetArg (args[argcnt], XmNmarginHeight, 0); ++argcnt;
104 XtSetArg (args[argcnt], XmNpacking, XmPACK_TIGHT); ++argcnt;
105 m_buttonWidget = (WXWidget) XmCreateOptionMenu ((Widget) m_formWidget,
106 "choiceButton",
107 args, argcnt);
31528cd3 108
f97c9854 109 m_mainWidget = m_buttonWidget;
31528cd3 110
f97c9854 111 XtManageChild ((Widget) m_buttonWidget);
9838df2c 112
f97c9854
JS
113 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
114 // Some time ago, I reported a problem with wxChoice-items under
115 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
116 // that I have found the code responsible for this behaviour.
117#if XmVersion >= 1002
118#if XmVersion < 2000
9838df2c
JS
119 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
120 // in controls sample.
dfe1eee3
VZ
121 //
122 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
123 // XtUnmanageChild (optionLabel);
f97c9854
JS
124#endif
125#endif
9838df2c 126
a29ee706
MB
127 wxSize bestSize = GetBestSize();
128 if( size.x > 0 ) bestSize.x = size.x;
129 if( size.y > 0 ) bestSize.y = size.y;
130
f97c9854 131 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 132
4b5f3fe6 133 ChangeFont(FALSE);
9838df2c 134
ec75d791 135 AttachWidget (parent, m_buttonWidget, m_formWidget,
a29ee706 136 pos.x, pos.y, bestSize.x, bestSize.y);
31528cd3 137
0d57be45 138 ChangeBackgroundColour();
31528cd3 139
f97c9854
JS
140 return TRUE;
141}
142
143wxChoice::~wxChoice()
144{
2d120f83
JS
145 // For some reason destroying the menuWidget
146 // can cause crashes on some machines. It will
147 // be deleted implicitly by deleting the parent form
148 // anyway.
149 // XtDestroyWidget (menuWidget);
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 }
ec75d791
MB
163 if ( HasClientObjectData() )
164 m_clientDataDict.DestroyData();
4bb6408c
JS
165}
166
c33c81c3 167int wxChoice::DoAppend(const wxString& item)
4bb6408c 168{
31528cd3 169 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
f97c9854 170#if USE_GADGETS
2d120f83 171 xmPushButtonGadgetClass, (Widget) m_menuWidget,
f97c9854 172#else
2d120f83 173 xmPushButtonWidgetClass, (Widget) m_menuWidget,
f97c9854 174#endif
2d120f83 175 NULL);
31528cd3 176
2d120f83 177 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
31528cd3 178
e1aae528
MB
179 if( m_font.Ok() )
180 wxDoChangeFont( w, m_font );
31528cd3 181
ec75d791 182 m_widgetArray.Add(w);
31528cd3 183
f6bcfd97
BP
184 char mnem = wxFindMnemonic ((char*) (const char*) item);
185 if (mnem != 0)
186 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
31528cd3 187
ec75d791
MB
188 XtAddCallback (w, XmNactivateCallback,
189 (XtCallbackProc) wxChoiceCallback,
190 (XtPointer) this);
31528cd3 191
f6bcfd97
BP
192 if (m_noStrings == 0 && m_buttonWidget)
193 {
194 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
195 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
c13c9657 196 wxXmString text( item );
f6bcfd97 197 XtVaSetValues (label,
c13c9657 198 XmNlabelString, text(),
f6bcfd97 199 NULL);
f6bcfd97 200 }
ec75d791 201 m_stringList.Add(item);
f6bcfd97 202 m_noStrings ++;
c33c81c3 203
ec75d791 204 return GetCount() - 1;
4bb6408c
JS
205}
206
ec75d791 207void wxChoice::Delete(int n)
4bb6408c 208{
ec75d791
MB
209 Widget w = (Widget)m_widgetArray[n];
210 XtRemoveCallback(w, XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
211 (XtPointer)this);
212 m_stringList.DeleteNode(m_stringList.Item(n));
213 m_widgetArray.RemoveAt(size_t(n));
214 m_clientDataDict.Delete(n, HasClientObjectData());
31528cd3 215
ec75d791 216 XtDestroyWidget(w);
4bb6408c
JS
217 m_noStrings --;
218}
219
220void wxChoice::Clear()
221{
f97c9854 222 m_stringList.Clear ();
fd304d98 223 size_t i;
f97c9854
JS
224 for (i = 0; i < m_noStrings; i++)
225 {
ec75d791
MB
226 XtRemoveCallback((Widget) m_widgetArray[i],
227 XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
228 (XtPointer)this);
229 XtUnmanageChild ((Widget) m_widgetArray[i]);
230 XtDestroyWidget ((Widget) m_widgetArray[i]);
f97c9854 231 }
ec75d791 232 m_widgetArray.Clear();
f97c9854 233 if (m_buttonWidget)
ec75d791
MB
234 XtVaSetValues ((Widget) m_buttonWidget,
235 XmNmenuHistory, (Widget) NULL,
236 NULL);
f6bcfd97
BP
237
238 if ( HasClientObjectData() )
ec75d791 239 m_clientDataDict.DestroyData();
f6bcfd97 240
4bb6408c
JS
241 m_noStrings = 0;
242}
243
244int wxChoice::GetSelection() const
245{
2d120f83 246 XmString text;
2d120f83
JS
247 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
248 XtVaGetValues (label,
249 XmNlabelString, &text,
250 NULL);
da494b40
MB
251 wxXmString freeMe(text);
252 wxString s = wxXmStringToString( text );
31528cd3 253
da494b40 254 if (!s.IsEmpty())
2d120f83
JS
255 {
256 int i = 0;
ec75d791
MB
257 for (wxStringListNode* node = m_stringList.GetFirst ();
258 node; node = node->GetNext ())
f97c9854 259 {
da494b40 260 if (wxStrcmp(node->GetData(), s.c_str()) == 0)
2d120f83 261 {
2d120f83
JS
262 return i;
263 }
264 else
265 i++;
266 } // for()
31528cd3 267
2d120f83
JS
268 return -1;
269 }
2d120f83 270 return -1;
4bb6408c
JS
271}
272
273void wxChoice::SetSelection(int n)
274{
2d120f83 275 m_inSetValue = TRUE;
31528cd3 276
ec75d791 277 wxStringListNode *node = m_stringList.Item(n);
2d120f83 278 if (node)
f97c9854 279 {
ec75d791 280#if 0
2d120f83 281 Dimension selectionWidth, selectionHeight;
ec75d791 282#endif
fd304d98 283 wxXmString text( node->GetData() );
ec75d791
MB
284// MBN: this seems silly, at best, and causes wxChoices to be clipped:
285// will remove "soon"
286#if 0
287 XtVaGetValues ((Widget) m_widgetArray[n],
288 XmNwidth, &selectionWidth,
289 XmNheight, &selectionHeight,
290 NULL);
291#endif
2d120f83
JS
292 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
293 XtVaSetValues (label,
ec75d791 294 XmNlabelString, text(),
2d120f83 295 NULL);
ec75d791 296#if 0
2d120f83
JS
297 XtVaSetValues ((Widget) m_buttonWidget,
298 XmNwidth, selectionWidth, XmNheight, selectionHeight,
ec75d791
MB
299 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
300#endif
f97c9854 301 }
2d120f83 302 m_inSetValue = FALSE;
4bb6408c
JS
303}
304
305int wxChoice::FindString(const wxString& s) const
306{
f97c9854 307 int i = 0;
ec75d791
MB
308 for (wxStringListNode* node = m_stringList.GetFirst();
309 node; node = node->GetNext ())
f97c9854 310 {
ec75d791 311 if (s == node->GetData())
f97c9854 312 return i;
ec75d791
MB
313
314 i++;
f97c9854 315 }
ec75d791 316
f97c9854 317 return -1;
4bb6408c
JS
318}
319
320wxString wxChoice::GetString(int n) const
321{
ec75d791 322 wxStringListNode *node = m_stringList.Item(n);
2d120f83 323 if (node)
ec75d791 324 return node->GetData();
2d120f83
JS
325 else
326 return wxEmptyString;
f97c9854
JS
327}
328
329void wxChoice::SetColumns(int n)
330{
2d120f83 331 if (n<1) n = 1 ;
31528cd3 332
2d120f83
JS
333 short numColumns = n ;
334 Arg args[3];
31528cd3 335
2d120f83
JS
336 XtSetArg(args[0], XmNnumColumns, numColumns);
337 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
338 XtSetValues((Widget) m_menuWidget,args,2) ;
f97c9854
JS
339}
340
341int wxChoice::GetColumns(void) const
342{
2d120f83 343 short numColumns ;
31528cd3 344
2d120f83
JS
345 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
346 return numColumns ;
f97c9854
JS
347}
348
349void wxChoice::SetFocus()
350{
2d120f83 351 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
4bb6408c
JS
352}
353
bfc6fde4 354void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 355{
f97c9854
JS
356 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
357 bool managed = XtIsManaged((Widget) m_formWidget);
31528cd3 358
f97c9854
JS
359 if (managed)
360 XtUnmanageChild ((Widget) m_formWidget);
31528cd3 361
f97c9854 362 int actualWidth = width, actualHeight = height;
31528cd3 363
f97c9854
JS
364 if (width > -1)
365 {
fd304d98 366 size_t i;
f97c9854 367 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
368 XtVaSetValues ((Widget) m_widgetArray[i],
369 XmNwidth, actualWidth,
370 NULL);
f97c9854 371 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
2d120f83 372 NULL);
f97c9854
JS
373 }
374 if (height > -1)
375 {
fd304d98 376 size_t i;
f97c9854 377 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
378 XtVaSetValues ((Widget) m_widgetArray[i],
379 XmNheight, actualHeight,
380 NULL);
f97c9854 381 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
2d120f83 382 NULL);
f97c9854 383 }
31528cd3 384
f97c9854
JS
385 if (managed)
386 XtManageChild ((Widget) m_formWidget);
387 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 388
bfc6fde4 389 wxControl::DoSetSize (x, y, width, height, sizeFlags);
4bb6408c
JS
390}
391
4bb6408c
JS
392void wxChoice::Command(wxCommandEvent & event)
393{
394 SetSelection (event.GetInt());
395 ProcessCommand (event);
396}
397
f9e02ac7 398void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
f97c9854
JS
399{
400 wxChoice *item = (wxChoice *) clientData;
401 if (item)
402 {
a4294b78 403 if (item->InSetValue())
f97c9854 404 return;
31528cd3 405
ec75d791
MB
406 int n = item->GetWidgets().Index(w);
407 if (n != wxNOT_FOUND)
f97c9854 408 {
ec75d791 409 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
55acd85e 410 event.SetEventObject(item);
ec75d791
MB
411 event.m_commandInt = n;
412 event.m_commandString = item->GetStrings().Item(n)->GetData();
413 if ( item->HasClientObjectData() )
414 event.SetClientObject( item->GetClientObject(n) );
415 else if ( item->HasClientUntypedData() )
416 event.SetClientData( item->GetClientData(n) );
f97c9854
JS
417 item->ProcessCommand (event);
418 }
419 }
420}
421
4b5f3fe6 422void wxChoice::ChangeFont(bool keepOriginalSize)
0d57be45 423{
321db4b6
JS
424 // Note that this causes the widget to be resized back
425 // to its original size! We therefore have to set the size
426 // back again. TODO: a better way in Motif?
da175b2c 427 if (m_font.Ok())
321db4b6
JS
428 {
429 int width, height, width1, height1;
430 GetSize(& width, & height);
31528cd3 431
da494b40
MB
432 WXFontType fontType =
433 m_font.GetFontType(XtDisplay((Widget) m_mainWidget));
434 WXString fontTag = wxFont::GetFontTag();
435
436 XtVaSetValues ((Widget) m_formWidget, fontTag, fontType, NULL);
437 XtVaSetValues ((Widget) m_buttonWidget, fontTag, fontType, NULL);
31528cd3 438
ec75d791
MB
439 for( size_t i = 0; i < m_noStrings; ++i )
440 XtVaSetValues( (Widget)m_widgetArray[i],
da494b40 441 fontTag, fontType,
ec75d791
MB
442 NULL );
443
321db4b6 444 GetSize(& width1, & height1);
4b5f3fe6 445 if (keepOriginalSize && (width != width1 || height != height1))
321db4b6
JS
446 {
447 SetSize(-1, -1, width, height);
448 }
449 }
0d57be45
JS
450}
451
452void wxChoice::ChangeBackgroundColour()
453{
321db4b6
JS
454 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
455 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
456 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
fd304d98 457 size_t i;
321db4b6 458 for (i = 0; i < m_noStrings; i++)
ec75d791 459 DoChangeBackgroundColour(m_widgetArray[i], m_backgroundColour);
0d57be45
JS
460}
461
462void wxChoice::ChangeForegroundColour()
463{
321db4b6
JS
464 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
465 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
466 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
fd304d98 467 size_t i;
321db4b6 468 for (i = 0; i < m_noStrings; i++)
ec75d791 469 DoChangeForegroundColour(m_widgetArray[i], m_foregroundColour);
0d57be45 470}
6adaedf0 471
6adaedf0
JS
472int wxChoice::GetCount() const
473{
ec75d791 474 return m_noStrings;
6adaedf0
JS
475}
476
ec75d791 477void wxChoice::DoSetItemClientData(int n, void* clientData)
6adaedf0 478{
ec75d791 479 m_clientDataDict.Set(n, (wxClientData*)clientData, FALSE);
6adaedf0
JS
480}
481
ec75d791 482void* wxChoice::DoGetItemClientData(int n) const
6adaedf0 483{
ec75d791 484 return (void*)m_clientDataDict.Get(n);
6adaedf0
JS
485}
486
ec75d791 487void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
6adaedf0 488{
ec75d791
MB
489 // don't delete, wxItemContainer does that for us
490 m_clientDataDict.Set(n, clientData, FALSE);
6adaedf0
JS
491}
492
ec75d791 493wxClientData* wxChoice::DoGetItemClientObject(int n) const
6adaedf0 494{
ec75d791 495 return m_clientDataDict.Get(n);
6adaedf0
JS
496}
497
ec75d791 498void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
6adaedf0 499{
ec75d791 500 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
6adaedf0
JS
501}
502
ec75d791 503wxSize wxChoice::GetItemsSize() const
6adaedf0 504{
ec75d791 505 int x, y, mx = 0, my = 0;
f6bcfd97 506
ec75d791
MB
507 // get my
508 GetTextExtent( "|", &x, &my );
f6bcfd97 509
ec75d791
MB
510 wxStringList::Node* curr = m_stringList.GetFirst();
511 while( curr )
512 {
513 GetTextExtent( curr->GetData(), &x, &y );
514 mx = wxMax( mx, x );
515 my = wxMax( my, y );
516 curr = curr->GetNext();
517 }
6adaedf0 518
ec75d791 519 return wxSize( mx, my );
6adaedf0
JS
520}
521
ec75d791 522wxSize wxChoice::DoGetBestSize() const
6adaedf0 523{
ec75d791
MB
524 wxSize items = GetItemsSize();
525 // FIXME arbitrary constants
526 return wxSize( ( items.x ? items.x + 50 : 120 ),
527 items.y + 15 );
6adaedf0 528}