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