1 # This file is part of MyPaint.
2 # Copyright (C) 2011 by Ben O'Steen <bosteen@gmail.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
11 from gettext import gettext as _
15 import tileddrawwidget, document
17 from drawwindow import button_press_cb_abstraction, button_release_cb_abstraction
24 def stock_button_generic(stock_id, b):
26 img.set_from_stock(stock_id, gtk.ICON_SIZE_MENU)
30 def stock_button(stock_id):
32 return stock_button_generic(stock_id, b)
34 class ToolWidget (gtk.VBox):
36 tool_widget_title = _("Scratchpad")
38 stock_id = stock.TOOL_SCRATCHPAD
40 def __init__(self, app):
41 gtk.VBox.__init__(self)
43 #self.set_size_request(200, 250)
45 self.is_updating = False
48 load_button = self.load_button = stock_button(gtk.STOCK_OPEN)
49 load_button.set_tooltip_text(_("Load Scratchpad"))
50 save_as_button = self.save_as_button = stock_button(gtk.STOCK_SAVE_AS)
51 save_as_button.set_tooltip_text(_("Save Scratchpad as..."))
52 revert_button = self.revert_button = stock_button(gtk.STOCK_UNDO)
53 revert_button.set_tooltip_text(_("Revert Scratchpad"))
54 new_button = self.delete_button = stock_button(gtk.STOCK_NEW)
55 new_button.set_tooltip_text(_("New Scratchpad"))
57 load_button.connect('clicked', self.load_cb)
58 save_as_button.connect('clicked', self.save_as_cb)
59 revert_button.connect('clicked', self.revert_cb)
60 new_button.connect('clicked', self.new_cb)
62 buttons_hbox = gtk.HBox()
63 buttons_hbox.pack_start(new_button)
64 buttons_hbox.pack_start(load_button)
65 buttons_hbox.pack_start(save_as_button)
66 buttons_hbox.pack_start(revert_button)
68 scratchpad_view = app.scratchpad_doc.tdw
70 self.connect("button-press-event", self.button_press_cb)
71 self.connect("button-release-event",self.button_release_cb)
72 self.connect("destroy-event", self.save_cb)
73 self.connect("delete-event", self.save_cb)
75 scratchpad_box = gtk.EventBox()
76 scratchpad_box.add(scratchpad_view)
78 self.pack_start(scratchpad_box)
79 self.pack_start(buttons_hbox, expand=False)
82 doc = app.scratchpad_doc.model
83 doc.doc_observers.append(self.update)
85 # FIXME pull the scratchpad filename from preferences instead of this
86 # self.app.scratchpad_filename = self.scratchpad_filename = os.path.join(self.app.filehandler.get_scratchpad_prefix(), "scratchpad_default.ora")
88 self.update(app.scratchpad_doc)
90 def new_cb(self, action):
91 if os.path.isfile(self.app.filehandler.get_scratchpad_default()):
92 self.app.filehandler.open_scratchpad(self.app.filehandler.get_scratchpad_default())
94 self.app.scratchpad_doc.model.clear()
95 # Keep the default white background (https://gna.org/bugs/?18520)
96 self.app.scratchpad_filename = self.app.preferences['scratchpad.last_opened'] = self.app.filehandler.get_scratchpad_autosave()
98 def revert_cb(self, action):
99 # Load last scratchpad
100 if os.path.isfile(self.app.scratchpad_filename):
101 self.app.filehandler.open_scratchpad(self.app.scratchpad_filename)
102 print "Reverted to %s" % self.app.scratchpad_filename
104 print "No file to revert to yet."
106 def load_cb(self, action):
107 if self.app.scratchpad_filename:
109 current_pad = self.app.scratchpad_filename
111 current_pad = self.app.filehandler.get_scratchpad_autosave()
112 self.app.filehandler.open_scratchpad_dialog()
113 # Check to see if a file has been opened outside of the scratchpad directory
114 if not os.path.abspath(self.app.scratchpad_filename).startswith(os.path.abspath(self.app.filehandler.get_scratchpad_prefix())):
115 # file is NOT within the scratchpad directory - load copy as current scratchpad
116 self.app.scratchpad_filename = self.app.preferences['scratchpad.last_opened'] = current_pad
119 # Altered 'load' functionality:
120 # If the loaded file is not within the scratchpad directory, a copy is placed as the
121 # currently named scratchpad. The external image will not be overwritten.
122 # I can see how reverting this to allow use of external (shared) scratchpads can be
123 useful, so am keeping the code here, just in case.
125 # Doesn't start with the prefix
126 d = gtk.Dialog(_("Scrachpad autosave warning"), self.app.drawWindow, gtk.DIALOG_MODAL)
128 b = d.add_button(_("I understand"), gtk.RESPONSE_OK)
129 b.set_image(gtk.image_new_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_BUTTON))
130 b = d.add_button(_("_Save a copy"), gtk.RESPONSE_APPLY)
131 b.set_image(gtk.image_new_from_stock(gtk.STOCK_SAVE, gtk.ICON_SIZE_BUTTON))
133 d.set_has_separator(False)
134 d.set_default_response(gtk.RESPONSE_APPLY)
136 l.set_markup("<b>Warning: the scratchpad automatically saves.</b>\n\nChanges made to the scratchpad will overwrite the file on disc\n As this file is not from the scratchpad directory, this may not be the behaviour you expect. \n\nIt is recommended that you save a copy into the scratchpad directory.")
137 l.set_padding(10, 10)
142 if response == gtk.RESPONSE_APPLY:
143 self.app.scratchpad_filename = ""
144 self.save_as_cb(None)
146 return response == gtk.RESPONSE_OK
149 def update(self, doc):
152 self.is_updating = True
154 def save_as_cb(self, action):
155 self.app.filehandler.save_scratchpad_as_dialog()
157 def save_cb(self, action):
158 print "Saving the scratchpad"
159 self.app.filehandler.save_scratchpad(self.app.scratchpad_filename)
161 def button_press_cb(self, win, event):
162 return button_press_cb_abstraction(self.app.drawWindow, win, event, self.app.scratchpad_doc)
164 def button_release_cb(self, win, event):
165 return button_release_cb_abstraction(win, event, self.app.scratchpad_doc)