tab_control.cpp

来自「ncbi源码」· C++ 代码 · 共 782 行 · 第 1/2 页

CPP
782
字号
/* * =========================================================================== * PRODUCTION $Log: tab_control.cpp,v $ * PRODUCTION Revision 1000.1  2004/06/01 21:09:17  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.8 * PRODUCTION * =========================================================================== *//*  $Id: tab_control.cpp,v 1000.1 2004/06/01 21:09:17 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors:  Andrey Yazhuk * * File Description: */ #include <ncbi_pch.hpp>#include <gui/widgets/fl/tab_control.hpp>#include <gui/widgets/fl/menu.hpp>#include <FL/fl_draw.H>BEGIN_NCBI_SCOPE////////////////////////////////////////////////////////////////////////////////// CTabControlCTabControl::CTabControl():   Fl_Group(0, 0, 0, 0),    m_Selected(-1),    m_TabAreaH(0){    x_Init();    end();}CTabControl::CTabControl(int x, int y, int w, int h, const char* label):   Fl_Group(x, y, w, h, label),    m_Selected(-1),    m_TabAreaH(0){    x_Init();    end();}CTabControl::~CTabControl(){}static const int kScrollBarSize = 15;void    CTabControl::x_Init(){    end();    m_ResizePolicies = fShrink;//|fExpand ;    box(FL_NO_BOX);    m_Event.StandardConfig();    m_BorderColor =  fl_rgb_color(127, 127, 127);    m_ActiveBorderColor =  fl_rgb_color(96, 96, 96);        m_BackColor =  fl_rgb_color(224, 224, 224);    m_ActiveBackColor = fl_rgb_color(240, 240, 240);        m_TextColor =  fl_rgb_color(64, 64, 64);    m_ActiveTextColor =  fl_rgb_color(0, 0, 0);    m_Tooltip.SetMode(CTooltip::eStayOnMove);    m_Tooltip.EnableActiveMode(this);}void    CTabControl::SetResizePolicies(int policies){    if(m_ResizePolicies != policies)    {        m_ResizePolicies = policies;        x_Layout();        redraw();    }}bool    CTabControl::AddTab(Fl_Widget* pane, const char* label){    if(pane)    {        int count = x_GetTabsCount();        return x_InsertTab(pane, count, label, "");    }    return false;}bool    CTabControl::InsertTab(Fl_Widget* pane, int index, const char* label){    int count = x_GetTabsCount();    if(pane  &&  index >=0  &&  index <= count)   {        return x_InsertTab(pane, index, label, "");    }     return false;}bool    CTabControl::RemoveTab(Fl_Widget* pane){    int index = x_GetTabIndex(pane);    if(index >= 0)   {        return x_RemoveTab(index);    }    return false;}bool    CTabControl::RemoveTab(int index){    return x_RemoveTab(index);}void    CTabControl::RemoveAllTabs(){    x_RemoveAllTabs();}int     CTabControl::GetTabsCount()  const{    return x_GetTabsCount();}    Fl_Widget*  CTabControl::GetTab(int index){    return x_GetPane(index);}string  CTabControl::GetTabLabel(int index)  const{    if(x_IsIndexValid(index))   {        return m_vDescrs[index]->m_Label;    } else return "";}string  CTabControl::GetTabTooltip(int index)  const{    if(x_IsIndexValid(index))   {        return m_vDescrs[index]->m_Tooltip;    } else return "";}void    CTabControl::SetTabLabel(int index, const char* label){    if(x_IsIndexValid(index))   {        m_vDescrs[index]->m_Label = label ? label : "";        x_Layout();    }}   void    CTabControl::SetTabTooltip(int index, const char* tooltip){    if(x_IsIndexValid(index))   {        m_vDescrs[index]->m_Tooltip = tooltip ? tooltip : "";        x_Layout();    }}int     CTabControl::GetSelectedTab() const{    return m_Selected;}void    CTabControl::SelectTab(int index){    x_SelectTab(index);}Fl_Widget*    CTabControl::x_GetPane(int index){    if(x_IsIndexValid(index))   {        return m_vDescrs[index]->m_pPane;    }    _ASSERT(false);    return false;}CTabControl::STabDescr*  CTabControl::x_GetTab(int index){    if(x_IsIndexValid(index))   {        return m_vDescrs[index];    }    _ASSERT(false);    return NULL;}int     CTabControl::x_GetTabIndex(Fl_Widget* pane)  const{    for( int i = 0; i < x_GetTabsCount(); i++ ) {        if(m_vDescrs[i]->m_pPane == pane)            return i;    }    return -1;}bool    CTabControl::x_InsertTab(Fl_Widget* pane, int index, const char* label,                         const char* tooltip){    bool b_valid = index >= 0  && index <= x_GetTabsCount();    _ASSERT(b_valid);    if(b_valid) {        STabDescr* p_descr = new STabDescr;        p_descr->m_pPane = pane;        p_descr->m_Label = label ? label : "";        p_descr->m_Tooltip = tooltip ? tooltip : "";        pane->hide();        Fl_Group::add(pane);        m_vDescrs.insert(m_vDescrs.begin() + index, p_descr);            x_LayoutTabs();        x_SelectTab(index);        return true;    } else return false;}bool    CTabControl::x_RemoveTab(int index){    _ASSERT(x_IsIndexValid(index));    if(x_IsIndexValid(index))    {        STabDescr* p_descr = m_vDescrs[index];            p_descr->m_pPane->hide();        Fl_Group::remove(p_descr->m_pPane);            delete p_descr;        m_vDescrs.erase(m_vDescrs.begin() + index);        x_LayoutTabs();        // adjust selected        if(index < m_Selected)  {            m_Selected--;        }        bool b_reselect = (index == m_Selected);                if(index == x_GetTabsCount())             index--;        if(b_reselect)  {                m_Selected = -1;            x_SelectTab(index);            }                redraw();           return true;    } else return false;}void    CTabControl::x_RemoveAllTabs(){    while(! m_vDescrs.empty())  {        STabDescr* p_descr = m_vDescrs.back();                Fl_Group::remove(p_descr->m_pPane);        m_vDescrs.pop_back();    }}void    CTabControl::x_SelectTab(int index){        _ASSERT(index >= -1  &&  index < (int) x_GetTabsCount());    if(index != m_Selected) {        Fl_Widget* curr_w = x_GetSelectedPane();        if(curr_w)  {            curr_w->hide();        }        m_Selected = index;                curr_w = x_GetSelectedPane();            if(curr_w)  {                int cl_x, cl_y, cl_w, cl_h;            x_GetClientRect(cl_x, cl_y, cl_w, cl_h);            curr_w->resize(cl_x, cl_y, cl_w, cl_h);            curr_w->show();        }        redraw();    }}CTabControl::STabDescr*   CTabControl::x_GetSelectedTab(){    if(x_IsIndexValid(m_Selected))  {        return m_vDescrs[m_Selected];    } else return NULL;}Fl_Widget*   CTabControl::x_GetSelectedPane(){    if(x_IsIndexValid(m_Selected))  {        return m_vDescrs[m_Selected]->m_pPane;    } else return NULL;}const static int    kClientOffset = 2; // client insetconst static int    kTabRowH = 24; // height of a single row of tabsconst static int    kTabOffsetX  = 8;const static int    kTabOffsetY  = 6;const static int    kTabSpaceY = 4; // vertical spacing between client and tab rectangleconst static int    kTabSpaceX = 2; // space between client frame and tab rectanglesvoid    CTabControl::draw(){    fl_push_clip(x(), y(), w(), h());    if (damage() & ~FL_DAMAGE_CHILD) { // redraw the entire thing:                          int has_tabs = x_GetTabsCount() > 0;        int x2 = x() + w() - 1;        int y2 = y() + h() - 1;                if(has_tabs)   {    // outer frame - backround color                        fl_color(m_BackColor);            fl_xyline(x(), y(), x2);                    fl_xyline(x(), y(), x(), y2);                    fl_xyline(x2,  y(), x2,  y2);            } else { // fill entire area            fl_rectf(x(), y(), w(), h(), m_BackColor);                        }        int tab_top = y() + h() - x_GetTabAreaH();        int tab_bottom = y() + h();        int row_h = x_GetTabRowH();        // fill Tab area        int area_h = x_GetTabAreaH() + kClientOffset;        int area_w = w() - 2 * kClientOffset;                int highlight_h = kTabSpaceY;                int off = kClientOffset - 1;                if(m_Selected != -1)    {            STabDescr* p_descr = m_vDescrs[m_Selected];            highlight_h = p_descr->m_y - (h() - x_GetTabAreaH() - kClientOffset);            y2 = y() + p_descr->m_y;        } else y2 -= off;                int y3 =  tab_top - kClientOffset;        fl_rectf(x() + kClientOffset, y3, area_w, highlight_h, m_ActiveBackColor);        fl_rectf(x(), y3 + highlight_h, w(), area_h - highlight_h, m_BackColor);                        // draw inner frame with border color        fl_color(m_ActiveBorderColor);        fl_xyline(x() + off, y() + off, x2 - off);                fl_xyline(x() + off, y() + off , x() + off, y2);                fl_xyline(x2 - off, y() + off, x2 - off, y2);            if(! has_tabs)  {            fl_xyline(x() + off, y2, x2 - off, y2);            }        // draw tabs        fl_font(FL_HELVETICA, 12);            for( int i = 0; i < x_GetTabsCount(); i++ )   {            x_DrawTab(i);        }    }    Fl_Group::draw();        fl_pop_clip();}/// draws Tab corresponding to a given indexvoid    CTabControl::x_DrawTab(int index)   {    STabDescr* p_descr = m_vDescrs[index];        bool bActive = (index == m_Selected);    int off_x = bActive ? 2 : 0;    int x1 = x() + p_descr->m_x - off_x;

⌨️ 快捷键说明

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