/******************************************************************** ** Image Component Library (ICL) ** ** ** ** Copyright (C) 2006-2012 CITEC, University of Bielefeld ** ** Neuroinformatics Group ** ** Website: www.iclcv.org and ** ** http://opensource.cit-ec.de/projects/icl ** ** ** ** File : ICLQt/examples/gui-callback-test.cpp ** ** Module : ICLQt ** ** Authors: Christof Elbrechter ** ** ** ** ** ** Commercial License ** ** ICL can be used commercially, please refer to our website ** ** www.iclcv.org for more details. ** ** ** ** GNU General Public License Usage ** ** Alternatively, this file may be used under the terms of the ** ** GNU General Public License version 3.0 as published by the ** ** Free Software Foundation and appearing in the file LICENSE.GPL ** ** included in the packaging of this file. Please review the ** ** following information to ensure the GNU General Public License ** ** version 3.0 requirements will be met: ** ** http://www.gnu.org/copyleft/gpl.html. ** ** ** ** The development of this software was supported by the ** ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** ** Forschungsgemeinschaft (DFG) in the context of the German ** ** Excellence Initiative. ** ** ** *********************************************************************/ #include /// global gui instance GUI gui; // Our working thread, calling it's run function // asynchronously to the GUI Thread void run(){ // shortcut to extract currentTimeLabel from the gui gui["currentTimeLabel"] = Time::now().toString(); Thread::sleep(1); } // a simple callback function of type "void (*callback)(void)" void exit_callback(void){ printf("exit callback was called \n"); exit(0); } // another one (we can also access the GUI from here) void click_callback(){ gui["currentTimeLabel"] = str("hello!"); } // a more complex callback implementing the GUI::Callback interface // In contrast to simple functions, this callbacks are able to have 'own data' struct MyCallback : public GUI::Callback{ Time m_lastTime; void foo(){ Time now = Time::now(); Time dt = now-m_lastTime; // here we could use the macro gui_LabelHandle(timeDiffLabel) as well gui["timeDiffLabel"] = str(dt.toSecondsDouble())+" sec"; m_lastTime = now; } } myCallback; void normal_slider_callback(){ std::cout << "normal slider callback called (slider value is " << gui["slider"].as() << ")" << std::endl; } template void slider_cb(){ static const std::string names[] = {"move","press","release","value","all"}; std::cout << __FUNCTION__ << "(" << names[n] << "): " << gui["slider"].as() << std::endl; } void slider_complex_cb(const std::string &handle){ std::cout << __FUNCTION__ << " (handle-name:" << handle << ")" << std::endl; } void init(){ // create some nice components gui << Label("something").handle("currentTimeLabel").label("current time") << Label("something").handle("timeDiffLabel").label("time since last call") << Slider(0,100,50).handle("slider").label("a slider") << Button("Click me!").handle("click") << Button("Click me too!").handle("click-2") << Button("Exit!").handle("exit") << Show(); /// sometimes, this works as well ! gui["currentTimeLabel"] = Time::now().toString(); // register callbacks (on the specific handles) gui["exit"].registerCallback(function(exit_callback)); gui["click"].registerCallback(function(click_callback)); // or let gui find the corresponding components internally gui.registerCallback(function(myCallback,&MyCallback::foo),"click-2"); // register a 'normal' value changed callback to the slider gui["slider"].registerCallback(normal_slider_callback); // .. or extract the slider-handle that provides an interface // for registering callbacks to specific slider events SliderHandle slider = gui["slider"]; slider.registerCallback(slider_cb<0>,"move"); slider.registerCallback(slider_cb<1>,"press"); slider.registerCallback(slider_cb<2>,"release"); slider.registerCallback(slider_cb<3>,"value"); slider.registerCallback(slider_cb<4>,"all"); slider.registerCallback(slider_complex_cb); } int main(int n, char **ppc){ return ICLApplication(n,ppc,"",init,run).exec(); }