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