// MUI Interface for psnup accessory
// =================================
//
// WinEdt MUI Sample
//
// Based on a macro using older MUI Plugin by Denis Stancer:
//
//     psnup-GUI.edt
//     Jim Schummer
//     Email schummer at northwestern

Requires(20061028);
PushTagsandRegisters;

  // Settings for this interface
  // Used by MUILoad(); and MUISave();
  // It does not have to exists if defaults are OK...
  // This works with profiles (%b<>%B) ...
  LetReg(1, "%b\MUI\psnup");
  GetFileName('%X',0);
  CreateFolders("%!1",1);
  LetReg(4, "%!1\%!0.mui");

  MUIReset('MUI - psnup...');

  // Main Form Size and Position
  MUISet('','width','460');
  MUISet('','height','464');
  MUISet('','position','1'); // Main Form Center

  MUIGet('','client width',9); // Get the form size
  MUIGet('','client height',8);

  // Options are in the Page Control --------------------------------

  MUIAdd(8,8,%!9-16,%!8-8-41,'PageControl','TPageControl');

  // OK/Cancel (aligned -> independent of the dialog size) ----------

  // Modal OK and Cancel Buttons (aligned bottom right)
  MUIAdd(0,0,0,40, 'Bottom_Panel','TPanel');
    MUISet('Bottom_Panel','aligned', 'alBottom');
    MUISet('Bottom_Panel','inner bevel', '0');
    MUISet('Bottom_Panel','outer bevel', '0');
    MUIAdd(0,0,164,0, 'Modal_Panel','TPanel','Bottom_Panel');
      MUISet('Modal_Panel','aligned', 'alRight');
      MUIAdd(0,8,0,0, 'OK','TButton','Modal_Panel','&OK');
        MUISet('OK','default', '1');
        MUISet('OK','on click', !'MUIHide(1);');
      MUIGet('OK','width',7);
      MUIAdd(%!7+8,8,0,0, 'Cancel','TButton','Modal_Panel','Cancel');
        MUISet('Cancel','cancel', '1'); // ESCAPE
        MUISet('Cancel','on click', !'MUIHide(2);');
      LetRegNum(6,16+2*%!7);
      MUISet('Modal_Panel','width','%!6');
      MUIGet('OK','height',7);
      LetRegNum(6,16+%!7);
      MUISet('Bottom_Panel','height','%!6');

  // Fancy URL and e-mail Label with event handler(s) ---------------
  // Comment out if not wanted...

  MUIAdd(10,4,0,0,'email','TLabel','Bottom_Panel','alex@winedt.com');
    MUISet('email','font color','clBlue');
    MUISet('email','font underline','1');
    MUISet('email','cursor','crHandPoint');
    MUISet('email','on click',!"Call('email');");
    MUISet('email','hint',"Send an email to the author...");
  MUIAdd(10,20,0,0,'http','TLabel','Bottom_Panel','http:/%_/www.winedt.org');
    MUISet('http','font color','clBlue');
    MUISet('http','font underline','1');
    MUISet('http','cursor','crHandPoint');
    MUISet('http','on click',!"Call('http');");
    MUISet('http','hint',"Check for the latest downloads...");

  // Pages and Options ----------------------------------------------

  MUIAdd(0,0,0,0,'Page1','TTabSheet','PageControl','Options');
  MUIAdd(0,0,0,0,'Page2','TTabSheet','PageControl','Advanced');
  MUIGet('Page1','client width',7);
  LetRegNum(7,'(%!7-24)/2'); // Group Width
  LetRegNum(9,24);           // Check Box Height+Vertical Spacing
  MUIGet('','text width',8,'Output Paper Size: ');
  LetRegNum(8,%!8+12);
  LetRegNum(6,%!7-%!8-8);

  MUIAdd(8,8,%!7, 24+4*%!9, 'GroupBox_Layout','TGroupBox','Page1','Layout');
    LetRegNum(9,22);
    MUIAdd(8,%!9+4,0,0, 'Pages_Label','TLabel','GroupBox_Layout','&Pages per Sheet: ');
      MUISet('Pages_Label','on click',!"MUISet('','active','Pages');");
    MUIAdd(%!8,%!9,%!6,0,'Pages','TComboBox','GroupBox_Layout','|1|2|3|4|5|6|7|8|9');
      MUISet('Pages','style','1');
      MUISet('Pages','on click','Call("Output");');
      MUISet('Pages','on change','Call("Output");');
      MUISet('Pages','index','0');
      MUISet('Pages_Label','focus','Pages');
    LetRegNum(9,%!9+32);
    MUIAdd(8,%!9+4,0,0,'Input_Label','TLabel','GroupBox_Layout','&Input Paper Size: ');
      MUISet('Input_Label','on click',!"MUISet('','active','Input_Paper');");
    MUIAdd(%!8,%!9,%!6,0,'Input_Paper','TComboBox','GroupBox_Layout',>
           '|letter|legal|a3|a4|a5|b5|tabloid|statement|executive|folio|quarto|10x14');
      MUISet('Input_Paper','style','1');
      MUISet('Input_Paper','index','0');
      MUISet('Input_Label','focus','Input_Paper');
    LetRegNum(9,%!9+32);
    MUIAdd(8,%!9+4,0,0,'Output_Label','TLabel','GroupBox_Layout','&Output Paper Size: ');
      MUISet('Output_Label','on click',!"MUISet('','active','Output_Paper');");
    MUIAdd(%!8,%!9,%!6,0,'Output_Paper','TComboBox','GroupBox_Layout',>
           '|letter|legal|a3|a4|a5|b5|tabloid|statement|executive|folio|quarto|10x14');
      MUISet('Output_Paper','style','1');
      MUISet('Output_Paper','index','0');
      MUISet('Output_Label','focus','Output_Paper');

  LetRegNum(9,24);   // Check Box Height+Vertical Spacing
  LetRegNum(8,20);   // Top Margin
  MUIAdd(%!7+16,8,%!7, 24+4*%!9, 'GroupBox_Orientation','TGroupBox','Page1','Orientation');
    LetRegNum(6, %!7-16); // Radio Button Width
    MUIAdd(8,%!8,%!6,0, 'RBO_Portrait','TRadioButton','GroupBox_Orientation','Portrait');
    MUISet('RBO_Portrait','checked','1'); // The first one is checked by default
    LetRegNum(8, %!8+%!9);
    MUIAdd(8,%!8,%!6,0, 'RBO_Landscape','TRadioButton','GroupBox_Orientation','Landscape');
    LetRegNum(8, %!8+%!9);
    MUIAdd(8,%!8,%!6,0, 'RBO_Seascape','TRadioButton','GroupBox_Orientation','Seascape');
    LetRegNum(8, %!8+%!9);
    MUIAdd(8,%!8,%!6,0, 'RBO_Rotated','TRadioButton','GroupBox_Orientation','Rotated');

  MUIGet('Page1','client width',7);
  MUIGet('GroupBox_Layout','top',9);
  MUIGet('GroupBox_Layout','height',8);
  LetRegNum(9,'%!9+%!8+8');
  LetRegNum(8,20);   // Top Margin
  MUIAdd(8,%!9,%!7-16, 24+24*3, 'GroupBox_Options','TGroupBox','Page1','Options');
    LetRegNum(9,24);   // Check Box Height+Vertical Spacing
    LetRegNum(6, '(%!7-16)'); // Check Box Width
    MUIAdd(8,%!8,%!6,0, 'CB_Borders','TCheckBox','GroupBox_Options','Draw Borders');
      MUISet('CB_Borders','checked','1');
    LetRegNum(8, %!8+%!9);
    MUIAdd(8,%!8,%!6,0, 'CB_Columns','TCheckBox','GroupBox_Options','Arrange in Columns (Instead of Rows)');
    LetRegNum(8, %!8+%!9);
    MUIAdd(8,%!8,%!6,0, 'CB_Ghostview','TCheckBox','GroupBox_Options','Automatically Open Output in Ghostview');

  MUIGet('GroupBox_Options','height',9);
  MUIGet('GroupBox_Options','top',8);
  LetRegNum(9,'%!9+%!8+8');
  MUIAdd(8,%!9,%!7-16, 64, 'GroupBox_Input','TGroupBox','Page1','Input File');
    MUIAdd(8,24,%!7-32-16-24, 0, 'InputFile','TComboBox','GroupBox_Input');
      MUISet('InputFile','style','1');
      MUISet('InputFile','save mode','3'); // maintain the list of 9 recent files
      MUISet('InputFile','on change',!'Call("Output");');
    MUIAdd(%!7-32-24,21,30,28, 'Browse','TSpeedButton','GroupBox_Input');
      MUISet('Browse','hint','Browse for Input File...');
      MUISet('Browse','glyph','Browse');
      MUISet('Browse','on click',!'Call("Browse");');

  MUIAdd(0,0,0,44, 'Action_Panel','TPanel','Page1');
    PushTagsandRegisters;
    MUISet('Action_Panel','aligned', 'alBottom');
    MUISet('Action_Panel','inner bevel', '0');
    MUISet('Action_Panel','outer bevel', '0');
      MUIGet('Action_Panel','client width',9); // Panel Width
      MUIGet('','text width', 8, '    Clear Input    '); // Button Width
      LetRegNum(7, %!8+8);       // Button Width + Horizontal Spacing
      LetRegNum(6, %!9 - 4*%!7); // Left
      LetRegNum(9, 8);
      MUIAdd(%!6+0*%!7,12,%!8,0, 'Compile','TButton','Action_Panel','Co&mpile ...');
        MUISet('Compile','on click',!"Call('Compile');Call('Enable');");
      MUIAdd(%!6+1*%!7,12,%!8,0, 'Preview','TButton','Action_Panel','Pre&view ...');
        MUISet('Preview','on click',!"Call('GSView');");
      MUIAdd(%!6+2*%!7,12,%!8,0, 'Clear','TButton','Action_Panel','Clear Input');
        MUISet('Clear','on click',!"MUISet('InputFile','text','');MUISet('InputFile','items','');Call('Output');");
      MUIAdd(%!6+3*%!7,12,%!8,0, 'Save','TButton','Action_Panel','Save Settings');
        MUISet('Save','on click',!"MUISave('%!4');Call('Enable');");
    PopTagsandRegisters;

  // Advanced Options -----------------------------------------------

  MUIAdd(10,8,0,0,'Label','TLabel','Page2','Create a postscript file with multiple pages per sheet...');
    MUISet('Label','font color','clMaroon');
  MUIAdd(10,24,0,0,'Labelpsnup','TLabel','Page2','See the documentation for psnup (TeX) accessory!!!');

  MUIAdd(8,48,%!7-16, 144, 'GroupBox_Output','TGroupBox','Page2','Output File');
    MUIAdd(8,24,%!7-32, 0, 'OutputFile','TEdit','GroupBox_Output');
    MUIAdd(8,56,%!7-32, 0, 'AutoOutput','TCheckBox','GroupBox_Output','Automatic Output Filename Generator');
      MUISet('AutoOutput','checked','1');
      MUISet('AutoOutput','on click',!'Call("Output");');
    MUIAdd(8,80,%!7-32, 0, 'PromptOverwrite','TCheckBox','GroupBox_Output','Prompt before overwriting the output');
      MUISet('PromptOverwrite','checked','0');
    MUIAdd(8,108,0,0, 'DeleteOutput','TButton','GroupBox_Output','Delete Output');
      MUIGet('DeleteOutput','width',9);
      LetRegNum(9,%!9+64);
      MUISet('DeleteOutput','width',"%!9");
      MUISet('DeleteOutput','on click',!"MUIGet('OutputFile','text',0);DeleteFile('%!0');Call('Enable');");
    MUIAdd(19+%!9,108,%!9,0, 'DeleteAll','TButton','GroupBox_Output','Delete All *-up Files');
      MUISet('DeleteAll','on click',!"Call('DeleteAll');Call('Enable');");

  MUIGet('GroupBox_Output','height',9);
  MUIGet('GroupBox_Output','top',8);
  MUIAdd(8,%!9+%!8+8,%!7-16, 72, 'GroupBox_Exec','TGroupBox','Page2','Execution Mode');
    MUIAdd(8,20,'(%!7-16)/2', 0, 'Detached','TCheckBox','GroupBox_Exec','Run Detached');
      MUISet('Detached','checked','1');
    MUIAdd(8,44,'(%!7-16)/2', 0, 'ExitCode','TCheckBox','GroupBox_Exec','Detect Exit Code');
      MUISet('ExitCode','checked','1');
    MUIAdd(%!7/2+8,20,'(%!7-16)/2', 0, 'Pause','TCheckBox','GroupBox_Exec','Pause');
      MUISet('Pause','checked','0');
    MUIAdd(%!7/2+8,44,'(%!7-16)/2', 0, 'High_Priority','TCheckBox','GroupBox_Exec','High Priority');
      MUISet('High_Priority','checked','0');

  MUIGet('GroupBox_Exec','height',9);
  MUIGet('GroupBox_Exec','top',8);
  MUIAdd(8,%!9+%!8+8,%!7-16, 60, 'GroupBox_Source','TGroupBox','Page2','MUI Macro Source');
    MUIAdd(8,24,%!7-32-16-24, 0, 'MUISource','TEdit','GroupBox_Source');
      MUISet('MUISource','save mode','-1'); // No Saving for this one
      MUISet('MUISource','text','%X');
      MUISet('MUISource','readonly','1');
    MUIAdd(%!7-32-24,21,30,28, 'Edit','TSpeedButton','GroupBox_Source');
      MUISet('Edit','hint','Edit MUI Source Macro Script...');
      MUISet('Edit','glyph','Open');
      MUISet('Edit','on click',!'Call("Edit");');

  JMP('End_Events');

// Event Handlers for email and http label
:email:: ============================================================
  // Send an email about this MUI
  MUIHide(2); // MS Outlook focuses the main window instead of MUI...
  SendMail('MUI - psnup...','',"Aleksander Simonic|alex@winedt.com","",1000);
  Return;

:http:: =============================================================
  // Start the internet browser
  ShellExecute("Open", "http:/%_/www.winedt.org", "", "", 1);
  Return;

:Edit:: =============================================================
  // Profile-friendly edit (make a local copy!)
  IfFileExists('%b\MUI\psnup\psnup.edt',!'Relax;',>
    !|CopyFile("%X","%b\MUI\psnup\psnup.edt",1,1);|);
  Open("%b\MUI\psnup\psnup.edt");
  Return;

:Browse:: ===========================================================
  // Start the Browse Open Dialog...
  MUIGet('InputFile','text',9);
  GetFilePath("%!9",9);
  IfFileExists("%!9\",!"Relax;",!"LetReg(9,'%P')");
  IfFileExists("%!9\",!"Relax;",!"LetReg(9,'%!P');");
  Browse(9,'Postscript files|*.ps;*.eps|All Files|*.*','%!9');
  IfOK(!|MUISet('InputFile','text','%!9');|);
  Call('Output');
  Return;

:Output:: ===========================================================
  // Fix the output filename from the current input
  MUIGet('AutoOutput','checked','9');
  IfNum(%!9,'1','<>',!"MUISet('OutputFile','readonly','0');Return;");
  MUISet('OutputFile','readonly','1');
  MUIGet('InputFile','text',9);
  IfStr('%!9','','>',>
    !|GetFilePath('%!9',8);>
      GetFileName('%!9',7);>
      MUIGet('Pages','text',1);>
      LetReg(8,'%!8\%!7-%!1up');>
      GetFileType('%!9',9);>
      LetReg(9,'%!8%!9');>
     |);
  MUISet('OutputFile','text','%!9');
  Call('Enable');
  Return;

:Enable:: ===========================================================
  // Enable controls depending on the current input...
  MUIGet('InputFile','text',9);
  IfFileExists('%!9',!'LetRegNum(9,1);',!'LetRegNum(9,0);');
  MUISet('Compile','enabled','%!9');
  MUIGet('InputFile','count',9);
  IfNum('%!9','0','>',!'LetRegNum(9,1);',!'LetRegNum(9,0);');
  MUISet('Clear','enabled','%!9');
  MUIGet('InputFile','text',9);
  IfStr('%!9','','>',!"MUISet('Clear','enabled','1');");
  MUIGet('OutputFile','text',9);
  IfFileExists('%!9',!'LetRegNum(9,1);',!'LetRegNum(9,0);');
  MUISet('Preview','enabled','%!9');
  MUISet('DeleteOutput','enabled','%!9');
  // Only enabled if some other -nup file exists
  MUISet('DeleteAll','enabled','0');
  MUIGet('InputFile','text',9);
  MUIGet('OutputFile','text',5);
  GetFilePath('%!9',8);
  GetFileName('%!9',7);
  GetFileType('%!9',6);
  LetReg(9,'%!8\%!7-up%!6');
  IfStr('%!9','%!5','=',!"LetReg(9,'');");
  IfFileExists('%!9',!"MUISet('DeleteAll','enabled','1');");
  LetReg(9,'%!8\%!7-2up%!6');
  IfStr('%!9','%!5','=',!"LetReg(9,'');");
  IfFileExists('%!9',!"MUISet('DeleteAll','enabled','1');");
  LetReg(9,'%!8\%!7-4up%!6');
  IfStr('%!9','%!5','=',!"LetReg(9,'');");
  IfFileExists('%!9',!"MUISet('DeleteAll','enabled','1');");
  LetReg(9,'%!8\%!7-6up%!6');
  IfStr('%!9','%!5','=',!"LetReg(9,'');");
  IfFileExists('%!9',!"MUISet('DeleteAll','enabled','1');");
  LetReg(9,'%!8\%!7-8up%!6');
  IfStr('%!9','%!5','=',!"LetReg(9,'');");
  IfFileExists('%!9',!"MUISet('DeleteAll','enabled','1');");
  Return;

:DeleteAll:: ========================================================

  MUIGet('InputFile','text',9);
  GetFilePath('%!9',8);
  GetFileName('%!9',7);
  GetFileType('%!9',6);
  LetReg(9,'%!8\%!7-up%!6');
  IfFileExists('%!9',!"DeleteFile('%!9');");
  LetReg(9,'%!8\%!7-2up%!6');
  IfFileExists('%!9',!"DeleteFile('%!9');");
  LetReg(9,'%!8\%!7-4up%!6');
  IfFileExists('%!9',!"DeleteFile('%!9');");
  LetReg(9,'%!8\%!7-6up%!6');
  IfFileExists('%!9',!"DeleteFile('%!9');");
  LetReg(9,'%!8\%!7-8up%!6');
  IfFileExists('%!9',!"DeleteFile('%!9');");
  Call('Enable');
  Return;

:GSView:: ===========================================================

  MUIGet('OutputFile','text',9);
  GetFileName("%!9",8);
  GetFileType("%!9",7);
  GetFilePath("%!9",6);
  LetReg(8,"%!8%!7");
  Run(|%$('GSView'); "%!9"|,'%!6',0,0,'%!8 - GSview',1,1);
  Return;

:Compile:: ==========================================================

  MUIGet('InputFile','text',9);
  MUIGet('OutputFile','text',8);

  LetReg(7,'');
  MUIGet('Pages','text',0);
  IfStr("%!0","",'>',!'LetReg(7,"%!7 -%!0up");');
  MUIGet('Input_Paper','text',0);
  IfStr("%!0","",'>',!'LetReg(7,"%!7 -P%!0");');
  MUIGet('Output_Paper','text',0);
  IfStr("%!0","",'>',!'LetReg(7,"%!7 -p%!0");');

  //  MUIGet('RBO_Portrait','checked',0);
  //  IfNum(%!0,0,'>',!'LetReg(7,"%!7");'); // Default: no switch
  MUIGet('RBO_Landscape','checked',0);
  IfNum(%!0,0,'>',!'LetReg(7,"%!7 -l");');
  MUIGet('RBO_Seascape','checked',0);
  IfNum(%!0,0,'>',!'LetReg(7,"%!7 -r");');
  MUIGet('RBO_Rotated','checked',0);
  IfNum(%!0,0,'>',!'LetReg(7,"%!7 -f");');

  MUIGet('CB_Borders','checked',0);
  IfNum(%!0,0,'>',!'LetReg(7,"%!7 -d");');
  MUIGet('CB_Columns','checked',0);
  IfNum(%!0,0,'>',!'LetReg(7,"%!7 -c");');

  MUIGet('PromptOverwrite','checked',0);
  IfNum(%!0,'0','=',!'JMP("no_Prompt");');
  IfFileExists("%!8",>
      !'Prompt("Overwrite ""%!8""?",2,0,"Relax;","PopTagsandRegisters;Exit;");');
:no_Prompt:: ========================================================

  MUIGet('Detached','checked',0);
  LetRegNum(1,00000100);
  IfNum(%!0,0,'>',!'LetRegNum(1,%!1+1000);');
  MUIGet('High_Priority','checked',2);
  MUIGet('Pause','checked',3);
  IfNum(%!3,0,'>',!'LetRegNum(3,111);',!'LetRegNum(3,000);');

  GetFilePath("%!9",6);

  WinExe("",'psnup.exe %!7 "%!9" "%!8"',"%!6",>
    "psnup...", %!1,%!2,'','','',%!3);

  // Exit Code: Errors (zero return)?
  MUIGet('ExitCode','checked',0);
  IfNum(%!0,0,'>',>
   !|>
      GetExitCode(0);>
      IfNum('%!0','0','=',!'Relax;',>
        !'Prompt("psnup.exe returned non-zero Exit Code (%!0) indicating "+>
                 "that some problems might have occurred during the compilation...");');>
    |);

  IfFileExists("%!8",'Relax;',>
        !'Prompt(|psnup.exe failed to create output file "%!8".>
          %\For possible explanations start the accessory from the Command Prompt and consult psnup''s Documentation!|);>
          PopTagsandRegisters;Exit;');

  MUIGet('CB_Ghostview','checked',0);
  IfNum(%!0,0,'>',>
   !|Call('GSView');|);

  Return;

:End_Events:: =======================================================

  // Load Previous Values ....
  MUILoad('%!4'); // It does not have to exist...
  // Except the Input File:
  IfFileExists("%P\%N.ps",>
    !|MUISet('InputFile','text','%P\%N.ps');|);
  Call('Output'); // Fix Output File Name and Enable Actions

  MUIShow; // Show Modal Dialog

  IfOK('Relax;','PopTagsandRegisters;Exit;');

  // Save the Current Values ....
  MUISave('%!4');

  MUIGet('InputFile','text',9);
  IfStr('%!9','','=',!'PopTagsandRegisters;Exit;'); // No Input (just exit)...
  IfFileExists('%!9','Relax;',!'PopTagsandRegisters;Exit;'); // No Input (just exit)...

  Call('Compile');

PopTagsandRegisters;

End;
