Fl_Offscreen rescaling

8 views
Skip to first unread message

Mo_Al_

unread,
Oct 31, 2021, 2:31:58 AM10/31/21
to fltk.general
Hello
Fl_Offscreen appears to automatically rescale on zoom events (ctrl/+/-) on MacOS, however this doesn't appear to happen on Windows or X11 systems, where basically one has to use Fl::add_handle() to detect zoom events and call fl_rescale_offscreen(). Would be possible to make this work automatically on Windows and X11.

Example code exhibiting this behavior:

#include <FL/Enumerations.H>
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>

class Canvas : public Fl_Box {
int old_x = 0, old_y = 0;
Fl_Offscreen ofs = 0;

public:
Canvas(int x, int y, int w, int h, const char *label = 0)
: Fl_Box(x, y, w, h, label) {}

void draw() {
if (ofs) {
fl_copy_offscreen(x(), y(), w(), h(), ofs, 0, 0);
} else {
ofs = fl_create_offscreen(w(), h());
fl_begin_offscreen(ofs);
fl_rectf(0, 0, w(), h(), FL_WHITE);
fl_end_offscreen();
fl_copy_offscreen(x(), y(), w(), h(), ofs, 0, 0);
}
}

int handle(int ev) {
switch (ev) {
case FL_PUSH: {
old_x = Fl::event_x();
old_y = Fl::event_y();
fl_begin_offscreen(ofs);
fl_color(FL_BLACK);
fl_point(old_x, old_y);
fl_end_offscreen();
redraw();
return 1;
}
case FL_DRAG: {
fl_begin_offscreen(ofs);
fl_color(FL_BLACK);
fl_line(old_x, old_y, Fl::event_x(), Fl::event_y());
fl_end_offscreen();
old_x = Fl::event_x();
old_y = Fl::event_y();
redraw();
return 1;
}
}
return Fl_Box::handle(ev);
}
};

int main() {
auto win = new Fl_Double_Window(400, 300);
auto box = new Canvas(5, 5, 390, 290);
win->end();
win->show();
printf("%d\n", Fl::abi_version());
return Fl::run();
}

If you press ctrl + or ctrl - on windows or x11 you' ll notice the x/y coordinates become wrong, and the size of the offscreen becomes affected as well.

Manolo

unread,
Oct 31, 2021, 5:58:52 AM10/31/21
to fltk.general
Le dimanche 31 octobre 2021 à 07:31:58 UTC+1, may64...@gmail.com a écrit :
Hello
Fl_Offscreen appears to automatically rescale on zoom events (ctrl/+/-) on MacOS, however this doesn't appear to happen on Windows or X11 systems, where basically one has to use Fl::add_handle() to detect zoom events and call fl_rescale_offscreen(). Would be possible to make this work automatically on Windows and X11.
Please, look  at the test/offscreen.cxx demo program. It draws into an offscreen buffer and handles zoom events OK on all 3 platforms.
The cause is that the draw function of this program memorizes the current value of the scale factor and calls fl_rescale_offscreen() when it detects
that the scale factor value changed.
I don't believe it's possible to have FLTK itself do that because it's not possible to know whether offscreens are in use.

Mo_Al_

unread,
Oct 31, 2021, 9:42:55 AM10/31/21
to fltk.general
Thank you for the explanation. Handling the rescaling in the draw() method is more convenient.
Reply all
Reply to author
Forward
0 new messages