Hi,

I was doing some tests with drawing an Fl_Overlay_Window inside
another window, and it works ok on Linux (didn't try Windows),
but on MacOSX, it draws the contents of the overlay window
(what is drawn in the draw() method) displaced. To be precise,
it draws with the x-position at 0 of the containing window, rather
than at 0 of the overlay window.

I tried to force the contents by adding the x-position inside the
draw, but the clipping region is displaced as well.

The overlay (draw_overlay) however _is_ drawn correctly.

I attach sample code and a screenshot of how it looks on Linux and
on MacOSX.

maarten
#include <FL/Fl_Window.H>
#include <FL/Fl_Overlay_Window.H>
#include <FL/Fl_Image.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <cstdio>

// this is an overlay window, which will do the background drawing
// and the draw method, the event handling, and draw a crosshair on
// top in the draw_overlay method.
class CrosshairWindow:public Fl_Overlay_Window
{
	public:
		unsigned char imagedata[256][256][3];
		unsigned char k;

		int lx,ly;

		CrosshairWindow(int x,int y,int w,int h)
		:Fl_Overlay_Window(x,y,w,h),lx(w/2),ly(h/2)
		{
			// important! no other widgets should be added here,
			// so we end the window here
			end();
			redraw_overlay();
		}

		int handle(int e);

		void draw_overlay(void);

		void draw(void);

		// change the position of the crosshair
		void moveCrosshair(int ex,int ey)
		{
			lx = ex;
			ly = ey;
			if (lx < 0) lx = 0;
			if (lx >= w()) lx = w()-1;
			if (ly < 0) ly = 0;
			if (ly >= h()) ly = h()-1;
			redraw_overlay(); // make sure the changed crosshair is drawn
		}
};

int CrosshairWindow::handle(int e)
{
	switch (e)
	{
		case FL_FOCUS: case FL_UNFOCUS: return 1;
		case FL_PUSH:	
		case FL_DRAG:
			moveCrosshair(Fl::event_x(),Fl::event_y());
			take_focus();
			return 1;
		case FL_KEYDOWN:
			{
				// change gradient when a 0..9 key is pressed
				int ch = Fl::event_key();
				if (ch>='0' && ch<='9')
				{
					k = (ch-'0')*255/10;
					redraw();
				}
			}
			redraw();
			return 1;
	}
	// pass any other events to the superclass
	return Fl_Overlay_Window::handle(e);
}

void CrosshairWindow::draw(void)
{
	// draw the background
	// 
	// make this artificially slow by doing it 200 times
	for (int makeslow=0;makeslow<200;makeslow++)
	{
		for (int j=0;j<256;j++)
		{
			for (int i=0;i<256;i++)
			{
				imagedata[j][i][0] = j; // RED
				imagedata[j][i][1] = i; // GREEN
				imagedata[j][i][2] = k; // BLUE
			}
		}
		fl_draw_image((unsigned char*)imagedata,0,0,w(),h());
	}
}

void CrosshairWindow::draw_overlay(void)
{
	// draw the crosshair
	fl_color(FL_WHITE);
	fl_yxline(lx,0,h()-1);
	fl_xyline(0,ly,w()-1);
}

int main()
{
	// create the main window
	Fl_Window w(400,400);

	// add some widgets:
	Fl_Button b1(5,5,100,20,"A Button");
	Fl_Button b2(200,5,150,20,"Another Button");
	Fl_Input inp(100,360,200,20,"Input:");

	// add the overlayed crosshair. this is a derived Fl_Overlay_Window,
	// but we use it as a widget (by adding it into another Window)
	CrosshairWindow l(50,100,256,256);

	w.end();
	// show the window
	w.show();

	// enter the event loop
	Fl::run();

	return 0;
}

<<attachment: linux.png>>

<<attachment: macosx.png>>

_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to