input_enum.vala 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #if CROWN_GTK3
  83. this.scroll_event.connect(() => {
  84. GLib.Signal.stop_emission_by_name(this, "scroll-event");
  85. return Gdk.EVENT_PROPAGATE;
  86. });
  87. #else
  88. _controller_scroll = new Gtk.EventControllerScroll(this, Gtk.EventControllerScrollFlags.BOTH_AXES);
  89. _controller_scroll.set_propagation_phase(Gtk.PropagationPhase.CAPTURE);
  90. _controller_scroll.scroll.connect(() => {
  91. // Do nothing, just consume the event to stop
  92. // the annoying scroll default behavior.
  93. });
  94. #endif
  95. }
  96. public void append(string? id, string label)
  97. {
  98. this.changed.disconnect(on_changed);
  99. Gtk.TreeIter iter;
  100. _store.insert_with_values(out iter, -1, 0, id, 1, label, -1);
  101. this.changed.connect(on_changed);
  102. }
  103. public void clear()
  104. {
  105. this.changed.disconnect(on_changed);
  106. _store.clear();
  107. insert_special_values();
  108. _inconsistent = false;
  109. this.changed.connect(on_changed);
  110. }
  111. public string any_valid_id()
  112. {
  113. string some_id = INCONSISTENT_ID;
  114. if (_store.iter_n_children(null) > 1u) {
  115. _store.foreach((model, path, iter) => {
  116. Value id_val;
  117. model.get_value(iter, 0, out id_val);
  118. if ((string)id_val != INCONSISTENT_ID) {
  119. some_id = (string)id_val;
  120. return true;
  121. }
  122. return false;
  123. });
  124. }
  125. return some_id;
  126. }
  127. private void on_changed()
  128. {
  129. if (this.get_active_id() == null)
  130. return;
  131. if (_inconsistent && this.get_active_id() == INCONSISTENT_ID)
  132. return;
  133. if (_inconsistent) {
  134. _inconsistent = false;
  135. _filter.refilter();
  136. }
  137. value_changed(this);
  138. }
  139. private void insert_special_values()
  140. {
  141. assert(_store.iter_n_children(null) == 0u);
  142. // Insert the "inconsistent" ID and label.
  143. Gtk.TreeIter iter;
  144. _store.insert_with_values(out iter, -1, 0, INCONSISTENT_ID, 1, INCONSISTENT_LABEL, -1);
  145. }
  146. }
  147. } /* namespace Crown */