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