ultimatepp/uppsrc/CtrlLib/DisplayPopup.cpp
cxl 719042275c TreeCtrl: Inplace editor fixes
git-svn-id: svn://ultimatepp.org/upp/trunk@15361 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2020-11-06 16:54:45 +00:00

215 lines
4.5 KiB
C++

#include "CtrlLib.h"
namespace Upp {
Point DisplayPopup::Op(Point p)
{
return p + GetScreenView().TopLeft() - ctrl->GetScreenView().TopLeft();
}
void DisplayPopup::LeftDown(Point p, dword flags)
{
if(ctrl) ctrl->LeftDown(Op(p), flags);
}
void DisplayPopup::LeftDrag(Point p, dword flags)
{
if(ctrl) ctrl->LeftDrag(Op(p), flags);
}
void DisplayPopup::LeftDouble(Point p, dword flags)
{
if(ctrl) ctrl->LeftDouble(Op(p), flags);
}
void DisplayPopup::RightDown(Point p, dword flags)
{
if(ctrl) ctrl->RightDown(Op(p), flags);
}
void DisplayPopup::LeftUp(Point p, dword flags)
{
if(ctrl) ctrl->LeftUp(Op(p), flags);
}
void DisplayPopup::MouseWheel(Point p, int zdelta, dword flags)
{
if(ctrl) ctrl->MouseWheel(Op(p), zdelta, flags);
}
void DisplayPopup::MouseLeave()
{
Cancel();
}
void DisplayPopup::MouseMove(Point p, dword flags)
{
p += GetScreenView().TopLeft();
if(!slim.Contains(p))
MouseLeave();
}
void DisplayPopup::Paint(Draw& w)
{
Rect r = GetSize();
w.DrawRect(r, SColorPaper);
if(display) {
display->PaintBackground(w, r, value, ink, paper, style);
r.left += margin;
if(usedisplaystdsize)
r.top += (r.Height() - display->GetStdSize(value).cy) / 2;
display->Paint(w, r, value, ink, paper, style);
}
}
Vector<DisplayPopup *>& DisplayPopup::all()
{
static Vector<DisplayPopup *> all;
return all;
}
DisplayPopup::DisplayPopup()
{
SetFrame(BlackFrame());
display = NULL;
paper = ink = Null;
style = 0;
item = slim = Null;
margin = 0;
ONCELOCK {
InstallStateHook(StateHook);
InstallMouseHook(MouseHook);
}
all().Add(this);
}
DisplayPopup::~DisplayPopup()
{
int q = FindIndex(all(), this);
if(q >= 0)
all().Remove(q);
}
void DisplayPopup::Sync()
{
if(!IsMainThread()) {
PostCallback(PTEBACK(Sync));
return;
}
if(display && ctrl && !ctrl->IsDragAndDropTarget() && !IsDragAndDropTarget()) {
Ctrl *top = ctrl->GetTopCtrl();
if(top && top->HasFocusDeep()) {
Size sz = display->GetStdSize(value);
if(sz.cx + 2 * margin > item.GetWidth() || sz.cy > item.GetHeight()) {
Rect vw = ctrl->GetScreenView();
slim = (item + vw.TopLeft()) & vw;
if(slim.Contains(GetMousePos())) {
Rect r = item;
r.right = max(r.right, r.left + sz.cx + 2 * margin);
r.bottom = max(r.bottom, r.top + sz.cy);
r.Inflate(1, 1);
Rect v = ctrl->GetScreenView();
r.Offset(v.TopLeft());
Rect wa = GetWorkArea(r.BottomLeft());
Size sz = r.GetSize();
if(r.left < wa.left) {
r.left = wa.left;
r.right = min(wa.right, r.left + sz.cx);
}
else
if(r.right > wa.right) {
r.left = max(wa.left, wa.right - sz.cx);
r.right = wa.right;
}
if(r.top < wa.top) {
r.top = wa.top;
r.bottom = min(wa.bottom, wa.top + sz.cy);
}
else
if(r.bottom > wa.bottom) {
if(wa.bottom - r.top < r.top - wa.top) { // there is more space upside
r.bottom = item.bottom + v.top;
r.top = max(wa.top, r.bottom - sz.cy);
}
else
r.bottom = wa.bottom;
}
SetRect(r);
if(!IsOpen())
Ctrl::PopUp(ctrl, true, false, false);
return;
}
}
}
}
if(IsOpen() && !GetDragAndDropSource())
Close();
}
void DisplayPopup::SyncAll()
{
int n = 0;
for(DisplayPopup *p : all())
if(p->ctrl && p->ctrl->IsOpen()) {
p->Sync();
n++;
}
}
bool DisplayPopup::StateHook(Ctrl *, int reason)
{
if(reason == FOCUS)
SyncAll();
return false;
}
bool DisplayPopup::MouseHook(Ctrl *, bool, int, Point, int, dword)
{
SyncAll();
return false;
}
void DisplayPopup::Cancel()
{
if(GetDragAndDropSource())
return;
display = NULL;
Sync();
}
bool DisplayPopup::IsOpen()
{
return Ctrl::IsOpen();
}
bool DisplayPopup::HasMouse()
{
return Ctrl::HasMouse() || ctrl && ctrl->HasMouse();
}
void DisplayPopup::Set(Ctrl *_ctrl, const Rect& _item,
const Value& _value, const Display *_display,
Color _ink, Color _paper, dword _style, int _margin)
{
if(!GUI_ToolTips() || GetDragAndDropSource())
return;
if(item != _item || ctrl != _ctrl || value != _value || display != _display || ink != _ink ||
paper != _paper || style != _style) {
item = _item;
ctrl = _ctrl;
value = _value;
display = _display;
ink = _ink;
paper = _paper;
style = _style;
margin = _margin;
if(ctrl)
Tip(ctrl->GetTip());
Sync();
Refresh();
}
}
}