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