File: joystick_list_widget.cpp

package info (click to toggle)
jstest-gtk 0.1.1~git20140501-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 428 kB
  • ctags: 346
  • sloc: cpp: 3,126; makefile: 53; xml: 35
file content (151 lines) | stat: -rw-r--r-- 4,371 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
**  jstest-gtk - A graphical joystick tester
**  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
**
**  This program is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**  
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**  
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <gtkmm/stock.h>

#include "main.hpp"
#include "joystick.hpp"
#include "joystick_description.hpp"
#include "joystick_list_widget.hpp"

class DeviceListColumns : public Gtk::TreeModel::ColumnRecord
{
private:
  static DeviceListColumns* instance_;

public:
  static DeviceListColumns& instance() {
    if (instance_)
      return *instance_;
    else
      return *(instance_ = new DeviceListColumns());
  }

  Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > icon;
  Gtk::TreeModelColumn<std::string>   path;
  Gtk::TreeModelColumn<Glib::ustring> name;

private:
  DeviceListColumns() {
    add(icon);
    add(path);
    add(name);
  }
};

DeviceListColumns* DeviceListColumns::instance_ = 0;

JoystickListWidget::JoystickListWidget()
  : Gtk::Dialog("Joystick Preferences"),
    label("Below is a list of available joysticks on the system. Press Refresh to "
          "update the list, press Properties to get a separate device dialog. The "
          "devices listed are only joystick devices, not evdev devices or SDL "
          "devices, you can view the other ones via the top tab.")
    //frame("Device List"),
{
  set_has_separator(false);
  set_default_size(450, 310);

  label.set_line_wrap();

  scrolled.set_border_width(5);
  scrolled.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);
  scrolled.add(treeview);
  get_vbox()->add(scrolled);

  add_button(Gtk::Stock::REFRESH, 2);
  add_button(Gtk::Stock::PROPERTIES, 1);
  add_button(Gtk::Stock::CLOSE, 0);

  signal_response().connect(sigc::mem_fun(this, &JoystickListWidget::on_response));

  // Set model
  device_list = Gtk::ListStore::create(DeviceListColumns::instance());
  treeview.set_model(device_list);
  treeview.set_headers_visible(false);
  treeview.append_column("Icon", DeviceListColumns::instance().icon);
  treeview.append_column("Name", DeviceListColumns::instance().name);

  treeview.signal_row_activated().connect(sigc::mem_fun(this, &JoystickListWidget::on_row_activated));

  on_refresh();
}

void
JoystickListWidget::on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
{
  Gtk::TreeModel::iterator it = treeview.get_model()->get_iter(path);
  if (it)
    {
      Main::current()->show_device_property_dialog((*it)[DeviceListColumns::instance().path]);
    }
}

void
JoystickListWidget::on_response(int v)
{
  if (v == 0)
    {
      hide();
    }
  else if (v == 1)
    {
      on_properties();
    }
  else if (v == 2)
    {
      on_refresh();
    }
}

void
JoystickListWidget::on_refresh()
{
  const std::vector<JoystickDescription>& joysticks = Joystick::get_joysticks();

  device_list->clear();

  for(std::vector<JoystickDescription>::const_iterator i = joysticks.begin(); i != joysticks.end(); ++i)
    {
      Gtk::ListStore::iterator it = device_list->append();
      (*it)[DeviceListColumns::instance().icon] = Gdk::Pixbuf::create_from_file("/usr/share/jstest-gtk/data/generic.png");
      (*it)[DeviceListColumns::instance().path] = i->filename;

      std::ostringstream out;
      out << i->name << "\n"
          << "Device: " << i->filename << "\n"
          << "Axes: " << i->axis_count << "\n"
          << "Buttons: " << i->button_count;
      (*it)[DeviceListColumns::instance().name] = out.str();
    }

  if (!joysticks.empty())
    treeview.get_selection()->select(device_list->children().begin());
}

void
JoystickListWidget::on_properties()
{
  Gtk::TreeModel::iterator it = treeview.get_selection()->get_selected();
  if (it)
    {
      Main::current()->show_device_property_dialog((*it)[DeviceListColumns::instance().path]);
    }
}

/* EOF */