Add screenshot from Sidux rev 783
[lxde/lxadmin.git] / src / screenshot / screenshot.py
1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 #**************************************************************************
4 # *
5 # Copyright (c) Februar 2008 by Elfriede Apfelkuchen *
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 import locale
22 locale.setlocale(locale.LC_ALL, '') #set locale from 'LANG'
23 import os
24 import sys
25 import string
26 import pygtk
27 pygtk.require('2.0')
28 import gtk
29 import codecs
30 import gobject
31
32
33 DefaultIcon=[
34 "16 14 4 1",
35 " c None",
36 ". c black",
37 "X c white",
38 "o c gray50",
39 " ...... ",
40 " .XXXXXX. ",
41 " ....Xo...oX... ",
42 ".XXXX.......XXX.",
43 ".......ooo......",
44 ".Xoo..oXXoo..oo.",
45 ".Xoo..oXooo..oo.",
46 ".Xoo..ooooo..oo.",
47 ".Xooo..ooo..ooo.",
48 ".Xooo.......ooo.",
49 ".Xooooo...ooooo.",
50 " .............. ",
51 " ",
52 " "]
53
54 I18N={}
55
56 def i18n(text):
57 if I18N_ready:
58 if I18N.has_key(text): return I18N[text]
59 else: return text
60 else: return text
61
62 def load_i18n(filename):
63 #lädt Internationalisierung.moo file
64 ready=False
65 lang=os.getenv('LANG')
66 if not lang: return ready
67 lang2='/usr/share/locale/'+lang[0:2] +'/LC_MESSAGES/'+filename
68 lang3='/usr/share/locale/'+lang[0:5] +'/LC_MESSAGES/'+filename
69 lang='no file'
70 if os.access(lang3,os.R_OK): lang=lang3
71 elif os.access(lang2,os.R_OK): lang=lang2
72 if lang != 'no file':
73 f=codecs.open(lang,'r','utf_8')
74 if f:
75 lang=f.readlines()
76 f.close()
77 ready=True
78 for z in lang:
79 s=z.replace('\n','').split('=')
80 if s[0]=='': continue
81 I18N[s[0]]=s[1]
82 return ready
83
84
85
86 class Simple:
87
88 def destroy(self, widget, data=None):
89 #wird beim beenden ausgeführt
90 gtk.main_quit()
91
92 def registerStock(self,namen,text,icon):
93 #registriert eine Standart-Button-Beschriftung mit icon
94 items = [(namen, text, 0, 0, '')]
95 # Register our stock items
96 gtk.stock_add(items)
97 # Add our custom icon factory to the list of defaults
98 factory = gtk.IconFactory()
99 factory.add_default()
100 pixbuf=gtk.gdk.pixbuf_new_from_xpm_data(icon)
101 icon_set = gtk.IconSet(pixbuf)
102 factory.add(namen, icon_set)
103
104 def start(self,data):
105 #screenshot angeklickt, verstecke Fenster und starte self.screenshot mit 0.5s Verzögerung
106 self.source_id = gobject.timeout_add(500, self.screenshot)
107 self.window.hide()
108
109 def screenshot(self):
110 #jetzt wird der screenshot tatsächlich ausgeführt
111 home=os.getenv("HOME")
112 filename=os.path.join(home,"screenshot-01.png")
113 i=1
114 while os.path.exists(filename):
115 i+=1
116 filename=os.path.join(home,"screenshot-%02i.png" % i)
117 rootWindow = gtk.gdk.get_default_root_window()
118 width=gtk.gdk.screen_width()
119 height=gtk.gdk.screen_height()
120 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,width,height)
121 pixbuf.get_from_drawable(rootWindow,rootWindow.get_colormap(),0,0,0,0,width,height)
122 pixbuf.save(filename,"png")
123 print filename
124 dialog = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,gtk.MESSAGE_INFO,gtk.BUTTONS_OK, filename)
125 result = dialog.run()
126 dialog.destroy()
127 gtk.main_quit()
128 return False
129
130 def __init__(self):
131 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
132 self.window=window
133 window.set_title(i18n('Screenshot'))
134 bbox=gtk.HBox(False,0)
135 window.add(bbox)
136 window.connect("destroy", self.destroy)
137 self.registerStock('cat-screenshot',i18n('_Screenshot'),DefaultIcon)
138 button1=gtk.Button(stock='cat-screenshot')
139 button2=gtk.Button(stock='gtk-cancel')
140 bbox.pack_start(button1,True,True,0)
141 bbox.pack_start(button2,True,True,0)
142 button1.connect('clicked',self.start)
143 button2.connect('clicked',self.destroy)
144 window.set_border_width(5)
145 window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
146 gtk.window_set_default_icon(gtk.gdk.pixbuf_new_from_xpm_data(DefaultIcon))
147 window.set_resizable(False)
148 window.show_all()
149
150 def main(self):
151 # All PyGTK applications must have a gtk.main(). Control ends here
152 # and waits for an event to occur (like a key press or mouse event).
153 gtk.main()
154
155
156 # If the program is run directly or passed as an argument to the python
157 # interpreter then create a HelloWorld instance and show it
158 if __name__ == "__main__":
159 I18N_ready = load_i18n('screenshot.py.moo')
160 app = Simple()
161 app.main()