initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / stack-info-tip.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (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 Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <config.h>
20
21 #include <gtkmm.h>
22
23 #include "stack-info-tip.h"
24
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27
28 #include "ucompose.hpp"
29 #include "vector.h"
30 #include "defs.h"
31 #include "stack.h"
32 #include "GraphicsCache.h"
33 #include "map-tip-position.h"
34 #include "decorated.h"
35 #include "File.h"
36 #include "stacktile.h"
37
38 StackInfoTip::StackInfoTip(Gtk::Widget *target, MapTipPosition mpos, StackTile *stile)
39 {
40     GraphicsCache *gc = GraphicsCache::getInstance();
41     Glib::RefPtr<Gtk::Builder> xml
42         = Gtk::Builder::create_from_file(get_glade_path()
43                                     + "/stack-info-window.ui");
44
45     xml->get_widget("window", window);
46     Decorated decorator;
47     decorator.decorate(window,File::getMiscFile("various/background.png"), 200);
48
49     xml->get_widget("image_hbox", image_hbox);
50
51     //fill up the hbox with images of the armies in the stack
52
53     Player *active = Playerlist::getActiveplayer();
54     std::list<Stack *> stks = stile->getFriendlyStacks(active);
55     for (std::list<Stack *>::iterator i = stks.begin(); i != stks.end(); i++)
56       for (Stack::iterator it = (*i)->begin(); it != (*i)->end(); it++)
57         {
58           Gtk::Image *image = new Gtk::Image();
59           image->property_pixbuf() = gc->getArmyPic(*it)->to_pixbuf();
60           image_hbox->add(*manage(image));
61         }
62
63     image_hbox->show_all();
64
65     // move into correct position
66     window->get_child()->show();
67     Vector<int> p(0, 0);
68     target->get_window()->get_origin(p.x, p.y);
69     if (target->has_no_window())
70     {
71         Gtk::Allocation a = target->get_allocation();
72         p.x += a.get_x();
73         p.y += a.get_y();
74     }
75     Vector<int> size(0, 0);
76     window->get_size(size.x, size.y);
77     switch (mpos.justification)
78     {
79     case MapTipPosition::LEFT:
80         window->set_gravity(Gdk::GRAVITY_NORTH_WEST);
81         break;
82     case MapTipPosition::RIGHT:
83         window->set_gravity(Gdk::GRAVITY_NORTH_EAST);
84         p.x -= size.x;
85         break;
86     case MapTipPosition::TOP:
87         window->set_gravity(Gdk::GRAVITY_NORTH_WEST);
88         break;
89     case MapTipPosition::BOTTOM:
90         window->set_gravity(Gdk::GRAVITY_SOUTH_WEST);
91         p.y -= size.y;
92         break;
93     }
94
95     p += mpos.pos;
96         
97     window->move(p.x, p.y);
98     window->show();
99 }
100 StackInfoTip::~StackInfoTip()
101 {
102   delete window;
103 }