tt_window.cpp

来自「ncbi源码」· C++ 代码 · 共 342 行

CPP
342
字号
/* * =========================================================================== * PRODUCTION $Log: tt_window.cpp,v $ * PRODUCTION Revision 1000.1  2004/06/01 21:10:29  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2 * PRODUCTION * =========================================================================== *//*  $Id: tt_window.cpp,v 1000.1 2004/06/01 21:10:29 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:  Mike DiCuccio * * File Description: * */#include <ncbi_pch.hpp>#include "tt_window.hpp"#include <gui/opengl/glutils.hpp>#include <math.h>BEGIN_NCBI_SCOPEstatic const int s_Border = 10;CTTWindow::CTTWindow(int x, int y, int w, int h, const char* label)    : CGlCanvas2d(x, y, w, h, label),      m_Font(CGlBitmapFont::eHelvetica12){    m_bActiveTooltip = false; // change this to see the difference    if(m_bActiveTooltip)        m_Tooltip.EnableActiveMode(static_cast<ITooltipClient*>(this));}void CTTWindow::draw(){    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glOrtho(0, w(), h(), 0, 1, -1);    x_RenderFirstRow();    x_RenderSecondRow();    x_RenderThirdRow();    CGlUtils::CheckGlError();}static int kRowH = 80;static int kRowSpace = 5;static int kButtons = 12;static int kButtonSize = 22;static int kButtonSpace = 4;    void CTTWindow::x_RenderFirstRow(){    static char Seq[8] = {'A', 0, 'C', 0, 'T', 0, 'G', 0    };    int text_h = m_Font.TextHeight();    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);            glColor3d(0.9, 0.9, 1.0);    glRectd(kRowSpace, kRowSpace,             kRowSpace * 2 + kButtons * (kButtonSize + kButtonSpace), kRowH  - kRowSpace);    glColor3i(0, 0, 0);    m_Font.TextOut(kRowSpace + 5, kRowSpace + 5 + text_h, "CTooltip::eHideOnMove");        float x = kRowSpace + kButtonSpace / 2;    float y = kRowH - kRowSpace - kButtonSpace / 2 - kButtonSize;    for(int i = 0; i < 12; i++) {                glColor3d(0.7, 0.7, 1.0);        glRectd(x, y, x + kButtonSize, y + kButtonSize);        glColor3d(0, 0, 0.25);        int ind = (i % 4) * 2;        m_Font.TextOut(x, y, kButtonSize, kButtonSize, Seq + ind, FL_ALIGN_CENTER);        x += kButtonSize + kButtonSpace;    }  }static int kAmp = 20;static int kSignalLength = 400;double GetFuncValue(int index)  {    double a = 0.1 * index;    return sin(a) * kAmp;}void CTTWindow::x_RenderSecondRow(){        int text_h = m_Font.TextHeight();    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);            glColor3d(0.9, 1.0, 0.9);    glRectd(kRowSpace, kRowH + kRowSpace,             kRowSpace * 2 + kSignalLength, 2 * kRowH  - kRowSpace);    glColor3i(0, 0, 0);    m_Font.TextOut(kRowSpace + 5, kRowH + kRowSpace + 5 + text_h, "CTooltip::eTrackOnMove");        //draw signal    glColor3d(0, 0.5, 0);    int y = 2 * kRowH - kRowSpace - kAmp;    glBegin(GL_LINE_STRIP);        for( int i = 0; i< kSignalLength; i++ ) {            double v = GetFuncValue(i);                glVertex2d(i + kRowSpace, y + v);        }    glEnd();}void CTTWindow::x_RenderThirdRow(){    int text_h = m_Font.TextHeight();    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);            glColor3d(1.0, 0.9, 0.9);    glRectd(kRowSpace, 2 * kRowH + kRowSpace,             kRowSpace * 2 + kButtons * (kButtonSize + kButtonSpace), 3 * kRowH  - kRowSpace);    glColor3i(0, 0, 0);    m_Font.TextOut(kRowSpace + 5, 2 * kRowH + kRowSpace + 5 + text_h, "CTooltip::eStayOnMove");        float x = kRowSpace + kButtonSpace / 2;    float y = 3 * kRowH - kRowSpace - kButtonSpace / 2 - kButtonSize;    for(int i = 0; i < 12; i++) {                glColor3d(1.0, 0.7, 0.7);        glRectd(x, y, x + kButtonSize, y + kButtonSize);        glColor3d(0, 0, 0.25);        int ind = (i % 4) * 2;        m_Font.TextOut(x, y, kButtonSize, kButtonSize, NStr::IntToString(i + 1).c_str(), FL_ALIGN_CENTER);        x += kButtonSize + kButtonSpace;    }}int CTTWindow::handle(int event){   switch (event) {   case FL_MOVE:        if(! m_bActiveTooltip)  { // this is needed only for "passive" tooltip            bool b_reset = true;                    int x = Fl::event_x();            int y = Fl::event_y();                        int row = x_HitTest(x, y, b_reset);                                    if(row == -1)    {                m_Tooltip.Deactivate(b_reset); // explicit deactivation            } else  {                CTooltip::EMode mode;                switch(row) {                case 0: mode = CTooltip::eHideOnMove; break;                case 1: mode = CTooltip::eTrackOnMove; break;                case 2: mode = CTooltip::eStayOnMove; break;                }                m_Tooltip.SetMode(mode);                                // explicit activation                m_Tooltip.Activate(this, m_TooltipText, m_X, m_Y, m_W, m_H);            }                    }        break;    }        // pass events to the tooltip object    m_Tooltip.Handle(event);    return CGlCanvas2d::handle(event);}/// Performs hit testing and save results (m_Tooltiptext, m_X, m_Y, m_W, m_H) /// for possible future useint    CTTWindow::x_HitTest(int x, int y, bool& b_reset){    switch(y / kRowH)   { // swith by row index    case 0: {        int btn_size = kButtonSize + kButtonSpace;        if(y >= kRowH  - kRowSpace - btn_size  &&  y < kRowH  - kRowSpace            && x > kRowSpace  &&  x < (kRowSpace * 2 + kButtons * btn_size) )    {            int loc_x = (x - kRowSpace - kButtonSpace / 2);            if(loc_x % btn_size < kButtonSize)  { // hit a button                int ind = loc_x / btn_size; // button index                m_TooltipText = "Postion ";                m_TooltipText += NStr::IntToString(ind);                if(! m_bActiveTooltip)  {                    m_X = x;                    m_Y = y;                    m_W = m_H = 1;                }                return 0;            }        }    }; break;    case 1: {        int bottom = 2 * kRowH  - kRowSpace;        if(y > bottom - 2 * kAmp  &&  y < bottom            && x > kRowSpace  &&  x < (kRowSpace * 2 + kSignalLength) )    {            int loc_x = x - kRowSpace;            double v = GetFuncValue(loc_x);                    char s[100];            sprintf(s, "Function value %.2f", v / -kAmp);            m_TooltipText = s;                        if(! m_bActiveTooltip)  {                m_X = x;                m_Y = y;                m_W = m_H = 1;            }            return 1;        }        };    case 2: {        int btn_size = kButtonSize + kButtonSpace;        int bottom = 3 * kRowH  - kRowSpace;        if(y >= bottom - btn_size  &&  y < bottom - kButtonSpace            && x > kRowSpace  &&  x < (kRowSpace * 2 + kButtons * btn_size) )    {            b_reset = false;            int loc_x = (x - kRowSpace - kButtonSpace / 2);            if(loc_x % btn_size < kButtonSize)  {                int ind = loc_x / btn_size;                m_TooltipText = "Button # ";                m_TooltipText += NStr::IntToString(ind + 1);                                // set tooltip area equal to button rectangle                m_X = kRowSpace + kButtonSpace / 2;                m_Y = 2 * kRowH  - kRowSpace;                m_W = m_H = kButtonSize;                return 2;            }        }    };    default: break;    }    return -1;}////////////////////////////////////////////////////////////////////////////////// ITooltip Implementation/// TC_NeedTooltip() and TC_GetTooltip() is evrything needed to show toolitps/// in "active" modebool    CTTWindow::TC_NeedTooltip(int x, int y){    bool b_reset; // dummy    int row = x_HitTest(x, y, b_reset);                        if(row == -1)    {        return false; // do not show (hide if visible)    } else  {        CTooltip::EMode mode;        switch(row) {        case 0: mode = CTooltip::eHideOnMove; break;        case 1: mode = CTooltip::eTrackOnMove; break;        case 2: mode = CTooltip::eStayOnMove; break;        }        m_Tooltip.SetMode(mode);        return true; // show tooltip    }    }string  CTTWindow::TC_GetTooltip(int& x, int& y, int& w, int& h){    x = m_X;    y = m_Y;    w = m_W;    h = m_H;    return m_TooltipText;}END_NCBI_SCOPE/* * =========================================================================== * $Log: tt_window.cpp,v $ * Revision 1000.1  2004/06/01 21:10:29  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2 * * Revision 1.2  2004/05/21 22:27:54  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.1  2004/01/14 11:55:57  lebedev * Moved from gui/opengl/demo * * Revision 1.3  2004/01/08 19:49:09  yazhuk * Implemented demonstration of "active" and "passive" CTooltips * * Revision 1.2  2003/12/29 21:14:01  yazhuk * Migrated from Fl_Tooltip to CTooltip * * Revision 1.1  2003/09/29 17:00:55  dicuccio * Initial revision * * =========================================================================== */

⌨️ 快捷键说明

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