Separation of presentation and functionality when creating software  
applications, a simple and light library and API.

I think FLTK is a good candidate, bellow is a functional minimal proof of  
the concept.

I wonder how much someone else can improve it, preserving it's simplicity  
and easy of use !


---------

#include <FL/Fl.H>
#include <FL/Fl_Double_window.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Int_Input.H>
#include <FL/Fl_Float_Input.H>
#include <FL/Fl_Button.H>

//the idea is to make a repository of common widgets to be used

#define MY_WIDGET_WRAPPER0(klass) \
class My##klass : public klass { \
        public: \
        typedef My##klass THISCLASS; \
        My##klass(int X, int Y, int W, int H, const char *  
L=0):klass(X,Y,W,H,L){} \
     My##klass(const char *  avalue=0):klass(0,0,100,25){if(avalue)  
label(avalue);} \
}

#define MY_WIDGET_WRAPPER2(klass, assign_type, value_suffix) \
class My##klass : public klass { \
        public: \
        typedef My##klass THISCLASS; \
        My##klass(int X, int Y, int W, int H, const char *  
L=0):klass(X,Y,W,H,L){} \
     My##klass(assign_type avalue=0):klass(0,0,100,25){if(avalue)  
assign(avalue);} \
     My##klass& assign(assign_type  
avalue){this->value##value_suffix(avalue); \
         return *this;} \
     My##klass& operator=(assign_type avalue){return assign(avalue);} \
}

#define MY_WIDGET_WRAPPER(klass, assign_type) \
     MY_WIDGET_WRAPPER2(klass, assign_type,)

MY_WIDGET_WRAPPER0(Fl_Double_Window);
MY_WIDGET_WRAPPER0(Fl_Box);
MY_WIDGET_WRAPPER0(Fl_Button);
MY_WIDGET_WRAPPER(Fl_Input, const char *);
MY_WIDGET_WRAPPER2(Fl_Int_Input, int, _int);
MY_WIDGET_WRAPPER2(Fl_Float_Input, double, _float);

#define MY_LABEL(name, label)\
struct MyLabel##name : public MyFl_Box { MyLabel##name():MyFl_Box(label)  
{} }

MY_LABEL(Age, "Age");
MY_LABEL(Name, "Name");

#define MY_BUTTON(name, label)\
struct MyButton##name : public MyFl_Button {  
MyButton##name():MyFl_Button(label) {} }

MY_BUTTON(Save, "Save");

//Experiments to separate presentation from functionality

//Functionality and data
struct MyClassData
{
     MyFl_Int_Input age;
     MyFl_Input name;

     void save()
     {
         //here the saving procedures
     }
     void load()
     {
         //here the loading procedures
     }
};

//Presentation
struct MyClassPresentation
{
     MyFl_Double_Window   win;

     MyLabelAge label_age;
     MyLabelName label_name;
     MyButtonSave btn_save;

     MyClassData data;

     Fl_End  win_end;

     MyClassPresentation(const char *title=0)
     {
         if(title) win.label(title);
         else win.label("MyClass");
         //btn_save.action(data.save);
     }
     void layout(/*a layout as paramter*/)
     {
         //ideally we should receive an asii description
         //like "EvaLayoutManager description"
         //without any/or few magic numbers
         /*
             rc  ,       A    ,   A   ,
             A   ,label_age   , age   ,
             A   ,label_name  , name  ,
             A   ,btn_save    ,       ,
         */

         //bellow is what I want to remove from my applications
         //need a LayoutManager for it ?
         int gap = 5;
         Fl_Widget *wdg = &label_age;
         wdg->resize(gap, gap, 90, 25);
         data.age.resize(wdg->x()+wdg->w(), wdg->y(), wdg->w(), wdg->h());
         label_name.resize(wdg->x(), data.age.y()+data.age.h()+gap,  
wdg->w(), wdg->h());
         wdg = &label_name;
         data.name.resize(wdg->x()+wdg->w(), wdg->y(), wdg->w(), wdg->h());
         btn_save.resize(wdg->x(), data.name.y() + data.name.h() + gap,  
wdg->w(), wdg->h());
         win.x(200);
         win.y(200);
         win.w(200);
         win.h(200);
         //win.resize(200,200,200,200);
         win.resizable(win);
     }
};

int main(int argc, char *argv[])
{
     MyClassPresentation mc("FLTK Simple");
     mc.layout(/*a layout description as parameter*/);

     mc.data.load(); //load data
     mc.data.age = 50; //do some manipulation with it
     mc.data.name = "FLTK";
     mc.data.save(); //save the work

     mc.win.show(argc, argv);
     return Fl::run();
}
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to