I am starting to port a very convenient and extremely space saving feature from 
FLTK 2 named Styles.

Even though FLTK Widgets are generally lightweight, the still haul a bunch of 
redundant information around. Every widget has an entry for its label font, 
color, size, box type, etc. etc. . Most of the time, these values are the same 
in an entire application. The font color in Fluid, for example, is pretty much 
always black.

FLTK3 now provides a single default style that will carry all those attributes. 
Every new widget will link to that style, instead of repeating the values over 
and over. Of course, you can change the default style. By changing the font 
size in the default style, you change the default font size throughout the 
application. That can be very neat.

Now, you may have widgets that need, let's say, a red label instead of a black 
one. No problem, just call "myWidget->labelcolor(RED);" and this one widgets 
changes color by automatically generating a private Style class. All other 
attributes btw. still "shine through", meaning that this label still uses the 
default label size. Unless of course you change that too, using 
"myWidget->labelsize(10);".

Of course, widgets can share styles, so you can have a number of widgets with a 
green label, and a number of widgets with a red label. You can build an entire 
hierarchy of styles with branches that change only one or many or all 
attributes.

Going further, there will be default styles for individual widget types. This 
way, a user can change the box type of all sliders from down to up, or 
whichever other attribute. This opens up another huge door: now a Theme 
interface simply changes a few default styles around as needed, and voila, we 
quickly generated a new theme.

Now for the cost: I am using a caching scheme to avoid climbing up the style 
hierarchy every time I need a single attribute. So performance wise, we have 
one more indirection - that's all (if I remember correctly, that would be "O" 
instead of "n x O"). In respect to RAM use, Styles should use much less memory 
than the old system! Yay! The Widget API btw. stays the same. All the 
widget->labelfont calls are still there and still work just as expected, only 
there is more smarts under the hood.

Lastly, here is a sample program (which I have not tested ;-)

#include <fltk3/all.h>

fltk3::Style *red_style;
fltk3::Style *blue_stle;

int main(int, void**) {

  red_style = new fltk3::Style(fltk3::default_style);
  red_style->labelcolor(fltk3::RED);
  red_style->labelsize(10);

  blue_style = new fltk3::Style(fltk3::default_style);
  blue_style->labelcolor(fltk3::BLUE);
  blue_style->labelsize(12);

  fltk3::Window *win(400, 400);
  for (int i=0; i<10; i++) {
    fltk3::Button *btn1 = new fltk3::Button(5, 5*60*i, 300, 18, "BLACK");
    // default style is black
    fltk3::Button *btn2 = new fltk3::Button(5, 5*60*i, 300, 18, "RED");
    btn2->style(red_style);
    fltk3::Button *btn3 = new fltk3::Button(5, 5*60*i, 300, 18, "BLUE");
    btn3->style(blue_style);
  }
  win->end();
  win->show();

  return fltk3::run();
}
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to