]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/choice.cpp
39b0f030860affe7ab284632d64ed05241fc6624
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/choice.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/choice.h"
21 #include "wx/arrstr.h"
25 #pragma message disable nosimpint
28 #include <Xm/PushBG.h>
30 #include <Xm/RowColumn.h>
32 #pragma message enable nosimpint
35 #include "wx/motif/private.h"
37 #define WIDTH_OVERHEAD 48
38 #define WIDTH_OVERHEAD_SUBTRACT 40
39 #define HEIGHT_OVERHEAD 15
41 void wxChoiceCallback (Widget w
, XtPointer clientData
,
51 m_buttonWidget
= (WXWidget
) 0;
52 m_menuWidget
= (WXWidget
) 0;
53 m_formWidget
= (WXWidget
) 0;
56 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
59 int n
, const wxString choices
[],
61 const wxValidator
& validator
,
64 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
68 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
70 m_formWidget
= (WXWidget
) XtVaCreateManagedWidget(name
.c_str(),
71 xmRowColumnWidgetClass
, parentWidget
,
74 XmNpacking
, XmPACK_TIGHT
,
75 XmNorientation
, XmHORIZONTAL
,
76 XmNresizeWidth
, False
,
77 XmNresizeHeight
, False
,
80 XtVaSetValues ((Widget
) m_formWidget
, XmNspacing
, 0, NULL
);
83 * Create the popup menu
85 m_menuWidget
= (WXWidget
) XmCreatePulldownMenu ((Widget
) m_formWidget
,
86 wxMOTIF_STR("choiceMenu"),
92 for (i
= 0; i
< n
; i
++)
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 wxMOTIF_STR("choiceButton"),
110 m_mainWidget
= m_buttonWidget
;
112 XtManageChild ((Widget
) m_buttonWidget
);
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
120 // JACS, 24/1/99: this seems to cause a malloc crash later on, e.g.
121 // in controls sample.
123 // Widget optionLabel = XmOptionLabelGadget ((Widget) m_buttonWidget);
124 // XtUnmanageChild (optionLabel);
128 wxSize bestSize
= GetBestSize();
129 if( size
.x
> 0 ) bestSize
.x
= size
.x
;
130 if( size
.y
> 0 ) bestSize
.y
= size
.y
;
132 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
135 AttachWidget (parent
, m_buttonWidget
, m_formWidget
,
136 pos
.x
, pos
.y
, bestSize
.x
, bestSize
.y
);
141 bool wxChoice::Create(wxWindow
*parent
, wxWindowID id
,
144 const wxArrayString
& choices
,
146 const wxValidator
& validator
,
147 const wxString
& name
)
149 wxCArrayString
chs(choices
);
150 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
151 style
, validator
, name
);
154 wxChoice::~wxChoice()
156 // For some reason destroying the menuWidget
157 // can cause crashes on some machines. It will
158 // be deleted implicitly by deleting the parent form
160 // XtDestroyWidget (menuWidget);
164 DetachWidget(GetMainWidget()); // Removes event handlers
165 DetachWidget(m_formWidget
);
167 XtDestroyWidget((Widget
) m_formWidget
);
168 m_formWidget
= (WXWidget
) 0;
170 // Presumably the other widgets have been deleted now, via the form
171 m_mainWidget
= (WXWidget
) 0;
172 m_buttonWidget
= (WXWidget
) 0;
176 static inline wxChar
* MYcopystring(const wxChar
* s
)
178 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
179 return wxStrcpy(copy
, s
);
182 // TODO auto-sorting is not supported by the code
183 int wxChoice::DoInsertItems(const wxArrayStringsAdapter
& items
,
185 void **clientData
, wxClientDataType type
)
187 #ifndef XmNpositionIndex
188 wxCHECK_MSG( pos
== GetCount(), -1, wxT("insert not implemented"));
191 const unsigned int numItems
= items
.GetCount();
192 AllocClientData(numItems
);
193 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
195 Widget w
= XtVaCreateManagedWidget (GetLabelText(items
[i
]),
197 xmPushButtonGadgetClass
, (Widget
) m_menuWidget
,
199 xmPushButtonWidgetClass
, (Widget
) m_menuWidget
,
201 #ifdef XmNpositionIndex
202 XmNpositionIndex
, pos
,
206 wxDoChangeBackgroundColour((WXWidget
) w
, m_backgroundColour
);
209 wxDoChangeFont( w
, m_font
);
211 m_widgetArray
.Insert(w
, pos
);
213 char mnem
= wxFindMnemonic (items
[i
]);
215 XtVaSetValues (w
, XmNmnemonic
, mnem
, NULL
);
217 XtAddCallback (w
, XmNactivateCallback
,
218 (XtCallbackProc
) wxChoiceCallback
,
221 if (m_stringArray
.GetCount() == 0 && m_buttonWidget
)
223 XtVaSetValues ((Widget
) m_buttonWidget
, XmNmenuHistory
, w
, NULL
);
224 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
225 wxXmString
text( items
[i
] );
226 XtVaSetValues (label
,
227 XmNlabelString
, text(),
231 m_stringArray
.Insert(items
[i
], pos
);
233 InsertNewItemClientData(pos
, clientData
, i
, type
);
239 void wxChoice::DoDeleteOneItem(unsigned int n
)
241 Widget w
= (Widget
)m_widgetArray
[n
];
242 XtRemoveCallback(w
, XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
245 m_stringArray
.RemoveAt(size_t(n
));
246 m_widgetArray
.RemoveAt(size_t(n
));
247 wxChoiceBase::DoDeleteOneItem(n
);
252 void wxChoice::DoClear()
254 m_stringArray
.Clear();
257 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
259 XtRemoveCallback((Widget
) m_widgetArray
[i
],
260 XmNactivateCallback
, (XtCallbackProc
)wxChoiceCallback
,
262 XtUnmanageChild ((Widget
) m_widgetArray
[i
]);
263 XtDestroyWidget ((Widget
) m_widgetArray
[i
]);
266 m_widgetArray
.Clear();
268 XtVaSetValues ((Widget
) m_buttonWidget
,
269 XmNmenuHistory
, (Widget
) NULL
,
272 wxChoiceBase::DoClear();
275 int wxChoice::GetSelection() const
278 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
279 XtVaGetValues (label
,
280 XmNlabelString
, &text
,
282 wxXmString
freeMe(text
);
283 wxString s
= wxXmStringToString( text
);
287 for (size_t i
=0; i
<m_stringArray
.GetCount(); i
++)
288 if (m_stringArray
[i
] == s
)
297 void wxChoice::SetSelection(int n
)
302 Dimension selectionWidth
, selectionHeight
;
304 wxXmString
text( m_stringArray
[n
] );
305 // MBN: this seems silly, at best, and causes wxChoices to be clipped:
306 // will remove "soon"
308 XtVaGetValues ((Widget
) m_widgetArray
[n
],
309 XmNwidth
, &selectionWidth
,
310 XmNheight
, &selectionHeight
,
313 Widget label
= XmOptionButtonGadget ((Widget
) m_buttonWidget
);
314 XtVaSetValues (label
,
315 XmNlabelString
, text(),
318 XtVaSetValues ((Widget
) m_buttonWidget
,
319 XmNwidth
, selectionWidth
, XmNheight
, selectionHeight
,
320 XmNmenuHistory
, (Widget
) m_widgetArray
[n
], NULL
);
323 m_inSetValue
= false;
326 wxString
wxChoice::GetString(unsigned int n
) const
328 return m_stringArray
[n
];
331 void wxChoice::SetColumns(int n
)
335 short numColumns
= (short)n
;
338 XtSetArg(args
[0], XmNnumColumns
, numColumns
);
339 XtSetArg(args
[1], XmNpacking
, XmPACK_COLUMN
);
340 XtSetValues((Widget
) m_menuWidget
,args
,2) ;
343 int wxChoice::GetColumns(void) const
347 XtVaGetValues((Widget
) m_menuWidget
,XmNnumColumns
,&numColumns
,NULL
) ;
351 void wxChoice::SetFocus()
353 XmProcessTraversal(XtParent((Widget
)m_mainWidget
), XmTRAVERSE_CURRENT
);
356 void wxChoice::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
358 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
359 bool managed
= XtIsManaged((Widget
) m_formWidget
);
362 XtUnmanageChild ((Widget
) m_formWidget
);
364 int actualWidth
= width
- WIDTH_OVERHEAD_SUBTRACT
,
365 actualHeight
= height
- HEIGHT_OVERHEAD
;
370 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
371 XtVaSetValues ((Widget
) m_widgetArray
[i
],
372 XmNwidth
, actualWidth
,
374 XtVaSetValues ((Widget
) m_buttonWidget
, XmNwidth
, actualWidth
,
381 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
382 XtVaSetValues ((Widget
) m_widgetArray
[i
],
383 XmNheight
, actualHeight
,
386 XtVaSetValues ((Widget
) m_buttonWidget
, XmNheight
, actualHeight
,
391 XtManageChild ((Widget
) m_formWidget
);
392 XtVaSetValues((Widget
) m_formWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
394 wxControl::DoSetSize (x
, y
, width
, height
, sizeFlags
);
397 void wxChoice::Command(wxCommandEvent
& event
)
399 SetSelection (event
.GetInt());
400 ProcessCommand (event
);
403 void wxChoiceCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
405 wxChoice
*item
= (wxChoice
*) clientData
;
408 if (item
->InSetValue())
411 int n
= item
->GetWidgets().Index(w
);
412 if (n
!= wxNOT_FOUND
)
414 wxCommandEvent
event(wxEVT_CHOICE
, item
->GetId());
415 event
.SetEventObject(item
);
417 event
.SetString( item
->GetStrings().Item(n
) );
418 if ( item
->HasClientObjectData() )
419 event
.SetClientObject( item
->GetClientObject(n
) );
420 else if ( item
->HasClientUntypedData() )
421 event
.SetClientData( item
->GetClientData(n
) );
422 item
->ProcessCommand (event
);
427 void wxChoice::ChangeFont(bool keepOriginalSize
)
429 // Note that this causes the widget to be resized back
430 // to its original size! We therefore have to set the size
431 // back again. TODO: a better way in Motif?
432 if (m_mainWidget
&& m_font
.IsOk())
434 Display
* dpy
= XtDisplay((Widget
) m_mainWidget
);
435 int width
, height
, width1
, height1
;
436 GetSize(& width
, & height
);
438 WXString fontTag
= wxFont::GetFontTag();
440 XtVaSetValues ((Widget
) m_formWidget
,
441 fontTag
, m_font
.GetFontTypeC(dpy
),
443 XtVaSetValues ((Widget
) m_buttonWidget
,
444 fontTag
, m_font
.GetFontTypeC(dpy
),
447 for( unsigned int i
= 0; i
< m_stringArray
.GetCount(); ++i
)
448 XtVaSetValues( (Widget
)m_widgetArray
[i
],
449 fontTag
, m_font
.GetFontTypeC(dpy
),
452 GetSize(& width1
, & height1
);
453 if (keepOriginalSize
&& (width
!= width1
|| height
!= height1
))
455 SetSize(wxDefaultCoord
, wxDefaultCoord
, width
, height
);
460 void wxChoice::ChangeBackgroundColour()
462 wxDoChangeBackgroundColour(m_formWidget
, m_backgroundColour
);
463 wxDoChangeBackgroundColour(m_buttonWidget
, m_backgroundColour
);
464 wxDoChangeBackgroundColour(m_menuWidget
, m_backgroundColour
);
466 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
467 wxDoChangeBackgroundColour(m_widgetArray
[i
], m_backgroundColour
);
470 void wxChoice::ChangeForegroundColour()
472 wxDoChangeForegroundColour(m_formWidget
, m_foregroundColour
);
473 wxDoChangeForegroundColour(m_buttonWidget
, m_foregroundColour
);
474 wxDoChangeForegroundColour(m_menuWidget
, m_foregroundColour
);
476 for (i
= 0; i
< m_stringArray
.GetCount(); i
++)
477 wxDoChangeForegroundColour(m_widgetArray
[i
], m_foregroundColour
);
480 unsigned int wxChoice::GetCount() const
482 return m_stringArray
.GetCount();
485 void wxChoice::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
487 wxFAIL_MSG( wxT("wxChoice::SetString not implemented") );
490 wxSize
wxChoice::GetItemsSize() const
492 int x
, y
, mx
= 0, my
= 0;
495 GetTextExtent( "|", &x
, &my
);
497 for (size_t i
=0; i
<m_stringArray
.GetCount(); i
++)
499 GetTextExtent( m_stringArray
[i
], &x
, &y
);
504 return wxSize( mx
, my
);
507 wxSize
wxChoice::DoGetBestSize() const
509 wxSize items
= GetItemsSize();
510 // FIXME arbitrary constants
511 return wxSize( ( items
.x
? items
.x
+ WIDTH_OVERHEAD
: 120 ),
512 items
.y
+ HEIGHT_OVERHEAD
);
515 #endif // wxUSE_CHOICE