Marc R.J. Brevoort wrote:
> I noticed things don't work properly if I set a label, and the 
> Fl_Button::draw() function is small enough to use instead of 
> calling the one of Fl::button, but I'm not sure I anderstand 
> what you're saying. Also, I'm not sure how to draw an image- or would it 
> work just call Fl_Image::draw()? Otherwise, I'd be interested to see how 
> you would solve it.

        Yes, that last example was not written with label()s in mind,
        the idea being the text would be in the image.

        However, it shouldn't be hard to support label text overlay too.
        Yes, that would involve overriding draw().

        I think the following should do the trick.

        Here the images are just buttons without text;
        the text is handled by the button's label()
        which is drawn over the image:


#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Pixmap.H>
#include <FL/fl_draw.H>
//
// Demo of custom button up/down image with label support -erco
//
/* XPM */
static char * up_xpm[] = {
"73 21 17 1",
"       c None",
".      c #373836",
"+      c #474946",
"@      c #565855",
"#      c #5F615E",
"$      c #70726F",
"%      c #7E807D",
"&      c #878986",
"*      c #969895",
"=      c #A1A3A0",
"-      c #B0B2AF",
";      c #BEC0BD",
">      c #CACCC9",
",      c #D7D9D6",
"'      c #E2E4E1",
")      c #EDEFEC",
"!      c #FBFDFA",
")))))));=%$##################################################$%=;))))))))",
")))))>*%=>')!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)'>=%*>))))))",
"))))-&='))))))))))))))))))))))))))))))))))))))))))))))))))))))))'=&-)))))",
")))-&;))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));&-))))",
"))>*-'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))'-*>)))",
"))=*,'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))',*=)))",
");&-''))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))''-&;))",
"'=*;,'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))',;*=')",
"'&-;>'))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))'>;-&')",
"'$;>>>>,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,>>>>;$')",
"'#>>>>,,,,'''''''''''''''''''''''''''''''''''''''''''''''''''',,,,>>>>#')",
"'$;,>,,,,'''''''''''''''''''''''''''''''''''''''''''''''''''''',,,,>,;#')",
"'%-,,,,'''''''''''''''''''''''''''''''''''''''''''''''''''''''''',,,,-%')",
"'**',,'''))))))))))))))))))))))))))))))))))))))))))))))))))))))''',,'**')",
"'-%,''''))))))))))))))))))))))))))))))))))))))))))))))))))))))))'''',%-')",
"',&=))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))=&,))",
")'-%>!)!!!!)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)!!!!)!>%-'))",
")),=&>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!>&=,)))",
"))'>*%-)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)-%*>')))",
")))'>=%%->)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)>-%%=>'))))",
"))))',;=&$$##################################################$$&=;,')))))"};
/* XPM */
static char * down_xpm[] = {
"73 21 17 1",
"       c None",
".      c #4C555E",
"+      c #666D74",
"@      c #61748B",
"#      c #777D83",
"$      c #7C8DA1",
"%      c #738FB1",
"&      c #909494",
"*      c #81A1C9",
"=      c #8AABD4",
"-      c #A5A8A8",
";      c #8EB4E3",
">      c #97BCEC",
",      c #BBC0C2",
"'      c #9CC8FD",
")      c #D2D6D6",
"!      c #EAECEA",
"!!!!!!!,-#+..................................................+#-,!!!!!!!!",
"!!!!!)&+$*;>''''''''''''''''''''''''''''''''''''''''''''''''>;*$+#,!!!!!!",
"!!!!-#$;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;%#-!!!!!",
"!!!-#*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*#-!!!!",
"!!,$%;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;%$,!!!",
"[EMAIL 
PROTECTED];>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;[EMAIL 
PROTECTED]",
"[EMAIL 
PROTECTED];;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;;[EMAIL
 PROTECTED]",
")[EMAIL 
PROTECTED];>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;[EMAIL 
PROTECTED])!",
")@%**;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;**%@)!",
").%****==========================================================*****+)!",
").****====;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;====****.!!",
")+$=*====;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;====*=*+!!",
"!#$====;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;====%#!!",
"!&$;==;;;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;;;==;$&!!",
"!-+=;;;;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;;;;=+-!!",
"!)#%>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%#)!!",
"!!-+*'>''''>''''''''''''''''''''''''''''''''''''''''''''''''>''''>'*+-!!!",
"!!)-#*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*#-)!!!",
"!!!)&#%>''''''''''''''''''''''''''''''''''''''''''''''''''''''''>%#&)!!!!",
"!!!!)-##$*>''''''''''''''''''''''''''''''''''''''''''''''''''>*$##-)!!!!!",
"!!!!!),-&#+..................................................+#&-,)!!!!!!"};
// CUSTOM IMAGE BUTTON CLASS
class MyButton : public Fl_Button {
    Fl_Pixmap *upimage, *dnimage;
public:
    MyButton(int X,int Y,int W,int H, const char*L=0):Fl_Button(X,Y,W,H,L) {
        upimage = dnimage = 0;
    }
    void updown(Fl_Pixmap &up,Fl_Pixmap &dn) {
        upimage = &up;
        dnimage = &dn;
        image(upimage);
        resize(x(),y(),up.w(),up.h());
    }
    int handle(int event) {
        int ret = Fl_Button::handle(event);
        if ( upimage && dnimage ) {
            switch ( event ) {
                case FL_PUSH:
                    if ( dnimage ) {
                        image(dnimage);
                        redraw();
                    }
                    ret = 1;
                    break;
                case FL_LEAVE:
                case FL_UNFOCUS:
                case FL_RELEASE:
                    if ( upimage ) {
                        image(upimage);
                        redraw();
                    }
                    ret = 1;
                    break;
            }
        }
        return(ret);
    }
    void draw() {
        if ( ! upimage ) {
            Fl_Button::draw();
            return;
        }
        // IMAGE? DRAW IT FIRST
        image()->draw(x(), y());
        // LABEL? DRAW IT OVER IMAGE
        if ( label() ) {
            fl_color(labelcolor());
            fl_font(labelfont(), labelsize());
            fl_draw(label(), x(), y(), w(), h(), align(), 0, 1);
        }
        // FOCUS BOX?
        if (Fl::focus() == this) draw_focus();
    }
};
int main(int argc, char **argv) {
    Fl_Pixmap up(up_xpm);
    Fl_Pixmap down(down_xpm);
    Fl_Window win(100,100,"Test");
    win.color(0xedefec00);              // bg color of mac buttons
    MyButton b1(10,10,73,21,"Foo"); b1.updown(up,down); 
b1.clear_visible_focus();
    MyButton b2(10,40,73,21,"Bar"); b2.updown(up,down); 
b2.clear_visible_focus();
    win.end();
    win.show();
    return(Fl::run());
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to