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