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

📄 tbarsmpl.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    wxToolBarToolSimple *tool = (wxToolBarToolSimple *)toolBase;

    wxMemoryDC memDC;
    PrepareDC(dc);

    wxPen dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID);
    wxPen white_pen(wxT("WHITE"), 1, wxSOLID);
    wxPen black_pen(wxT("BLACK"), 1, wxSOLID);

    wxBitmap bitmap = tool->GetNormalBitmap();
    if (!bitmap.Ok())
        return;

    if ( !tool->IsToggled() )
    {
#if wxUSE_PALETTE
#ifndef __WXGTK__
        if (bitmap.GetPalette())
            memDC.SetPalette(*bitmap.GetPalette());
#endif
#endif // wxUSE_PALETTE

        int ax = (int)tool->m_x,
        ay = (int)tool->m_y,
        bx = (int)(tool->m_x+tool->GetWidth()),
        by = (int)(tool->m_y+tool->GetHeight());

        memDC.SelectObject(bitmap);
        if (m_windowStyle & wxTB_3DBUTTONS)
        {
            dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
            dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
            wxPen * old_pen = & dc.GetPen();
            dc.SetPen( white_pen );
            dc.DrawLine(ax,(by-1),ax,ay);
            dc.DrawLine(ax,ay,(bx-1),ay);
            dc.SetPen( dark_grey_pen );
            dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
            dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
            dc.SetPen( black_pen );
            dc.DrawLine(bx,ay,bx,by);
            dc.DrawLine(bx,by,ax,by);
            dc.SetPen( *old_pen );
            dc.DestroyClippingRegion();
            // Select bitmap out of the DC
        }
        else
        {
            dc.Blit(tool->m_x, tool->m_y,
                    bitmap.GetWidth(), bitmap.GetHeight(),
                    &memDC, 0, 0);
        }
        memDC.SelectObject(wxNullBitmap);

#if wxUSE_PALETTE
#ifndef __WXGTK__
        if (bitmap.GetPalette())
            memDC.SetPalette(wxNullPalette);
#endif
#endif // wxUSE_PALETTE
    }
    // No second bitmap, so draw a thick line around bitmap, or invert if mono
    else if ( tool->IsToggled() )
    {
        bool drawBorder = false;
#ifdef __X__ // X doesn't invert properly on colour
        drawBorder = wxColourDisplay();
#else       // Inversion works fine under Windows
        drawBorder = false;
#endif

        if (!drawBorder)
        {
            memDC.SelectObject(tool->GetNormalBitmap());
            dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(),
                    &memDC, 0, 0, wxSRC_INVERT);
            memDC.SelectObject(wxNullBitmap);
        }
        else
        {
            bitmap = tool->GetNormalBitmap();

            if (m_windowStyle & wxTB_3DBUTTONS)
            {
                int ax = (int)tool->m_x,
                ay = (int)tool->m_y,
                bx = (int)(tool->m_x+tool->GetWidth()),
                by = (int)(tool->m_y+tool->GetHeight());

                memDC.SelectObject(bitmap);
                dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
                dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
                wxPen * old_pen = & dc.GetPen();
                dc.SetPen( black_pen );
                dc.DrawLine(ax,(by-1),ax,ay);
                dc.DrawLine(ax,ay,(bx-1),ay);
                dc.SetPen( dark_grey_pen );
                dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
                dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
                dc.SetPen( white_pen );
                dc.DrawLine(bx,ay,bx,by);
                dc.DrawLine(bx,by,ax,by);
                dc.SetPen( *old_pen );
                dc.DestroyClippingRegion();
                memDC.SelectObject(wxNullBitmap);
            }
            else
            {
                wxCoord x = tool->m_x;
                wxCoord y = tool->m_y;
                wxCoord w = bitmap.GetWidth();
                wxCoord h = bitmap.GetHeight();
                wxPen thick_black_pen(wxT("BLACK"), 3, wxSOLID);

                memDC.SelectObject(bitmap);
                dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
                dc.Blit(tool->m_x, tool->m_y, w, h,
                        &memDC, 0, 0);
                dc.SetPen(thick_black_pen);
                dc.SetBrush(*wxTRANSPARENT_BRUSH);
                dc.DrawRectangle(x, y, w-1, h-1);
                dc.DestroyClippingRegion();
                memDC.SelectObject(wxNullBitmap);
            }
        }
    }
}

// ----------------------------------------------------------------------------
// toolbar geometry
// ----------------------------------------------------------------------------

void wxToolBarSimple::SetRows(int nRows)
{
    wxCHECK_RET( nRows != 0, _T("max number of rows must be > 0") );

    m_maxCols = (GetToolsCount() + nRows - 1) / nRows;

    AdjustScrollbars();
    Refresh();
}

wxToolBarToolBase *wxToolBarSimple::FindToolForPosition(wxCoord x,
                                                        wxCoord y) const
{
    wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
    while (node)
    {
        wxToolBarToolSimple *tool = (wxToolBarToolSimple *)node->GetData();
        if ((x >= tool->m_x) && (y >= tool->m_y) &&
            (x <= (tool->m_x + tool->GetWidth())) &&
            (y <= (tool->m_y + tool->GetHeight())))
        {
            return tool;
        }

        node = node->GetNext();
    }

    return (wxToolBarToolBase *)NULL;
}

// ----------------------------------------------------------------------------
// tool state change handlers
// ----------------------------------------------------------------------------

void wxToolBarSimple::DoEnableTool(wxToolBarToolBase *tool,
                                   bool WXUNUSED(enable))
{
    DrawTool(tool);
}

void wxToolBarSimple::DoToggleTool(wxToolBarToolBase *tool,
                                   bool WXUNUSED(toggle))
{
    DrawTool(tool);
}

void wxToolBarSimple::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
                                  bool WXUNUSED(toggle))
{
    // nothing to do
}

// Okay, so we've left the tool we're in ... we must check if the tool we're
// leaving was a 'sprung push button' and if so, spring it back to the up
// state.
void wxToolBarSimple::SpringUpButton(int id)
{
    wxToolBarToolBase *tool = FindById(id);

    if ( tool && tool->CanBeToggled() )
    {
        if (tool->IsToggled())
            tool->Toggle();

        DrawTool(tool);
    }
}

// ----------------------------------------------------------------------------
// scrolling implementation
// ----------------------------------------------------------------------------

/*
 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
 * noUnitsX/noUnitsY:        : no. units per scrollbar
 */
void wxToolBarSimple::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
                                   int noUnitsX, int noUnitsY,
                                   int xPos, int yPos)
{
    m_xScrollPixelsPerLine = pixelsPerUnitX;
    m_yScrollPixelsPerLine = pixelsPerUnitY;
    m_xScrollLines = noUnitsX;
    m_yScrollLines = noUnitsY;

    int w, h;
    GetSize(&w, &h);

    // Recalculate scroll bar range and position
    if (m_xScrollLines > 0)
    {
        m_xScrollPosition = xPos;
        SetScrollPos (wxHORIZONTAL, m_xScrollPosition, true);
    }
    else
    {
        SetScrollbar(wxHORIZONTAL, 0, 0, 0, false);
        m_xScrollPosition = 0;
    }

    if (m_yScrollLines > 0)
    {
        m_yScrollPosition = yPos;
        SetScrollPos (wxVERTICAL, m_yScrollPosition, true);
    }
    else
    {
        SetScrollbar(wxVERTICAL, 0, 0, 0, false);
        m_yScrollPosition = 0;
    }
    AdjustScrollbars();
    Refresh();

#if 0 //def __WXMSW__
    ::UpdateWindow ((HWND) GetHWND());
#endif
}

void wxToolBarSimple::OnScroll(wxScrollEvent& event)
{
    int orient = event.GetOrientation();

    int nScrollInc = CalcScrollInc(event);
    if (nScrollInc == 0)
        return;

    if (orient == wxHORIZONTAL)
    {
        int newPos = m_xScrollPosition + nScrollInc;
        SetScrollPos(wxHORIZONTAL, newPos, true );
    }
    else
    {
        int newPos = m_yScrollPosition + nScrollInc;
        SetScrollPos(wxVERTICAL, newPos, true );
    }

    if (orient == wxHORIZONTAL)
    {
        if (m_xScrollingEnabled)
            ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
        else
            Refresh();
    }
    else
    {
        if (m_yScrollingEnabled)
            ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
        else
            Refresh();
    }

    if (orient == wxHORIZONTAL)
    {
        m_xScrollPosition += nScrollInc;
    }
    else
    {
        m_yScrollPosition += nScrollInc;
    }

}

int wxToolBarSimple::CalcScrollInc(wxScrollEvent& event)
{
    int pos = event.GetPosition();
    int orient = event.GetOrientation();

    int nScrollInc = 0;
    if (event.GetEventType() == wxEVT_SCROLL_TOP)
    {
            if (orient == wxHORIZONTAL)
                nScrollInc = - m_xScrollPosition;
            else
                nScrollInc = - m_yScrollPosition;
    } else
    if (event.GetEventType() == wxEVT_SCROLL_BOTTOM)
    {
            if (orient == wxHORIZONTAL)
                nScrollInc = m_xScrollLines - m_xScrollPosition;
            else
                nScrollInc = m_yScrollLines - m_yScrollPosition;
    } else
    if (event.GetEventType() == wxEVT_SCROLL_LINEUP)
    {
            nScrollInc = -1;
    } else
    if (event.GetEventType() == wxEVT_SCROLL_LINEDOWN)
    {
            nScrollInc = 1;
    } else
    if (event.GetEventType() == wxEVT_SCROLL_PAGEUP)
    {
            if (orient == wxHORIZONTAL)
                nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
            else
                nScrollInc = -GetScrollPageSize(wxVERTICAL);
    } else
    if (event.GetEventType() == wxEVT_SCROLL_PAGEDOWN)
    {
            if (orient == wxHORIZONTAL)
                nScrollInc = GetScrollPageSize(wxHORIZONTAL);
            else
                nScrollInc = GetScrollPageSize(wxVERTICAL);
    } else
    if ((event.GetEventType() == wxEVT_SCROLL_THUMBTRACK) ||
        (event.GetEventType() == wxEVT_SCROLL_THUMBRELEASE))
    {
            if (orient == wxHORIZONTAL)
                nScrollInc = pos - m_xScrollPosition;
            else
                nScrollInc = pos - m_yScrollPosition;
    }

    if (orient == wxHORIZONTAL)
    {
        int w, h;
        GetClientSize(&w, &h);

        int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
        int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
        if (noPositions < 0)
            noPositions = 0;

        if ( (m_xScrollPosition + nScrollInc) < 0 )
            nScrollInc = -m_xScrollPosition; // As -ve as we can go
        else if ( (m_xScrollPosition + nScrollInc) > noPositions )
            nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go

        return nScrollInc;
    }
    else
    {
        int w, h;
        GetClientSize(&w, &h);

        int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
        int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
        if (noPositions < 0)
            noPositions = 0;

        if ( (m_yScrollPosition + nScrollInc) < 0 )
            nScrollInc = -m_yScrollPosition; // As -ve as we can go
        else if ( (m_yScrollPosition + nScrollInc) > noPositions )
            nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go

        return nScrollInc;
    }
}

// Adjust the scrollbars - new version.
void wxToolBarSimple::AdjustScrollbars()
{
    int w, h;
    GetClientSize(&w, &h);

    // Recalculate scroll bar range and position
    if (m_xScrollLines > 0)
    {
        int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
        int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
        if (newRange < 0)
            newRange = 0;

        m_xScrollPosition = wxMin(newRange, m_xScrollPosition);

        // Calculate page size i.e. number of scroll units you get on the
        // current client window
        int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
        if (noPagePositions < 1)
            noPagePositions = 1;

        SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
        SetScrollPageSize(wxHORIZONTAL, noPagePositions);
    }
    if (m_yScrollLines > 0)
    {
        int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
        int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
        if (newRange < 0)
            newRange = 0;

        m_yScrollPosition = wxMin(newRange, m_yScrollPosition);

        // Calculate page size i.e. number of scroll units you get on the
        // current client window
        int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
        if (noPagePositions < 1)
            noPagePositions = 1;

        SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
        SetScrollPageSize(wxVERTICAL, noPagePositions);
    }
}

// Prepare the DC by translating it according to the current scroll position
void wxToolBarSimple::PrepareDC(wxDC& dc)
{
    dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
}

void wxToolBarSimple::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
{
      *x_unit = m_xScrollPixelsPerLine;
      *y_unit = m_yScrollPixelsPerLine;
}

int wxToolBarSimple::GetScrollPageSize(int orient) const
{
    if ( orient == wxHORIZONTAL )
        return m_xScrollLinesPerPage;
    else
        return m_yScrollLinesPerPage;
}

void wxToolBarSimple::SetScrollPageSize(int orient, int pageSize)
{
    if ( orient == wxHORIZONTAL )
        m_xScrollLinesPerPage = pageSize;
    else
        m_yScrollLinesPerPage = pageSize;
}

/*
 * Scroll to given position (scroll position, not pixel position)
 */
void wxToolBarSimple::Scroll (int x_pos, int y_pos)
{
    int old_x, old_y;
    ViewStart (&old_x, &old_y);
    if (((x_pos == wxDefaultCoord) || (x_pos == old_x)) && ((y_pos == wxDefaultCoord) || (y_pos == old_y)))
        return;

    if (x_pos != wxDefaultCoord)
    {
        m_xScrollPosition = x_pos;
        SetScrollPos (wxHORIZONTAL, x_pos, true);
    }
    if (y_pos != wxDefaultCoord)
    {
        m_yScrollPosition = y_pos;
        SetScrollPos (wxVERTICAL, y_pos, true);
    }
    Refresh();

#if 0 //def __WXMSW__
    UpdateWindow ((HWND) GetHWND());
#endif
}

void wxToolBarSimple::EnableScrolling (bool x_scroll, bool y_scroll)
{
    m_xScrollingEnabled = x_scroll;
    m_yScrollingEnabled = y_scroll;
}

void wxToolBarSimple::GetVirtualSize (int *x, int *y) const
{
    *x = m_xScrollPixelsPerLine * m_xScrollLines;
    *y = m_yScrollPixelsPerLine * m_yScrollLines;
}

// Where the current view starts from
void wxToolBarSimple::ViewStart (int *x, int *y) const
{
    *x = m_xScrollPosition;
    *y = m_yScrollPosition;
}

#endif // wxUSE_TOOLBAR_SIMPLE

⌨️ 快捷键说明

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