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