]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
Native wxControl::DoGetBestSize() implementation
[wxWidgets.git] / src / motif / button.cpp
CommitLineData
4bb6408c
JS
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
dfe1eee3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
4bb6408c
JS
13#pragma implementation "button.h"
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
bcd055ae
JJ
19#ifdef __VMS
20#define XtDisplay XTDISPLAY
21#endif
22
f6045f99
GD
23#include "wx/defs.h"
24
4bb6408c
JS
25#include "wx/button.h"
26
338dd992
JJ
27#ifdef __VMS__
28#pragma message disable nosimpint
29#endif
02e8b2f9
JS
30#include <Xm/PushBG.h>
31#include <Xm/PushB.h>
338dd992
JJ
32#ifdef __VMS__
33#pragma message enable nosimpint
34#endif
02e8b2f9
JS
35
36#include "wx/motif/private.h"
37
38void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
39
4bb6408c 40IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
4bb6408c
JS
41
42// Button
43
44bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2d120f83
JS
45 const wxPoint& pos,
46 const wxSize& size, long style,
47 const wxValidator& validator,
48 const wxString& name)
4bb6408c 49{
458ca8c1
MB
50 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
51 return false;
dfe1eee3 52
89c7e962 53 wxString label1(wxStripMenuCodes(label));
458ca8c1 54 wxXmString text( label1 );
dfe1eee3 55
02e8b2f9 56 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 57
02e8b2f9 58 /*
2d120f83
JS
59 * Patch Note (important)
60 * There is no major reason to put a defaultButtonThickness here.
61 * Not requesting it give the ability to put wxButton with a spacing
62 * as small as requested. However, if some button become a DefaultButton,
63 * other buttons are no more aligned -- This is why we set
64 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
65 * in the ::SetDefaultButton method.
66 */
02e8b2f9 67 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
68 xmPushButtonWidgetClass,
69 parentWidget,
da494b40 70 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
458ca8c1 71 XmNlabelString, text(),
84fb430b 72 XmNrecomputeSize, False,
458ca8c1
MB
73 // See comment for wxButton::SetDefault
74 // XmNdefaultButtonShadowThickness, 1,
2d120f83 75 NULL);
dfe1eee3 76
458ca8c1
MB
77 XtAddCallback ((Widget) m_mainWidget,
78 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
79 (XtPointer) this);
dfe1eee3 80
458ca8c1
MB
81 wxSize best = GetBestSize();
82 if( size.x != -1 ) best.x = size.x;
83 if( size.y != -1 ) best.y = size.y;
84
85 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
86 pos.x, pos.y, best.x, best.y);
dfe1eee3 87
0d57be45 88 ChangeBackgroundColour();
dfe1eee3 89
02e8b2f9 90 return TRUE;
4bb6408c
JS
91}
92
458ca8c1
MB
93void 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
84fb430b
MB
107 // this can't currently be done, because user code that calls SetDefault
108 // will break otherwise
109#if 0
458ca8c1
MB
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 );
84fb430b 116#endif
458ca8c1
MB
117}
118
119
4bb6408c
JS
120void wxButton::SetDefault()
121{
dfe1eee3 122 wxWindow *parent = GetParent();
b0ee47ff 123 if ( parent )
d5f005cc 124 parent->SetDefaultItem(this);
dfe1eee3 125
458ca8c1
MB
126 // We initially do not set XmNdefaultShadowThickness, to have
127 // small buttons. Unfortunately, buttons are now mis-aligned. We
128 // try to correct this now -- setting this ressource to 1 for each
129 // button in the same row. Because it's very hard to find
130 // wxButton in the same row, correction is straighforward: we set
131 // resource for all wxButton in this parent (but not sub panels)
132
ac32ba44 133 for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst ();
fd304d98 134 node; node = node->GetNext ())
02e8b2f9 135 {
fd304d98
MB
136 wxWindow *win = node->GetData ();
137 wxButton *item = wxDynamicCast(win, wxButton);
138 if (item)
458ca8c1
MB
139 item->SetDefaultShadowThicknessAndResize();
140 }
141
142 XtVaSetValues ((Widget) parent->GetMainWidget(),
143 XmNdefaultButton, (Widget) GetMainWidget(),
144 NULL);
4bb6408c
JS
145}
146
4d194d63
MB
147/* static */
148wxSize wxButton::GetDefaultSize()
149{
150 // TODO: check font size as in wxMSW ? MB
84fb430b
MB
151 // Note: this is the button size (text + margin + shadow + defaultBorder)
152 return wxSize(78,30);
458ca8c1
MB
153}
154
155wxSize wxButton::DoGetBestSize() const
156{
157 Dimension xmargin, ymargin, highlight, shadow, defThickness;
158
159 XtVaGetValues( (Widget)m_mainWidget,
160 XmNmarginWidth, &xmargin,
161 XmNmarginHeight, &ymargin,
162 XmNhighlightThickness, &highlight,
163 XmNshadowThickness, &shadow,
164 XmNdefaultButtonShadowThickness, &defThickness,
165 NULL );
166
167 int x = 0; int y = 0;
168 GetTextExtent( GetLabel(), &x, &y );
169
170 int margin = highlight * 2 +
171 ( defThickness ? ( ( shadow + defThickness ) * 4 ) : ( shadow * 2 ) );
172 wxSize best( x + xmargin * 2 + margin,
173 y + ymargin * 2 + margin );
174
175 // all buttons have at least the standard size unless the user explicitly
176 // wants them to be of smaller size and used wxBU_EXACTFIT style when
177 // creating the button
178 if( !HasFlag( wxBU_EXACTFIT ) )
179 {
180 wxSize def = GetDefaultSize();
181 int margin = highlight * 2 +
182 ( defThickness ? ( shadow * 4 + defThickness * 4 ) : 0 );
183 def.x += margin;
184 def.y += margin;
185
186 if( def.x > best.x )
187 best.x = def.x;
188 if( def.y > best.y )
189 best.y = def.y;
190 }
191
192 return best;
4d194d63
MB
193}
194
4bb6408c
JS
195void wxButton::Command (wxCommandEvent & event)
196{
197 ProcessCommand (event);
198}
199
f9e02ac7 200void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 201{
2d120f83
JS
202 if (!wxGetWindowFromTable(w))
203 // Widget has been deleted!
204 return;
dfe1eee3 205
2d120f83
JS
206 wxButton *item = (wxButton *) clientData;
207 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
208 event.SetEventObject(item);
209 item->ProcessCommand (event);
02e8b2f9 210}