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