home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pstoedit.zip / source.zip / pstoedit.2.50 / src / bc5gui / winp2eap.cpp < prev    next >
C/C++ Source or Header  |  1996-12-18  |  15KB  |  537 lines

  1. /*
  2.    winp2eap.cpp : This file is part of pstoedit
  3.    Source file for implementation of TWinp2eApp (TApplication).
  4.  
  5.    Copyright (C) 1996 Jens Weber, wr@lzh1.lzh.de
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22.  
  23.  
  24.  
  25. #include <owl/pch.h>
  26.  
  27. #include <owl/buttonga.h>
  28. #include <owl/statusba.h>
  29. #include <classlib/cmdline.h>
  30. #include <winsys/registry.h>
  31. #include <stdio.h>
  32.  
  33. #include <io.h>
  34. #include <dos.h>   //JW fuer Fileatrribute des Fehlerfiles
  35. #include <fcntl.h> // JW dito
  36.  
  37.  
  38.  
  39. // AppExpert-includes
  40. #include "winp2eap.h"
  41. #include "winp2edc.h"                        // Definition of client class.
  42. #include "winp2ead.h"                        // Definition of about dialog.
  43.  
  44. #ifdef PSTOEDIT_INPROC
  45. // call pstoedit as subprogram
  46. #include "pstoedit.h"
  47. #endif
  48. //{{TWinp2eApp Implementation}}
  49.  
  50.  
  51. //
  52. // Build a response table for all messages/commands handled
  53. // by the application.
  54. //
  55. DEFINE_RESPONSE_TABLE1(TWinp2eApp, TApplication)
  56. //{{TWinp2eAppRSP_TBL_BEGIN}}
  57.   EV_COMMAND(CM_CONVERT1, CmConvert),
  58.   EV_COMMAND(CM_PARAMETER, CmParameter),
  59.   EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  60. //{{TWinp2eAppRSP_TBL_END}}
  61. END_RESPONSE_TABLE;
  62.  
  63.  
  64. //--------------------------------------------------------
  65. // TWinp2eApp
  66. //
  67. TWinp2eApp::TWinp2eApp() : TApplication("Winp2e")
  68. {
  69.  
  70.   // INSERT>> Your constructor code here.
  71. }
  72.  
  73.  
  74. TWinp2eApp::~TWinp2eApp()
  75. {
  76.   // INSERT>> Your destructor code here.
  77. }
  78.  
  79.  
  80. void TWinp2eApp::SetupSpeedBar(TDecoratedFrame* frame)
  81. {
  82.   ApxHarbor = new THarbor(*frame);
  83.  
  84.   // Create default toolbar New and associate toolbar buttons with commands.
  85.   //
  86.   TDockableControlBar* cb = new TDockableControlBar(frame);
  87.   cb->Insert(*new TButtonGadget(CM_CONVERT1, CM_CONVERT1));
  88.   cb->Insert(*new TButtonGadget(CM_PARAMETER, CM_PARAMETER));
  89.  
  90.  
  91.   // Add caption and fly-over help hints.
  92.   //
  93.   cb->SetCaption("Toolbar");
  94.   cb->SetHintMode(TGadgetWindow::EnterHints);
  95.  
  96.   ApxHarbor->Insert(*cb, alTop);
  97. }
  98.  
  99.  
  100. //--------------------------------------------------------
  101. // TWinp2eApp
  102. // ~~~~~
  103. // Application intialization.
  104. //
  105. void TWinp2eApp::InitMainWindow()
  106. {
  107.   if (nCmdShow != SW_HIDE)
  108.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  109.  
  110.   TSDIDecFrame* frame = new TSDIDecFrame(0, GetName(), 0, true);
  111.   frame->SetFlag(wfShrinkToClient);
  112.  
  113.   // Assign ICON w/ this application.
  114.   //
  115.   frame->SetIcon(this, IDI_SDIAPPLICATION);
  116.   frame->SetIconSm(this, IDI_SDIAPPLICATION);
  117.  
  118.   // Menu associated with window and accelerator table associated with table.
  119.   //
  120.   frame->AssignMenu(IDM_SDI);
  121.  
  122.   // Associate with the accelerator table.
  123.   //
  124.   frame->Attr.AccelTable = IDM_SDI;
  125.  
  126.   SetupSpeedBar(frame);
  127.  
  128.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed,
  129.                                   TStatusBar::CapsLock        |
  130.                                   TStatusBar::NumLock         |
  131.                                   TStatusBar::ScrollLock);
  132.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  133.  
  134.   SetMainWindow(frame);
  135.  
  136.   frame->SetMenuDescr(TMenuDescr(IDM_SDI));
  137. }
  138.  
  139.  
  140. //--------------------------------------------------------
  141. // TWinp2eApp
  142. // ~~~~~
  143. // Application instance initialization.
  144. //
  145. void TWinp2eApp::InitInstance()
  146. {
  147.   TApplication::InitInstance();
  148.  
  149.   ProcessCmdLine(lpCmdLine);
  150.     // Defaultwerte vorgeben
  151.  
  152.     Parameter.magnification = 1.0;
  153.     Parameter.flatness = 1.0;
  154.     Parameter.merging = FALSE;
  155.     Parameter.drawtext = FALSE;
  156.     Parameter.display = FALSE;
  157.     Parameter.verbose = FALSE;
  158.     Parameter.enhanced = TRUE;
  159.     strcpy(Parameter.format, "wmf");
  160.     Parameter.backendonly = FALSE;
  161.     Parameter.nomaplatin = FALSE;
  162.     strcpy(Parameter.resolution_x,"300");
  163.     strcpy(Parameter.resolution_y,"300");
  164.     Parameter.noquit = FALSE;
  165.  
  166. }
  167.  
  168.  
  169. //--------------------------------------------------------
  170. // TWinp2eApp
  171. // ~~~~~
  172. // Process command line parameters.
  173. //
  174. void TWinp2eApp::ProcessCmdLine(char * CmdLine)
  175. {
  176.   TCmdLine cmd(CmdLine);
  177.  
  178.   while (cmd.Kind != TCmdLine::Done) {
  179.     if (cmd.Kind == TCmdLine::Option) {
  180.       if (strnicmp(cmd.Token, "unregister", cmd.TokenLen) == 0) {
  181.         UnRegisterInfo();
  182.         return;
  183.       }
  184.     }
  185.     cmd.NextToken();
  186.   }
  187.  
  188.   RegisterInfo();
  189. }
  190.  
  191.  
  192. //--------------------------------------------------------
  193. // TWinp2eApp
  194. // ~~~~~
  195. // Register application info.
  196. //
  197. void TWinp2eApp::RegisterInfo()
  198. {
  199.   TAPointer<char> buffer = new char[_MAX_PATH];
  200.  
  201.   GetModuleFileName(buffer, _MAX_PATH);
  202.  
  203.   TRegKey(TRegKey::ClassesRoot, "Winp2e.Application\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  204.   strcat(buffer, ",1");
  205. }
  206.  
  207.  
  208. //--------------------------------------------------------
  209. // TWinp2eApp
  210. // ~~~~~
  211. // Unregister application info.
  212. //
  213. void TWinp2eApp::UnRegisterInfo()
  214. {
  215.   TAPointer<char> buffer = new char[_MAX_PATH];
  216.  
  217.   GetModuleFileName(buffer, _MAX_PATH);
  218.  
  219.   TRegKey(TRegKey::ClassesRoot, "Winp2e.Application").DeleteKey("DefaultIcon");
  220.   TRegKey(TRegKey::ClassesRoot, "Winp2e.Document.1").DeleteKey("DefaultIcon");
  221.  
  222.   TRegKey::ClassesRoot.DeleteKey("Winp2e.Application");
  223. }
  224.  
  225.  
  226.  
  227. //{{TSDIDecFrame Implementation}}
  228.  
  229.  
  230. TSDIDecFrame::TSDIDecFrame(TWindow* parent, const char far* title, TWindow* clientWnd, bool trackMenuSelection, TModule* module)
  231. :
  232.   TDecoratedFrame(parent, title, !clientWnd ? new TWinp2eDlgClient(0) : clientWnd, trackMenuSelection, module)
  233. {
  234.   // INSERT>> Your constructor code here.
  235.  
  236. }
  237.  
  238.  
  239. TSDIDecFrame::~TSDIDecFrame()
  240. {
  241.   // INSERT>> Your destructor code here.
  242.  
  243. }
  244.  
  245.  
  246. void TSDIDecFrame::SetupWindow()
  247. {
  248.   TDecoratedFrame::SetupWindow();
  249.   TRect  r;
  250.   GetWindowRect(r);
  251.  
  252. //  r.bottom += 60;
  253.   r.left = 10;
  254.   r.top = 10;
  255.   r.right = 410;
  256.   r.bottom = 210;
  257.  
  258.   SetWindowPos(0, r, SWP_NOZORDER | SWP_NOMOVE);
  259.  
  260.   // INSERT>> Your code here.
  261.  
  262. }
  263.  
  264.  
  265. //--------------------------------------------------------
  266. // TWinp2eApp
  267. // ~~~~~~~~~~~
  268. // Menu Help About Winp2e command
  269. //
  270. void TWinp2eApp::CmHelpAbout()
  271. {
  272.   // Show the modal dialog.
  273.   //
  274.   TWinp2eAboutDlg(MainWindow).Execute();
  275. }
  276.  
  277. void TWinp2eApp::CmConvert()
  278. {
  279.  
  280.   char tmpstring[200]; // wogl
  281.   char MyCmdLine[1000]; // wogl
  282.   char OutFileName[200]; // wogl
  283.   MyCmdLine[0]=NULL;
  284.  
  285.   InFileData.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  286.   InFileData.SetFilter("PostScript  Files (*.ps *.eps)|*.ps;*.eps|All Files (*.*)|*.*|");
  287.   InFileData.DefExt = "ps";
  288.   *InFileData.FileName = 0;
  289.  
  290.    if (TFileOpenDialog(GetMainWindow(), InFileData).Execute() == IDOK) {
  291.       int i = strlen(InFileData.FileName)-1;
  292.         for( ;i>=0;i--)
  293.             if(InFileData.FileName[i]=='.') break;
  294.         if(i > 0) {
  295.             strncpy(OutFileName,InFileData.FileName,i);
  296.             OutFileName[i]=NULL;
  297.             }
  298.         else strcpy(OutFileName,InFileData.FileName);
  299.  
  300.         if (strcmp(Parameter.format,"wmf") == 0) {
  301.         if(Parameter.enhanced==TRUE) strcat(OutFileName,".emf");
  302.         else strcat(OutFileName,".wmf");
  303.         }
  304.         else if (strcmp(Parameter.format,"pdf") == 0) strcat(OutFileName,".pdf");
  305.         else if (strcmp(Parameter.format,"cgm") == 0) strcat(OutFileName,".cgm");
  306.         else if (strcmp(Parameter.format,"cgmt") == 0) strcat(OutFileName,".cgm");
  307.         else if (strcmp(Parameter.format,"mif") == 0) strcat(OutFileName,".mif");
  308.         else if (strcmp(Parameter.format,"ps") == 0) strcat(OutFileName,".pss");
  309.         else if (strcmp(Parameter.format,"dxf") == 0) strcat(OutFileName,".dxf");
  310.         else if (strcmp(Parameter.format,"java") == 0) strcat(OutFileName,".java");
  311.         else if (strcmp(Parameter.format,"debug") == 0) strcat(OutFileName,".dbg");
  312.         else if (strcmp(Parameter.format,"dump") == 0) strcat(OutFileName,".dbg");
  313.         else  strcat(OutFileName,".xxx");
  314.  
  315.         // Zusammenbauen der Commandline
  316.  
  317.       if(Parameter.magnification!=1.0) {
  318.           sprintf(tmpstring,"-s %f ",Parameter.magnification);
  319.           strcat(MyCmdLine,tmpstring); }
  320.  
  321.         if(Parameter.flatness!=1.0) {
  322.           sprintf(tmpstring,"-flat %f ",Parameter.flatness);
  323.           strcat(MyCmdLine,tmpstring); }
  324.  
  325.       if(Parameter.merging==TRUE) strcat(MyCmdLine,"-merge ");
  326.         if(Parameter.display==TRUE) strcat(MyCmdLine,"-dis ");
  327.         if(Parameter.drawtext==TRUE) strcat(MyCmdLine,"-dt ");
  328.         if(Parameter.backendonly==TRUE) strcat(MyCmdLine,"-bo ");
  329.         if(Parameter.noquit==TRUE) strcat(MyCmdLine,"-nq ");
  330.         if(Parameter.nomaplatin==TRUE) strcat(MyCmdLine,"-nomaptoisolatin1 ");
  331.  
  332.       sprintf(tmpstring,"-r%sx%s ",Parameter.resolution_x,Parameter.resolution_y);
  333.         strcat(MyCmdLine,tmpstring);
  334.  
  335.         strcat(MyCmdLine," -f ");
  336.         strcat(MyCmdLine,Parameter.format);
  337.         if(Parameter.verbose==TRUE || Parameter.enhanced==TRUE) { // Options
  338.           strcat(MyCmdLine,":");
  339.           if(Parameter.enhanced==TRUE) strcat(MyCmdLine,"e"); // emf-File
  340.           if(Parameter.verbose==TRUE) strcat(MyCmdLine,"v"); // verbose
  341.       }
  342.         strcat(MyCmdLine," ");
  343.  
  344.         strcat(MyCmdLine,InFileData.FileName);
  345.         strcat(MyCmdLine," ");
  346.         strcat(MyCmdLine,OutFileName);
  347.  
  348.       ::SetCursor(::LoadCursor(0,IDC_WAIT)); // Cursor -> Sanduhr
  349.  
  350.       const int    status=WMFMain(MyCmdLine);
  351.  
  352.       ::SetCursor(::LoadCursor(0,IDC_ARROW)); // Cursor -> Pfeil
  353.  
  354.     }
  355.  
  356. }
  357.  
  358. void TWinp2eApp::CmParameter()
  359. {
  360.   // Show the modal dialog.
  361.   //
  362.   TParaDlg(GetMainWindow(),IDD_PARAMETER, &Parameter, AnotherTransferStruct).Execute();
  363.  
  364. }
  365.  
  366. static int callgs(const char * commandline)
  367. {
  368.         int gsresult = 0;
  369.        STARTUPINFO MyStartupInfo; // Prozessdaten
  370.        memset(&MyStartupInfo,'\0',sizeof(MyStartupInfo));
  371.        MyStartupInfo.cb = sizeof(STARTUPINFO);
  372.        PROCESS_INFORMATION MyProcessInformation;
  373.        // wird von CreateProcess gefuellt
  374.        DWORD gs_status = 0;
  375.        MyStartupInfo.wShowWindow=SW_SHOWMINNOACTIVE;
  376.  
  377.        MessageBox(NULL,commandline,"Kommandozeile",MB_OK); // wogl NULL inserted
  378.  
  379.         int status=(int)CreateProcess(
  380.               NULL, // Application Name -> wird in der naechsten Zeile mitgegeben
  381.               (LPSTR)commandline,
  382.               NULL, // Prozessattribute (NULL == Default)
  383.               NULL, // Thread-Atrribute (Default)
  384.               FALSE, // InheritHandles
  385.               CREATE_NEW_PROCESS_GROUP, // CreationFlags
  386.               NULL, // Environment (NULL: same as calling-process)
  387.               NULL, // Current Directory (NULL: same as calling process)
  388.               (LPSTARTUPINFO)(&MyStartupInfo), // Windows-state at Startup
  389.                    // das Fenster der aufzurufenden Funktion wird verkleinert + nicht aktiv
  390.                   // dargestellt
  391.              (LPPROCESS_INFORMATION)(&MyProcessInformation)
  392.             );
  393.  
  394.        if(status==TRUE) gsresult=0;
  395.        else gsresult=-1; // Failure
  396.  
  397.        while(0==0) {
  398.              status=GetExitCodeProcess(MyProcessInformation.hProcess, &gs_status);
  399.             if(status==FALSE) {  // Proze▀-Status konnte nicht ermittelt werden
  400.               gsresult=-1;
  401.               break;
  402.             }
  403.  
  404.                 if(gs_status==STILL_ACTIVE) { // gswin arbeitet noch...
  405.                         Sleep(500); // Wartezeit in msec
  406.                 }
  407.             else break; // Prozess beendet
  408.          }
  409.         if (gsresult != 0) {
  410.             char tmpstring[100];
  411.             sprintf(tmpstring,"Postscript interpreter failure: %d", gsresult);
  412.             MessageBox(NULL,tmpstring,"ERROR",MB_OK);
  413.         }
  414.  
  415.         return gsresult;
  416. }
  417.  
  418. #if 0
  419. static const char * whichPI(ostream & errstream)
  420. {
  421.     return "gswin32.exe"  ;
  422. }
  423. #endif
  424.  
  425. static const char * whichPI(ostream & errstream)
  426. {
  427. // determines which PostScript interpreter to use
  428. #ifdef DEFAULTGS
  429. #define str(x) #x
  430. #define xstr(x) str(x)
  431.     static const char * const defaultgs = xstr(DEFAULTGS);
  432. #else
  433.     static const char * const defaultgs = "gswin32.exe";
  434. #endif
  435.     const char * gstocall = getenv("GS");
  436.     if ( gstocall  == 0 ) {
  437.         // env var GS not set, so try default
  438.         if ( strlen(defaultgs) > 0 ) {
  439.             gstocall = defaultgs;
  440.         } else {
  441.             errstream << "Fatal: don't know which interpreter to call. "
  442.                       << "Either setenv GS or compile again with -DDEFAULTGS=..." << endl;
  443.             exit(1);
  444.         }
  445.     }
  446.     return gstocall;
  447. }
  448.  
  449. int TWinp2eApp::WMFMain(char *TheCmdLine)
  450.  
  451. {
  452. #ifdef PSTOEDIT_INPROC
  453.     char * argv[100];
  454.     int argc=0;
  455.     argv[argc] = "pstoedit"; argc++;
  456.  
  457.  
  458.    const char * errfilename = "makeedit.err";
  459.    ofstream errorfile;
  460. #if 0
  461.    unsigned attrib; // Fileattribute
  462.  
  463.    if (!_dos_getfileattr(errfilename,&attrib))  // File existiert schon
  464.       { // read only ? aendern !
  465.       if (attrib & _A_RDONLY) {
  466.        attrib &= ~_A_RDONLY;
  467.           if (_dos_setfileattr(errfilename,attrib) != 0) {
  468.           MessageBox(NULL,errfilename,"Could not delete read-only attribute:",MB_OK|MB_ICONEXCLAMATION);
  469.           return -1;
  470.             }
  471.        }
  472.     }
  473. #endif
  474.  
  475.     errorfile.open(errfilename,O_RDWR|O_CREAT|O_TEXT);
  476.    if(errorfile.fail())    {
  477.       MessageBox(NULL,errfilename,"Could not open for error and message  output:",MB_OK|MB_ICONEXCLAMATION);
  478.       return -1; }
  479.  
  480.     {
  481.     const unsigned int newmask = 0; // don't take away any permission
  482.     unsigned int oldmask = umask( newmask );
  483.        // errorfile <<  "Oldmask = 0x%.4x\n" <<  oldmask  << endl;
  484.        errorfile <<  "Oldmask = " <<  oldmask  << endl;
  485.     }
  486.  
  487.    errorfile << "Commandline " << TheCmdLine << endl;
  488.     // first quick hack,  turn TheCmdLine into a vector
  489.     char * cp = TheCmdLine;
  490.     argv[argc] = cp; argc++;
  491.     while (*cp) {
  492.         if (*cp == ' ') {
  493.             *cp = '\0'; cp++;
  494.             while ((*cp) && (*cp == ' ')) cp++;
  495.             if (*cp) {
  496.                 argv[argc] = cp; argc++;
  497.             } else {
  498.                 break; // while
  499.             }
  500.         }
  501.         cp++;
  502.     }
  503.  
  504.     for (int i= 0; i < argc ; i++) {
  505.         errorfile << "argv["<< i << "]: " << argv[i] << endl;
  506.     }
  507.     const int result = pstoedit(argc,argv,errorfile,callgs,whichPI);
  508. #else
  509. //
  510. // call pstoedit as separate process. This is a sort of workaround
  511. // for the still open problem of files beeing opened read-only 
  512. // when beeing called directly as subprogram.
  513. //
  514.     const char progname[] = "pstoedit.exe ";
  515.     char * cmdline = (char *) malloc(strlen(TheCmdLine) + 1 + strlen(progname));
  516.     strcpy(cmdline,progname);
  517.     strcat(cmdline,TheCmdLine);
  518.     const int result = callgs(cmdline);
  519.     free(cmdline);
  520. #endif
  521.    if (result == 0) {
  522.        // MessageBox(NULL,"Convertion complete","INFO",MB_OK);
  523.    } else {
  524.        // this message is given directly after the return from gs
  525.       // MessageBox(NULL,"Convertion failed","INFO",MB_OK);
  526.    }
  527.  
  528.     return result;
  529. }
  530.  
  531.  
  532. int OwlMain(int , char* [])
  533. {
  534.   TWinp2eApp   app;
  535.   return app.Run();
  536. }
  537.