rhtml.cxx

Go to the documentation of this file.
00001 // @(#)root/test/rhtml/:$Id: rhtml.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Bertrand Bellenot   09/05/2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, 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 #include "TApplication.h"
00013 #include "TSystem.h"
00014 #include "TGMenu.h"
00015 #include "TGComboBox.h"
00016 #include "TGFrame.h"
00017 #include "TGButton.h"
00018 #include "TGTextBuffer.h"
00019 #include "TGTextEntry.h"
00020 #include "TGStatusBar.h"
00021 #include "TGFileDialog.h"
00022 #include "TFile.h"
00023 #include "TBrowser.h"
00024 #include "TGHtml.h"
00025 #include "TString.h"
00026 #include "TUrl.h"
00027 #include "TSocket.h"
00028 #include "Riostream.h"
00029 #include "rhtml.h"
00030 
00031 #ifdef WIN32
00032 #include "TWin32SplashThread.h"
00033 #endif
00034 
00035 enum EMyMessageTypes {
00036    M_FILE_OPEN,
00037    M_FILE_BROWSE,
00038    M_FILE_EXIT,
00039    M_FAVORITES_ADD,
00040    M_TOOLS_CLEARHIST,
00041    M_HELP_ABOUT
00042 };
00043 
00044 const char *filetypes[] = { 
00045    "HTML files",    "*.html",
00046    "All files",     "*",
00047     0,               0 
00048 };
00049 
00050 const char *HtmlError[] = {
00051 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd\"> ",
00052 "<HTML><HEAD><TITLE>RHTML cannot display the webpage</TITLE> ",
00053 "<META http-equiv=Content-Type content=\"text/html; charset=UTF-8\"></HEAD> ",
00054 "<BODY> ",
00055 "<TABLE cellSpacing=0 cellPadding=0 width=730 border=0> ",
00056 "  <TBODY> ",
00057 "  <TR> ",
00058 "    <TD id=infoIconAlign vAlign=top align=left width=60 rowSpan=2> ",
00059 "    <IMG src=\"info.gif\"> ",
00060 "    </TD> ",
00061 "    <TD id=mainTitleAlign vAlign=center align=left width=*> ",
00062 "      <H1 id=mainTitle>RHTML cannot display the webpage</H1></TD></TR> ",
00063 "  <TR> ",
00064 "    <TD class=errorCodeAndDivider id=errorCodeAlign align=right>&nbsp;  ",
00065 "      <DIV class=divider></DIV></TD></TR> ",
00066 "  <TR> ",
00067 "      <UL> ",
00068 "      </UL> ",
00069 "    <TD>&nbsp; </TD> ",
00070 "    <TD id=MostLikelyAlign vAlign=top align=left> ",
00071 "      <H3 id=likelyCauses>Most likely causes:</H3> ",
00072 "      <UL> ",
00073 "        <LI id=causeNotConnected>You are not connected to the Internet.  ",
00074 "        <LI id=causeSiteProblem>The website is encountering problems.  ",
00075 "        <LI id=causeErrorInAddress>There might be a typing error in the address.  ",
00076 "        <LI id=causeOtherError>  ",
00077 "        </LI></UL></TD></TR> ",
00078 "  <TR> ",
00079 "    <TD id=infoBlockAlign vAlign=top align=right>&nbsp; </TD> ",
00080 "    <TD id=moreInformationAlign vAlign=center align=left> ",
00081 "      <H4> ",
00082 "      <TABLE> ",
00083 "        <TBODY> ",
00084 "        <TR> ",
00085 "          <TD vAlign=top><SPAN id=moreInfoContainer></SPAN><ID  ",
00086 "            id=moreInformation>More information</ID> ",
00087 "      </TD></TR></TBODY></TABLE></H4> ",
00088 "      <DIV class=infoBlock id=infoBlockID> ",
00089 "      <P><ID id=errorExpl1>This problem can be caused by a variety of issues,  ",
00090 "      including:</ID>  ",
00091 "      <UL> ",
00092 "        <LI id=errorExpl2>Internet connectivity has been lost.  ",
00093 "        <LI id=errorExpl3>The website is temporarily unavailable.  ",
00094 "        <LI id=errorExpl4>The Domain Name Server (DNS) is not reachable.  ",
00095 "        <LI id=errorExpl5>The Domain Name Server (DNS) does not have a listing  ",
00096 "        for the website's domain.  ",
00097 "      <P></P> ",
00098 "      <P></P></DIV></TD></TR></TBODY></TABLE></BODY></HTML> ",
00099 0
00100 };
00101 
00102 //______________________________________________________________________________
00103 TGHtmlBrowser::TGHtmlBrowser(const char *filename, const TGWindow *p, UInt_t w, UInt_t h) 
00104              : TGMainFrame(p, w, h)
00105 {
00106    // TGHtmlBrowser constructor.
00107 
00108    SetCleanup(kDeepCleanup);
00109    fNbFavorites = 1000;
00110    fMenuBar = new TGMenuBar(this, 35, 50, kHorizontalFrame);
00111 
00112    fMenuFile = new TGPopupMenu(gClient->GetRoot());
00113    fMenuFile->AddEntry(" &Open...            Ctrl+O", M_FILE_OPEN, 0,  
00114                        gClient->GetPicture("bld_open.png"));
00115    fMenuFile->AddEntry(" &Browse...         Ctrl+B", M_FILE_BROWSE);
00116    fMenuFile->AddSeparator();
00117    fMenuFile->AddEntry(" E&xit                   Ctrl+Q", M_FILE_EXIT, 0, 
00118                        gClient->GetPicture("bld_exit.png"));
00119    fMenuFile->Associate(this);
00120 
00121    fMenuFavorites = new TGPopupMenu(gClient->GetRoot());
00122    fMenuFavorites->AddEntry("&Add to Favorites", M_FAVORITES_ADD, 0, 
00123                             gClient->GetPicture("bld_plus.png"));
00124    fMenuFavorites->AddSeparator();
00125    fMenuFavorites->AddEntry("http://root.cern.ch", fNbFavorites++, 0, 
00126                             gClient->GetPicture("htmlfile.gif"));
00127    fMenuFavorites->Associate(this);
00128 
00129    fMenuTools = new TGPopupMenu(gClient->GetRoot());
00130    fMenuTools->AddEntry("&Clear History", M_TOOLS_CLEARHIST, 0,
00131                         gClient->GetPicture("ed_delete.png"));
00132    fMenuTools->Associate(this);
00133 
00134    fMenuHelp = new TGPopupMenu(gClient->GetRoot());
00135    fMenuHelp->AddEntry(" &About...", M_HELP_ABOUT, 0, gClient->GetPicture("about.xpm"));
00136    fMenuHelp->Associate(this);
00137 
00138    fMenuBar->AddPopup("&File", fMenuFile, new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
00139    fMenuBar->AddPopup("&Favorites", fMenuFavorites, new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
00140    fMenuBar->AddPopup("&Tools", fMenuTools, new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0));
00141    fMenuBar->AddPopup("&Help", fMenuHelp, new TGLayoutHints(kLHintsTop | kLHintsRight));
00142 
00143    AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));
00144 
00145    // vertical frame
00146    fVerticalFrame = new TGVerticalFrame(this,727,600,kVerticalFrame);
00147 
00148    fHorizontalFrame = new TGHorizontalFrame(fVerticalFrame,727,600);
00149 
00150    fBack = new TGPictureButton(fHorizontalFrame,gClient->GetPicture("GoBack.gif"));
00151    fBack->SetToolTipText("Go Back");
00152    fHorizontalFrame->AddFrame(fBack, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY,2,2,2,2));
00153    fBack->Connect("Clicked()", "TGHtmlBrowser", this, "Back()");
00154 
00155    fForward = new TGPictureButton(fHorizontalFrame,gClient->GetPicture("GoForward.gif"));
00156    fForward->SetToolTipText("Go Forward");
00157    fHorizontalFrame->AddFrame(fForward, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY,2,2,2,2));
00158    fForward->Connect("Clicked()", "TGHtmlBrowser", this, "Forward()");
00159 
00160    fReload = new TGPictureButton(fHorizontalFrame,gClient->GetPicture("ReloadPage.gif"));
00161    fReload->SetToolTipText("Reload Page");
00162    fHorizontalFrame->AddFrame(fReload, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY,2,2,2,2));
00163    fReload->Connect("Clicked()", "TGHtmlBrowser", this, "Reload()");
00164 
00165    fStop = new TGPictureButton(fHorizontalFrame,gClient->GetPicture("StopLoading.gif"));
00166    fStop->SetToolTipText("Stop Loading");
00167    fHorizontalFrame->AddFrame(fStop, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY,2,2,2,2));
00168    fStop->Connect("Clicked()", "TGHtmlBrowser", this, "Stop()");
00169 
00170    fHome = new TGPictureButton(fHorizontalFrame,gClient->GetPicture("GoHome.gif"));
00171    fHome->SetToolTipText("Go to ROOT HomePage\n  (http://root.cern.ch)");
00172    fHorizontalFrame->AddFrame(fHome, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsCenterY,2,2,2,2));
00173    fHome->Connect("Clicked()", "TGHtmlBrowser", this, "Selected(=\"http://root.cern.ch\")");
00174 
00175    // combo box
00176    fURLBuf   = new TGTextBuffer(256);
00177    fComboBox = new TGComboBox(fHorizontalFrame, "");
00178    fURL      = fComboBox->GetTextEntry();
00179    fURLBuf   = fURL->GetBuffer();
00180    fComboBox->Resize(200, fURL->GetDefaultHeight());
00181    fURL->Connect("ReturnPressed()", "TGHtmlBrowser", this, "URLChanged()");
00182 
00183    fComboBox->AddEntry(filename,1);
00184    fURL->SetText(filename);
00185 
00186    fComboBox->Select(0);
00187    fComboBox->Connect("Selected(char *)", "TGHtmlBrowser", this, "Selected(char *)");
00188 
00189    fHorizontalFrame->AddFrame(fComboBox, new TGLayoutHints(kLHintsLeft | kLHintsCenterY | kLHintsExpandX,2,2,2,2));
00190 
00191    fVerticalFrame->AddFrame(fHorizontalFrame, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX,2,2,2,2));
00192 
00193    // embedded canvas
00194    fHtml = new TGHtml(fVerticalFrame, 10, 10, -1);
00195    fVerticalFrame->AddFrame(fHtml, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX | kLHintsExpandY,2,2,2,2));
00196 
00197    AddFrame(fVerticalFrame, new TGLayoutHints(kLHintsLeft | kLHintsTop | kLHintsExpandX | kLHintsExpandY,2,2,2,2));
00198 
00199    // status bar
00200    fStatusBar = new TGStatusBar(this,100,20);
00201    Int_t partsusBar[] = {75,25};
00202    fStatusBar->SetParts(partsusBar,2);
00203    AddFrame(fStatusBar, new TGLayoutHints(kLHintsBottom | kLHintsExpandX));
00204 
00205    fHtml->Connect("MouseOver(char *)", "TGHtmlBrowser", this, "MouseOver(char *)");
00206    fHtml->Connect("MouseDown(char *)", "TGHtmlBrowser", this, "MouseDown(char *)");
00207 
00208    Selected(filename);
00209 
00210    MapSubwindows();
00211    Resize(GetDefaultSize());
00212    MapWindow();
00213    Resize(w, h);
00214 }
00215 
00216 //______________________________________________________________________________
00217 void TGHtmlBrowser::CloseWindow()
00218 {
00219    // Close TGHtmlBrowser window.
00220 
00221    Cleanup();
00222    delete this;
00223    gApplication->Terminate(0);
00224 }
00225 
00226 //______________________________________________________________________________
00227 static char *ReadRemote(const char *url)
00228 {
00229    // Read (open) remote files.
00230 
00231    static char *buf = 0;
00232    TUrl fUrl(url);
00233 
00234    TString msg = "GET ";
00235    msg += fUrl.GetProtocol();
00236    msg += "://";
00237    msg += fUrl.GetHost();
00238    msg += ":";
00239    msg += fUrl.GetPort();
00240    msg += "/";
00241    msg += fUrl.GetFile();
00242    msg += "\r\n";
00243 
00244    TString uri(url);
00245    if (!uri.BeginsWith("http://"))
00246       return 0;
00247    TSocket s(fUrl.GetHost(), fUrl.GetPort());
00248    if (!s.IsValid())
00249       return 0;
00250    if (s.SendRaw(msg.Data(), msg.Length()) == -1)
00251       return 0;
00252    Int_t size = 1024*1024;
00253    buf = (char *)calloc(size, sizeof(char));
00254    if (s.RecvRaw(buf, size) == -1) {
00255       free(buf);
00256       return 0;
00257    }
00258    return buf;
00259 }
00260 
00261 //______________________________________________________________________________
00262 void TGHtmlBrowser::Selected(const char *uri)
00263 {
00264    // Open (browse) selected URL.
00265 
00266    char *buf = 0;
00267    FILE *f;
00268 
00269    TString surl(gSystem->UnixPathName(uri));
00270    if (!surl.BeginsWith("http://") && !surl.BeginsWith("file://"))
00271       surl.Prepend("file://");
00272    if (surl.EndsWith(".root")) {
00273       gVirtualX->SetCursor(fHtml->GetId(), gVirtualX->CreateCursor(kWatch));
00274       TFile *f = TFile::Open(surl.Data());
00275       if (f && !f->IsZombie()) {
00276          f->Browse(new TBrowser());
00277       }
00278       gVirtualX->SetCursor(fHtml->GetId(), gVirtualX->CreateCursor(kPointer));
00279       return;
00280    }
00281    gVirtualX->SetCursor(fHtml->GetId(), gVirtualX->CreateCursor(kWatch));
00282    TUrl url(surl.Data());
00283    if ((!strcmp(url.GetProtocol(), "http"))) {
00284       buf = ReadRemote(url.GetUrl());
00285       if (buf) {
00286          fHtml->Clear();
00287          fHtml->Layout();
00288          fHtml->SetBaseUri(url.GetUrl());
00289          fHtml->ParseText(buf);
00290          free(buf);
00291          fURL->SetText(surl.Data());
00292          if (!fComboBox->FindEntry(surl.Data()))
00293             fComboBox->AddEntry(surl.Data(), fComboBox->GetNumberOfEntries()+1);
00294       }
00295       else {
00296          fHtml->Clear();
00297          fHtml->Layout();
00298          fHtml->SetBaseUri("");
00299          for (int i=0; HtmlError[i]; i++) {
00300             fHtml->ParseText((char *)HtmlError[i]);
00301          }
00302       }
00303    }
00304    else {
00305       f = fopen(url.GetFile(), "r");
00306       if (f) {
00307          fHtml->Clear();
00308          fHtml->Layout();
00309          fHtml->SetBaseUri("");
00310          buf = (char *)calloc(4096, sizeof(char));
00311          while (fgets(buf, 4096, f)) {
00312             fHtml->ParseText(buf);
00313          }
00314          free(buf);
00315          fclose(f);
00316          fURL->SetText(surl.Data());
00317          if (!fComboBox->FindEntry(surl.Data()))
00318             fComboBox->AddEntry(surl.Data(), fComboBox->GetNumberOfEntries()+1);
00319       }
00320       else {
00321          fHtml->Clear();
00322          fHtml->Layout();
00323          fHtml->SetBaseUri("");
00324          for (int i=0; HtmlError[i]; i++) {
00325             fHtml->ParseText((char *)HtmlError[i]);
00326          }
00327       }
00328    }
00329    gVirtualX->SetCursor(fHtml->GetId(), gVirtualX->CreateCursor(kPointer));
00330    fHtml->Layout();
00331    SetWindowName(Form("%s - RHTML",surl.Data()));
00332 }
00333 
00334 //______________________________________________________________________________
00335 void TGHtmlBrowser::URLChanged()
00336 {
00337    // URL combobox has changed.
00338 
00339    const char *string = fURL->GetText();
00340    if (string) {
00341       Selected(StrDup(gSystem->UnixPathName(string)));
00342    }
00343 }
00344 
00345 //______________________________________________________________________________
00346 void TGHtmlBrowser::Back()
00347 {
00348    // Handle "Back" navigation button.
00349 
00350    Int_t index = 0;
00351    const char *string = fURL->GetText();
00352    TGLBEntry * lbe1 = fComboBox->FindEntry(string); 
00353    if (lbe1)
00354       index = lbe1->EntryId(); 
00355    if (index > 0) {
00356       fComboBox->Select(index - 1, kTRUE);
00357       TGTextLBEntry *entry = (TGTextLBEntry *)fComboBox->GetSelectedEntry();
00358       if (entry) {
00359          const char *string = entry->GetTitle();
00360          if (string)
00361             Selected(string);
00362       }
00363    }
00364 }
00365 
00366 //______________________________________________________________________________
00367 void TGHtmlBrowser::Forward()
00368 {
00369    // Handle "Forward" navigation button.
00370 
00371    Int_t index = 0;
00372    const char *string = fURL->GetText();
00373    TGLBEntry * lbe1 = fComboBox->FindEntry(string);
00374    if (lbe1)
00375       index = lbe1->EntryId();
00376    if (index < fComboBox->GetNumberOfEntries()) {
00377       fComboBox->Select(index + 1, kTRUE);
00378       TGTextLBEntry *entry = (TGTextLBEntry *)fComboBox->GetSelectedEntry();
00379       if (entry) {
00380          const char *string = entry->GetTitle();
00381          if (string)
00382             Selected(string);
00383       }
00384    }
00385 }
00386 
00387 //______________________________________________________________________________
00388 void TGHtmlBrowser::Reload()
00389 {
00390    // Handle "Reload" navigation button.
00391 
00392    const char *string = fURL->GetText();
00393    if (string)
00394       Selected(string);
00395 }
00396 
00397 //______________________________________________________________________________
00398 void TGHtmlBrowser::Stop()
00399 {
00400    // Handle "Reload" navigation button.
00401 
00402 }
00403 
00404 //______________________________________________________________________________
00405 void TGHtmlBrowser::MouseOver(char *url)
00406 {
00407    // Handle "MouseOver" TGHtml signal.
00408 
00409    fStatusBar->SetText(url, 0);
00410 }
00411 
00412 //______________________________________________________________________________
00413 void TGHtmlBrowser::MouseDown(char *url)
00414 {
00415    // Handle "MouseDown" TGHtml signal.
00416 
00417    Selected(url);
00418 }
00419 
00420 //______________________________________________________________________________
00421 Bool_t TGHtmlBrowser::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
00422 {
00423    // Process Events.
00424 
00425    switch (GET_MSG(msg)) {
00426    case kC_COMMAND:
00427       {
00428          switch (GET_SUBMSG(msg)) {
00429 
00430             case kCM_MENU:
00431             case kCM_BUTTON:
00432 
00433                switch(parm1) {
00434 
00435                   case M_FILE_EXIT:
00436                      CloseWindow();
00437                      break;
00438 
00439                   case M_FILE_OPEN:
00440                      {
00441                         static TString dir(".");
00442                         TGFileInfo fi;
00443                         fi.fFileTypes = filetypes;
00444                         fi.fIniDir    = StrDup(dir);
00445                         new TGFileDialog(fClient->GetRoot(), this,
00446                                          kFDOpen, &fi);
00447                         dir = fi.fIniDir;
00448                         if (fi.fFilename) {
00449                            Selected(StrDup(Form("file://%s",
00450                               gSystem->UnixPathName(fi.fFilename))));
00451                         }
00452                      }
00453                      break;
00454 
00455                   case M_FAVORITES_ADD:
00456                      fMenuFavorites->AddEntry(Form("%s",
00457                            fURL->GetText()), fNbFavorites++, 0, 
00458                            gClient->GetPicture("htmlfile.gif"));
00459                      break;
00460 
00461                   case M_TOOLS_CLEARHIST:
00462                      fComboBox->RemoveEntries(1,fComboBox->GetNumberOfEntries());
00463                      break;
00464 
00465                   case M_FILE_BROWSE:
00466                      new TBrowser();
00467                      break;
00468                   
00469                   case M_HELP_ABOUT:
00470                      {
00471 #ifdef R__UNIX
00472                         TString rootx;
00473 # ifdef ROOTBINDIR
00474                         rootx = ROOTBINDIR;
00475 # else
00476                         rootx = gSystem->Getenv("ROOTSYS");
00477                         if (!rootx.IsNull()) rootx += "/bin";
00478 # endif
00479                         rootx += "/root -a &";
00480                         gSystem->Exec(rootx);
00481 #else
00482 #ifdef WIN32
00483                         new TWin32SplashThread(kTRUE);
00484 #else
00485                         char str[32];
00486                         sprintf(str, "About ROOT %s...", gROOT->GetVersion());
00487                         TRootHelpDialog *hd = new TRootHelpDialog(this, str,
00488                                                                   600, 400);
00489                         hd->SetText(gHelpAbout);
00490                         hd->Popup();
00491 #endif
00492 #endif
00493                      }
00494                      break;
00495 
00496                   default:
00497                      {
00498                         if (parm1 < 1000) break;
00499                         TGMenuEntry *entry = fMenuFavorites->GetEntry(parm1);
00500                         if (!entry) break;
00501                         const char *shortcut = entry->GetName();
00502                         if (shortcut)
00503                            Selected(shortcut);
00504                      }
00505                      break;
00506                }
00507                break;
00508          }
00509          break;
00510       }
00511    }
00512    return kTRUE;
00513 }
00514 
00515 //______________________________________________________________________________
00516 int main(int argc, char **argv)
00517 {
00518    // Main application.
00519    
00520    TApplication theApp("App", &argc, argv);
00521    new TGHtmlBrowser("http://root.cern.ch");
00522    theApp.Run();
00523    return 0;
00524 }

Generated on Tue Jul 5 15:15:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1