1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "button.h"
16 #include "wx/button.h"
21 #pragma message disable nosimpint
23 #include <Xm/PushBG.h>
26 #pragma message enable nosimpint
29 #include "wx/motif/private.h"
31 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
39 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
41 const wxSize
& size
, long style
,
42 const wxValidator
& validator
,
46 SetValidator(validator
);
47 m_windowStyle
= style
;
48 m_backgroundColour
= parent
->GetBackgroundColour();
49 m_foregroundColour
= parent
->GetForegroundColour();
50 m_font
= parent
->GetFont();
52 parent
->AddChild((wxButton
*)this);
55 m_windowId
= NewControlId();
59 wxString
label1(wxStripMenuCodes(label
));
61 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
62 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
64 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
67 * Patch Note (important)
68 * There is no major reason to put a defaultButtonThickness here.
69 * Not requesting it give the ability to put wxButton with a spacing
70 * as small as requested. However, if some button become a DefaultButton,
71 * other buttons are no more aligned -- This is why we set
72 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
73 * in the ::SetDefaultButton method.
75 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
76 xmPushButtonWidgetClass
,
78 XmNfontList
, fontList
,
80 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
85 XtAddCallback ((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
88 SetCanAddEventHandler(TRUE
);
91 wxFont
new_font( parent
->GetFont() );
92 GetTextExtent( label1
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
94 wxSize newSize
= size
;
95 if (newSize
.x
== -1) newSize
.x
= 30+x
;
96 if (newSize
.y
== -1) newSize
.y
= 27+y
;
97 SetSize( newSize
.x
, newSize
.y
);
99 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, newSize
.x
, newSize
.y
);
101 ChangeBackgroundColour();
106 void wxButton::SetDefault()
108 wxWindow
*parent
= GetParent();
109 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
111 panel
->SetDefaultItem(this);
113 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
114 // Unfortunately, buttons are now mis-aligned. We try to correct this
115 // now -- setting this ressource to 1 for each button in the same row.
116 // Because it's very hard to find wxButton in the same row,
117 // correction is straighforward: we set resource for all wxButton
118 // in this parent (but not sub panels)
119 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
121 wxButton
*item
= (wxButton
*) node
->Data ();
122 if (item
->IsKindOf(CLASSINFO(wxButton
)))
124 bool managed
= XtIsManaged((Widget
) item
->GetMainWidget());
126 XtUnmanageChild ((Widget
) item
->GetMainWidget());
128 XtVaSetValues ((Widget
) item
->GetMainWidget(),
129 XmNdefaultButtonShadowThickness
, 1,
133 XtManageChild ((Widget
) item
->GetMainWidget());
137 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
138 XtVaSetValues ((Widget
) parent
->GetMainWidget(), XmNdefaultButton
, (Widget
) GetMainWidget(), NULL
);
142 wxSize
wxButton::GetDefaultSize()
144 // TODO: check font size as in wxMSW ? MB
146 return wxSize(80,26);
149 void wxButton::Command (wxCommandEvent
& event
)
151 ProcessCommand (event
);
154 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
156 if (!wxGetWindowFromTable(w
))
157 // Widget has been deleted!
160 wxButton
*item
= (wxButton
*) clientData
;
161 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
162 event
.SetEventObject(item
);
163 item
->ProcessCommand (event
);
166 void wxButton::ChangeFont(bool keepOriginalSize
)
168 wxWindow::ChangeFont(keepOriginalSize
);
171 void wxButton::ChangeBackgroundColour()
173 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
176 void wxButton::ChangeForegroundColour()
178 wxWindow::ChangeForegroundColour();