Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » U++ community news and announcements » Lambda support in MenuBar
Lambda support in MenuBar [message #46596] Mon, 06 June 2016 20:29 Go to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Menu bar now has new methods for direct lambda support, as demonstrated by upgraded reference example:

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct App : public TopWindow {
	bool numbers_enabled;

	void Exit()
	{
		Close();
	}

	void SubMenu(Bar& bar)
	{
		for(int i = 0; i < 10; i++)
			bar.Add(~AsString(i), [=] { PromptOK(AsString(i)); });
	}

	void Menu(Bar& bar)
	{
		bar.Add("Enable numbers", [=] { numbers_enabled = !numbers_enabled; })
		   .Check(numbers_enabled);
		bar.Add(numbers_enabled, "Numbers", THISBACK(SubMenu));
		bar.Add("Exit", [=] { Exit(); })
		   .Key(K_CTRL_E);
	}

	void MainBar(Bar& bar)
	{
		bar.Add("Numbers", THISBACK(Menu));
		bar.Sub("Items", [=](Bar& bar) {
			bar.Add("Item 1", [&] { Exclamation("Item 1 invoked"); });
			bar.Add("Item 2", [&] { Exclamation("Item 2 invoked"); });
		});
	}

	MenuBar menu;

	typedef App CLASSNAME;

	App()
	{
		numbers_enabled = false;
		AddFrame(menu);
		menu.Set(THISBACK(MainBar));
	}
};

GUI_APP_MAIN
{
	App().Run();
}


Nice thing is the possibility to define submenu 'inline'. Unfortunately, I had to rename the method name to 'Sub' (instead of Add) because of overloading ambiguity issues...

Mirek
Re: Lambda support in MenuBar [message #46599 is a reply to message #46596] Mon, 06 June 2016 22:03 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1076
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello Mirek,

It seems that this is a future that everybody waits for. It will help develop several things inside TheIDE. I will plan to use it in our "New file" implementation in near future.

Sincerely and thanks for information and implementation Smile,
Klugier


U++ - one framework to rule them all.

[Updated on: Mon, 06 June 2016 22:10]

Report message to a moderator

Re: Lambda support in MenuBar [message #46611 is a reply to message #46599] Sat, 11 June 2016 11:23 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1076
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello Mirek - Now more serious,

I tried to integrated new lambda callback with ide New file menu - but there is a problem. Here is my new code:
void WorkspaceWork::NewMenu(Bar& bar)
{
	bar.Add("File", CtrlImg::File(), THISBACK2(NewPackageFile, "New file", ""));
	bar.Add("Separator", THISBACK(AddSeparator))
		.Help("Add text separator line");
	
	bar.Separator();
	for (int i = 0; i < categories.GetCount(); i++) {
                // Lambda in place - as argument
		bar.Add(categories.GetKey(i), [&, i](Bar& subBar) {
			for (FileType& fileType : categories[i]) {
				String name = fileType.GetName();
				String extension = fileType.GetExtension();
				
				if (fileType.IsSeparator())
					subBar.Separator();
				else
					subBar.Add(name, fileType.GetImage(), THISBACK2(NewPackageFile, "New " + name, extension));
			}
		});
	}
}


And i have got following compilation issue:
/home/klugier/upp/uppsrc/Core/Callback.h: In instantiation of 'Res Upp::Function<Res(ArgTypes ...)>::Wrapper<F>::Execute(ArgTypes ...) [with F = WorkspaceWork::NewMenu(Upp::Bar&)::<lambda(Upp::Bar&)>; Res = void; ArgTypes = {}]
    ':
/home/klugier/upp/uppsrc/ide/UppWspc.cpp:1238:1:   required from here
/home/klugier/upp/uppsrc/Core/Callback.h:15:60: error: no match for call to '(WorkspaceWork::NewMenu(Upp::Bar&)::<lambda(Upp::Bar&)>) ()'
   virtual Res Execute(ArgTypes... args) { return fn(args...); }
                                                            ^
/home/klugier/upp/uppsrc/ide/UppWspc.cpp:931:51: note: candidate: WorkspaceWork::NewMenu(Upp::Bar&)::<lambda(Upp::Bar&)>
   bar.Add(categories.GetKey(i), [&, i](Bar& subBar) {
                                                   ^
/home/klugier/upp/uppsrc/ide/UppWspc.cpp:931:51: note:   candidate expects 1 argument, 0 provided
In file included from /home/klugier/upp/uppsrc/Core/Core.h:293:0,
                 from /home/klugier/upp/uppsrc/Esc/Esc.h:4,
                 from /home/klugier/upp/uppsrc/ide/Core/Core.h:4,
                 from /home/klugier/upp/uppsrc/ide/Common/Common.h:4,
                 from /home/klugier/upp/uppsrc/ide/ide.h:4,
                 from /home/klugier/upp/uppsrc/ide/UppWspc.cpp:1:
/home/klugier/upp/uppsrc/Core/Callback.h:15:60: error: return-statement with a value, in function returning 'void' [-fpermissive]
   virtual Res Execute(ArgTypes... args) { return fn(args...); }


Is that problem here that bar lambda is created in THISBACK method?

Sincerely,
Klugier


U++ - one framework to rule them all.
Re: Lambda support in MenuBar [message #46613 is a reply to message #46611] Sat, 11 June 2016 12:55 Go to previous messageGo to next message
Klugier is currently offline  Klugier
Messages: 1076
Registered: September 2012
Location: Poland, Kraków
Senior Contributor
Hello,

Solved! Just replace bar.Add with bar.Sub when the lambda sequence occurred and everything works fine.

Here is polished version of above wrong code (Maybe someone come in handy):
void WorkspaceWork::NewMenu(Bar& bar)
{
	bar.Add("File", CtrlImg::File(), [=] { NewPackageFile("New file", ""); });
	bar.Add("Separator", [=] { AddSeparator(); })
		.Help("Add text separator line");
	
	bar.Separator();
	for (int i = 0; i < categories.GetCount(); i++) {
		bar.Sub(categories.GetKey(i), [=](Bar& subBar) {
			for (FileType& fileType : categories[i]) {
				String name = fileType.GetName();
				String extension = fileType.GetExtension();
				
				if (fileType.IsSeparator())
					subBar.Separator();
				else
					subBar.Add(name, fileType.GetImage(), [=] { NewPackageFile("New " + name, extension); });
			}
		});
	}
}


Sincerely,
Klugier


U++ - one framework to rule them all.
Re: Lambda support in MenuBar [message #46817 is a reply to message #46596] Tue, 16 August 2016 22:00 Go to previous message
slashupp is currently offline  slashupp
Messages: 231
Registered: July 2009
Experienced Member
thx mirek

doesn't seem to support auto lambdas with parameters (yet?)
e.g.:
void Menu(Bar& bar)
{
	//auto num_ok = [&](bool b){ numbers_enabled = b; };
	//std::function<void(bool)> num_ok = [&](bool b){ numbers_enabled = b; };

	//bar.Add("Enable numbers", [=] { numbers_enabled = !numbers_enabled; }) //--orig code
	//bar.Add("Enable numbers", THISBACK1(num_ok, !numbers_enabled) ) //fail 
	//bar.Add("Enable numbers", Callback1<bool>(num_ok, !numbers_enabled) ) //fail
	   .Check(numbers_enabled);

	bar.Add(numbers_enabled, "Numbers", THISBACK(SubMenu));
	bar.Add("Exit", [=] { Exit(); })
	   .Key(K_CTRL_E);
}

Previous Topic: How You will celebrate 10 000 commit of U++?
Next Topic: EditField::operator String() removed
Goto Forum:
  


Current Time: Fri Apr 19 11:33:47 CEST 2024

Total time taken to generate the page: 0.01937 seconds