Cadabra
Computer algebra system for field theory problems
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 "DocumentThread.hh"
21 #include "ComputeThread.hh"
22 #include "Console.hh"
23 #include "GUIBase.hh"
24 #include "NotebookCanvas.hh"
25 #include "../common/TeXEngine.hh"
26 
30 
31 class Cadabra;
32 
33 namespace cadabra {
34 
39 
40  class NotebookWindow : public Gtk::Window, public DocumentThread, public GUIBase {
41  public:
42  NotebookWindow(Cadabra *, bool read_only=false);
44 
45  // Virtual functions from GUIBase.
46 
47  virtual void add_cell(const DTree&, DTree::iterator, bool) override;
48  virtual void remove_cell(const DTree&, DTree::iterator) override;
49  virtual void remove_all_cells() override;
50  virtual void update_cell(const DTree&, DTree::iterator) override;
51  virtual void position_cursor(const DTree&, DTree::iterator, int pos) override;
52  virtual size_t get_cursor_position(const DTree&, DTree::iterator) override;
53 
54  virtual void on_connect() override;
55  virtual void on_disconnect(const std::string&) override;
56  virtual void on_network_error() override;
57  virtual void on_kernel_runstatus(bool) override;
58 
59  virtual void process_data() override;
60 
61  // TeX stuff
63  double scale; // highdpi scale
64 
65  // Handler for vertical scrollbar changes.
66  bool on_vscroll_changed(Gtk::ScrollType, double);
67 
68  // Handler for mouse wheel events.
69  // bool on_mouse_wheel(GdkEventButton*);
70 
71  // Handler for scroll events.
72  bool on_scroll(GdkEventScroll*);
73 
74  // When something inside the large notebook canvas changes, we need
75  // to make sure that the current cell stays into view (if we are
76  // editing that cell). We can only do that once all size info is
77  // known, which is when the scrolledwindow gets its size_allocate
78  // signal. Here's the handler for it.
79  void on_scroll_size_allocate(Gtk::Allocation&);
80 
81  // Ensure that the current cell is visible. This will assume
82  // that all size allocations of widgets inside the scrolled window
83  // have been made; it only does scrolling, based on the current
84  // allocations.
86 
87  void set_name(const std::string&);
88  void set_title_prefix(const std::string&);
89 
90  void load_file(const std::string& notebook_contents);
91  void refresh_highlighting();
92  void on_help_register();
93 
94  virtual void set_compute_thread(ComputeThread* compute) override;
95 
96  virtual void on_interactive_output(const Json::Value& msg) override;
97 
98  protected:
99  virtual bool on_key_press_event(GdkEventKey*) override;
100  virtual bool on_delete_event(GdkEventAny*) override;
101  virtual bool on_configure_event(GdkEventConfigure *cfg) override;
102 
103  DTree::iterator current_cell;
104 
105  bool handle_outbox_select(GdkEventButton *, DTree::iterator it);
106  DTree::iterator selected_cell;
107  void unselect_output_cell();
108  void on_outbox_copy(Glib::RefPtr<Gtk::Clipboard> refClipboard, DTree::iterator it);
109 
110  private:
112 
113  std::vector<Glib::RefPtr<Gtk::Action>> default_actions;
114 
115  // Main handler which fires whenever the Client object signals
116  // that the document is changing or the network status is modified.
117  // Runs on the GUI thread.
118 
119  Glib::Dispatcher dispatcher;
120 
121  // GUI elements.
122 
123  Glib::RefPtr<Gtk::ActionGroup> actiongroup;
124  Glib::RefPtr<Gtk::UIManager> uimanager;
125 
126  Gtk::VBox topbox;
127  Gtk::HBox supermainbox;
128  Gtk::VBox mainbox;
129  // Gtk::HBox buttonbox;
130  Gtk::HBox statusbarbox;
131 
133  Gtk::Dialog console_win;
134 
135  // All canvasses which are stored in the ...
136  // These pointers are managed by gtkmm.
137  std::vector<NotebookCanvas *> canvasses;
139 
140  // Buttons
141  // Gtk::Button b_kill, b_run, b_run_to, b_run_from, b_help, b_stop, b_undo, b_redo;
142 
143  // Status bar
144  Gtk::ProgressBar progressbar;
145  Gtk::Spinner kernel_spinner;
148 
149  // GUI data which is the autoritative source for things displayed in
150  // the status bars declared above. These strings are filled on the
151  // compute thread and then updated into the gui on the gui thread.
152 
153  std::mutex status_mutex;
155 
156  // Name and modification data.
157  void update_title();
158  void set_stop_sensitive(bool);
159  std::string name, title_prefix;
161 
162  // Menu and button callbacks.
163  void on_file_new();
164  void on_file_open();
165  void on_file_close();
166  void on_file_save();
167  void on_file_save_as();
168  void on_file_export_html();
170  void on_file_export_latex();
171  void on_file_export_python();
172  void on_file_quit();
173  bool quit_safeguard(bool quit);
174 
175  void on_edit_undo();
176  void on_edit_copy();
177  Glib::RefPtr<Gtk::Action> action_copy, action_paste;
178  void on_edit_paste();
179  void on_edit_insert_above();
180  void on_edit_insert_below();
181  void on_edit_delete();
182  void on_edit_split();
183  void on_edit_cell_is_latex();
184  void on_edit_cell_is_python();
185 
186  void on_view_split();
187  void on_view_close();
188 
189  void on_run_cell();
190  void on_run_runall();
191  void on_run_runtocursor();
192  void on_run_stop();
193 
194  void on_prefs_set_cv(int vis);
195  void on_prefs_font_size(int num);
196  void on_prefs_highlight_syntax(int on);
198  void on_prefs_use_defaults();
199  void on_help_about();
200  void on_help() const;
201 
202  void on_kernel_restart();
203 
205  void on_clipboard_get(Gtk::SelectionData&, guint info);
206  void on_clipboard_clear();
208 
209  // FIXME: move to DocumentThread
210  std::string save(const std::string& fn) const;
211 
212  // Todo deque processing logic. This gets called by the dispatcher, but it
213  // is also allowed to call this from within NotebookWindow itself. The important
214  // thing is that it is run on the GUI thread.
215  void process_todo_queue();
216 
217  void on_crash_window_closed(int);
219 
220  // The following are handlers that get called when the cell
221  // gets focus, the content of a cell is changed, the user
222  // requests to run it (shift-enter). The last two parameters are
223  // always the cell in the DTree and the canvas number.
224 
225  bool cell_got_focus(DTree::iterator, int);
226  bool cell_toggle_visibility(DTree::iterator it, int);
227  bool cell_content_insert(const std::string&, int, DTree::iterator, int);
228  bool cell_content_erase(int, int, DTree::iterator, int);
229  bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed);
230  bool cell_content_changed(const std::string& content, DTree::iterator it, int canvas_number);
231 
232  void interactive_execute();
233 
234  void dim_output_cells(DTree::iterator it);
235 
236  // Handler for callbacks from TeXView cells.
237 
238  bool on_tex_error(const std::string&, DTree::iterator);
239 
240  // Styling through CSS
241  void load_css(const std::string&);
242  Glib::RefPtr<Gtk::CssProvider> css_provider;
243  Glib::RefPtr<Gio::Settings> settings;
244  void on_text_scaling_factor_changed(const std::string& key);
245 
247  DTree::iterator follow_cell;
248 
249 
251 
252  Glib::RefPtr<Gtk::Action> menu_help_register;
253 
254  // Transition animations.
255 #if GTKMM_MINOR_VERSION>=10
256  std::vector<Gtk::Revealer *> to_reveal;
257 #endif
258  bool idle_handler();
259  };
260 
261 };
bool handle_outbox_select(GdkEventButton *, DTree::iterator it)
Definition: NotebookWindow.cc:1841
DTree::iterator selected_cell
Definition: NotebookWindow.hh:106
void dim_output_cells(DTree::iterator it)
Definition: NotebookWindow.cc:1023
NotebookWindow(Cadabra *, bool read_only=false)
Definition: NotebookWindow.cc:26
bool on_scroll(GdkEventScroll *)
Definition: NotebookWindow.cc:922
std::string clipboard_txt
Definition: NotebookWindow.hh:207
std::string save(const std::string &fn) const
Definition: NotebookWindow.cc:1324
TeXEngine engine
Definition: NotebookWindow.hh:62
void on_prefs_highlight_syntax(int on)
Definition: NotebookWindow.cc:1754
void on_file_open()
Definition: NotebookWindow.cc:1141
bool cell_content_erase(int, int, DTree::iterator, int)
Definition: NotebookWindow.cc:1010
Cadabra * cdbapp
Definition: NotebookWindow.hh:111
ComputeThread * compute
Definition: DocumentThread.hh:117
virtual size_t get_cursor_position(const DTree &, DTree::iterator) override
Retrieve the position of the cursor in the current cell.
Definition: NotebookWindow.cc:860
void refresh_highlighting()
Definition: NotebookWindow.cc:1808
std::vector< Glib::RefPtr< Gtk::Action > > default_actions
Definition: NotebookWindow.hh:113
A base class with all the logic to manipulate a Cadabra notebook document.
Definition: DocumentThread.hh:39
void on_edit_paste()
Definition: NotebookWindow.cc:1411
bool modified
Definition: NotebookWindow.hh:160
Glib::Dispatcher dispatcher
Definition: NotebookWindow.hh:119
virtual void on_disconnect(const std::string &) override
Definition: NotebookWindow.cc:501
void unselect_output_cell()
Definition: NotebookWindow.cc:1827
void on_view_split()
Definition: NotebookWindow.cc:1484
Definition: Console.hh:27
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:487
void on_file_quit()
Definition: NotebookWindow.cc:1387
int last_configure_width
Definition: NotebookWindow.hh:246
void on_help() const
Definition: NotebookWindow.cc:1554
std::vector< NotebookCanvas * > canvasses
Definition: NotebookWindow.hh:137
bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed)
Definition: NotebookWindow.cc:1055
bool quit_safeguard(bool quit)
Definition: NotebookWindow.cc:1349
Gtk::Spinner kernel_spinner
Definition: NotebookWindow.hh:145
void on_file_save_as()
Definition: NotebookWindow.cc:1207
virtual void on_kernel_runstatus(bool) override
Definition: NotebookWindow.cc:515
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:1586
bool on_tex_error(const std::string &, DTree::iterator)
Definition: NotebookWindow.cc:1094
void on_run_stop()
Definition: NotebookWindow.cc:1542
void on_file_export_python()
Definition: NotebookWindow.cc:1281
Glib::RefPtr< Gtk::ActionGroup > actiongroup
Definition: NotebookWindow.hh:123
void on_prefs_choose_colours()
Definition: NotebookWindow.cc:1785
std::mutex status_mutex
Definition: NotebookWindow.hh:153
void on_clipboard_clear()
Definition: NotebookWindow.cc:1901
~NotebookWindow()
Definition: NotebookWindow.cc:365
Gtk::Label kernel_label
Definition: NotebookWindow.hh:147
Gtk::ProgressBar progressbar
Definition: NotebookWindow.hh:144
void on_scroll_size_allocate(Gtk::Allocation &)
Definition: NotebookWindow.cc:931
void on_crash_window_closed(int)
Definition: NotebookWindow.cc:570
void on_file_export_latex()
Definition: NotebookWindow.cc:1258
bool cell_toggle_visibility(DTree::iterator it, int)
Definition: NotebookWindow.cc:948
void on_file_close()
Definition: NotebookWindow.cc:1135
double scale
Definition: NotebookWindow.hh:63
virtual bool on_key_press_event(GdkEventKey *) override
Definition: NotebookWindow.cc:575
int quit(void *)
Definition: Server.cc:215
void on_edit_insert_above()
Definition: NotebookWindow.cc:1415
bool read_only
Definition: NotebookWindow.hh:160
void load_file(const std::string &notebook_contents)
Definition: NotebookWindow.cc:1178
void on_file_new()
Definition: NotebookWindow.cc:1122
virtual bool on_configure_event(GdkEventConfigure *cfg) override
Definition: NotebookWindow.cc:433
virtual void remove_cell(const DTree &, DTree::iterator) override
Remove a single cell.
Definition: NotebookWindow.cc:767
bool cell_content_changed(const std::string &content, DTree::iterator it, int canvas_number)
Definition: NotebookWindow.cc:976
Glib::RefPtr< Gtk::Action > menu_help_register
Definition: NotebookWindow.hh:252
DTree::iterator follow_cell
Definition: NotebookWindow.hh:247
std::string kernel_string
Definition: NotebookWindow.hh:154
The Cadabra notebook application.
Definition: Cadabra.hh:11
Abstract base class with methods that need to be implemented by any GUI.
Definition: GUIBase.hh:16
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:835
Each notebook has one main window which controls it.
Definition: NotebookWindow.hh:40
void set_name(const std::string &)
Definition: NotebookWindow.cc:1172
void on_kernel_restart()
Definition: NotebookWindow.cc:1547
bool kernel_spinner_status
Definition: NotebookWindow.hh:146
Glib::RefPtr< Gtk::Action > action_paste
Definition: NotebookWindow.hh:177
DTree::iterator current_cell
Definition: NotebookWindow.hh:103
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Algorithm.cc:1030
TeXEngine is used to convert LaTeX strings into PNG images.
Definition: TeXEngine.hh:26
bool on_vscroll_changed(Gtk::ScrollType, double)
Definition: NotebookWindow.cc:907
Gtk::Label status_label
Definition: NotebookWindow.hh:147
void on_edit_delete()
Definition: NotebookWindow.cc:1437
std::string clipboard_cdb
Definition: NotebookWindow.hh:207
Gtk::VBox mainbox
Definition: NotebookWindow.hh:128
void on_edit_cell_is_python()
Definition: NotebookWindow.cc:1462
Glib::RefPtr< Gtk::CssProvider > css_provider
Definition: NotebookWindow.hh:242
void set_title_prefix(const std::string &)
Definition: NotebookWindow.cc:458
void on_file_save()
Definition: NotebookWindow.cc:1187
bool is_configured
Definition: NotebookWindow.hh:250
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:821
void on_view_close()
Definition: NotebookWindow.cc:1494
Glib::RefPtr< Gtk::Action > action_copy
Definition: NotebookWindow.hh:177
virtual bool on_delete_event(GdkEventAny *) override
Definition: NotebookWindow.cc:387
void process_todo_queue()
Definition: NotebookWindow.cc:522
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:811
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:599
void load_css(const std::string &)
Definition: NotebookWindow.cc:369
const char info[]
Definition: Snoop.hh:158
int current_canvas
Definition: NotebookWindow.hh:138
bool crash_window_hidden
Definition: NotebookWindow.hh:218
bool cell_got_focus(DTree::iterator, int)
Definition: NotebookWindow.cc:1040
tree< DataCell > DTree
Definition: DataCell.hh:107
void on_edit_copy()
Definition: NotebookWindow.cc:1400
bool cell_content_insert(const std::string &, int, DTree::iterator, int)
Definition: NotebookWindow.cc:997
void scroll_current_cell_into_view()
Definition: NotebookWindow.cc:873
Glib::RefPtr< Gtk::UIManager > uimanager
Definition: NotebookWindow.hh:124
void on_clipboard_get(Gtk::SelectionData &, guint info)
Clipboard handling.
Definition: NotebookWindow.cc:1890
Gtk::HBox supermainbox
Definition: NotebookWindow.hh:127
void on_prefs_set_cv(int vis)
Definition: NotebookWindow.cc:397
void on_help_register()
Definition: NotebookWindow.cc:1621
void on_run_cell()
Definition: NotebookWindow.cc:1505
void on_file_export_html_segment()
Definition: NotebookWindow.cc:1301
void set_stop_sensitive(bool)
Definition: NotebookWindow.cc:479
void on_edit_undo()
Definition: NotebookWindow.cc:1395
virtual void on_network_error() override
Definition: NotebookWindow.cc:508
void on_prefs_font_size(int num)
Definition: NotebookWindow.cc:1706
void on_file_export_html()
Definition: NotebookWindow.cc:1238
Gtk::HBox statusbarbox
Definition: NotebookWindow.hh:130
Base class which talks to the server and sends Action objects back to the DocumentThread.
Definition: ComputeThread.hh:35
void on_help_about()
Definition: NotebookWindow.cc:1592
void on_prefs_use_defaults()
Definition: NotebookWindow.cc:1792
void on_edit_cell_is_latex()
Definition: NotebookWindow.cc:1473
virtual void on_interactive_output(const Json::Value &msg) override
Definition: NotebookWindow.cc:762
void on_outbox_copy(Glib::RefPtr< Gtk::Clipboard > refClipboard, DTree::iterator it)
Definition: NotebookWindow.cc:1863
std::string title_prefix
Definition: NotebookWindow.hh:159
void on_text_scaling_factor_changed(const std::string &key)
Definition: NotebookWindow.cc:1689
void on_run_runtocursor()
Definition: NotebookWindow.cc:1530
void on_run_runall()
Definition: NotebookWindow.cc:1518
Gtk::Dialog console_win
Definition: NotebookWindow.hh:133
void on_edit_split()
Definition: NotebookWindow.cc:1455
virtual void on_connect() override
Network status is propagated from the ComputeThread to the.
Definition: NotebookWindow.cc:493
void interactive_execute()
Definition: NotebookWindow.cc:1050
std::string name
Definition: NotebookWindow.hh:159
std::string status_string
Definition: NotebookWindow.hh:154
Glib::RefPtr< Gio::Settings > settings
Definition: NotebookWindow.hh:243
Gtk::VBox topbox
Definition: NotebookWindow.hh:126
Console console
Definition: NotebookWindow.hh:132
void on_edit_insert_below()
Definition: NotebookWindow.cc:1426
void update_title()
Definition: NotebookWindow.cc:463
bool idle_handler()
Definition: NotebookWindow.cc:1816