]> git.tdb.fi Git - libs/gltk.git/blob - source/connector.h
Pass coordinates relative to the receiving widget's geometry
[libs/gltk.git] / source / connector.h
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GLTK_CONNECTOR_H_
9 #define MSP_GLTK_CONNECTOR_H_
10
11 #include <map>
12 #include <string>
13
14 namespace Msp {
15 namespace GLtk {
16
17 class Connector;
18 class Logic;
19 class Widget;
20
21 class ConnAction
22 {
23 public:
24         virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const =0;
25         virtual ~ConnAction() { }
26 };
27
28 template<typename C, typename W>
29 class ConnFunc0: public ConnAction
30 {
31 public:
32         typedef void (C::*FuncType)(W &);
33
34         ConnFunc0(FuncType f): func(f) { }
35         virtual void connect(Connector &conn, Widget &wdg, const std::string &) const
36         {
37                 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg));
38         }
39
40 private:
41         FuncType func;
42 };
43
44 template<typename C, typename W>
45 class ConnFunc1: public ConnAction
46 {
47 public:
48         typedef void (C::*FuncType)(W &, const std::string &);
49
50         ConnFunc1(FuncType f): func(f) { }
51         virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const
52         {
53                 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg), data);
54         }
55
56 private:
57         FuncType func;
58 };
59
60 /**
61 Provides an interface for associating the actions stored in a Logic object with
62 actual code.  Derive a class from this and use the add functions to specify
63 handlers for each action type.
64 */
65 class Connector
66 {
67 private:
68         std::map<std::string, ConnAction *> actions;
69
70 protected:
71         Connector() { }
72 public:
73         virtual ~Connector();
74
75         void connect(const Logic &);
76
77 protected:
78         template<typename C, typename W>
79         void add(const std::string &type, void (C::*func)(W &))
80         { add(type, new ConnFunc0<C, W>(func)); }
81
82         template<typename C, typename W>
83         void add(const std::string &type, void (C::*func)(W &, const std::string &))
84         { add(type, new ConnFunc1<C, W>(func)); }
85
86 private:
87         void add(const std::string &, ConnAction *);
88 };
89
90 } // namespace GLtk
91 } // namespace Msp
92
93 #endif