~stephen-smally/lxscreenshot/trunk

« back to all changes in this revision

Viewing changes to src/lxscreenshot.vala

  • Committer: Stephen Smally
  • Date: 2011-11-10 20:49:53 UTC
  • Revision ID: eco.stefi@fastwebnet.it-20111110204953-wf2dk9vkvkhij7d6
reported to Vala

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LxScreenshot
 
2
Copyright © 2011 Stephen Smally
 
3
 
 
4
This program is free software; you can redistribute it and/or
 
5
modify it under the terms of the GNU General License
 
6
as published by the Free Software Foundation; either version 3
 
7
of the License, or (at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
GNU General License for more details.
 
13
 
 
14
You should have received a copy of the GNU General License
 
15
along with this program; if not, write to the Free Software
 
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
*/
 
18
 
 
19
 
 
20
using Gtk;
 
21
using Gdk;
 
22
 
 
23
namespace LxScreenshot {
 
24
    public class App : Gtk.Window {
 
25
        public Image scrot_img;
 
26
        public Button get_button;
 
27
        public Button clear_button;
 
28
        public Pixbuf screenshot;
 
29
        public SpinButton delay_spin;
 
30
        public CheckButton hide_toggle;
 
31
        
 
32
        public void take_screenshot() {
 
33
            var root = Gdk.get_default_root_window();
 
34
            var root_height = root.get_height();
 
35
            var root_width = root.get_width();
 
36
            screenshot = Gdk.pixbuf_get_from_window(root, 0, 0, root_width, root_height);
 
37
            var screenshot_scaled = screenshot.scale_simple(root_width/3, root_height/3, InterpType.BILINEAR);
 
38
            scrot_img.set_from_pixbuf(screenshot_scaled);
 
39
            get_button.set_label("Save in home");
 
40
            clear_button.set_visible(true);
 
41
        }
 
42
 
 
43
        public bool take_screenshot_timer () {
 
44
            take_screenshot();
 
45
            set_visible(true);
 
46
            return false;
 
47
        }
 
48
 
 
49
        public void save_screenshot() {
 
50
            try {
 
51
                 var now = new DateTime.now_local();
 
52
                var day = now.get_day_of_month().to_string();
 
53
                var hour = now.get_hour().to_string();
 
54
                var minute = now.get_minute().to_string();
 
55
                var second = now.get_second().to_string();
 
56
                string filename = "/screenshot-"+day+"-"+hour+":"+minute+":"+second+".png";
 
57
                screenshot.save(Environment.get_home_dir()+filename, "png", null, "quality", 100, null);
 
58
            } catch (Error msg_error) {
 
59
            print("%s\n", msg_error.message);
 
60
            }
 
61
            get_button.set_label("Take Screenshot");
 
62
            clear_button.set_visible(false);
 
63
        }
 
64
        
 
65
        public void on_get_button(Button widget) {
 
66
            switch (widget.get_label()) {
 
67
                case "Take Screenshot":
 
68
                    if (hide_toggle.get_active()) {
 
69
                        set_visible(false);
 
70
                    }
 
71
                    Timeout.add((int)delay_spin.get_value()*1000+500, take_screenshot_timer);
 
72
                    break;
 
73
                case "Save in home":
 
74
                    save_screenshot();
 
75
                    break;
 
76
            }
 
77
        }
 
78
 
 
79
        public void clear_all(Button widget) {
 
80
            scrot_img.set_from_stock("", IconSize.DIALOG);
 
81
            widget.set_visible(false);
 
82
            get_button.set_label("Take Screenshot");
 
83
        }
 
84
 
 
85
        public void show_about(Button widget) {
 
86
            var dialog = new AboutDialog();
 
87
            dialog.set_program_name("LxScreenshot");
 
88
            dialog.set_copyright("Copyright © 2011");
 
89
            dialog.set_comments("A screenshot tool for the LXDE project");
 
90
            string[] authors = {"Stephen Smally <eco.stefi@fastwebnet.it>"};
 
91
            dialog.set_authors(authors);
 
92
            dialog.set_logo_icon_name("gnome-screenshot");
 
93
            var license = """LxScreenshot
 
94
Copyright © 2011 Stephen Smally
 
95
 
 
96
This program is free software; you can redistribute it and/or
 
97
modify it under the terms of the GNU General License
 
98
as published by the Free Software Foundation; either version 3
 
99
of the License, or (at your option) any later version.
 
100
 
 
101
This program is distributed in the hope that it will be useful,
 
102
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
103
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
104
GNU General License for more details.
 
105
 
 
106
You should have received a copy of the GNU General License
 
107
along with this program; if not, write to the Free Software
 
108
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
109
            """;
 
110
            dialog.set_license(license);
 
111
            dialog.run();
 
112
            dialog.destroy();
 
113
        }
 
114
 
 
115
        public void add_title(VBox container) {
 
116
            var hbox = new HBox(false, 5);
 
117
            var img = new Image();
 
118
            img.set_from_icon_name("gnome-screenshot", IconSize.DIALOG);
 
119
            var label = new Label("");
 
120
            label.set_markup("<b>Take Screenshot</b>");
 
121
            hbox.pack_start(img, false, false, 0);
 
122
            hbox.pack_start(label, false, false, 0);
 
123
            container.pack_start(hbox, false, false, 0);
 
124
        }
 
125
 
 
126
        public void add_delay_spin(VBox container) {
 
127
            var hbox = new HBox(false, 0);
 
128
            var label = new Label("");
 
129
            label.set_markup("<b>Take screenshot in</b>");
 
130
            delay_spin = new SpinButton.with_range(0.0, 30.0, 1.0);
 
131
            hbox.pack_start(label, false, false, 0);
 
132
            hbox.pack_end(delay_spin, false, false, 0);
 
133
            container.pack_start(hbox, false, false, 0);
 
134
        }
 
135
 
 
136
        public void add_hide_toggle(VBox container) {
 
137
            var hbox = new HBox(false, 0);
 
138
            var label = new Label("");
 
139
            label.set_markup("<b>Hide window</b>");
 
140
            hide_toggle = new CheckButton();
 
141
            hbox.pack_start(label, false, false, 0);
 
142
            hbox.pack_end(hide_toggle, false, false, 0);
 
143
            container.pack_start(hbox, false, false, 0);
 
144
        }
 
145
 
 
146
        public void add_buttons(VBox container) {
 
147
            var hbox = new HBox(false, 5);
 
148
            get_button = new Button();
 
149
            get_button.set_label("Take Screenshot");
 
150
            get_button.clicked.connect(on_get_button);
 
151
            clear_button = new Button();
 
152
            clear_button.set_label("Clear");
 
153
            clear_button.clicked.connect(clear_all);
 
154
            hbox.pack_start(get_button, true, true, 0);
 
155
            hbox.pack_start(clear_button, false, false, 0);
 
156
            container.pack_start(hbox, false, false, 0);
 
157
        }
 
158
 
 
159
        public void add_controls(VBox container) {
 
160
            var hbox = new HButtonBox();
 
161
            hbox.set_layout(ButtonBoxStyle.EDGE);
 
162
            var about = new Button.from_stock(Stock.ABOUT);
 
163
            about.clicked.connect(show_about);
 
164
            var close = new Button.from_stock(Stock.CLOSE);
 
165
            close.clicked.connect(Gtk.main_quit);
 
166
            hbox.pack_start(about, false, false, 0);
 
167
            hbox.pack_start(close, false, false, 0);
 
168
            container.pack_start(hbox, false, false, 0);
 
169
        }
 
170
 
 
171
        public App() {
 
172
            set_title("LxScreenshot");
 
173
            destroy.connect(Gtk.main_quit);
 
174
            set_default_size(300, 250);
 
175
            set_position(WindowPosition.CENTER);
 
176
            var box = new VBox(false, 5);
 
177
            box.set_border_width(5);
 
178
            scrot_img = new Image();
 
179
            var scrot_img_cont = new HBox(false, 0);
 
180
            scrot_img_cont.set_border_width(5);
 
181
            scrot_img_cont.pack_start(scrot_img, true, true, 0);
 
182
            add_title(box);
 
183
            add_delay_spin(box);
 
184
            add_hide_toggle(box);
 
185
            var last_box = new VBox(false, 5);
 
186
            add_buttons(last_box);
 
187
            add_controls(last_box);
 
188
            box.pack_start(scrot_img_cont, false, false, 0);
 
189
            box.pack_end(last_box, false, false, 0);
 
190
            add(box);
 
191
            show_all();
 
192
            clear_button.set_visible(false);
 
193
        }
 
194
    }
 
195
}
 
196
 
 
197
static int main(string[] args) {
 
198
    Gtk.init(ref args);
 
199
    new LxScreenshot.App();
 
200
    Gtk.main();
 
201
    return 0;
 
202
}