Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "GUIBase.hh"
23 #include "NotebookCanvas.hh"
24 #include "../common/TeXEngine.hh"
25 
29 
30 class Cadabra;
31 
32 namespace 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  virtual void on_connect() override;
54  virtual void on_disconnect(const std::string&) override;
55  virtual void on_network_error() override;
56  virtual void on_kernel_runstatus(bool) override;
57 
58  virtual void process_data() override;
59 
60  // TeX stuff
62  double scale; // highdpi scale
63 
64  // Handler for vertical scrollbar changes.
65  bool on_vscroll_changed(Gtk::ScrollType, double);
66 
67  // Handler for mouse wheel events.
68  // bool on_mouse_wheel(GdkEventButton*);
69 
70  // Handler for scroll events.
71  bool on_scroll(GdkEventScroll*);
72 
73  // When something inside the large notebook canvas changes, we need
74  // to make sure that the current cell stays into view (if we are
75  // editing that cell). We can only do that once all size info is
76  // known, which is when the scrolledwindow gets its size_allocate
77  // signal. Here's the handler for it.
78  void on_scroll_size_allocate(Gtk::Allocation&);
79 
80  // Ensure that the current cell is visible. This will assume
81  // that all size allocations of widgets inside the scrolled window
82  // have been made; it only does scrolling, based on the current
83  // allocations.
85 
86  void set_name(const std::string&);
87  void set_title_prefix(const std::string&);
88 
89  void load_file(const std::string& notebook_contents);
90 
91 
92  protected:
93  virtual bool on_key_press_event(GdkEventKey*) override;
94  virtual bool on_delete_event(GdkEventAny*) override;
95  virtual bool on_configure_event(GdkEventConfigure *cfg) override;
96 
97  DTree::iterator current_cell;
98 
99  bool handle_outbox_select(GdkEventButton *, DTree::iterator it);
100  DTree::iterator selected_cell;
101  void unselect_output_cell();
102  void on_outbox_copy(Glib::RefPtr<Gtk::Clipboard> refClipboard, DTree::iterator it);
103 
104  private:
106 
107  // Main handler which fires whenever the Client object signals
108  // that the document is changing or the network status is modified.
109  // Runs on the GUI thread.
110 
111  Glib::Dispatcher dispatcher;
112 
113  // GUI elements.
114 
115  Glib::RefPtr<Gtk::ActionGroup> actiongroup;
116  Glib::RefPtr<Gtk::UIManager> uimanager;
117 
118  Gtk::VBox topbox;
119  Gtk::HBox supermainbox;
120  Gtk::VBox mainbox;
121 // Gtk::HBox buttonbox;
122  Gtk::HBox statusbarbox;
123 
124  // All canvasses which are stored in the ...
125  // These pointers are managed by gtkmm.
126  std::vector<NotebookCanvas *> canvasses;
128 
129  // Buttons
130 // Gtk::Button b_kill, b_run, b_run_to, b_run_from, b_help, b_stop, b_undo, b_redo;
131 
132  // Status bar
133  Gtk::ProgressBar progressbar;
134  Gtk::Spinner kernel_spinner;
137 
138  // GUI data which is the autoritative source for things displayed in
139  // the status bars declared above. These strings are filled on the
140  // compute thread and then updated into the gui on the gui thread.
141 
142  std::mutex status_mutex;
144 
145  // Name and modification data.
146  void update_title();
147  void set_stop_sensitive(bool);
148  std::string name, title_prefix;
150 
151  // Menu and button callbacks.
152  void on_file_new();
153  void on_file_open();
154  void on_file_close();
155  void on_file_save();
156  void on_file_save_as();
157  void on_file_export_html();
159  void on_file_export_latex();
160  void on_file_export_python();
161  void on_file_quit();
162  bool quit_safeguard(bool quit);
163 
164  void on_edit_undo();
165  void on_edit_copy();
166  Glib::RefPtr<Gtk::Action> action_copy, action_paste;
167  void on_edit_paste();
168  void on_edit_insert_above();
169  void on_edit_insert_below();
170  void on_edit_delete();
171  void on_edit_split();
172  void on_edit_cell_is_latex();
173  void on_edit_cell_is_python();
174 
175  void on_view_split();
176  void on_view_close();
177 
178  void on_run_cell();
179  void on_run_runall();
180  void on_run_runtocursor();
181  void on_run_stop();
182 
183  void on_prefs_font_size(int num);
184 
185  void on_help_about();
186  void on_help() const;
187 
188  void on_kernel_restart();
189 
191  void on_clipboard_get(Gtk::SelectionData&, guint info);
192  void on_clipboard_clear();
194 
195  // FIXME: move to DocumentThread
196  std::string save(const std::string& fn) const;
197 
198  // Todo deque processing logic. This gets called by the dispatcher, but it
199  // is also allowed to call this from within NotebookWindow itself. The important
200  // thing is that it is run on the GUI thread.
201  void process_todo_queue();
202 
203  void on_crash_window_closed(int);
205 
206  // The following are handlers that get called when the cell
207  // gets focus, the content of a cell is changed, the user
208  // requests to run it (shift-enter). The last two parameters are
209  // always the cell in the DTree and the canvas number.
210 
211  bool cell_got_focus(DTree::iterator, int);
212  bool cell_toggle_visibility(DTree::iterator it, int);
213  bool cell_content_insert(const std::string&, int, DTree::iterator, int);
214  bool cell_content_erase(int, int, DTree::iterator, int);
215  bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed);
216  bool cell_content_changed(const std::string& content, DTree::iterator it, int canvas_number);
217 
218  void dim_output_cells(DTree::iterator it);
219 
220  // Handler for callbacks from TeXView cells.
221 
222  bool on_tex_error(const std::string&, DTree::iterator);
223 
224  // Styling through CSS
225  void setup_css_provider();
226  Glib::RefPtr<Gtk::CssProvider> css_provider;
227  Glib::RefPtr<Gio::Settings> settings;
228  void on_text_scaling_factor_changed(const std::string& key);
229 
231  DTree::iterator follow_cell;
232 
233  class Prefs {
234  public:
235  Prefs();
236 
238  };
239 
242 
243 
244  // Transition animations.
245 #if GTKMM_MINOR_VERSION>=10
246  std::vector<Gtk::Revealer *> to_reveal;
247 #endif
248  bool idle_handler();
249  };
250 
251 };
bool handle_outbox_select(GdkEventButton *, DTree::iterator it)
Definition: NotebookWindow.cc:1597
DTree::iterator selected_cell
Definition: NotebookWindow.hh:100
void dim_output_cells(DTree::iterator it)
Definition: NotebookWindow.cc:934
NotebookWindow(Cadabra *, bool read_only=false)
Definition: NotebookWindow.cc:27
bool on_scroll(GdkEventScroll *)
Definition: NotebookWindow.cc:833
std::string clipboard_txt
Definition: NotebookWindow.hh:193
TeXEngine engine
Definition: NotebookWindow.hh:61
void on_file_open()
Definition: NotebookWindow.cc:1047
bool cell_content_erase(int, int, DTree::iterator, int)
Definition: NotebookWindow.cc:921
Cadabra * cdbapp
Definition: NotebookWindow.hh:105
virtual size_t get_cursor_position(const DTree &, DTree::iterator) override
Retrieve the position of the cursor in the current cell.
Definition: NotebookWindow.cc:771
A base class with all the logic to manipulate a Cadabra notebook document.
Definition: DocumentThread.hh:38
void on_edit_paste()
Definition: NotebookWindow.cc:1314
bool modified
Definition: NotebookWindow.hh:149
Glib::Dispatcher dispatcher
Definition: NotebookWindow.hh:111
virtual void on_disconnect(const std::string &) override
Definition: NotebookWindow.cc:409
void unselect_output_cell()
Definition: NotebookWindow.cc:1583
void on_view_split()
Definition: NotebookWindow.cc:1385
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:396
void on_file_quit()
Definition: NotebookWindow.cc:1290
int last_configure_width
Definition: NotebookWindow.hh:230
std::vector< NotebookCanvas * > canvasses
Definition: NotebookWindow.hh:126
bool cell_content_execute(DTree::iterator, int, bool shift_enter_pressed)
Definition: NotebookWindow.cc:961
bool quit_safeguard(bool quit)
Definition: NotebookWindow.cc:1252
Gtk::Spinner kernel_spinner
Definition: NotebookWindow.hh:134
Prefs()
Definition: NotebookWindow.cc:22
void on_file_save_as()
Definition: NotebookWindow.cc:1113
virtual void on_kernel_runstatus(bool) override
Definition: NotebookWindow.cc:423
bool on_tex_error(const std::string &, DTree::iterator)
Definition: NotebookWindow.cc:1000
void on_run_stop()
Definition: NotebookWindow.cc:1435
void on_file_export_python()
Definition: NotebookWindow.cc:1184
Glib::RefPtr< Gtk::ActionGroup > actiongroup
Definition: NotebookWindow.hh:115
Prefs prefs
Definition: NotebookWindow.hh:240
std::mutex status_mutex
Definition: NotebookWindow.hh:142
void on_clipboard_clear()
Definition: NotebookWindow.cc:1657
~NotebookWindow()
Definition: NotebookWindow.cc:329
Gtk::Label kernel_label
Definition: NotebookWindow.hh:136
Gtk::ProgressBar progressbar
Definition: NotebookWindow.hh:133
void on_scroll_size_allocate(Gtk::Allocation &)
Definition: NotebookWindow.cc:842
void on_crash_window_closed(int)
Definition: NotebookWindow.cc:473
void on_file_export_latex()
Definition: NotebookWindow.cc:1164
bool cell_toggle_visibility(DTree::iterator it, int)
Definition: NotebookWindow.cc:859
void on_file_close()
Definition: NotebookWindow.cc:1041
double scale
Definition: NotebookWindow.hh:62
virtual bool on_key_press_event(GdkEventKey *) override
Definition: NotebookWindow.cc:478
int quit(void *)
Definition: Server.cc:246
void on_edit_insert_above()
Definition: NotebookWindow.cc:1318
bool read_only
Definition: NotebookWindow.hh:149
void load_file(const std::string &notebook_contents)
Definition: NotebookWindow.cc:1084
void on_file_new()
Definition: NotebookWindow.cc:1028
virtual bool on_configure_event(GdkEventConfigure *cfg) override
Definition: NotebookWindow.cc:342
virtual void remove_cell(const DTree &, DTree::iterator) override
Remove a single cell.
Definition: NotebookWindow.cc:678
bool cell_content_changed(const std::string &content, DTree::iterator it, int canvas_number)
Definition: NotebookWindow.cc:887
DTree::iterator follow_cell
Definition: NotebookWindow.hh:231
int font_step
Definition: NotebookWindow.hh:237
std::string kernel_string
Definition: NotebookWindow.hh:143
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:746
Definition: NotebookWindow.hh:233
Each notebook has one main window which controls it.
Definition: NotebookWindow.hh:39
void set_name(const std::string &)
Definition: NotebookWindow.cc:1078
void on_kernel_restart()
Definition: NotebookWindow.cc:1440
bool kernel_spinner_status
Definition: NotebookWindow.hh:135
Glib::RefPtr< Gtk::Action > action_paste
Definition: NotebookWindow.hh:166
DTree::iterator current_cell
Definition: NotebookWindow.hh:97
TeXEngine is used to convert LaTeX strings into PNG images.
Definition: TeXEngine.hh:26
bool on_vscroll_changed(Gtk::ScrollType, double)
Definition: NotebookWindow.cc:818
Gtk::Label status_label
Definition: NotebookWindow.hh:136
void on_edit_delete()
Definition: NotebookWindow.cc:1340
std::string clipboard_cdb
Definition: NotebookWindow.hh:193
Gtk::VBox mainbox
Definition: NotebookWindow.hh:120
void on_edit_cell_is_python()
Definition: NotebookWindow.cc:1365
Glib::RefPtr< Gtk::CssProvider > css_provider
Definition: NotebookWindow.hh:226
void set_title_prefix(const std::string &)
Definition: NotebookWindow.cc:367
void on_file_save()
Definition: NotebookWindow.cc:1093
bool is_configured
Definition: NotebookWindow.hh:241
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:732
void on_view_close()
Definition: NotebookWindow.cc:1395
Glib::RefPtr< Gtk::Action > action_copy
Definition: NotebookWindow.hh:166
virtual bool on_delete_event(GdkEventAny *) override
Definition: NotebookWindow.cc:333
void process_todo_queue()
Definition: NotebookWindow.cc:430
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:722
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:502
const char info[]
Definition: Snoop.hh:154
int current_canvas
Definition: NotebookWindow.hh:127
bool crash_window_hidden
Definition: NotebookWindow.hh:204
bool cell_got_focus(DTree::iterator, int)
Definition: NotebookWindow.cc:951
tree< DataCell > DTree
Definition: DataCell.hh:106
void on_edit_copy()
Definition: NotebookWindow.cc:1303
bool cell_content_insert(const std::string &, int, DTree::iterator, int)
Definition: NotebookWindow.cc:908
void scroll_current_cell_into_view()
Definition: NotebookWindow.cc:784
Glib::RefPtr< Gtk::UIManager > uimanager
Definition: NotebookWindow.hh:116
void on_clipboard_get(Gtk::SelectionData &, guint info)
Clipboard handling.
Definition: NotebookWindow.cc:1646
Gtk::HBox supermainbox
Definition: NotebookWindow.hh:119
std::string save(const std::string &fn) const
Definition: NotebookWindow.cc:1227
void on_run_cell()
Definition: NotebookWindow.cc:1406
void on_file_export_html_segment()
Definition: NotebookWindow.cc:1204
void set_stop_sensitive(bool)
Definition: NotebookWindow.cc:388
void on_edit_undo()
Definition: NotebookWindow.cc:1298
virtual void on_network_error() override
Definition: NotebookWindow.cc:416
void on_prefs_font_size(int num)
Definition: NotebookWindow.cc:1524
void on_file_export_html()
Definition: NotebookWindow.cc:1144
Gtk::HBox statusbarbox
Definition: NotebookWindow.hh:122
void on_help_about()
Definition: NotebookWindow.cc:1480
void on_edit_cell_is_latex()
Definition: NotebookWindow.cc:1375
void on_outbox_copy(Glib::RefPtr< Gtk::Clipboard > refClipboard, DTree::iterator it)
Definition: NotebookWindow.cc:1619
std::string title_prefix
Definition: NotebookWindow.hh:148
void on_text_scaling_factor_changed(const std::string &key)
Definition: NotebookWindow.cc:1507
void on_run_runtocursor()
Definition: NotebookWindow.cc:1431
void on_run_runall()
Definition: NotebookWindow.cc:1419
void on_edit_split()
Definition: NotebookWindow.cc:1358
virtual void on_connect() override
Network status is propagated from the ComputeThread to the.
Definition: NotebookWindow.cc:402
void on_help() const
Definition: NotebookWindow.cc:1447
std::string name
Definition: NotebookWindow.hh:148
std::string status_string
Definition: NotebookWindow.hh:143
Glib::RefPtr< Gio::Settings > settings
Definition: NotebookWindow.hh:227
Gtk::VBox topbox
Definition: NotebookWindow.hh:118
void on_edit_insert_below()
Definition: NotebookWindow.cc:1329
void update_title()
Definition: NotebookWindow.cc:372
bool idle_handler()
Definition: NotebookWindow.cc:1572