input_enum.vala 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class InputEnum : InputField, Gtk.ComboBox
  8. {
  9. public bool _inconsistent;
  10. public Gtk.ListStore _store;
  11. public Gtk.TreeModelFilter _filter;
  12. public Gtk.EventControllerScroll _controller_scroll;
  13. public void set_inconsistent(bool inconsistent)
  14. {
  15. if (_inconsistent != inconsistent) {
  16. _inconsistent = inconsistent;
  17. _filter.refilter();
  18. if (_inconsistent) {
  19. this.set_active_id(INCONSISTENT_ID);
  20. }
  21. }
  22. }
  23. public bool is_inconsistent()
  24. {
  25. return _inconsistent;
  26. }
  27. public GLib.Value union_value()
  28. {
  29. return this.value;
  30. }
  31. public void set_union_value(GLib.Value v)
  32. {
  33. this.value = (string)v;
  34. }
  35. public string value
  36. {
  37. get
  38. {
  39. return this.get_active_id();
  40. }
  41. set
  42. {
  43. _filter.refilter();
  44. bool success = this.set_active_id(value);
  45. set_inconsistent(!success);
  46. }
  47. }
  48. public bool filter_visible_func(Gtk.TreeModel model, Gtk.TreeIter iter)
  49. {
  50. Value id_val;
  51. model.get_value(iter, 0, out id_val);
  52. if (!_inconsistent && (string)id_val == INCONSISTENT_ID)
  53. return false;
  54. return true;
  55. }
  56. public InputEnum(string default_id = "DEFAULT", string[]? labels = null, string[]? ids = null)
  57. {
  58. _inconsistent = false;
  59. _store = new Gtk.ListStore(2
  60. , typeof(string) // ID
  61. , typeof(string) // Label
  62. );
  63. _filter = new Gtk.TreeModelFilter(_store, null);
  64. _filter.set_visible_func(filter_visible_func);
  65. this.model = _filter;
  66. this.id_column = 0;
  67. this.entry_text_column = 1;
  68. Gtk.CellRendererText renderer = new Gtk.CellRendererText();
  69. this.pack_start(renderer, true);
  70. this.add_attribute(renderer, "text", 1);
  71. insert_special_values();
  72. if (labels != null) {
  73. Gtk.TreeIter iter;
  74. for (int ii = 0; ii < ids.length; ++ii) {
  75. unowned string? id = ids != null ? ids[ii] : null;
  76. _store.insert_with_values(out iter, -1, 0, id, 1, labels[ii], -1);
  77. }
  78. // if (ids != null && default_id < ids.length)
  79. this.value = default_id;
  80. }
  81. this.changed.connect(on_changed);
  82. _controller_scroll = new Gtk.EventControllerScroll(this, Gtk.EventControllerScrollFlags.BOTH_AXES);
  83. _controller_scroll.set_propagation_phase(Gtk.PropagationPhase.CAPTURE);
  84. _controller_scroll.scroll.connect(() => {
  85. // Do nothing, just consume the event to stop
  86. // the annoying scroll default behavior.
  87. });
  88. }
  89. public void append(string? id, string label)
  90. {
  91. this.changed.disconnect(on_changed);
  92. Gtk.TreeIter iter;
  93. _store.insert_with_values(out iter, -1, 0, id, 1, label, -1);
  94. this.changed.connect(on_changed);
  95. }
  96. public void clear()
  97. {
  98. this.changed.disconnect(on_changed);
  99. _store.clear();
  100. insert_special_values();
  101. _inconsistent = false;
  102. this.changed.connect(on_changed);
  103. }
  104. public string any_valid_id()
  105. {
  106. string some_id = INCONSISTENT_ID;
  107. if (_store.iter_n_children(null) > 1u) {
  108. _store.foreach((model, path, iter) => {
  109. Value id_val;
  110. model.get_value(iter, 0, out id_val);
  111. if ((string)id_val != INCONSISTENT_ID) {
  112. some_id = (string)id_val;
  113. return true;
  114. }
  115. return false;
  116. });
  117. }
  118. return some_id;
  119. }
  120. private void on_changed()
  121. {
  122. if (this.get_active_id() == null)
  123. return;
  124. if (_inconsistent && this.get_active_id() == INCONSISTENT_ID)
  125. return;
  126. if (_inconsistent) {
  127. _inconsistent = false;
  128. _filter.refilter();
  129. }
  130. value_changed(this);
  131. }
  132. private void insert_special_values()
  133. {
  134. assert(_store.iter_n_children(null) == 0u);
  135. // Insert the "inconsistent" ID and label.
  136. Gtk.TreeIter iter;
  137. _store.insert_with_values(out iter, -1, 0, INCONSISTENT_ID, 1, INCONSISTENT_LABEL, -1);
  138. }
  139. }
  140. } /* namespace Crown */