input_string.vala 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 InputString : InputField, Gtk.Entry
  8. {
  9. public bool _inconsistent;
  10. public string _value;
  11. public Gtk.GestureMultiPress _gesture_click;
  12. public void set_inconsistent(bool inconsistent)
  13. {
  14. if (_inconsistent != inconsistent) {
  15. _inconsistent = inconsistent;
  16. if (_inconsistent) {
  17. this.text = INCONSISTENT_LABEL;
  18. } else {
  19. this.text = _value;
  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 _value;
  40. }
  41. set
  42. {
  43. set_value_safe(value);
  44. }
  45. }
  46. public InputString()
  47. {
  48. _inconsistent = false;
  49. _value = "";
  50. _gesture_click = new Gtk.GestureMultiPress(this);
  51. _gesture_click.pressed.connect(on_button_pressed);
  52. _gesture_click.released.connect(on_button_released);
  53. this.activate.connect(on_activate);
  54. this.focus_in_event.connect(on_focus_in);
  55. this.focus_out_event.connect(on_focus_out);
  56. }
  57. public void on_button_pressed(int n_press, double x, double y)
  58. {
  59. this.grab_focus();
  60. }
  61. public void on_button_released(int n_press, double x, double y)
  62. {
  63. uint button = _gesture_click.get_current_button();
  64. if (button == Gdk.BUTTON_PRIMARY) {
  65. if (_inconsistent)
  66. this.text = "";
  67. else
  68. this.text = _value;
  69. GLib.Idle.add(() => {
  70. this.set_position(-1);
  71. this.select_region(0, -1);
  72. return GLib.Source.REMOVE;
  73. });
  74. }
  75. }
  76. public void on_activate()
  77. {
  78. this.select_region(0, 0);
  79. this.set_position(-1);
  80. set_value_safe(this.text);
  81. }
  82. public bool on_focus_in(Gdk.EventFocus ev)
  83. {
  84. var app = (LevelEditorApplication)GLib.Application.get_default();
  85. app.entry_any_focus_in(this);
  86. if (_inconsistent)
  87. this.text = "";
  88. else
  89. this.text = _value;
  90. this.set_position(-1);
  91. this.select_region(0, -1);
  92. return Gdk.EVENT_PROPAGATE;
  93. }
  94. public bool on_focus_out(Gdk.EventFocus ef)
  95. {
  96. var app = (LevelEditorApplication)GLib.Application.get_default();
  97. app.entry_any_focus_out(this);
  98. if (_inconsistent) {
  99. if (this.text != "") {
  100. set_value_safe(this.text);
  101. } else {
  102. this.text = INCONSISTENT_LABEL;
  103. }
  104. } else {
  105. set_value_safe(this.text);
  106. }
  107. this.select_region(0, 0);
  108. return Gdk.EVENT_PROPAGATE;
  109. }
  110. public virtual void set_value_safe(string text)
  111. {
  112. this.text = text;
  113. _inconsistent = false;
  114. // Notify value changed.
  115. if (_value != text) {
  116. _value = text;
  117. value_changed(this);
  118. }
  119. }
  120. }
  121. } /* namespace Crown */