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