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