1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "button.h"
17 #define XtDisplay XTDISPLAY
22 #include "wx/button.h"
27 #pragma message disable nosimpint
29 #include <Xm/PushBG.h>
32 #pragma message enable nosimpint
35 #include "wx/motif/private.h"
37 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
39 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
43 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
45 const wxSize
& size
, long style
,
46 const wxValidator
& validator
,
50 SetValidator(validator
);
51 m_windowStyle
= style
;
52 m_backgroundColour
= parent
->GetBackgroundColour();
53 m_foregroundColour
= parent
->GetForegroundColour();
54 m_font
= parent
->GetFont();
56 parent
->AddChild((wxButton
*)this);
59 m_windowId
= NewControlId();
63 wxString
label1(wxStripMenuCodes(label
));
65 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
66 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
68 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
71 * Patch Note (important)
72 * There is no major reason to put a defaultButtonThickness here.
73 * Not requesting it give the ability to put wxButton with a spacing
74 * as small as requested. However, if some button become a DefaultButton,
75 * other buttons are no more aligned -- This is why we set
76 * defaultButtonThickness of ALL buttons belonging to the same wxPanel,
77 * in the ::SetDefaultButton method.
79 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
80 xmPushButtonWidgetClass
,
82 XmNfontList
, fontList
,
84 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
89 XtAddCallback ((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
92 SetCanAddEventHandler(TRUE
);
95 wxFont
new_font( parent
->GetFont() );
96 GetTextExtent( label1
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
98 wxSize newSize
= size
;
99 if (newSize
.x
== -1) newSize
.x
= 30+x
;
100 if (newSize
.y
== -1) newSize
.y
= 27+y
;
101 SetSize( newSize
.x
, newSize
.y
);
103 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, newSize
.x
, newSize
.y
);
105 ChangeBackgroundColour();
110 void wxButton::SetDefault()
112 wxWindow
*parent
= GetParent();
113 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
115 panel
->SetDefaultItem(this);
117 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
118 // Unfortunately, buttons are now mis-aligned. We try to correct this
119 // now -- setting this ressource to 1 for each button in the same row.
120 // Because it's very hard to find wxButton in the same row,
121 // correction is straighforward: we set resource for all wxButton
122 // in this parent (but not sub panels)
123 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
125 wxButton
*item
= (wxButton
*) node
->Data ();
126 if (item
->IsKindOf(CLASSINFO(wxButton
)))
128 bool managed
= XtIsManaged((Widget
) item
->GetMainWidget());
130 XtUnmanageChild ((Widget
) item
->GetMainWidget());
132 XtVaSetValues ((Widget
) item
->GetMainWidget(),
133 XmNdefaultButtonShadowThickness
, 1,
137 XtManageChild ((Widget
) item
->GetMainWidget());
141 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
142 XtVaSetValues ((Widget
) parent
->GetMainWidget(), XmNdefaultButton
, (Widget
) GetMainWidget(), NULL
);
146 wxSize
wxButton::GetDefaultSize()
148 // TODO: check font size as in wxMSW ? MB
150 return wxSize(80,26);
153 void wxButton::Command (wxCommandEvent
& event
)
155 ProcessCommand (event
);
158 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
160 if (!wxGetWindowFromTable(w
))
161 // Widget has been deleted!
164 wxButton
*item
= (wxButton
*) clientData
;
165 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
166 event
.SetEventObject(item
);
167 item
->ProcessCommand (event
);
170 void wxButton::ChangeFont(bool keepOriginalSize
)
172 wxWindow::ChangeFont(keepOriginalSize
);
175 void wxButton::ChangeBackgroundColour()
177 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
180 void wxButton::ChangeForegroundColour()
182 wxWindow::ChangeForegroundColour();