Debian Codegraph @

src/joystick_list_widget.cpp

raw
1 /*
2 ** jstest-gtk - A graphical joystick tester
3 ** Copyright (C) 2009 Ingo Ruhnke <[email protected]>
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <gtkmm.h>
20
21 #include "main.hpp"
22 #include "joystick.hpp"
23 #include "joystick_description.hpp"
24 #include "joystick_list_widget.hpp"
25
26 class DeviceListColumns : public Gtk::TreeModel::ColumnRecord
27 {
28 private:
29 static DeviceListColumns* instance_;
30
31 public:
32 static DeviceListColumns& instance() {
33 if (instance_)
34 return *instance_;
35 else
36 return *(instance_ = new DeviceListColumns());
37 }
38
39 Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > icon;
40 Gtk::TreeModelColumn<std::string> path;
41 Gtk::TreeModelColumn<Glib::ustring> name;
42
43 private:
44 DeviceListColumns() {
45 add(icon);
46 add(path);
47 add(name);
48 }
49 };
50
51 DeviceListColumns* DeviceListColumns::instance_ = 0;
52
53 JoystickListWidget::JoystickListWidget() :
54 Gtk::Window(),
55 m_vbox(),
56 m_buttonbox(),
57 m_refresh_button(Gtk::Stock::REFRESH),
58 m_properties_button(Gtk::Stock::PROPERTIES),
59 m_close_button(Gtk::Stock::CLOSE)
60
61 {
62 set_title("Joystick Preferences");
63 set_icon_from_file(Main::current()->get_data_directory() + "generic.png");
64 set_default_size(450, 310);
65 //set_border_width(5);
66
67 scrolled.set_vexpand(true);
68 //scrolled.set_border_width(5);
69 scrolled.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
70 scrolled.add(treeview);
71
72 m_buttonbox.set_border_width(5);
73 m_buttonbox.pack_end(m_refresh_button);
74 m_buttonbox.pack_end(m_properties_button);
75 m_buttonbox.pack_end(m_close_button);
76
77 m_vbox.add(scrolled);
78 m_vbox.pack_end(m_buttonbox, Gtk::PACK_SHRINK);
79
80 add(m_vbox);
81
82 // Set model
83 device_list = Gtk::ListStore::create(DeviceListColumns::instance());
84 treeview.set_model(device_list);
85 treeview.set_headers_visible(false);
86 treeview.append_column("Icon", DeviceListColumns::instance().icon);
87 treeview.append_column("Name", DeviceListColumns::instance().name);
88
89 // Signals
90 treeview.signal_row_activated().connect(sigc::mem_fun(this, &JoystickListWidget::on_row_activated));
91 m_refresh_button.signal_clicked().connect([this]{ on_refresh_button(); });
92 m_properties_button.signal_clicked().connect([this]{ on_properties_button(); });
93 m_close_button.signal_clicked().connect([this]{ hide(); });
94
95 m_close_button.grab_focus();
96
97 on_refresh_button();
98 }
99
100 void
101 JoystickListWidget::on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
102 {
103 Gtk::TreeModel::iterator it = treeview.get_model()->get_iter(path);
104 if (it)
105 {
106 Main::current()->show_device_property_dialog((*it)[DeviceListColumns::instance().path], this);
107 }
108 }
109
110 void
111 JoystickListWidget::on_refresh_button()
112 {
113 const std::vector<JoystickDescription>& joysticks = Joystick::get_joysticks();
114
115 device_list->clear();
116
117 for(std::vector<JoystickDescription>::const_iterator i = joysticks.begin(); i != joysticks.end(); ++i)
118 {
119 Gtk::ListStore::iterator it = device_list->append();
120
121 const Glib::ustring& name = i->name;
122 Glib::ustring icon_filename;
123 //Playstation icon for ps3 controller
124 if(name == "Sony PLAYSTATION(R)3 Controller") icon_filename = "PS3.png";
125 //Xbox icon for xbox controller
126 else if(name.find("X-Box") != Glib::ustring::npos) icon_filename = "xbox360_small.png";
127 //General icon for the rest
128 else icon_filename = "generic.png";
129
130 (*it)[DeviceListColumns::instance().icon] = Gdk::Pixbuf::create_from_file(Main::current()->get_data_directory() + icon_filename);
131 (*it)[DeviceListColumns::instance().path] = i->filename;
132
133 std::ostringstream out;
134 out << name << "\n"
135 << "Device: " << i->filename << "\n"
136 << "Axes: " << i->axis_count << "\n"
137 << "Buttons: " << i->button_count;
138 (*it)[DeviceListColumns::instance().name] = out.str();
139 }
140
141 if (!joysticks.empty())
142 treeview.get_selection()->select(device_list->children().begin());
143 }
144
145 void
146 JoystickListWidget::on_properties_button()
147 {
148 Gtk::TreeModel::iterator it = treeview.get_selection()->get_selected();
149 if (it)
150 {
151 Main::current()->show_device_property_dialog((*it)[DeviceListColumns::instance().path], this);
152 }
153 }
154
155 /* EOF */