OSDN Git Service

colorize: add brushmodifier action & toolbar entry
[mypaint-anime/master.git] / gui / scratchwindow.py
1 # This file is part of MyPaint.                                                                                      
2 # Copyright (C) 2011 by Ben O'Steen <bosteen@gmail.com>
3 #
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.
8
9 import gtk
10 gdk = gtk.gdk
11 from gettext import gettext as _
12 import gobject
13 import pango
14
15 import tileddrawwidget, document
16
17 from drawwindow import button_press_cb_abstraction, button_release_cb_abstraction
18
19 import dialogs
20 import os
21
22 import stock
23
24 def stock_button_generic(stock_id, b):
25     img = gtk.Image()
26     img.set_from_stock(stock_id, gtk.ICON_SIZE_MENU)
27     b.add(img)
28     return b
29
30 def stock_button(stock_id):
31     b = gtk.Button()
32     return stock_button_generic(stock_id, b)
33
34 class ToolWidget (gtk.VBox):
35
36     tool_widget_title = _("Scratchpad")
37
38     stock_id = stock.TOOL_SCRATCHPAD
39
40     def __init__(self, app):
41         gtk.VBox.__init__(self)
42         self.app = app
43         #self.set_size_request(200, 250)
44
45         self.is_updating = False
46         
47         # Common controls
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"))
56
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)
61
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)
67
68         scratchpad_view = app.scratchpad_doc.tdw
69
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)
74
75         scratchpad_box = gtk.EventBox()
76         scratchpad_box.add(scratchpad_view)
77
78         self.pack_start(scratchpad_box)
79         self.pack_start(buttons_hbox, expand=False)
80
81         # Updates
82         doc = app.scratchpad_doc.model
83         doc.doc_observers.append(self.update)
84
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")
87
88         self.update(app.scratchpad_doc)
89
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())
93         else:
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()
97
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
103         else:
104             print "No file to revert to yet."
105
106     def load_cb(self, action):
107         if self.app.scratchpad_filename:
108             self.save_cb(action)
109             current_pad = self.app.scratchpad_filename
110         else:
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
117
118         """
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.
124
125         # Doesn't start with the prefix
126         d = gtk.Dialog(_("Scrachpad autosave warning"), self.app.drawWindow, gtk.DIALOG_MODAL)
127
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))
132
133         d.set_has_separator(False)
134         d.set_default_response(gtk.RESPONSE_APPLY)
135         l = gtk.Label()
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)
138         l.show()
139         d.vbox.pack_start(l)
140         response = d.run()
141         d.destroy()
142         if response == gtk.RESPONSE_APPLY:
143             self.app.scratchpad_filename = ""
144             self.save_as_cb(None)
145             return True
146         return response == gtk.RESPONSE_OK
147         """
148
149     def update(self, doc):
150         if self.is_updating:
151             return
152         self.is_updating = True
153
154     def save_as_cb(self, action):
155         self.app.filehandler.save_scratchpad_as_dialog()
156
157     def save_cb(self, action):
158         print "Saving the scratchpad"
159         self.app.filehandler.save_scratchpad(self.app.scratchpad_filename)
160
161     def button_press_cb(self, win, event):
162         return button_press_cb_abstraction(self.app.drawWindow, win, event, self.app.scratchpad_doc)
163
164     def button_release_cb(self, win, event):
165         return button_release_cb_abstraction(win, event, self.app.scratchpad_doc)
166