Use wxXmString instead of XmString.
[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 // int i;
89 if (n > 0)
90 {
91 int i;
92 for (i = 0; i < n; i++)
93 Append (choices[i]);
94 }
95
96 /*
97 * Create button
98 */
99 Arg args[10];
100 Cardinal argcnt = 0;
101
102 XtSetArg (args[argcnt], XmNsubMenuId, (Widget) m_menuWidget); ++argcnt;
103 XtSetArg (args[argcnt], XmNmarginWidth, 0); ++argcnt;
104 XtSetArg (args[argcnt], XmNmarginHeight, 0); ++argcnt;
105 XtSetArg (args[argcnt], XmNpacking, XmPACK_TIGHT); ++argcnt;
106 m_buttonWidget = (WXWidget) XmCreateOptionMenu ((Widget) m_formWidget,
107 "choiceButton",
108 args, argcnt);
109
110 m_mainWidget = m_buttonWidget;
111
112 XtManageChild ((Widget) m_buttonWidget);
113
114 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
115 // Some time ago, I reported a problem with wxChoice-items under
116 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
117 // that I have found the code responsible for this behaviour.
118 #if XmVersion >= 1002
119 #if XmVersion < 2000
120 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
121 // in controls sample.
122 //
123 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
124 // XtUnmanageChild (optionLabel);
125 #endif
126 #endif
127
128 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
129
130 ChangeFont(FALSE);
131
132 AttachWidget (parent, m_buttonWidget, m_formWidget,
133 pos.x, pos.y, size.x, size.y);
134
135 ChangeBackgroundColour();
136
137 return TRUE;
138 }
139
140 wxChoice::~wxChoice()
141 {
142 // For some reason destroying the menuWidget
143 // can cause crashes on some machines. It will
144 // be deleted implicitly by deleting the parent form
145 // anyway.
146 // XtDestroyWidget (menuWidget);
147
148 if (GetMainWidget())
149 {
150 DetachWidget(GetMainWidget()); // Removes event handlers
151 DetachWidget(m_formWidget);
152
153 XtDestroyWidget((Widget) m_formWidget);
154 m_formWidget = (WXWidget) 0;
155
156 // Presumably the other widgets have been deleted now, via the form
157 m_mainWidget = (WXWidget) 0;
158 m_buttonWidget = (WXWidget) 0;
159 }
160 if ( HasClientObjectData() )
161 m_clientDataDict.DestroyData();
162 }
163
164 int wxChoice::DoAppend(const wxString& item)
165 {
166 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
167 #if USE_GADGETS
168 xmPushButtonGadgetClass, (Widget) m_menuWidget,
169 #else
170 xmPushButtonWidgetClass, (Widget) m_menuWidget,
171 #endif
172 NULL);
173
174 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
175
176 if (m_font.Ok())
177 XtVaSetValues (w,
178 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
179 NULL);
180
181 m_widgetArray.Add(w);
182
183 char mnem = wxFindMnemonic ((char*) (const char*) item);
184 if (mnem != 0)
185 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
186
187 XtAddCallback (w, XmNactivateCallback,
188 (XtCallbackProc) wxChoiceCallback,
189 (XtPointer) this);
190
191 if (m_noStrings == 0 && m_buttonWidget)
192 {
193 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
194 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
195 wxXmString text( item );
196 XtVaSetValues (label,
197 XmNlabelString, text(),
198 NULL);
199 }
200 m_stringList.Add(item);
201 m_noStrings ++;
202
203 return GetCount() - 1;
204 }
205
206 void wxChoice::Delete(int n)
207 {
208 Widget w = (Widget)m_widgetArray[n];
209 XtRemoveCallback(w, XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
210 (XtPointer)this);
211 m_stringList.DeleteNode(m_stringList.Item(n));
212 m_widgetArray.RemoveAt(size_t(n));
213 m_clientDataDict.Delete(n, HasClientObjectData());
214
215 XtDestroyWidget(w);
216 m_noStrings --;
217 }
218
219 void wxChoice::Clear()
220 {
221 m_stringList.Clear ();
222 int i;
223 for (i = 0; i < m_noStrings; i++)
224 {
225 XtRemoveCallback((Widget) m_widgetArray[i],
226 XmNactivateCallback, (XtCallbackProc)wxChoiceCallback,
227 (XtPointer)this);
228 XtUnmanageChild ((Widget) m_widgetArray[i]);
229 XtDestroyWidget ((Widget) m_widgetArray[i]);
230 }
231 m_widgetArray.Clear();
232 if (m_buttonWidget)
233 XtVaSetValues ((Widget) m_buttonWidget,
234 XmNmenuHistory, (Widget) NULL,
235 NULL);
236
237 if ( HasClientObjectData() )
238 m_clientDataDict.DestroyData();
239
240 m_noStrings = 0;
241 }
242
243 int wxChoice::GetSelection() const
244 {
245 XmString text;
246 char *s;
247 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
248 XtVaGetValues (label,
249 XmNlabelString, &text,
250 NULL);
251
252 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
253 {
254 int i = 0;
255 for (wxStringListNode* node = m_stringList.GetFirst ();
256 node; node = node->GetNext ())
257 {
258 if (strcmp(node->GetData(), s) == 0)
259 {
260 XmStringFree(text) ;
261 XtFree (s);
262 return i;
263 }
264 else
265 i++;
266 } // for()
267
268 XmStringFree(text) ;
269 XtFree (s);
270 return -1;
271 }
272 XmStringFree(text) ;
273 return -1;
274 }
275
276 void wxChoice::SetSelection(int n)
277 {
278 m_inSetValue = TRUE;
279
280 wxStringListNode *node = m_stringList.Item(n);
281 if (node)
282 {
283 #if 0
284 Dimension selectionWidth, selectionHeight;
285 #endif
286 wxXmString text( (char*)node->Data() );
287 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
288 // will remove "soon"
289 #if 0
290 XtVaGetValues ((Widget) m_widgetArray[n],
291 XmNwidth, &selectionWidth,
292 XmNheight, &selectionHeight,
293 NULL);
294 #endif
295 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
296 XtVaSetValues (label,
297 XmNlabelString, text(),
298 NULL);
299 #if 0
300 XtVaSetValues ((Widget) m_buttonWidget,
301 XmNwidth, selectionWidth, XmNheight, selectionHeight,
302 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
303 #endif
304 }
305 m_inSetValue = FALSE;
306 }
307
308 int wxChoice::FindString(const wxString& s) const
309 {
310 int i = 0;
311 for (wxStringListNode* node = m_stringList.GetFirst();
312 node; node = node->GetNext ())
313 {
314 if (s == node->GetData())
315 return i;
316
317 i++;
318 }
319
320 return -1;
321 }
322
323 wxString wxChoice::GetString(int n) const
324 {
325 wxStringListNode *node = m_stringList.Item(n);
326 if (node)
327 return node->GetData();
328 else
329 return wxEmptyString;
330 }
331
332 void wxChoice::SetColumns(int n)
333 {
334 if (n<1) n = 1 ;
335
336 short numColumns = n ;
337 Arg args[3];
338
339 XtSetArg(args[0], XmNnumColumns, numColumns);
340 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
341 XtSetValues((Widget) m_menuWidget,args,2) ;
342 }
343
344 int wxChoice::GetColumns(void) const
345 {
346 short numColumns ;
347
348 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
349 return numColumns ;
350 }
351
352 void wxChoice::SetFocus()
353 {
354 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
355 }
356
357 void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
358 {
359 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
360 bool managed = XtIsManaged((Widget) m_formWidget);
361
362 if (managed)
363 XtUnmanageChild ((Widget) m_formWidget);
364
365 int actualWidth = width, actualHeight = height;
366
367 if (width > -1)
368 {
369 int i;
370 for (i = 0; i < m_noStrings; i++)
371 XtVaSetValues ((Widget) m_widgetArray[i],
372 XmNwidth, actualWidth,
373 NULL);
374 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
375 NULL);
376 }
377 if (height > -1)
378 {
379 int i;
380 for (i = 0; i < m_noStrings; i++)
381 XtVaSetValues ((Widget) m_widgetArray[i],
382 XmNheight, actualHeight,
383 NULL);
384 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
385 NULL);
386 }
387
388 if (managed)
389 XtManageChild ((Widget) m_formWidget);
390 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
391
392 wxControl::DoSetSize (x, y, width, height, sizeFlags);
393 }
394
395 void wxChoice::Command(wxCommandEvent & event)
396 {
397 SetSelection (event.GetInt());
398 ProcessCommand (event);
399 }
400
401 void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
402 {
403 wxChoice *item = (wxChoice *) clientData;
404 if (item)
405 {
406 if (item->InSetValue())
407 return;
408
409 int n = item->GetWidgets().Index(w);
410 if (n != wxNOT_FOUND)
411 {
412 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
413 event.SetEventObject(item);
414 event.m_commandInt = n;
415 event.m_commandString = item->GetStrings().Item(n)->GetData();
416 if ( item->HasClientObjectData() )
417 event.SetClientObject( item->GetClientObject(n) );
418 else if ( item->HasClientUntypedData() )
419 event.SetClientData( item->GetClientData(n) );
420 item->ProcessCommand (event);
421 }
422 }
423 }
424
425 void wxChoice::ChangeFont(bool keepOriginalSize)
426 {
427 // Note that this causes the widget to be resized back
428 // to its original size! We therefore have to set the size
429 // back again. TODO: a better way in Motif?
430 if (m_font.Ok())
431 {
432 int width, height, width1, height1;
433 GetSize(& width, & height);
434
435 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
436 XtVaSetValues ((Widget) m_formWidget, XmNfontList, fontList, NULL);
437 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
438
439 for( size_t i = 0; i < m_noStrings; ++i )
440 XtVaSetValues( (Widget)m_widgetArray[i],
441 XmNfontList, fontList,
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 int 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 int 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 }