U++ framework
Do not panic. Ask here before giving up.

Home » Community » Newbie corner » How to use callbacks
How to use callbacks [message #45865] Fri, 15 January 2016 18:56 Go to next message
Patisab is currently offline  Patisab
Messages: 21
Registered: December 2015
Location: France
Promising Member
Good evening,

I would like to execute a function when the value in an EditField changes. I've tried something like this :

testcallback.h :
#ifndef _testcallback_testcallback_h
#define _testcallback_testcallback_h
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#define LAYOUTFILE <testcallback/testcallback.lay>
#include <CtrlCore/lay.h>
class testcallback : public WithtestcallbackLayout<TopWindow> {
public:
	void fCalcul();
	typedef testcallback CLASSNAME;
	testcallback();
};
#endif


main.cpp :
#include "testcallback.h"
testcallback::testcallback()
{
	CtrlLayout(*this, "Test CallBack");
	go <<= THISBACK(fCalcul);
}
GUI_APP_MAIN
{
	testcallback app;
	app.Run();
}


calcul.cpp :
#include "testcallback.h"
struct Saisie : public WithsaisieLayout<TopWindow> {
	Saisie()  {
		CtrlLayoutOKCancel(*this, "Saisie des valeurs");
		vs.WhenAction <<= THISBACK(Calcul);
		vm.WhenAction <<= THISBACK(Calcul);
	}
    void Calcul()
    {
        int Vs, Vm, Vc;
    	Vs = vs.GetData();
        Vm = vm.GetData();
    	Vc = Vs * Vm;
        vc.SetData(Vc);
        vc.Refresh();
    }
};
void testcallback::fCalcul()
{
	Saisie dlg;
	dlg.Run();
}


testcallback.lay :
LAYOUT(testcallbackLayout, 200, 100)
	ITEM(Button, go, SetLabel(t_("Calcul")).LeftPosZ(72, 56).TopPosZ(80, 15))
END_LAYOUT
LAYOUT(saisieLayout, 200, 100)
	ITEM(Label, dv___0, SetLabel(t_("Valeur à saisir :")).LeftPosZ(4, 92).TopPosZ(4, 19))
	ITEM(EditInt, vs, LeftPosZ(104, 64).TopPosZ(4, 19))
	ITEM(Label, dv___2, SetLabel(t_("Valeur modifiable :")).LeftPosZ(4, 92).TopPosZ(28, 19))
	ITEM(EditInt, vm, LeftPosZ(104, 64).TopPosZ(28, 19))
	ITEM(Button, ok, SetLabel(t_("OK")).LeftPosZ(40, 56).TopPosZ(80, 15))
	ITEM(Button, cancel, SetLabel(t_("Cancel")).LeftPosZ(104, 56).TopPosZ(80, 15))
	ITEM(Label, dv___6, SetLabel(t_("Valeur calculée :")).LeftPosZ(4, 92).TopPosZ(52, 19))
	ITEM(EditInt, vc, SetEditable(false).LeftPosZ(104, 64).TopPosZ(52, 19))
END_LAYOUT


When I compile I obtain this error message :

error: 'Calcul' is not a member of 'Upp::TopWindow::CLASSNAME {aka Upp::TopWindow}'

Thank very much for your help.

Best regards.
Re: How to use callbacks [message #45867 is a reply to message #45865] Fri, 15 January 2016 20:12 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1266
Registered: August 2007
Senior Contributor
In order to use THISBACK macro(s), which actually expands to callback(this, &CLASSNAME::foo) and other variants, where foo should be a method (memeber) of the CLASS pointed with "this" pointer, you simply need to add the following line to your class' declaration.

typedef Saisie CLASSNAME;


Now, of course you can also use a (1) non-member function as a callback or a (2) public method of another object, without defining a CLASSNAME (type).

E.g.,


1) callback(&foo)

2) callback(&cpp_object, &other_cpp_object::foo)



See U++ documents and reference examples (there is a "Callback" named example code in reference examples.)

Regards,

Oblivion



[Updated on: Fri, 15 January 2016 20:38]

Report message to a moderator

[SOLVED]: How to use callbacks [message #45870 is a reply to message #45867] Sat, 16 January 2016 10:00 Go to previous message
Patisab is currently offline  Patisab
Messages: 21
Registered: December 2015
Location: France
Promising Member
Good morning,

Thank you very much Oblivion. Your explainations are clear. I've seen example but I didn't understand it because of use of one layout which make the use of callback in the main function. I've not understand the necessary declaration of the classname in the structure using another layout.

Thanks a lot.

Best regards.

[Updated on: Sat, 16 January 2016 10:07]

Report message to a moderator

Previous Topic: PlotCtrl in upp-x11-src-9360.tar.gz missing
Next Topic: Empty Template vs CtrlLib application with main window
Goto Forum:
  


Current Time: Wed Apr 29 00:44:11 GMT+2 2026

Total time taken to generate the page: 0.00534 seconds