Sign up

Scale and cursor keys

I'm trying to get a horizontal scale with

  • origin on the left
  • cursor keys left/right = decrease/increase
  • cursor keys up/down = increase/decrease (and the same for pageup/pagedown/end/home)

setInverted(true) doesn't help much because then I get the origin to the right. Would I have to intercept/override some key events to do this?

import std.stdio;

import gtk.Main;
import gtk.MainWindow;
import gtk.Box;
import gtk.Scale;

void main(string[] args)
{
	Main.init(args);

	MainWindow window = new MainWindow("title");
	window.setSizeRequest(400, 200);
	Box box = new Box(Orientation.VERTICAL, 5);
	{
		// ✓ origin left
		// ✓ left/right cursor keys
		// ✕ up/down cursor keys
		Scale scale1 = new Scale(GtkOrientation.HORIZONTAL, 0, 100, 10);
		box.add(scale1);

		// ✕ origin left
		// ✓ left/right cursor keys
		// ✓ up/down cursor keys
		Scale scale2 = new Scale(GtkOrientation.HORIZONTAL, 0, 100, 10);
		scale2.setInverted(true);
		box.add(scale2);
	}
	window.add(box);

	window.showAll();
	Main.run();

}

Re: Scale and cursor keys

On Fri, 29 Mar 2019 17:11:25 GMT, number wrote:

I'm trying to get a horizontal scale with

Overriding the keypresses works:

import std.stdio;

import gtk.Main;
import gtk.MainWindow;
import gtk.Scale;
import gdk.Event;
import gdk.Keysyms;
import gtk.Widget;
import gtk.Range;

void main(string[] args)
{
	Main.init(args);

	MainWindow window = new MainWindow("title");
	window.setSizeRequest(400, 200);

	// ✓ origin left
	// ✓ left/right cursor keys
	// ✓ up/down cursor keys
	Scale scale = new MyScale();
	window.add(scale);

	window.showAll();
	Main.run();
}

class MyScale : Scale
{
	this()
	{
		super(GtkOrientation.HORIZONTAL, 0, 100, 10);
		addOnKeyPress(&onKeyPress);
	}

	bool onKeyPress(Event e, Widget w)
	{
		bool result = false;
		Range r = cast(Range) w;
		if (r)
		{
			switch (e.key.keyval)
			{
			default:
				break;
			case GdkKeysyms.GDK_Up:
				r.setValue(r.getValue + r.getAdjustment.getStepIncrement);
				result = true;
				break;
			case GdkKeysyms.GDK_Down:
				r.setValue(r.getValue - r.getAdjustment.getStepIncrement);
				result = true;
				break;
			case GdkKeysyms.GDK_Page_Up:
				r.setValue(r.getValue + r.getAdjustment.getPageIncrement);
				result = true;
				break;
			case GdkKeysyms.GDK_Page_Down:
				r.setValue(r.getValue - r.getAdjustment.getPageIncrement);
				result = true;
				break;
			}
		}
		return result;
	}

}

Re: Scale and cursor keys

On 29-03-2019 18:11, number wrote:

I'm trying to get a horizontal scale with

  • origin on the left
  • cursor keys left/right = decrease/increase
  • cursor keys up/down = increase/decrease (and the same for pageup/pagedown/end/home)

setInverted(true) doesn't help much because then I get the origin to the right. Would I have to intercept/override some key events to do this?

You can use the OnKeyPress event to change the way the buttons are handled.

import std.algorithm : max, min;

import gdk.Keysyms;
import gtk.Adjustment;
import gtk.Box;
import gtk.Main;
import gtk.MainWindow;
import gtk.Scale;

void main(string[] args)
{
	Main.init(args);

	MainWindow window = new MainWindow("title");
	window.setSizeRequest(400, 200);
	Box box = new Box(Orientation.VERTICAL, 5);
	{
		// ✓ origin left
		// ✓ left/right cursor keys
		// ✕ up/down cursor keys
		Scale scale1 = new Scale(GtkOrientation.HORIZONTAL, 0, 100, 10);
		scale1.addOnKeyPress((GdkEventKey* event, _){
			Adjustment a = scale1.getAdjustment();

			if (event.keyval == Keysyms.GDK_Up)
			{
				a.setValue(min(a.getUpper(), a.getValue() + a.getStepIncrement()));
				return true;
			}
			else if (event.keyval == Keysyms.GDK_Down)
			{
				a.setValue(max(a.getLower(), a.getValue() - a.getStepIncrement()));
				return true;
			}
			return false;
		});
		box.add(scale1);
	}
	window.add(box);

	window.showAll();
	Main.run();
}

Re: Scale and cursor keys

On Fri, 29 Mar 2019 20:14:02 +0100, Mike Wey wrote:

You can use the OnKeyPress event to change the way the buttons are handled.

Thank you for the response.

the docs say the value will be clamped automatically, but using min/max looks cleaner.

Re: Scale and cursor keys

On 30-03-2019 13:36, number wrote:

On Fri, 29 Mar 2019 20:14:02 +0100, Mike Wey wrote:

You can use the OnKeyPress event to change the way the buttons are handled.

Thank you for the response.

the docs say the value will be clamped automatically, but using min/max looks cleaner.

I missed the setValue for the gtk.Range, the setValue function for
gtk.Ajustment doesn't clamp the value.