Debian Codegraph @

src/remap_widget.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2009 Ingo Ruhnke <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <iostream>
5 #include <gtkmm/stock.h>
6
7 #include "joystick.hpp"
8 #include "remap_widget.hpp"
9
10 class RemapWidgetColumns : public Gtk::TreeModel::ColumnRecord
11 {
12 public:
13 static RemapWidgetColumns* instance_;
14
15 public:
16 static RemapWidgetColumns& instance() {
17 if (instance_)
18 return *instance_;
19 else
20 return *(instance_ = new RemapWidgetColumns());
21 }
22
23 Gtk::TreeModelColumn<int> id;
24 Gtk::TreeModelColumn<std::string> name;
25
26 private:
27 RemapWidgetColumns() {
28 add(id);
29 add(name);
30 }
31 };
32
33 RemapWidgetColumns* RemapWidgetColumns::instance_ = 0;
34
35 RemapWidget::RemapWidget(Joystick& joystick_, Mode mode_)
36 : joystick(joystick_),
37 mode(mode_)
38 {
39 map_list = Gtk::ListStore::create(RemapWidgetColumns::instance());
40 treeview.set_model(map_list);
41 if (mode == REMAP_AXIS)
42 treeview.append_column("Axes", RemapWidgetColumns::instance().name);
43 else
44 treeview.append_column("Buttons", RemapWidgetColumns::instance().name);
45
46 set_border_width(5);
47 treeview.set_border_width(5);
48
49 scroll.add(treeview);
50 scroll.set_size_request(-1, 300);
51 scroll.set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
52 pack_start(scroll, Gtk::PACK_EXPAND_WIDGET);
53
54 treeview.set_reorderable();
55
56 map_list->signal_row_inserted().connect(sigc::mem_fun(this, &RemapWidget::on_my_row_inserted));
57 map_list->signal_row_deleted().connect(sigc::mem_fun(this, &RemapWidget::on_my_row_deleted));
58 map_list->signal_rows_reordered().connect(sigc::mem_fun(this, &RemapWidget::on_my_rows_reordered));
59 }
60
61 void
62 RemapWidget::add_entry(int id, const std::string& str)
63 {
64 Gtk::ListStore::iterator it = map_list->append();
65 (*it)[RemapWidgetColumns::instance().id] = id;
66 (*it)[RemapWidgetColumns::instance().name] = str;
67 }
68
69 struct RemapEntry {
70 int id;
71 std::string name;
72
73 bool operator<(const RemapEntry& rhs) const {
74 return id < rhs.id;
75 }
76 };
77
78 void
79 RemapWidget::on_clear()
80 {
81 // We simple sort the list here by 'id'
82
83 // Convert the ListStore into a vector
84 std::vector<RemapEntry> rows;
85 for(Gtk::TreeIter i = map_list->children().begin(); i != map_list->children().end(); ++i)
86 {
87 RemapEntry entry;
88 entry.id = (*i)[RemapWidgetColumns::instance().id];
89 entry.name = (*i)[RemapWidgetColumns::instance().name];
90 rows.push_back(entry);
91 }
92
93 // Sort the vector
94 std::sort(rows.begin(), rows.end());
95
96 // Renter the vector into the liststore
97 map_list->clear();
98 for(std::vector<RemapEntry>::iterator i = rows.begin(); i != rows.end(); ++i)
99 {
100 add_entry(i->id, i->name);
101 }
102
103 on_apply();
104 }
105
106 void
107 RemapWidget::on_apply()
108 {
109 std::vector<int> mapping;
110 std::vector<int> mapping_old;
111 for(Gtk::TreeIter i = map_list->children().begin(); i != map_list->children().end(); ++i)
112 {
113 mapping.push_back((*i)[RemapWidgetColumns::instance().id]);
114 }
115
116 if (mode == REMAP_AXIS)
117 {
118 mapping_old = joystick.get_axis_mapping();
119 joystick.set_axis_mapping(mapping);
120 joystick.correct_calibration(mapping_old, mapping);
121 }
122 else if (mode == REMAP_BUTTON)
123 {
124 joystick.set_button_mapping(mapping);
125 }
126 }
127
128 void
129 RemapWidget::on_my_rows_reordered(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter, int* new_order)
130 {
131 // std::cout << "on_my_rows_reordered" << std::endl;
132 }
133
134 void
135 RemapWidget::on_my_row_inserted(const Gtk::TreeModel::Path& path, const Gtk::TreeModel::iterator& iter)
136 {
137 // std::cout << "on_my_rows_inserted" << std::endl;
138 }
139
140 void
141 RemapWidget::on_my_row_deleted(const Gtk::TreeModel::Path& path)
142 {
143 // std::cout << "on_my_rows_deleted" << std::endl;
144
145 if (mode == REMAP_AXIS)
146 {
147 if (joystick.get_axis_count() == (int)map_list->children().size())
148 {
149 on_apply();
150 }
151 }
152 else if (mode == REMAP_BUTTON)
153 {
154 if (joystick.get_button_count() == (int)map_list->children().size())
155 {
156 on_apply();
157 }
158 }
159 }
160
161 /* EOF */