Back to index

plt-scheme  4.2.1
Button.cc
Go to the documentation of this file.
00001 /*                                                      -*- C++ -*-
00002  *
00003  * Purpose: button panel item
00004  *
00005  * Authors: Markus Holzem and Julian Smart
00006  *
00007  * Copyright: (C) 2004-2009 PLT Scheme Inc.
00008  * Copyright: (C) 1995, AIAI, University of Edinburgh (Julian)
00009  * Copyright: (C) 1995, GNU (Markus)
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00027 #ifdef __GNUG__
00028 #pragma implementation "Button.h"
00029 #endif
00030 
00031 #define  Uses_XtIntrinsic
00032 #define  Uses_wxButton
00033 #include "wx.h"
00034 #define  Uses_ButtonWidget
00035 #define  Uses_TraversingEnforcerWidget
00036 #include "widgets.h"
00037 
00038 //-----------------------------------------------------------------------------
00039 // create and destroy button
00040 //-----------------------------------------------------------------------------
00041 
00042 wxButton::wxButton(wxPanel *panel, wxFunction function, char *label,
00043                  int x, int y, int width, int height,
00044                  long style, wxFont *_font, char *name) : wxItem(_font)
00045 {
00046     __type = wxTYPE_BUTTON;
00047 
00048     Create(panel, function, label, x, y, width, height, style, name);
00049 }
00050 
00051 wxButton::wxButton(wxPanel *panel, wxFunction function, wxBitmap *bitmap,
00052                  int x, int y, int width, int height,
00053                  long style, wxFont *_font, char *name) : wxItem(_font)
00054 {
00055     __type = wxTYPE_BUTTON;
00056 
00057     Create(panel, function, bitmap, x, y, width, height, style, name);
00058 }
00059 
00060 Bool wxButton::Create(wxPanel *panel, wxFunction function, char *label,
00061                     int x, int y, int width, int height,
00062                     long style, char *name)
00063 {
00064     wxWindow_Xintern *ph;
00065     Widget wgt;
00066 
00067     ChainToPanel(panel, style, name);
00068 
00069     label = wxGetCtlLabel(label);
00070     bm_label = NULL;
00071 
00072     ph = parent->GetHandle();
00073 
00074     // create frame
00075     wgt = XtVaCreateWidget
00076        (name, xfwfTraversingEnforcerWidgetClass, ph->handle,
00077         XtNbackground,  wxGREY_PIXEL,
00078         XtNforeground,  wxBLACK_PIXEL,
00079         XtNhighlightColor, wxCTL_HIGHLIGHT_PIXEL,
00080         XtNfont,        font->GetInternalFont(),
00081 #ifdef WX_USE_XFT
00082         XtNxfont,       font->GetInternalAAFont(),
00083 #endif
00084         XtNshrinkToFit, (width < 0 || height < 0),
00085         XtNframeWidth,  (style & wxBORDER) ? 2 : 0,
00086         XtNframeType,   XfwfSunken,
00087         NULL);
00088     if (!(style & wxINVISIBLE))
00089       XtManageChild(wgt);
00090     else
00091       XtRealizeWidget(wgt);
00092     X->frame = wgt;
00093     // create widget
00094     wgt = XtVaCreateManagedWidget
00095        ("button", xfwfButtonWidgetClass, X->frame,
00096         XtNlabel,       label,
00097         XtNbackground,  wxBUTTON_PIXEL,
00098         XtNforeground,  wxBLACK_PIXEL,
00099         XtNfont,        font->GetInternalFont(),
00100 #ifdef WX_USE_XFT
00101         XtNxfont,       font->GetInternalAAFont(),
00102 #endif
00103         XtNshrinkToFit, (width < 0 || height < 0),
00104         XtNhighlightThickness, 0,
00105         XtNtraversalOn, FALSE,
00106         NULL);
00107     X->handle = wgt;
00108     // propagate key events from frame to button widget
00109     XtVaSetValues(X->frame, XtNpropagateTarget, X->handle, NULL);
00110     // set data declared in wxItem
00111     callback = function;
00112     XtAddCallback(X->handle, XtNactivate, wxButton::EventCallback, (XtPointer)saferef);
00113 
00114     panel->PositionItem(this, x, y, width, height);
00115     AddEventHandlers();
00116     AllowResize(FALSE);
00117 
00118     if (style & wxINVISIBLE)
00119       Show(FALSE);
00120 
00121     return TRUE;
00122 }
00123 
00124 Bool wxButton::Create(wxPanel *panel, wxFunction function, wxBitmap *bitmap,
00125                     int x, int y, int width, int height,
00126                     long style, char *name)
00127 {
00128     wxWindow_Xintern *ph;
00129     Pixmap pm, mpm;
00130     Widget wgt;
00131 
00132     if (!bitmap->Ok() || (bitmap->selectedIntoDC < 0))
00133       return Create(panel, function, "<bad-image>", x, y, width, height, style, name);
00134 
00135     bitmap->selectedIntoDC++;
00136     bm_label = bitmap;
00137 
00138     bm_label_mask = CheckMask(bm_label);
00139 
00140     ChainToPanel(panel, style, name);
00141 
00142     ph = parent->GetHandle();
00143 
00144     // create frame
00145     wgt = XtVaCreateWidget
00146        (name, xfwfTraversingEnforcerWidgetClass, ph->handle,
00147         XtNbackground,  wxGREY_PIXEL,
00148         XtNforeground,  wxBLACK_PIXEL,
00149         XtNhighlightColor, wxCTL_HIGHLIGHT_PIXEL,
00150         XtNfont,        font->GetInternalFont(),
00151         XtNshrinkToFit, (width < 0 || height < 0),
00152         XtNframeWidth,  (style & wxBORDER) ? 2 : 0,
00153         XtNframeType,   XfwfSunken,
00154         NULL);
00155     if (!(style & wxINVISIBLE))
00156       XtManageChild(wgt);
00157     else
00158       XtRealizeWidget(wgt);
00159     X->frame = wgt;
00160     // create widget
00161     pm = (Pixmap)bitmap->GetLabelPixmap(TRUE);
00162     if (bm_label_mask)
00163       mpm = GETPIXMAP(bm_label_mask);
00164     else
00165       mpm = 0;
00166     wgt = XtVaCreateManagedWidget
00167        ("button", xfwfButtonWidgetClass, X->frame,
00168         XtNpixmap,      pm,
00169         XtNmaskmap,     mpm,
00170         XtNbackground,  wxBUTTON_PIXEL,
00171         XtNforeground,  wxBLACK_PIXEL,
00172         XtNfont,        font->GetInternalFont(),
00173         XtNshrinkToFit, (width < 0 || height < 0),
00174         XtNhighlightThickness, 0, XtNtraversalOn, FALSE,
00175         NULL);
00176     X->handle = wgt;
00177     // propagate key events from frame to button widget
00178     XtVaSetValues(X->frame, XtNpropagateTarget, X->handle, NULL);
00179     // set data declared in wxItem
00180     callback = function;
00181     XtAddCallback(X->handle, XtNactivate, wxButton::EventCallback, (XtPointer)saferef);
00182 
00183     panel->PositionItem(this, x, y, width, height);
00184     AddEventHandlers();
00185     AllowResize(FALSE);
00186 
00187     if (style & wxINVISIBLE)
00188       Show(FALSE);
00189 
00190     return TRUE;
00191 }
00192 
00193 wxButton::~wxButton(void)
00194 {
00195   if (bm_label) {
00196     --bm_label->selectedIntoDC;
00197     bm_label->ReleaseLabel();
00198     XtVaSetValues(X->handle, XtNpixmap, NULL, XtNmaskmap, NULL, NULL);
00199   }
00200   if (bm_label_mask) {
00201     --bm_label_mask->selectedIntoDC;
00202   }
00203 }
00204 
00205 //-----------------------------------------------------------------------------
00206 // alternate SetLabel for changing bitmap
00207 //-----------------------------------------------------------------------------
00208 
00209 void wxButton::AllowResize(Bool allow)
00210 {
00211     XtVaSetValues(X->handle, XtNshrinkToFit, allow, NULL);
00212 }
00213 
00214 void wxButton::SetAlignment(long alignment)
00215 {
00216     XtVaSetValues(X->handle, XtNalignment, alignment, NULL);
00217 }
00218 
00219 void wxButton::SetDefault(void)
00220 {
00221     ((wxPanel*)parent)->default_item = this;
00222 }
00223 
00224 void wxButton::SetLabel(char *label)
00225 {
00226   if (!bm_label) {
00227     label = wxGetCtlLabel(label);
00228 
00229     XtVaSetValues(X->handle, XtNlabel, label, NULL);
00230   }
00231 }
00232 
00233 void wxButton::SetLabel(wxBitmap *bitmap)
00234 {
00235   if (bm_label && bitmap && bitmap->Ok() && (bitmap->selectedIntoDC >= 0)
00236       && (bitmap->GetDepth()==1 || bitmap->GetDepth()==wxDisplayDepth())) {
00237     Pixmap pm, mpm;
00238 
00239     --bm_label->selectedIntoDC;
00240     bm_label->ReleaseLabel();
00241     if (bm_label_mask) {
00242       --bm_label_mask->selectedIntoDC;
00243       bm_label_mask = NULL;
00244     }
00245 
00246     bm_label = bitmap;
00247     bm_label->selectedIntoDC++;
00248 
00249     bm_label_mask = CheckMask(bm_label);
00250 
00251     pm = (Pixmap)bitmap->GetLabelPixmap(TRUE);
00252     if (bm_label_mask)
00253       mpm = GETPIXMAP(bm_label_mask);
00254     else
00255       mpm = 0;
00256 
00257     XtVaSetValues(X->handle, XtNpixmap, pm, XtNmaskmap, mpm, NULL);
00258   }
00259 }
00260 
00261 char *wxButton::GetLabel(void)
00262 {
00263     char *label = NULL;
00264     
00265     if (!X->handle) // forbid, if no widget associated
00266        return NULL;
00267 
00268     XtVaGetValues(X->handle, XtNlabel, &label, NULL);
00269     return label;
00270 }
00271 
00272 void wxButton::ChangeToGray(Bool gray)
00273 {
00274   if (!X->handle) // forbid, if no widget associated
00275     return;
00276 
00277   wxItem::ChangeToGray(gray);
00278 
00279   if (gray) {
00280     /* Un-click it, if during a click */
00281     XtVaSetValues(X->handle, XtNframeType, XfwfRaised, NULL);
00282   }
00283 }
00284 
00285 void wxButton::SetBorder(Bool on)
00286 {
00287   XtVaSetValues(X->frame, XtNframeType, on ? XfwfSunken : XfwfNothing, NULL);
00288 }
00289 
00290 //-----------------------------------------------------------------------------
00291 // do the same as if button was clicked
00292 //-----------------------------------------------------------------------------
00293 
00294 void wxButton::Command(wxCommandEvent *event)
00295 {
00296     ProcessCommand(event);
00297 }
00298 
00299 //-----------------------------------------------------------------------------
00300 // callback for commandWidgetClass
00301 //-----------------------------------------------------------------------------
00302 
00303 void wxButton::EventCallback(Widget WXUNUSED(w), XtPointer clientData,
00304                           XtPointer WXUNUSED(ptr))
00305 {
00306     wxButton       *button = (wxButton*)GET_SAFEREF(clientData);
00307     wxCommandEvent *event;
00308 
00309     if (button) {
00310       event = new wxCommandEvent(wxEVENT_TYPE_BUTTON_COMMAND);
00311       
00312       button->ProcessCommand(event);
00313     }
00314 
00315 #ifdef MZ_PRECISE_GC
00316     XFORM_RESET_VAR_STACK;
00317 #endif
00318 }