]> git.saurik.com Git - wxWidgets.git/blame - src/motif/choice.cpp
Applied patch [ 666379 ] CalCtrl doesn't use best sizes for the year combobox
[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
da175b2c 179 if (m_font.Ok())
2d120f83 180 XtVaSetValues (w,
da175b2c 181 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
2d120f83 182 NULL);
31528cd3 183
ec75d791 184 m_widgetArray.Add(w);
31528cd3 185
f6bcfd97
BP
186 char mnem = wxFindMnemonic ((char*) (const char*) item);
187 if (mnem != 0)
188 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
31528cd3 189
ec75d791
MB
190 XtAddCallback (w, XmNactivateCallback,
191 (XtCallbackProc) wxChoiceCallback,
192 (XtPointer) this);
31528cd3 193
f6bcfd97
BP
194 if (m_noStrings == 0 && m_buttonWidget)
195 {
196 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
197 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
c13c9657 198 wxXmString text( item );
f6bcfd97 199 XtVaSetValues (label,
c13c9657 200 XmNlabelString, text(),
f6bcfd97 201 NULL);
f6bcfd97 202 }
ec75d791 203 m_stringList.Add(item);
f6bcfd97 204 m_noStrings ++;
c33c81c3 205
ec75d791 206 return GetCount() - 1;
4bb6408c
JS
207}
208
ec75d791 209void wxChoice::Delete(int n)
4bb6408c 210{
ec75d791
MB
211 Widget w = (Widget)m_widgetArray[n];
212 XtRemoveCallback(w, XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
213 (XtPointer)this);
214 m_stringList.DeleteNode(m_stringList.Item(n));
215 m_widgetArray.RemoveAt(size_t(n));
216 m_clientDataDict.Delete(n, HasClientObjectData());
31528cd3 217
ec75d791 218 XtDestroyWidget(w);
4bb6408c
JS
219 m_noStrings --;
220}
221
222void wxChoice::Clear()
223{
f97c9854 224 m_stringList.Clear ();
fd304d98 225 size_t i;
f97c9854
JS
226 for (i = 0; i < m_noStrings; i++)
227 {
ec75d791
MB
228 XtRemoveCallback((Widget) m_widgetArray[i],
229 XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
230 (XtPointer)this);
231 XtUnmanageChild ((Widget) m_widgetArray[i]);
232 XtDestroyWidget ((Widget) m_widgetArray[i]);
f97c9854 233 }
ec75d791 234 m_widgetArray.Clear();
f97c9854 235 if (m_buttonWidget)
ec75d791
MB
236 XtVaSetValues ((Widget) m_buttonWidget,
237 XmNmenuHistory, (Widget) NULL,
238 NULL);
f6bcfd97
BP
239
240 if ( HasClientObjectData() )
ec75d791 241 m_clientDataDict.DestroyData();
f6bcfd97 242
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);
31528cd3 254
2d120f83
JS
255 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
256 {
257 int i = 0;
ec75d791
MB
258 for (wxStringListNode* node = m_stringList.GetFirst ();
259 node; node = node->GetNext ())
f97c9854 260 {
ec75d791 261 if (strcmp(node->GetData(), s) == 0)
2d120f83
JS
262 {
263 XmStringFree(text) ;
264 XtFree (s);
265 return i;
266 }
267 else
268 i++;
269 } // for()
31528cd3 270
2d120f83
JS
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 281 m_inSetValue = TRUE;
31528cd3 282
ec75d791 283 wxStringListNode *node = m_stringList.Item(n);
2d120f83 284 if (node)
f97c9854 285 {
ec75d791 286#if 0
2d120f83 287 Dimension selectionWidth, selectionHeight;
ec75d791 288#endif
fd304d98 289 wxXmString text( node->GetData() );
ec75d791
MB
290// MBN: this seems silly, at best, and causes wxChoices to be clipped:
291// will remove "soon"
292#if 0
293 XtVaGetValues ((Widget) m_widgetArray[n],
294 XmNwidth, &selectionWidth,
295 XmNheight, &selectionHeight,
296 NULL);
297#endif
2d120f83
JS
298 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
299 XtVaSetValues (label,
ec75d791 300 XmNlabelString, text(),
2d120f83 301 NULL);
ec75d791 302#if 0
2d120f83
JS
303 XtVaSetValues ((Widget) m_buttonWidget,
304 XmNwidth, selectionWidth, XmNheight, selectionHeight,
ec75d791
MB
305 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
306#endif
f97c9854 307 }
2d120f83 308 m_inSetValue = FALSE;
4bb6408c
JS
309}
310
311int wxChoice::FindString(const wxString& s) const
312{
f97c9854 313 int i = 0;
ec75d791
MB
314 for (wxStringListNode* node = m_stringList.GetFirst();
315 node; node = node->GetNext ())
f97c9854 316 {
ec75d791 317 if (s == node->GetData())
f97c9854 318 return i;
ec75d791
MB
319
320 i++;
f97c9854 321 }
ec75d791 322
f97c9854 323 return -1;
4bb6408c
JS
324}
325
326wxString wxChoice::GetString(int n) const
327{
ec75d791 328 wxStringListNode *node = m_stringList.Item(n);
2d120f83 329 if (node)
ec75d791 330 return node->GetData();
2d120f83
JS
331 else
332 return wxEmptyString;
f97c9854
JS
333}
334
335void wxChoice::SetColumns(int n)
336{
2d120f83 337 if (n<1) n = 1 ;
31528cd3 338
2d120f83
JS
339 short numColumns = n ;
340 Arg args[3];
31528cd3 341
2d120f83
JS
342 XtSetArg(args[0], XmNnumColumns, numColumns);
343 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
344 XtSetValues((Widget) m_menuWidget,args,2) ;
f97c9854
JS
345}
346
347int wxChoice::GetColumns(void) const
348{
2d120f83 349 short numColumns ;
31528cd3 350
2d120f83
JS
351 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
352 return numColumns ;
f97c9854
JS
353}
354
355void wxChoice::SetFocus()
356{
2d120f83 357 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
4bb6408c
JS
358}
359
bfc6fde4 360void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 361{
f97c9854
JS
362 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
363 bool managed = XtIsManaged((Widget) m_formWidget);
31528cd3 364
f97c9854
JS
365 if (managed)
366 XtUnmanageChild ((Widget) m_formWidget);
31528cd3 367
f97c9854 368 int actualWidth = width, actualHeight = height;
31528cd3 369
f97c9854
JS
370 if (width > -1)
371 {
fd304d98 372 size_t i;
f97c9854 373 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
374 XtVaSetValues ((Widget) m_widgetArray[i],
375 XmNwidth, actualWidth,
376 NULL);
f97c9854 377 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
2d120f83 378 NULL);
f97c9854
JS
379 }
380 if (height > -1)
381 {
fd304d98 382 size_t i;
f97c9854 383 for (i = 0; i < m_noStrings; i++)
ec75d791
MB
384 XtVaSetValues ((Widget) m_widgetArray[i],
385 XmNheight, actualHeight,
386 NULL);
f97c9854 387 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
2d120f83 388 NULL);
f97c9854 389 }
31528cd3 390
f97c9854
JS
391 if (managed)
392 XtManageChild ((Widget) m_formWidget);
393 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
31528cd3 394
bfc6fde4 395 wxControl::DoSetSize (x, y, width, height, sizeFlags);
4bb6408c
JS
396}
397
4bb6408c
JS
398void wxChoice::Command(wxCommandEvent & event)
399{
400 SetSelection (event.GetInt());
401 ProcessCommand (event);
402}
403
f9e02ac7 404void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
f97c9854
JS
405{
406 wxChoice *item = (wxChoice *) clientData;
407 if (item)
408 {
a4294b78 409 if (item->InSetValue())
f97c9854 410 return;
31528cd3 411
ec75d791
MB
412 int n = item->GetWidgets().Index(w);
413 if (n != wxNOT_FOUND)
f97c9854 414 {
ec75d791 415 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
55acd85e 416 event.SetEventObject(item);
ec75d791
MB
417 event.m_commandInt = n;
418 event.m_commandString = item->GetStrings().Item(n)->GetData();
419 if ( item->HasClientObjectData() )
420 event.SetClientObject( item->GetClientObject(n) );
421 else if ( item->HasClientUntypedData() )
422 event.SetClientData( item->GetClientData(n) );
f97c9854
JS
423 item->ProcessCommand (event);
424 }
425 }
426}
427
4b5f3fe6 428void wxChoice::ChangeFont(bool keepOriginalSize)
0d57be45 429{
321db4b6
JS
430 // Note that this causes the widget to be resized back
431 // to its original size! We therefore have to set the size
432 // back again. TODO: a better way in Motif?
da175b2c 433 if (m_font.Ok())
321db4b6
JS
434 {
435 int width, height, width1, height1;
436 GetSize(& width, & height);
31528cd3 437
da175b2c 438 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
ec75d791 439 XtVaSetValues ((Widget) m_formWidget, XmNfontList, fontList, NULL);
321db4b6 440 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
31528cd3 441
ec75d791
MB
442 for( size_t i = 0; i < m_noStrings; ++i )
443 XtVaSetValues( (Widget)m_widgetArray[i],
444 XmNfontList, fontList,
445 NULL );
446
321db4b6 447 GetSize(& width1, & height1);
4b5f3fe6 448 if (keepOriginalSize && (width != width1 || height != height1))
321db4b6
JS
449 {
450 SetSize(-1, -1, width, height);
451 }
452 }
0d57be45
JS
453}
454
455void wxChoice::ChangeBackgroundColour()
456{
321db4b6
JS
457 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
458 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
459 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
fd304d98 460 size_t i;
321db4b6 461 for (i = 0; i < m_noStrings; i++)
ec75d791 462 DoChangeBackgroundColour(m_widgetArray[i], m_backgroundColour);
0d57be45
JS
463}
464
465void wxChoice::ChangeForegroundColour()
466{
321db4b6
JS
467 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
468 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
469 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
fd304d98 470 size_t i;
321db4b6 471 for (i = 0; i < m_noStrings; i++)
ec75d791 472 DoChangeForegroundColour(m_widgetArray[i], m_foregroundColour);
0d57be45 473}
6adaedf0 474
6adaedf0
JS
475int wxChoice::GetCount() const
476{
ec75d791 477 return m_noStrings;
6adaedf0
JS
478}
479
ec75d791 480void wxChoice::DoSetItemClientData(int n, void* clientData)
6adaedf0 481{
ec75d791 482 m_clientDataDict.Set(n, (wxClientData*)clientData, FALSE);
6adaedf0
JS
483}
484
ec75d791 485void* wxChoice::DoGetItemClientData(int n) const
6adaedf0 486{
ec75d791 487 return (void*)m_clientDataDict.Get(n);
6adaedf0
JS
488}
489
ec75d791 490void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
6adaedf0 491{
ec75d791
MB
492 // don't delete, wxItemContainer does that for us
493 m_clientDataDict.Set(n, clientData, FALSE);
6adaedf0
JS
494}
495
ec75d791 496wxClientData* wxChoice::DoGetItemClientObject(int n) const
6adaedf0 497{
ec75d791 498 return m_clientDataDict.Get(n);
6adaedf0
JS
499}
500
ec75d791 501void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
6adaedf0 502{
ec75d791 503 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
6adaedf0
JS
504}
505
ec75d791 506wxSize wxChoice::GetItemsSize() const
6adaedf0 507{
ec75d791 508 int x, y, mx = 0, my = 0;
f6bcfd97 509
ec75d791
MB
510 // get my
511 GetTextExtent( "|", &x, &my );
f6bcfd97 512
ec75d791
MB
513 wxStringList::Node* curr = m_stringList.GetFirst();
514 while( curr )
515 {
516 GetTextExtent( curr->GetData(), &x, &y );
517 mx = wxMax( mx, x );
518 my = wxMax( my, y );
519 curr = curr->GetNext();
520 }
6adaedf0 521
ec75d791 522 return wxSize( mx, my );
6adaedf0
JS
523}
524
ec75d791 525wxSize wxChoice::DoGetBestSize() const
6adaedf0 526{
ec75d791
MB
527 wxSize items = GetItemsSize();
528 // FIXME arbitrary constants
529 return wxSize( ( items.x ? items.x + 50 : 120 ),
530 items.y + 15 );
6adaedf0 531}