Advertisement
GlobalAccessSoftware

PrintForm1.cs

Apr 26th, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 33.02 KB | None | 0 0
  1.  
  2. #region Top Part
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  4. // PrintForm1.cs 17-Dec-17 v1.1.4.27
  5. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  6. // Run This On a Worker Thread
  7. // so it's independent of Smesa
  8. // as an App within an App.
  9. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  10. // Brought to PrintBlaster NameSpace
  11. // v2.3.7.28 21-Jan-2018 (Beginning)
  12. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  13. // v2.5.9.31 09-Feb-2018 Isolated &
  14. // Ready for Further Development!
  15. // v3.2.3.47 05-Apr-2018 ^ Beginning
  16. // v3.2.4.47 09 Near Total Concept!
  17. // v3.3.4.48 18-Apr-2018
  18. // v4.3.5.49 22-Apr-2018 Integrated.
  19. // v4.3.5.51 Mmf2-Data Mechanisms.
  20. // v4.3.5.52 9-May-18 Up/Down Move.
  21. // v4.4.5.53 Mapped in to Lock Out.
  22. // (c) John P. Edwards 20-May-2018
  23. // Revision4.5.7.54 FileWatcher impl.
  24. // v4.5.7.57nobadges Brings in MCD stuff.
  25. // and MyToolbox 09-Jun-18
  26. // v4.5.7.57nobagiac 22-Jun-2018 Rescued!
  27. // v4.5.7.58 Abouts Perfected 02-Jul
  28. // v4.5.7.58nobloop 03-Jul-2018
  29. // Integrating with MyCustomDialogs
  30. // v4.5.7.58nobluff 05-Jul-2018 Viewer,
  31. // About, and Skip Togglerized. 4 Kinds!
  32. // v4.5.8.60 18-Jul-18 Shared Info!
  33. // v4.5.8.60 23-Jul-18 FULLY TESTED! 26-Jul
  34. // v4.5.8.61 04-Aug Integration w/ Shared.sme
  35. // v4.5.9.63 Omega Plan
  36. // v4.5.9.63 15-Aug-2018 Complete & TestsWell.
  37. // v4.5.9.64 21-Aug Fixed and finalized.
  38. // v4.6.9.65 29-Aug-2018 Viewer Rapid-Fire!
  39. // v4.6.9.67 06-Sep-2018 Shared, Skip, Testing
  40. // v4.6.9.69 17- " " Perfected PathPb & FSWs!
  41. // v4.7.0.70 18-" " Assy Thread Safe & IO Safe.
  42. // v4.7.0.71 19-Sep About Colors.
  43. // v4.7.0.72 21-"" "" Added Invert List Tool.
  44. // v4.7.1.73 23-Sep Perfected OpenFolder mods.
  45. // v4.7.1.75 28 " " PageSetup1 & 2 ProtoTypes.
  46. // v4.7.1.76 30-Sep MCT.SaveList & GetList mods.
  47. // v4.8.0.76 30-Sep MCT.SaveList & GetList mods.
  48. // v4.8.1.77 03-Oct Proves New Design Model!
  49. // v4.8.1.78 08-Oct Sort List Option.
  50. // v4.8.2.81 20-Oct Switch to Editor.
  51. // v4.8.3.84 06-Nov-2018 Myriad Mods.
  52. // v4.9.4.90 27-Nov-2018 ReadRemovals() and
  53. // GetSelectedIndex() integration completed.
  54. // NOTE: See calls to GetSelectedIndex();
  55. // v4.9.4.92 04-Dec-2018 Killed.sme Mods.
  56. // v4.9.5.94 13-Dec-2018 ME2PB Killed 1 & 2
  57. // v4.9.5.96 31-Dec-2018 Integrate Prototype.
  58. // v5.0.6.03 23-Mar-2019 Alphabetize overlook
  59. // v5.1.6.07 19-Apr-2019 SetPaths Assurances.
  60. // v5.1.7.09 24-Apr-2019 Debugged & Tested.
  61. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  62.  
  63. using System;
  64. using System.Collections.Generic;
  65. using System.Collections.ObjectModel;
  66. using System.Collections.Specialized;
  67. using System.Diagnostics;
  68. using System.Drawing;
  69. using System.IO;
  70. using System.Linq;
  71. using System.Reflection;
  72. using System.Threading;
  73. using System.Windows.Forms;
  74.  
  75. // Custom References:
  76. using MyCustomLibrary;
  77. using MyEditor;
  78.  
  79. // Class Aliases:
  80. using MCD = MyCustomLibrary.Dialogs;
  81. using MCT = MyCustomLibrary.MyToolbox;
  82. using SNM = MyCustomLibrary.SafeNativeMethods;
  83.  
  84. // Property Aliases:
  85. using PFR = PrintBlaster.Properties.Resources;
  86. using PFS = PrintBlaster.Properties.Settings;
  87.  
  88. namespace PrintBlaster
  89. {
  90.   public partial class PrintForm1 : Form
  91.   {
  92.     // See PrintFormHeader.cs for Globals:
  93.  
  94.     /// <summary> 1st Instance only
  95.     /// Class Constructor. (1 of 2)
  96.     /// Ist Instance here, and 2nd
  97.     /// Instance is the only other
  98.     /// Class Constructor. v4.5.8.61
  99.     /// (c) 02-Aug-2018 by -JpE-
  100.     /// For G.A.S.I.T.
  101.     /// </summary>
  102.     public PrintForm1() // Class Constructor 1 of 2
  103.     {
  104.       WaitCursor();
  105.       Args = Environment
  106.         .GetCommandLineArgs();
  107.       Detect();
  108.       SetPaths();
  109.       ClearMarkers();
  110.       if (Which > 0 && Which < 7)
  111.       { // When Sent by MyEditor
  112.         // & a 1st instance.
  113.         Switch1(Which);
  114.         Close();
  115.       }
  116.       else // Normal App Start.
  117.         InitializeComponent();
  118.     }
  119. #endregion Top Part
  120.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  121.  
  122.  
  123. #region Paths and Mmf Magic
  124.  
  125.     // First Instance Only.
  126.     private void SetPaths()
  127.     {
  128.       // Assure variable integrity.
  129.       var root = MCT.GetRoot;
  130.       if (string.IsNullOrEmpty(root)
  131.        || !Directory.Exists(root)) throw
  132.          new ArgumentException(PFR.Err7);
  133.       var myAp = root + PFR.PrintApp;
  134.       // Ascertain the PrintBlaster Path.
  135.       PbPath = File.Exists(myAp)
  136.         ? myAp : Args[0];
  137.       if (File.Exists(PbPath))
  138.         WriteShared.AppRoot =
  139.           Path.GetDirectoryName(PbPath);
  140.       // Ascertain "MyEditor.exe" Path.
  141.       GetEdPath();
  142.       GetMappedPathAndWrapUp(root);
  143.       if (!string.IsNullOrEmpty(PbPath)
  144.         && File.Exists(PbPath))
  145.           Args[0] = PbPath;
  146.     }
  147.  
  148.     private void
  149.       GetMappedPathAndWrapUp(string root)
  150.     {
  151.       var file = root + PFR.MMF1;
  152.       // Ascertain the Editor.sme Path.
  153.       if (File.Exists(file))// ^ (MMF1)
  154.         PFS.Default.PathEditor = file;
  155.       else GetEditorPath();
  156.       // do it ^ old school otherwise!
  157.       PFS.Default.PathSetup =
  158.         root + PFR.PageSetup;
  159.       PFS.Default.PathForm  =
  160.         root + PFR.Form;
  161.       PFS.Default.Save();
  162.       Data = GetData2();
  163.       WriteSetupPath();
  164.     }
  165.    
  166.     // First Instance Only.
  167.     // Get "Editor.sme" Path from file.
  168.     private void GetEditorPath()
  169.     {
  170.       var docu = MCT.GetDocFolder;
  171.       var path = docu + PFR.EditorFile;
  172.       if (File.Exists(path))
  173.       {
  174.         path = ReadPath(path);// reused var
  175.         if (!string.IsNullOrEmpty(path)
  176.           && File.Exists(path))
  177.             PFS.Default.PathEditor = path;
  178.         else GetOldSchool();
  179.       }
  180.       else GetOldSchool();
  181.     }
  182.  
  183.     // First Instance Only. Last Try to
  184.     // Get "Editor.sme" Path another way.
  185.     private void GetOldSchool()
  186.     {
  187.       string answ = "", fl = PFR.MMF1;
  188.  
  189.       var dir = Path
  190.         .GetDirectoryName(PbPath);
  191.       if (!string.IsNullOrEmpty(dir)
  192.         && Directory.Exists(dir)
  193.         && File.Exists(dir+fl))
  194.           answ = dir+fl;
  195.  
  196.       dir = Path.GetDirectoryName(EdPath);
  197.       if (string.IsNullOrEmpty(answ))
  198.         if (!string.IsNullOrEmpty(dir)
  199.           && Directory.Exists(dir)
  200.           && File.Exists(dir+fl))
  201.             answ = dir+fl;
  202.  
  203.       dir = Path.GetDirectoryName(
  204.         PFS.Default.PathEditor);
  205.       if (string.IsNullOrEmpty(answ))
  206.         if (!string.IsNullOrEmpty(dir)
  207.           && Directory.Exists(dir)
  208.           && File.Exists(dir+fl))
  209.             answ = dir+fl;
  210.  
  211.       if (!string.IsNullOrEmpty(answ)
  212.         && File.Exists(answ))
  213.           PFS.Default.PathEditor = answ;
  214.     }
  215.  
  216.     #region Data Doc
  217.     // Args[0] = path   // (PB\Bin\Release)            
  218.     // Args[1] = size   // (0 for autosize)
  219.     // Args[2] = offset // (0 for autosize)
  220.     // Args[3] = 1;     // Flip Byte Switch ON!
  221.     // Args[4] = true;  // read sent msg.
  222.     // Args[5] = "mmf2";// unique name.
  223.     // Args[6] = true;  // delete after read.
  224.     // Args[7] = view length from offset. (Optional)
  225.     // ^ defaults to 0L which goes to the end of size.
  226.     #endregion
  227.  
  228.     // First Instance Only.
  229.     public static object[] GetData2()
  230.     {
  231.       var path = PFS.Default.PathSetup;
  232.  
  233.       return new object[]
  234.       {
  235.         path, 1, 0, new
  236.         Collection<byte> {1}, true,
  237.         Path.GetFileNameWithoutExtension(
  238.         path), true
  239.       };
  240.     }
  241.    
  242.     // First Instance Only.
  243.     private static void WriteSetupPath()
  244.     {
  245.       if (string.IsNullOrEmpty(
  246.         PFS.Default.PathSetup)) throw
  247.           new ArgumentException(PFR.Err5);
  248.       var path = MCT.GetDocFolder +
  249.         PFR.PathSetup;
  250.       var file = PFS.Default.PathSetup;
  251.       var data = new[]
  252.       {
  253.         file, "1", "0", "null", "true",
  254.         Path.GetFileNameWithoutExtension(
  255.           file), "true"
  256.       };
  257.       File.WriteAllLines(path, data);
  258.       File.SetLastAccessTime(
  259.         path, DateTime.Now);
  260.     }
  261.  
  262.     // First Instance Only.
  263.     // Ascertain "MyEditor.exe" Path.
  264.     // typical ^v Quickie for All.
  265.     // v5.1.6.07 19-Mar-2019
  266.     private void GetEdPath()
  267.     {
  268.       var d = MCT.GetRoot;
  269.       var f = d + PFR.EdApp;
  270.       // New & Improved Quick Way.
  271.       if (!string.IsNullOrEmpty(d)
  272.         && Directory.Exists(d)
  273.         && File.Exists(f)) EdPath = f;
  274.       else // else do it the old way.
  275.       { // Note ReUsing Var f
  276.         f = MCT.GetDocFolder + PFR.PathMe;
  277.         if (File.Exists(f))
  278.           EdPath = ReadPath(f);
  279.         // try ^ reading it.
  280.         // or...
  281.         if (string.IsNullOrEmpty(EdPath)
  282.             || !File.Exists(EdPath)
  283.             && (!string.IsNullOrEmpty(Args[0])
  284.             && File.Exists(Args[0])))
  285.           EdPath = Args[0];
  286.       }
  287.     }
  288.  
  289.     public static string ReadPath(string fn)
  290.     {
  291.       if (string.IsNullOrEmpty(fn)
  292.         || !File.Exists(fn)) return "";
  293.       var file = File.ReadAllText(fn);
  294.       if (string.IsNullOrEmpty(file)
  295.         || !File.Exists(file)) return "";
  296.       return file;
  297.     }
  298. #endregion Paths and Mmf Magic
  299.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  300.  
  301.  
  302. #region Load, Watcher, B2F, Closing
  303.  
  304.     private void PrintLoad
  305.       (object sender, EventArgs e)
  306.     {
  307.       Size     = PFS.Default.PF_Size;
  308.       Location = PFS.Default.PF_Loca;
  309.       // v4.9.4.90 27-Nov-2018 -JpE-
  310.       ToggleLastAccessSort.Checked   =
  311.         PFS.Default.SortByAccess;
  312.       ToggleSortAlphabetical.Checked =
  313.         PFS.Default.SortAlphabet;
  314.       // REM: ListBoxLoad Checks Favs.
  315.       ListBoxLoad();
  316.       RoadKill(); // <-= Hook v4.9.5.95!
  317.       BtfWatcher = new FileSystemWatcher();
  318.       SetupBtfWatcher();
  319.       SkipWatcher = new FileSystemWatcher();
  320.       SetupSkipWatcher();
  321.       SharedWatcher = new FileSystemWatcher();
  322.       SetupSharedWatcher();
  323.       KilledWatcher = new FileSystemWatcher();
  324.       SetupKilledWatcher();
  325.       SkipToggle.Checked = MCT.Skip;
  326.       if (ReadSharedData()) GetProperties();
  327.       SelectedChanged("PrintLoad", e);
  328.       ArrowCursor();
  329.       GetFileCount();
  330.       listBox1.SelectedIndexChanged +=
  331.         SelectedChanged;
  332.       TestCodeVisibility();
  333.     }
  334.  
  335.     private void TestCodeVisibility()
  336.     {
  337.       TestCode1.Visible  =
  338.       TestCode2.Visible  =
  339.       TestCode3.Visible  =
  340.       TestCode4.Visible  = MCT.Testing;
  341.     }
  342.  
  343.     // ReUsed elsewhere now too.
  344.     // v4.5.9.63 17-Aug-2018 -JpE-
  345.     private void GetProperties()
  346.     {
  347.       ForeColor          = _formFore;
  348.       BackColor          = _formBack;
  349.       listBox1.ForeColor = _foreColor;
  350.       listBox1.BackColor = _backColor;
  351.       listBox1.Font      = _boxFont;
  352.       Validate();
  353.     }
  354.  
  355.     private void SetupBtfWatcher()
  356.     {
  357.       BtfWatcher.NotifyFilter =
  358.         NotifyFilters.FileName;
  359.       BtfWatcher.InternalBufferSize  = 8192;
  360.       BtfWatcher.EnableRaisingEvents = false;
  361.       // Setup File System Watcher:
  362.       BtfWatcher.Path = // for Mapped File.
  363.         Path.GetDirectoryName(
  364.           PFS.Default.PathForm);
  365.       BtfWatcher.Filter = Path
  366.         .GetFileName(
  367.           PFS.Default.PathForm);
  368.       // Create Eventhandler...
  369.       BtfWatcher.Created += BtfEventHandler;
  370.       BtfWatcher.EnableRaisingEvents = true;
  371.     }
  372.    
  373.     private void BtfEventHandler(
  374.       object sender, FileSystemEventArgs e)
  375.     {
  376.       #region testcode
  377.       //var txt = listBox1.InvokeRequired
  378.       //  ? "Invoke Required."
  379.       //  : "Invoke NOT Required!";
  380.       //txt = "PrintForm Class " + txt;
  381.       //MessageBox.Show(txt); //PFR.BtfText);
  382.       #endregion
  383.       if (listBox1.InvokeRequired)
  384.         Invoke(new Invoker1(
  385.           Bring2Front));
  386.       else
  387.         Bring2Front();
  388.     }
  389.    
  390.     // Bring UI window (hWnd) to front
  391.     // using SNM.B2F. Ref it.
  392.     private void Bring2Front()
  393.     {
  394.       BtfWatcher.EnableRaisingEvents = false;
  395.       SNM.B2F(Process
  396.         .GetCurrentProcess()
  397.           .MainWindowHandle);
  398.       // v Thread Safe / IO Safe.
  399.       MemoryMapper.ReadBytes(GetData3());
  400.       // ^ Reset trigger by..| effectively
  401.       // | deleting it with: data[6] = true;
  402.       BtfWatcher.EnableRaisingEvents = true;
  403.     }
  404.  
  405.     private static object[] GetData3()
  406.     {
  407.       var p3 = PFS.Default.PathForm;
  408.       #region Release Test Code
  409.       //MessageBox.Show(string.Format(
  410.       //  "p3 is {0}", p3));
  411.       #endregion
  412.       var nm = Path. // } to simplify.
  413.         GetFileNameWithoutExtension(p3);
  414.       // Then just return the object array.
  415.       return new object[]
  416.       {
  417.         p3, 1, 0, new Collection<byte> {1},
  418.         true, nm, true
  419.       };
  420.     }
  421.  
  422.     private void PrintFormClosing(
  423.       object sender, FormClosingEventArgs e)
  424.     {
  425.       if (MyPrintDoc != null)
  426.         MyPrintDoc.Dispose();
  427.       PFS.Default.PF_Size = Size;
  428.       PFS.Default.PF_Loca = Location;
  429.       ListBoxSave();
  430.       UpdateFavorites();
  431.       PFS.Default.SortByAccess =
  432.         ToggleLastAccessSort.Checked;
  433.       PFS.Default.SortAlphabet =
  434.         ToggleSortAlphabetical.Checked;
  435.       PFS.Default.Save();
  436.       var fn = WriteShared.AppRoot
  437.         + PFR.PB;
  438.       if (File.Exists(fn))
  439.         File.Delete(fn);
  440.     }
  441. #endregion Load, Watcher, B2F, Closing
  442.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  443.  
  444.  
  445. #region Detect if Smesa sent.
  446.  
  447.     /// <summary> Detect Task Selected from Smesa
  448.     /// My Editor Project has sent a special
  449.     /// request for a particular Form
  450.     /// from This Project. PrintForm isn't shown
  451.     /// unless it was an actual Run from Smesa Class
  452.     /// See Smesa PrintItemHandler in Smesa Options.
  453.     /// </summary>
  454.     private void Detect()
  455.     {
  456.       if (Args.Length < 3) return;
  457.       // Which = 0; ^
  458.       Which = Args[1].Contains(Key1)
  459.         ? 7 : Detect(Args);
  460.     }
  461.  
  462. #region Shared IntraProcess
  463.  
  464.     // Critical for both instances. <-=
  465.     // Must Be Static & Hard Coded.
  466.     // ThreadSafe \ ProcessSafe \ IOSafe
  467.     private static int Detect(
  468.       IList<string> args)
  469.     {
  470.       // Print v v Preview is Default.
  471.       var item = 1;
  472.       PbMutex.WaitOne();
  473.       // Defaults to Preview.
  474.       var sent = args[1];
  475.       // File>>Page Setup
  476.       if (sent.Contains("Page")) item = 2;
  477.       // File>>Print File or Selection
  478.       if (sent.Contains("This")) item = 3;
  479.       // File>>Print Font & Color v5.1.7.09
  480.       if (sent.Contains("Font")// 24Apr2019
  481.        && sent.Contains("Color"))item = 5;
  482.       // Code Set Here. Legend Also.
  483.       if (args.Count() > 3) item = 3;
  484.       // Batch handler is^above at 3;
  485.       #region Release TestCode.
  486.       //const string can =
  487.       //  "Font request made" +
  488.       //  " it to Detect(IList);";
  489.       //if (item.Equals(5))
  490.       //  MessageBox.Show(can);
  491.       #endregion
  492.       PbMutex.ReleaseMutex();
  493.       return item;
  494.     }
  495. #endregion Shared IntraProcess
  496.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  497.  
  498.  
  499.     private void Switch1(int which)
  500.     {
  501.       ArrowCursor();
  502.       switch (which)
  503.       {
  504.         case 1: PrintPreview1();
  505.           break;
  506.         case 3: PrintThis1();
  507.           break;
  508.         case 5: FontSelect1();// New v5.1.7
  509.           break;
  510.         default: PageSetup1();
  511.           break;
  512.       }
  513.     }
  514.  
  515.     private void PrintFormKeyDown(
  516.       object sender, KeyEventArgs e)
  517.     {
  518.       if (e.KeyData.Equals(Keys.Escape))
  519.         Close();
  520.     }
  521. #endregion Detect if Smesa sent.
  522.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  523.  
  524.  
  525. #region File Menu Items
  526.  
  527.     private void AddFilesClick(
  528.       object sender, EventArgs e)
  529.     {
  530.       // Easy using existing code to Select files.
  531.       Smesa.SetDirectory();
  532.       var dir = Directory.GetCurrentDirectory();
  533.       var fnm = Smesa.GetDefaultFileName(0);
  534.       if (listBox1.SelectedItems.Count > 0)
  535.       {
  536.         fnm = listBox1.SelectedItems[0].ToString();
  537.         dir = Path.GetDirectoryName(fnm);
  538.         // Justin Case
  539.         if ( string.IsNullOrEmpty(fnm)
  540.           || string.IsNullOrEmpty(dir)
  541.           || !File.Exists(fnm)
  542.           || !Directory.Exists(dir))
  543.           return;
  544.       }
  545.       var args = new[]{ fnm };
  546.       args = Smesa.FileNameGet(args, dir);
  547.       // if viable, add to the top of list.
  548.       if (args == null
  549.         || args.Count() == 1
  550.         && args[0] == fnm) return;
  551.       foreach (var file in args
  552.         .Where(file => !listBox1
  553.           .Items.Contains(file)))
  554.             listBox1.Items.Insert(0, file);
  555.       GetFileCount(); // NOTE: ^ inversion;
  556.       UpdateFavorites();
  557.     }
  558.  
  559.     private void FontSelectClick(
  560.       object sender, EventArgs e)
  561.     { FontSelect1(); }
  562.  
  563.     private void PageSetupClick(
  564.       object sender, EventArgs e)
  565.     { PageSetup1(); }
  566.  
  567.     // convert selection / collection
  568.     // to Args String Array and let
  569.     // PrintPreview(); Handle the rest.
  570.     private void PreviewClick(
  571.       object sender, EventArgs e)
  572.     {
  573.       if (listBox1.SelectedItems
  574.         .Count < 1) return;
  575.       GetArgs(sender);
  576.       PrintPreview1();
  577.     }
  578.  
  579.     /// <summary>
  580.     /// convert selection / collection
  581.     /// to Args String Array and let
  582.     /// PrintThis(); Handle the rest.
  583.     /// </summary>
  584.     /// <param name="sender"></param>
  585.     /// <param name="e"></param>
  586.     private void PrintThisClick(
  587.       object sender, EventArgs e)
  588.     {
  589.       if (listBox1.SelectedItems
  590.         .Count < 1) return;
  591.       GetArgs(sender);
  592.       PrintThis1();
  593.     }
  594.  
  595.     private void GetArgs(object sender)
  596.     {
  597.       var l = listBox1
  598.         .SelectedItems.Count + 2;
  599.       Args = new string[l];
  600.       Args[0] = PbPath;
  601.       Args[1] = sender.ToString();
  602.       for (var i = 2; i < l; ++i) Args[i] =
  603.         listBox1.SelectedItems[
  604.           i - 2].ToString();
  605.     }
  606.  
  607.     private void ExitClick(
  608.       object sender, EventArgs e)
  609.     {
  610.       if(MCD.YesNoDialog(PFR.Exit)) Close();
  611.     }
  612. #endregion File Menu Items
  613.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  614.  
  615.  
  616. #region Edit Menu Items
  617.  
  618.     #region NOTE:
  619.     // Several of the Main Menu Items are simply linked to the equivalent Context Menu Item. (And vise-versa)
  620.     #endregion
  621.  
  622.     private void SelectAllClick(
  623.       object sender, EventArgs e)
  624.     {
  625.       var c = listBox1.Items.Count;
  626.       listBox1.ClearSelected();
  627.       for (var i = c-1; i > -1; --i)
  628.         listBox1.SetSelected(i, true);
  629.     }
  630.  
  631.     // Send this selection to
  632.     // (or open) Smesa via CommandLine.
  633.     // uses existing 2nd Instance & MMF.
  634.     // a simple call to
  635.     // MCL.MyToolbox.RunApp(); But...
  636.     // ...make sure the args are right.
  637.     private void AddToEditorClick(
  638.       object sender, EventArgs e)
  639.     {
  640.       if (string.IsNullOrEmpty(EdPath)
  641.         || !File.Exists(EdPath)
  642.         || !MCD.YesNoDialog(PFR.Edit))
  643.         return;
  644.       // else 1 extra for EdPath itself.
  645.       var count = 1 + listBox1
  646.         .SelectedItems.Count;
  647.       var args  = new string[count];
  648.       var i = 0;
  649.       args[i++] = EdPath;
  650.       for ( ; i < count; ++i)
  651.         args[i] = listBox1
  652.           .SelectedItems[i - 1]
  653.             .ToString();
  654.       WaitCursor();
  655.       MCT.RunApp(args);
  656.       Thread.Sleep(1000);
  657.       ArrowCursor();
  658.     }
  659.  
  660.     /// <summary> Flip byte 8190 to 1
  661.     /// in Watched File:
  662.     /// "Editor.sme" on AppRoot dir
  663.     /// v4.8.2.81 23-Oct-2018
  664.     /// </summary>
  665.     /// <param name="sender"></param>
  666.     /// <param name="e"></param>
  667.     private void Switch2Editor(
  668.       object sender, EventArgs e)
  669.     {
  670.       if (!MCD.YesNoDialog(
  671.         PFR.GoEdit, "", 1)) return;
  672.       if (MyEditorIsRunning())
  673.       {
  674.         var f = Path.GetDirectoryName(
  675.           EdPath) + PFR.MMF1;
  676.         var n = Path
  677.           .GetFileNameWithoutExtension(f);
  678.         var args = new object[]
  679.         {
  680.           f, Smesa.EditorSize,
  681.           Smesa.Offset1 -2,
  682.           new Collection<byte> {1},
  683.           false, n, false
  684.         };
  685.         if (!MemoryMapper
  686.           .ReverseWriteBytes(args))
  687.             MessageBox.Show(PFR.Failed);
  688.       }
  689.       else Process.Start(EdPath);
  690.     }
  691.  
  692.     private bool MyEditorIsRunning()
  693.     {
  694.       return File.Exists(
  695.         Path.GetDirectoryName(
  696.           EdPath) + PFR.ME);
  697.     }
  698. #endregion Edit Menu Items
  699.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  700.  
  701.  
  702. #region View Menu Items
  703.  
  704.     // See PrintForm3.cs
  705.  
  706.     // v4.7.1.73 23-Sep-2018
  707.     private void OpenFolderClick(
  708.       object sender, EventArgs e)
  709.     {
  710.       var list = listBox1.SelectedItems;
  711.       if (list.Count < 1) return;
  712.       if (!MCD.YesNoDialog(PFR.Open)) return;
  713.       var launched = new Collection<string>();
  714.       var was  = MCT.Skip;
  715.       MCT.Skip = true;
  716.       foreach (var dir in list.Cast<string>()
  717.         .Select(Path.GetDirectoryName).Where(
  718.         dir => !string.IsNullOrEmpty(
  719.           dir) && Directory.Exists(dir)
  720.           && !launched.Contains(dir)))
  721.       { launched.Add(dir);
  722.         MCT.OpenFolder(dir); }
  723.       MCT.Skip = was;
  724.     }
  725.  
  726.     private void ShowInViewerClick(
  727.       object sender, EventArgs e)
  728.     {
  729.       if (listBox1.SelectedItems.Count < 1
  730.         || string.IsNullOrEmpty(PbPath)) return;
  731.       if ( !MCD.YesNoDialog(PFR.Show)) return;
  732.       var dir = Path.GetDirectoryName(PbPath);
  733.       if (string.IsNullOrEmpty(dir)
  734.         || !Directory.Exists(dir)) return;
  735.       var app = dir + PFR.Sme;
  736.       if (!File.Exists(app)) return;
  737.       // And Send to ShowMeForm
  738.       SharedWatcher // v4.8.3.84
  739.         .EnableRaisingEvents = false;
  740.       foreach (string file in
  741.         listBox1.SelectedItems)
  742.       { MCT.RunApp(file, string.Format(
  743.           "Viewing: {0}", file), app);
  744.         Thread.Sleep(800); }
  745.       SharedWatcher // ^ wait a sec.
  746.         .EnableRaisingEvents = true;
  747.     }
  748. #endregion View Menu Items
  749.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  750.  
  751.  
  752. #region Tools Menu Items
  753.  
  754.     private void AboutClick(
  755.       object sender, EventArgs e)
  756.     {
  757.       var args = new[]
  758.       {
  759.         AssemblyTitle,
  760.         AssemblyProduct,
  761.         AssemblyVersion,
  762.         AssemblyCopyright,
  763.         AssemblyCompany,
  764.         PFR.About
  765.       };
  766.       var colors = new Collection<Color>
  767.       {
  768.         listBox1.ForeColor,      
  769.         listBox1.BackColor,            
  770.         ForeColor, BackColor
  771.       };
  772.       MCD.AboutApp(colors, args);
  773.     }
  774.  
  775.     /// <summary> Uses a dummy file-exists
  776.     /// "flag" as a Universal Boolean:
  777.     /// AppRoot + "skip.sme" esta nada.
  778.     /// Exists == true : Not == false;
  779.     /// </summary>
  780.     /// <param name="sender"></param>
  781.     /// <param name="e"></param>
  782.     private void ToggleSkip(
  783.       object sender, EventArgs e)
  784.     {
  785.       var skip = MCT.Skip;
  786.       if (!MCD.YesNoDialog(PFR.Toggle))
  787.         SkipToggle.Checked = skip;
  788.       else // Test code that...
  789.       {   //  ...became a Feature!
  790.         Thread.Sleep(250);
  791.         MCT.Skip = !skip;
  792.         #region NOTE:
  793.         // ^ This tiny lil guy triggers
  794.         // the PBF2.SkipEventHandler and thus
  795.         // Resets the SkipToggle.Checked value.
  796.         // Even if Smesa.Preferences made the change!
  797.         #endregion
  798.       }
  799.     }
  800.  
  801.     private void InvertListClick(
  802.       object sender, EventArgs e)
  803.     {
  804.       if (!MCD.YesNoDialog(
  805.         PFR.Invert)) return;
  806.       var slct = listBox1.SelectedItems;
  807.       var temp = new Collection<object>();
  808.       foreach (var item in listBox1.Items)
  809.         temp.Add(item);
  810.       var last = temp.Count  -1;
  811.       listBox1.SelectedItems.Clear();
  812.       listBox1.Items.Clear();
  813.       for (var i = last; i > -1; --i)
  814.         listBox1.Items.Add(temp[i]);
  815.       foreach (var file in slct)
  816.         listBox1.SelectedItems.Add(file);
  817.     }
  818. #endregion Tools Menu Items
  819.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  820.  
  821.  
  822. #region Assembly Attribute Accessors
  823.  
  824.     public static string AssemblyTitle
  825.     {
  826.       get
  827.       {
  828.         var attributes = Assembly.GetExecutingAssembly()
  829.           .GetCustomAttributes(typeof(
  830.             AssemblyTitleAttribute), false);
  831.         if (attributes.Length > 0)
  832.         {
  833.           var titleAttribute =
  834.             (AssemblyTitleAttribute)attributes[0];
  835.           if (!string.IsNullOrEmpty(titleAttribute.Title))
  836.             return titleAttribute.Title;
  837.         }
  838.         return Path
  839.           .GetFileNameWithoutExtension(Assembly
  840.             .GetExecutingAssembly().CodeBase);
  841.       }
  842.     }
  843.  
  844.     public static string AssemblyVersion
  845.     {
  846.       get
  847.       {
  848.         return Assembly.GetExecutingAssembly()
  849.           .GetName().Version.ToString();
  850.       }
  851.     }
  852.  
  853. //    public string AssemblyDescription
  854. //    {
  855. //      get
  856. //      {
  857. //        var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
  858. //        return attributes.Length == 0 ?
  859. //          "" : ((AssemblyDescriptionAttribute)attributes[0]).Description;
  860. //      }
  861. //    }
  862.  
  863.     public static string AssemblyProduct
  864.     {
  865.       get
  866.       {
  867.         var attributes = Assembly.GetExecutingAssembly()
  868.           .GetCustomAttributes(typeof(
  869.             AssemblyProductAttribute), false);
  870.         return attributes.Length == 0 ?
  871.           "" : ((AssemblyProductAttribute)attributes[0]).Product;
  872.       }
  873.     }
  874.  
  875.     public static string AssemblyCopyright
  876.     {
  877.       get
  878.       {
  879.         var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
  880.         return attributes.Length == 0 ?
  881.           "" : ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
  882.       }
  883.     }
  884.  
  885.     public static string AssemblyCompany
  886.     {
  887.       get
  888.       {
  889.         var attributes = Assembly.GetExecutingAssembly()
  890.           .GetCustomAttributes(typeof(
  891.             AssemblyCompanyAttribute), false);
  892.         return attributes.Length == 0 ?
  893.           "" : ((AssemblyCompanyAttribute)attributes[0]).Company;
  894.       }
  895.     }
  896. #endregion  Assembly Attribute Accessors
  897.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  898.  
  899.  
  900. #region ALL TestCode Only Below Here
  901.  
  902.     private void TestCode4Click(
  903.       object sender, EventArgs e)
  904.     {
  905.       const string
  906.         cancel  = "Full Removal Cancelled!",
  907.         remove  = "Selected Files Removed.";
  908.       var which = !MCD.YesNoDialog(
  909.         PFR.ReSet, PFR.ReSetTtl, 1)
  910.           ? cancel : remove;
  911.       MessageBox.Show(which);
  912.  
  913.       //const string txt =
  914.       //  "For Lease or Let #4.";
  915.       //MessageBox.Show(txt);
  916.     }
  917.  
  918.     private void TestCode3Click(
  919.       object sender, EventArgs e)
  920.     {
  921.       const string txt =
  922.         "For Lease or Let #3.";
  923.       MessageBox.Show(txt);
  924.     }
  925.  
  926.     private void TestCode2Click(
  927.       object sender, EventArgs e)
  928.     {
  929.       // Testing Killed1.sme & Killed2.sme
  930.       // EventHandler and Kill Later.
  931.       var d1 = listBox1.SelectedItems;
  932.       var df = new StringCollection();
  933.       foreach (string s in d1) df.Add(s);
  934.  
  935.       // Test itself on Killed1 Handling.
  936.       var fn = WriteShared
  937.         .AppRoot + PFR.KillNow;
  938.       if (File.Exists(fn)) File.Delete(fn);
  939.       var ar = MCT.GetCommonArgs(fn,
  940.         new Collection<string>(
  941.           df.Cast<string>().ToList()));
  942.       if (!MemoryMapper
  943.         .WriteStrings(ar)) throw new
  944.           FileLoadException(
  945.             "Not Written!");
  946.  
  947.       // Test itself on Killed2 Handling.
  948.       Thread.Yield();
  949.       WriteShared.KillLater(df);
  950.       if (File.Exists(fn)) File.Delete(fn);
  951.  
  952.       //const string txt =
  953.       //  "For Lease or Let #2.";
  954.       //MessageBox.Show(txt);
  955.     }
  956.  
  957.     // MemoryMapper Class TesterCode;
  958.     private void TestCode1Click(
  959.       object sender, EventArgs e)
  960.     {
  961.       //\\//\\//\\//\\
  962.       //  Strings \\//
  963.       //\\//\\//\\//\\
  964.  
  965.       var strings=
  966.         new Collection<string>
  967.         {"Testing strings, test test test.","Hello World!","String 3","4","string 5","6 string","string 7","The 8th String","The bottom of the 9th!","10th string","string #11","thfsahf fshfeeifj jkljfksdjkjf ieiiif jfkdkls l\n\nhahahah love it akfjsakjei sjk hskshf fjksdhfhue\n\nHeheh just testing a long string here.","snowball","testcode","15","16 string","string 17","string 18","19th string","Tada string #20!\nskjdfsk fjfidowijwei fkdsjk fjkdsjie jfkdjs;al;sjfj jfijw0ew slfklks'; jfdsasp[df fdks jgjkszllkshfhdhskh hfjksh hfjs sksdhf jhsue hfjdsk fhue\n\njskkjfj jfidosijfk jlksdjkhf -JpE-"};
  968.  
  969.       var dir = Path.GetDirectoryName(
  970.         Data[0].ToString());
  971.       var args=
  972.         new object[]
  973.         { dir + "\\testStrings.sme",
  974.           0, 0, strings, true, "testMmf", true};
  975.       // Faked args & strings ^^^^^^^^^^^^^^
  976.       if (File.Exists(args[0].ToString()))
  977.         File.Delete(args[0].ToString());
  978.       // So now we can test it
  979.       if (MCD.YesNoDialog(
  980.         "Test Write Strings()?"))
  981.           MemoryMapper.WriteStrings(args);
  982.       if (MCD.YesNoDialog(
  983.         "Test Read Strings()?") )
  984.       { var coun = 0;
  985.         var read = MemoryMapper.ReadStrings(
  986.           args).ToList();
  987.         read.Sort(); // Watch This Result!
  988.         MessageBox.Show(read.Aggregate(
  989.           "",(current,s)=>current+string.Format(
  990.             "String#{0}: {1}\n",++coun,s)));}
  991.  
  992.  
  993.       //\\//\\//\\//
  994.       //  Bytes //\\
  995.       //\\//\\//\\//
  996.  
  997.       args[0] = dir + "\\testBytes.sme";
  998.       if (File.Exists(args[0].ToString()))
  999.         File.Delete(args[0].ToString());
  1000.       args[3] = new Collection<byte>
  1001.         {1,2,3,4,50,255,128,43,17,14,4,21,
  1002.           27,120,254,0,253,0,99,2,25};
  1003.       args[5] = "ByteSwitch";
  1004.       if (MCD.YesNoDialog(
  1005.         "Test out Write Bytes?"))
  1006.           MemoryMapper.WriteBytes(args);
  1007.       if (MCD.YesNoDialog(
  1008.         "Test out Read Bytes?"))
  1009.       {
  1010.         var x = MemoryMapper
  1011.           .ReadBytes(args).ToList();
  1012.         x.Sort();
  1013.         var t = x.Aggregate("",
  1014.           (current, b) => current +
  1015.             string.Format("{0}, ", b));
  1016.         t = t.Remove(t.Length-2);
  1017.         t = t.Normalize();
  1018.         MessageBox.Show(string.Format(
  1019.           "Sorted List of Bytes:" +
  1020.           " (0-255 only)\n\n{0}", t ));
  1021.       }
  1022.  
  1023.  
  1024.       //\\//\\//\\//\\
  1025.       //   Int64  \\//  <-= Holds so much More!
  1026.       //\\//\\//\\//\\
  1027.  
  1028.       args[0] = dir + "\\testPointers.sme";
  1029.       if (File.Exists(args[0].ToString()))
  1030.         File.Delete(args[0].ToString());
  1031.       args[5] = "mmf17";
  1032.       args[3] = new List<Int64>
  1033.       {
  1034.         39876, -2134598, 398764231087432, 0, 1,
  1035.         -11, 64, 62, 3600, 18, 16, 14, 15, 13,
  1036.         -19, 21, 1956, 1234567890, 128, 9001,
  1037.         9002, -69, 75, 41, 2, 56, 3, -56, -2,
  1038.         11, 13298436567129, -10, 12, 29, -17,
  1039.         10, -100200300400500, 17, 37, 2018,
  1040.         97, -1, 98, 96, -3451065487, -3, -4, 111
  1041.       };
  1042.       #region Noted Details.
  1043.  
  1044.       // Fake data for testing ^ ^ ^
  1045.       // ulong max value 18,446,744,073,709,551,615!
  1046.       // That's: 18 quintillion, 446 quadrillion,
  1047.       // 744 trillion, 73 billion, 709 million,
  1048.       // 551 thousand, 615. (20 digits)
  1049.       //Int64() methods range is:
  1050.       // -9223372036854775808 through:
  1051.       // +9223372036854775807
  1052.  
  1053.       #endregion
  1054.       if (MCD.YesNoDialog(
  1055.         "Test out Write longs?"))
  1056.           MemoryMapper.WriteInt64(args);
  1057.  
  1058.       if (!MCD.YesNoDialog(
  1059.         "Test out Read longs?")) return;
  1060.       var result = MemoryMapper
  1061.         .ReadInt64(args).ToList();
  1062.       result.Sort();
  1063.       var long1S = result.Aggregate(
  1064.         "", (current, l) => current +
  1065.           string.Format("{0}, ", l));
  1066.       long1S = long1S.Remove(long1S.Length-2);
  1067.       long1S = long1S.Normalize();
  1068.  
  1069.       MessageBox.Show(string.Format(
  1070.         "\nMin long Value: {0}" +
  1071.         "\nMax long Value: {1}" +
  1072.         "\n\nSorted List of Int64:\n{2}",
  1073.         long.MinValue, long.MaxValue, long1S));
  1074.     }
  1075.    
  1076.     // Space for Rent or Lease, nothing down!
  1077.  
  1078.  
  1079. #endregion TestCode Only
  1080.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  1081.  
  1082.  
  1083. #region Scrap Pile
  1084.  
  1085.       //var file = Path
  1086.       //  .GetDirectoryName(PbPath)
  1087.       //  + "\\" + MCT.SharedFile;
  1088.       //var waz = new Collection<string>{"Test"};
  1089.       //var bc = BackColor;
  1090.       //var data = WriteShared
  1091.       //  .GetData(file, waz, bc);
  1092.       //var args = ShowMe.ShowMeForm
  1093.       //  .GetSharedArgs(file, data);
  1094.       //args[6] = true;
  1095.       //MMF.ReadStrings(args);
  1096.       ////Thread.Sleep(333);
  1097.       //MMF.WriteStrings(args);
  1098.  
  1099.   //private static bool TestSkip()
  1100.   //{
  1101.   //  var sk = MCT.Skip;
  1102.   //  MCT.Skip = false;
  1103.   //  if (MCD.YesNoDialog(
  1104.   //    "Test Get Skip?"))
  1105.   //    for (var x = 1; x < 250; ++x)
  1106.   //    {
  1107.   //      Thread.Sleep(125);
  1108.   //      MCT.Skip = !MCT.Skip;
  1109.   //    }
  1110.   //  else
  1111.   //  {
  1112.   //    MCT.Skip = sk;
  1113.   //    return false;
  1114.   //  }
  1115.   //  MCT.Skip = sk;
  1116.   //  return true;
  1117.   //}
  1118.  
  1119.     //MCT.Skip = false;
  1120.     //if (MCD.YesNoDialog("Test Set Skip?"))
  1121.     //  for (var i = 1; i < 250; ++i)
  1122.     //  {
  1123.     //    Thread.Sleep(250);
  1124.     //    MCT.Skip = !IsEven(i);
  1125.     //    #region Release Test Code
  1126.     //    //MessageBox.Show(
  1127.     //    //  string.Format(
  1128.     //    //    "MCT.Skip is Set to {0}",
  1129.     //    //    MCT.Skip));
  1130.     //    #endregion
  1131.     //  }
  1132.     //else
  1133.     //{
  1134.     //  MCT.Skip = sk;
  1135.     //  return false;
  1136.     //}
  1137.     //MCT.Skip = sk;
  1138.     //return true;
  1139.   //}
  1140.  
  1141.     //private static bool IsEven(int i)
  1142.     //{
  1143.     //  decimal even = i;
  1144.     //  // ReSharper disables PossibleLossOfFraction
  1145.     //  return even/2 == i/2;
  1146.     //  // ReSharper restore PossibleLossOfFraction
  1147.     //}
  1148.     //// that's my ^ point, it's uneven!
  1149.  
  1150. #endregion
  1151.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  1152.  
  1153.  
  1154.   }
  1155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement