1 /**********************************************************************
2  *
3  *   FreeDoko a Doppelkopf-Game
4  *
5  *   Copyright (C) 2001 – 2018 by Diether Knof and Borg Enders
6  *
7  *   This program is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU General Public License as
9  *   published by the Free Software Foundation; either version 2 of
10  *   the License, or (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *   You can find this license in the file 'gpl.txt'.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  *   MA  02111-1307  USA
22  *
23  *  Contact:
24  *    Diether Knof dknof@posteo.de
25  *
26  *********************************************************************/
27 
28 #include "constants.h"
29 
30 #ifdef USE_UI_GTKMM
31 
32 #include "iconset.h"
33 
34 #include "../ui.h"
35 #include "../cards.h"
36 
37 #include <gtkmm/scrolledwindow.h>
38 #include <gtkmm/flowbox.h>
39 #include <gtkmm/image.h>
40 #include <gtkmm/box.h>
41 
42 namespace UI_GTKMM_NS {
43 
44 namespace {
45 std::string const icons_list[] = {
46   "re.png"s,
47   "contra.png"s,
48   "marriage.png"s,
49   "poverty.png"s,
50   "swines.png"s,
51 };
52 }
53 
54 /** constructor
55  **/
Iconset(Preferences & preferences)56 Preferences::Iconset::Iconset(Preferences& preferences) :
57   Selection(preferences, ::Preferences::Type::iconset)
58 { }
59 
60 /** destructor
61  **/
62 Preferences::Iconset::~Iconset()
63   = default;
64 
65 /** creates all subelements for the cardsorder
66  **/
67 void
init()68 Preferences::Iconset::init()
69 {
70   Selection::init();
71   container->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
72   box->set_max_children_per_line(1);
73   return ;
74 }
75 
76 /** @return   list of all iconsets
77  **/
78 bool
path_valid(string path) const79 Preferences::Iconset::path_valid(string path) const
80 {
81   return (   Glib::file_test(path, Glib::FILE_TEST_IS_DIR)
82           && Glib::file_test(path + "/License.txt", Glib::FILE_TEST_IS_REGULAR));
83 }
84 
85 /** add the images to the box
86  **/
87 Gtk::Widget*
create_entry(Entry entry)88 Preferences::Iconset::create_entry(Entry entry)
89 {
90   if (!path_valid(entry.path))
91     return {};
92 
93   auto vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
94   vbox->set_margin_top(1 EX);
95   vbox->set_margin_bottom(1 EX);
96   auto hbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
97   for (auto const icon : icons_list) {
98     try {
99       auto pixbuf = Gdk::Pixbuf::create_from_file(entry.path + "/" + icon);
100       auto image = Gtk::manage(new Gtk::Image(pixbuf));
101       hbox->add(*image);
102     } catch (...) {
103     }
104   }
105   if (hbox->get_children().empty()) {
106     auto label = Gtk::manage(new Gtk::Label(entry.name));
107     label->set_markup("<span weight=\"bold\" size=\"xx-large\">" + entry.name + "</span>");
108     label->set_halign(Gtk::ALIGN_START);
109     label->set_margin_top(1 EX);
110     label->set_margin_bottom(1 EX);
111     vbox->add(*label);
112   } else {
113     vbox->add(*hbox);
114     auto label = Gtk::manage(new Gtk::Label(entry.name));
115     label->set_halign(Gtk::ALIGN_START);
116     label->set_margin_top(0.5 EX);
117     vbox->add(*label);
118   }
119   vbox->show_all_children();
120   return vbox;
121 }
122 
123 } // namespace UI_GTKMM_NS
124 
125 #endif // #ifdef USE_UI_GTKMM
126