ultimatepp/bazaar/WithEnterAction/WithEnterAction.h
kohait 28982960b5 fixing some of my Bazaar things, work in progress
git-svn-id: svn://ultimatepp.org/upp/trunk@14139 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2020-03-08 20:37:30 +00:00

59 lines
1.6 KiB
C++

#ifndef _WithEnterAction_WithEnterAction_h
#define _WithEnterAction_WithEnterAction_h
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
template<class B>
class WithEnterAction : public B {
public:
typedef WithEnterAction CLASSNAME;
WithEnterAction() : unfocusonenter(false), multipleenter(false), informed(false) { B::WantFocus(); }
public:
virtual bool Key(dword key, int count)
{
Event<> cb = B::WhenAction;
bool up = key & K_KEYUP;
dword purekey = key & ~K_KEYUP;
if(purekey == K_ENTER)
{
CallbackArgTarget<bool> c; c[false]();
B::WhenAction = c[true];
bool b = B::Key(key, count);
B::WhenAction = cb;
if(!multipleenter && informed) return false;
//not informed, we generally process up and down enter, though we dont need up
if(up) return true;
if(unfocusonenter && (B::HasFocus() && B::GetParent()))
B::GetParent()->SetFocus();
else
{
informed = true;
if(!c) B::Action(); //prevent double invoke
}
return true;
}
B::WhenAction.Clear();
bool b = B::Key(key, count);
B::WhenAction = cb;
if(!up && b) informed = false; //prepare for resend on lostfocus or on enter after each keypress
return b;
}
virtual void LostFocus() { B::LostFocus(); if(!informed) { informed = true; B::Action(); } }
virtual bool Accept() { if(!B::Accept()) return false; if(!informed) { informed = true; B::Action(); } return true; }
virtual void Reject() { informed = true; B::Reject(); }
bool unfocusonenter;
bool multipleenter;
protected:
bool informed;
};
#endif