initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / quest-report-dialog.cpp
1 //  Copyright (C) 2007, 2008, 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
23 #include "quest-report-dialog.h"
24
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27 #include "input-helpers.h"
28 #include "ucompose.hpp"
29 #include "hero.h"
30 #include "defs.h"
31 #include "GameMap.h"
32
33 QuestReportDialog::QuestReportDialog(std::vector<Quest *>q, Hero *hero)
34 {
35   quests = q;
36
37   Glib::RefPtr<Gtk::Builder> xml
38     = Gtk::Builder::create_from_file(get_glade_path()
39                                      + "/quest-report-dialog.ui");
40
41   xml->get_widget("dialog", dialog);
42   decorate(dialog);
43   window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
44
45   xml->get_widget("map_image", map_image);
46
47   questmap = NULL;
48
49   Gtk::EventBox *map_eventbox;
50   xml->get_widget("map_eventbox", map_eventbox);
51
52   xml->get_widget("label", label);
53   xml->get_widget("hero_label", hero_label);
54
55
56   heroes_list = Gtk::ListStore::create(heroes_columns);
57   xml->get_widget("heroes_treeview", heroes_treeview);
58   heroes_treeview->set_model(heroes_list);
59   heroes_treeview->append_column(_("Hero"), heroes_columns.hero_name);
60   heroes_treeview->set_headers_visible(false);
61
62   heroes_list->clear();
63   guint32 count = 0;
64   heroes_treeview->get_selection()->signal_changed()
65     .connect(sigc::mem_fun(this, &QuestReportDialog::on_hero_changed));
66   for (std::vector<Quest*>::iterator it = quests.begin(); it != quests.end();
67        it++)
68     {
69       add_questing_hero (*it, (*it)->getHero());
70       if ((*it)->getHero() == hero || count == 0)
71         {
72           Gtk::TreeModel::Row row;
73           row = heroes_treeview->get_model()->children()[count];
74           heroes_treeview->get_selection()->select(row);
75         }
76       count++;
77     }
78
79   if (quests.size() == 0)
80     fill_quest_info(NULL);
81 }
82
83 void QuestReportDialog::add_questing_hero(Quest *quest, Hero *h)
84 {
85     Gtk::TreeIter i = heroes_list->append();
86     (*i)[heroes_columns.hero_name] = h->getName();
87     (*i)[heroes_columns.quest] = quest;
88 }
89
90 void QuestReportDialog::fill_quest_info(Quest *q)
91 {
92   Glib::ustring s;
93
94   if (questmap)
95     delete questmap;
96   questmap = new QuestMap(q);
97   questmap->map_changed.connect
98     (sigc::mem_fun(this, &QuestReportDialog::on_map_changed));
99   if (dialog->is_realized() == true)
100     {
101       questmap->resize();
102       questmap->draw(Playerlist::getActiveplayer());
103     }
104
105   if (q)
106     {
107       set_title(String::ucompose(_("Quest for %1"), q->getHero()->getName()));
108
109       s = q->getDescription();
110       s += "\n\n";
111       s += q->getProgress();
112       label->set_text(s);
113     }
114   else
115     {
116       set_title(_("No Quest"));
117       int num = rand() % 3;
118       switch (num)
119         {
120         case 0:
121           s = _("Seek a quest in a temple!");
122           break;
123         case 1:
124           s = _("Quest?  What Quest?");
125           break;
126         case 2:
127           s = _("Thou hast no quests!");
128           break;
129         }
130       label->set_text(s);
131     }
132 }
133
134 void QuestReportDialog::on_hero_changed()
135 {
136   Glib::RefPtr<Gtk::TreeSelection> selection = heroes_treeview->get_selection();
137   Gtk::TreeModel::iterator iterrow = selection->get_selected();
138
139   if (iterrow)
140     {
141       Gtk::TreeModel::Row row = *iterrow;
142       Quest *quest = row[heroes_columns.quest];
143       fill_quest_info(quest);
144     }
145
146 }
147 QuestReportDialog::~QuestReportDialog()
148 {
149   delete dialog;
150   delete questmap;
151 }
152
153 void QuestReportDialog::set_parent_window(Gtk::Window &parent)
154 {
155     dialog->set_transient_for(parent);
156     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
157 }
158
159 void QuestReportDialog::hide()
160 {
161   dialog->hide();
162 }
163 void QuestReportDialog::run()
164 {
165   questmap->resize();
166   questmap->draw(Playerlist::getActiveplayer());
167
168   dialog->show_all();
169   if (quests.size() <= 1)
170     {
171       hero_label->hide();
172       heroes_treeview->hide();
173     }
174   dialog->run();
175 }
176
177 void QuestReportDialog::on_map_changed(Glib::RefPtr<Gdk::Pixmap> map)
178 {
179   map_image->property_pixmap() = map;
180 }
181