entry_text.vala 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 EntryText : Gtk.Entry, Property
  8. {
  9. // Data
  10. public bool _inconsistent;
  11. public bool _stop_emit;
  12. public string _value;
  13. // Signals
  14. public signal void value_changed();
  15. public void set_inconsistent(bool inconsistent)
  16. {
  17. if (_inconsistent != inconsistent) {
  18. _inconsistent = inconsistent;
  19. _stop_emit = true;
  20. if (_inconsistent) {
  21. this.text = INCONSISTENT_LABEL;
  22. } else {
  23. this.text = _value;
  24. }
  25. _stop_emit = false;
  26. }
  27. }
  28. public bool is_inconsistent()
  29. {
  30. return _inconsistent;
  31. }
  32. public Value? generic_value()
  33. {
  34. return this.value;
  35. }
  36. public void set_generic_value(Value? val)
  37. {
  38. this.value = (string)val;
  39. }
  40. public string value
  41. {
  42. get
  43. {
  44. return _value;
  45. }
  46. set
  47. {
  48. _stop_emit = true;
  49. set_value_safe(value);
  50. _stop_emit = false;
  51. }
  52. }
  53. public EntryText()
  54. {
  55. _stop_emit = false;
  56. _inconsistent = false;
  57. _value = "";
  58. this.activate.connect(on_activate);
  59. this.button_press_event.connect(on_button_press);
  60. this.button_release_event.connect(on_button_release);
  61. this.focus_in_event.connect(on_focus_in);
  62. this.focus_out_event.connect(on_focus_out);
  63. }
  64. private bool on_button_press(Gdk.EventButton ev)
  65. {
  66. this.grab_focus();
  67. return Gdk.EVENT_PROPAGATE;
  68. }
  69. private bool on_button_release(Gdk.EventButton ev)
  70. {
  71. if (ev.button == Gdk.BUTTON_PRIMARY) {
  72. if (_inconsistent)
  73. this.text = "";
  74. else
  75. this.text = _value;
  76. this.set_position(-1);
  77. this.select_region(0, -1);
  78. return Gdk.EVENT_STOP;
  79. }
  80. return Gdk.EVENT_PROPAGATE;
  81. }
  82. private void on_activate()
  83. {
  84. this.select_region(0, 0);
  85. this.set_position(-1);
  86. set_value_safe(this.text);
  87. }
  88. private bool on_focus_in(Gdk.EventFocus ev)
  89. {
  90. var app = (LevelEditorApplication)GLib.Application.get_default();
  91. app.entry_any_focus_in(this);
  92. if (_inconsistent)
  93. this.text = "";
  94. else
  95. this.text = _value;
  96. this.set_position(-1);
  97. this.select_region(0, -1);
  98. return Gdk.EVENT_PROPAGATE;
  99. }
  100. private bool on_focus_out(Gdk.EventFocus ef)
  101. {
  102. var app = (LevelEditorApplication)GLib.Application.get_default();
  103. app.entry_any_focus_out(this);
  104. if (_inconsistent) {
  105. if (this.text != "") {
  106. set_value_safe(this.text);
  107. } else {
  108. this.text = INCONSISTENT_LABEL;
  109. }
  110. } else {
  111. set_value_safe(this.text);
  112. }
  113. this.select_region(0, 0);
  114. return Gdk.EVENT_PROPAGATE;
  115. }
  116. protected virtual void set_value_safe(string text)
  117. {
  118. this.text = text;
  119. _inconsistent = false;
  120. // Notify value changed.
  121. if (_value != text) {
  122. _value = text;
  123. if (!_stop_emit)
  124. value_changed();
  125. }
  126. }
  127. }
  128. } /* namespace Crown */