Re: [gtkmm] ScrolledWindow doesn't scroll a DrawingArea



I've found a far simpler example of a broken ScrolledWindow displaying a
DrawingArea.  This is (as much as I know how) directly converted from a
straight C example that *does* work.  I can post the C code if anyone is
interested in comparing, but the unintended behavior should be pretty
obvious from running this.  I hope I'm simply missing an event handler or
something.

Much thanks,

Nik Melchior
#include <gtkmm.h>

#define WIDTH (1000)
#define HEIGHT (1000)
#define ADDR( X, Y ) (image + ((Y) * WIDTH + (X)) * 3)

class ExampleWin : public Gtk::Window {
public:
	unsigned char image[WIDTH * HEIGHT * 3];
	Gtk::DrawingArea darea;
	Gtk::Adjustment *hadj;
	Gtk::Adjustment *vadj;
	Gtk::ScrolledWindow swin;


	void
	init_image( void )
	{
		int x, y;

		for( y = 0; y < HEIGHT; y++ ) 
			for( x = 0; x < WIDTH; x++ ) {
				unsigned char * const p = ADDR( x, y );

				p[0] = (256 * x) / WIDTH;
				p[1] = (256 * y) / HEIGHT;
				p[2] = x + y;
			}
	}

	bool
	expose( GdkEventExpose *event )
	{
		const unsigned char *buf = ADDR(event->area.x, event->area.y);

		darea.get_window()->draw_rgb_image(darea.get_style()->get_white_gc(),
										   event->area.x,
										   event->area.y,
										   event->area.width,
										   event->area.height,
										   Gdk::RGB_DITHER_MAX,
										   (guchar *) buf, WIDTH * 3 );

		return FALSE;
	}

	void
	adj_centre( Gtk::Adjustment *adj )
	{
		const double range = adj->get_upper() - adj->get_lower()
		                     - adj->get_page_size();

		adj->set_value(adj->get_lower() + range / 2);
	}

	bool
	conf (GdkEventConfigure *event)
	{
		hadj->set_upper(WIDTH);
		vadj->set_upper(HEIGHT);
		hadj->set_page_size(darea.get_allocation().width);
		vadj->set_page_size(darea.get_allocation().height);

		adj_centre(hadj);
		adj_centre(vadj);
	}

	ExampleWin ()
	{
		init_image();
		gtk_widget_set_default_colormap( gdk_rgb_get_cmap() );
		gtk_widget_set_default_visual( gdk_rgb_get_visual() );

		darea.signal_expose_event().connect(slot(*this, &ExampleWin::expose));
		darea.signal_configure_event().connect(slot(*this, &ExampleWin::conf));

		swin.add(darea);
		hadj = swin.get_hadjustment();
		vadj = swin.get_vadjustment();

		add(swin);

		set_size_request(500, 500);
		show_all_children();

		adj_centre(hadj);
		adj_centre(vadj);
	}
};

int
main( int argc, char **argv )
{
	Gtk::Main kit(argc, argv);

	ExampleWin window;

	Gtk::Main::run(window);

	return( 0 );
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]