Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Gtk;
- using Wrote;
- public class Wrote.Scroller: Gtk.Bin {
- public Gtk.Adjustment hadjustment { get; construct set; }
- public Gtk.Adjustment vadjustment { get; construct set; }
- public Scroller() {
- Gtk.Adjustment v, h;
- v = new Gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
- h = new Gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
- GLib.Object(hadjustment: h, vadjustment: v);
- }
- public override void add(Gtk.Widget widget) {
- bool scrollable = widget.set_scroll_adjustments(
- this.hadjustment, this.vadjustment);
- if (!scrollable)
- Wrote.Log.error("Don't add a GtkWidget to WroteScroller "
- + "that doesn't have native scrolling abilities.");
- base.add(widget);
- }
- public override void size_request(out Gtk.Requisition requisition) {
- Gtk.Widget? child = this.get_child();
- Gtk.Requisition child_requisition = Gtk.Requisition();
- if (child != null)
- child.size_request(out child_requisition);
- this.vadjustment.upper =
- child_requisition.height.clamp(0, int.MAX);
- this.vadjustment.step_increment =
- (child_requisition.height - this.vadjustment.step_increment)
- .clamp(1, this.vadjustment.upper);
- this.vadjustment.changed();
- this.hadjustment.upper =
- child_requisition.width.clamp(0, int.MAX);
- this.hadjustment.step_increment =
- (child_requisition.width - this.hadjustment.step_increment)
- .clamp(1, this.hadjustment.upper);
- this.hadjustment.changed();
- requisition.width =
- child_requisition.width + this.style.xthickness * 2;
- requisition.height = this.style.ythickness * 2;
- }
- public override void size_allocate(Gdk.Rectangle allocation) {
- Gdk.Rectangle child_allocation = allocation;
- child_allocation.x += this.style.xthickness;
- child_allocation.y += this.style.ythickness;
- child_allocation.width -= this.style.xthickness;
- child_allocation.height -= this.style.ythickness;
- if (this.get_child() != null)
- this.get_child().size_allocate(child_allocation);
- this.vadjustment.page_size =
- child_allocation.height.clamp(0, int.MAX);
- this.vadjustment.changed();
- this.hadjustment.page_size =
- child_allocation.width.clamp(0, int.MAX);
- this.hadjustment.changed();
- }
- public override bool scroll_event(Gdk.EventScroll e) {
- Gtk.Adjustment adjustment;
- if (e.direction < Gdk.ScrollDirection.LEFT)
- adjustment = this.vadjustment;
- else
- adjustment = this.hadjustment;
- double delta = this.compute_delta(adjustment, e.direction);
- double new_value =
- (adjustment.value + delta)
- .clamp(0.0, adjustment.upper - adjustment.page_size);
- adjustment.value = new_value;
- return true;
- }
- protected double compute_delta(Gtk.Adjustment adjustment,
- Gdk.ScrollDirection direction)
- {
- double delta = adjustment.step_increment
- .clamp(0.0, adjustment.upper - adjustment.page_size);
- if (direction == Gdk.ScrollDirection.UP ||
- direction == Gdk.ScrollDirection.LEFT)
- delta *= -1;
- return delta;
- }
- }
Add Comment
Please, Sign In to add comment