3 This file is part of libmspgltk
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_GLTK_CONNECTOR_H_
9 #define MSP_GLTK_CONNECTOR_H_
13 #include <sigc++/functors/mem_fun.h>
25 virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const =0;
26 virtual ~ConnAction() { }
29 template<typename C, typename W>
30 class ConnFunc0: public ConnAction
33 typedef void (C::*FuncType)(W &);
35 ConnFunc0(FuncType f): func(f) { }
36 virtual void connect(Connector &conn, Widget &wdg, const std::string &) const
38 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg));
45 template<typename C, typename W>
46 class ConnFunc1: public ConnAction
49 typedef void (C::*FuncType)(W &, const std::string &);
51 ConnFunc1(FuncType f): func(f) { }
52 virtual void connect(Connector &conn, Widget &wdg, const std::string &data) const
54 (dynamic_cast<C &>(conn).*func)(dynamic_cast<W &>(wdg), data);
61 template<typename C, typename W, typename S, typename F>
62 class ConnSignal: public ConnAction
65 ConnSignal(S W::*s, F f): signal(s), func(f) { }
66 virtual void connect(Connector &conn, Widget &wdg, const std::string &) const
68 (dynamic_cast<W &>(wdg).*signal).connect(sigc::mem_fun(&dynamic_cast<C &>(conn).get_object(), func));
77 Provides an interface for associating the bindings stored in a Logic object
78 with actual code. Derive a class from this and use the add functions to
79 specify handlers for each binding type.
81 Bindings are normally handled by member functions of the Connector class. The
82 function must take a reference to a widget (of any type) as its first parameter.
83 If it takes a second parameter, the binding data is passed in as well.
85 As a shortcut for simple connections, signals of widgets can be connected
86 directly to a handler object. For this to work, the Connector class must be a
87 public inner class of the handler class and it must have a get_object() member
88 function returning a reference to the handler object.
90 TODO: lexical_cast the binding data (requires working around references)
95 std::map<std::string, ConnAction *> actions;
100 virtual ~Connector();
103 Processes all bindings in the Logic object and calls appropriate handlers.
105 void connect(const Logic &);
109 Adds a handler function for a binding.
111 template<typename C, typename W>
112 void add(const std::string &type, void (C::*func)(W &))
113 { add(type, new ConnFunc0<C, W>(func)); }
116 Adds a handler function for a binding. The binding data is passed in the
119 template<typename C, typename W>
120 void add(const std::string &type, void (C::*func)(W &, const std::string &))
121 { add(type, new ConnFunc1<C, W>(func)); }
124 Adds a signal connector for a binding.
126 template<typename W, typename S, typename H, typename F>
127 void add(const std::string &type, S W::*signal, F H::*func)
128 { add(type, new ConnSignal<typename H::Connector, W, S, F H::*>(signal, func)); }
131 void add(const std::string &, ConnAction *);