]> git.saurik.com Git - wxWidgets.git/blob - src/motif/button.cpp
another file I added on the wxUniv branch and forgot to merge
[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/button.h"
21 #include "wx/utils.h"
22 #include "wx/panel.h"
23
24 #ifdef __VMS__
25 #pragma message disable nosimpint
26 #endif
27 #include <Xm/PushBG.h>
28 #include <Xm/PushB.h>
29 #ifdef __VMS__
30 #pragma message enable nosimpint
31 #endif
32
33 #include "wx/motif/private.h"
34
35 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr);
36
37 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
38
39 // Button
40
41 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
42 const wxPoint& pos,
43 const wxSize& size, long style,
44 const wxValidator& validator,
45 const wxString& name)
46 {
47 SetName(name);
48 SetValidator(validator);
49 m_windowStyle = style;
50 m_backgroundColour = parent->GetBackgroundColour();
51 m_foregroundColour = parent->GetForegroundColour();
52 m_font = parent->GetFont();
53
54 parent->AddChild((wxButton *)this);
55
56 if (id == -1)
57 m_windowId = NewControlId();
58 else
59 m_windowId = id;
60
61 wxString label1(wxStripMenuCodes(label));
62
63 XmString text = XmStringCreateSimple ((char*) (const char*) label1);
64 Widget parentWidget = (Widget) parent->GetClientWidget();
65
66 XmFontList fontList = (XmFontList) m_font.GetFontList(1.0, XtDisplay(parentWidget));
67
68 /*
69 * Patch Note (important)
70 * There is no major reason to put a defaultButtonThickness here.
71 * Not requesting it give the ability to put wxButton with a spacing
72 * as small as requested. However, if some button become a DefaultButton,
73 * other buttons are no more aligned -- This is why we set
74 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
75 * in the ::SetDefaultButton method.
76 */
77 m_mainWidget = (WXWidget) XtVaCreateManagedWidget ("button",
78 xmPushButtonWidgetClass,
79 parentWidget,
80 XmNfontList, fontList,
81 XmNlabelString, text,
82 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
83 NULL);
84
85 XmStringFree (text);
86
87 XtAddCallback ((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc) wxButtonCallback,
88 (XtPointer) this);
89
90 SetCanAddEventHandler(TRUE);
91
92 int x = 0; int y = 0;
93 wxFont new_font( parent->GetFont() );
94 GetTextExtent( label1, &x, &y, (int*)NULL, (int*)NULL, &new_font );
95
96 wxSize newSize = size;
97 if (newSize.x == -1) newSize.x = 30+x;
98 if (newSize.y == -1) newSize.y = 27+y;
99 SetSize( newSize.x, newSize.y );
100
101 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, newSize.x, newSize.y);
102
103 ChangeBackgroundColour();
104
105 return TRUE;
106 }
107
108 void wxButton::SetDefault()
109 {
110 wxWindow *parent = GetParent();
111 wxPanel *panel = wxDynamicCast(parent, wxPanel);
112 if ( panel )
113 panel->SetDefaultItem(this);
114
115 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
116 // Unfortunately, buttons are now mis-aligned. We try to correct this
117 // now -- setting this ressource to 1 for each button in the same row.
118 // Because it's very hard to find wxButton in the same row,
119 // correction is straighforward: we set resource for all wxButton
120 // in this parent (but not sub panels)
121 for (wxNode * node = parent->GetChildren().First (); node; node = node->Next ())
122 {
123 wxButton *item = (wxButton *) node->Data ();
124 if (item->IsKindOf(CLASSINFO(wxButton)))
125 {
126 bool managed = XtIsManaged((Widget) item->GetMainWidget());
127 if (managed)
128 XtUnmanageChild ((Widget) item->GetMainWidget());
129
130 XtVaSetValues ((Widget) item->GetMainWidget(),
131 XmNdefaultButtonShadowThickness, 1,
132 NULL);
133
134 if (managed)
135 XtManageChild ((Widget) item->GetMainWidget());
136 }
137 } // while
138
139 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
140 XtVaSetValues ((Widget) parent->GetMainWidget(), XmNdefaultButton, (Widget) GetMainWidget(), NULL);
141 }
142
143 /* static */
144 wxSize wxButton::GetDefaultSize()
145 {
146 // TODO: check font size as in wxMSW ? MB
147 //
148 return wxSize(80,26);
149 }
150
151 void wxButton::Command (wxCommandEvent & event)
152 {
153 ProcessCommand (event);
154 }
155
156 void wxButtonCallback (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
157 {
158 if (!wxGetWindowFromTable(w))
159 // Widget has been deleted!
160 return;
161
162 wxButton *item = (wxButton *) clientData;
163 wxCommandEvent event (wxEVT_COMMAND_BUTTON_CLICKED, item->GetId());
164 event.SetEventObject(item);
165 item->ProcessCommand (event);
166 }
167
168 void wxButton::ChangeFont(bool keepOriginalSize)
169 {
170 wxWindow::ChangeFont(keepOriginalSize);
171 }
172
173 void wxButton::ChangeBackgroundColour()
174 {
175 DoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE);
176 }
177
178 void wxButton::ChangeForegroundColour()
179 {
180 wxWindow::ChangeForegroundColour();
181 }
182