Guest User

Untitled

a guest
Jul 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using Gtk;
  2. using Wrote;
  3.  
  4. public class Wrote.Scroller: Gtk.Bin {
  5. public Gtk.Adjustment hadjustment { get; construct set; }
  6. public Gtk.Adjustment vadjustment { get; construct set; }
  7.  
  8. public Scroller() {
  9. Gtk.Adjustment v, h;
  10. v = new Gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  11. h = new Gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  12.  
  13. GLib.Object(hadjustment: h, vadjustment: v);
  14. }
  15.  
  16. public override void add(Gtk.Widget widget) {
  17. bool scrollable = widget.set_scroll_adjustments(
  18. this.hadjustment, this.vadjustment);
  19.  
  20. if (!scrollable)
  21. Wrote.Log.error("Don't add a GtkWidget to WroteScroller "
  22. + "that doesn't have native scrolling abilities.");
  23.  
  24. base.add(widget);
  25. }
  26.  
  27. public override void size_request(out Gtk.Requisition requisition) {
  28. Gtk.Widget? child = this.get_child();
  29. Gtk.Requisition child_requisition = Gtk.Requisition();
  30.  
  31. if (child != null)
  32. child.size_request(out child_requisition);
  33.  
  34. this.vadjustment.upper =
  35. child_requisition.height.clamp(0, int.MAX);
  36. this.vadjustment.step_increment =
  37. (child_requisition.height - this.vadjustment.step_increment)
  38. .clamp(1, this.vadjustment.upper);
  39. this.vadjustment.changed();
  40.  
  41. this.hadjustment.upper =
  42. child_requisition.width.clamp(0, int.MAX);
  43. this.hadjustment.step_increment =
  44. (child_requisition.width - this.hadjustment.step_increment)
  45. .clamp(1, this.hadjustment.upper);
  46. this.hadjustment.changed();
  47.  
  48. requisition.width =
  49. child_requisition.width + this.style.xthickness * 2;
  50. requisition.height = this.style.ythickness * 2;
  51. }
  52.  
  53. public override void size_allocate(Gdk.Rectangle allocation) {
  54. Gdk.Rectangle child_allocation = allocation;
  55. child_allocation.x += this.style.xthickness;
  56. child_allocation.y += this.style.ythickness;
  57. child_allocation.width -= this.style.xthickness;
  58. child_allocation.height -= this.style.ythickness;
  59.  
  60. if (this.get_child() != null)
  61. this.get_child().size_allocate(child_allocation);
  62.  
  63. this.vadjustment.page_size =
  64. child_allocation.height.clamp(0, int.MAX);
  65. this.vadjustment.changed();
  66.  
  67. this.hadjustment.page_size =
  68. child_allocation.width.clamp(0, int.MAX);
  69. this.hadjustment.changed();
  70. }
  71.  
  72. public override bool scroll_event(Gdk.EventScroll e) {
  73. Gtk.Adjustment adjustment;
  74.  
  75. if (e.direction < Gdk.ScrollDirection.LEFT)
  76. adjustment = this.vadjustment;
  77. else
  78. adjustment = this.hadjustment;
  79.  
  80. double delta = this.compute_delta(adjustment, e.direction);
  81.  
  82. double new_value =
  83. (adjustment.value + delta)
  84. .clamp(0.0, adjustment.upper - adjustment.page_size);
  85.  
  86. adjustment.value = new_value;
  87.  
  88. return true;
  89. }
  90.  
  91. protected double compute_delta(Gtk.Adjustment adjustment,
  92. Gdk.ScrollDirection direction)
  93. {
  94. double delta = adjustment.step_increment
  95. .clamp(0.0, adjustment.upper - adjustment.page_size);
  96.  
  97. if (direction == Gdk.ScrollDirection.UP ||
  98. direction == Gdk.ScrollDirection.LEFT)
  99. delta *= -1;
  100.  
  101. return delta;
  102. }
  103. }
Add Comment
Please, Sign In to add comment