console_view.vala 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  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 ConsoleView(Project project, Gtk.ComboBoxText combo, PreferencesDialog preferences_dialog)
  82. {
  83. Object(orientation: Gtk.Orientation.VERTICAL, spacing: 0);
  84. // Data
  85. _entry_history = new EntryHistory(256);
  86. _distance = 0;
  87. _project = project;
  88. _preferences_dialog = preferences_dialog;
  89. // Widgets
  90. _text_cursor = new Gdk.Cursor.from_name(this.get_display(), "text");
  91. _pointer_cursor = new Gdk.Cursor.from_name(this.get_display(), "pointer");
  92. _cursor_is_hovering_link = false;
  93. _text_view = new Gtk.TextView();
  94. _text_view.editable = false;
  95. _text_view.can_focus = false;
  96. // // Create tags for color-formatted text
  97. Gtk.TextTag tag_info = new Gtk.TextTag("info");
  98. tag_info.foreground_rgba = { 0.7, 0.7, 0.7, 1.0 };
  99. Gtk.TextTag tag_warning = new Gtk.TextTag("warning");
  100. tag_warning.foreground_rgba = { 1.0, 1.0, 0.4, 1.0 };
  101. Gtk.TextTag tag_error = new Gtk.TextTag("error");
  102. tag_error.foreground_rgba = { 1.0, 0.4, 0.4, 1.0 };
  103. Gtk.TextBuffer tb = _text_view.buffer;
  104. tb.tag_table.add(tag_info);
  105. tb.tag_table.add(tag_warning);
  106. tb.tag_table.add(tag_error);
  107. Gtk.TextIter end_iter;
  108. tb.get_end_iter(out end_iter);
  109. tb.create_mark("scroll", end_iter, true);
  110. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  111. _scrolled_window.vscrollbar_policy = Gtk.PolicyType.ALWAYS;
  112. _scrolled_window.add(_text_view);
  113. _entry = new EntryText();
  114. _entry.key_press_event.connect(on_entry_key_pressed);
  115. _entry.activate.connect(on_entry_activated);
  116. _entry_hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  117. _entry_hbox.pack_start(_entry, true, true);
  118. _entry_hbox.pack_end(combo, false, false);
  119. this.pack_start(_scrolled_window, true, true, 0);
  120. this.pack_start(_entry_hbox, false, true, 0);
  121. this.show.connect(on_show);
  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. {
  134. _entry_history.push(text);
  135. _distance = 0;
  136. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  137. ConsoleClient? client = ((LevelEditorApplication)app).current_selected_client();
  138. if (text[0] == ':')
  139. {
  140. string[] args = text[1:text.length].split(" ");
  141. if (args.length > 0)
  142. {
  143. if (client != null)
  144. {
  145. client.send(DeviceApi.command(args));
  146. client.send(DeviceApi.frame());
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (client != null)
  153. {
  154. logi("> %s".printf(text));
  155. client.send_script(text);
  156. client.send(DeviceApi.frame());
  157. }
  158. }
  159. }
  160. _entry.text = "";
  161. }
  162. private bool on_entry_key_pressed(Gdk.EventKey ev)
  163. {
  164. if (ev.keyval == Gdk.Key.Down)
  165. {
  166. if (_distance > 1)
  167. {
  168. --_distance;
  169. _entry.text = _entry_history.element(_distance);
  170. }
  171. else
  172. {
  173. _entry.text = "";
  174. }
  175. _entry.set_position(_entry.text.length);
  176. return Gdk.EVENT_STOP;
  177. }
  178. else if (ev.keyval == Gdk.Key.Up)
  179. {
  180. if (_distance < _entry_history._size)
  181. {
  182. ++_distance;
  183. _entry.text = _entry_history.element(_distance);
  184. }
  185. _entry.set_position(_entry.text.length);
  186. return Gdk.EVENT_STOP;
  187. }
  188. return Gdk.EVENT_PROPAGATE;
  189. }
  190. private void on_show()
  191. {
  192. _entry.grab_focus_without_selecting();
  193. }
  194. private void on_destroy()
  195. {
  196. _console_view_valid = false;
  197. }
  198. private bool on_button_released(Gdk.EventButton ev)
  199. {
  200. if (ev.button == Gdk.BUTTON_PRIMARY)
  201. {
  202. // Do not handle click if some text is selected.
  203. Gtk.TextIter dummy_iter;
  204. if (_text_view.buffer.get_selection_bounds(out dummy_iter, out dummy_iter))
  205. return Gdk.EVENT_PROPAGATE;
  206. int buffer_x;
  207. int buffer_y;
  208. _text_view.window_to_buffer_coords(Gtk.TextWindowType.WIDGET
  209. , (int)ev.x
  210. , (int)ev.y
  211. , out buffer_x
  212. , out buffer_y
  213. );
  214. Gtk.TextIter iter;
  215. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y))
  216. {
  217. // Check whether the text under the mouse pointer has a link tag.
  218. GLib.SList<unowned TextTag> tags = iter.get_tags();
  219. foreach (var item in tags)
  220. {
  221. string resource_name = item.get_data<string>("resource_name");
  222. if (resource_name != null)
  223. {
  224. Gtk.Application app = ((Gtk.Window)this.get_toplevel()).application;
  225. app.activate_action("open-resource", new GLib.Variant.string(resource_name));
  226. }
  227. }
  228. }
  229. }
  230. return Gdk.EVENT_PROPAGATE;
  231. }
  232. private bool on_motion_notify(Gdk.EventMotion ev)
  233. {
  234. bool hovering = false;
  235. int buffer_x;
  236. int buffer_y;
  237. _text_view.window_to_buffer_coords(TextWindowType.WIDGET
  238. , (int)ev.x
  239. , (int)ev.y
  240. , out buffer_x
  241. , out buffer_y
  242. );
  243. Gtk.TextIter iter;
  244. if (_text_view.get_iter_at_location(out iter, buffer_x, buffer_y))
  245. {
  246. // Check whether the text under the mouse pointer has a link tag.
  247. GLib.SList<unowned TextTag> tags = iter.get_tags();
  248. foreach (var item in tags)
  249. {
  250. string resource_name = item.get_data<string>("resource_name");
  251. if (resource_name != null)
  252. {
  253. hovering = true;
  254. }
  255. }
  256. }
  257. if (_cursor_is_hovering_link != hovering)
  258. {
  259. _cursor_is_hovering_link = hovering;
  260. if (_cursor_is_hovering_link)
  261. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_pointer_cursor);
  262. else
  263. _text_view.get_window(Gtk.TextWindowType.TEXT).set_cursor(_text_cursor);
  264. }
  265. return Gdk.EVENT_PROPAGATE;
  266. }
  267. public void log(string severity, string message)
  268. {
  269. Gtk.TextBuffer buffer = _text_view.buffer;
  270. // Limit number of lines recorded.
  271. int max_lines = (int)_preferences_dialog._console_max_lines.value;
  272. if (buffer.get_line_count()-1 >= max_lines)
  273. {
  274. Gtk.TextIter start_of_first_line;
  275. buffer.get_iter_at_line(out start_of_first_line, 0);
  276. Gtk.TextIter end_of_first_line = start_of_first_line;
  277. start_of_first_line.forward_line();
  278. buffer.delete(ref start_of_first_line, ref end_of_first_line);
  279. }
  280. Gtk.TextIter end_iter;
  281. buffer.get_end_iter(out end_iter);
  282. // Replace all IDs with corresponding human-readable names.
  283. int id_index = 0;
  284. do
  285. {
  286. // Search for occurrences of the ID string.
  287. int id_index_orig = id_index;
  288. id_index = message.index_of("#ID(", id_index);
  289. if (id_index != -1)
  290. {
  291. // If an occurrenct is found, insert the preceding text as usual.
  292. string line_chunk = message.substring(id_index_orig, id_index-id_index_orig);
  293. buffer.insert_with_tags(ref end_iter
  294. , line_chunk
  295. , line_chunk.length
  296. , buffer.tag_table.lookup(severity)
  297. , null
  298. );
  299. // Try to extract the ID from the ID string.
  300. int id_closing_parentheses = message.index_of(")", id_index+4);
  301. if (id_closing_parentheses == -1)
  302. {
  303. // If the ID is malformed, insert the whole line as-is.
  304. buffer.insert_with_tags(ref end_iter
  305. , message.substring(id_index)
  306. , -1
  307. , buffer.tag_table.lookup(severity)
  308. , null
  309. );
  310. break;
  311. }
  312. // Convert the resource ID to human-readable resource name.
  313. string resource_name;
  314. string resource_id = message.substring(id_index+4, id_closing_parentheses-(id_index+4));
  315. _project.resource_id_to_name(out resource_name, resource_id);
  316. // Create a tag for hyperlink.
  317. Gtk.TextTag hyperlink = null;
  318. hyperlink = buffer.create_tag(null, "underline", Pango.Underline.SINGLE, null);
  319. hyperlink.set_data("resource_name", resource_name);
  320. buffer.insert_with_tags(ref end_iter
  321. , resource_name
  322. , -1
  323. , buffer.tag_table.lookup(severity)
  324. , hyperlink
  325. , null
  326. );
  327. id_index += 4 + resource_id.length;
  328. continue;
  329. }
  330. else
  331. {
  332. buffer.insert_with_tags(ref end_iter
  333. , message.substring(id_index_orig)
  334. , -1
  335. , buffer.tag_table.lookup(severity)
  336. , null
  337. );
  338. }
  339. }
  340. while (id_index++ >= 0);
  341. // Scroll to bottom.
  342. // See: gtk3-demo "Automatic Scrolling".
  343. end_iter.set_line_offset(0);
  344. Gtk.TextMark mark = buffer.get_mark("scroll");
  345. buffer.move_mark(mark, end_iter);
  346. _text_view.scroll_mark_onscreen(mark);
  347. }
  348. }
  349. }