Intro

Introduction

On the stack or the heap

#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_PNG_Image.H> // fltk-config --use-images --compile
#include <FL/Fl.H>

using namespace std;
/*
int main(){
    Fl_Window win(300,300);
    win.begin();
    Fl_Button but(20,20,70,40);
    win.end();

    Fl_PNG_Image pic("flag.png");

    but.image(pic.copy(40,40));
    win.show();
    Fl::run();

}
*/

int main(){
    Fl_Window* win = new Fl_Window(300,300);
    win->begin();
    Fl_Button* but = new Fl_Button(20,20,70,40);
    win->end();
    Fl_PNG_Image*  pic = new Fl_PNG_Image("flag.png");
    but->image(pic->copy(40,40));
    win->show();
    Fl::run();
}

Simple callback

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
using namespace std;

//--------------------------------------------
void but_cb( Fl_Widget* o  ) {
   Fl_Button* b=(Fl_Button*)o;
   b->label("Good job"); //redraw not necessary
   b->resize(10,150,140,30); //redraw needed
   b->redraw();
}

//--------------------------------------------
int main() {
    Fl_Window win( 300,200,"Testing" );
    win.begin();
    Fl_Button but( 10, 150, 70, 30, "Click me" );
    win.end();
    but.callback( but_cb );
    win.show();
    return Fl::run();
}

2D vector of widgets. Passing info to callbacks using optional user_data

#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl.H>
#include <vector>
#include <stdio.h> //for spintf

using namespace std;

void foo(Fl_Widget* wid, void* v){
    int* i=(int*)v;
    char s[8];
    sprintf(s, "%d", *i); //convert integer to cstring
    wid->copy_label(s); //must use copy_label as char s does not exist after foo
}

int main(){
    vector <vector <Fl_Button*> > buts;

    int ar[16];
    for (int x=0; x<16; x++)
        ar[x]=x; //[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

    int x=0;
    Fl_Window win(300,300);
    win.begin();
    for (int r=0; r<4; r++)
    {
        vector <Fl_Button*> temp;
        for (int c=0; c<4; c++)
        {
            temp.push_back(new Fl_Button(c*30, r*30, 30, 30));
            temp.back()->callback(foo, ar+x); //pointer arithmetic
            x++;
        }
        buts.push_back(temp);
    }
    win.end();
    win.show();
    Fl::run();
}

Utilizing overloaded callback which takes longs as user_data. Also resize vector instead of push_back.

#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl.H>
#include <vector>
#include <string> //for to_string

using namespace std;

void foo(Fl_Widget* wid, long y){
    string s= to_string(y);
    wid->copy_label(s.c_str());
    // if using c++ string. Use .c_str() to convert C++ to Cstring
}

int main(){
    vector <vector <Fl_Button*> > buts;
    buts.resize(4, vector <Fl_Button*> (4)); //use push_back if no resize

    long x=0;
    Fl_Window win(300,300);
    win.begin();
    for (int r=0; r<4; r++)
    {
        for (int c=0; c<4; c++)
        {
            buts[r][c]=new Fl_Button(c*40, r*40, 40, 40);
            buts[r][c]->callback(foo, x); //this overloaded callback accepts a long
            x++;
        }
    }
    win.end();
    win.show();
    Fl::run();
}