⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wxsdragwindow.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            UpdateDragPointData(Widget,DragPoints[Index]->WidgetPoints);
            HintIndex = ( Index + 1 ) % DragPoints.size();
        }
    }
}

void wxsDragWindow::SetWidget(wxsWidget* _RootWidget)
{
	if ( RootWidget == _RootWidget )
	{
		RecalculateDragPoints();
	}
	else
	{
        ClearDragPoints();
        RootWidget = _RootWidget;
        BuildDragPoints(RootWidget);
	}
	Refresh();
}

wxsWidget* wxsDragWindow::FindWidgetAtPos(int PosX,int PosY,wxsWidget* Widget)
{
    if ( !Widget || !Widget->GetPreview() || !Widget->GetPreview()->IsShown() ) return NULL;

    int WdgX, WdgY;
    int WdgSX, WdgSY;
    FindAbsolutePosition(Widget,&WdgX,&WdgY);
    ScreenToClient(&WdgX,&WdgY);
    Widget->GetPreview()->GetSize(&WdgSX,&WdgSY);

    if ( PosX >= WdgX && PosY >= WdgY && PosX < WdgX + WdgSX && PosY < WdgY + WdgSY )
    {
        for ( int i=0; i<Widget->GetChildCount(); ++i )
        {
            wxsWidget* Wdg = FindWidgetAtPos(PosX,PosY,Widget->GetChild(i));
            if ( Wdg ) return Wdg;
        }

    	return Widget;
    }

    return NULL;
}

void wxsDragWindow::AddGraphics(wxDC& DC)
{
    int DragAssistType = wxsDWAssistType;

    if ( DragAssistType )
    {
        if ( DragParent && DragParent->GetPreview() )
        {
            int PosX, PosY;
            int SizeX, SizeY;
            FindAbsolutePosition(DragParent,&PosX,&PosY);
            ScreenToClient(&PosX,&PosY);
            DragParent->GetPreview()->GetSize(&SizeX,&SizeY);
            long Col = wxsDWParentCol;
            int R = (Col>>16)&0xFF;
            int G = (Col>> 8)&0xFF;
            int B = (Col    )&0xFF;

            if ( DragAssistType == 1 )
            {
                DC.SetPen(wxPen(wxColour(R,G,B),2));
                DC.SetBrush(*wxTRANSPARENT_BRUSH);
                DC.DrawRectangle(PosX,PosY,SizeX,SizeY);
            }
            else
            {
                if ( !DragParentBitmap )
                {
                    wxImage Covered = Background->GetSubBitmap(wxRect(PosX,PosY,SizeX,SizeY)).ConvertToImage();
                    for ( int y=0; y<SizeY; y++ )
                    {
                        for ( int x=0; x<SizeX; x++ )
                        {
                            Covered.SetRGB(x,y,
                                ( Covered.GetRed(x,y)   + R ) / 2,
                                ( Covered.GetGreen(x,y) + G ) / 2,
                                ( Covered.GetBlue(x,y)  + B ) / 2 );
                        }
                    }
                    DragParentBitmap = new wxBitmap(Covered);
                }

                if ( DragParentBitmap )
                {
                    DC.DrawBitmap(*DragParentBitmap,PosX,PosY);
                }
            }
        }

        if ( DragTarget && (DragTarget!=DragParent) && DragTarget->GetPreview() )
        {
            int PosX, PosY;
            int SizeX, SizeY;
            FindAbsolutePosition(DragTarget,&PosX,&PosY);
            ScreenToClient(&PosX,&PosY);
            DragTarget->GetPreview()->GetSize(&SizeX,&SizeY);
            long Col = wxsDWTargetCol;
            int R = (Col>>16)&0xFF;
            int G = (Col>> 8)&0xFF;
            int B = (Col    )&0xFF;

            if ( DragAssistType == 1 )
            {
                DC.SetPen(wxPen(wxColour(R,G,B),2));
                DC.SetBrush(*wxTRANSPARENT_BRUSH);
                DC.DrawRectangle(PosX,PosY,SizeX,SizeY);
            }
            else
            {
                if ( !DragTargetBitmap )
                {
                    wxImage Covered = Background->GetSubBitmap(wxRect(PosX,PosY,SizeX,SizeY)).ConvertToImage();
                    for ( int y=0; y<SizeY; y++ )
                    {
                        for ( int x=0; x<SizeX; x++ )
                        {
                            Covered.SetRGB(x,y,
                                ( Covered.GetRed(x,y)   + R ) / 2,
                                ( Covered.GetGreen(x,y) + G ) / 2,
                                ( Covered.GetBlue(x,y)  + B ) / 2 );
                        }
                    }
                    DragTargetBitmap = new wxBitmap(Covered);
                }

                if ( DragTargetBitmap )
                {
                    DC.DrawBitmap(*DragTargetBitmap,PosX,PosY);
                }
            }
        }
    }

    for ( DragPointsI i = DragPoints.begin(); i != DragPoints.end(); ++i )
    {
    	DragPointData* DPD = *i;
        wxColor DrawColor( DPD->Inactive ? wxColor(0x80,0x80,0x80) : wxColor(0,0,0) );
        DC.SetPen( wxPen(DrawColor,1) );
        int Style = IsVisible(DPD->Widget) ? wxSOLID : wxTRANSPARENT;
        DC.SetBrush( wxBrush(DrawColor,Style) );

        int PosX = DPD->PosX - DragBoxSize/2;
        int PosY = DPD->PosY - DragBoxSize/2;

    	DC.DrawRectangle(PosX , PosY, DragBoxSize, DragBoxSize );
    }
}

void wxsDragWindow::SetCur(int Cur)
{
	SetCursor(wxCursor(Cur));
	if ( RootWidget && RootWidget->GetPreview() )
	{
        RootWidget->GetPreview()->SetCursor(wxCursor(Cur));
	}
}

void wxsDragWindow::OnSize(wxSizeEvent& event)
{
	NotifySizeChange(event.GetSize());
	event.Skip();
}

void wxsDragWindow::NotifySizeChange(const wxSize& Size)
{
    delete Background;
    Background = new wxBitmap(Size.GetWidth(),Size.GetHeight());
}

wxsWidget* wxsDragWindow::GetSelection()
{
	for ( DragPointsI i =  DragPoints.begin(); i!=DragPoints.end(); ++i )
	{
		if ( !(*i)->Inactive )
		{
			// Here's active drag point - it's at the edge of current selection
			return (*i)->Widget;
		}
	}
    return NULL;
}

int wxsDragWindow::GetMultipleSelCount()
{
	return DragPoints.size() / DragBoxTypeCnt;
}

wxsWidget* wxsDragWindow::GetMultipleSelWidget(int Index)
{
	Index *= DragBoxTypeCnt;
	if ( Index < 0 || Index >= (int)DragPoints.size() ) return NULL;
	return DragPoints[Index]->Widget;
}

void wxsDragWindow::OnFetchBackground(wxTimerEvent& event)
{
    if ( DragTargetBitmap )
    {
        delete DragTargetBitmap;
        DragTargetBitmap = NULL;
    }
    if ( DragParentBitmap )
    {
        delete DragParentBitmap;
        DragParentBitmap = NULL;
    }
	wxScreenDC DC;
	wxMemoryDC DestDC;
    int X = 0, Y = 0;
    ClientToScreen(&X,&Y);
    DestDC.SelectObject(*Background);
    wxRegionIterator upd(FetchArea);
    while ( upd )
    {
        int x = upd.GetX();
        int y = upd.GetY();
        int W = upd.GetW();
        int H = upd.GetH();
        DestDC.Blit(x,y,W,H,&DC,X+x,Y+y);
        upd++;
    }
    FetchArea.Clear();

    ProcessPendingEvents();
	PaintAfterFetch = true;
	Show();
	Update();
	ProcessPendingEvents();
}

void wxsDragWindow::FindAbsolutePosition(wxsWidget* Widget,int* X,int* Y)
{
    *X = 0;
    *Y = 0;
    wxWindow* Wnd = Widget->GetPreview();

    Wnd->GetPosition(X,Y);
    Wnd->GetParent()->ClientToScreen(X,Y);
}

void wxsDragWindow::GrayDragPoints()
{
    for ( DragPointsI i = DragPoints.begin(); i != DragPoints.end(); ++i )
    {
    	(*i)->Inactive = true;
    }
}

void wxsDragWindow::BlackDragPoints(wxsWidget* Widget)
{
    for ( DragPointsI i = DragPoints.begin(); i != DragPoints.end(); ++i )
    {
        DragPointData* DPD = *i;
        if ( DPD->Widget == Widget )
        {
            (*i)->Inactive = false;
        }
    }
}

bool wxsDragWindow::IsInside(wxsWidget* What,wxsWidget* Where )
{

    if ( !Where ) return false;
	return Where->FindChild(What,0) >= 0;
}

bool wxsDragWindow::IsVisible(wxsWidget* Widget)
{
	if ( !Widget ) return true;
	if ( !Widget->GetPreview() ) return false;
	if ( !Widget->GetPreview()->IsShown() ) return false;
	return IsVisible(Widget->GetParent());
}

void wxsDragWindow::GetSelectionNoChildren(std::vector<wxsWidget*>& Vector)
{
	Vector.clear();
	GetSelectionNoChildrenReq(RootWidget,Vector);
}

void wxsDragWindow::GetSelectionNoChildrenReq(wxsWidget* Widget,std::vector<wxsWidget*>& Vector)
{
    if ( !Widget )
    {
        return;
    }

	if ( IsSelected(Widget) )
	{
	    Vector.push_back(Widget);
	    return;
	}

    int Cnt = Widget->GetChildCount();
    for ( int i=0; i<Cnt; i++ )
    {
        GetSelectionNoChildrenReq(Widget->GetChild(i),Vector);
    }
}

void wxsDragWindow::SelectWidget(wxsWidget* Widget)
{
    BlockWidgetSelect = true;
    wxsSelectWidget(Widget);
    BlockWidgetSelect = false;
}

void wxsDragWindow::UpdateGraphics()
{
    wxClientDC ClientDC(this);

    wxBufferedDC DC(&ClientDC,GetSize());
    DC.DrawBitmap(*Background,0,0,false);
    AddGraphics(DC);
}

bool wxsDragWindow::IsSelected(wxsWidget* Widget)
{
    for ( DragPointsI i = DragPoints.begin(); i!=DragPoints.end(); ++i )
    {
        if ( (*i)->Widget == Widget ) return true;
    }
    return false;
}

void wxsDragWindow::UpdateAssist(bool Dragging,wxsWidget* UnderCursor)
{
    if ( !Dragging || !UnderCursor || DragDistanceSmall || !CurDragWidget )
    {
        DragTarget = NULL;
        DragParent = NULL;
        if ( DragTargetBitmap )
        {
            delete DragTargetBitmap;
            DragTargetBitmap = NULL;
        }
        if ( DragParentBitmap )
        {
            delete DragParentBitmap;
            DragParentBitmap = NULL;
        }
        return;
    }

    wxsWidget* Parent = UnderCursor;
    if ( !Parent->IsContainer() )
    {
        Parent = Parent->GetParent();
    }

    if ( DragTarget != UnderCursor )
    {
        DragTarget = UnderCursor;
        if ( DragTargetBitmap )
        {
            delete DragTargetBitmap;
            DragTargetBitmap = NULL;
        }
    }

    if ( DragParent != Parent )
    {
        DragParent = Parent;
        if ( DragParentBitmap )
        {
            delete DragParentBitmap;
            DragParentBitmap = NULL;
        }
    }
}

void wxsDragWindow::RebuildEdgePoints(wxsDragWindow::DragPointData** WidgetPoints)
{
    WidgetPoints[Top  ]->PosX = ( WidgetPoints[LeftTop ]->PosX + WidgetPoints[RightTop]->PosX ) / 2;
    WidgetPoints[Top  ]->PosY =   WidgetPoints[LeftTop ]->PosY;
    WidgetPoints[Left ]->PosX =   WidgetPoints[LeftTop ]->PosX;
    WidgetPoints[Left ]->PosY = ( WidgetPoints[LeftTop ]->PosY + WidgetPoints[LeftBtm ]->PosY ) / 2;
    WidgetPoints[Right]->PosX =   WidgetPoints[RightTop]->PosX;
    WidgetPoints[Right]->PosY = ( WidgetPoints[RightTop]->PosY + WidgetPoints[RightBtm]->PosY ) / 2;
    WidgetPoints[Btm  ]->PosX = ( WidgetPoints[LeftBtm ]->PosX + WidgetPoints[RightBtm]->PosX ) / 2;
    WidgetPoints[Btm  ]->PosY =   WidgetPoints[LeftBtm ]->PosY;
}

BEGIN_EVENT_TABLE(wxsDragWindow,wxControl)
    EVT_PAINT(wxsDragWindow::OnPaint)
    EVT_MOUSE_EVENTS(wxsDragWindow::OnMouse)
    EVT_ERASE_BACKGROUND(wxsDragWindow::OnEraseBack)
    EVT_TIMER(1,wxsDragWindow::TimerRefresh)
    EVT_TIMER(2,wxsDragWindow::OnFetchBackground)
    EVT_SELECT_WIDGET(wxsDragWindow::OnSelectWidget)
    EVT_UNSELECT_WIDGET(wxsDragWindow::OnUnselectWidget)
    EVT_SIZE(wxsDragWindow::OnSize)
END_EVENT_TABLE()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -