1 //  Copyright (C) 2010, 2014, 2017 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 "item-report-dialog.h"
24 
25 #include "ucompose.hpp"
26 #include "defs.h"
27 #include "stack.h"
28 #include "MapBackpack.h"
29 #include "itemmap.h"
30 #include "playerlist.h"
31 
32 #define method(x) sigc::mem_fun(*this, &ItemReportDialog::x)
33 
ItemReportDialog(Gtk::Window & parent,std::list<Stack * > item_laden_stacks,std::list<MapBackpack * > bags_of_stuff)34 ItemReportDialog::ItemReportDialog(Gtk::Window &parent, std::list<Stack*> item_laden_stacks, std::list<MapBackpack*> bags_of_stuff)
35  : LwDialog(parent, "item-report-dialog.ui")
36 {
37   stacks = item_laden_stacks;
38   bags = bags_of_stuff;
39   xml->get_widget("map_image", map_image);
40 
41   itemmap = new ItemMap(item_laden_stacks, bags);
42   itemmap->map_changed.connect(method(on_map_changed));
43 
44   xml->get_widget("label", label);
45 
46   fill_in_item_info();
47 }
48 
run()49 void ItemReportDialog::run()
50 {
51   itemmap->resize();
52   itemmap->draw();
53 
54   dialog->show_all();
55   dialog->run();
56 }
57 
on_map_changed(Cairo::RefPtr<Cairo::Surface> map)58 void ItemReportDialog::on_map_changed(Cairo::RefPtr<Cairo::Surface> map)
59 {
60   Glib::RefPtr<Gdk::Pixbuf> pixbuf =
61     Gdk::Pixbuf::create(map, 0, 0, itemmap->get_width(), itemmap->get_height());
62   map_image->property_pixbuf() = pixbuf;
63   fill_in_item_info();
64 }
65 
fill_in_item_info()66 void ItemReportDialog::fill_in_item_info()
67 {
68   int count = 0;
69   for (std::list<Stack*>::iterator it = stacks.begin(); it != stacks.end();
70        it++)
71     {
72       Stack *stack = (*it);
73       count += stack->countItems();
74     }
75 
76   Glib::ustring s = "";
77   if (count > 0)
78     s = String::ucompose(ngettext("You have %1 item!",
79                                   "You have %1 items!", count), count);
80   else
81     s += _("You don't have any items!");
82   label->set_text(s);
83 }
84