Added new __WXMOTIF20__ macro, which is true if the Motif version
[wxWidgets.git] / src / motif / choice.cpp
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
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #define XtParent XTPARENT
19 #endif
20
21 #include "wx/defs.h"
22
23 #include "wx/choice.h"
24 #include "wx/utils.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/Xm.h>
30 #include <Xm/PushBG.h>
31 #include <Xm/PushB.h>
32 #include <Xm/RowColumn.h>
33 #ifdef __VMS__
34 #pragma message enable nosimpint
35 #endif
36
37 #include "wx/motif/private.h"
38
39 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
40
41 void wxChoiceCallback (Widget w, XtPointer clientData,
42 XtPointer ptr);
43
44 wxChoice::wxChoice()
45 {
46 Init();
47 }
48
49 void wxChoice::Init()
50 {
51 InitBase();
52
53 m_noStrings = 0;
54 m_buttonWidget = (WXWidget) 0;
55 m_menuWidget = (WXWidget) 0;
56 m_formWidget = (WXWidget) 0;
57 }
58
59 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
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)
66 {
67 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
68 return FALSE;
69
70 Widget parentWidget = (Widget) parent->GetClientWidget();
71
72 m_formWidget = (WXWidget) XtVaCreateManagedWidget(name.c_str(),
73 xmRowColumnWidgetClass, parentWidget,
74 XmNmarginHeight, 0,
75 XmNmarginWidth, 0,
76 XmNpacking, XmPACK_TIGHT,
77 XmNorientation, XmHORIZONTAL,
78 NULL);
79
80 XtVaSetValues ((Widget) m_formWidget, XmNspacing, 0, NULL);
81
82 /*
83 * Create the popup menu
84 */
85 m_menuWidget = (WXWidget) XmCreatePulldownMenu ((Widget) m_formWidget,
86 "choiceMenu", NULL, 0);
87
88 if (n > 0)
89 {
90 int i;
91 for (i = 0; i < n; i++)
92 Append (choices[i]);
93 }
94
95 /*
96 * Create button
97 */
98 Arg args[10];
99 Cardinal argcnt = 0;
100
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);
108
109 m_mainWidget = m_buttonWidget;
110
111 XtManageChild ((Widget) m_buttonWidget);
112
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
119 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
120 // in controls sample.
121 //
122 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
123 // XtUnmanageChild (optionLabel);
124 #endif
125 #endif
126
127 wxSize bestSize = GetBestSize();
128 if( size.x > 0 ) bestSize.x = size.x;
129 if( size.y > 0 ) bestSize.y = size.y;
130
131 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
132
133 ChangeFont(FALSE);
134
135 AttachWidget (parent, m_buttonWidget, m_formWidget,
136 pos.x, pos.y, bestSize.x, bestSize.y);
137
138 ChangeBackgroundColour();
139
140 return TRUE;
141 }
142
143 wxChoice::~wxChoice()
144 {
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);
150
151 if (GetMainWidget())
152 {
153 DetachWidget(GetMainWidget()); // Removes event handlers
154 DetachWidget(m_formWidget);
155
156 XtDestroyWidget((Widget) m_formWidget);
157 m_formWidget = (WXWidget) 0;
158
159 // Presumably the other widgets have been deleted now, via the form
160 m_mainWidget = (WXWidget) 0;
161 m_buttonWidget = (WXWidget) 0;
162 }
163 if ( HasClientObjectData() )
164 m_clientDataDict.DestroyData();
165 }
166
167 int wxChoice::DoAppend(const wxString& item)
168 {
169 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
170 #if USE_GADGETS
171 xmPushButtonGadgetClass, (Widget) m_menuWidget,
172 #else
173 xmPushButtonWidgetClass, (Widget) m_menuWidget,
174 #endif
175 NULL);
176
177 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
178
179 if( m_font.Ok() )
180 wxDoChangeFont( w, m_font );
181
182 m_widgetArray.Add(w);
183
184 char mnem = wxFindMnemonic ((char*) (const char*) item);
185 if (mnem != 0)
186 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
187
188 XtAddCallback (w, XmNactivateCallback,
189 (XtCallbackProc) wxChoiceCallback,
190 (XtPointer) this);
191
192 if (m_noStrings == 0 && m_buttonWidget)
193 {
194 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
195 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
196 wxXmString text( item );
197 XtVaSetValues (label,
198 XmNlabelString, text(),
199 NULL);
200 }
201 m_stringList.Add(item);
202 m_noStrings ++;
203
204 return GetCount() - 1;
205 }
206
207 void wxChoice::Delete(int n)
208 {
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());
215
216 XtDestroyWidget(w);
217 m_noStrings --;
218 }
219
220 void wxChoice::Clear()
221 {
222 m_stringList.Clear ();
223 size_t i;
224 for (i = 0; i < m_noStrings; i++)
225 {
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]);
231 }
232 m_widgetArray.Clear();
233 if (m_buttonWidget)
234 XtVaSetValues ((Widget) m_buttonWidget,
235 XmNmenuHistory, (Widget) NULL,
236 NULL);
237
238 if ( HasClientObjectData() )
239 m_clientDataDict.DestroyData();
240
241 m_noStrings = 0;
242 }
243
244 int wxChoice::GetSelection() const
245 {
246 XmString text;
247 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
248 XtVaGetValues (label,
249 XmNlabelString, &text,
250 NULL);
251 wxXmString freeMe(text);
252 wxString s = wxXmStringToString( text );
253
254 if (!s.IsEmpty())
255 {
256 int i = 0;
257 for (wxStringListNode* node = m_stringList.GetFirst ();
258 node; node = node->GetNext ())
259 {
260 if (wxStrcmp(node->GetData(), s.c_str()) == 0)
261 {
262 return i;
263 }
264 else
265 i++;
266 } // for()
267
268 return -1;
269 }
270 return -1;
271 }
272
273 void wxChoice::SetSelection(int n)
274 {
275 m_inSetValue = TRUE;
276
277 wxStringListNode *node = m_stringList.Item(n);
278 if (node)
279 {
280 #if 0
281 Dimension selectionWidth, selectionHeight;
282 #endif
283 wxXmString text( node->GetData() );
284 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
285 // will remove "soon"
286 #if 0
287 XtVaGetValues ((Widget) m_widgetArray[n],
288 XmNwidth, &selectionWidth,
289 XmNheight, &selectionHeight,
290 NULL);
291 #endif
292 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
293 XtVaSetValues (label,
294 XmNlabelString, text(),
295 NULL);
296 #if 0
297 XtVaSetValues ((Widget) m_buttonWidget,
298 XmNwidth, selectionWidth, XmNheight, selectionHeight,
299 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
300 #endif
301 }
302 m_inSetValue = FALSE;
303 }
304
305 int wxChoice::FindString(const wxString& s) const
306 {
307 int i = 0;
308 for (wxStringListNode* node = m_stringList.GetFirst();
309 node; node = node->GetNext ())
310 {
311 if (s == node->GetData())
312 return i;
313
314 i++;
315 }
316
317 return -1;
318 }
319
320 wxString wxChoice::GetString(int n) const
321 {
322 wxStringListNode *node = m_stringList.Item(n);
323 if (node)
324 return node->GetData();
325 else
326 return wxEmptyString;
327 }
328
329 void wxChoice::SetColumns(int n)
330 {
331 if (n<1) n = 1 ;
332
333 short numColumns = n ;
334 Arg args[3];
335
336 XtSetArg(args[0], XmNnumColumns, numColumns);
337 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
338 XtSetValues((Widget) m_menuWidget,args,2) ;
339 }
340
341 int wxChoice::GetColumns(void) const
342 {
343 short numColumns ;
344
345 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
346 return numColumns ;
347 }
348
349 void wxChoice::SetFocus()
350 {
351 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
352 }
353
354 void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
355 {
356 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
357 bool managed = XtIsManaged((Widget) m_formWidget);
358
359 if (managed)
360 XtUnmanageChild ((Widget) m_formWidget);
361
362 int actualWidth = width, actualHeight = height;
363
364 if (width > -1)
365 {
366 size_t i;
367 for (i = 0; i < m_noStrings; i++)
368 XtVaSetValues ((Widget) m_widgetArray[i],
369 XmNwidth, actualWidth,
370 NULL);
371 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
372 NULL);
373 }
374 if (height > -1)
375 {
376 size_t i;
377 for (i = 0; i < m_noStrings; i++)
378 XtVaSetValues ((Widget) m_widgetArray[i],
379 XmNheight, actualHeight,
380 NULL);
381 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
382 NULL);
383 }
384
385 if (managed)
386 XtManageChild ((Widget) m_formWidget);
387 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
388
389 wxControl::DoSetSize (x, y, width, height, sizeFlags);
390 }
391
392 void wxChoice::Command(wxCommandEvent & event)
393 {
394 SetSelection (event.GetInt());
395 ProcessCommand (event);
396 }
397
398 void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
399 {
400 wxChoice *item = (wxChoice *) clientData;
401 if (item)
402 {
403 if (item->InSetValue())
404 return;
405
406 int n = item->GetWidgets().Index(w);
407 if (n != wxNOT_FOUND)
408 {
409 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
410 event.SetEventObject(item);
411 event.m_commandInt = n;
412 event.m_commandString = item->GetStrings().Item(n)->GetData();
413 if ( item->HasClientObjectData() )
414 event.SetClientObject( item->GetClientObject(n) );
415 else if ( item->HasClientUntypedData() )
416 event.SetClientData( item->GetClientData(n) );
417 item->ProcessCommand (event);
418 }
419 }
420 }
421
422 void wxChoice::ChangeFont(bool keepOriginalSize)
423 {
424 // Note that this causes the widget to be resized back
425 // to its original size! We therefore have to set the size
426 // back again. TODO: a better way in Motif?
427 if (m_font.Ok())
428 {
429 int width, height, width1, height1;
430 GetSize(& width, & height);
431
432 WXFontType fontType =
433 m_font.GetFontType(XtDisplay((Widget) m_mainWidget));
434 WXString fontTag = wxFont::GetFontTag();
435
436 XtVaSetValues ((Widget) m_formWidget, fontTag, fontType, NULL);
437 XtVaSetValues ((Widget) m_buttonWidget, fontTag, fontType, NULL);
438
439 for( size_t i = 0; i < m_noStrings; ++i )
440 XtVaSetValues( (Widget)m_widgetArray[i],
441 fontTag, fontType,
442 NULL );
443
444 GetSize(& width1, & height1);
445 if (keepOriginalSize && (width != width1 || height != height1))
446 {
447 SetSize(-1, -1, width, height);
448 }
449 }
450 }
451
452 void wxChoice::ChangeBackgroundColour()
453 {
454 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
455 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
456 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
457 size_t i;
458 for (i = 0; i < m_noStrings; i++)
459 DoChangeBackgroundColour(m_widgetArray[i], m_backgroundColour);
460 }
461
462 void wxChoice::ChangeForegroundColour()
463 {
464 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
465 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
466 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
467 size_t i;
468 for (i = 0; i < m_noStrings; i++)
469 DoChangeForegroundColour(m_widgetArray[i], m_foregroundColour);
470 }
471
472 int wxChoice::GetCount() const
473 {
474 return m_noStrings;
475 }
476
477 void wxChoice::DoSetItemClientData(int n, void* clientData)
478 {
479 m_clientDataDict.Set(n, (wxClientData*)clientData, FALSE);
480 }
481
482 void* wxChoice::DoGetItemClientData(int n) const
483 {
484 return (void*)m_clientDataDict.Get(n);
485 }
486
487 void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
488 {
489 // don't delete, wxItemContainer does that for us
490 m_clientDataDict.Set(n, clientData, FALSE);
491 }
492
493 wxClientData* wxChoice::DoGetItemClientObject(int n) const
494 {
495 return m_clientDataDict.Get(n);
496 }
497
498 void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
499 {
500 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
501 }
502
503 wxSize wxChoice::GetItemsSize() const
504 {
505 int x, y, mx = 0, my = 0;
506
507 // get my
508 GetTextExtent( "|", &x, &my );
509
510 wxStringList::Node* curr = m_stringList.GetFirst();
511 while( curr )
512 {
513 GetTextExtent( curr->GetData(), &x, &y );
514 mx = wxMax( mx, x );
515 my = wxMax( my, y );
516 curr = curr->GetNext();
517 }
518
519 return wxSize( mx, my );
520 }
521
522 wxSize wxChoice::DoGetBestSize() const
523 {
524 wxSize items = GetItemsSize();
525 // FIXME arbitrary constants
526 return wxSize( ( items.x ? items.x + 50 : 120 ),
527 items.y + 15 );
528 }