clamp.vala 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. // Drop-in replacement (sort-of) for HdyClamp from libhandy1.
  8. public class Clamp : Gtk.Container
  9. {
  10. public Gtk.Widget _child;
  11. public Clamp()
  12. {
  13. base.set_has_window(false);
  14. base.set_can_focus(false);
  15. base.set_redraw_on_allocate(false);
  16. this._child = null;
  17. }
  18. public void set_child(Gtk.Widget widget)
  19. {
  20. if (this._child == null) {
  21. widget.set_parent(this);
  22. this._child = widget;
  23. }
  24. }
  25. public override void remove(Gtk.Widget widget)
  26. {
  27. if (this._child == widget) {
  28. widget.unparent();
  29. this._child = null;
  30. if (this.get_visible() && widget.get_visible())
  31. this.queue_resize_no_redraw();
  32. }
  33. }
  34. public override void forall_internal(bool include_internals, Gtk.Callback callback)
  35. {
  36. if (this._child != null)
  37. callback(this._child);
  38. }
  39. public override Gtk.SizeRequestMode get_request_mode()
  40. {
  41. if (this._child != null)
  42. return this._child.get_request_mode();
  43. else
  44. return Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
  45. }
  46. public Gtk.Widget get_child()
  47. {
  48. return this._child;
  49. }
  50. public override void size_allocate(Gtk.Allocation alloc)
  51. {
  52. if (this._child == null || !this._child.is_visible())
  53. return;
  54. int child_min_width;
  55. this._child.get_preferred_width(out child_min_width, null);
  56. Gtk.Allocation child_alloc = {};
  57. child_alloc.width = 600;
  58. child_alloc.height = alloc.height;
  59. child_alloc.x = alloc.x + (alloc.width - child_alloc.width) / 2;
  60. child_alloc.y = alloc.y;
  61. this._child.size_allocate_with_baseline(child_alloc, this.get_allocated_baseline());
  62. }
  63. public new void get_preferred_size(out Gtk.Requisition minimum_size
  64. , out Gtk.Requisition natural_size
  65. )
  66. {
  67. Gtk.Requisition title_minimum_size = {0, 0};
  68. Gtk.Requisition title_natural_size = {0, 0};
  69. Gtk.Requisition child_minimum_size = {0, 0};
  70. Gtk.Requisition child_natural_size = {0, 0};
  71. if (this._child != null && this._child.get_visible())
  72. this._child.get_preferred_size(out child_minimum_size, out child_natural_size);
  73. minimum_size = {0, 0};
  74. natural_size = {0, 0};
  75. minimum_size.width = int.max(title_minimum_size.width, child_minimum_size.width);
  76. minimum_size.height = title_minimum_size.height + child_minimum_size.height;
  77. natural_size.width = int.max(title_natural_size.width, child_natural_size.width);
  78. natural_size.height = title_natural_size.height + child_natural_size.height;
  79. }
  80. }
  81. } /* namespace Crown */