Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
Console.hh
Go to the documentation of this file.
1#pragma once
2
3#include <queue>
4#include <list>
5#include <gtkmm.h>
6#ifdef USE_GTK4
7#include <gtkmm/eventcontrollerkey.h>
8#endif
9#include "nlohmann/json.hpp"
10#include "../common/TeXEngine.hh"
11#include "DataCell.hh"
12
13namespace cadabra {
14 class Console;
15
16 // Interactive Console for Cadabra. Deived from Gtk::Box so can be packed into most
17 // other widgets.
18 class Console : public Gtk::Box {
19 public:
20 enum class Position : int {
21 Hidden=0,
22 DockedH=1,
23 DockedV=2,
24 Floating=3
25 };
26
27 // run_slot is the callback which should be run when the console sends input to
28 // be run. This callback should call console.grab_input to get a line of input,
29 // process it and then send the output block back to the console.
30 Console(sigc::slot<void()> run_slot);
31
32 // Display welcome message and first prompt
33 void initialize();
34
35 // Process a line of code, either creating a new prompt asking for
36 // more input or adding a string to the run_queue and calling the run_slot callback
37 void send_input(const std::string& code);
38
39 // Grab a line of input, this should only be called fom the run_slot callback.
40 // The id parameter will be set to a unique id which should be the parent_id of the
41 // block when it is sent back to the console
42 std::string grab_input(uint64_t& id);
43
44 // Add a message to the console's message queue and ask it to process the message queue
45 void signal_message(const nlohmann::json& msg);
46
47 // Scroll the console to the latest output
48 void scroll_to_bottom();
49
50 private:
51 // Hidden proxy TextView which stores the user input by intercepting key press
52 // events from the main console TextView
53 class TextViewProxy : public Gtk::TextView {
54 public:
56
62 bool handle_key_press(guint keyval, Gdk::ModifierType state);
63
66 void sync_to_view();
67
68#ifndef USE_GTK4
70#endif
72
73 private:
74 void set_text_quietly(const Glib::ustring& text);
76
78 std::string temp_in;
79 std::list<std::string> history;
80 std::list<std::string>::iterator history_ptr;
83 bool updating_buffer = false;
84#ifdef USE_GTK4
85 Glib::RefPtr<Gtk::EventControllerKey> key_controller;
86#endif
87 };
88
89 // Set the current prompt to particular text
90 void set_input(const Glib::ustring& line, size_t range_start = std::string::npos, size_t range_end = std::string::npos);
91
92 // Create a new prompt
93 void prompt(bool continuation, bool newline = false);
94
95 // Allocate a mark to a cell
97
98 // Return the current server cell (pseudo-input cell for inputs received from the server) or
99 // create one and return it if none exist
101
102 void insert_text(uint64_t cell_id, const std::string& text, const Glib::RefPtr<Gtk::TextTag>& tag);
103 void insert_graphic(uint64_t cell_id, const std::string& bytes);
104 void insert_tex(uint64_t cell_id, const std::shared_ptr<TeXEngine::TeXRequest>& content);
105
106 // Handle current incoming messages
108
112 Gtk::Widget& focus_widget();
113
114#ifdef USE_GTK4
117 void size_allocate_vfunc(int width, int height, int baseline) override;
118#endif
119
120 Glib::Dispatcher dispatch_message; // callback attached to process_message_queue
121 Glib::Dispatcher run; // initialized by run_slot, callback when cell needs running
122
123 bool needs_focus; // True if console should grab focus on output
124 Glib::RefPtr<Gtk::TextBuffer> buffer; // Reference to tv.get_buffer()
125 std::string collect; // Collected input if prompt is in continuation mode
126 std::queue<nlohmann::json> message_queue; // Current messages needing processing
127 std::queue<std::pair<std::string, Glib::RefPtr<Gtk::TextMark>>> run_queue; // Cells waiting to be run, .first is input string and .second is the input cell
128 TeXEngine tex_engine; // Engine for compiling TeX outputs
129 std::map<uint64_t, Glib::RefPtr<Gtk::TextMark>> cells; // Storage of currently displayed cells
130 uint64_t server_cell_id; // ID of current server cell, or 0 if invalidated
131 Glib::RefPtr<Gtk::TextMark> input_begin; // Beginning of input
132 Glib::RefPtr<Gtk::TextMark> prompt_begin; // Beginning of input prompt
133 Glib::RefPtr<Gtk::TextTag> prompt_tag, input_tag, output_tag, warning_tag, error_tag;
134 TextViewProxy input; // Input prompt
135 Gtk::TextView tv; // Main view
136 Gtk::ScrolledWindow win; // Main window
137 };
138
139 }
Definition Console.hh:53
bool updating_buffer
Set while we rewrite the buffer ourselves, so that the GTK4 buffer-changed handler does not mistake i...
Definition Console.hh:83
bool handle_key_press(guint keyval, Gdk::ModifierType state)
The keys the console interprets itself (Return, Up, Down).
Definition Console.cc:55
std::string temp_in
Definition Console.hh:78
bool on_key_press_event(GdkEventKey *key_event) override
Definition Console.cc:147
void sync_to_view()
Mirror this (model) buffer and its selection into the parent's visible buffer.
Definition Console.cc:129
Console & parent
Definition Console.hh:77
void remember_partial_input()
Remember the partially typed line, so that walking down off the end of the history can restore it.
Definition Console.cc:120
std::list< std::string >::iterator history_ptr
Definition Console.hh:80
void set_text_quietly(const Glib::ustring &text)
Replace the buffer contents without it counting as user editing.
Definition Console.cc:110
size_t history_max_length
Definition Console.hh:71
std::list< std::string > history
Definition Console.hh:79
Definition Console.hh:18
Glib::RefPtr< Gtk::TextTag > warning_tag
Definition Console.hh:133
Glib::RefPtr< Gtk::TextBuffer > buffer
Definition Console.hh:124
Glib::RefPtr< Gtk::TextTag > error_tag
Definition Console.hh:133
std::queue< std::pair< std::string, Glib::RefPtr< Gtk::TextMark > > > run_queue
Definition Console.hh:127
void process_message_queue()
Definition Console.cc:430
Glib::Dispatcher run
Definition Console.hh:121
std::queue< nlohmann::json > message_queue
Definition Console.hh:126
Gtk::ScrolledWindow win
Definition Console.hh:136
void scroll_to_bottom()
Definition Console.cc:325
Glib::RefPtr< Gtk::TextTag > input_tag
Definition Console.hh:133
std::map< uint64_t, Glib::RefPtr< Gtk::TextMark > > cells
Definition Console.hh:129
void set_input(const Glib::ustring &line, size_t range_start=std::string::npos, size_t range_end=std::string::npos)
Definition Console.cc:331
Glib::Dispatcher dispatch_message
Definition Console.hh:120
uint64_t server_cell_id
Definition Console.hh:130
void insert_graphic(uint64_t cell_id, const std::string &bytes)
Definition Console.cc:398
void insert_tex(uint64_t cell_id, const std::shared_ptr< TeXEngine::TeXRequest > &content)
Definition Console.cc:416
TeXEngine tex_engine
Definition Console.hh:128
Glib::RefPtr< Gtk::TextMark > prompt_begin
Definition Console.hh:132
Gtk::TextView tv
Definition Console.hh:135
void send_input(const std::string &code)
Definition Console.cc:276
uint64_t get_server_cell()
Definition Console.cc:376
Position
Definition Console.hh:20
std::string collect
Definition Console.hh:125
Glib::RefPtr< Gtk::TextMark > input_begin
Definition Console.hh:131
Gtk::Widget & focus_widget()
The widget that carries the keyboard focus: on gtkmm-3 the visible view, which forwards to the proxy;...
Definition Console.cc:249
Glib::RefPtr< Gtk::TextTag > prompt_tag
Definition Console.hh:133
std::string grab_input(uint64_t &id)
Definition Console.cc:309
void insert_text(uint64_t cell_id, const std::string &text, const Glib::RefPtr< Gtk::TextTag > &tag)
Definition Console.cc:392
TextViewProxy input
Definition Console.hh:134
void create_cell(uint64_t parent_id, uint64_t cell_id)
Definition Console.cc:359
Glib::RefPtr< Gtk::TextTag > output_tag
Definition Console.hh:133
bool needs_focus
Definition Console.hh:123
void signal_message(const nlohmann::json &msg)
Definition Console.cc:319
void initialize()
Definition Console.cc:266
void prompt(bool continuation, bool newline=false)
Definition Console.cc:349
TeXEngine is used to convert LaTeX strings into PNG images.
Definition TeXEngine.hh:23
Functions to handle the exchange properties of two or more symbols in a product.
Definition Adjform.cc:83
void set(rset_t::iterator &num, multiplier_t fac)
Definition Storage.cc:1180