Advertisement
GlobalAccessSoftware

PrintFormContext.cs

Apr 26th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.92 KB | None | 0 0
  1.  
  2.  
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  4. // PrintForm3.cs 30-Apr v4.3.5.51n
  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. // Perfected 11-May-2018
  22. // v4.4.5.52 Finally Fixes it RIGHT!
  23. // v4.9.3.87 14-Nov-2018 Removals.
  24. // v4.9.4.88 17-Nov Evolved touches.
  25. // v4.9.4.90 27-Nov-2018 ReadRemovals() and
  26. // GetSelectedIndex() integration completed.
  27. // NOTE: See calls to GetSelectedIndex();
  28. // v4.9.4.91 01-Dec-2018 Integration ^
  29. // v4.9.4.92 04-Dec-2018 Killed.sme Mods.
  30. // v4.9.5.93 08-Dec-2018 ME.WriteShared
  31. // for Killed file passback to Blaster
  32. // v4.9.5.94 10-Dec Polish and Read Thru.
  33. // v4.9.5.94 13-Dec-2018 ME2PB Killed 1 & 2
  34. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  35.  
  36. using System;
  37. using System.Collections.ObjectModel;
  38. using System.Collections.Specialized;
  39. using System.IO;
  40. using System.Linq;
  41. using System.Text;
  42. using System.Threading;
  43. using System.Windows.Forms;
  44. using System.Collections.Generic;
  45.  
  46. // Class Aliases
  47. using MyCustomLibrary;
  48. using MyEditor;
  49. using MCD = MyCustomLibrary.Dialogs;
  50. using MCT = MyCustomLibrary.MyToolbox;
  51. // Property Aliases
  52. using PFR = PrintBlaster.Properties.Resources;
  53. using PFS = PrintBlaster.Properties.Settings;
  54.  
  55. namespace PrintBlaster
  56. {
  57.   public partial class PrintForm1
  58.   {
  59.     #region NOTE:
  60.     // Several of the Context Menu items are simply sent to the existing Menu item handler. (And vise-versa.)
  61.     #endregion
  62.  
  63. #region Up/Down Method Group
  64.  
  65.     /// <summary> This Method calls all
  66.     /// others in the below Method Group.
  67.     /// They've been split-out to
  68.     /// simplify this method's reading
  69.     /// and compartmentalize the tasking.
  70.     /// Perfected 11-May-2018 v4.4.5.52
  71.     /// </summary>
  72.     /// <param name="sender"></param>
  73.     /// <param name="e"></param>
  74.     private void MoveClick(
  75.       object sender, EventArgs e)
  76.     {
  77.       var dir = GetDirectional(sender);
  78.       if (dir.Equals(0)) return;// err handled.
  79.       var ct = listBox1.SelectedItems.Count;
  80.       var indices = new int[ct];
  81.       var items = new string[ct];
  82.       GetArrays(ref indices, ref items);
  83.       var  up  = dir < 0;
  84.       var last = listBox1.Items.Count - 1;
  85.         // v if it's Up from the top...
  86.       if (up && indices.Contains(0)
  87.         // v ...or if it's Down from the bottom:
  88.         || !up && indices.Contains(last))
  89.           QuickMove(up);// Wrap around!
  90.       else // <-= no indices contain 0 OR last!
  91.         MoveSelected(indices, items, up);
  92.       // then reSelect previously selected items.
  93.       for (var i = 0; i < ct; ++i)
  94.         listBox1.SetSelected(
  95.           listBox1.Items.IndexOf(items[i]), true);
  96.     }
  97.  
  98.     // Nice when wrapping is easier!
  99.     private void QuickMove(bool up)
  100.     {
  101.       if (up)
  102.       { // from the top...
  103.         var item = listBox1.Items[0];
  104.         listBox1.Items.RemoveAt(0);
  105.         listBox1.Items.Add(item);
  106.         // ^ ...to the bottom.
  107.       }
  108.       else
  109.       { // Or from the bottom...
  110.         var last = listBox1.Items.Count - 1;
  111.         var item = listBox1.Items[last];
  112.         listBox1.Items.RemoveAt(last);
  113.         listBox1.Items.Insert(0, item);
  114.       } // ...to the top! (Swap)
  115.     }
  116.  
  117.     // ^ thus Nothing is top or bottom here!
  118.     private void MoveSelected(
  119.       IList<int> indices,
  120.       IList<string> items, bool up)
  121.     {
  122.       if (up)
  123.       { // Move em Up...
  124.         var c = listBox1
  125.           .SelectedItems.Count;
  126.         for (var i = 0; i < c; ++i)
  127.         { // Highest 1st For Up   ^
  128.           var item = items[i];
  129.           var to = indices[i];
  130.           listBox1.Items.Remove(item);
  131.           listBox1.Items.Insert(--to, item);
  132.         }
  133.       } else { // v if Down...
  134.         var last = listBox1.Items.Count - 1;
  135.         for (var i = listBox1.SelectedItems
  136.           .Count -1; i > -1; --i)// <-count down
  137.         { // Lowest first For ^ Down!
  138.           var item = items[i];
  139.           var jp = indices[i];
  140.           listBox1.Items.Remove(item);
  141.           if (++jp < last) listBox1.Items
  142.             .Insert(jp, item);
  143.           else // it's last. (Add last)
  144.             listBox1.Items.Add(item);
  145.         }
  146.       }
  147.       Validate();
  148.     }
  149.  
  150.     private static int
  151.       GetDirectional(object sender)
  152.     {
  153.       var dir = 0; // dir == direction;
  154.       var str = Convert.ToString(sender);
  155.       if (str.Contains("Up")) dir = -1;
  156.       else if (str.Contains("Down")) dir = 1;
  157.       return dir;
  158.     }
  159.  
  160.     private void GetArrays(// indirectional
  161.       ref int[] indices, ref string[] items)
  162.     {
  163.       var ct = listBox1.SelectedItems.Count;
  164.       try
  165.       {
  166.         for (var i = 0; i < ct; ++i)
  167.         {
  168.           indices[i] = Convert.ToInt32(
  169.             listBox1.SelectedIndices[i]);
  170.           items[i] = listBox1
  171.             .SelectedItems[i].ToString();
  172.         }
  173.       }
  174.       catch (Exception e)
  175.       {
  176.         if (!MCT.Testing) return;
  177.         MessageBox.Show(e.ToString());
  178.         throw new InvalidCastException(
  179.           "Get Arrays");
  180.       }
  181.     }
  182. #endregion Up/Down Method Group
  183.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  184.  
  185.  
  186. #region Favorites Mods from PB to ME
  187.  
  188.     private void RemoveFromListClick(
  189.       object sender, EventArgs e)
  190.     {
  191.       if (!MCD.YesNoDialog(
  192.         PFR.Remove)) return;
  193.       var s = listBox1.SelectedItems;
  194.       var z = new StringCollection();
  195.       foreach (var w in from object o in s
  196.         select Convert.ToString(o)
  197.         into w where !string.IsNullOrEmpty(w)
  198.         && File.Exists(w)
  199.         && !z.Contains(w) select w) z.Add(w);
  200.       // REM: Remove from Bottom up.
  201.       for (var i = s.Count - 1; i > -1; --i)
  202.         listBox1.Items.Remove(s[i]);
  203.       var n = z.Cast<string>().ToList();
  204.       var t = new Thread(
  205.         ReSetFavoritesSetting);
  206.       t.Start(n);
  207.     }
  208.  
  209.     private void DeleteFromDiskClick(
  210.       object sender, EventArgs e)
  211.     {
  212.       if (!MCD.YesNoDialog(
  213.         PFR.Delete)) return;
  214.       var was = MCT.Skip;
  215.       MCT.Skip = false;
  216.       if (!MCD.YesNoDialog(PFR.Sure))
  217.       { MCT.Skip = was;
  218.         return; }
  219.       MCT.Skip = was;
  220.       var s = listBox1.SelectedItems;
  221.       var z = new StringCollection();
  222.       foreach (var w in from object o in s
  223.         select Convert.ToString(o) into w
  224.         where !string.IsNullOrEmpty(w)
  225.         && File.Exists(w) // v4.9.3.87
  226.         && !z.Contains(w) // <======<<
  227.         select w) z.Add(w); // Nice!
  228.       for (var i = s.Count-1; i > -1; --i)
  229.       { if (!MCT.Testing)
  230.           File.Delete(s[i].ToString());
  231.         listBox1.Items.Remove(s[i]); }
  232.       var n = z.Cast<string>().ToList();
  233.       var t = new Thread(
  234.         ReSetFavoritesSetting);
  235.       t.Start(n);
  236.     }
  237.  
  238.     // Have this worker thread send
  239.     // a mmf msg to ME telling it the file
  240.     // Removals for ResetFavorites(), but
  241.     // if ME not running, add to Removals.
  242.     private void
  243.       ReSetFavoritesSetting(object n)
  244.     {
  245.       var fn = Path.GetDirectoryName(
  246.         EdPath) + PFR.Favs;
  247.       var  list = n as Collection<string>;
  248.       // handle both remove AND delete.
  249.       if ( list == null
  250.         || list.Count < 1
  251.         || !File.Exists(fn)
  252.         || !MCT.RemoveFromFavoritesFile(
  253.           list, fn) // v4.9.4.91
  254.         || !MCD.YesNoDialog(
  255.           PFR.ReSet, PFR.ReSetTtl, 1)
  256.         ) return;
  257.       // else let's getter done.0
  258.       if (MyEditorIsRunning()) ReSet2(list);
  259.       else AppendToRemovals(list);
  260.       // Worker thread requires. v
  261.       if (CountLabel2.InvokeRequired)
  262.         Invoke(new Invoker1(GetFileCount));
  263.       else GetFileCount();
  264.     }
  265.  
  266.     private void ReSet2(
  267.       Collection<string> list)
  268.     {
  269.       var file = Path.GetDirectoryName(
  270.         EdPath)+ PFR.MMF1;
  271.       var name = Path.
  272.         GetFileNameWithoutExtension(file);
  273.       var args = new object[]
  274.       {
  275.         file, PFS.Default.EditorSize,
  276.         PFS.Default.EditorOffset2,
  277.         list, true, name, false, // Uses...
  278.         PFS.Default.EditorView2 //<-= ALL 8
  279.       };
  280.       MemoryMapper.WriteStrings(args);
  281.     }
  282.  
  283.     private void GetFileCount()
  284.     {
  285.       CountLabel2.Text =
  286.         Convert.ToString(
  287.           listBox1.Items.Count);
  288.       CountLabel2.Text =
  289.         CountLabel2.Text.PadLeft(4);
  290.     }
  291.  
  292.     // NOW: When MyEditor is NOT Running...
  293.     // do it this way in case they accumulate.
  294.     // (Named like a mmf, but it's a text file.)
  295.     // v4.9.4.88RC 17-Nov-2018
  296.     private void AppendToRemovals(
  297.       IList<string> list)
  298.     {
  299.       if (list == null
  300.         ||list.Count < 1) return;
  301.       var path  = Path.GetDirectoryName(
  302.         EdPath) + PFR.Removals;
  303.       if (File.Exists(path))
  304.       {
  305.         var coun = list.Count;
  306.         var copy = new string[coun];
  307.         for (var i = 0; i < coun; ++i)
  308.           copy[i] = list[i];
  309.         var removes = File.ReadAllLines(
  310.           path, Encoding.Unicode);
  311.         // When already in there forget it.
  312.         foreach (var file in copy.Where(
  313.           file => removes.Contains(file)
  314.           && File.Exists(file)))
  315.             list.Remove(file);
  316.       }
  317.       if (list.Count > 0)
  318.         File.AppendAllLines(
  319.           path, list, Encoding.Unicode);
  320.     }
  321. #endregion Favorites Mods from PB to ME
  322.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  323.  
  324.  
  325. #region Favorites Mods from ME to PB
  326.  
  327.     /// <summary> For Removals while RUNNING
  328.     /// Checks for List of Strings
  329.     /// Passed in via the mmf Killed1.sme
  330.     /// from MyEditor.WriteShared.Kill1();
  331.     /// Calls Reset with List of files
  332.     /// to Remove from listBox1 & Settings.
  333.     /// </summary>
  334.     private void SetupKilledWatcher()
  335.     {
  336.       KilledWatcher.NotifyFilter =
  337.         NotifyFilters.FileName;
  338.       KilledWatcher.InternalBufferSize  = 8192;
  339.       KilledWatcher.EnableRaisingEvents = false;
  340.         KilledWatcher.Path = WriteShared.AppRoot;
  341.       KilledWatcher.Filter =
  342.         Path.GetFileName(PFR.KillNow);
  343.       KilledWatcher.Created +=
  344.         KilledEventHandler;
  345.       KilledWatcher.EnableRaisingEvents = true;
  346.     }
  347.  
  348.     /// <summary> Go ahead and handle Kills:
  349.     /// (above) Removing them from listBox1
  350.     /// and the corresponding Setting.
  351.     /// REM: Favorites.sme data file
  352.     /// has already been taken care of.
  353.     /// (Worker Thread) Thread Safe,
  354.     /// File-Safe via MMF Class I wrote.
  355.     /// </summary>
  356.     /// <param name="sender"></param>
  357.     /// <param name="e"></param>
  358.     private void KilledEventHandler(
  359.       object sender, FileSystemEventArgs e)
  360.     {
  361.       var file = WriteShared
  362.         .AppRoot + PFR.KillNow;
  363.       if (string.IsNullOrEmpty(file)
  364.         || !File.Exists(file)) throw new
  365.           ArgumentException(PFR.Err6);
  366.       var args =
  367.         new object[]
  368.         {
  369.           file, 0, 0, null, true, Path.
  370.           GetFileNameWithoutExtension(file),
  371.           true // <-= Delete v file once read.
  372.         };
  373.       var  data = MemoryMapper.ReadStrings(args);
  374.       if ( data == null
  375.         || data.Count < 1) return;
  376.       if (listBox1.InvokeRequired)
  377.         Invoke(new Invoker2(
  378.            KillFileNames), data);
  379.       else KillFileNames(  data);
  380.       // REM: Favorites.sme and MyEditor...
  381.       // Setting, both handled by that side.
  382.     }
  383.  
  384.     /// <summary> (worker) Handles Killed
  385.     /// Files from Settings for file list
  386.     /// sent from MyEditor, Using file
  387.     /// system Watcher & Eventhandler
  388.     /// for Killed1.sme and method group.
  389.     /// NOT TO BE CONFUSED W/ Favorites.sme
  390.     /// Which is already handled elsewhere.
  391.     /// That goes from PB2ME and this one
  392.     /// goes from ME2PB.
  393.     /// v4.9.4.92 06-Dec-2018 by -JpE-
  394.     /// </summary>
  395.     /// <param name="parms"></param>
  396.     private void KillFileNames(object parms)
  397.     {
  398.       using (new Mutex(false, PFR.Killer))
  399.       {
  400.         if (parms == null) throw new
  401.           ArgumentException(PFR.Err4);
  402.         var data = parms as Collection<string>;
  403.         if (data == null
  404.             || data.Count < 1) return;
  405.         // REM: Direct to OverLoad below.
  406.         RoadKill(data);
  407.       }
  408.     }
  409.  
  410.     /// <summary> Checks for Killed2.sme Files
  411.     /// on Launch When Blaster was NOT Running:
  412.     /// Similar to the above but not; as this 1
  413.     /// While the above handles it when running.
  414.     /// (Reads Killed2.sme File on Launch)
  415.     /// </summary>
  416.     private void RoadKill()
  417.     { // Executes on Startup Only!
  418.       var dir = WriteShared.AppRoot;
  419.       using (File.Create(dir + PFR.PB)){}
  420.       // Set PB is Now Running flag!  ^
  421.       var fn = dir + PFR.KillLater;// |
  422.       if (!File.Exists(fn)) return;
  423.       var files = File.ReadAllLines(
  424.         fn, Encoding.Unicode);
  425.       if (files.Length < 1) return;
  426.       // Now, just "getter" Done.
  427.       RoadKill(files);
  428.       if (File.Exists(fn)) File.Delete(fn);
  429.     }
  430.  
  431.     /// <summary> OverLoad for special Call
  432.     /// from KillFileNames();
  433.     /// For better ReUse of TestedCode.
  434.     /// </summary>
  435.     /// <param name="files"></param>
  436.     private void RoadKill(
  437.       IEnumerable<string> files)
  438.     {
  439.       using (new Mutex(false, PFR.RoadKill))
  440.       {
  441.         foreach (var file in files)
  442.         {
  443.           if (PFS.Default.Files.Contains(file))
  444.             PFS.Default.Files.Remove(file);
  445.           if (listBox1 == null
  446.               || listBox1.Items.Count < 1) break;
  447.           if (listBox1.Items.Cast<string>()
  448.             .Contains(file))
  449.             listBox1.Items.Remove(file);
  450.         }
  451.         PFS.Default.Save();
  452.       }
  453.     }
  454. #endregion Favorites Mods from ME to PB
  455.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  456.  
  457.  
  458.   }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement