Implemented DoGetBestSize for wxListBox, (native) wxComboBox and
[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 char *s;
248 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
249 XtVaGetValues (label,
250 XmNlabelString, &text,
251 NULL);
252
253 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
254 {
255 int i = 0;
256 for (wxStringListNode* node = m_stringList.GetFirst ();
257 node; node = node->GetNext ())
258 {
259 if (strcmp(node->GetData(), s) == 0)
260 {
261 XmStringFree(text) ;
262 XtFree (s);
263 return i;
264 }
265 else
266 i++;
267 } // for()
268
269 XmStringFree(text) ;
270 XtFree (s);
271 return -1;
272 }
273 XmStringFree(text) ;
274 return -1;
275 }
276
277 void wxChoice::SetSelection(int n)
278 {
279 m_inSetValue = TRUE;
280
281 wxStringListNode *node = m_stringList.Item(n);
282 if (node)
283 {
284 #if 0
285 Dimension selectionWidth, selectionHeight;
286 #endif
287 wxXmString text( node->GetData() );
288 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
289 // will remove "soon"
290 #if 0
291 XtVaGetValues ((Widget) m_widgetArray[n],
292 XmNwidth, &selectionWidth,
293 XmNheight, &selectionHeight,
294 NULL);
295 #endif
296 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
297 XtVaSetValues (label,
298 XmNlabelString, text(),
299 NULL);
300 #if 0
301 XtVaSetValues ((Widget) m_buttonWidget,
302 XmNwidth, selectionWidth, XmNheight, selectionHeight,
303 XmNmenuHistory, (Widget) m_widgetArray[n], NULL);
304 #endif
305 }
306 m_inSetValue = FALSE;
307 }
308
309 int wxChoice::FindString(const wxString& s) const
310 {
311 int i = 0;
312 for (wxStringListNode* node = m_stringList.GetFirst();
313 node; node = node->GetNext ())
314 {
315 if (s == node->GetData())
316 return i;
317
318 i++;
319 }
320
321 return -1;
322 }
323
324 wxString wxChoice::GetString(int n) const
325 {
326 wxStringListNode *node = m_stringList.Item(n);
327 if (node)
328 return node->GetData();
329 else
330 return wxEmptyString;
331 }
332
333 void wxChoice::SetColumns(int n)
334 {
335 if (n<1) n = 1 ;
336
337 short numColumns = n ;
338 Arg args[3];
339
340 XtSetArg(args[0], XmNnumColumns, numColumns);
341 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
342 XtSetValues((Widget) m_menuWidget,args,2) ;
343 }
344
345 int wxChoice::GetColumns(void) const
346 {
347 short numColumns ;
348
349 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
350 return numColumns ;
351 }
352
353 void wxChoice::SetFocus()
354 {
355 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
356 }
357
358 void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
359 {
360 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
361 bool managed = XtIsManaged((Widget) m_formWidget);
362
363 if (managed)
364 XtUnmanageChild ((Widget) m_formWidget);
365
366 int actualWidth = width, actualHeight = height;
367
368 if (width > -1)
369 {
370 size_t i;
371 for (i = 0; i < m_noStrings; i++)
372 XtVaSetValues ((Widget) m_widgetArray[i],
373 XmNwidth, actualWidth,
374 NULL);
375 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
376 NULL);
377 }
378 if (height > -1)
379 {
380 size_t i;
381 for (i = 0; i < m_noStrings; i++)
382 XtVaSetValues ((Widget) m_widgetArray[i],
383 XmNheight, actualHeight,
384 NULL);
385 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
386 NULL);
387 }
388
389 if (managed)
390 XtManageChild ((Widget) m_formWidget);
391 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
392
393 wxControl::DoSetSize (x, y, width, height, sizeFlags);
394 }
395
396 void wxChoice::Command(wxCommandEvent & event)
397 {
398 SetSelection (event.GetInt());
399 ProcessCommand (event);
400 }
401
402 void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
403 {
404 wxChoice *item = (wxChoice *) clientData;
405 if (item)
406 {
407 if (item->InSetValue())
408 return;
409
410 int n = item->GetWidgets().Index(w);
411 if (n != wxNOT_FOUND)
412 {
413 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
414 event.SetEventObject(item);
415 event.m_commandInt = n;
416 event.m_commandString = item->GetStrings().Item(n)->GetData();
417 if ( item->HasClientObjectData() )
418 event.SetClientObject( item->GetClientObject(n) );
419 else if ( item->HasClientUntypedData() )
420 event.SetClientData( item->GetClientData(n) );
421 item->ProcessCommand (event);
422 }
423 }
424 }
425
426 void wxChoice::ChangeFont(bool keepOriginalSize)
427 {
428 // Note that this causes the widget to be resized back
429 // to its original size! We therefore have to set the size
430 // back again. TODO: a better way in Motif?
431 if (m_font.Ok())
432 {
433 int width, height, width1, height1;
434 GetSize(& width, & height);
435
436 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
437 XtVaSetValues ((Widget) m_formWidget, XmNfontList, fontList, NULL);
438 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
439
440 for( size_t i = 0; i < m_noStrings; ++i )
441 XtVaSetValues( (Widget)m_widgetArray[i],
442 XmNfontList, fontList,
443 NULL );
444
445 GetSize(& width1, & height1);
446 if (keepOriginalSize && (width != width1 || height != height1))
447 {
448 SetSize(-1, -1, width, height);
449 }
450 }
451 }
452
453 void wxChoice::ChangeBackgroundColour()
454 {
455 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
456 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
457 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
458 size_t i;
459 for (i = 0; i < m_noStrings; i++)
460 DoChangeBackgroundColour(m_widgetArray[i], m_backgroundColour);
461 }
462
463 void wxChoice::ChangeForegroundColour()
464 {
465 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
466 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
467 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
468 size_t i;
469 for (i = 0; i < m_noStrings; i++)
470 DoChangeForegroundColour(m_widgetArray[i], m_foregroundColour);
471 }
472
473 int wxChoice::GetCount() const
474 {
475 return m_noStrings;
476 }
477
478 void wxChoice::DoSetItemClientData(int n, void* clientData)
479 {
480 m_clientDataDict.Set(n, (wxClientData*)clientData, FALSE);
481 }
482
483 void* wxChoice::DoGetItemClientData(int n) const
484 {
485 return (void*)m_clientDataDict.Get(n);
486 }
487
488 void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
489 {
490 // don't delete, wxItemContainer does that for us
491 m_clientDataDict.Set(n, clientData, FALSE);
492 }
493
494 wxClientData* wxChoice::DoGetItemClientObject(int n) const
495 {
496 return m_clientDataDict.Get(n);
497 }
498
499 void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
500 {
501 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
502 }
503
504 wxSize wxChoice::GetItemsSize() const
505 {
506 int x, y, mx = 0, my = 0;
507
508 // get my
509 GetTextExtent( "|", &x, &my );
510
511 wxStringList::Node* curr = m_stringList.GetFirst();
512 while( curr )
513 {
514 GetTextExtent( curr->GetData(), &x, &y );
515 mx = wxMax( mx, x );
516 my = wxMax( my, y );
517 curr = curr->GetNext();
518 }
519
520 return wxSize( mx, my );
521 }
522
523 wxSize wxChoice::DoGetBestSize() const
524 {
525 wxSize items = GetItemsSize();
526 // FIXME arbitrary constants
527 return wxSize( ( items.x ? items.x + 50 : 120 ),
528 items.y + 15 );
529 }