X-Git-Url: http://git.tdb.fi/?p=libs%2Fgltk.git;a=blobdiff_plain;f=source%2Fconnector.h;h=1960e81b133a26af1c0a8fb2c9662d8fad5b6e1f;hp=a93664875df72a3911080d8948329fe591dbaf68;hb=8ffcd589660022f05d8b3b1dbb36c0c0aa91954a;hpb=c435423919a20a87d100e1ee4cd1fc6ce223040c diff --git a/source/connector.h b/source/connector.h index a936648..1960e81 100644 --- a/source/connector.h +++ b/source/connector.h @@ -10,6 +10,7 @@ Distributed under the LGPL #include #include +#include namespace Msp { namespace GLtk { @@ -57,10 +58,36 @@ private: FuncType func; }; +template +class ConnSignal: public ConnAction +{ +public: + ConnSignal(S W::*s, F f): signal(s), func(f) { } + virtual void connect(Connector &conn, Widget &wdg, const std::string &) const + { + (dynamic_cast(wdg).*signal).connect(sigc::mem_fun(&dynamic_cast(conn).get_object(), func)); + } + +private: + S W::*signal; + F func; +}; + /** -Provides an interface for associating the actions stored in a Logic object with -actual code. Derive a class from this and use the add functions to specify -handlers for each action type. +Provides an interface for associating the bindings stored in a Logic object +with actual code. Derive a class from this and use the add functions to +specify handlers for each binding type. + +Bindings are normally handled by member functions of the Connector class. The +function must take a reference to a widget (of any type) as its first parameter. +If it takes a second parameter, the binding data is passed in as well. + +As a shortcut for simple connections, signals of widgets can be connected +directly to a handler object. For this to work, the Connector class must be a +public inner class of the handler class and it must have a get_object() member +function returning a reference to the handler object. + +TODO: lexical_cast the binding data (requires working around references) */ class Connector { @@ -72,17 +99,34 @@ protected: public: virtual ~Connector(); + /** + Processes all bindings in the Logic object and calls appropriate handlers. + */ void connect(const Logic &); protected: + /** + Adds a handler function for a binding. + */ template void add(const std::string &type, void (C::*func)(W &)) { add(type, new ConnFunc0(func)); } + /** + Adds a handler function for a binding. The binding data is passed in the + second parameter. + */ template void add(const std::string &type, void (C::*func)(W &, const std::string &)) { add(type, new ConnFunc1(func)); } + /** + Adds a signal connector for a binding. + */ + template + void add(const std::string &type, S W::*signal, F H::*func) + { add(type, new ConnSignal(signal, func)); } + private: void add(const std::string &, ConnAction *); };