toolwnd.cpp

来自「Wxpython Implemented on Windows CE, Sou」· C++ 代码 · 共 1,109 行 · 第 1/2 页

CPP
1,109
字号
        wxSize preferred = GetPreferredSize( wxSize( newRect.width  - borderDim.x,
                                             newRect.height - borderDim.y ) );

        preferred.x += borderDim.x;
        preferred.y += borderDim.y;

        //CalcResizedRect( newRect, delta, preferred );

        wxRect finalRect = newRect;

        AdjustRectPos( newRect, preferred, finalRect );

        if ( mRealTimeUpdatesOn )
        {
            SetSize( finalRect.x, finalRect.y,
                     finalRect.width, finalRect.height, 0 );
        }
        else
        {
            DrawHintRect( mPrevHintRect );
            DrawHintRect( finalRect );

            ::wxLogTrace(wxT("wxToolWindow"),wxT("%d,%d / %d,%d\n"), finalRect.x, finalRect.y, finalRect.width, finalRect.height);
        }

        mPrevHintRect = finalRect;
    }
}

void wxToolWindow::OnLeftDown( wxMouseEvent& event )
{
    int result = HitTestWindow( event );

    for( size_t i = 0; i != mButtons.Count(); ++i )
    {
        mButtons[i]->OnLeftDown( wxPoint( event.m_x, event.m_y ) );

        if ( mButtons[i]->IsPressed() )
            return; // button hitted,
    }

    if ( result >= HITS_WND_LEFT_EDGE || result == HITS_WND_TITLE )
    {
        GetScrMousePos( event, mDragOrigin );

        /*
        if ( mMouseCaptured `)
        {
            ReleaseMouse();
            mMouseCaptured = false;
        }*/

        if ( result == HITS_WND_TITLE &&
             HandleTitleClick( event ) )
            return;

        mResizeStarted = true;

        int x,y;
        GetPosition( &x, &y );

        mInitialRect.x = x;
        mInitialRect.y = y;

        GetSize( &x, &y );
        mInitialRect.width  = x;
        mInitialRect.height = y;

        mPrevHintRect = mInitialRect;

        if ( mCursorType != HITS_WND_TITLE && !mRealTimeUpdatesOn )
        {
            mpScrDc = new wxScreenDC();

            wxScreenDC::StartDrawingOnTop( (wxRect*)NULL );

            DrawHintRect( mInitialRect );
        }
    }
}

void wxToolWindow::OnLeftUp( wxMouseEvent& event )
{
    for( size_t i = 0; i != mButtons.Count(); ++i )
    {
        mButtons[i]->OnLeftUp( wxPoint( event.m_x, event.m_y ) );

        if ( mButtons[i]->WasClicked() )
        {
            OnMiniButtonClicked( i ); // notify derived classes
            mButtons[i]->Reset();
        }
    }

    if ( mResizeStarted )
    {
        mResizeStarted = false;

        if ( mCursorType != HITS_WND_TITLE )
        {
            if ( !mRealTimeUpdatesOn )
            {
                DrawHintRect( mPrevHintRect );

                wxScreenDC::EndDrawingOnTop();

                delete mpScrDc;

                mpScrDc = NULL;

                SetSize( mPrevHintRect.x, mPrevHintRect.y,
                         mPrevHintRect.width, mPrevHintRect.height, 0 );
            }
        }
    }
}

void wxToolWindow::OnSize( wxSizeEvent& WXUNUSED(event) )
{
    if ( mpClientWnd )
    {
        int w,h;
        GetSize( &w, &h );

        int x = mWndHorizGap + mClntHorizGap;
        int y = mWndVertGap  + mTitleHeight + mClntVertGap;

        mpClientWnd->SetSize( x-1, y-1,
                              w - 2*(mWndHorizGap + mClntHorizGap),
                              h - y - mClntVertGap - mWndVertGap,
                              0
                            );
    }

    LayoutMiniButtons();
}

wxSize wxToolWindow::GetPreferredSize( const wxSize& given )
{
    return given;
}

void wxToolWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
{
    // nothing
}

/***** Implementation for class cbMiniButton *****/

cbMiniButton::cbMiniButton()

    : mVisible( true ),
      mEnabled( true ),

      mpLayout( NULL ),
      mpPane  ( NULL ),
      mpPlugin( NULL ),
      mpWnd   ( NULL ),

      mWasClicked( false ),
      mDragStarted( false ),
      mPressed( false )
{}

void cbMiniButton::SetPos( const wxPoint& pos )
{
    mPos = pos;
}

bool cbMiniButton::HitTest( const wxPoint& pos )
{
    if ( !mVisible ) return false;

    return ( pos.x >= mPos.x && pos.y >= mPos.y &&
             pos.x < mPos.x + BTN_BOX_WIDTH     &&
             pos.y < mPos.y + BTN_BOX_HEIGHT );
}

void cbMiniButton::OnLeftDown( const wxPoint& pos )
{
    if ( !mVisible || mDragStarted ) return;

    if ( HitTest( pos ) && mEnabled )
    {
        if ( mpPlugin )
        {
            mpLayout->CaptureEventsForPane( mpPane );
            mpLayout->CaptureEventsForPlugin( mpPlugin );
        }
        else
            mpWnd->CaptureMouse();

        mDragStarted = true;
        mPressed     = true;
        mWasClicked  = false;

        Refresh();
    }
}

void cbMiniButton::OnLeftUp( const wxPoint& WXUNUSED(pos) )
{
    if ( !mVisible || !mDragStarted ) return;

    if ( mpPlugin )
    {
        mpLayout->ReleaseEventsFromPane( mpPane );
        mpLayout->ReleaseEventsFromPlugin( mpPlugin );
    }
    else
        mpWnd->ReleaseMouse();

    mWasClicked  = mPressed;
    mDragStarted = false;

    mPressed = false;
    Refresh();
}

void cbMiniButton::OnMotion( const wxPoint& pos )
{
    if ( !mVisible ) return;

    if ( mDragStarted )
    {
        mPressed = HitTest( pos );

        Refresh();
    }
}

void cbMiniButton::Refresh()
{
    if ( mpLayout )
    {
         wxClientDC dc( &mpLayout->GetParentFrame() );

         Draw( dc );
    }
    else
    {
        wxWindowDC dc( mpWnd );

        Draw( dc );
    }
}

void cbMiniButton::Draw( wxDC& dc )
{
    if ( !mVisible ) return;

    dc.SetPen( *wxTRANSPARENT_PEN );

    dc.SetBrush( *wxLIGHT_GREY_BRUSH );

    dc.DrawRectangle( mPos.x + 1, mPos.y + 1, BTN_BOX_WIDTH - 2, BTN_BOX_HEIGHT - 2 );

    // "hard-code" metafile

    if ( !mPressed )
        dc.SetPen( *wxWHITE_PEN );
    else
        dc.SetPen( *wxBLACK_PEN );

    dc.DrawLine( mPos.x, mPos.y, mPos.x + BTN_BOX_WIDTH, mPos.y );
    dc.DrawLine( mPos.x, mPos.y, mPos.x, mPos.y + BTN_BOX_HEIGHT );

    dc.SetPen( *wxGREY_PEN );

    if ( !mPressed )
    {
        dc.DrawLine( mPos.x + 1, mPos.y + BTN_BOX_HEIGHT - 2,
                     mPos.x + BTN_BOX_WIDTH - 1, mPos.y + BTN_BOX_HEIGHT - 2 );

        dc.DrawLine( mPos.x + BTN_BOX_WIDTH - 2, mPos.y + 1,
                     mPos.x + BTN_BOX_WIDTH - 2, mPos.y + BTN_BOX_HEIGHT - 1 );
    }
    else
    {
        dc.DrawLine( mPos.x + 1, mPos.y + 1,
                     mPos.x + BTN_BOX_WIDTH - 2, mPos.y + 1 );

        dc.DrawLine( mPos.x + 1, mPos.y + 1,
                     mPos.x + 1, mPos.y + BTN_BOX_HEIGHT - 2 );
    }

    if ( !mPressed )
        dc.SetPen( *wxBLACK_PEN );
    else
        dc.SetPen( *wxWHITE_PEN );

    dc.DrawLine( mPos.x, mPos.y + BTN_BOX_HEIGHT - 1,
                 mPos.x + BTN_BOX_WIDTH, mPos.y + BTN_BOX_HEIGHT - 1 );

    dc.DrawLine( mPos.x + BTN_BOX_WIDTH - 1, mPos.y ,
                 mPos.x + BTN_BOX_WIDTH - 1, mPos.y + BTN_BOX_HEIGHT );
}

bool cbMiniButton::WasClicked()
{
    return mWasClicked;
}

void cbMiniButton::Reset()
{
    mWasClicked = false;
}

/***** Implementation fro class cbCloseBox *****/

void cbCloseBox::Draw( wxDC& dc )
{
    cbMiniButton::Draw( dc );

    dc.SetPen( *wxBLACK_PEN );

    int width = BTN_BOX_WIDTH - 7;

    int xOfs = (mPressed) ? 4 : 3;
    int yOfs = (mPressed) ? 4 : 3;

    for( int i = 0; i != BTN_X_WEIGHT; ++i )
    {
        dc.DrawLine( mPos.x + xOfs + i,
                     mPos.y + yOfs,
                     mPos.x + xOfs + i + width,
                     mPos.y + yOfs + width );

        dc.DrawLine( mPos.x + xOfs + i + width - 1,
                     mPos.y + yOfs,
                     mPos.x + xOfs + i - 1,
                     mPos.y + yOfs + width );
    }
}

/***** Implementation fro class cbCollapseBox *****/

inline static void my_swap( int& a, int& b )
{
    long tmp = a;
    a = b;
    b = tmp;
}

void cbCollapseBox::Draw( wxDC& dc )
{
    cbMiniButton::Draw( dc );

    dc.SetPen( *wxTRANSPARENT_PEN );

    wxPoint arr[3];

    int yOfs  = (mPressed) ? 3 : 2;
    int xOfs  = (mPressed) ? 5 : 4;
    int width = BTN_BOX_WIDTH - 8;

    // rotating/shifting triangle inside collapse box

    arr[0].x = xOfs;
    arr[0].y = yOfs-1;
    arr[2].x = xOfs;
    arr[2].y = BTN_BOX_HEIGHT - yOfs - 1;
    arr[1].x = xOfs + width;
    arr[1].y = (arr[2].y + arr[0].y)/2;

    if ( !mIsAtLeft )
    {
        arr[0].x = BTN_BOX_WIDTH - arr[0].x;
        arr[1].x = BTN_BOX_WIDTH - arr[1].x;
        arr[2].x = BTN_BOX_WIDTH - arr[2].x;
    }

    if ( !mpPane->IsHorizontal() )
    {
        my_swap( arr[0].y, arr[0].x );
        my_swap( arr[1].y, arr[1].x );
        my_swap( arr[2].y, arr[2].x );

        arr[0].x += 1;
        arr[1].x += 1;
        arr[2].x += 1;

        //arr[1].y -= 1;
    }

    arr[0].x += mPos.x;
    arr[0].y += mPos.y;
    arr[1].x += mPos.x;
    arr[1].y += mPos.y;
    arr[2].x += mPos.x;
    arr[2].y += mPos.y;

    if ( !mEnabled ) dc.SetBrush( *wxGREY_BRUSH );
    else dc.SetBrush( *wxBLACK_BRUSH );

    dc.DrawPolygon( 3, arr );
    dc.SetBrush( wxNullBrush );
}

/***** Implementation for class cbDockBoxBox *****/

void cbDockBox::Draw( wxDC& dc )
{
    cbMiniButton::Draw( dc );

    int width = BTN_BOX_WIDTH - 7;

    int xOfs = (mPressed) ? 4 : 3;
    int yOfs = (mPressed) ? 4 : 3;

    dc.SetPen( *wxBLACK_PEN );
    dc.SetBrush( *wxBLACK_BRUSH );

    dc.DrawRectangle( mPos.x + xOfs, mPos.y + yOfs, width, width );

    xOfs += 1;
    yOfs += 1;

    dc.SetBrush( *wxWHITE_BRUSH );

    dc.DrawRectangle( mPos.x + xOfs, mPos.y + yOfs, width-2, width-2 );
}

/***** Implementation for class wxToolWindow *****/

IMPLEMENT_DYNAMIC_CLASS( cbFloatedBarWindow, wxToolWindow )

BEGIN_EVENT_TABLE( cbFloatedBarWindow, wxToolWindow )

    EVT_LEFT_DCLICK( cbFloatedBarWindow::OnDblClick )

END_EVENT_TABLE()

cbFloatedBarWindow::cbFloatedBarWindow()

    : mpBar( NULL )
{
    AddMiniButton( new cbCloseBox() );
    AddMiniButton( new cbDockBox()  );
}

void cbFloatedBarWindow::SetBar( cbBarInfo* pBar )
{
    mpBar = pBar;
}

cbBarInfo* cbFloatedBarWindow::GetBar()
{
    return mpBar;
}

void cbFloatedBarWindow::SetLayout( wxFrameLayout* pLayout )
{
    mpLayout = pLayout;
}

void cbFloatedBarWindow::PositionFloatedWnd( int scrX,  int scrY,
                                             int width, int height )
{
    wxSize minDim = GetMinimalWndDim();

    SetSize( scrX - mWndHorizGap - mClntHorizGap,
             scrY - mClntVertGap - mTitleHeight - mWndVertGap,
             width + minDim.x, height + minDim.y, 0 );
}

wxSize cbFloatedBarWindow::GetPreferredSize( const wxSize& given )
{
    if ( mpBar->mDimInfo.GetDimHandler() )
    {
        cbBarDimHandlerBase* pHandler = mpBar->mDimInfo.GetDimHandler();

        wxSize prefDim;

        // int vtad = *((int*)pHandler);

        pHandler->OnResizeBar( mpBar, given, prefDim );

        return prefDim;
    }
    else
    {
        if ( mpBar->IsFixed() )
            return mpBar->mDimInfo.mSizes[ wxCBAR_FLOATING ];
        else
            return given; // not-fixed bars are resized exactly the way user wants
    }
}

void cbFloatedBarWindow::OnMiniButtonClicked( int btnIdx )
{
    // #1 - close mini-button
    // #0 - dock mini-button

    if ( btnIdx == 0 )
    {
        mpBar->mAlignment = -1; // sepcial "marking" for hidden bars out of floated state
        mpLayout->SetBarState( mpBar, wxCBAR_HIDDEN, true );
    }
    else
        mpLayout->SetBarState( mpBar, wxCBAR_DOCKED_HORIZONTALLY, true );
}

bool cbFloatedBarWindow::HandleTitleClick( wxMouseEvent& event )
{
    ReleaseMouse();
    mMouseCaptured = false;

    wxPoint scrPos;
    GetScrMousePos( event, scrPos );

    int msX = scrPos.x,
        msY = scrPos.y;

    mpLayout->GetParentFrame().ScreenToClient( &msX, &msY );

    int x,y;
    GetPosition(&x,&y);
    int w,h;
    GetSize( &w, &h );

    wxSize minDim = GetMinimalWndDim();

    w -= minDim.x;
    h -= minDim.y;

    x += mWndHorizGap + mClntHorizGap;
    y += mWndVertGap  + mTitleHeight + mClntVertGap;

    mpLayout->GetParentFrame().ScreenToClient( &x, &y );

    wxRect& bounds = mpBar->mDimInfo.mBounds[ wxCBAR_FLOATING ];

    bounds.x = x;
    bounds.y = y;
    bounds.width  = w;
    bounds.height = h;

    cbStartBarDraggingEvent dragEvt( mpBar, wxPoint(msX,msY),
                                     mpLayout->GetPanesArray()[FL_ALIGN_TOP] );

    mpLayout->FirePluginEvent( dragEvt );

    return true;
}

void cbFloatedBarWindow::OnDblClick( wxMouseEvent& WXUNUSED(event) )
{
    mpLayout->SetBarState( mpBar, wxCBAR_DOCKED_HORIZONTALLY, true );

    //wxMessageBox("toolWnd - dblClick!");
}

⌨️ 快捷键说明

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