1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_file_editor_h)
27 #define octave_file_editor_h 1
28 
29 #include <list>
30 #include <map>
31 
32 #include <QAction>
33 #include <QCloseEvent>
34 #include <QDragEnterEvent>
35 #include <QDropEvent>
36 #include <QMenuBar>
37 #include <QStackedWidget>
38 #include <QStatusBar>
39 #include <QTabWidget>
40 #include <QToolBar>
41 
42 #include "file-editor-interface.h"
43 #include "file-editor-tab.h"
44 #include "find-dialog.h"
45 #include "tab-bar.h"
46 
47 namespace octave
48 {
49   class base_qobject;
50 
51   // subclassed QTabWidget for using custom tabbar
52 
53   class file_editor_tab_widget : public QTabWidget
54   {
55     Q_OBJECT
56 
57   public:
58 
59     file_editor_tab_widget (QWidget *p);
60 
61     ~file_editor_tab_widget (void) = default;
62 
63     tab_bar * get_tab_bar (void) const;
64 
65     std::list<file_editor_tab *> tab_list (void) const;
66   };
67 
68   // the class for the file editor
69 
70   class file_editor : public file_editor_interface
71   {
72     Q_OBJECT
73 
74   public:
75 
76     // struct that allows to sort with respect to the tab index
77     struct session_data
78     {
79       int index;
80       int line;
81       QString file_name;
82       QString new_file_name;
83       QString encoding;
84 
85       bool operator < (const session_data& other) const
86       {
87         return index < other.index;
88       }
89     };
90 
91     file_editor (QWidget *p, base_qobject& oct_qobj);
92 
93     ~file_editor (void);
94 
get_mru_menu(void)95     QMenu * get_mru_menu (void) { return m_mru_file_menu; }
96 
debug_menu(void)97     QMenu * debug_menu (void) { return m_debug_menu; }
98 
toolbar(void)99     QToolBar * toolbar (void) { return m_tool_bar; }
100 
101     void insert_global_actions (QList<QAction*>);
102 
103     enum shared_actions_idx
104     {
105       NEW_SCRIPT_ACTION = 0,
106       NEW_FUNCTION_ACTION,
107       OPEN_ACTION,
108       FIND_FILES_ACTION,
109       UNDO_ACTION,
110       COPY_ACTION,
111       PASTE_ACTION,
112       SELECTALL_ACTION
113     };
114 
115     void handle_enter_debug_mode (void);
116     void handle_exit_debug_mode (void);
117 
118     void check_actions (void);
119     void empty_script (bool startup, bool visible);
120     void restore_session (gui_settings *settings);
121 
122   signals:
123 
124     void fetab_settings_changed (const gui_settings *settings);
125     void fetab_change_request (const QWidget *ID);
126     // Save is a ping-pong type of communication
127     void fetab_save_file (const QWidget *ID, const QString& fileName,
128                           bool remove_on_success);
129     // No fetab_open, functionality in editor
130     // No fetab_new, functionality in editor
131     void fetab_context_help (const QWidget *ID, bool);
132     void fetab_context_edit (const QWidget *ID);
133     void fetab_save_file (const QWidget *ID);
134     void fetab_save_file_as (const QWidget *ID);
135     void fetab_print_file (const QWidget *ID);
136     void fetab_run_file (const QWidget *ID, bool step_into = false);
137     void fetab_context_run (const QWidget *ID);
138     void fetab_toggle_bookmark (const QWidget *ID);
139     void fetab_next_bookmark (const QWidget *ID);
140     void fetab_previous_bookmark (const QWidget *ID);
141     void fetab_remove_bookmark (const QWidget *ID);
142     void fetab_toggle_breakpoint (const QWidget *ID);
143     void fetab_next_breakpoint (const QWidget *ID);
144     void fetab_previous_breakpoint (const QWidget *ID);
145     void fetab_remove_all_breakpoints (const QWidget *ID);
146     void fetab_comment_selected_text (const QWidget *ID, bool);
147     void fetab_uncomment_selected_text (const QWidget *ID);
148     void fetab_indent_selected_text (const QWidget *ID);
149     void fetab_unindent_selected_text (const QWidget *ID);
150     void fetab_smart_indent_line_or_selected_text (const QWidget *ID);
151     void fetab_convert_eol (const QWidget *ID, QsciScintilla::EolMode eol_mode);
152     void fetab_goto_line (const QWidget *ID, int line = -1);
153     void fetab_move_match_brace (const QWidget *ID, bool select);
154     void fetab_completion (const QWidget*);
155     void fetab_insert_debugger_pointer (const QWidget *ID, int line = -1);
156     void fetab_delete_debugger_pointer (const QWidget *ID, int line = -1);
157     void fetab_do_breakpoint_marker (bool insert, const QWidget *ID,
158                                      int line = -1, const QString& = "");
159     void fetab_set_focus (const QWidget *ID);
160     void fetab_scintilla_command (const QWidget *ID, unsigned int sci_msg);
161 
162     void fetab_zoom_in (const QWidget *ID);
163     void fetab_zoom_out (const QWidget *ID);
164     void fetab_zoom_normal (const QWidget *ID);
165 
166     void fetab_set_directory (const QString& dir);
167     void fetab_recover_from_exit (void);
168 
169     void edit_area_changed (octave_qscintilla *edit_area);
170 
171     void request_settings_dialog (const QString&);
172     void request_open_file_external (const QString& file_name, int line);
173     void file_loaded_signal (void);
174 
175     void editor_tabs_changed_signal (bool);
176     void request_dbcont_signal (void);
177 
178   public slots:
179 
180     void activate (void);
181     void set_focus (QWidget *fet);
182     void enable_menu_shortcuts (bool);
183     void save_session (void);
184     bool check_closing (void);
185     void handle_tab_ready_to_close (void);
186 
187     void request_new_file (const QString& commands);
188     void request_close_file (bool);
189     void request_close_all_files (bool);
190     void request_close_other_files (bool);
191     void request_mru_open_file (QAction *action);
192     void request_print_file (bool);
193 
194     void request_redo (bool);
195     void request_cut (bool);
196     void request_context_help (bool);
197     void request_context_doc (bool);
198     void request_context_edit (bool);
199     void request_save_file (bool);
200     void request_save_file_as (bool);
201     void request_run_file (bool);
202     void request_step_into_file ();
203     void request_context_run (bool);
204     void request_toggle_bookmark (bool);
205     void request_next_bookmark (bool);
206     void request_previous_bookmark (bool);
207     void request_remove_bookmark (bool);
208 
209     void request_move_match_brace (bool);
210     void request_sel_match_brace (bool);
211     void request_toggle_breakpoint (bool);
212     void request_next_breakpoint (bool);
213     void request_previous_breakpoint (bool);
214     void request_remove_breakpoint (bool);
215 
216     void request_delete_start_word (bool);
217     void request_delete_end_word (bool);
218     void request_delete_start_line (bool);
219     void request_delete_end_line (bool);
220     void request_delete_line (bool);
221     void request_copy_line (bool);
222     void request_cut_line (bool);
223     void request_duplicate_selection (bool);
224     void request_transpose_line (bool);
225 
226     void request_comment_selected_text (bool);
227     void request_uncomment_selected_text (bool);
228     void request_comment_var_selected_text (bool);
229 
230     void request_upper_case (bool);
231     void request_lower_case (bool);
232     void request_indent_selected_text (bool);
233     void request_unindent_selected_text (bool);
234     void request_smart_indent_line_or_selected_text (void);
235     void request_conv_eol_windows (bool);
236     void request_conv_eol_unix (bool);
237     void request_conv_eol_mac (bool);
238 
239     void request_find (bool);
240     void request_find_next (bool);
241     void request_find_previous (bool);
242 
243     void request_goto_line (bool);
244     void request_completion (bool);
245 
246     void handle_file_name_changed (const QString& fileName,
247                                    const QString& toolTip,
248                                    bool modified);
249     void handle_tab_close_request (int index);
250     void handle_tab_remove_request (void);
251     void active_tab_changed (int index);
252     void handle_editor_state_changed (bool enableCopy, bool is_octave_file);
253     void handle_mru_add_file (const QString& file_name, const QString& encoding);
254     void check_conflict_save (const QString& fileName, bool remove_on_success);
255 
256     void handle_insert_debugger_pointer_request (const QString& file, int line);
257     void handle_delete_debugger_pointer_request (const QString& file, int line);
258     void handle_update_breakpoint_marker_request (bool insert,
259                                                   const QString& file, int line,
260                                                   const QString& cond);
261 
262     void handle_edit_file_request (const QString& file);
263 
264     void handle_file_remove (const QString&, const QString&);
265     void handle_file_renamed (bool load_new = true);
266 
267     // Tells the editor to react on changed settings.
268     void notice_settings (const gui_settings *settings);
269 
270     void set_shortcuts (void);
271 
272     void handle_visibility (bool visible);
273 
274     void update_octave_directory (const QString& dir);
275 
276     void toplevel_change (bool toplevel);
277 
278     void handle_autoc_cancelled (void);
279 
280     file_editor_tab* reset_focus (void);
281 
282   protected slots:
283 
284     void copyClipboard (void);
285     void pasteClipboard (void);
286     void selectAll (void);
287     void do_undo (void);
288 
289   private slots:
290 
291     void request_open_file (const QString& fileName,
292                             const QString& encoding = QString (),
293                             int line = -1, bool debug_pointer = false,
294                             bool breakpoint_marker = false, bool insert = true,
295                             const QString& cond = "", int index = -1);
296     void request_preferences (bool);
297     void request_styles_preferences (bool);
298 
299     void show_line_numbers (bool);
300     void show_white_space (bool);
301     void show_eol_chars (bool);
302     void show_indent_guides (bool);
303     void show_long_line (bool);
304     void show_toolbar (bool);
305     void show_statusbar (bool);
306     void show_hscrollbar (bool);
307     void zoom_in (bool);
308     void zoom_out (bool);
309     void zoom_normal (bool);
310 
311     void create_context_menu (QMenu *);
312     void edit_status_update (bool, bool);
313 
314   protected:
315 
316     void closeEvent (QCloseEvent *event);
317     void dragEnterEvent (QDragEnterEvent *event);
318     void dropEvent (QDropEvent *event);
319     void focusInEvent (QFocusEvent *e);
320 
321   private:
322 
323     file_editor_tab * make_file_editor_tab (const QString& directory = "");
324 
325     bool is_editor_console_tabbed (void);
326     void construct (void);
327     void add_file_editor_tab (file_editor_tab *f, const QString& fn,
328                               int index = -1);
329     void mru_menu_update (void);
330     bool call_custom_editor (const QString& file_name = QString (), int line = -1);
331 
332     void toggle_preference (const gui_pref& preference);
333 
334     void handle_dir_remove (const QString& old_name, const QString& new_name);
335 
336     bool editor_tab_has_focus (void);
337 
338     void find_create (void);
339 
340     file_editor_tab * find_tab_widget (const QString& openFileName);
341     QAction * add_action (QMenu *menu, const QString& text,
342                           const char *member, QWidget *receiver = nullptr);
343     QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
344                           const char *member, QWidget *receiver = nullptr);
345 
346     QMenu * add_menu (QMenuBar *p, QString text);
347 
348     int m_number_of_tabs;
349     QHash<QMenu*, QStringList> m_hash_menu_text;
350 
351     QString m_ced;
352 
353     QMenuBar *m_menu_bar;
354     QToolBar *m_tool_bar;
355     QMenu *m_debug_menu;
356 
357     QAction *m_new_action;
358     QAction *m_new_function_action;
359     QAction *m_open_action;
360 
361     QAction *m_upper_case_action;
362     QAction *m_lower_case_action;
363     QAction *m_comment_selection_action;
364     QAction *m_comment_var_selection_action;
365     QAction *m_uncomment_selection_action;
366     QAction *m_indent_selection_action;
367     QAction *m_unindent_selection_action;
368     QAction *m_smart_indent_line_or_selection_action;
369     QAction *m_conv_eol_windows_action;
370     QAction *m_conv_eol_unix_action;
371     QAction *m_conv_eol_mac_action;
372 
373     QAction *m_copy_action;
374     QAction *m_cut_action;
375     QAction *m_paste_action;
376     QAction *m_selectall_action;
377     QAction *m_context_help_action;
378     QAction *m_context_doc_action;
379 
380     QAction *m_show_linenum_action;
381     QAction *m_show_whitespace_action;
382     QAction *m_show_eol_action;
383     QAction *m_show_indguide_action;
384     QAction *m_show_longline_action;
385     QAction *m_show_toolbar_action;
386     QAction *m_show_statusbar_action;
387     QAction *m_show_hscrollbar_action;
388     QAction *m_zoom_in_action;
389     QAction *m_zoom_out_action;
390     QAction *m_zoom_normal_action;
391 
392     QAction *m_delete_start_word_action;
393     QAction *m_delete_end_word_action;
394     QAction *m_delete_start_line_action;
395     QAction *m_delete_end_line_action;
396     QAction *m_delete_line_action;
397     QAction *m_copy_line_action;
398     QAction *m_cut_line_action;
399     QAction *m_duplicate_selection_action;
400     QAction *m_transpose_line_action;
401 
402     QAction *m_find_action;
403     QAction *m_find_next_action;
404     QAction *m_find_previous_action;
405     QAction *m_find_files_action;
406     QAction *m_goto_line_action;
407     QAction *m_completion_action;
408 
409     QAction *m_move_to_matching_brace;
410     QAction *m_sel_to_matching_brace;
411     QAction *m_next_bookmark_action;
412     QAction *m_previous_bookmark_action;
413     QAction *m_toggle_bookmark_action;
414     QAction *m_remove_bookmark_action;
415 
416     QAction *m_print_action;
417     QAction *m_run_action;
418     QAction *m_run_selection_action;
419 
420     QAction *m_edit_function_action;
421     QAction *m_popdown_mru_action;
422     QAction *m_save_action;
423     QAction *m_save_as_action;
424     QAction *m_close_action;
425     QAction *m_close_all_action;
426     QAction *m_close_others_action;
427 
428     QAction *m_redo_action;
429     QAction *m_undo_action;
430 
431     QAction *m_preferences_action;
432     QAction *m_styles_preferences_action;
433 
434     QAction *m_switch_left_tab_action;
435     QAction *m_switch_right_tab_action;
436     QAction *m_move_tab_left_action;
437     QAction *m_move_tab_right_action;
438     QAction *m_sort_tabs_action;
439 
440     QAction *m_toggle_breakpoint_action;
441     QAction *m_next_breakpoint_action;
442     QAction *m_previous_breakpoint_action;
443     QAction *m_remove_all_breakpoints_action;
444 
445     bool m_copy_action_enabled;
446     bool m_undo_action_enabled;
447 
448     QMenu *m_edit_menu;
449     QMenu *m_edit_cmd_menu;
450     QMenu *m_edit_fmt_menu;
451     QMenu *m_edit_nav_menu;
452     QMenu *m_fileMenu;
453     QMenu *m_view_editor_menu;
454 
455     file_editor_tab_widget *m_tab_widget;
456 
457     int m_marker_breakpoint;
458 
459     bool m_closing_canceled;
460     bool m_closed;
461     bool m_no_focus;
462 
463     enum { MaxMRUFiles = 10 };
464     QMenu *m_mru_file_menu;
465     QAction *m_mru_file_actions[MaxMRUFiles];
466     QStringList m_mru_files;
467     QStringList m_mru_files_encodings;
468 
469     QPointer<find_dialog> m_find_dialog;
470 
471     // List of data on temporarily closed files for later reloading.
472     QList<session_data> m_tmp_closed_files;
473   };
474 }
475 
476 #endif
477