Allow for per-widget image-surface backing
[ardour.git] / libs / gtkmm2ext / cursors.cc
1 /*
2  * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
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 2 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <sstream>
20
21 #include "pbd/gstdio_compat.h"
22 #include "pbd/error.h"
23 #include "pbd/compose.h"
24
25 #include "gtkmm2ext/cursors.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace Gtkmm2ext;
30
31 CursorInfo::Infos CursorInfo::infos;
32
33 CursorInfo::CursorInfo (const std::string& n, int hotspot_x, int hotspot_y)
34         : name (n)
35         , x (hotspot_x)
36         , y (hotspot_y)
37 {
38 }
39
40 int
41 CursorInfo::load_cursor_info (const std::string& path)
42 {
43         gchar *buf = NULL;
44         if (!g_file_get_contents (path.c_str(), &buf, NULL, NULL))  {
45                 return -1;
46         }
47         std::stringstream infofile (buf);
48         g_free (buf);
49
50         std::string name;
51         int x;
52         int y;
53         bool parse_ok;
54         int line_number = 1;
55
56         do {
57                 parse_ok = false;
58                 infofile >> name;
59                 if (!infofile) {
60                         /* failing here is OK ... EOF */
61                         parse_ok = true;
62                         break;
63                 }
64                 infofile >> x;
65                 if (!infofile) {
66                         break;
67                 }
68                 infofile >> y;
69                 if (!infofile) {
70                         break;
71                 }
72
73                 parse_ok = true;
74                 line_number++;
75
76                 infos[name] = new CursorInfo (name, x, y);
77
78         } while (true);
79
80         if (!parse_ok) {
81                 PBD::error << string_compose (_("cursor hotspots info file %1 has an error on line %2"), path, line_number) << endmsg;
82                 infos.clear ();
83                 return -1;
84         }
85
86         return 0;
87 }
88
89 void
90 CursorInfo::drop_cursor_info ()
91 {
92         infos.clear ();
93 }
94
95 CursorInfo*
96 CursorInfo::lookup_cursor_info (const std::string& name)
97 {
98         Infos::iterator i = infos.find (name);
99
100         if (i == infos.end()) {
101                 return 0;
102         }
103         return i->second;
104 }