initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / image-editor-dialog.cpp
1 //  Copyright (C) 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22 #include <stdlib.h>
23
24 #include "image-editor-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "gui/image-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "File.h"
31 #include "shieldsetlist.h"
32 #include "GraphicsCache.h"
33
34
35 ImageEditorDialog::ImageEditorDialog(std::string filename, int frames)
36 {
37   num_frames = frames;
38     Glib::RefPtr<Gtk::Builder> xml
39         = Gtk::Builder::create_from_file(get_glade_path()
40                                     + "/image-editor-dialog.ui");
41
42     xml->get_widget("dialog", dialog);
43
44     xml->get_widget("filechooserbutton", filechooserbutton);
45     xml->get_widget("image", image);
46     show_image(filename);
47     target_filename = filename;
48     update_panel();
49     filechooserbutton->signal_selection_changed().connect
50        (sigc::mem_fun(*this, &ImageEditorDialog::on_image_chosen));
51
52 }
53
54 ImageEditorDialog::~ImageEditorDialog()
55 {
56   delete dialog;
57 }
58 void ImageEditorDialog::set_parent_window(Gtk::Window &parent)
59 {
60     dialog->set_transient_for(parent);
61     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
62 }
63
64 int ImageEditorDialog::run()
65 {
66     dialog->show_all();
67     int response = dialog->run();
68     if (response == Gtk::RESPONSE_ACCEPT)
69       target_filename = "";
70
71     return response;
72 }
73
74 void ImageEditorDialog::on_image_chosen()
75 {
76   std::string selected_filename = filechooserbutton->get_filename();
77   if (selected_filename.empty())
78     return;
79
80   target_filename = selected_filename;
81   show_image(selected_filename);
82 }
83
84
85 void ImageEditorDialog::update_panel()
86 {
87   if (target_filename != "")
88     filechooserbutton->set_filename (target_filename);
89 }
90
91 void ImageEditorDialog::show_image(std::string filename)
92 {
93   if (filename == "")
94     return;
95
96   if (heartbeat.connected())
97     heartbeat.disconnect();
98
99   frames = disassemble_row(filename, num_frames);
100   active_frame = 0;
101   image->clear();
102   if (num_frames > 0)
103     heartbeat = Glib::signal_timeout().connect
104       (bind_return
105        (sigc::mem_fun (*this, &ImageEditorDialog::on_heartbeat), 
106         true), 500);
107   else
108     on_heartbeat();
109 }
110
111 void ImageEditorDialog::on_heartbeat()
112 {
113   image->property_pixbuf() = frames[active_frame]->to_pixbuf();
114   active_frame++;
115   if (active_frame >= num_frames)
116     active_frame = 0;
117 }