1 #ifndef MSP_GLTK_CONNECTOR_H_
2 #define MSP_GLTK_CONNECTOR_H_
6 #include <sigc++/functors/mem_fun.h>
18 virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const =0;
19 virtual ~ConnAction() { }
22 template<typename C, typename W>
23 class ConnFunc0: public ConnAction
26 typedef void (C::*FuncType)(W &);
28 ConnFunc0(FuncType f): func(f) { }
29 virtual void connect(Connector &conn, Widget &wdg, const std::string &) const
31 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg));
38 template<typename C, typename W>
39 class ConnFunc1: public ConnAction
42 typedef void (C::*FuncType)(W &, const std::string &);
44 ConnFunc1(FuncType f): func(f) { }
45 virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const
47 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg), data);
54 template<typename C, typename W, typename S, typename F>
55 class ConnSignal: public ConnAction
58 ConnSignal(S W::*s, F f): signal(s), func(f) { }
59 virtual void connect(Connector &conn, Widget &wdg, const std::string &) const
61 (dynamic_cast<W &>(wdg).*signal).connect(sigc::mem_fun(&dynamic_cast<C &>(conn).get_object(), func));
70 Provides an interface for associating the bindings stored in a Logic object
71 with actual code. Derive a class from this and use the add functions to
72 specify handlers for each binding type.
74 Bindings are normally handled by member functions of the Connector class. The
75 function must take a reference to a widget (of any type) as its first parameter.
76 If it takes a second parameter, the binding data is passed in as well.
78 As a shortcut for simple connections, signals of widgets can be connected
79 directly to a handler object. For this to work, the Connector class must be a
80 public inner class of the handler class and it must have a get_object() member
81 function returning a reference to the handler object.
83 TODO: lexical_cast the binding data (requires working around references)
88 std::map<std::string, ConnAction *> actions;
96 Processes all bindings in the Logic object and calls appropriate handlers.
98 void connect(const Logic &);
102 Adds a handler function for a binding.
104 template<typename C, typename W>
105 void add(const std::string &type, void (C::*func)(W &))
106 { add(type, new ConnFunc0<C, W>(func)); }
109 Adds a handler function for a binding. The binding data is passed in the
112 template<typename C, typename W>
113 void add(const std::string &type, void (C::*func)(W &, const std::string &))
114 { add(type, new ConnFunc1<C, W>(func)); }
117 Adds a signal connector for a binding.
119 template<typename W, typename S, typename H, typename F>
120 void add(const std::string &type, S W::*signal, F H::*func)
121 { add(type, new ConnSignal<typename H::Connector, W, S, F H::*>(signal, func)); }
124 void add(const std::string &, ConnAction *);