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 
16 #include <thread>
17 #include <mutex>
18 
19 #include "DocumentThread.hh"
20 #include "ComputeThread.hh"
21 #include "GUIBase.hh"
22 #include "NotebookCanvas.hh"
23 #include "../common/TeXEngine.hh"
24 
28 
29 class Cadabra;
30 
31 namespace cadabra {
32 
37 
38  class NotebookWindow : public Gtk::Window, public DocumentThread, public GUIBase {
39  public:
40  NotebookWindow(Cadabra *, bool read_only=false);
42 
43  // Virtual functions from GUIBase.
44 
45  virtual void add_cell(const DTree&, DTree::iterator, bool) override;
46  virtual void remove_cell(const DTree&, DTree::iterator) override;
47  virtual void remove_all_cells() override;
48  virtual void update_cell(const DTree&, DTree::iterator) override;
49  virtual void position_cursor(const DTree&, DTree::iterator, int pos) override;
50  virtual size_t get_cursor_position(const DTree&, DTree::iterator) override;
51 
52  virtual void on_connect() override;
53  virtual void on_disconnect(const std::string&) override;
54  virtual void on_network_error() override;
55  virtual void on_kernel_runstatus(bool) override;
56 
57  virtual void process_data() override;
58 
59  // TeX stuff
61  double scale; // highdpi scale
62 
63  // Handler for vertical scrollbar changes.
64  bool on_vscroll_changed(Gtk::ScrollType, double);
65 
66  // Handler for mouse wheel events.
67  // bool on_mouse_wheel(GdkEventButton*);
68 
69  // Handler for scroll events.
70  bool on_scroll(GdkEventScroll*);
71 
72  // When something inside the large notebook canvas changes, we need
73  // to make sure that the current cell stays into view (if we are
74  // editing that cell). We can only do that once all size info is
75  // known, which is when the scrolledwindow gets its size_allocate
76  // signal. Here's the handler for it.
77  void on_scroll_size_allocate(Gtk::Allocation&);
78 
79  // Ensure that the current cell is visible. This will assume
80  // that all size allocations of widgets inside the scrolled window
81  // have been made; it only does scrolling, based on the current
82  // allocations.
84 
85  void set_name(const std::string&);
86  void set_title_prefix(const std::string&);
87 
88  void load_file(const std::string& notebook_contents);
89 
90 
91  protected:
92  virtual bool on_key_press_event(GdkEventKey*) override;
93  virtual bool on_delete_event(GdkEventAny*) override;
94  virtual bool on_configure_event(GdkEventConfigure *cfg) override;
95 
96  DTree::iterator current_cell;
97 
98  private:
100 
101  // Main handler which fires whenever the Client object signals
102  // that the document is changing or the network status is modified.
103  // Runs on the GUI thread.
104 
105  Glib::Dispatcher dispatcher;
106 
107  // GUI elements.
108 
109  Glib::RefPtr<Gtk::ActionGroup> actiongroup;
110  Glib::RefPtr<Gtk::UIManager> uimanager;
111 
112  Gtk::VBox topbox;
113  Gtk::HBox supermainbox;
114  Gtk::VBox mainbox;
115 // Gtk::HBox buttonbox;
116  Gtk::HBox statusbarbox;
117 
118  // All canvasses which are stored in the ...
119  // These pointers are managed by gtkmm.
120  std::vector<NotebookCanvas *> canvasses;
122 
123  // Buttons
124 // Gtk::Button b_kill, b_run, b_run_to, b_run_from, b_help, b_stop, b_undo, b_redo;
125 
126  // Status bar
127  Gtk::ProgressBar progressbar;
128  Gtk::Spinner kernel_spinner;
131 
132  // GUI data which is the autoritative source for things displayed in
133  // the status bars declared above. These strings are filled on the
134  // compute thread and then updated into the gui on the gui thread.
135 
136  std::mutex status_mutex;
138 
139  // Name and modification data.
140  void update_title();
141  void set_stop_sensitive(bool);
142  std::string name, title_prefix;
144 
145  // Menu and button callbacks.
146  void on_file_new();
147  void on_file_open();
148  void on_file_close();
149  void on_file_save();
150  void on_file_save_as();
151  void on_file_export_html();
153  void on_file_export_latex();
154  void on_file_export_python();
155  void on_file_quit();
156  bool quit_safeguard(bool quit);
157 
158  void on_edit_undo();
159  void on_edit_insert_above();
160  void on_edit_insert_below();
161  void on_edit_delete();
162  void on_edit_split();
163  void on_edit_cell_is_latex();
164  void on_edit_cell_is_python();
165 
166  void on_view_split();
167  void on_view_close();
168 
169  void on_run_cell();
170  void on_run_runall();
171  void on_run_runtocursor();
172  void on_run_stop();
173 
174  void on_prefs_font_size(int num);
175 
176  void on_help_about();
177  void on_help() const;
178 
179  void on_kernel_restart();
180 
181  // FIXME: move to DocumentThread
182  std::string save(const std::string& fn) const;
183 
184  // Todo deque processing logic. This gets called by the dispatcher, but it
185  // is also allowed to call this from within NotebookWindow itself. The important
186  // thing is that it is run on the GUI thread.
187  void process_todo_queue();
188 
189  void on_crash_window_closed(int);
191 
192  // The following are handlers that get called when the cell
193  // gets focus, the content of a cell is changed, the user
194  // requests to run it (shift-enter). The last two parameters are
195  // always the cell in the DTree and the canvas number.
196 
197  bool cell_got_focus(DTree::iterator, int);
198  bool cell_toggle_visibility(DTree::iterator it, int);
199  bool cell_content_insert(const std::string&, int, DTree::iterator, int);
200  bool cell_content_erase(int, int, DTree::iterator, int);
201  bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed);
202  bool cell_content_changed(const std::string& content, DTree::iterator it, int canvas_number);
203 
204  void dim_output_cells(DTree::iterator it);
205 
206  // Handler for callbacks from TeXView cells.
207 
208  bool on_tex_error(const std::string&, DTree::iterator);
209 
210  // Styling through CSS
211  void setup_css_provider();
212  Glib::RefPtr<Gtk::CssProvider> css_provider;
213  Glib::RefPtr<Gio::Settings> settings;
214  void on_text_scaling_factor_changed(const std::string& key);
215 
217  DTree::iterator follow_cell;
218 
219  class Prefs {
220  public:
221  Prefs();
222 
224  };
225 
228 
229 
230  // Transition animations.
231 #if GTKMM_MINOR_VERSION>=10
232  std::vector<Gtk::Revealer *> to_reveal;
233 #endif
234  bool idle_handler();
235  };
236 
237 };
void dim_output_cells(DTree::iterator it)
Definition: NotebookWindow.cc:908
NotebookWindow(Cadabra *, bool read_only=false)
Definition: NotebookWindow.cc:27
bool on_scroll(GdkEventScroll *)
Definition: NotebookWindow.cc:810
TeXEngine engine
Definition: NotebookWindow.hh:60
void on_file_open()
Definition: NotebookWindow.cc:1019
bool cell_content_erase(int, int, DTree::iterator, int)
Definition: NotebookWindow.cc:896
Cadabra * cdbapp
Definition: NotebookWindow.hh:99
virtual size_t get_cursor_position(const DTree &, DTree::iterator) override
Retrieve the position of the cursor in the current cell.
Definition: NotebookWindow.cc:748
A base class with all the logic to manipulate a Cadabra notebook document.
Definition: DocumentThread.hh:38
bool modified
Definition: NotebookWindow.hh:143
Glib::Dispatcher dispatcher
Definition: NotebookWindow.hh:105
virtual void on_disconnect(const std::string &) override
Definition: NotebookWindow.cc:396
void on_view_split()
Definition: NotebookWindow.cc:1340
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:383
void on_file_quit()
Definition: NotebookWindow.cc:1262
int last_configure_width
Definition: NotebookWindow.hh:216
std::vector< NotebookCanvas * > canvasses
Definition: NotebookWindow.hh:120
bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed)
Definition: NotebookWindow.cc:933
bool quit_safeguard(bool quit)
Definition: NotebookWindow.cc:1224
Gtk::Spinner kernel_spinner
Definition: NotebookWindow.hh:128
Prefs()
Definition: NotebookWindow.cc:22
void on_file_save_as()
Definition: NotebookWindow.cc:1085
virtual void on_kernel_runstatus(bool) override
Definition: NotebookWindow.cc:410
bool on_tex_error(const std::string &, DTree::iterator)
Definition: NotebookWindow.cc:972
void on_run_stop()
Definition: NotebookWindow.cc:1390
void on_file_export_python()
Definition: NotebookWindow.cc:1156
Glib::RefPtr< Gtk::ActionGroup > actiongroup
Definition: NotebookWindow.hh:109
Prefs prefs
Definition: NotebookWindow.hh:226
std::mutex status_mutex
Definition: NotebookWindow.hh:136
~NotebookWindow()
Definition: NotebookWindow.cc:316
Gtk::Label kernel_label
Definition: NotebookWindow.hh:130
Gtk::ProgressBar progressbar
Definition: NotebookWindow.hh:127
void on_scroll_size_allocate(Gtk::Allocation &)
Definition: NotebookWindow.cc:819
void on_crash_window_closed(int)
Definition: NotebookWindow.cc:460
void on_file_export_latex()
Definition: NotebookWindow.cc:1136
bool cell_toggle_visibility(DTree::iterator it, int)
Definition: NotebookWindow.cc:836
void on_file_close()
Definition: NotebookWindow.cc:1013
double scale
Definition: NotebookWindow.hh:61
virtual bool on_key_press_event(GdkEventKey *) override
Definition: NotebookWindow.cc:465
int quit(void *)
Definition: Server.cc:244
void on_edit_insert_above()
Definition: NotebookWindow.cc:1275
bool read_only
Definition: NotebookWindow.hh:143
void load_file(const std::string &notebook_contents)
Definition: NotebookWindow.cc:1056
void on_file_new()
Definition: NotebookWindow.cc:1000
virtual bool on_configure_event(GdkEventConfigure *cfg) override
Definition: NotebookWindow.cc:329
virtual void remove_cell(const DTree &, DTree::iterator) override
Remove a single cell.
Definition: NotebookWindow.cc:655
bool cell_content_changed(const std::string &content, DTree::iterator it, int canvas_number)
Definition: NotebookWindow.cc:864
DTree::iterator follow_cell
Definition: NotebookWindow.hh:217
int font_step
Definition: NotebookWindow.hh:223
std::string kernel_string
Definition: NotebookWindow.hh:137
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:723
Definition: NotebookWindow.hh:219
Each notebook has one main window which controls it.
Definition: NotebookWindow.hh:38
void set_name(const std::string &)
Definition: NotebookWindow.cc:1050
void on_kernel_restart()
Definition: NotebookWindow.cc:1395
bool kernel_spinner_status
Definition: NotebookWindow.hh:129
DTree::iterator current_cell
Definition: NotebookWindow.hh:96
Class to display expressions in a format that Sympy can parse.
Definition: Algorithm.cc:1614
TeXEngine is used to convert LaTeX strings into PNG images.
Definition: TeXEngine.hh:26
bool on_vscroll_changed(Gtk::ScrollType, double)
Definition: NotebookWindow.cc:795
Gtk::Label status_label
Definition: NotebookWindow.hh:130
void on_edit_delete()
Definition: NotebookWindow.cc:1297
Gtk::VBox mainbox
Definition: NotebookWindow.hh:114
void on_edit_cell_is_python()
Definition: NotebookWindow.cc:1320
Glib::RefPtr< Gtk::CssProvider > css_provider
Definition: NotebookWindow.hh:212
void set_title_prefix(const std::string &)
Definition: NotebookWindow.cc:354
void on_file_save()
Definition: NotebookWindow.cc:1065
bool is_configured
Definition: NotebookWindow.hh:227
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:709
void on_view_close()
Definition: NotebookWindow.cc:1350
virtual bool on_delete_event(GdkEventAny *) override
Definition: NotebookWindow.cc:320
void process_todo_queue()
Definition: NotebookWindow.cc:417
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:699
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:489
int current_canvas
Definition: NotebookWindow.hh:121
bool crash_window_hidden
Definition: NotebookWindow.hh:190
bool cell_got_focus(DTree::iterator, int)
Definition: NotebookWindow.cc:925
tree< DataCell > DTree
Definition: DataCell.hh:105
bool cell_content_insert(const std::string &, int, DTree::iterator, int)
Definition: NotebookWindow.cc:884
void scroll_current_cell_into_view()
Definition: NotebookWindow.cc:761
Glib::RefPtr< Gtk::UIManager > uimanager
Definition: NotebookWindow.hh:110
Gtk::HBox supermainbox
Definition: NotebookWindow.hh:113
std::string save(const std::string &fn) const
Definition: NotebookWindow.cc:1199
void on_run_cell()
Definition: NotebookWindow.cc:1361
void on_file_export_html_segment()
Definition: NotebookWindow.cc:1176
void set_stop_sensitive(bool)
Definition: NotebookWindow.cc:375
void on_edit_undo()
Definition: NotebookWindow.cc:1270
virtual void on_network_error() override
Definition: NotebookWindow.cc:403
void on_prefs_font_size(int num)
Definition: NotebookWindow.cc:1479
void on_file_export_html()
Definition: NotebookWindow.cc:1116
Gtk::HBox statusbarbox
Definition: NotebookWindow.hh:116
void on_help_about()
Definition: NotebookWindow.cc:1435
void on_edit_cell_is_latex()
Definition: NotebookWindow.cc:1330
std::string title_prefix
Definition: NotebookWindow.hh:142
void on_text_scaling_factor_changed(const std::string &key)
Definition: NotebookWindow.cc:1462
void on_run_runtocursor()
Definition: NotebookWindow.cc:1386
void on_run_runall()
Definition: NotebookWindow.cc:1374
void on_edit_split()
Definition: NotebookWindow.cc:1313
virtual void on_connect() override
Network status is propagated from the ComputeThread to the.
Definition: NotebookWindow.cc:389
void on_help() const
Definition: NotebookWindow.cc:1402
std::string name
Definition: NotebookWindow.hh:142
std::string status_string
Definition: NotebookWindow.hh:137
Glib::RefPtr< Gio::Settings > settings
Definition: NotebookWindow.hh:213
Gtk::VBox topbox
Definition: NotebookWindow.hh:112
void on_edit_insert_below()
Definition: NotebookWindow.cc:1286
void update_title()
Definition: NotebookWindow.cc:359
bool idle_handler()
Definition: NotebookWindow.cc:1527