FLTK logo

Re: Refresh displaying ?

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Refresh displaying ? Greg Ercolano Aug 15, 2005  
 
	Take a look at the "Drawing Things in FLTK" documentation:
	http://fltk.org/documentation.php/doc-1.1/drawing.html#drawing
	..in particular the part about "when can you draw things in fltk".

	In your DrawFn class, you have a draw() method defined, but no
	code in it:

>   private:
>      void draw(){}

	In the {}'s, put the code that draws your text, then it should work.

	Add a string in the class (eg. 'char *sometext;'), then set the string
	via your button callback function, and tell the widget to redraw()..
	that should do it.

	Since your button callback needs access to both the input and the custom
	display widget to do it's job, it's probably best to make an 'Application'
	class, and put all the fltk widgets and callbacks in there, so the callback
	can access everything it needs.

	Here's my rewrite of your app:

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Box.H>
#include <string.h>

#define XBOX 10
#define YBOX 10

class MyDisplay : public Fl_Box {
    const char *sometext;
    void draw() {
        Fl_Box::draw();
        fl_color(FL_RED);
        fl_font(FL_HELVETICA, 12);
        fl_push_no_clip();
        if ( sometext ) fl_draw(sometext, XBOX + x() + fl_descent(), YBOX + y() + fl_height() - fl_descent());
        fl_pop_clip();
    }
public:
    MyDisplay(int x, int y, int w, int h, const char *l = 0):Fl_Box(x, y, w, h, l) {
        sometext = 0;
    }
    ~MyDisplay() {
        SetText(0);
    }
    void SetText(const char *val) {
        if ( sometext ) free((void*)sometext);
        sometext = (val) ? strdup(val) : 0;
    }
};

class Application {
    // All the fltk widgets here, so but_cb() can access them
    Fl_Double_Window *win;
    Fl_Input         *input;
    Fl_Button        *button;
    MyDisplay        *disp;
    // Button callback snapshots current contents of input widget to display
    static void Button_CB(Fl_Widget*, void* data) {
        Application *o = (Application*)data;
        o->disp->SetText( o->input->value() );
        o->disp->redraw();
    }
public:
    Application(int w, int h, const char *l, int argc, char *argv[]) {
        win = new Fl_Double_Window(w,h,l);
        disp = new MyDisplay(XBOX,YBOX,300-(XBOX*2),100);
        disp->box(FL_DOWN_BOX);
        disp->color((Fl_Color) FL_WHITE);
        input = new Fl_Input(10,140,230,30);
        button = new Fl_Button(10,170,230,30, "Display !");
        button->callback(Button_CB, this);
        win->show(argc, argv);
    }
};

int main (int argc, char *argv[]) {
    Application(300, 280, "Display Text", argc, argv);
    return Fl::run();
}

// END

Is anybody would tell me how to refresh/erase displaying when a new one occurs ?

	Note the call to redraw() in the button callback.

Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.