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 » U++ Library support » Menus&Toolbars » Close context menu from within Execute() handler
Close context menu from within Execute() handler [message #25347] Thu, 18 February 2010 00:23 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 916
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I have a EditField control inside MenuBar, which is shown with Execute(). WhenEnter virtual function of EditField is overriden so that user may write some custom text in this field and then press Enter. Of course, after that, context menu should be closed.
I tried a number of functions (Close/CloseMenu/Hide/etc.) to close context menu automatically inside WhenEnter, but none of them worked.
Debugging led me to a condition in Ctrl.cpp @ 675:
void Ctrl::Close()
{
	GuiLock __;
	Ctrl *q = GetTopCtrl();
	if(!q->top) return;
	//...

which is never true in this case.

Please help, I meet this issue for the third time and still have no solution for it.
Re: Close context menu from within Execute() handler [message #25349 is a reply to message #25347] Thu, 18 February 2010 01:08 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
Hi,
What about using menuBar.Break(); ? Smile
It works for me.

Andrei
Re: Close context menu from within Execute() handler [message #25353 is a reply to message #25349] Thu, 18 February 2010 10:39 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 916
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

No, Break() doesn't work too. Looks like it's due to MenuBar contains control which has focus.
If I do Hide() for that control ahd then do Break() for MenuBar, it doesn't work either.
Re: Close context menu from within Execute() handler [message #25356 is a reply to message #25353] Thu, 18 February 2010 11:44 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
Mindtraveller wrote on Thu, 18 February 2010 11:39

No, Break() doesn't work too. Looks like it's due to MenuBar contains control which has focus.
If I do Hide() for that control ahd then do Break() for MenuBar, it doesn't work either.

Yes, you're right. Perhaps using menuBar.PopUp(); ? Smile That definitely works.
Re: Close context menu from within Execute() handler [message #25357 is a reply to message #25356] Thu, 18 February 2010 12:07 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
A little sample;
class MenuTest : public TopWindow {
public:
	typedef MenuTest CLASSNAME;
	MenuTest()
	{
		menuBar.Set(THISBACK(ThisMenu));
		ef.WhenEnter = THISBACK(EnterPressed);
	}
	void ThisMenu(Bar& bar)
	{
		bar.Gap(10);
		bar.Add(ef.SizePos(), Size(200, 20));
		bar.Gap(10);
		bar.Add("One", THISBACK(Action));
		bar.Add("Two", THISBACK(Action));
	}
	void EnterPressed()
	{
		PromptOK(String(~ef));
	}
	void RightDown(Point p, dword d)
	{
		if (menuBar.IsOpen()) {
			menuBar.Close();
			menuBar.PopUp();
		} else
			menuBar.PopUp();
	}
	void Action() {}
	EditField ef;
	MenuBar menuBar;
};
Re: Close context menu from within Execute() handler [message #25366 is a reply to message #25357] Fri, 19 February 2010 09:35 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 916
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Showing popup dialog certainly closes prevously opened context menu, so it's not exactly what I wanted.
I've modified your example a bit, and your idea using PopUp/Close worked! Thanks a lot!

Still I want to warn everyone: don't show menu with Execute() if you want it to be closed automatically, it doesn't work in any way. Just use PopUp/Close.

Slightly modified Andrei's working example is following
(thank you, Andrei, again!)

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

class MenuTest : public TopWindow {
public:
	typedef MenuTest CLASSNAME;
	MenuTest()
	{
		menuBar.Set(THISBACK(ThisMenu));
		ef.WhenEnter = THISBACK(EnterPressed);
	}
	void ThisMenu(Bar& bar)
	{
		bar.Gap(10);
		bar.Add(ef.SizePos(), Size(200, 20));
		bar.Gap(10);
		bar.Add("One", THISBACK(Action));
		bar.Add("Two", THISBACK(Action));
	}
	void EnterPressed()
	{
		//...
		//some internal work
		//...
		menuBar.Close(); // <-- try to close menu here
	}
	void RightDown(Point p, dword d)
	{
		if (menuBar.IsOpen()) {
			menuBar.Close();
			menuBar.PopUp();
		} else
			menuBar.PopUp();
	}
	void Action() {}
	EditField ef;
	MenuBar menuBar;
};

GUI_APP_MAIN
{
	MenuTest().Run();
}
Re: Close context menu from within Execute() handler [message #25406 is a reply to message #25366] Mon, 22 February 2010 09:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12604
Registered: November 2005
Ultimate Member
I believe that Break should work on Execute, if it does not, I will try to fix it.

Anyway, testcase would be helpful to be sure I am following exactly your situation.
Re: Close context menu from within Execute() handler [message #25417 is a reply to message #25347] Mon, 22 February 2010 13:55 Go to previous messageGo to next message
gxl117 is currently offline  gxl117
Messages: 71
Registered: March 2009
Location: China
Member
For testcase,if close context menu by the press ESC key. menu can't call again by mouse rightkey!
Re: Close context menu from within Execute() handler [message #25421 is a reply to message #25347] Mon, 22 February 2010 15:36 Go to previous messageGo to next message
gxl117 is currently offline  gxl117
Messages: 71
Registered: March 2009
Location: China
Member
The following example is normal.
#include <CtrlLib/CtrlLib.h>
using namespace Upp;

class MenuTest : public TopWindow {
public:
	typedef MenuTest CLASSNAME;
	MenuTest()
	{
		ef.WhenEnter = THISBACK(EnterPressed);
	}
	void ThisMenu(Bar& bar)
	{
		bar.Gap(10);
		bar.Add(ef.SizePos(), Size(200, 20));
		bar.Gap(10);
		bar.Add("One", THISBACK(Action));
		bar.Add("Two", THISBACK(Action));
	}
	
	void EnterPressed()
	{
		//...
		//some internal work
		//...
		//PromptOK(AsString(~ef));
		CloseTopCtrls(); // <-- try to close menu here
		
	}
	void RightDown(Point p, dword d)
	{
		MenuBar::Execute(THISBACK(ThisMenu));
		
	}
	void Action() {}
	EditField ef;
	
};

GUI_APP_MAIN
{
	MenuTest().Run();
}
Re: Close context menu from within Execute() handler [message #25513 is a reply to message #25421] Fri, 26 February 2010 16:06 Go to previous message
mirek is currently offline  mirek
Messages: 12604
Registered: November 2005
Ultimate Member
It needed a little fix in MenuBar code and it is not quite transparent and usual code, but it works now:

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class MenuTest : public TopWindow {
public:
	MenuBar menu;

	typedef MenuTest CLASSNAME;
	MenuTest()
	{
		ef.WhenEnter = THISBACK(EnterPressed);
	}
	void ThisMenu(Bar& bar)
	{
		bar.Gap(10);
		bar.Add(ef.SizePos(), Size(200, 20));
		bar.Gap(10);
		bar.Add("One", THISBACK(Action));
		bar.Add("Two", THISBACK(Action));
	}
	
	void EnterPressed()
	{
		menu.EndLoop();
	}
	void RightDown(Point p, dword d)
	{
		menu.Set(THISBACK(ThisMenu));
		menu.Execute();
	}
	void Action() {}
	EditField ef;	
};

GUI_APP_MAIN
{
	MenuTest().Run();
}
Previous Topic: Bigger images/icons in menu
Next Topic: Popup menu on button click
Goto Forum:
  


Current Time: Wed Aug 05 03:11:28 CEST 2020

Total time taken to generate the page: 0.02673 seconds