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