console_view.vala 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gtk;
  6. namespace Crown
  7. {
  8. public class EntryHistory
  9. {
  10. public uint _capacity;
  11. public uint _size;
  12. public uint _index;
  13. public string[] _data;
  14. // Creates a new history with room for capacity records.
  15. public EntryHistory(uint capacity)
  16. {
  17. _capacity = capacity;
  18. _size = 0;
  19. _index = 0;
  20. _data = new string[capacity];
  21. }
  22. // Push a new string into the history.
  23. public void push(string text)
  24. {
  25. // Add command to history
  26. _data[_index] = text;
  27. _index = (_index + 1) % _capacity;
  28. if (_size < _capacity)
  29. ++_size;
  30. }
  31. public void clear()
  32. {
  33. _size = 0;
  34. _index = 0;
  35. }
  36. // Returns the element at @a distance slots from the current index.
  37. // Distance must be in the [1; _size] range.
  38. public string element(uint distance)
  39. {
  40. if (distance < 1 || distance > _size)
  41. return "ERROR";
  42. if (_index >= distance)
  43. return _data[_index - distance];
  44. else
  45. return _data[_capacity - (distance - _index)];
  46. }
  47. public void save(string path)
  48. {
  49. FileStream fs = FileStream.open(path, "wb");
  50. if (fs == null)
  51. return;
  52. uint first_entry = _index + (_capacity - _size);
  53. for (uint ii = 0; ii < _size; ++ii)
  54. fs.printf("%s\n", _data[(first_entry + ii) % _capacity]);
  55. }
  56. public void load(string path)
  57. {
  58. FileStream fs = FileStream.open(path, "rb");
  59. if (fs == null)
  60. return;
  61. string? line = null;
  62. while ((line = fs.read_line()) != null)
  63. push(line);
  64. }
  65. }
  66. public class ConsoleView : Gtk.Box
  67. {
  68. // Data
  69. public EntryHistory _entry_history;
  70. public uint _distance;
  71. public Project _project;
  72. public PreferencesDialog _preferences_dialog;
  73. // Widgets
  74. public Gdk.Cursor _text_cursor;
  75. public Gdk.Cursor _pointer_cursor;
  76. public bool _cursor_is_hovering_link;
  77. public Gtk.TextView _text_view;
  78. public Gtk.ScrolledWindow _scrolled_window;
  79. public EntryText _entry;
  80. public Gtk.Box _entry_hbox;
  81. public Gtk.TextMark _scroll_mark;
  82. public ConsoleView(Project project, Gtk.ComboBoxText combo, PreferencesDialog preferences_dialog)
  83. {
  84. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  85. // Data
  86. _entry_history = new EntryHistory(256);
  87. _distance = 0;
  88. _project = project;
  89. _preferences_dialog = preferences_dialog;
  90. // Widgets
  91. _text_cursor = new Gdk.Cursor.from_name(this.get_display(), "text");
  92. _pointer_cursor = new Gdk.Cursor.from_name(this.get_display(), "pointer");
  93. _cursor_is_hovering_link = false;
  94. _text_view = new Gtk.TextView();
  95. _text_view.editable = false;
  96. _text_view.can_focus = false;
  97. // // Create tags for color-formatted text
  98. Gtk.TextTag tag_info = new Gtk.TextTag("info");
  99. tag_info.foreground_rgba = { 0.7, 0.7, 0.7, 1.0 };
  100. Gtk.TextTag tag_warning = new Gtk.TextTag("warning");
  101. tag_warning.foreground_rgba = { 1.0, 1.0, 0.4, 1.0 };
  102. Gtk.TextTag tag_error = new Gtk.TextTag("error");
  103. tag_error.foreground_rgba = { 1.0, 0.4, 0.4, 1.0 };
  104. Gtk.TextBuffer tb = _text_view.buffer;
  105. tb.tag_table.add(tag_info);
  106. tb.tag_table.add(tag_warning);
  107. tb.tag_table.add(tag_error);
  108. Gtk.TextIter end_iter;
  109. tb.get_end_iter(out end_iter);
  110. _scroll_mark = tb.create_mark("scroll", end_iter, true);
  111. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  112. _scrolled_window.vscrollbar_policy = Gtk.PolicyType.ALWAYS;
  113. _scrolled_window.add(_text_view);
  114. _entry = new EntryText();
  115. _entry.key_press_event.connect(on_entry_key_pressed);
  116. _entry.activate.connect(on_entry_activated);
  117. _entry_hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  118. _entry_hbox.pack_start(_entry, true, true);
  119. _entry_hbox.pack_end(combo, false, false);
  120. this.pack_start(_scrolled_window, true, true, 0);
  121. this.pack_start(_entry_hbox, false, true, 0);
  122. this.destroy.connect(on_destroy);
  123. this.button_release_event.connect(on_button_released);
  124. this.motion_notify_event.connect(on_motion_notify);
  125. this.get_style_context().add_class("console-view");
  126. _console_view_valid = true;
  127. }
  128. private void on_entry_activated()
  129. {
  130. string text = _entry.text;
  131. text = text.strip();
  132. if (text.length > 0) {
  133. _entry_history.push(text);
  134. _distance = 0;
  135. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  136. ConsoleClient? client = ((LevelEditorApplication)app).current_selected_client();
  137. if (text[0] == ':') {
  138. string[] args = text[1 : text.length].split(" ");
  139. if (args.length > 0) {
  140. if (client != null) {
  141. client.send(DeviceApi.command(args));
  142. client.send(DeviceApi.frame());
  143. }
  144. }
  145. } else {
  146. if (client != null) {
  147. logi("> %s".printf(text));
  148. client.send_script(text);
  149. client.send(DeviceApi.frame());
  150. }
  151. }
  152. }
  153. _entry.text = "";
  154. }
  155. private bool on_entry_key_pressed(Gdk.EventKey ev)
  156. {
  157. if (ev.keyval == Gdk.Key.Down) {
  158. if (_distance > 1) {
  159. --_distance;
  160. _entry.text = _entry_history.element(_distance);
  161. } else {
  162. _entry.text = "";
  163. }
  164. _entry.set_position(_entry.text.length);
  165. return Gdk.EVENT_STOP;
  166. } else if (ev.keyval == Gdk.Key.Up) {
  167. if (_distance < _entry_history._size) {
  168. ++_distance;
  169. _entry.text = _entry_history.element(_distance);
  170. }
  171. _entry.set_position(_entry.text.length);
  172. return Gdk.EVENT_STOP;
  173. }
  174. return Gdk.EVENT_PROPAGATE;
  175. }
  176. private void on_destroy()
  177. {
  178. _console_view_valid = false;
  179. }
  180. private bool on_button_released(Gdk.EventButton ev)
  181. {
  182. if (ev.button == Gdk.BUTTON_PRIMARY) {
  183. // Do not handle click if some text is selected.
  184. Gtk.TextIter dummy_iter;
  185. if (_text_view.buffer.get_selection_bounds(out dummy_iter, out dummy_iter))
  186. return Gdk.EVENT_PROPAGATE;
  187. int buffer_x;
  188. int buffer_y;
  189. _text_view.window_to_buffer_coords(Gtk.TextWindowType.WIDGET
  190. , (int)ev.x
  191. , (int)ev.y
  192. , out buffer_x
  193. , out buffer_y
  194. );
  195. Gtk.TextIter iter;
  196. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y)) {
  197. // Check whether the text under the mouse pointer has a link tag.
  198. GLib.SList<unowned TextTag> tags = iter.get_tags();
  199. foreach (var item in tags) {
  200. string resource_name = item.get_data<string>("resource_name");
  201. if (resource_name != null) {
  202. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  203. app.activate_action("open-resource", new GLib.Variant.string(resource_name));
  204. }
  205. }
  206. }
  207. }
  208. return Gdk.EVENT_PROPAGATE;
  209. }
  210. private bool on_motion_notify(Gdk.EventMotion ev)
  211. {
  212. bool hovering = false;
  213. int buffer_x;
  214. int buffer_y;
  215. _text_view.window_to_buffer_coords(TextWindowType.WIDGET
  216. , (int)ev.x
  217. , (int)ev.y
  218. , out buffer_x
  219. , out buffer_y
  220. );
  221. Gtk.TextIter iter;
  222. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y)) {
  223. // Check whether the text under the mouse pointer has a link tag.
  224. GLib.SList<unowned TextTag> tags = iter.get_tags();
  225. foreach (var item in tags) {
  226. string resource_name = item.get_data<string>("resource_name");
  227. if (resource_name != null) {
  228. hovering = true;
  229. }
  230. }
  231. }
  232. if (_cursor_is_hovering_link != hovering) {
  233. _cursor_is_hovering_link = hovering;
  234. if (_cursor_is_hovering_link)
  235. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_pointer_cursor);
  236. else
  237. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_text_cursor);
  238. }
  239. return Gdk.EVENT_PROPAGATE;
  240. }
  241. public void log(string severity, string message)
  242. {
  243. Gtk.TextBuffer buffer = _text_view.buffer;
  244. // Limit number of lines recorded.
  245. int max_lines = (int)_preferences_dialog._console_max_lines.value;
  246. if (buffer.get_line_count() - 1 >= max_lines) {
  247. Gtk.TextIter start_of_first_line;
  248. buffer.get_iter_at_line(out start_of_first_line, 0);
  249. Gtk.TextIter end_of_first_line = start_of_first_line;
  250. start_of_first_line.forward_line();
  251. buffer.delete(ref start_of_first_line, ref end_of_first_line);
  252. }
  253. Gtk.TextIter end_iter;
  254. buffer.get_end_iter(out end_iter);
  255. // Replace all IDs with corresponding human-readable names.
  256. int id_index = 0;
  257. do {
  258. // Search for occurrences of the ID string.
  259. int id_index_orig = id_index;
  260. id_index = message.index_of("#ID(", id_index);
  261. if (id_index != -1) {
  262. // If an occurrenct is found, insert the preceding text as usual.
  263. string line_chunk = message.substring(id_index_orig, id_index - id_index_orig);
  264. buffer.insert_with_tags(ref end_iter
  265. , line_chunk
  266. , line_chunk.length
  267. , buffer.tag_table.lookup(severity)
  268. , null
  269. );
  270. // Try to extract the ID from the ID string.
  271. int id_closing_parentheses = message.index_of(")", id_index + 4);
  272. if (id_closing_parentheses == -1) {
  273. // If the ID is malformed, insert the whole line as-is.
  274. buffer.insert_with_tags(ref end_iter
  275. , message.substring(id_index)
  276. , -1
  277. , buffer.tag_table.lookup(severity)
  278. , null
  279. );
  280. break;
  281. }
  282. // Convert the resource ID to human-readable resource name.
  283. string resource_name;
  284. string resource_id = message.substring(id_index + 4, id_closing_parentheses - (id_index + 4));
  285. _project.resource_id_to_name(out resource_name, resource_id);
  286. // Create a tag for hyperlink.
  287. Gtk.TextTag hyperlink = null;
  288. hyperlink = buffer.create_tag(null, "underline", Pango.Underline.SINGLE, null);
  289. hyperlink.set_data("resource_name", resource_name);
  290. buffer.insert_with_tags(ref end_iter
  291. , resource_name
  292. , -1
  293. , buffer.tag_table.lookup(severity)
  294. , hyperlink
  295. , null
  296. );
  297. id_index += 4 + resource_id.length;
  298. continue;
  299. } else {
  300. buffer.insert_with_tags(ref end_iter
  301. , message.substring(id_index_orig)
  302. , -1
  303. , buffer.tag_table.lookup(severity)
  304. , null
  305. );
  306. }
  307. } while (id_index++ >= 0);
  308. // Line height is computed in an idle handler, wait a bit before scrolling to bottom.
  309. // See: https://valadoc.org/gtk+-3.0/Gtk.TextView.scroll_to_iter.html
  310. GLib.Idle.add(scroll_to_bottom);
  311. }
  312. private bool scroll_to_bottom()
  313. {
  314. Gtk.TextBuffer buffer = _text_view.buffer;
  315. Gtk.TextIter end_iter;
  316. buffer.get_end_iter(out end_iter);
  317. // Scroll to bottom.
  318. // See: gtk3-demo "Automatic Scrolling".
  319. end_iter.set_line_offset(0);
  320. buffer.move_mark(_scroll_mark, end_iter);
  321. _text_view.scroll_mark_onscreen(_scroll_mark);
  322. return GLib.Source.REMOVE;
  323. }
  324. }
  325. } /* namespace Crown */