#include "../include/sipmscan.h"
 
#include "../include/workstation.h"
 
 
 
#include <stdio.h>
 
#include <stdlib.h>
 
 
 
// Title labels for each of the frame
 
void TGAppMainFrame::TGTitleLabel(TGWindow *parent, TGHorizontalFrame *fTitle, const char *title, Pixel_t foreColor, Pixel_t backColor, const char *font)
 
{
 
   TGLabel *lab = new TGLabel(fTitle, title);
 
   lab->ChangeBackground(backColor);
 
   lab->SetTextColor(foreColor);
 
   lab->SetTextFont(font);
 
   fTitle->AddFrame(lab, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY) );
 
   fTitle->ChangeBackground(backColor);
 
}
 
 
 
// Splitter to a set number of frames
 
bool TGAppMainFrame::TGSplitter(TGWindow *parent, const char *majorSplit, int *horSplits, int *vertSplits, const char *frameTitles[512], TGCompositeFrame **frames, int *frameWidth, int *frameHeight)
 
{
 
   // Number of Frames that are not the ones we supply
 
   int nrofmed = 0;
 
   // Number of all splits in the minor direction
 
   int minorSplits = 0;
 
   int k = 0;
 
 
 
   // Title frame height
 
   int titHeight = 25;
 
 
 
   if(strcmp("horizontal",majorSplit) == 0)
 
   {
 
      fLayout[idtotal] = new TGCompositeFrame(parent, 300, 300, kHorizontalFrame);
 
      for(int i = 0; i <= horSplits[0]; i++)
 
      {
 
         minorSplits += vertSplits[i];
 
         if(vertSplits[i] > 0)
 
            nrofmed++;
 
      }
 
 
 
      if(DBGSIG > 1) printf("TGSplitter(): Number of intermediate frames = %d, all minor splits = %d\n", nrofmed, minorSplits);
 
 
 
 
 
      TGCompositeFrame *fInter[nrofmed];
 
      TGVSplitter *vsplit[horSplits[0]];
 
      TGHSplitter *hsplit[minorSplits];
 
 
 
      nrofmed = 0;
 
 
 
      for(int i = 0; i <= horSplits[0]; i++)
 
      {
 
         if(DBGSIG > 1) printf("TGSplitter(): i = %d\n",i);
 
         for(int j = 0; j <= vertSplits[i]; j++)
 
         {
 
            if(DBGSIG > 1) printf("TGSplitter(): j = %d, vertSplits = %d\n",j, vertSplits[i]);
 
            if( vertSplits[i] > 0 )
 
            {
 
               if(j == 0)
 
               {
 
                  if(DBGSIG > 1) printf("TGSplitter(): vertSplits - j = %d (nrofmed = %d)\n", (vertSplits[i]-j), nrofmed);
 
                  if(i > 0)
 
                  {
 
                     fInter[nrofmed] = new TGCompositeFrame(fLayout[idtotal], frameWidth[k], 200, kVerticalFrame);
 
                     fLayout[idtotal]->AddFrame(fInter[nrofmed], new TGLayoutHints(kLHintsExpandY | kLHintsExpandX));
 
                  }
 
                  else
 
                  {
 
                     fInter[nrofmed] = new TGCompositeFrame(fLayout[idtotal], frameWidth[k], 200, kVerticalFrame | kFixedWidth);
 
                     fLayout[idtotal]->AddFrame(fInter[nrofmed], new TGLayoutHints(kLHintsExpandY));
 
                  }
 
               }
 
 
 
               if( (vertSplits[i]-j) > 0)
 
               {
 
                  if(DBGSIG > 1) printf("TGSplitter(): Step 0a (k = %d)\n", k);
 
 
 
                  frames[k] = new TGCompositeFrame(fInter[nrofmed], frameWidth[k], frameHeight[k], kVerticalFrame | kFixedHeight | kSunkenFrame);
 
                  fTitle = new TGHorizontalFrame(frames[k], 100, titHeight, kFixedHeight | kSunkenFrame);
 
                  TGTitleLabel(frames[k], fTitle, frameTitles[k], (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
 
                  frames[k]->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop) );
 
                  fInter[nrofmed]->AddFrame(frames[k], new TGLayoutHints(kLHintsExpandX));
 
 
 
                  if(DBGSIG > 1) printf("TGSplitter(): Step 1a\n");
 
 
 
                  hsplit[nrofmed] = new TGHSplitter(fInter[nrofmed]);
 
                  hsplit[nrofmed]->SetFrame(frames[k], kTRUE);
 
                  fInter[nrofmed]->AddFrame(hsplit[nrofmed], new TGLayoutHints(kLHintsExpandX));
 
 
 
                  if(DBGSIG > 1) printf("TGSplitter(): Step 2a\n");
 
 
 
                  k++;
 
               }
 
               else
 
               {
 
                  if(DBGSIG > 1) printf("TGSplitter(): Step 0b (k = %d)\n", k);
 
 
 
                  frames[k] = new TGCompositeFrame(fInter[nrofmed], frameWidth[k], frameHeight[k], kVerticalFrame | kSunkenFrame);
 
                  fTitle = new TGHorizontalFrame(frames[k], 100, titHeight, kFixedHeight | kSunkenFrame);
 
                  TGTitleLabel(frames[k], fTitle, frameTitles[k], (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
 
                  frames[k]->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop) );
 
                  fInter[nrofmed]->AddFrame(frames[k], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
 
 
                  if(DBGSIG > 1) printf("TGSplitter(): Step 1b\n");
 
                  k++;
 
               }
 
            }
 
            else
 
            {
 
               if(DBGSIG > 1) printf("TGSplitter(): Step 0c (k = %d)\n", k);
 
 
 
               frames[k] = new TGCompositeFrame(fLayout[idtotal], frameWidth[k], frameHeight[k], kVerticalFrame | kSunkenFrame);
 
               fTitle = new TGHorizontalFrame(frames[k], 100, titHeight, kFixedHeight | kSunkenFrame);
 
               TGTitleLabel(frames[k], fTitle, frameTitles[k], (Pixel_t)FORECOLOR, (Pixel_t)BACKCOLOR, FONT);
 
               frames[k]->AddFrame(fTitle, new TGLayoutHints(kLHintsExpandX | kLHintsTop) );
 
               fLayout[idtotal]->AddFrame(frames[k], new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
 
 
 
               if(DBGSIG > 1) printf("TGSplitter(): Step 1c\n");
 
               k++;
 
            }
 
         }
 
 
 
         if(DBGSIG > 1) printf("TGSplitter(): i = %d, horSplits = %d\n", i, horSplits[0]);
 
 
 
         if(i != horSplits[0])
 
         {
 
            vsplit[i] = new TGVSplitter(fLayout[idtotal]);
 
            if( vertSplits[i] > 0 )
 
               vsplit[i]->SetFrame(fInter[nrofmed], kTRUE);
 
            else
 
               vsplit[i]->SetFrame(frames[k], kTRUE);
 
            fLayout[idtotal]->AddFrame(vsplit[nrofmed], new TGLayoutHints(kLHintsExpandY));
 
         }
 
 
 
         nrofmed++;
 
      }
 
 
 
      if(DBGSIG > 1) printf("TGSplitter(): Finished horizontal layout\n");
 
   }
 
   else if(strcmp("vertical",majorSplit) == 0)
 
   {
 
/*      for(int i = 0; i <= vertSplits[0]; i++)
 
      {
 
         minorSplits += horSplits[i];
 
         if(horSplits[i] > 0)
 
            nrofmed++;
 
      }
 
 
 
      printf("Number of intermediate frames = %d\n", nrofmed);
 
 
 
      TGCompositeFrame *fInter[nrofmed];*/
 
   }
 
   else
 
      return false;
 
 
 
   return true;
 
}
 
 
 
// Function for setting up the layout
 
void TGAppMainFrame::LayoutRead(int nrframes, int *w, int *h)
 
{
 
   int WM, HM, k = 0, start = 0;
 
 
 
   std::ifstream ilayout;
 
   
 
   char *cTemp, *cTemp2, readTemp[1024];
 
   cTemp = new char[512];
 
   cTemp2 = new char[512];
 
   sprintf(cTemp, "%s/layout/selected_layout.txt", rootdir);
 
   ilayout.open(cTemp, std::ifstream::in);
 
   if(ilayout.is_open())
 
   {
 
      ilayout >> cTemp2;
 
   }
 
   ilayout.close();
 
   printf("Loaded layout file is: %s\n", cTemp2);
 
 
 
   sprintf(cTemp, "%s/layout/%s", rootdir, cTemp2);
 
   ilayout.open(cTemp, std::ifstream::in);
 
   if(ilayout.is_open())
 
   {
 
      while(1)
 
      {
 
         if(ilayout.peek() == '#')
 
         {
 
            ilayout.getline(readTemp, 1024, '\n');
 
            if(DBGSIG > 1) printf("LayoutRead(): readTemp = %s\n", readTemp);
 
         }
 
         else if(ilayout.peek() == '\n')
 
            ilayout.ignore(1, '\n');
 
         else
 
         {  
 
            if(start == 0)
 
            {
 
               ilayout >> WM >> HM >> readTemp;
 
               ilayout.ignore(1, '\n');
 
               start++;
 
               if(DBGSIG > 1) printf("LayoutRead(): W = %d, H = %d, Name = %s\n", WM, HM, readTemp);
 
            }
 
            else
 
            {
 
               ilayout >> w[k] >> h[k] >> readTemp;
 
               ilayout.ignore(1, '\n');
 
               if(DBGSIG > 1) printf("LayoutRead(): w[%d] = %d, h[%d] = %d, Name = %s\n", k, w[k], k, h[k], readTemp);
 
               k++;
 
 
 
               if(k == nrframes) break;
 
            }
 
         }
 
      }
 
   }
 
 
 
   ilayout.close();
 
   delete[] cTemp;
 
   delete[] cTemp2;
 
}
 
 
 
// Function for saving the current layout
 
void TGAppMainFrame::LayoutSave()
 
{
 
   TGFileInfo file_info;
 
   const char *filetypes[] = {"Layout","*.layout",0,0};
 
   char *cTemp;
 
   file_info.fFileTypes = filetypes;
 
   cTemp = new char[1024];
 
   sprintf(cTemp, "%s/layout", rootdir);
 
   file_info.fIniDir = StrDup(cTemp);
 
   new TGFileDialog(gClient->GetDefaultRoot(), fMain, kFDSave, &file_info);
 
   delete[] cTemp;
 
 
 
   if(file_info.fFilename != NULL)
 
   {
 
      if(DBGSIG) printf("LayoutSave(): The layout save name: %s\n", file_info.fFilename);
 
    
 
      std::ofstream olayout;
 
    
 
      olayout.open(file_info.fFilename, std::ofstream::out);
 
      if(olayout.is_open())
 
      {
 
         olayout << "# Whole window width and height" << std::endl;
 
         olayout << fMain->GetWidth() << "\t" << fMain->GetHeight() << "\tmain" << std::endl << std::endl;
 
         olayout << "# Measurement subwindows width and height" << std::endl;
 
         for(int i = 0; i < measwin; i++)
 
            olayout << measLayout[i]->GetWidth() << "\t" << measLayout[i]->GetHeight() << "\tmeasurementwindow" << i << std::endl;
 
         olayout << std::endl;
 
         olayout << "# Analysis subwindows width and height" << std::endl;
 
         for(int i = 0; i < analysiswin; i++)
 
            olayout << analysisLayout[i]->GetWidth() << "\t" << analysisLayout[i]->GetHeight() << "\tanalysiswindow" << i << std::endl;
 
      }
 
      else
 
         printf("Error! Save file can not be opened (please do not use default.layout since it is write protected).\n");
 
      olayout.close();
 
   }
 
}
 
 
 
// Function for setting a user created layout
 
void TGAppMainFrame::LayoutSet()
 
{
 
   TGFileInfo file_info;
 
   int ret = 0;
 
   const char *filetypes[] = {"Layout","*.layout",0,0};
 
   char *cTemp, *layoutdir;
 
   file_info.fFileTypes = filetypes;
 
   layoutdir = new char[1024];
 
   sprintf(layoutdir, "%s/layout", rootdir);
 
   file_info.fIniDir = StrDup(layoutdir);
 
   new TGFileDialog(gClient->GetDefaultRoot(), fMain, kFDOpen, &file_info);
 
 
 
   if(file_info.fFilename != NULL)
 
   {
 
      if(DBGSIG) printf("LayoutSet(): The layout save name: %s\n", file_info.fFilename);
 
      cTemp = new char[512];
 
      remove_before_last(file_info.fFilename, '/', cTemp);
 
      if(DBGSIG) printf("LayoutSet(): New selected layout: %s\n", cTemp);
 
 
 
      FILE *fp;
 
      sprintf(layoutdir, "%s/layout/selected_layout.txt", rootdir);
 
      fp = fopen(layoutdir, "w");
 
      fprintf(fp, "%s", cTemp);
 
      fclose(fp);
 
 
 
      sprintf(layoutdir, "Please restart the program to enable the selected layout (%s) for future use.", cTemp);
 
      new TGMsgBox(gClient->GetRoot(), fMain, "Setting new layout", layoutdir, kMBIconAsterisk, kMBOk, &ret);
 
      delete[] cTemp;
 
   }
 
   delete[] layoutdir;
 
}
 
 
 
// Layout function for the main window (width and height)
 
void layoutMainWindow(int *w, int *h)
 
{
 
   std::ifstream ilayout;
 
   
 
   char *cTemp, *cTemp2, readTemp[1024];
 
   cTemp = new char[512];
 
   cTemp2 = new char[512];
 
   sprintf(cTemp, "%s/layout/selected_layout.txt", rootdir);
 
   ilayout.open(cTemp, std::ifstream::in);
 
   if(ilayout.is_open())
 
   {
 
      ilayout >> cTemp2;
 
   }
 
   ilayout.close();
 
   if(DBGSIG) printf("layoutMainWindow(): Loaded layout file is: %s\n", cTemp2);
 
 
 
   sprintf(cTemp, "%s/layout/%s", rootdir, cTemp2);
 
   ilayout.open(cTemp, std::ifstream::in);
 
   if(ilayout.is_open())
 
   {
 
      while(1)
 
      {
 
         if(ilayout.peek() == '#')
 
            ilayout.getline(readTemp, 1024, '\n');
 
         else if(ilayout.peek() == '\n')
 
            ilayout.ignore(1, '\n');
 
         else
 
         {  
 
            ilayout >> *w >> *h;
 
            if(DBGSIG > 1) printf("layoutMainWindow(): W = %d, H = %d\n", *w, *h);
 
            break;
 
         }
 
      }
 
   }
 
 
 
   ilayout.close();
 
   delete[] cTemp;
 
   delete[] cTemp2;
 
}