[PythonCAD] zooming with mouse wheel

Art Haas ahaas at airmail.net
Wed Jun 14 22:35:20 CEST 2006

On Mon, Jun 12, 2006 at 04:57:31PM -0400, Russ Nelson wrote:
> This patch allows you to zoom in and out using the mouse wheel.  I
> think it has three problems:
>   1) the scroll increment is too large for a wheel.
>   2) it's a little hackish to be calling a menu item (but I wanted the
>      patch to be short).
>   3) The scrolling should also pan at the same time so that the object
>      you're pointing at remains underneath the pointer.
> Anyway, if people try this patch and think this is useful, I'll keep
> working on it.  Any suggestions beyond the three above?

I like the idea of the patch, but the problems you mention above make me
not want to apply it. As you wrote, the scroll increment is too large,
and calling the 'zoom_in' and 'zoom_out' functions are a bit hackish.

I'd suggest changing window_scroll_event() handler by replacing
the zoom_foo() calls by calculating a slightly larger or smaller
viewing area and using the gtkimage.setView() method to redraw
the screen. For example, the untested code below changes the
units-per-pixel scaling by 1% and recalculates the view with
the centerpoint of the current view remaining the same. Like
I said, the code is untested so it probably needs some
tweaking. A potential enhancement is to test if a certain key
is pressed while the scroll event happens and increasing or
descreasing the scale change by a larger amount, say 5%.

def window_scroll_event(widget, event, gtkimage):
	_xmin, _ymin, _xmax, _ymax = gtkimage.getView()
	_scale = gtkimage.getUnitsPerPixel()
	_w, _h = gtkimage.getSize() # window size in pixels
	_xm = (_xmax - _xmin)/2.0 # x-midpoint
	_ym = (_ymax - _ymin)/2.0 # y-midpoint
	if event.direction == gtk.gdk.SCROLL_UP: # zoom in
		_scale = 0.99 * _scale
	elif event.direction == gtk.gdk.SCROLL_DOWN: # zoom out
		_scale = 1.01 * _scale
		return TRUE
	_dx = (_scale * _w)/2.0
	_dy = (_scale * _h)/2.0
	gtkimage.setView((_xm - _dx), (_ym - _dy),
	                 (_xm + _dx), (_ym + _dy))
	return TRUE	

> [ ... snip patch ... ]

If you try the code above, does it work? If not, can you make it work?

Art Haas
Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.

-Thomas Jefferson to James Smith, 1822

More information about the PythonCAD mailing list