]> git.saurik.com Git - wxWidgets.git/blob - src/motif/choice.cpp
Added "set new icon" menu item to taskbar sample; updated some makefiles
[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 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/choice.h"
23 #include "wx/utils.h"
24
25 #ifdef __VMS__
26 #pragma message disable nosimpint
27 #endif
28 #include <Xm/Xm.h>
29 #include <Xm/PushBG.h>
30 #include <Xm/PushB.h>
31 #include <Xm/RowColumn.h>
32 #ifdef __VMS__
33 #pragma message enable nosimpint
34 #endif
35
36 #include "wx/motif/private.h"
37
38 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
39
40 void wxChoiceCallback (Widget w, XtPointer clientData,
41 XtPointer ptr);
42
43 wxChoice::wxChoice()
44 {
45 m_noStrings = 0;
46 m_buttonWidget = (WXWidget) 0;
47 m_menuWidget = (WXWidget) 0;
48 m_widgetList = (WXWidget*) 0;
49 m_formWidget = (WXWidget) 0;
50 }
51
52 bool wxChoice::Create(wxWindow *parent, wxWindowID id,
53 const wxPoint& pos,
54 const wxSize& size,
55 int n, const wxString choices[],
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 SetName(name);
61 SetValidator(validator);
62 m_noStrings = 0; // Starts off with none, incremented in Append
63 m_windowStyle = style;
64 m_buttonWidget = (WXWidget) 0;
65 m_menuWidget = (WXWidget) 0;
66 m_widgetList = (WXWidget*) 0;
67 m_formWidget = (WXWidget) 0;
68
69 if (parent) parent->AddChild(this);
70
71 if ( id == -1 )
72 m_windowId = (int)NewControlId();
73 else
74 m_windowId = id;
75
76 m_backgroundColour = parent->GetBackgroundColour();
77 m_foregroundColour = parent->GetForegroundColour();
78 m_font = parent->GetFont();
79
80 Widget parentWidget = (Widget) parent->GetClientWidget();
81
82 m_formWidget = (WXWidget) XtVaCreateManagedWidget(name.c_str(),
83 xmRowColumnWidgetClass, parentWidget,
84 XmNmarginHeight, 0,
85 XmNmarginWidth, 0,
86 XmNpacking, XmPACK_TIGHT,
87 XmNorientation, XmHORIZONTAL,
88 NULL);
89
90 XtVaSetValues ((Widget) m_formWidget, XmNspacing, 0, NULL);
91
92 /*
93 * Create the popup menu
94 */
95 m_menuWidget = (WXWidget) XmCreatePulldownMenu ((Widget) m_formWidget, "choiceMenu", NULL, 0);
96
97 // int i;
98 if (n > 0)
99 {
100 int i;
101 for (i = 0; i < n; i++)
102 Append (choices[i]);
103 }
104
105 /*
106 * Create button
107 */
108 Arg args[10];
109 Cardinal argcnt = 0;
110
111 XtSetArg (args[argcnt], XmNsubMenuId, (Widget) m_menuWidget);
112 argcnt++;
113 XtSetArg (args[argcnt], XmNmarginWidth, 0);
114 argcnt++;
115 XtSetArg (args[argcnt], XmNmarginHeight, 0);
116 argcnt++;
117 XtSetArg (args[argcnt], XmNpacking, XmPACK_TIGHT);
118 argcnt++;
119 m_buttonWidget = (WXWidget) XmCreateOptionMenu ((Widget) m_formWidget, "choiceButton", args, argcnt);
120
121 m_mainWidget = m_buttonWidget;
122
123 XtManageChild ((Widget) m_buttonWidget);
124
125 // New code from Roland Haenel (roland_haenel@ac.cybercity.de)
126 // Some time ago, I reported a problem with wxChoice-items under
127 // Linux and Motif 2.0 (they caused sporadic GPFs). Now it seems
128 // that I have found the code responsible for this behaviour.
129 #if XmVersion >= 1002
130 #if XmVersion < 2000
131 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
132 // in controls sample.
133 //
134 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
135 // XtUnmanageChild (optionLabel);
136 #endif
137 #endif
138
139 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
140
141 ChangeFont(FALSE);
142
143 AttachWidget (parent, m_buttonWidget, m_formWidget, pos.x, pos.y, size.x, size.y);
144
145 ChangeBackgroundColour();
146
147 return TRUE;
148 }
149
150 wxChoice::~wxChoice()
151 {
152 // For some reason destroying the menuWidget
153 // can cause crashes on some machines. It will
154 // be deleted implicitly by deleting the parent form
155 // anyway.
156 // XtDestroyWidget (menuWidget);
157 if (m_widgetList)
158 delete[] m_widgetList;
159
160 if (GetMainWidget())
161 {
162 DetachWidget(GetMainWidget()); // Removes event handlers
163 DetachWidget(m_formWidget);
164
165 XtDestroyWidget((Widget) m_formWidget);
166 m_formWidget = (WXWidget) 0;
167
168 // Presumably the other widgets have been deleted now, via the form
169 m_mainWidget = (WXWidget) 0;
170 m_buttonWidget = (WXWidget) 0;
171 }
172 }
173
174 void wxChoice::Append(const wxString& item)
175 {
176 Widget w = XtVaCreateManagedWidget (wxStripMenuCodes(item),
177 #if USE_GADGETS
178 xmPushButtonGadgetClass, (Widget) m_menuWidget,
179 #else
180 xmPushButtonWidgetClass, (Widget) m_menuWidget,
181 #endif
182 NULL);
183
184 DoChangeBackgroundColour((WXWidget) w, m_backgroundColour);
185
186 if (m_font.Ok())
187 XtVaSetValues (w,
188 XmNfontList, (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_formWidget)),
189 NULL);
190
191 WXWidget *new_widgetList = new WXWidget[m_noStrings + 1];
192 int i;
193 if (m_widgetList)
194 for (i = 0; i < m_noStrings; i++)
195 new_widgetList[i] = m_widgetList[i];
196
197 new_widgetList[m_noStrings] = (WXWidget) w;
198
199 if (m_widgetList)
200 delete[] m_widgetList;
201 m_widgetList = new_widgetList;
202
203 char mnem = wxFindMnemonic ((char*) (const char*) item);
204 if (mnem != 0)
205 XtVaSetValues (w, XmNmnemonic, mnem, NULL);
206
207 XtAddCallback (w, XmNactivateCallback, (XtCallbackProc) wxChoiceCallback, (XtPointer) this);
208
209 if (m_noStrings == 0 && m_buttonWidget)
210 {
211 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, w, NULL);
212 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
213 XmString text = XmStringCreateSimple ((char*) (const char*) item);
214 XtVaSetValues (label,
215 XmNlabelString, text,
216 NULL);
217 XmStringFree (text);
218 }
219 wxNode *node = m_stringList.Add (item);
220 XtVaSetValues (w, XmNuserData, node->Data (), NULL);
221
222 if (m_noStrings == 0)
223 m_clientList.Append((wxObject*) NULL);
224 else
225 m_clientList.Insert( m_clientList.Item(m_noStrings-1),
226 (wxObject*) NULL );
227 m_noStrings ++;
228 }
229
230 void wxChoice::Delete(int WXUNUSED(n))
231 {
232 wxFAIL_MSG( "Sorry, wxChoice::Delete isn't implemented yet. Maybe you'd like to volunteer? :-)" );
233
234 // What should we do -- remove the callback for this button widget,
235 // delete the m_stringList entry, delete the button widget, construct a new widget list
236 // (see Append)
237
238 // TODO
239 m_noStrings --;
240 }
241
242 void wxChoice::Clear()
243 {
244 m_stringList.Clear ();
245 int i;
246 for (i = 0; i < m_noStrings; i++)
247 {
248 XtUnmanageChild ((Widget) m_widgetList[i]);
249 XtDestroyWidget ((Widget) m_widgetList[i]);
250 }
251 if (m_noStrings)
252 delete[] m_widgetList;
253 m_widgetList = (WXWidget*) NULL;
254 if (m_buttonWidget)
255 XtVaSetValues ((Widget) m_buttonWidget, XmNmenuHistory, (Widget) NULL, NULL);
256
257 if ( HasClientObjectData() )
258 {
259 // destroy the data (due to Robert's idea of using wxList<wxObject>
260 // and not wxList<wxClientData> we can't just say
261 // m_clientList.DeleteContents(TRUE) - this would crash!
262 wxNode *node = m_clientList.First();
263 while ( node )
264 {
265 delete (wxClientData *)node->Data();
266 node = node->Next();
267 }
268 }
269 m_clientList.Clear();
270
271 m_noStrings = 0;
272 }
273
274 int wxChoice::GetSelection() const
275 {
276 XmString text;
277 char *s;
278 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
279 XtVaGetValues (label,
280 XmNlabelString, &text,
281 NULL);
282
283 if (XmStringGetLtoR (text, XmSTRING_DEFAULT_CHARSET, &s))
284 {
285 int i = 0;
286 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
287 {
288 char *s1 = (char *) node->Data ();
289 if (s1 == s || strcmp (s1, s) == 0)
290 {
291 XmStringFree(text) ;
292 XtFree (s);
293 return i;
294 }
295 else
296 i++;
297 } // for()
298
299 XmStringFree(text) ;
300 XtFree (s);
301 return -1;
302 }
303 XmStringFree(text) ;
304 return -1;
305 }
306
307 void wxChoice::SetSelection(int n)
308 {
309 m_inSetValue = TRUE;
310
311 wxNode *node = m_stringList.Nth (n);
312 if (node)
313 {
314 Dimension selectionWidth, selectionHeight;
315
316 char *s = (char *) node->Data ();
317 XmString text = XmStringCreateSimple (s);
318 XtVaGetValues ((Widget) m_widgetList[n], XmNwidth, &selectionWidth, XmNheight, &selectionHeight, NULL);
319 Widget label = XmOptionButtonGadget ((Widget) m_buttonWidget);
320 XtVaSetValues (label,
321 XmNlabelString, text,
322 NULL);
323 XmStringFree (text);
324 XtVaSetValues ((Widget) m_buttonWidget,
325 XmNwidth, selectionWidth, XmNheight, selectionHeight,
326 XmNmenuHistory, (Widget) m_widgetList[n], NULL);
327 }
328 m_inSetValue = FALSE;
329 }
330
331 int wxChoice::FindString(const wxString& s) const
332 {
333 int i = 0;
334 for (wxNode * node = m_stringList.First (); node; node = node->Next ())
335 {
336 char *s1 = (char *) node->Data ();
337 if (s == s1)
338 {
339 return i;
340 }
341 else
342 i++;
343 }
344 return -1;
345 }
346
347 wxString wxChoice::GetString(int n) const
348 {
349 wxNode *node = m_stringList.Nth (n);
350 if (node)
351 return wxString((char *) node->Data ());
352 else
353 return wxEmptyString;
354 }
355
356 void wxChoice::SetColumns(int n)
357 {
358 if (n<1) n = 1 ;
359
360 short numColumns = n ;
361 Arg args[3];
362
363 XtSetArg(args[0], XmNnumColumns, numColumns);
364 XtSetArg(args[1], XmNpacking, XmPACK_COLUMN);
365 XtSetValues((Widget) m_menuWidget,args,2) ;
366 }
367
368 int wxChoice::GetColumns(void) const
369 {
370 short numColumns ;
371
372 XtVaGetValues((Widget) m_menuWidget,XmNnumColumns,&numColumns,NULL) ;
373 return numColumns ;
374 }
375
376 void wxChoice::SetFocus()
377 {
378 XmProcessTraversal(XtParent((Widget)m_mainWidget), XmTRAVERSE_CURRENT);
379 }
380
381 void wxChoice::DoSetSize(int x, int y, int width, int height, int sizeFlags)
382 {
383 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
384 bool managed = XtIsManaged((Widget) m_formWidget);
385
386 if (managed)
387 XtUnmanageChild ((Widget) m_formWidget);
388
389 int actualWidth = width, actualHeight = height;
390
391 if (width > -1)
392 {
393 int i;
394 for (i = 0; i < m_noStrings; i++)
395 XtVaSetValues ((Widget) m_widgetList[i], XmNwidth, actualWidth, NULL);
396 XtVaSetValues ((Widget) m_buttonWidget, XmNwidth, actualWidth,
397 NULL);
398 }
399 if (height > -1)
400 {
401 int i;
402 for (i = 0; i < m_noStrings; i++)
403 XtVaSetValues ((Widget) m_widgetList[i], XmNheight, actualHeight, NULL);
404 XtVaSetValues ((Widget) m_buttonWidget, XmNheight, actualHeight,
405 NULL);
406 }
407
408 if (managed)
409 XtManageChild ((Widget) m_formWidget);
410 XtVaSetValues((Widget) m_formWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
411
412 wxControl::DoSetSize (x, y, width, height, sizeFlags);
413 }
414
415 wxString wxChoice::GetStringSelection () const
416 {
417 int sel = GetSelection ();
418 if (sel > -1)
419 return wxString(this->GetString (sel));
420 else
421 return wxEmptyString;
422 }
423
424 bool wxChoice::SetStringSelection (const wxString& s)
425 {
426 int sel = FindString (s);
427 if (sel > -1)
428 {
429 SetSelection (sel);
430 return TRUE;
431 }
432 else
433 return FALSE;
434 }
435
436 void wxChoice::Command(wxCommandEvent & event)
437 {
438 SetSelection (event.GetInt());
439 ProcessCommand (event);
440 }
441
442 void wxChoiceCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
443 {
444 wxChoice *item = (wxChoice *) clientData;
445 if (item)
446 {
447 if (item->InSetValue())
448 return;
449
450 char *s = NULL;
451 XtVaGetValues (w, XmNuserData, &s, NULL);
452 if (s)
453 {
454 wxCommandEvent event (wxEVT_COMMAND_CHOICE_SELECTED, item->GetId());
455 event.SetEventObject(item);
456 event.m_commandInt = item->FindString (s);
457 // event.m_commandString = s;
458 item->ProcessCommand (event);
459 }
460 }
461 }
462
463 void wxChoice::ChangeFont(bool keepOriginalSize)
464 {
465 // Note that this causes the widget to be resized back
466 // to its original size! We therefore have to set the size
467 // back again. TODO: a better way in Motif?
468 if (m_font.Ok())
469 {
470 int width, height, width1, height1;
471 GetSize(& width, & height);
472
473 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay((Widget) m_mainWidget));
474 XtVaSetValues ((Widget) m_mainWidget, XmNfontList, fontList, NULL);
475 XtVaSetValues ((Widget) m_buttonWidget, XmNfontList, fontList, NULL);
476
477 /* TODO: why does this cause a crash in XtWidgetToApplicationContext?
478 int i;
479 for (i = 0; i < m_noStrings; i++)
480 XtVaSetValues ((Widget) m_widgetList[i], XmNfontList, fontList, NULL);
481 */
482 GetSize(& width1, & height1);
483 if (keepOriginalSize && (width != width1 || height != height1))
484 {
485 SetSize(-1, -1, width, height);
486 }
487 }
488 }
489
490 void wxChoice::ChangeBackgroundColour()
491 {
492 DoChangeBackgroundColour(m_formWidget, m_backgroundColour);
493 DoChangeBackgroundColour(m_buttonWidget, m_backgroundColour);
494 DoChangeBackgroundColour(m_menuWidget, m_backgroundColour);
495 int i;
496 for (i = 0; i < m_noStrings; i++)
497 DoChangeBackgroundColour(m_widgetList[i], m_backgroundColour);
498 }
499
500 void wxChoice::ChangeForegroundColour()
501 {
502 DoChangeForegroundColour(m_formWidget, m_foregroundColour);
503 DoChangeForegroundColour(m_buttonWidget, m_foregroundColour);
504 DoChangeForegroundColour(m_menuWidget, m_foregroundColour);
505 int i;
506 for (i = 0; i < m_noStrings; i++)
507 DoChangeForegroundColour(m_widgetList[i], m_foregroundColour);
508 }
509
510
511 // These implement functions needed by wxControlWithItems.
512 // Unfortunately, they're not all implemented yet.
513
514 int wxChoice::GetCount() const
515 {
516 return Number();
517 }
518
519 int wxChoice::DoAppend(const wxString& item)
520 {
521 Append(item);
522 return GetCount() - 1;
523 }
524
525 // Just appends, doesn't yet insert
526 void wxChoice::DoInsertItems(const wxArrayString& items, int WXUNUSED(pos))
527 {
528 size_t nItems = items.GetCount();
529
530 for ( size_t n = 0; n < nItems; n++ )
531 {
532 Append( items[n]);
533 }
534 }
535
536 void wxChoice::DoSetItems(const wxArrayString& items, void **WXUNUSED(clientData))
537 {
538 Clear();
539 size_t nItems = items.GetCount();
540
541 for ( size_t n = 0; n < nItems; n++ )
542 {
543 Append(items[n]);
544 }
545 }
546
547 void wxChoice::DoSetFirstItem(int WXUNUSED(n))
548 {
549 wxFAIL_MSG( wxT("wxChoice::DoSetFirstItem not implemented") );
550 }
551
552 void wxChoice::DoSetItemClientData(int n, void* clientData)
553 {
554 wxNode *node = m_clientList.Nth( n );
555 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientData") );
556
557 node->SetData( (wxObject*) clientData );
558 }
559
560 void* wxChoice::DoGetItemClientData(int n) const
561 {
562 wxNode *node = m_clientList.Nth( n );
563 wxCHECK_MSG( node, NULL, wxT("invalid index in wxChoice::DoGetItemClientData") );
564
565 return node->Data();
566 }
567
568 void wxChoice::DoSetItemClientObject(int n, wxClientData* clientData)
569 {
570 wxNode *node = m_clientList.Nth( n );
571 wxCHECK_RET( node, wxT("invalid index in wxChoice::DoSetItemClientObject") );
572
573 wxClientData *cd = (wxClientData*) node->Data();
574 delete cd;
575
576 node->SetData( (wxObject*) clientData );
577 }
578
579 wxClientData* wxChoice::DoGetItemClientObject(int n) const
580 {
581 wxNode *node = m_clientList.Nth( n );
582 wxCHECK_MSG( node, (wxClientData *)NULL,
583 wxT("invalid index in wxChoice::DoGetItemClientObject") );
584
585 return (wxClientData*) node->Data();
586 }
587
588 void wxChoice::Select(int n)
589 {
590 SetSelection(n);
591 }
592
593 void wxChoice::SetString(int WXUNUSED(n), const wxString& WXUNUSED(s))
594 {
595 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
596 }