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"
20 #include <Xm/PushBG.h>
23 #include "wx/motif/private.h"
25 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer ptr
);
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
33 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
35 const wxSize
& size
, long style
,
36 const wxValidator
& validator
,
40 SetValidator(validator
);
41 m_windowStyle
= style
;
42 m_backgroundColour
= parent
->GetBackgroundColour();
43 m_foregroundColour
= parent
->GetForegroundColour();
44 m_font
= parent
->GetFont();
46 parent
->AddChild((wxButton
*)this);
49 m_windowId
= NewControlId();
53 wxString
label1(wxStripMenuCodes(label
));
55 XmString text
= XmStringCreateSimple ((char*) (const char*) label1
);
56 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
58 XmFontList fontList
= (XmFontList
) m_font
.GetFontList(1.0, XtDisplay(parentWidget
));
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.
69 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ("button",
70 xmPushButtonWidgetClass
,
72 XmNfontList
, fontList
,
74 // XmNdefaultButtonShadowThickness, 1, // See comment for wxButton::SetDefault
79 XtAddCallback ((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
) wxButtonCallback
,
82 SetCanAddEventHandler(TRUE
);
85 wxFont
new_font( parent
->GetFont() );
86 GetTextExtent( label1
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
88 wxSize newSize
= size
;
89 if (newSize
.x
== -1) newSize
.x
= 30+x
;
90 if (newSize
.y
== -1) newSize
.y
= 27+y
;
91 SetSize( newSize
.x
, newSize
.y
);
93 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, newSize
.x
, newSize
.y
);
95 ChangeBackgroundColour();
100 void wxButton::SetDefault()
102 wxWindow
*parent
= GetParent();
103 wxPanel
*panel
= wxDynamicCast(panel
, wxPanel
);
105 panel
->SetDefaultItem(this);
107 // We initially do not set XmNdefaultShadowThickness, to have small buttons.
108 // Unfortunately, buttons are now mis-aligned. We try to correct this
109 // now -- setting this ressource to 1 for each button in the same row.
110 // Because it's very hard to find wxButton in the same row,
111 // correction is straighforward: we set resource for all wxButton
112 // in this parent (but not sub panels)
113 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
115 wxButton
*item
= (wxButton
*) node
->Data ();
116 if (item
->IsKindOf(CLASSINFO(wxButton
)))
118 bool managed
= XtIsManaged((Widget
) item
->GetMainWidget());
120 XtUnmanageChild ((Widget
) item
->GetMainWidget());
122 XtVaSetValues ((Widget
) item
->GetMainWidget(),
123 XmNdefaultButtonShadowThickness
, 1,
127 XtManageChild ((Widget
) item
->GetMainWidget());
131 // XtVaSetValues((Widget)handle, XmNshowAsDefault, 1, NULL);
132 XtVaSetValues ((Widget
) parent
->GetMainWidget(), XmNdefaultButton
, (Widget
) GetMainWidget(), NULL
);
136 wxSize
wxButton::GetDefaultSize()
138 // TODO: check font size as in wxMSW ? MB
140 return wxSize(80,26);
143 void wxButton::Command (wxCommandEvent
& event
)
145 ProcessCommand (event
);
148 void wxButtonCallback (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
150 if (!wxGetWindowFromTable(w
))
151 // Widget has been deleted!
154 wxButton
*item
= (wxButton
*) clientData
;
155 wxCommandEvent
event (wxEVT_COMMAND_BUTTON_CLICKED
, item
->GetId());
156 event
.SetEventObject(item
);
157 item
->ProcessCommand (event
);
160 void wxButton::ChangeFont(bool keepOriginalSize
)
162 wxWindow::ChangeFont(keepOriginalSize
);
165 void wxButton::ChangeBackgroundColour()
167 DoChangeBackgroundColour(m_mainWidget
, m_backgroundColour
, TRUE
);
170 void wxButton::ChangeForegroundColour()
172 wxWindow::ChangeForegroundColour();