Greg Ercolano wrote:
> Mikko L wrote:
>> Hope this helps somebody.
> 
>     Yes, thanks -- I see now.
> 
>     So it looks like I missed the part about assigning
>     Button::default_style = &G_my_style;
>     ..in main(), and using NamedStyle instead of Style.
> 
>     Referring to your changes, I was able to slim the code
>     a bit more (to keep the code as small as possible) which
>     prevents the need for deriving a custom class.
> 
>     Here's what I ended up with; comments welcome:
> 

Following is bad, because you cant know if Window::default_style is already 
constructed or not.
For example, in my windows box app will crash, because because both 
default_style's are empty.
G_window_style and G_button_style gets constructed before default_style's.
 > NamedStyle G_window_style(*Window::default_style);
 > NamedStyle G_button_style(*Button::default_style);

In fact copy ctor of NamedStyle is bad, cause it will also copy style name
and break Style::find() function.

Instead you should use, if you dont want use revert function.
NamedStyle G_window_style("MyWindowStyle", NULL, NULL);
NamedStyle G_button_style("MyButtonStyle", NULL, NULL);

And then set parent styles before you overwrite default_style's in main.

BR,
Mikko L

----------------------------------------------
Here's working code:

#include <fltk/run.h>
#include <fltk/Window.h>
#include <fltk/Button.h>
#include <fltk/Choice.h>
#include <fltk/Style.h>
#include <stdio.h>
#include <string.h>
using namespace fltk;

//
// FLTK 2.x example showing how to use NamedStyle to change widget defaults
//

// Globals for separate window + button styles
NamedStyle G_window_style("MyWindowStyle", NULL, NULL);
NamedStyle G_button_style("MyButtonStyle", NULL, NULL);


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, "GrayButton"  ) == 0 ) { 
G_button_style.buttoncolor(GRAY80); }
     if ( strcmp(choice, "RedButton"   ) == 0 ) { 
G_button_style.buttoncolor(RED); }
     if ( strcmp(choice, "GrayWindow"  ) == 0 ) { G_window_style.color(GRAY80); 
}
     if ( strcmp(choice, "RedWindow"   ) == 0 ) { G_window_style.color(RED); }
     w->window()->redraw();                              // tell window to 
redraw itself..
}

int main(int argc, char** argv) {
     // Set correct parents for styles
     G_window_style.parent_ = Window::default_style;
     G_button_style.parent_ = Button::default_style;
     // Set our styles as default
     Window::default_style = &G_window_style;
     Button::default_style = &G_button_style;

     // Now do normal widget construction..
     Window w(300,200);
     w.begin();
         Button b(50,10,200,25,"Some Button");
         Button b2(50,40,200,25,"Some Button");
         Button b3(50,70,200,25,"Some Button");
         Choice c(50,100,200,25,"Style");
         c.add("SmallFont");
         c.add("BigFont");
         c.add("RedButton");
         c.add("GrayButton");
         c.add("GrayWindow");
         c.add("RedWindow");
         c.callback(Choice_CB);
     w.end();
     w.show(argc,argv);
     return(run());
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to