]> git.saurik.com Git - wxWidgets.git/blame - src/motif/choice.cpp
Applied patch [ 642157 ] [MSW] HMENU resource leak from wxMenuBar
[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
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
ec75d791
MB
102 XtSetArg (args[argcnt], XmNsubMenuId, (Widget) m_menuWidget); ++argcnt;
103 XtSetArg (args[argcnt], XmNmarginWidth, 0); ++argcnt;
104 XtSetArg (args[argcnt], XmNmarginHeight, 0); ++argcnt;
105 XtSetArg (args[argcnt], XmNpacking, XmPACK_TIGHT); ++argcnt;
106 m_buttonWidget = (WXWidget) XmCreateOptionMenu ((Widget) m_formWidget,
107 "choiceButton",
108 args, argcnt);
31528cd3 109
f97c9854 110 m_mainWidget = m_buttonWidget;
31528cd3 111
f97c9854 112 XtManageChild ((Widget) m_buttonWidget);
9838df2c 113
f97c9854
JS
114 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
115 // Some time ago, I reported a problem with wxChoice-items under
116 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
117 // that I have found the code responsible for this behaviour.
118#if XmVersion >= 1002
119#if XmVersion < 2000
9838df2c
JS
120 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
121 // in controls sample.
dfe1eee3
VZ
122 //
123 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
124 // XtUnmanageChild (optionLabel);
f97c9854
JS
125#endif
126#endif
9838df2c 127
f97c9854 128 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 129
4b5f3fe6 130 ChangeFont(FALSE);
9838df2c 131
ec75d791
MB
132 AttachWidget (parent, m_buttonWidget, m_formWidget,
133 pos.x, pos.y, size.x, size.y);
31528cd3 134
0d57be45 135 ChangeBackgroundColour();
31528cd3 136
f97c9854
JS
137 return TRUE;
138}
139
140wxChoice::~wxChoice()
141{
2d120f83
JS
142 // For some reason destroying the menuWidget
143 // can cause crashes on some machines. It will
144 // be deleted implicitly by deleting the parent form
145 // anyway.
146 // XtDestroyWidget (menuWidget);
31528cd3 147
8aa04e8b
JS
148 if (GetMainWidget())
149 {
150 DetachWidget(GetMainWidget()); // Removes event handlers
b412f9be 151 DetachWidget(m_formWidget);
31528cd3 152
8aa04e8b
JS
153 XtDestroyWidget((Widget) m_formWidget);
154 m_formWidget = (WXWidget) 0;
31528cd3 155
8aa04e8b
JS
156 // Presumably the other widgets have been deleted now, via the form
157 m_mainWidget = (WXWidget) 0;
158 m_buttonWidget = (WXWidget) 0;
159 }
ec75d791
MB
160 if ( HasClientObjectData() )
161 m_clientDataDict.DestroyData();
4bb6408c
JS
162}
163
c33c81c3 164int wxChoice::DoAppend(const wxString& item)
4bb6408c 165{
31528cd3 166 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
f97c9854 167#if USE_GADGETS
2d120f83 168 xmPushButtonGadgetClass, (Widget) m_menuWidget,
f97c9854 169#else
2d120f83 170 xmPushButtonWidgetClass, (Widget) m_menuWidget,
f97c9854 171#endif
2d120f83 172 NULL);
31528cd3 173
2d120f83 174 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
31528cd3 175
da175b2c 176 if (m_font.Ok())
2d120f83 177 XtVaSetValues (w,
da175b2c 178 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
2d120f83 179 NULL);
31528cd3 180
ec75d791 181 m_widgetArray.Add(w);
31528cd3 182
f6bcfd97
BP
183 char mnem = wxFindMnemonic ((char*) (const char*) item);
184 if (mnem != 0)
185 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
31528cd3 186
ec75d791
MB
187 XtAddCallback (w, XmNactivateCallback,
188 (XtCallbackProc) wxChoiceCallback,
189 (XtPointer) this);
31528cd3 190
f6bcfd97
BP
191 if (m_noStrings == 0 && m_buttonWidget)
192 {
193 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
194 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
195 XmString text = XmStringCreateSimple ((char*) (const char*) item);
196 XtVaSetValues (label,
197 XmNlabelString, text,
198 NULL);
199 XmStringFree (text);
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
JS
222 m_stringList.Clear ();
223 int i;
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
JS
246 XmString text;
247 char *s;
248 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
249 XtVaGetValues (label,
250 XmNlabelString, &text,
251 NULL);
31528cd3 252
2d120f83
JS
253 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
254 {
255 int i = 0;
ec75d791
MB
256 for (wxStringListNode* node = m_stringList.GetFirst ();
257 node; node = node->GetNext ())
f97c9854 258 {
ec75d791 259 if (strcmp(node->GetData(), s) == 0)
2d120f83
JS
260 {
261 XmStringFree(text) ;
262 XtFree (s);
263 return i;
264 }
265 else
266 i++;
267 } // for()
31528cd3 268
2d120f83
JS
269 XmStringFree(text) ;
270 XtFree (s);
271 return -1;
272 }
273 XmStringFree(text) ;
274 return -1;
4bb6408c
JS
275}
276
277void wxChoice::SetSelection(int n)
278{
2d120f83 279 m_inSetValue = TRUE;
31528cd3 280
ec75d791 281 wxStringListNode *node = m_stringList.Item(n);
2d120f83 282 if (node)
f97c9854 283 {
ec75d791 284#if 0
2d120f83 285 Dimension selectionWidth, selectionHeight;
ec75d791
MB
286#endif
287 wxXmString text( (char*)node->Data() );
288// MBN: this seems silly, at best, and causes wxChoices to be clipped:
289// will remove "soon"
290#if 0
291 XtVaGetValues ((Widget) m_widgetArray[n],
292 XmNwidth, &selectionWidth,
293 XmNheight, &selectionHeight,
294 NULL);
295#endif
2d120f83
JS
296 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
297 XtVaSetValues (label,
ec75d791 298 XmNlabelString, text(),
2d120f83 299 NULL);
ec75d791 300#if 0
2d120f83
JS
301 XtVaSetValues ((Widget) m_buttonWidget,
302 XmNwidth, selectionWidth, XmNheight, selectionHeight,
ec75d791
MB
303 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
304#endif
f97c9854 305 }
2d120f83 306 m_inSetValue = FALSE;
4bb6408c
JS
307}
308
309int wxChoice::FindString(const wxString& s) const
310{
f97c9854 311 int i = 0;
ec75d791
MB
312 for (wxStringListNode* node = m_stringList.GetFirst();
313 node; node = node->GetNext ())
f97c9854 314 {
ec75d791 315 if (s == node->GetData())
f97c9854 316 return i;
ec75d791
MB
317
318 i++;
f97c9854 319 }
ec75d791 320
f97c9854 321 return -1;
4bb6408c
JS
322}
323
324wxString wxChoice::GetString(int n) const
325{
ec75d791 326 wxStringListNode *node = m_stringList.Item(n);
2d120f83 327 if (node)
ec75d791 328 return node->GetData();
2d120f83
JS
329 else
330 return wxEmptyString;
f97c9854
JS
331}
332
333void wxChoice::SetColumns(int n)
334{
2d120f83 335 if (n<1) n = 1 ;
31528cd3 336
2d120f83
JS
337 short numColumns = n ;
338 Arg args[3];
31528cd3 339
2d120f83
JS
340 XtSetArg(args[0], XmNnumColumns, numColumns);
341 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
342 XtSetValues((Widget) m_menuWidget,args,2) ;
f97c9854
JS
343}
344
345int wxChoice::GetColumns(void) const
346{
2d120f83 347 short numColumns ;
31528cd3 348
2d120f83
JS
349 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
350 return numColumns ;
f97c9854
JS
351}
352
353void wxChoice::SetFocus()
354{
2d120f83 355 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
4bb6408c
JS
356}
357
bfc6fde4 358void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 359{
f97c9854
JS
360 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
361 bool managed = XtIsManaged((Widget) m_formWidget);
31528cd3 362
f97c9854
JS
363 if (managed)
364 XtUnmanageChild ((Widget) m_formWidget);
31528cd3 365
f97c9854 366 int actualWidth = width, actualHeight = height;
31528cd3 367
f97c9854
JS
368 if (width > -1)
369 {
370 int i;
371 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
372 XtVaSetValues ((Widget) m_widgetArray[i],
373 XmNwidth, actualWidth,
374 NULL);
f97c9854 375 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
2d120f83 376 NULL);
f97c9854
JS
377 }
378 if (height > -1)
379 {
380 int i;
381 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
382 XtVaSetValues ((Widget) m_widgetArray[i],
383 XmNheight, actualHeight,
384 NULL);
f97c9854 385 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
2d120f83 386 NULL);
f97c9854 387 }
31528cd3 388
f97c9854
JS
389 if (managed)
390 XtManageChild ((Widget) m_formWidget);
391 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 392
bfc6fde4 393 wxControl::DoSetSize (x, y, width, height, sizeFlags);
4bb6408c
JS
394}
395
4bb6408c
JS
396void wxChoice::Command(wxCommandEvent & event)
397{
398 SetSelection (event.GetInt());
399 ProcessCommand (event);
400}
401
f9e02ac7 402void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
f97c9854
JS
403{
404 wxChoice *item = (wxChoice *) clientData;
405 if (item)
406 {
a4294b78 407 if (item->InSetValue())
f97c9854 408 return;
31528cd3 409
ec75d791
MB
410 int n = item->GetWidgets().Index(w);
411 if (n != wxNOT_FOUND)
f97c9854 412 {
ec75d791 413 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
55acd85e 414 event.SetEventObject(item);
ec75d791
MB
415 event.m_commandInt = n;
416 event.m_commandString = item->GetStrings().Item(n)->GetData();
417 if ( item->HasClientObjectData() )
418 event.SetClientObject( item->GetClientObject(n) );
419 else if ( item->HasClientUntypedData() )
420 event.SetClientData( item->GetClientData(n) );
f97c9854
JS
421 item->ProcessCommand (event);
422 }
423 }
424}
425
4b5f3fe6 426void wxChoice::ChangeFont(bool keepOriginalSize)
0d57be45 427{
321db4b6
JS
428 // Note that this causes the widget to be resized back
429 // to its original size! We therefore have to set the size
430 // back again. TODO: a better way in Motif?
da175b2c 431 if (m_font.Ok())
321db4b6
JS
432 {
433 int width, height, width1, height1;
434 GetSize(& width, & height);
31528cd3 435
da175b2c 436 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
ec75d791 437 XtVaSetValues ((Widget) m_formWidget, XmNfontList, fontList, NULL);
321db4b6 438 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
31528cd3 439
ec75d791
MB
440 for( size_t i = 0; i < m_noStrings; ++i )
441 XtVaSetValues( (Widget)m_widgetArray[i],
442 XmNfontList, fontList,
443 NULL );
444
321db4b6 445 GetSize(& width1, & height1);
4b5f3fe6 446 if (keepOriginalSize && (width != width1 || height != height1))
321db4b6
JS
447 {
448 SetSize(-1, -1, width, height);
449 }
450 }
0d57be45
JS
451}
452
453void wxChoice::ChangeBackgroundColour()
454{
321db4b6
JS
455 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
456 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
457 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
458 int i;
459 for (i = 0; i < m_noStrings; i++)
ec75d791 460 DoChangeBackgroundColour(m_widgetArray[i], m_backgroundColour);
0d57be45
JS
461}
462
463void wxChoice::ChangeForegroundColour()
464{
321db4b6
JS
465 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
466 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
467 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
468 int i;
469 for (i = 0; i < m_noStrings; i++)
ec75d791 470 DoChangeForegroundColour(m_widgetArray[i], m_foregroundColour);
0d57be45 471}
6adaedf0 472
6adaedf0
JS
473int wxChoice::GetCount() const
474{
ec75d791 475 return m_noStrings;
6adaedf0
JS
476}
477
ec75d791 478void wxChoice::DoSetItemClientData(int n, void* clientData)
6adaedf0 479{
ec75d791 480 m_clientDataDict.Set(n, (wxClientData*)clientData, FALSE);
6adaedf0
JS
481}
482
ec75d791 483void* wxChoice::DoGetItemClientData(int n) const
6adaedf0 484{
ec75d791 485 return (void*)m_clientDataDict.Get(n);
6adaedf0
JS
486}
487
ec75d791 488void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
6adaedf0 489{
ec75d791
MB
490 // don't delete, wxItemContainer does that for us
491 m_clientDataDict.Set(n, clientData, FALSE);
6adaedf0
JS
492}
493
ec75d791 494wxClientData* wxChoice::DoGetItemClientObject(int n) const
6adaedf0 495{
ec75d791 496 return m_clientDataDict.Get(n);
6adaedf0
JS
497}
498
ec75d791 499void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
6adaedf0 500{
ec75d791 501 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
6adaedf0
JS
502}
503
ec75d791 504wxSize wxChoice::GetItemsSize() const
6adaedf0 505{
ec75d791 506 int x, y, mx = 0, my = 0;
f6bcfd97 507
ec75d791
MB
508 // get my
509 GetTextExtent( "|", &x, &my );
f6bcfd97 510
ec75d791
MB
511 wxStringList::Node* curr = m_stringList.GetFirst();
512 while( curr )
513 {
514 GetTextExtent( curr->GetData(), &x, &y );
515 mx = wxMax( mx, x );
516 my = wxMax( my, y );
517 curr = curr->GetNext();
518 }
6adaedf0 519
ec75d791 520 return wxSize( mx, my );
6adaedf0
JS
521}
522
ec75d791 523wxSize wxChoice::DoGetBestSize() const
6adaedf0 524{
ec75d791
MB
525 wxSize items = GetItemsSize();
526 // FIXME arbitrary constants
527 return wxSize( ( items.x ? items.x + 50 : 120 ),
528 items.y + 15 );
6adaedf0 529}