1 /*
2  * gnote
3  *
4  * Copyright (C) 2011-2017,2019,2021 Aurimas Cernius
5  * Copyright (C) 2009 Hubert Figuiere
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 
23 
24 #ifndef _NOTEWINDOW_HPP__
25 #define _NOTEWINDOW_HPP__
26 
27 #include <gtkmm/accelgroup.h>
28 #include <gtkmm/checkbutton.h>
29 #include <gtkmm/grid.h>
30 #include <gtkmm/searchentry.h>
31 #include <gtkmm/toolbutton.h>
32 #include <gtkmm/popovermenu.h>
33 #include <gtkmm/textview.h>
34 #include <gtkmm/scrolledwindow.h>
35 
36 #include "mainwindowembeds.hpp"
37 #include "note.hpp"
38 #include "undo.hpp"
39 #include "utils.hpp"
40 #include "notebuffer.hpp"
41 #include "preferences.hpp"
42 #include "tag.hpp"
43 
44 namespace gnote {
45 
46 class IconManager;
47 
48 
49 class NoteTextMenu
50   : public Gtk::PopoverMenu
51 {
52 public:
53   NoteTextMenu(EmbeddableWidget & widget, const Glib::RefPtr<NoteBuffer> & buffer, UndoManager& undo_manager);
54   void set_accels(utils::GlobalKeybinder & keybinder);
55   void refresh_state();
56 
57   sigc::signal<void, utils::GlobalKeybinder> signal_set_accels;
58 protected:
59   virtual void on_show() override;
60 
61 private:
62   void on_widget_foregrounded();
63   void on_widget_backgrounded();
64   void refresh_sizing_state();
65   void link_clicked();
66   void font_clicked(const char *action, const Glib::VariantBase & state, void (NoteTextMenu::*func)());
67   void bold_clicked(const Glib::VariantBase & state);
68   void bold_pressed();
69   void italic_clicked(const Glib::VariantBase & state);
70   void italic_pressed();
71   void strikeout_clicked(const Glib::VariantBase & state);
72   void strikeout_pressed();
73   void highlight_clicked(const Glib::VariantBase & state);
74   void highlight_pressed();
75   Gtk::Widget *create_font_size_item(const char *label, const char *markup, const char *size);
76   void font_style_clicked(const char * tag);
77   void font_size_activated(const Glib::VariantBase & state);
78   void undo_clicked();
79   void redo_clicked();
80   void undo_changed();
81   void toggle_bullets_clicked(const Glib::VariantBase&);
82   void increase_indent_clicked(const Glib::VariantBase&);
83   void increase_indent_pressed();
84   void decrease_indent_clicked(const Glib::VariantBase&);
85   void decrease_indent_pressed();
86   void increase_font_clicked();
87   void decrease_font_clicked();
88   Gtk::Widget *create_font_item(const char *action, const char *label, const char *markup);
89 
90   EmbeddableWidget     &m_widget;
91   Glib::RefPtr<NoteBuffer> m_buffer;
92   UndoManager          &m_undo_manager;
93   bool                  m_event_freeze;
94   std::vector<sigc::connection> m_signal_cids;
95 };
96 
97 class NoteFindHandler
98 {
99 public:
100   NoteFindHandler(Note & );
101   void perform_search(const Glib::ustring & text);
102   bool goto_next_result();
103   bool goto_previous_result();
104 private:
105   struct Match
106   {
107     Glib::RefPtr<NoteBuffer>     buffer;
108     Glib::RefPtr<Gtk::TextMark>  start_mark;
109     Glib::RefPtr<Gtk::TextMark>  end_mark;
110     bool                         highlighting;
111   };
112 
113   void jump_to_match(const Match & match);
114   void perform_search (bool scroll_to_hit);
115   void update_sensitivity();
116   void update_search();
117   void note_changed_timeout();
118   void highlight_matches(bool);
119   void cleanup_matches();
120   void find_matches_in_buffer(const Glib::RefPtr<NoteBuffer> & buffer,
121                               const std::vector<Glib::ustring> & words,
122                               std::vector<Match> & matches);
123 
124   Note           & m_note;
125   std::vector<Match> m_current_matches;
126 };
127 
128 class NoteWindow
129   : public Gtk::Grid
130   , public EmbeddableWidget
131   , public SearchableItem
132   , public HasEmbeddableToolbar
133   , public HasActions
134 {
135 public:
136   NoteWindow(Note &, IGnote &);
137   ~NoteWindow();
138 
139   virtual Glib::ustring get_name() const override;
140   void set_name(const Glib::ustring & name);
141   virtual void foreground() override;
142   virtual void background() override;
143   virtual void hint_size(int & width, int & height) override;
144   virtual void size_internals() override;
145 
146   virtual void perform_search(const Glib::ustring & text) override;
147   virtual bool supports_goto_result() override;
148   virtual bool goto_next_result() override;
149   virtual bool goto_previous_result() override;
150 
151   // use co-variant return
152   virtual Gtk::Grid *embeddable_toolbar() override;
153 
154   virtual std::vector<PopoverWidget> get_popover_widgets() override;
155 
set_size(int width,int height)156   void set_size(int width, int height)
157     {
158       m_width = width;
159       m_height = height;
160     }
editor() const161   Gtk::TextView * editor() const
162     {
163       return m_editor;
164     }
text_menu() const165   Gtk::PopoverMenu * text_menu() const
166     {
167       return m_text_menu;
168     }
get_accel_group()169   const Glib::RefPtr<Gtk::AccelGroup> & get_accel_group()
170     {
171       return m_accel_group;
172     }
get_find_handler()173   NoteFindHandler & get_find_handler()
174     {
175       return m_find_handler;
176     }
177   void enabled(bool enable);
enabled() const178   bool enabled() const
179     {
180       return m_enabled;
181     }
182 private:
183   static Glib::RefPtr<Gio::Icon> get_icon_pin_active(IconManager & icon_manager);
184   static Glib::RefPtr<Gio::Icon> get_icon_pin_down(IconManager & icon_manager);
185 
186   void on_delete_button_clicked(const Glib::VariantBase&);
187   void on_selection_mark_set(const Gtk::TextIter&, const Glib::RefPtr<Gtk::TextMark>&);
188   void on_populate_popup(Gtk::Menu*);
189   Gtk::Grid *make_toolbar();
190   Gtk::Grid * make_template_bar();
191   void on_untemplate_button_click();
192   void on_save_size_check_button_toggled();
193   void on_save_selection_check_button_toggled();
194   void on_save_title_check_button_toggled();
195   void on_note_tag_added(const NoteBase&, const Tag::Ptr&);
196   void on_note_tag_removed(const NoteBase::Ptr&, const Glib::ustring&);
197   void link_button_clicked();
198   void open_help_activate();
199   void change_depth_right_handler();
200   void change_depth_left_handler();
201   void add_accel_group(Gtk::Window &);
202   void remove_accel_group(Gtk::Window &);
203   void on_pin_status_changed(const Note &, bool);
204   void on_pin_button_clicked(const Glib::VariantBase & state);
205   void on_text_button_clicked();
206 
207   Note                        & m_note;
208   IGnote                      & m_gnote;
209   Glib::ustring                 m_name;
210   int                           m_height;
211   int                           m_width;
212   Glib::RefPtr<Gtk::AccelGroup> m_accel_group;
213   Gtk::Grid                    *m_embeddable_toolbar;
214   NoteTextMenu                 *m_text_menu;
215   Gtk::TextView                *m_editor;
216   Gtk::ScrolledWindow          *m_editor_window;
217   NoteFindHandler              m_find_handler;
218   sigc::connection              m_delete_note_slot;
219   sigc::connection              m_important_note_slot;
220   Gtk::Grid                    *m_template_widget;
221   Gtk::CheckButton             *m_save_size_check_button;
222   Gtk::CheckButton             *m_save_selection_check_button;
223   Gtk::CheckButton             *m_save_title_check_button;
224 
225   utils::GlobalKeybinder       *m_global_keys;
226   bool                         m_enabled;
227 
228   Tag::Ptr m_template_tag;
229   Tag::Ptr m_template_save_size_tag;
230   Tag::Ptr m_template_save_selection_tag;
231   Tag::Ptr m_template_save_title_tag;
232 };
233 
234 
235 
236 
237 
238 }
239 
240 #endif
241