Files
musrfit/src/classes/PMsgBox.cpp

145 lines
5.7 KiB
C++

/***************************************************************************
PMsgBox.cpp
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
***************************************************************************/
/***************************************************************************
* Copyright (C) 2007-2025 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "PMsgBox.h"
//--------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------
/**
* \brief Constructor that creates and displays the message box GUI.
*
* This constructor performs the following steps:
* 1. Creates a scrollable list box widget
* 2. Parses the error message string by splitting at newline characters
* 3. Populates the list box with individual message lines
* 4. Creates an Exit button with callback to DoExit()
* 5. Configures layout and displays the window
*
* The message parsing algorithm:
* - Adds an initial empty line
* - Splits the input string at each '\n' character
* - Each substring becomes a separate list box entry
* - Handles multi-line messages gracefully
*
* GUI Layout:
* - List box: 600x200 pixels, expandable, with 5-pixel margins
* - Button frame: Fixed width (150px), contains Exit button
* - Window title: "Error Message"
*
* \param errMsg Multi-line error message string with '\n' separators
* \param p Parent window pointer (typically gClient->GetRoot())
* \param w Initial window width in pixels
* \param h Initial window height in pixels
*
* \note The window is automatically mapped (displayed) after construction
* \note The list box ID is set to 89 (ROOT widget identifier)
*/
PMsgBox::PMsgBox(const std::string errMsg, const TGWindow *p, UInt_t w, UInt_t h) : TGMainFrame(p, w, h)
{
fListBox = new TGListBox(this, 89);
// feed list box with errMsg
size_t start = 0;
size_t end = errMsg.find("\n");
unsigned int i=1;
std::string tok{""};
fListBox->AddEntry(tok.c_str(), i++);
while (end != std::string::npos) {
tok = errMsg.substr(start, end - start);
start = end + 1;
end = errMsg.find("\n", start);
fListBox->AddEntry(tok.c_str(), i++);
}
fListBox->Resize(600, 200);
AddFrame(fListBox, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5));
// Create a horizontal frame containing button(s)
TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 150, 20, kFixedWidth);
TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
exit->Connect("Pressed()", "PMsgBox", this, "DoExit()");
hframe->AddFrame(exit, new TGLayoutHints(kLHintsExpandX, 5, 5, 3, 4));
AddFrame(hframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 5, 1));
// Set a name to the main frame
SetWindowName("Error Message");
MapSubwindows();
// Initialize the layout algorithm via Resize()
Resize(GetDefaultSize());
// Map main frame
MapWindow();
}
//--------------------------------------------------------------------------
// Destructor
//--------------------------------------------------------------------------
/**
* \brief Destructor that cleans up the message box resources.
*
* The ROOT framework automatically handles cleanup of GUI components
* (list box, buttons, frames) through the TGMainFrame destructor chain,
* so no explicit cleanup is required here.
*
* \note GUI widgets are managed by ROOT and are automatically deleted
* when the main frame is destroyed
*/
PMsgBox::~PMsgBox()
{
// nothing to be done here - ROOT handles GUI cleanup automatically
}
//--------------------------------------------------------------------------
// DoExit
//--------------------------------------------------------------------------
/**
* \brief Exit button callback that terminates the application.
*
* This method is invoked when the user clicks the Exit button.
* It terminates the entire ROOT application with exit code 0,
* which causes a clean shutdown of the program.
*
* The method is connected to the Exit button's "Pressed()" signal
* in the constructor via ROOT's signal/slot mechanism:
* \code
* exit->Connect("Pressed()", "PMsgBox", this, "DoExit()");
* \endcode
*
* \note This terminates the entire application, not just the message box window
* \note Exit code 0 indicates successful termination
*
* \see gApplication for ROOT's global application object
*/
void PMsgBox::DoExit()
{
gApplication->Terminate(0);
}