]> git.saurik.com Git - wxWidgets.git/blame - src/motif/button.cpp
cleanup
[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
65571936 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 35
5f7bcb48 36#include "wx/stockitem.h"
02e8b2f9 37#include "wx/motif/private.h"
e04b7e8e 38#include "wx/sysopt.h"
02e8b2f9
JS
39
40void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
41
4bb6408c 42IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
4bb6408c 43
e04b7e8e
MB
44#define MIN_WIDTH 78
45#define MIN_LARGE_HEIGHT 30
46
4bb6408c
JS
47// Button
48
5f7bcb48 49bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl,
2d120f83
JS
50 const wxPoint& pos,
51 const wxSize& size, long style,
52 const wxValidator& validator,
53 const wxString& name)
4bb6408c 54{
5f7bcb48
VS
55 wxString label(lbl);
56 if (label.empty() && wxIsStockID(id))
57 label = wxGetStockLabel(id);
58
458ca8c1
MB
59 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
60 return false;
dfe1eee3 61
89c7e962 62 wxString label1(wxStripMenuCodes(label));
458ca8c1 63 wxXmString text( label1 );
dfe1eee3 64
02e8b2f9 65 Widget parentWidget = (Widget) parent->GetClientWidget();
dfe1eee3 66
02e8b2f9 67 /*
2d120f83
JS
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 */
02e8b2f9 76 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
2d120f83
JS
77 xmPushButtonWidgetClass,
78 parentWidget,
da494b40 79 wxFont::GetFontTag(), m_font.GetFontType(XtDisplay(parentWidget)),
458ca8c1 80 XmNlabelString, text(),
84fb430b 81 XmNrecomputeSize, False,
458ca8c1
MB
82 // See comment for wxButton::SetDefault
83 // XmNdefaultButtonShadowThickness, 1,
2d120f83 84 NULL);
dfe1eee3 85
458ca8c1
MB
86 XtAddCallback ((Widget) m_mainWidget,
87 XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
88 (XtPointer) this);
dfe1eee3 89
458ca8c1
MB
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);
dfe1eee3 96
0d57be45 97 ChangeBackgroundColour();
dfe1eee3 98
e04b7e8e 99 return true;
4bb6408c
JS
100}
101
458ca8c1
MB
102void 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
84fb430b
MB
116 // this can't currently be done, because user code that calls SetDefault
117 // will break otherwise
118#if 0
458ca8c1
MB
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 );
84fb430b 125#endif
458ca8c1
MB
126}
127
128
4bb6408c
JS
129void wxButton::SetDefault()
130{
dfe1eee3 131 wxWindow *parent = GetParent();
b0ee47ff 132 if ( parent )
d5f005cc 133 parent->SetDefaultItem(this);
dfe1eee3 134
458ca8c1
MB
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
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
185wxSize wxButton::OldGetBestSize() const
458ca8c1
MB
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;
4d194d63
MB
223}
224
4bb6408c
JS
225void wxButton::Command (wxCommandEvent & event)
226{
227 ProcessCommand (event);
228}
229
f9e02ac7 230void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 231{
2d120f83
JS
232 if (!wxGetWindowFromTable(w))
233 // Widget has been deleted!
234 return;
dfe1eee3 235
2d120f83
JS
236 wxButton *item = (wxButton *) clientData;
237 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
238 event.SetEventObject(item);
239 item->ProcessCommand (event);
02e8b2f9 240}