Cadabra
Computer algebra system for field theory problems
Loading...
Searching...
No Matches
NotebookWindow.hh
Go to the documentation of this file.
1
2#pragma once
3
4#include <gtkmm/window.h>
5#include <gtkmm/box.h>
6#include <gtkmm/progressbar.h>
7#include <gtkmm/spinner.h>
8#include <gtkmm/label.h>
9#include <gtkmm/stock.h>
10#include <gtkmm/button.h>
11#include <gtkmm/uimanager.h>
12#include <gtkmm/cssprovider.h>
13#include <glibmm/dispatcher.h>
14#include <giomm/settings.h>
15#include <gtkmm/action.h>
16
17#include <thread>
18#include <mutex>
19
20#include "nlohmann/json.hpp"
21
22#include "DocumentThread.hh"
23#include "ComputeThread.hh"
24#include "Console.hh"
25#include "GUIBase.hh"
26#include "NotebookCanvas.hh"
28#include "DiffViewer.hh"
29
30class Cadabra;
31
32namespace cadabra {
33
38
39 class NotebookWindow : public Gtk::Window, public DocumentThread, public GUIBase {
40 public:
41 NotebookWindow(Cadabra *, bool read_only=false);
43
44 // Virtual functions from GUIBase.
45
46 virtual void add_cell(const DTree&, DTree::iterator, bool) override;
47 virtual void remove_cell(const DTree&, DTree::iterator) override;
48 virtual void remove_all_cells() override;
49 virtual void update_cell(const DTree&, DTree::iterator) override;
50 virtual void position_cursor(const DTree&, DTree::iterator, int pos) override;
51 virtual size_t get_cursor_position(const DTree&, DTree::iterator) override;
52
53 void select_range(const DTree&, DTree::iterator, int start, int len);
54
55 virtual void on_connect() override;
56 virtual void on_disconnect(const std::string&) override;
57 virtual void on_network_error() override;
58 virtual void on_kernel_runstatus(bool) override;
59
60 virtual void process_data() override;
61
62 // TeX stuff
64 double scale; // total scale factor (hdpi and textscale)
65 double display_scale; // hdpi scale only
66
67 // Handler for vertical scrollbar changes.
68 bool on_vscroll_changed(Gtk::ScrollType, double);
69
70 // Handler for mouse wheel events.
71 // bool on_mouse_wheel(GdkEventButton*);
72
73 // Handler for scroll events.
75
76 // When something inside the large notebook canvas changes, we need
77 // to make sure that the current cell stays into view (if we are
78 // editing that cell). We can only do that once all size info is
79 // known, which is when the scrolledwindow gets its size_allocate
80 // signal. Here's the handler for it.
81 void on_scroll_size_allocate(Gtk::Allocation&);
82
83 // Ensure that the current cell is visible. This will assume
84 // that all size allocations of widgets inside the scrolled window
85 // have been made; it only does scrolling, based on the current
86 // allocations. Calls `scroll_cell_into_view`.
88
89 // Ensure that the indicated cell is visible.
90 void scroll_cell_into_view(DTree::iterator cell);
91
92 void set_name(const std::string&);
93 void set_title_prefix(const std::string&);
94
95 void load_file(const std::string& notebook_contents);
97 void on_help_register();
98
99 void set_statusbar_message(const std::string& message = "", int line = -1, int col = -1);
100
102 void select_git_path();
103 void compare_to_file();
104 void compare_git_latest();
105 void compare_git_choose();
107 void compare_git(const std::string& commit_hash);
108 std::string run_git_command(const std::string& args);
109
110 virtual void set_compute_thread(ComputeThread* compute) override;
111
112 virtual void on_interactive_output(const nlohmann::json& msg) override;
113 virtual void set_progress(const std::string& msg, int cur_step, int total_steps) override;
114 protected:
115 virtual bool on_key_press_event(GdkEventKey*) override;
116 virtual bool on_delete_event(GdkEventAny*) override;
117 virtual bool on_configure_event(GdkEventConfigure *cfg) override;
118 virtual bool on_unhandled_error(const std::exception& err) override;
119
120 DTree::iterator current_cell;
121
122 bool handle_outbox_select(GdkEventButton *, DTree::iterator it);
123 DTree::iterator selected_cell;
125 void on_outbox_copy(Glib::RefPtr<Gtk::Clipboard> refClipboard, DTree::iterator it);
126
127 private:
129
130 std::vector<Glib::RefPtr<Gtk::Action>> default_actions;
131
132 // Main handler which fires whenever the Client object signals
133 // that the document is changing or the network status is modified.
134 // Runs on the GUI thread.
135
136 Glib::Dispatcher dispatcher;
137
138 // GUI elements.
139
140 Glib::RefPtr<Gtk::ActionGroup> actiongroup;
141 Glib::RefPtr<Gtk::UIManager> uimanager;
142
143 Gtk::VBox topbox;
144 Gtk::HBox supermainbox;
145 Gtk::Paned dragbox;
146 Gtk::VBox mainbox;
147 // Gtk::HBox buttonbox;
148 Gtk::SearchBar searchbar;
149 Gtk::HBox search_hbox;
150 Gtk::SearchEntry searchentry;
151 Gtk::CheckButton search_case_insensitive;
152 Gtk::Label search_result;
153 Gtk::HBox statusbarbox;
154
156 Gtk::Dialog console_win;
157
158 std::unique_ptr<DiffViewer> diffviewer;
159
160 // All canvasses which are stored in the ...
161 // These pointers are managed by gtkmm.
162 std::vector<NotebookCanvas *> canvasses;
164
165 // Buttons
166 // Gtk::Button b_kill, b_run, b_run_to, b_run_from, b_help, b_stop, b_undo, b_redo;
167
168 // Status bar
169 Gtk::ProgressBar progressbar;
170 Gtk::Spinner kernel_spinner;
173
174 // GUI data which is the autoritative source for things displayed in
175 // the status bars declared above. These strings are filled on the
176 // compute thread and then updated into the gui on the gui thread.
177
178 std::mutex status_mutex;
183 void update_status();
184
185 // Run the TeX engine on a separate thread, then call
186 // `dispatch_refresh` to update the display.
187 void tex_run_async();
188
189 // Name and modification data.
190 void update_title();
191 void set_stop_sensitive(bool);
192 std::string name, title_prefix;
194
195 // Menu and button callbacks.
196 void on_file_new();
197 void on_file_open();
198 void on_file_close();
199 void on_file_save();
200 void on_file_save_as();
202 void on_file_export_html();
206 void on_file_quit();
207 bool quit_safeguard(bool quit);
208
209 void on_edit_undo();
210 void on_edit_copy();
211 Glib::RefPtr<Gtk::Action> action_copy, action_paste;
212 void on_edit_paste();
215 void on_edit_delete();
216 void on_edit_split();
220 void on_edit_find();
221
222 void on_view_split();
223 void on_view_close();
224
225 void on_run_cell();
226 void on_run_runall();
227 void on_run_runtocursor();
228 void on_run_stop();
229
231 void on_prefs_font_size(int num);
232 void on_prefs_highlight_syntax(bool on);
235
236 void on_tools_options();
238
239 void on_help_about();
240 void on_help() const;
241
242 void on_kernel_restart();
243
246
248 void on_clipboard_get(Gtk::SelectionData&, guint info);
249 void on_clipboard_clear();
251
252 // FIXME: move to DocumentThread
253 std::string save(const std::string& fn) const;
254
258 void process_todo_queue();
259
265
269
270 void on_crash_window_closed(int);
272
273 // The following are handlers that get called when the cell
274 // gets focus, the content of a cell is changed, the user
275 // requests to run it (shift-enter). The last two parameters are
276 // always the cell in the DTree and the canvas number.
277
278 bool cell_got_focus(DTree::iterator, int);
279 bool cell_toggle_visibility(DTree::iterator it, int);
280 bool cell_content_insert(const std::string&, int, DTree::iterator, int);
281 bool cell_content_erase(int, int, DTree::iterator, int);
282 bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed);
283 bool cell_content_changed(DTree::iterator it, int i);
284 bool cell_complete_request(DTree::iterator it, int pos, int i);
285
286 void interactive_execute();
287
288 void dim_output_cells(DTree::iterator it);
289
290 // Handler for callbacks from TeXView cells.
291
292 bool on_tex_error(const std::string&, DTree::iterator);
293
294 // Styling through CSS
295 void load_css();
296 Glib::RefPtr<Gtk::CssProvider> css_provider;
297 Glib::RefPtr<Gio::Settings> settings;
298 void on_text_scaling_factor_changed(const std::string& key);
299
301 DTree::iterator follow_cell;
302
303 // Mutex to protect the variables below.
304 std::recursive_mutex tex_need_width_mutex;
305 std::unique_ptr<std::thread> tex_thread;
308 std::string tex_error_string;
309
310 std::pair<DTree::iterator, size_t> last_find_location;
311 std::string last_find_string;
312
314
315 Glib::RefPtr<Gtk::Action> menu_help_register;
316
317 // Transition animations.
318#if GTKMM_MINOR_VERSION>=10
319 std::vector<Gtk::Revealer *> to_reveal;
320#endif
321 bool idle_handler();
322 };
323
324 };
websocketpp::client< websocketpp::config::asio_client > client
Definition ComputeThread.cc:15
int quit(void *)
Definition Server.cc:281
The Cadabra notebook application.
Definition Cadabra.hh:11
Base class which talks to the server and sends Action objects back to the DocumentThread.
Definition ComputeThread.hh:36
Definition Console.hh:15
Position
Definition Console.hh:17
A base class with all the logic to manipulate a Cadabra notebook document.
Definition DocumentThread.hh:40
ComputeThread * compute
Definition DocumentThread.hh:128
Abstract base class with methods that need to be implemented by any GUI.
Definition GUIBase.hh:16
Each notebook has one main window which controls it.
Definition NotebookWindow.hh:39
void on_file_quit()
Definition NotebookWindow.cc:1849
void on_view_split()
Definition NotebookWindow.cc:2010
bool kernel_spinner_status
Definition NotebookWindow.hh:171
virtual void process_data() override
When the ComputeThread needs to modify the document, it stores an ActionBase object on the stack (see...
Definition NotebookWindow.cc:674
virtual void set_compute_thread(ComputeThread *compute) override
Let the notebook know about the ComputeThread so that it can send cells for evaluation.
Definition NotebookWindow.cc:2120
bool cell_content_erase(int, int, DTree::iterator, int)
Definition NotebookWindow.cc:1404
DTree::iterator current_cell
Definition NotebookWindow.hh:120
void tex_run_async()
Definition NotebookWindow.cc:718
Gtk::VBox topbox
Definition NotebookWindow.hh:143
std::unique_ptr< DiffViewer > diffviewer
Definition NotebookWindow.hh:158
Gtk::Label kernel_label
Definition NotebookWindow.hh:172
Gtk::HBox statusbarbox
Definition NotebookWindow.hh:153
virtual void set_progress(const std::string &msg, int cur_step, int total_steps) override
Definition NotebookWindow.cc:1054
std::string progress_string
Definition NotebookWindow.hh:179
void on_tools_options()
Definition NotebookWindow.cc:2543
void on_file_export_html()
Definition NotebookWindow.cc:1700
virtual void on_connect() override
Network status is propagated from the ComputeThread to the GUI using the following methods.
Definition NotebookWindow.cc:680
std::recursive_mutex tex_need_width_mutex
Definition NotebookWindow.hh:304
bool tex_running
Definition NotebookWindow.hh:306
double display_scale
Definition NotebookWindow.hh:65
Glib::RefPtr< Gtk::CssProvider > css_provider
Definition NotebookWindow.hh:296
void on_edit_copy()
Definition NotebookWindow.cc:1862
void compare_to_file()
Definition NotebookWindow.cc:2255
void on_scroll_size_allocate(Gtk::Allocation &)
Definition NotebookWindow.cc:1335
Glib::Dispatcher dispatch_update_status
Definition NotebookWindow.hh:182
virtual bool on_configure_event(GdkEventConfigure *cfg) override
Definition NotebookWindow.cc:590
void on_crash_window_closed(int)
Definition NotebookWindow.cc:813
void on_prefs_choose_colours()
Definition NotebookWindow.cc:2520
int status_col
Definition NotebookWindow.hh:181
bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed)
Definition NotebookWindow.cc:1470
void on_run_cell()
Definition NotebookWindow.cc:2031
void on_search_text_changed()
Search handling.
Definition NotebookWindow.cc:1935
int tex_need_width
Definition NotebookWindow.hh:307
int last_configure_width
Definition NotebookWindow.hh:300
void select_range(const DTree &, DTree::iterator, int start, int len)
Definition NotebookWindow.cc:1221
void process_todo_queue()
Todo deque processing logic.
Definition NotebookWindow.cc:766
virtual void remove_cell(const DTree &, DTree::iterator) override
Remove a single cell together with all its child cells.
Definition NotebookWindow.cc:1091
double progress_frac
Definition NotebookWindow.hh:180
virtual size_t get_cursor_position(const DTree &, DTree::iterator) override
Retrieve the position of the cursor in the current cell.
Definition NotebookWindow.cc:1243
Glib::RefPtr< Gtk::Action > action_paste
Definition NotebookWindow.hh:211
void on_run_runall()
Definition NotebookWindow.cc:2044
void compare_git_latest()
Definition NotebookWindow.cc:2327
Glib::RefPtr< Gtk::Action > action_copy
Definition NotebookWindow.hh:211
Glib::Dispatcher dispatcher
Definition NotebookWindow.hh:136
virtual bool on_unhandled_error(const std::exception &err) override
Definition NotebookWindow.cc:627
void interactive_execute()
Definition NotebookWindow.cc:1444
void on_edit_find()
Definition NotebookWindow.cc:1927
std::string kernel_string
Definition NotebookWindow.hh:179
void handle_thread_tex_error()
Handle a TeX error which occurred on a threaded TeX run (activated by tex_run_async) and is stored in...
Definition NotebookWindow.cc:620
bool read_only
Definition NotebookWindow.hh:193
DTree::iterator selected_cell
Definition NotebookWindow.hh:123
void load_file(const std::string &notebook_contents)
Definition NotebookWindow.cc:1604
Gtk::Label status_label
Definition NotebookWindow.hh:172
void on_edit_paste()
Definition NotebookWindow.cc:1873
void on_file_export_python()
Definition NotebookWindow.cc:1743
std::mutex status_mutex
Definition NotebookWindow.hh:178
Gtk::SearchBar searchbar
Definition NotebookWindow.hh:148
void refresh_highlighting()
Definition NotebookWindow.cc:2649
std::string last_find_string
Definition NotebookWindow.hh:311
Gtk::ProgressBar progressbar
Definition NotebookWindow.hh:169
void on_view_close()
Definition NotebookWindow.cc:2020
Gtk::CheckButton search_case_insensitive
Definition NotebookWindow.hh:151
void on_help_about()
Definition NotebookWindow.cc:2125
Cadabra * cdbapp
Definition NotebookWindow.hh:128
Gtk::Label search_result
Definition NotebookWindow.hh:152
void on_edit_insert_below()
Definition NotebookWindow.cc:1893
void on_edit_insert_above()
Definition NotebookWindow.cc:1877
void on_prefs_use_defaults()
Definition NotebookWindow.cc:2527
std::string run_git_command(const std::string &args)
Definition NotebookWindow.cc:2282
void on_help() const
Definition NotebookWindow.cc:2088
Glib::Dispatcher dispatch_tex_error
Definition NotebookWindow.hh:182
void on_edit_split()
Definition NotebookWindow.cc:1972
virtual bool on_key_press_event(GdkEventKey *) override
Definition NotebookWindow.cc:818
std::string clipboard_cdb
Definition NotebookWindow.hh:250
void on_help_register()
Definition NotebookWindow.cc:2155
void update_title()
Definition NotebookWindow.cc:639
std::vector< Glib::RefPtr< Gtk::Action > > default_actions
Definition NotebookWindow.hh:130
std::unique_ptr< std::thread > tex_thread
Definition NotebookWindow.hh:305
NotebookWindow(Cadabra *, bool read_only=false)
Definition NotebookWindow.cc:34
void on_edit_delete()
Definition NotebookWindow.cc:1909
bool is_configured
Definition NotebookWindow.hh:313
void on_clipboard_get(Gtk::SelectionData &, guint info)
Clipboard handling.
Definition NotebookWindow.cc:2733
Gtk::Paned dragbox
Definition NotebookWindow.hh:145
std::string clipboard_txt
Definition NotebookWindow.hh:250
void on_prefs_highlight_syntax(bool on)
Definition NotebookWindow.cc:2489
void on_file_save_as_jupyter()
Definition NotebookWindow.cc:1667
virtual void position_cursor(const DTree &, DTree::iterator, int pos) override
Position the cursor in the current canvas in the widget corresponding to the indicated cell.
Definition NotebookWindow.cc:1196
void set_stop_sensitive(bool)
Definition NotebookWindow.cc:666
void on_clipboard_clear()
Definition NotebookWindow.cc:2744
void unselect_output_cell()
Definition NotebookWindow.cc:2668
std::string tex_error_string
Definition NotebookWindow.hh:308
void scroll_cell_into_view(DTree::iterator cell)
Definition NotebookWindow.cc:1262
void on_file_export_latex()
Definition NotebookWindow.cc:1720
void on_run_stop()
Definition NotebookWindow.cc:2068
double scale
Definition NotebookWindow.hh:64
void on_prefs_font_size(int num)
Definition NotebookWindow.cc:2441
std::pair< DTree::iterator, size_t > last_find_location
Definition NotebookWindow.hh:310
void select_git_path()
Functionality for the diff viewer.
Definition NotebookWindow.cc:2245
void on_text_scaling_factor_changed(const std::string &key)
Definition NotebookWindow.cc:2223
bool crash_window_hidden
Definition NotebookWindow.hh:271
std::vector< NotebookCanvas * > canvasses
Definition NotebookWindow.hh:162
virtual void on_disconnect(const std::string &) override
Definition NotebookWindow.cc:697
bool cell_complete_request(DTree::iterator it, int pos, int i)
Definition NotebookWindow.cc:1451
void on_ignore_cell_on_import()
Definition NotebookWindow.cc:1990
bool on_vscroll_changed(Gtk::ScrollType, double)
Definition NotebookWindow.cc:1311
void on_edit_undo()
Definition NotebookWindow.cc:1857
virtual void on_interactive_output(const nlohmann::json &msg) override
Definition NotebookWindow.cc:1049
void on_file_new()
Definition NotebookWindow.cc:1538
void on_prefs_set_cv(Console::Position vis)
Definition NotebookWindow.cc:551
void refresh_after_tex_engine_run()
Refresh the display after a TeX engine run has completed.
Definition NotebookWindow.cc:614
Glib::RefPtr< Gtk::UIManager > uimanager
Definition NotebookWindow.hh:141
bool quit_safeguard(bool quit)
Definition NotebookWindow.cc:1811
virtual void on_network_error() override
Definition NotebookWindow.cc:704
void on_outbox_copy(Glib::RefPtr< Gtk::Clipboard > refClipboard, DTree::iterator it)
Definition NotebookWindow.cc:2706
virtual bool on_delete_event(GdkEventAny *) override
Definition NotebookWindow.cc:541
TeXEngine engine
Definition NotebookWindow.hh:63
void on_edit_cell_is_python()
Definition NotebookWindow.cc:1979
void dim_output_cells(DTree::iterator it)
Definition NotebookWindow.cc:1417
std::string save(const std::string &fn) const
Definition NotebookWindow.cc:1786
void on_kernel_restart()
Definition NotebookWindow.cc:2073
Glib::RefPtr< Gtk::Action > menu_help_register
Definition NotebookWindow.hh:315
void on_file_save()
Definition NotebookWindow.cc:1613
void scroll_current_cell_into_view()
Definition NotebookWindow.cc:1256
void on_run_runtocursor()
Definition NotebookWindow.cc:2056
Gtk::HBox supermainbox
Definition NotebookWindow.hh:144
void on_file_save_as()
Definition NotebookWindow.cc:1633
bool idle_handler()
Definition NotebookWindow.cc:2657
void compare_git_choose()
Definition NotebookWindow.cc:2359
Gtk::VBox mainbox
Definition NotebookWindow.hh:146
void set_title_prefix(const std::string &)
Definition NotebookWindow.cc:634
bool cell_got_focus(DTree::iterator, int)
Definition NotebookWindow.cc:1434
bool on_tex_error(const std::string &, DTree::iterator)
Definition NotebookWindow.cc:1510
Gtk::SearchEntry searchentry
Definition NotebookWindow.hh:150
bool modified
Definition NotebookWindow.hh:193
void update_status()
Definition NotebookWindow.cc:1070
Gtk::Dialog console_win
Definition NotebookWindow.hh:156
void on_edit_cell_is_latex()
Definition NotebookWindow.cc:1999
bool cell_toggle_visibility(DTree::iterator it, int)
Definition NotebookWindow.cc:1352
Console console
Definition NotebookWindow.hh:155
int status_line
Definition NotebookWindow.hh:181
void on_tools_clear_cache()
Definition NotebookWindow.cc:2631
void compare_git_specific()
Definition NotebookWindow.cc:2414
bool handle_outbox_select(GdkEventButton *, DTree::iterator it)
Definition NotebookWindow.cc:2682
virtual void update_cell(const DTree &, DTree::iterator) override
The basic manipulations that a GUI needs to implement are adding, removing and updating (refreshing t...
Definition NotebookWindow.cc:1180
std::string name
Definition NotebookWindow.hh:192
Gtk::HBox search_hbox
Definition NotebookWindow.hh:149
void set_statusbar_message(const std::string &message="", int line=-1, int col=-1)
Definition NotebookWindow.cc:655
virtual void add_cell(const DTree &, DTree::iterator, bool) override
Add a GUI cell corresponding to the document cell at the iterator.
Definition NotebookWindow.cc:876
bool cell_content_changed(DTree::iterator it, int i)
Definition NotebookWindow.cc:1380
~NotebookWindow()
Definition NotebookWindow.cc:472
bool on_scroll(GdkEventScroll *)
Definition NotebookWindow.cc:1326
void compare_git(const std::string &commit_hash)
Definition NotebookWindow.cc:2309
void on_file_open()
Definition NotebookWindow.cc:1565
std::string status_string
Definition NotebookWindow.hh:179
virtual void on_kernel_runstatus(bool) override
Definition NotebookWindow.cc:711
void on_file_export_html_segment()
Definition NotebookWindow.cc:1763
Glib::Dispatcher dispatch_refresh
Definition NotebookWindow.hh:182
DTree::iterator follow_cell
Definition NotebookWindow.hh:301
Gtk::Spinner kernel_spinner
Definition NotebookWindow.hh:170
Glib::RefPtr< Gio::Settings > settings
Definition NotebookWindow.hh:297
void on_file_close()
Definition NotebookWindow.cc:1551
bool cell_content_insert(const std::string &, int, DTree::iterator, int)
Definition NotebookWindow.cc:1391
void load_css()
Definition NotebookWindow.cc:476
int current_canvas
Definition NotebookWindow.hh:163
Glib::RefPtr< Gtk::ActionGroup > actiongroup
Definition NotebookWindow.hh:140
std::string title_prefix
Definition NotebookWindow.hh:192
void set_name(const std::string &)
Definition NotebookWindow.cc:1598
virtual void remove_all_cells() override
Remove all GUI cells from the display (used as a quick way to clear all before loading a new document...
Definition NotebookWindow.cc:1170
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
tree< DataCell > DTree
Definition DataCell.hh:108