Greg Ercolano wrote:
> mal content wrote:
>> See that's the part I didn't get. I thought that I would need
>> to modify some top-level structure in order to get colours, etc,
>> to propagate down, but couldn't work it out from that description.
>
> Hmm, yeah, didn't work for me either.
>
> For lack of any example, I tried to follow my nose on this one
> making a simple app that uses Style to change the defaults of the
> app by changing a chooser.
>
> I implemented it the way it /seemed/ it should work (always a
> mistake ;)
> and without resorting to RTSL.
>
> This is as far as I got.
>
> It compiles and runs but doesn't work; changing the chooser
> doesn't actually change anything about how the app looks.
> (Using latest fltk2.x SVN/fedora3)
>
> Maybe someone who knows FLTK 2.0 can fix this code..?
>
Hi,
Following code works fine (atleast under win32)
I extended example by adding Window default style also.
Hope this helps somebody.
BR,
Mikko L.
#include <fltk/run.h>
#include <fltk/Window.h>
#include <fltk/Button.h>
#include <fltk/Choice.h>
#include <fltk/Style.h>
#include <fltk/Image.h>
#include <fltk/xpmImage.h>
#include <fltk/tiledImage.h>
#include <fltk/draw.h>
#include <stdio.h>
#include <string.h>
//
// FLTK 2.x example showing how to use Style to change defaults
//
using namespace fltk;
// Load window BG image
#include "porsche.xpm" // <-- REPLACE THIS WITH XPM YOU WANT (porsche.xpm is
found in fltk2/test)
static xpmImage window_bg_image(porsche_xpm);
// Simple box type draws tiled image
class ImageBox : public Box {
public:
Image *image;
TiledImage tiled;
void _draw(const Rectangle &r) const {
if (drawflags(INVISIBLE)) return;
if (r.empty()) return;
image->fetch();
if (image->pixeltype() == ARGB32) {
// There might be trasparency, draw background
const Color fg = getcolor();
setcolor(getbgcolor());
fillrect(r);
setcolor(fg);
}
// Draw image
if (tiled.image()) {
tiled.draw(r);
} else {
image->draw(r);
}
}
bool fills_rectangle() const { return true; }
bool is_frame() const { return true; }
ImageBox(const char* n, Image *i, bool tile=false) : Box(n), image(i),
tiled(0) {
if (tile) {
tiled.image(image);
}
}
};
static ImageBox my_window_box("MyTiledImageBox", &window_bg_image, true);
// Default style for Window objects
static void my_window_revert(Style *s)
{
// Setup parent (only if first time called)
if (s->parent_ == Widget::default_style) {
s->parent_ = Window::default_style;
}
// Setup all style attributes
s->box(&my_window_box);
}
static NamedStyle G_window_style("MyWindowStyle", my_window_revert,
&Window::default_style);
// Default style for Button objects
static void my_button_revert(Style *s)
{
// Setup parent (only if first time called)
if (s->parent_ == Widget::default_style) {
s->parent_ = Button::default_style;
}
// Setup all style attributes
s->box(PLASTIC_UP_BOX);
s->buttoncolor(GRAY60);
}
static NamedStyle G_button_style("MyButtonStyle", my_button_revert,
&Button::default_style);
static void Choice_CB(fltk::Widget *w)
{
Choice *c = (Choice*)w;
const char *choice = c->get_item()->label(); // "SmallFont",
"BigFont", etc
printf("You picked: '%s'\n", choice);
if ( strcmp(choice, "SmallFont" ) == 0 ) { G_button_style.labelsize(12);
} // ?
if ( strcmp(choice, "BigFont" ) == 0 ) { G_button_style.labelsize(24);
} // ?
if ( strcmp(choice, "RedButton" ) == 0 ) {
G_button_style.buttoncolor(RED); } // ?
if ( strcmp(choice, "GreenButton" ) == 0 ) {
G_button_style.buttoncolor(GREEN); } // ?
if ( strcmp(choice, "FlatWindowBox" ) == 0 ) {
G_window_style.box(FLAT_BOX); } // ?
if ( strcmp(choice, "Reset" ) == 0 ) { fltk::reset_theme(); }
// ?
w->window()->redraw(); // tell window to redraw itself..
}
int main(int argc, char** argv)
{
// Setup default style pointers here
Button::default_style = &G_button_style;
Window::default_style = &G_window_style;
Window w(300,200);
w.begin();
Button b(50,10,200,30,"Some Button");
Choice c(50,45,200,25,"Style");
c.add("SmallFont");
c.add("BigFont");
c.add("RedButton");
c.add("GreenButton");
c.add("FlatWindowBox");
c.add("Reset");
c.callback(Choice_CB);
w.end();
w.show(argc,argv);
return(run());
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk