Michael Schmid wrote:
> Is there any way to have a Fl_Button with a image in it and a text on
> this image? The text should be changeable, so I can't put the text onto
> the picture directly... :-(
This should work.
Supply an image that is 100x100 (/var/tmp/foo.jpg), and it
will fit perfectly in the button. Smaller images will center,
larger images will clip.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/fl_draw.H>
//
// Demonstrate button with text over image for fltk 1.1.x and older -- erco
09/29/09
//
class MyButton : public Fl_Button {
void draw() {
int X = x()+Fl::box_dx(box()); // area inside the button's frame
int Y = y()+Fl::box_dy(box());
int W = w()-Fl::box_dw(box());
int H = h()-Fl::box_dh(box());
draw_box(value()?(down_box()?down_box():fl_down(box())):box(),
color()); // button up/dn
fl_push_clip(X,Y,W,H); // clip inside button's frame
{
if ( image() ) {
int imgx = X + ((image()->w()<W) ? (W-image()->w())/2 : 0);
// horiz center
int imgy = Y + ((image()->h()<H) ? (H-image()->h())/2 : 0);
// vert center
image()->draw(imgx,imgy);
}
if ( label() ) {
fl_color(labelcolor());
fl_font(labelfont(),labelsize());
fl_draw(label(),x(),y(),w(),h(),align());
}
}
fl_pop_clip();
}
public:
// CONSTRUCTOR
MyButton(int X,int Y,int W,int H,const char*L=0) : Fl_Button(X,Y,W,H,L) {
}
};
int main() {
fl_register_images(); // initialize image lib
Fl_Window win(720,486); // make a window
MyButton but(10,10,104,104,"Test"); // button that will contain
image
but.labelfont(FL_HELVETICA_BOLD); // make button font bold
but.labelsize(20); // make large font
Fl_JPEG_Image jpg("/var/tmp/foo.jpg"); // get an image (100x100 or
smaller in this case)
but.image(jpg); // attach jpg image to button
win.show();
return(Fl::run());
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk