-
Notifications
You must be signed in to change notification settings - Fork 107
/
menu.py
452 lines (416 loc) · 15.3 KB
/
menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# -----------------------------------------------------------------------
# Copyright (c) 2008-2024 Jendrik Seipp
#
# RedNotebook is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# RedNotebook is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with RedNotebook; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# -----------------------------------------------------------------------
import os
import webbrowser
from gi.repository import GdkPixbuf, Gtk
from rednotebook import info, storage
from rednotebook.gui import format_menu, insert_menu
from rednotebook.help import help_text
from rednotebook.util import filesystem, utils
MENUBAR_XML = """\
<ui>
<menubar name="MainMenuBar">
<menu action="Journal">
<menuitem action="New"/>
<menuitem action="Open"/>
<separator/>
<menuitem action="Save"/>
<menuitem action="SaveAs"/>
<separator/>
<menuitem action="Export"/>
<menuitem action="Backup"/>
<menuitem action="Statistics"/>
<separator/>
<menuitem action="Quit"/>
</menu>
<menu action="Edit">
<menuitem action="Undo"/>
<menuitem action="Redo"/>
<separator/>
<menuitem action="Cut"/>
<menuitem action="Copy"/>
<menuitem action="Paste"/>
<separator/>
<menuitem action="Fullscreen"/>
<separator/>
<menuitem action="Find"/>
<separator/>
<menuitem action="CheckSpelling"/>
<menuitem action="Options"/>
</menu>
{}
{}
<menu action="HelpMenu">
<menuitem action="Help"/>
<separator/>
<menuitem action="GiveFeedback"/>
<menuitem action="Donate"/>
<menuitem action="Translate"/>
<menuitem action="ReportBug"/>
<separator/>
<menuitem action="Info"/>
</menu>
</menubar>
</ui>""".format(
insert_menu.MENUBAR_XML,
format_menu.MENUBAR_XML,
)
class MainMenuBar:
def __init__(self, main_window, *args, **kwargs):
self.main_window = main_window
self.uimanager = main_window.uimanager
self.journal = self.main_window.journal
self.setup()
def get_actiongroup(self):
# Create an ActionGroup
actiongroup = Gtk.ActionGroup("MainMenuActionGroup")
# Create actions
actiongroup.add_actions(
[
("Journal", None, _("_Journal")),
(
"New",
None,
_("New"),
"<Ctrl>n",
_("Create a new journal. The old one will be saved"),
self.on_new_journal_button_activate,
),
(
"Open",
None,
_("Open"),
"<Ctrl>o",
_("Load an existing journal. The old journal will be saved"),
self.on_open_journal_button_activate,
),
("Save", None, _("Save"), "<Ctrl>s", None, self.on_save_button_clicked),
(
"SaveAs",
None,
_("Save As"),
"<Ctrl><Shift>s",
_(
"Save journal at a new location. The old journal files will also be saved"
),
self.on_save_as_menu_item_activate,
),
# Translators: Verb
(
"Export",
None,
_("Export"),
"<Ctrl>e",
_("Open the export assistant"),
self.on_export_menu_item_activate,
),
# Translators: Verb
(
"Backup",
None,
_("_Backup"),
None,
_("Save all the data in a zip archive"),
self.on_backup_activate,
),
(
"Statistics",
None,
_("S_tatistics"),
None,
_("Show some statistics about the journal"),
self.on_statistics_menu_item_activate,
),
(
"Quit",
None,
_("Quit"),
"<Ctrl>q",
_("Shutdown RedNotebook. It will not be sent to the tray."),
self.main_window.on_quit_activate,
),
("Edit", None, _("_Edit")),
(
"Undo",
None,
_("Undo"),
"<Ctrl>z",
_("Undo text or tag edits"),
self.on_undo,
),
(
"Redo",
None,
_("Redo"),
"<Ctrl>y",
_("Redo text or tag edits"),
self.on_redo,
),
(
"Cut",
None,
_("Cut"),
"<Ctrl>x",
None,
self.on_cut_menu_item_activate,
),
(
"Copy",
None,
_("Copy"),
"<Ctrl>c",
None,
self.on_copy_menu_item_activate,
),
(
"Paste",
None,
_("Paste"),
"<Ctrl>v",
None,
self.on_paste_menu_item_activate,
),
(
"Fullscreen",
None,
_("Fullscreen"),
"F11",
None,
self.on_fullscreen_menuitem_activate,
),
(
"Find",
None,
_("Find"),
"<Ctrl>f",
None,
self.on_find_menuitem_activate,
),
]
)
actiongroup.add_toggle_actions(
[
(
"CheckSpelling",
None,
_("Spell Check"),
"F7",
_("Underline misspelled words"),
self.on_checkspelling_menuitem_toggled,
)
]
)
actiongroup.add_actions(
[
(
"Options",
None,
_("Preferences"),
"<Ctrl><Alt>p",
None,
self.on_options_menuitem_activate,
),
("HelpMenu", None, _("_Help")),
(
"Help",
None,
_("Contents"),
"<Ctrl>h",
_("Open the RedNotebook documentation"),
self.on_help_menu_item_activate,
),
(
"Donate",
None,
_("Donate"),
None,
_("Support RedNotebook with a donation"),
self.on_donate,
),
(
"Translate",
None,
_("Translate RedNotebook"),
None,
_("Help translate RedNotebook to your language"),
self.on_translate,
),
(
"ReportBug",
None,
_("Report a Problem"),
None,
_("Fill out a short form about the problem"),
self.on_report_bug,
),
(
"GiveFeedback",
None,
_("Give Feedback"),
None,
_("How can we improve RedNotebook?"),
self.on_give_feedback,
),
("Info", None, _("About"), None, None, self.on_info_activate),
]
)
return actiongroup
def setup(self):
# Add the actiongroup to the uimanager
self.uimanager.insert_action_group(self.get_actiongroup(), 0)
# Add a UI description
self.uimanager.add_ui_from_string(MENUBAR_XML)
def get_menu_bar(self):
return self.uimanager.get_widget("/MainMenuBar")
def check_journal_dir(self, action, new_dir):
if not new_dir:
return False
title = _("Wrong directory")
# Check if dir exists and is no forbidden path (e.g. $HOME dir).
if not self.journal.dirs.is_valid_journal_path(new_dir):
self.journal.show_message(
_("You cannot use this directory for your journal:") + " %s" % new_dir,
title=title,
error=True,
)
return False
if action in ["new", "saveas"] and os.listdir(new_dir):
self.journal.show_message(
_("Please select an empty directory."), title=title, error=True
)
return False
elif action in ["open"] and not list(storage.get_journal_files(new_dir)):
self.journal.show_message(
_("This directory contains no journal files:") + " " + new_dir,
title=title,
error=True,
)
return False
return True
def select_journal(self, action, title, message):
new_dir = self.main_window.get_new_journal_dir(title, message)
if not self.check_journal_dir(action, new_dir):
return
if action == "saveas":
self.journal.dirs.data_dir = new_dir
self.journal.save_to_disk(saveas=True)
self.journal.open_journal(new_dir)
def on_new_journal_button_activate(self, widget):
msg = "{}\n{}".format(
_("Journals are saved in a directory, not in a single file."),
_("The directory name will be the title of the new journal."),
)
self.select_journal(
"new", _("Select an empty folder for your new journal"), msg
)
def on_open_journal_button_activate(self, widget):
self.select_journal(
"open",
_("Select an existing journal directory"),
_("The directory should contain your journal's data files"),
)
def on_save_button_clicked(self, widget):
self.journal.save_to_disk()
def on_save_as_menu_item_activate(self, widget):
# widget is None when we call this method after the journal could not be
# saved. Then another try won't succeed either.
if widget is not None:
self.journal.save_to_disk()
self.select_journal(
"saveas",
_("Select an empty folder for the new location of your journal"),
_("The directory name will be the new title of the journal"),
)
def on_undo(self, widget):
editor = self.main_window.day_text_field
editor.day_text_buffer.undo()
def on_redo(self, widget):
editor = self.main_window.day_text_field
editor.day_text_buffer.redo()
def on_copy_menu_item_activate(self, widget):
if self.main_window.preview_mode:
self.main_window.html_editor.copy_to_clipboard()
else:
self.main_window.day_text_field.day_text_view.emit("copy_clipboard")
def on_paste_menu_item_activate(self, widget):
if self.main_window.preview_mode:
pass
else:
self.main_window.day_text_field.day_text_view.emit("paste_clipboard")
def on_cut_menu_item_activate(self, widget):
if self.main_window.preview_mode:
pass
else:
self.main_window.day_text_field.day_text_view.emit("cut_clipboard")
def on_fullscreen_menuitem_activate(self, widget):
self.main_window.toggle_fullscreen()
def on_find_menuitem_activate(self, widget):
"""
Change to search page and put the cursor into the search box
"""
self.main_window.search_box.entry.grab_focus()
def on_checkspelling_menuitem_toggled(self, widget):
self.main_window.day_text_field.enable_spell_check(widget.get_active())
enabled = self.main_window.day_text_field.is_spell_check_enabled()
widget.set_active(enabled)
self.journal.config["spellcheck"] = int(enabled)
def on_options_menuitem_activate(self, widget):
self.main_window.options_manager.on_options_dialog()
def on_backup_activate(self, widget):
self.journal.archiver.backup()
def on_export_menu_item_activate(self, widget):
self.journal.save_old_day()
self.main_window.export_assistant.run()
def on_statistics_menu_item_activate(self, widget):
self.journal.stats.show_dialog(self.main_window.stats_dialog)
def on_help_menu_item_activate(self, widget):
temp_dir = self.journal.dirs.temp_dir
filesystem.write_file(os.path.join(temp_dir, "source.txt"), help_text)
html = self.journal.convert(
help_text,
"xhtml",
headers=[_("RedNotebook Documentation"), info.version, ""],
options={"toc": 1},
)
utils.show_html_in_browser(html, os.path.join(temp_dir, "help.html"))
def on_donate(self, widget):
webbrowser.open(info.donation_url)
def on_translate(self, widget):
webbrowser.open(info.translation_url)
def on_report_bug(self, widget):
webbrowser.open(info.bug_url)
def on_give_feedback(self, widget):
webbrowser.open(info.discussion_url)
def on_info_activate(self, widget):
self.info_dialog = self.main_window.builder.get_object("about_dialog")
self.info_dialog.set_transient_for(self.main_window.main_frame)
self.info_dialog.set_program_name(info.program_name)
self.info_dialog.set_version(info.version)
self.info_dialog.set_copyright(info.copyright_)
self.info_dialog.set_comments(info.tagline)
self.info_dialog.set_website(info.url)
self.info_dialog.set_website_label(info.url)
self.info_dialog.set_artists(info.artists)
self.info_dialog.set_authors(info.developers)
self.info_dialog.add_credit_section(_("Contributors:"), [info.contributors_url])
self.info_dialog.set_translator_credits(_("translator-credits"))
img_path = os.path.join(filesystem.image_dir, "rednotebook-icon", "rn-128.png")
self.info_dialog.set_logo(GdkPixbuf.Pixbuf.new_from_file(img_path))
self.info_dialog.set_license_type(Gtk.License.GPL_2_0)
self.info_dialog.run()
self.info_dialog.hide()