message_dlg.cpp

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

CPP
397
字号
/* * =========================================================================== * PRODUCTION $Log: message_dlg.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 21:05:08  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10 * PRODUCTION * =========================================================================== *//*  $Id: message_dlg.cpp,v 1000.3 2004/06/01 21:05:08 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 "message_dlg.hpp"#include <FL/Fl_Pixmap.H>#include <FL/fl_draw.H>#include <FL/fl_ask.H>#include <FL/Fl_Help_View.H>#include <gui/utils/app_popup.hpp>BEGIN_NCBI_SCOPE//// icons//#include "info.xpm"#include "question.xpm"#include "exclamation.xpm"// callback function for linksstatic const char *s_DoLink(Fl_Widget *w, const char *uri){    CAppPopup::PopupURL(uri);    return 0;  // tells widget to do nothing}CMessageDlg::CMessageDlg(const string& msg, TDialogType type,                         EDialogIcon icon, EDialogTextMode text_mode)    : m_Message(msg){    // line-wrap the message if requested    if (text_mode == eWrap) {        list<string> lines;        NStr::Wrap(m_Message, 70, lines);        m_Message = NStr::Join(lines, "\n");    }    //    // plan our widget sizes and placements    //    // left/right/top/bottom margins    const int margin  = 10;    // button sizes    const int button_wid = 80;    const int button_ht = 25;    // icon sizes    const int icon_wid = 64;    const int icon_ht = 64;    // label postion    const int label_x = margin + icon_wid + 5;    const int label_y = margin;    // message label    // we create this first because we need the font information from it    Fl_Widget* label;    if (text_mode != eHtml) {        label = new Fl_Box(label_x, label_y, 5, 5);    } else {        Fl_Help_View* hv = new Fl_Help_View(label_x, label_y, 5, 5);        hv->link(s_DoLink);        label = hv;    }    label->labelsize(12);    // text label size:    // we need to compute this, based on its length    int label_wid = 0;    int label_ht = 0;    {{         int orig_font = fl_font();         int orig_size = fl_size();         fl_font(label->labelfont(), label->labelsize());         string::size_type pos = 0;         while (pos < m_Message.length()) {             string::size_type pos1 = m_Message.find_first_of("\n", pos);             if (pos1 == string::npos) {                 pos1 = m_Message.length();             }             string sub = m_Message.substr(pos, pos1 - pos);             if (sub.empty()) {                 sub = " ";             }             int w = 0;             int h = 0;             fl_measure(sub.c_str(), w, h);             label_wid = max(label_wid, w);             label_ht += h;             pos = pos1 + 1;         }         label_ht = max(label_ht, icon_ht);         fl_font(orig_font, orig_size);     }}    label->size(label_wid, label_ht);    label->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);    if (text_mode != eHtml) {        label->label(m_Message.c_str());    } else {        dynamic_cast<Fl_Help_View *>(label)->value(m_Message.c_str());    }    // window size: based on label size + fixed sizes for buttons, icons,    // margins, etc    const int window_wid = max(margin * 2 + icon_wid + 5 + label_wid,                               button_wid * 2 + margin * 3);    const int window_ht  = margin * 2 + label_ht + 5 + 25;    // finalize placements, depending on results of layout calculations    const int icon_x = margin;    const int icon_y = margin + (label_ht - icon_ht) / 2;    int button_y = margin + label_ht + 5;    //    // button placements - two options:    //  - darwin-style (centered)    //  - windows-style (left-justified)#ifdef NCBI_OS_DARWIN    int spare_wid = window_wid - button_wid * 2 - margin;    int button_x = (window_wid - button_wid) / 2;    int ok_button_x = spare_wid / 2;    int cancel_button_x = ok_button_x + button_wid + margin;#else    int button_x = window_wid - button_wid - margin;    int ok_button_x = window_wid - button_wid * 2 - margin * 2;    int cancel_button_x = ok_button_x + button_wid + margin;#endif    //    // now, build our windows    //    // create the top-level window    m_Window.reset(new Fl_Window(window_wid, window_ht));    m_Window->user_data(this);    if (type & eDialog_Modal) {        m_Window->set_modal();    }    // icon image: 64x64 pixels, centered and on the left    Fl_Box* image_box = new Fl_Box(icon_x, icon_y, icon_wid, icon_ht);    image_box->box(FL_DOWN_FRAME);    switch (icon) {    case eIcon_Info:        fl_beep(FL_BEEP_MESSAGE);        m_IconImg.reset(new Fl_Pixmap(sc_IconImg_Info));        break;    case eIcon_Question:        fl_beep(FL_BEEP_QUESTION);        m_IconImg.reset(new Fl_Pixmap(sc_IconImg_Question));        break;    case eIcon_Exclamation:        fl_beep(FL_BEEP_ERROR);        m_IconImg.reset(new Fl_Pixmap(sc_IconImg_Exclamation));        break;    case eIcon_Stop:        fl_beep(FL_BEEP_ERROR);        m_IconImg.reset(new Fl_Pixmap(sc_IconImg_Exclamation));        break;    }    if (m_IconImg.get()) {        image_box->image(m_IconImg.get());    }    // now, add the label    m_Window->add(label);    //    // buttons - depends on dialog type; layout is (slightly) platform-specific    //    switch ((EDialogType) (type & eDialog_StyleMask)) {    case eDialog_Ok:        {{             Fl_Button* ok_button =                 new Fl_Return_Button(button_x, button_y,                                      button_wid, button_ht, "OK");             ok_button->labelsize(12);             ok_button->callback((Fl_Callback*)x_callback_OK);             m_Window->callback((Fl_Callback*)x_callback_OK);         }}        break;    case eDialog_OkCancel:        {{             Fl_Button* cancel_button =                 new Fl_Return_Button(cancel_button_x, button_y,                                      button_wid, button_ht, "Cancel");             cancel_button->labelsize(12);             cancel_button->callback((Fl_Callback*)x_callback_Cancel);             m_Window->callback((Fl_Callback*)x_callback_Cancel);             Fl_Button* ok_button =                 new Fl_Button(ok_button_x, button_y,                               button_wid, button_ht, "OK");             ok_button->callback((Fl_Callback*)x_callback_OK);         }}        break;    case eDialog_YesNo:        {{             Fl_Button* no_button =                 new Fl_Return_Button(cancel_button_x, button_y,                                      button_wid, button_ht, "No");             no_button->labelsize(12);             no_button->callback((Fl_Callback*)x_callback_No);             m_Window->callback((Fl_Callback*)x_callback_No);             Fl_Button* yes_button =                 new Fl_Button(ok_button_x, button_y,                               button_wid, button_ht, "Yes");             yes_button->labelsize(12);             yes_button->callback((Fl_Callback*)x_callback_Yes);         }}        break;    }    // don't forget to end()    m_Window->end();}void CMessageDlg::SetTitle(const string& str){    m_Title = str;    m_Window->label(m_Title.c_str());}void CMessageDlg::x_callback_Cancel(Fl_Widget* w, void* data){    while (w->parent()) {        w = w->parent();    }    static_cast<CMessageDlg*>(w->user_data())->x_OnCancel();}void CMessageDlg::x_callback_OK(Fl_Widget* w, void* data){    while (w->parent()) {        w = w->parent();    }    static_cast<CMessageDlg*>(w->user_data())->x_OnOK();}void CMessageDlg::x_callback_Yes(Fl_Widget* w, void* data){    while (w->parent()) {        w = w->parent();    }    static_cast<CMessageDlg*>(w->user_data())->x_OnYes();}void CMessageDlg::x_callback_No(Fl_Widget* w, void* data){    while (w->parent()) {        w = w->parent();    }    static_cast<CMessageDlg*>(w->user_data())->x_OnNo();}void CMessageDlg::x_OnCancel(){    m_RetValue = eCancel;    m_Window->hide();}void CMessageDlg::x_OnOK(){    m_RetValue = eOK;    m_Window->hide();}void CMessageDlg::x_OnYes(){    m_RetValue = eYes;    m_Window->hide();}void CMessageDlg::x_OnNo(){    m_RetValue = eNo;    m_Window->hide();}END_NCBI_SCOPE/* * =========================================================================== * $Log: message_dlg.cpp,v $ * Revision 1000.3  2004/06/01 21:05:08  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10 * * Revision 1.10  2004/05/28 15:07:26  dicuccio * Adjusted .fl files: added ncbi_pch.hpp, fixed fonts. * * Revision 1.9  2004/05/21 22:27:51  gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.8  2004/03/10 19:27:17  johnson * fix m_RetVal/m_RetValue slip * * Revision 1.7  2004/03/01 15:14:04  dicuccio * Inherit from CDialog * * Revision 1.6  2003/12/03 03:59:57  jcherry * Made s_DoLink() really static * * Revision 1.5  2003/11/07 18:45:23  ucko * Properly capitalize Fl_Help_View.H. * * Revision 1.4  2003/11/07 17:16:44  jcherry * Added optional line-wrapping and html display * * Revision 1.3  2003/10/20 15:14:34  johnson * tweaked EDialogType such that one can specify modality as well as button * style. * * Revision 1.2  2003/06/02 11:40:04  dicuccio * Fixed compilation for DARWIN - missing spare_wid * * Revision 1.1  2003/05/30 12:51:11  dicuccio * Moved message box code from gui/dialogs/general -> gui/utils, as it is a very * basic piece of code that lots of other code depends on (specifically works * around circular dependency with various gui/widget libraries) * * Revision 1.2  2003/05/19 16:40:16  dicuccio * Fix compilation error - multiple x_OnCancel() functions * * Revision 1.1  2003/05/19 13:54:10  dicuccio * Initial revision * * =========================================================================== */

⌨️ 快捷键说明

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