| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: bmpbuttn.cpp |
| 3 | // Purpose: wxBitmapButton |
| 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 13 | #pragma implementation "bmpbuttn.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __VMS |
| 20 | #define XtScreen XTSCREEN |
| 21 | #endif |
| 22 | |
| 23 | #include "wx/defs.h" |
| 24 | |
| 25 | #include "wx/bmpbuttn.h" |
| 26 | |
| 27 | #ifdef __VMS__ |
| 28 | #pragma message disable nosimpint |
| 29 | #endif |
| 30 | #include <Xm/PushBG.h> |
| 31 | #include <Xm/PushB.h> |
| 32 | #ifdef __VMS__ |
| 33 | #pragma message enable nosimpint |
| 34 | #endif |
| 35 | |
| 36 | #include "wx/motif/private.h" |
| 37 | |
| 38 | // Implemented in button.cpp |
| 39 | void wxButtonCallback (Widget w, XtPointer clientData, XtPointer ptr); |
| 40 | |
| 41 | // Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap ); |
| 42 | |
| 43 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) |
| 44 | |
| 45 | wxBitmapButton::wxBitmapButton() |
| 46 | { |
| 47 | m_marginX = m_marginY = wxDEFAULT_BUTTON_MARGIN; |
| 48 | m_insensPixmap = (WXPixmap) 0; |
| 49 | } |
| 50 | |
| 51 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, |
| 52 | const wxBitmap& bitmap, |
| 53 | const wxPoint& pos, |
| 54 | const wxSize& size, long style, |
| 55 | const wxValidator& validator, |
| 56 | const wxString& name) |
| 57 | { |
| 58 | if( !CreateControl( parent, id, pos, size, style, validator, name ) ) |
| 59 | return false; |
| 60 | |
| 61 | m_bmpNormal = m_bmpNormalOriginal = bitmap; |
| 62 | m_bmpSelected = m_bmpSelectedOriginal = bitmap; |
| 63 | |
| 64 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
| 65 | |
| 66 | /* |
| 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. |
| 74 | */ |
| 75 | Widget buttonWidget = XtVaCreateManagedWidget ("button", |
| 76 | |
| 77 | // Gadget causes problems for default button operation. |
| 78 | #if wxUSE_GADGETS |
| 79 | xmPushButtonGadgetClass, parentWidget, |
| 80 | #else |
| 81 | xmPushButtonWidgetClass, parentWidget, |
| 82 | #endif |
| 83 | // See comment for wxButton::SetDefault |
| 84 | // XmNdefaultButtonShadowThickness, 1, |
| 85 | XmNrecomputeSize, False, |
| 86 | NULL); |
| 87 | |
| 88 | m_mainWidget = (WXWidget) buttonWidget; |
| 89 | |
| 90 | ChangeFont(FALSE); |
| 91 | |
| 92 | ChangeBackgroundColour (); |
| 93 | |
| 94 | DoSetBitmap(); |
| 95 | |
| 96 | XtAddCallback (buttonWidget, |
| 97 | XmNactivateCallback, (XtCallbackProc) wxButtonCallback, |
| 98 | (XtPointer) this); |
| 99 | |
| 100 | wxSize best = m_bmpNormal.Ok() ? GetBestSize() : wxSize(30, 30); |
| 101 | if( size.x != -1 ) best.x = size.x; |
| 102 | if( size.y != -1 ) best.y = size.y; |
| 103 | |
| 104 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
| 105 | pos.x, pos.y, best.x, best.y); |
| 106 | |
| 107 | return TRUE; |
| 108 | } |
| 109 | |
| 110 | wxBitmapButton::~wxBitmapButton() |
| 111 | { |
| 112 | SetBitmapLabel(wxNullBitmap); |
| 113 | |
| 114 | if (m_insensPixmap) |
| 115 | XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()), |
| 116 | (Pixmap) m_insensPixmap); |
| 117 | } |
| 118 | |
| 119 | void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap) |
| 120 | { |
| 121 | m_bmpNormalOriginal = bitmap; |
| 122 | m_bmpNormal = bitmap; |
| 123 | |
| 124 | DoSetBitmap(); |
| 125 | } |
| 126 | |
| 127 | void wxBitmapButton::SetBitmapSelected(const wxBitmap& sel) |
| 128 | { |
| 129 | m_bmpSelected = sel; |
| 130 | m_bmpSelectedOriginal = sel; |
| 131 | |
| 132 | DoSetBitmap(); |
| 133 | }; |
| 134 | |
| 135 | void wxBitmapButton::SetBitmapFocus(const wxBitmap& focus) |
| 136 | { |
| 137 | m_bmpFocus = focus; |
| 138 | // Not used in Motif |
| 139 | }; |
| 140 | |
| 141 | void wxBitmapButton::SetBitmapDisabled(const wxBitmap& disabled) |
| 142 | { |
| 143 | m_bmpDisabled = disabled; |
| 144 | m_bmpDisabledOriginal = disabled; |
| 145 | |
| 146 | DoSetBitmap(); |
| 147 | }; |
| 148 | |
| 149 | void wxBitmapButton::DoSetBitmap() |
| 150 | { |
| 151 | if (m_bmpNormalOriginal.Ok()) |
| 152 | { |
| 153 | Pixmap pixmap = 0; |
| 154 | Pixmap insensPixmap = 0; |
| 155 | Pixmap armPixmap = 0; |
| 156 | |
| 157 | // Must re-make the bitmap to have its transparent areas drawn |
| 158 | // in the current widget background colour. |
| 159 | if (m_bmpNormalOriginal.GetMask()) |
| 160 | { |
| 161 | int backgroundPixel; |
| 162 | XtVaGetValues((Widget) m_mainWidget, |
| 163 | XmNbackground, &backgroundPixel, |
| 164 | NULL); |
| 165 | |
| 166 | wxColour col; |
| 167 | col.SetPixel(backgroundPixel); |
| 168 | |
| 169 | wxBitmap newBitmap = |
| 170 | wxCreateMaskedBitmap(m_bmpNormalOriginal, col); |
| 171 | m_bmpNormal = newBitmap; |
| 172 | m_bitmapCache.SetBitmap( m_bmpNormal ); |
| 173 | |
| 174 | pixmap = (Pixmap) m_bmpNormal.GetDrawable(); |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | m_bitmapCache.SetBitmap( m_bmpNormal ); |
| 179 | pixmap = (Pixmap) m_bitmapCache.GetLabelPixmap(m_mainWidget); |
| 180 | } |
| 181 | |
| 182 | if (m_bmpDisabledOriginal.Ok()) |
| 183 | { |
| 184 | if (m_bmpDisabledOriginal.GetMask()) |
| 185 | { |
| 186 | int backgroundPixel; |
| 187 | XtVaGetValues((Widget) m_mainWidget, |
| 188 | XmNbackground, &backgroundPixel, |
| 189 | NULL); |
| 190 | |
| 191 | wxColour col; |
| 192 | col.SetPixel(backgroundPixel); |
| 193 | |
| 194 | wxBitmap newBitmap = |
| 195 | wxCreateMaskedBitmap(m_bmpDisabledOriginal, col); |
| 196 | m_bmpDisabled = newBitmap; |
| 197 | |
| 198 | insensPixmap = (Pixmap) m_bmpDisabled.GetDrawable(); |
| 199 | } |
| 200 | else |
| 201 | insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget); |
| 202 | } |
| 203 | else |
| 204 | insensPixmap = (Pixmap) m_bitmapCache.GetInsensPixmap(m_mainWidget); |
| 205 | |
| 206 | // Now make the bitmap representing the armed state |
| 207 | if (m_bmpSelectedOriginal.Ok()) |
| 208 | { |
| 209 | if (m_bmpSelectedOriginal.GetMask()) |
| 210 | { |
| 211 | int backgroundPixel; |
| 212 | XtVaGetValues((Widget) m_mainWidget, |
| 213 | XmNarmColor, &backgroundPixel, |
| 214 | NULL); |
| 215 | |
| 216 | wxColour col; |
| 217 | col.SetPixel(backgroundPixel); |
| 218 | |
| 219 | wxBitmap newBitmap = |
| 220 | wxCreateMaskedBitmap(m_bmpSelectedOriginal, col); |
| 221 | m_bmpSelected = newBitmap; |
| 222 | |
| 223 | armPixmap = (Pixmap) m_bmpSelected.GetDrawable(); |
| 224 | } |
| 225 | else |
| 226 | armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget); |
| 227 | } |
| 228 | else |
| 229 | armPixmap = (Pixmap) m_bitmapCache.GetArmPixmap(m_mainWidget); |
| 230 | |
| 231 | XtVaSetValues ((Widget) m_mainWidget, |
| 232 | XmNlabelPixmap, pixmap, |
| 233 | XmNlabelInsensitivePixmap, insensPixmap, |
| 234 | XmNarmPixmap, armPixmap, |
| 235 | XmNlabelType, XmPIXMAP, |
| 236 | NULL); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | // Null bitmap: must not use current pixmap |
| 241 | // since it is no longer valid. |
| 242 | XtVaSetValues ((Widget) m_mainWidget, |
| 243 | XmNlabelType, XmSTRING, |
| 244 | XmNlabelPixmap, XmUNSPECIFIED_PIXMAP, |
| 245 | XmNlabelInsensitivePixmap, XmUNSPECIFIED_PIXMAP, |
| 246 | XmNarmPixmap, XmUNSPECIFIED_PIXMAP, |
| 247 | NULL); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void wxBitmapButton::ChangeBackgroundColour() |
| 252 | { |
| 253 | wxDoChangeBackgroundColour(m_mainWidget, m_backgroundColour, TRUE); |
| 254 | |
| 255 | // Must reset the bitmaps since the colours have changed. |
| 256 | DoSetBitmap(); |
| 257 | } |
| 258 | |
| 259 | wxSize wxBitmapButton::DoGetBestSize() const |
| 260 | { |
| 261 | wxSize ret( 30,30 ); |
| 262 | |
| 263 | if (m_bmpNormal.Ok()) |
| 264 | { |
| 265 | int border = (GetWindowStyle() & wxNO_BORDER) ? 4 : 10; |
| 266 | ret.x = m_bmpNormal.GetWidth()+border; |
| 267 | ret.y = m_bmpNormal.GetHeight()+border; |
| 268 | } |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |