TGMsgBox.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGMsgBox.cxx 35518 2010-09-21 09:54:10Z bellenot $
00002 // Author: Fons Rademakers   09/01/98
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 /**************************************************************************
00012 
00013     This source is based on Xclass95, a Win95-looking GUI toolkit.
00014     Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
00015 
00016     Xclass95 is free software; you can redistribute it and/or
00017     modify it under the terms of the GNU Library General Public
00018     License as published by the Free Software Foundation; either
00019     version 2 of the License, or (at your option) any later version.
00020 
00021 **************************************************************************/
00022 
00023 //////////////////////////////////////////////////////////////////////////
00024 //                                                                      //
00025 // TMsgBox                                                              //
00026 //                                                                      //
00027 // A message dialog box.                                                //
00028 //                                                                      //
00029 //////////////////////////////////////////////////////////////////////////
00030 
00031 #include "TGMsgBox.h"
00032 #include "TGButton.h"
00033 #include "TGIcon.h"
00034 #include "TGLabel.h"
00035 #include "TList.h"
00036 
00037 
00038 ClassImp(TGMsgBox)
00039 
00040 
00041 //______________________________________________________________________________
00042 TGMsgBox::TGMsgBox(const TGWindow *p, const TGWindow *main,
00043                    const char *title, const char *msg, const TGPicture *icon,
00044                    Int_t buttons, Int_t *ret_code, UInt_t options,
00045                    Int_t text_align) :
00046    TGTransientFrame(p, main, 10, 10, options)
00047 {
00048    // Create a message dialog box.
00049 
00050    if (p)
00051       PMsgBox(title, msg, icon, buttons, ret_code, text_align);
00052    else
00053       MakeZombie();
00054 }
00055 
00056 //______________________________________________________________________________
00057 TGMsgBox::TGMsgBox(const TGWindow *p, const TGWindow *main,
00058                    const char *title, const char *msg, EMsgBoxIcon icon,
00059                    Int_t buttons, Int_t *ret_code, UInt_t options,
00060                    Int_t text_align) :
00061    TGTransientFrame(p, main, 10, 10, options)
00062 {
00063    // Create a message dialog box with the following parameters:.
00064    //       title: Window title
00065    //         msg: Message to be shown ('\n' may be used to split it in lines)
00066    //        icon: Picture to be shown at the left on the dialog window.
00067    //              It might take any of the following values:
00068    //              kMBIconStop, kMBIconQuestion,
00069    //              kMBIconExclamation, kMBIconAsterisk
00070    //     buttons: Buttons to be shown at the botton of the dialgo window.
00071    //              Look at EMsgBoxButton for the different possible values.
00072    //    ret_code: It will hold the value of the button pressed when the
00073    //              dialog is closed
00074    //     options: Frame options of this dialog window.
00075    //  text_align: Align options for 'msg'. See ETextJustification for the values.
00076 
00077    const TGPicture *icon_pic;
00078 
00079    switch (icon) {
00080       case kMBIconStop:
00081          icon_pic = fClient->GetPicture("mb_stop_s.xpm");
00082          if (!icon_pic) Error("TGMsgBox", "mb_stop_s.xpm not found");
00083          break;
00084 
00085       case kMBIconQuestion:
00086          icon_pic = fClient->GetPicture("mb_question_s.xpm");
00087          if (!icon_pic) Error("TGMsgBox", "mb_question_s.xpm not found");
00088          break;
00089 
00090       case kMBIconExclamation:
00091          icon_pic = fClient->GetPicture("mb_exclamation_s.xpm");
00092          if (!icon_pic) Error("TGMsgBox", "mb_exclamation_s.xpm not found");
00093          break;
00094 
00095       case kMBIconAsterisk:
00096          icon_pic = fClient->GetPicture("mb_asterisk_s.xpm");
00097          if (!icon_pic) Error("TGMsgBox", "mb_asterisk_s.xpm not found");
00098          break;
00099 
00100       default:
00101          icon_pic = 0;
00102          break;
00103    }
00104 
00105    if (p)
00106       PMsgBox(title, msg, icon_pic, buttons, ret_code, text_align);
00107    else
00108       MakeZombie();
00109 }
00110 
00111 //______________________________________________________________________________
00112 void TGMsgBox::PMsgBox(const char *title, const char *msg,
00113                        const TGPicture *icon, Int_t buttons, Int_t *ret_code,
00114                        Int_t text_align)
00115 {
00116    // Protected, common message dialog box initialization.
00117 
00118    UInt_t nb, width, height;
00119 
00120    fYes = fNo = fOK = fApply = fRetry = fIgnore = fCancel = fClose =
00121    fYesAll = fNoAll = fNewer = fAppend = fDismiss   = 0;
00122    fIcon      = 0;
00123    fMsgList   = new TList;
00124    fRetCode   = ret_code;
00125    nb = width = 0;
00126 
00127    // create the buttons
00128 
00129    fButtonFrame = new TGHorizontalFrame(this, 60, 20, kFixedWidth);
00130    fL1 = new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 3, 3, 0, 0);
00131 
00132    buttons &= (kMBYes | kMBNo | kMBOk | kMBApply |
00133                kMBRetry | kMBIgnore | kMBCancel | kMBClose | kMBDismiss |
00134                kMBYesAll | kMBNoAll | kMBAppend | kMBNewer);
00135    if (buttons == 0) buttons = kMBDismiss;
00136 
00137    if (buttons & kMBYes) {
00138       fYes = new TGTextButton(fButtonFrame, new TGHotString("&Yes"), kMBYes);
00139       fYes->Associate(this);
00140       fButtonFrame->AddFrame(fYes, fL1);
00141       width = TMath::Max(width, fYes->GetDefaultWidth()); ++nb;
00142    }
00143 
00144    if (buttons & kMBNo) {
00145       fNo = new TGTextButton(fButtonFrame, new TGHotString("&No"), kMBNo);
00146       fNo->Associate(this);
00147       fButtonFrame->AddFrame(fNo, fL1);
00148       width = TMath::Max(width, fNo->GetDefaultWidth()); ++nb;
00149    }
00150 
00151    if (buttons & kMBOk) {
00152       fOK = new TGTextButton(fButtonFrame, new TGHotString("&OK"), kMBOk);
00153       fOK->Associate(this);
00154       fButtonFrame->AddFrame(fOK, fL1);
00155       width = TMath::Max(width, fOK->GetDefaultWidth()); ++nb;
00156    }
00157 
00158    if (buttons & kMBApply) {
00159       fApply = new TGTextButton(fButtonFrame, new TGHotString("&Apply"), kMBApply);
00160       fApply->Associate(this);
00161       fButtonFrame->AddFrame(fApply, fL1);
00162       width = TMath::Max(width, fApply->GetDefaultWidth()); ++nb;
00163    }
00164 
00165    if (buttons & kMBRetry) {
00166       fRetry = new TGTextButton(fButtonFrame, new TGHotString("&Retry"), kMBRetry);
00167       fRetry->Associate(this);
00168       fButtonFrame->AddFrame(fRetry, fL1);
00169       width = TMath::Max(width, fRetry->GetDefaultWidth()); ++nb;
00170    }
00171 
00172    if (buttons & kMBIgnore) {
00173       fIgnore = new TGTextButton(fButtonFrame, new TGHotString("&Ignore"), kMBIgnore);
00174       fIgnore->Associate(this);
00175       fButtonFrame->AddFrame(fIgnore, fL1);
00176       width = TMath::Max(width, fIgnore->GetDefaultWidth()); ++nb;
00177    }
00178 
00179    if (buttons & kMBCancel) {
00180       fCancel = new TGTextButton(fButtonFrame, new TGHotString("&Cancel"), kMBCancel);
00181       fCancel->Associate(this);
00182       fButtonFrame->AddFrame(fCancel, fL1);
00183       width = TMath::Max(width, fCancel->GetDefaultWidth()); ++nb;
00184    }
00185 
00186    if (buttons & kMBClose) {
00187       fClose = new TGTextButton(fButtonFrame, new TGHotString("C&lose"), kMBClose);
00188       fClose->Associate(this);
00189       fButtonFrame->AddFrame(fClose, fL1);
00190       width = TMath::Max(width, fClose->GetDefaultWidth()); ++nb;
00191    }
00192 
00193    if (buttons & kMBYesAll) {
00194       fYesAll = new TGTextButton(fButtonFrame, new TGHotString("Y&es to All"), kMBYesAll);
00195       fYesAll->Associate(this);
00196       fButtonFrame->AddFrame(fYesAll, fL1);
00197       width = TMath::Max(width, fYesAll->GetDefaultWidth()); ++nb;
00198    }
00199 
00200    if (buttons & kMBNoAll) {
00201       fNoAll = new TGTextButton(fButtonFrame, new TGHotString("No &to All"), kMBNoAll);
00202       fNoAll->Associate(this);
00203       fButtonFrame->AddFrame(fNoAll, fL1);
00204       width = TMath::Max(width, fNoAll->GetDefaultWidth()); ++nb;
00205    }
00206 
00207    if (buttons & kMBNewer) {
00208       fNewer = new TGTextButton(fButtonFrame, new TGHotString("Ne&wer Only"), kMBNewer);
00209       fNewer->Associate(this);
00210       fButtonFrame->AddFrame(fNewer, fL1);
00211       width = TMath::Max(width, fNewer->GetDefaultWidth()); ++nb;
00212    }
00213 
00214    if (buttons & kMBAppend) {
00215       fAppend = new TGTextButton(fButtonFrame, new TGHotString("A&ppend"), kMBAppend);
00216       fAppend->Associate(this);
00217       fButtonFrame->AddFrame(fAppend, fL1);
00218       width = TMath::Max(width, fAppend->GetDefaultWidth()); ++nb;
00219    }
00220 
00221    if (buttons & kMBDismiss) {
00222       fDismiss = new TGTextButton(fButtonFrame, new TGHotString("&Dismiss"), kMBDismiss);
00223       fDismiss->Associate(this);
00224       fButtonFrame->AddFrame(fDismiss, fL1);
00225       width = TMath::Max(width, fDismiss->GetDefaultWidth()); ++nb;
00226    }
00227 
00228    // place buttons at the bottom
00229 
00230    fL2 = new TGLayoutHints(kLHintsBottom | kLHintsCenterX, 0, 0, 5, 5);
00231    AddFrame(fButtonFrame, fL2);
00232 
00233    // keep the buttons centered and with the same width
00234 
00235    fButtonFrame->Resize((width + 20) * nb, GetDefaultHeight());
00236 
00237    fIconFrame = new TGHorizontalFrame(this, 60, 20);
00238 
00239    fL3 = new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 2, 2, 2, 2);
00240 
00241    if (icon) {
00242       fIcon = new TGIcon(fIconFrame, icon, icon->GetWidth(), icon->GetHeight());
00243       fIconFrame->AddFrame(fIcon, fL3);
00244    }
00245 
00246    fLabelFrame = new TGVerticalFrame(fIconFrame, 60, 20);
00247 
00248    fL4 = new TGLayoutHints(kLHintsCenterY | kLHintsLeft | kLHintsExpandX,
00249                            4, 2, 2, 2);
00250    fL5 = new TGLayoutHints(kLHintsTop | kLHintsExpandX, 10, 10, 7, 2);
00251 
00252    // make one label per line of the message
00253    TGLabel *label;
00254 
00255    char *line;
00256    char *tmpMsg, *nextLine;
00257 
00258    tmpMsg = new char[strlen(msg) + 1];
00259    nextLine = tmpMsg;
00260 
00261    line = tmpMsg;
00262    strlcpy(nextLine, msg, strlen(msg) + 1);
00263    while ((nextLine = strchr(line, '\n'))) {
00264       *nextLine = 0;
00265       label = new TGLabel(fLabelFrame, line);
00266       label->SetTextJustify(text_align);
00267       fMsgList->Add(label);
00268       fLabelFrame->AddFrame(label, fL4);
00269       line = nextLine + 1;
00270    }
00271 
00272    label = new TGLabel(fLabelFrame, line);
00273    label->SetTextJustify(text_align);
00274    fMsgList->Add(label);
00275    fLabelFrame->AddFrame(label, fL4);
00276    delete [] tmpMsg;
00277 
00278    fIconFrame->AddFrame(fLabelFrame, fL4);
00279    AddFrame(fIconFrame, fL5);
00280 
00281    MapSubwindows();
00282 
00283    width  = GetDefaultWidth();
00284    height = GetDefaultHeight();
00285 
00286    Resize(width, height);
00287 
00288    // position relative to the parent's window
00289 
00290    CenterOnParent();
00291 
00292    // make the message box non-resizable
00293 
00294    SetWMSize(width, height);
00295    SetWMSizeHints(width, height, width, height, 0, 0);
00296 
00297    // set names
00298 
00299    SetWindowName(title);
00300    SetIconName(title);
00301    SetClassHints("MsgBox", "MsgBox");
00302 
00303    SetMWMHints(kMWMDecorAll | kMWMDecorResizeH  | kMWMDecorMaximize |
00304                               kMWMDecorMinimize | kMWMDecorMenu,
00305                kMWMFuncAll  | kMWMFuncResize    | kMWMFuncMaximize |
00306                               kMWMFuncMinimize,
00307                kMWMInputModeless);
00308 
00309    MapRaised();
00310    fClient->WaitFor(this);
00311 }
00312 
00313 //______________________________________________________________________________
00314 TGMsgBox::~TGMsgBox()
00315 {
00316    // Destroy message dialog box.
00317 
00318    if (IsZombie()) return;
00319    if (fYes)     delete fYes;
00320    if (fNo)      delete fNo;
00321    if (fOK)      delete fOK;
00322    if (fApply)   delete fApply;
00323    if (fRetry)   delete fRetry;
00324    if (fIgnore)  delete fIgnore;
00325    if (fCancel)  delete fCancel;
00326    if (fClose)   delete fClose;
00327    if (fDismiss) delete fDismiss;
00328    if (fYesAll)  delete fYesAll;
00329    if (fNoAll)   delete fNoAll;
00330    if (fNewer)   delete fNewer;
00331    if (fAppend)  delete fAppend;
00332 
00333    if (fIcon) delete fIcon;
00334    delete fButtonFrame;
00335    delete fIconFrame;
00336    delete fLabelFrame;
00337    fMsgList->Delete();
00338    delete fMsgList;
00339    delete fL1; delete fL2; delete fL3; delete fL4; delete fL5;
00340 }
00341 
00342 //______________________________________________________________________________
00343 void TGMsgBox::CloseWindow()
00344 {
00345    // Close dialog box. Before deleting itself it sets the return code
00346    // to kMBClose.
00347 
00348    if (fRetCode) *fRetCode = kMBClose;
00349    DeleteWindow();
00350 }
00351 
00352 //______________________________________________________________________________
00353 Bool_t TGMsgBox::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
00354 {
00355    // Process message dialog box event.
00356 
00357    switch (GET_MSG(msg)) {
00358       case kC_COMMAND:
00359          switch (GET_SUBMSG(msg)) {
00360             case kCM_BUTTON:
00361                if (fRetCode) *fRetCode = (Int_t) parm1;
00362                DeleteWindow();
00363                break;
00364 
00365             default:
00366                break;
00367          }
00368          break;
00369       default:
00370          break;
00371    }
00372    return kTRUE;
00373 }

Generated on Tue Jul 5 14:22:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1