]> git.saurik.com Git - wxWidgets.git/blob - src/motif/button.cpp
Fix another crash when conversion fails in Unix PostScript code.
[wxWidgets.git] / src / motif / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/button.h"
15
16 #ifdef __VMS__
17 #pragma message disable nosimpint
18 #endif
19 #include <Xm/PushBG.h>
20 #include <Xm/PushB.h>
21 #ifdef __VMS__
22 #pragma message enable nosimpint
23 #endif
24
25
26 #ifndef WX_PRECOMP
27 #include "wx/toplevel.h"
28 #endif
29
30 #include "wx/stockitem.h"
31 #include "wx/motif/private.h"
32 #include "wx/sysopt.h"
33
34 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
35
36 #define MIN_WIDTH 78
37 #define MIN_LARGE_HEIGHT 30
38
39 // Button
40
41 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl,
42 const wxPoint& pos,
43 const wxSize& size, long style,
44 const wxValidator& validator,
45 const wxString& name)
46 {
47 wxString label(lbl);
48 if (label.empty() && wxIsStockID(id))
49 label = wxGetStockLabel(id);
50
51 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
52 return false;
53 PreCreation();
54
55 wxXmString text( GetLabelText(label) );
56
57 Widget parentWidget = (Widget) parent->GetClientWidget();
58
59 /*
60 * Patch Note (important)
61 * There is no major reason to put a defaultButtonThickness here.
62 * Not requesting it give the ability to put wxButton with a spacing
63 * as small as requested. However, if some button become a DefaultButton,
64 * other buttons are no more aligned -- This is why we set
65 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
66 * in the ::SetDefaultButton method.
67 */
68 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
69 xmPushButtonWidgetClass,
70 parentWidget,
71 wxFont::GetFontTag(), m_font.GetFontTypeC(XtDisplay(parentWidget)),
72 XmNlabelString, text(),
73 XmNrecomputeSize, False,
74 // See comment for wxButton::SetDefault
75 // XmNdefaultButtonShadowThickness, 1,
76 NULL);
77
78 XtAddCallback ((Widget) m_mainWidget,
79 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
80 (XtPointer) this);
81
82 wxSize best = GetBestSize();
83 if( size.x != -1 ) best.x = size.x;
84 if( size.y != -1 ) best.y = size.y;
85
86 PostCreation();
87 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
88 pos.x, pos.y, best.x, best.y);
89
90 return true;
91 }
92
93 void wxButton::SetDefaultShadowThicknessAndResize()
94 {
95 Widget buttonWidget = (Widget)GetMainWidget();
96 bool managed = XtIsManaged( buttonWidget );
97 if( managed )
98 XtUnmanageChild( buttonWidget );
99
100 XtVaSetValues( buttonWidget,
101 XmNdefaultButtonShadowThickness, 1,
102 NULL );
103
104 if( managed )
105 XtManageChild( buttonWidget );
106
107 // this can't currently be done, because user code that calls SetDefault
108 // will break otherwise
109 #if 0
110 wxSize best = GetBestSize(), actual = GetSize();
111 if( best.x < actual.x ) best.x = actual.x;
112 if( best.y < actual.y ) best.y = actual.y;
113
114 if( best != actual )
115 SetSize( best );
116 #endif
117 InvalidateBestSize();
118 }
119
120
121 wxWindow *wxButton::SetDefault()
122 {
123 wxWindow *oldDefault = wxButtonBase::SetDefault();
124
125 // We initially do not set XmNdefaultShadowThickness, to have
126 // small buttons. Unfortunately, buttons are now mis-aligned. We
127 // try to correct this now -- setting this ressource to 1 for each
128 // button in the same row. Because it's very hard to find
129 // wxButton in the same row, correction is straighforward: we set
130 // resource for all wxButton in this parent (but not sub panels)
131
132 wxWindow *parent = GetParent();
133 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
134 node; node = node->GetNext ())
135 {
136 wxWindow *win = node->GetData ();
137 wxButton *item = wxDynamicCast(win, wxButton);
138 if (item)
139 item->SetDefaultShadowThicknessAndResize();
140 }
141
142 XtVaSetValues ((Widget) parent->GetMainWidget(),
143 XmNdefaultButton, (Widget) GetMainWidget(),
144 NULL);
145
146 return oldDefault;
147 }
148
149 static inline bool wxMotifLargeButtons()
150 {
151 return wxSystemOptions::HasOption("motif.largebuttons")
152 && wxSystemOptions::GetOptionInt("motif.largebuttons") != 0;
153 }
154
155 /* static */
156 wxSize wxButton::GetDefaultSize()
157 {
158 // TODO: check font size as in wxMSW ? MB
159 // Note: this is the button size (text + margin + shadow + defaultBorder)
160 return wxSize( MIN_WIDTH, MIN_LARGE_HEIGHT );
161 }
162
163 wxSize wxButton::DoGetBestSize() const
164 {
165 if( wxMotifLargeButtons() )
166 return OldGetBestSize();
167
168 wxSize best = wxControl::DoGetBestSize();
169
170 if( HasFlag( wxBU_EXACTFIT ) )
171 return best;
172 else if( best.x < MIN_WIDTH )
173 best.x = MIN_WIDTH;
174
175 return best;
176 }
177
178 wxSize wxButton::GetMinSize() const
179 {
180 if( wxMotifLargeButtons() )
181 return OldGetMinSize();
182
183 return DoGetBestSize();
184 }
185
186 wxSize wxButton::OldGetMinSize() const
187 {
188 return OldGetBestSize();
189 }
190
191 wxSize wxButton::OldGetBestSize() const
192 {
193 Dimension xmargin, ymargin, highlight, shadow, defThickness;
194
195 XtVaGetValues( (Widget)m_mainWidget,
196 XmNmarginWidth, &xmargin,
197 XmNmarginHeight, &ymargin,
198 XmNhighlightThickness, &highlight,
199 XmNshadowThickness, &shadow,
200 XmNdefaultButtonShadowThickness, &defThickness,
201 NULL );
202
203 int x = 0; int y = 0;
204 GetTextExtent( GetLabel(), &x, &y );
205
206 int margin = highlight * 2 +
207 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
208
209 wxSize best( x + xmargin * 2 + margin,
210 y + ymargin * 2 + margin );
211
212 // all buttons have at least the standard size unless the user explicitly
213 // wants them to be of smaller size and used wxBU_EXACTFIT style when
214 // creating the button
215 if( !HasFlag( wxBU_EXACTFIT ) )
216 {
217 wxSize def = GetDefaultSize();
218 int margin = highlight * 2 +
219 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
220 def.x += margin;
221 def.y += margin;
222
223 if( def.x > best.x )
224 best.x = def.x;
225 if( def.y > best.y )
226 best.y = def.y;
227 }
228
229 return best;
230 }
231
232 void wxButton::Command (wxCommandEvent & event)
233 {
234 ProcessCommand (event);
235 }
236
237 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
238 {
239 if (!wxGetWindowFromTable(w))
240 // Widget has been deleted!
241 return;
242
243 wxButton *item = (wxButton *) clientData;
244 wxCommandEvent event (wxEVT_BUTTON, item->GetId());
245 event.SetEventObject(item);
246 item->ProcessCommand (event);
247 }