input_string.vala 2.8 KB

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