openpdn /src/Menus/FileMenu.cs

Language C# Lines 510
MD5 Hash 1949eba409de5de3be79de6d741c3fea Estimated Cost $9,593 (why?)
Repository https://bitbucket.org/tuldok89/openpdn View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using PaintDotNet.Actions;
using PaintDotNet.Base;
using PaintDotNet.SystemLayer;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace PaintDotNet.Menus
{
    internal sealed class FileMenu
        : PdnMenuItem
    {
        private PdnMenuItem _menuFileNew;
        private PdnMenuItem _menuFileOpen;
        private PdnMenuItem _menuFileOpenRecent;
        private PdnMenuItem _menuFileOpenRecentSentinel;
        private PdnMenuItem _menuFileAcquire;
        private PdnMenuItem _menuFileAcquireFromScannerOrCamera;
        private PdnMenuItem _menuFileClose;
        private ToolStripSeparator _menuFileSeparator1;
        private PdnMenuItem _menuFileSave;
        private PdnMenuItem _menuFileSaveAs;
        private ToolStripSeparator _menuFileSeparator2;
        private PdnMenuItem _menuFilePrint;
        private ToolStripSeparator _menuFileSeparator3;
        private PdnMenuItem _menuFileViewPluginLoadErrors;
        private ToolStripSeparator _menuFileSeparator4;
        private PdnMenuItem _menuFileExit;

        private bool OnCtrlF4Typed(Keys keys)
        {
            _menuFileClose.PerformClick();
            return true;
        }

        public FileMenu()
        {
            PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.F4, OnCtrlF4Typed);
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            _menuFileNew = new PdnMenuItem();
            _menuFileOpen = new PdnMenuItem();
            _menuFileOpenRecent = new PdnMenuItem();
            _menuFileOpenRecentSentinel = new PdnMenuItem();
            _menuFileAcquire = new PdnMenuItem();
            _menuFileAcquireFromScannerOrCamera = new PdnMenuItem();
            _menuFileClose = new PdnMenuItem();
            _menuFileSeparator1 = new ToolStripSeparator();
            _menuFileSave = new PdnMenuItem();
            _menuFileSaveAs = new PdnMenuItem();
            _menuFileSeparator2 = new ToolStripSeparator();
            _menuFilePrint = new PdnMenuItem();
            _menuFileSeparator3 = new ToolStripSeparator();
            _menuFileViewPluginLoadErrors = new PdnMenuItem();
            _menuFileSeparator4 = new ToolStripSeparator();
            _menuFileExit = new PdnMenuItem();
            //
            // FileMenu
            //
            DropDownItems.AddRange(GetMenuItemsToAdd(true));
            Name = "Menu.File";
            Text = PdnResources.GetString("Menu.File.Text");
            // 
            // menuFileNew
            // 
            _menuFileNew.Name = "New";
            _menuFileNew.ShortcutKeys = Keys.Control | Keys.N;
            _menuFileNew.Click += MenuFileNewClick;
            // 
            // menuFileOpen
            // 
            _menuFileOpen.Name = "Open";
            _menuFileOpen.ShortcutKeys = Keys.Control | Keys.O;
            _menuFileOpen.Click += MenuFileOpenClick;
            // 
            // menuFileOpenRecent
            // 
            _menuFileOpenRecent.Name = "OpenRecent";
            _menuFileOpenRecent.DropDownItems.AddRange(
                new ToolStripItem[] 
                {
                    _menuFileOpenRecentSentinel
                });
            _menuFileOpenRecent.DropDownOpening += MenuFileOpenRecentDropDownOpening;
            // 
            // menuFileOpenRecentSentinel
            // 
            _menuFileOpenRecentSentinel.Text = "sentinel";
            // 
            // menuFileAcquire
            // 
            _menuFileAcquire.Name = "Acquire";
            _menuFileAcquire.DropDownItems.AddRange(
                new ToolStripItem[] 
                {
                    _menuFileAcquireFromScannerOrCamera
                });
            _menuFileAcquire.DropDownOpening += MenuFileAcquireDropDownOpening;
            // 
            // menuFileAcquireFromScannerOrCamera
            // 
            _menuFileAcquireFromScannerOrCamera.Name = "FromScannerOrCamera";
            _menuFileAcquireFromScannerOrCamera.Click += MenuFileAcquireFromScannerOrCameraClick;
            //
            // menuFileClose
            //
            _menuFileClose.Name = "Close";
            _menuFileClose.Click += MenuFileCloseClick;
            _menuFileClose.ShortcutKeys = Keys.Control | Keys.W;
            // 
            // menuFileSave
            // 
            _menuFileSave.Name = "Save";
            _menuFileSave.ShortcutKeys = Keys.Control | Keys.S;
            _menuFileSave.Click += MenuFileSaveClick;
            // 
            // menuFileSaveAs
            // 
            _menuFileSaveAs.Name = "SaveAs";
            _menuFileSaveAs.ShortcutKeys = Keys.Control | Keys.Shift | Keys.S;
            _menuFileSaveAs.Click += MenuFileSaveAsClick;
            // 
            // menuFilePrint
            // 
            _menuFilePrint.Name = "Print";
            _menuFilePrint.ShortcutKeys = Keys.Control | Keys.P;
            _menuFilePrint.Click += MenuFilePrintClick;
            //
            // menuFileViewPluginLoadErrors
            //
            _menuFileViewPluginLoadErrors.Name = "ViewPluginLoadErrors";
            _menuFileViewPluginLoadErrors.Click += MenuFileViewPluginLoadErrorsClick;
            // 
            // menuFileExit
            // 
            _menuFileExit.Name = "Exit";
            _menuFileExit.Click += MenuFileExitClick;
        }

        private ToolStripItem[] GetMenuItemsToAdd(bool includeLoadErrors)
        {
            var items = new List<ToolStripItem>
                            {
                                _menuFileNew,
                                _menuFileOpen,
                                _menuFileOpenRecent,
                                _menuFileAcquire,
                                _menuFileClose,
                                _menuFileSeparator1,
                                _menuFileSave,
                                _menuFileSaveAs,
                                _menuFileSeparator2,
                                _menuFilePrint,
                                _menuFileSeparator3
                            };

            if (includeLoadErrors)
            {
                items.Add(_menuFileViewPluginLoadErrors);
                items.Add(_menuFileSeparator4);
            }

            items.Add(_menuFileExit);

            return items.ToArray();
        }

        private static List<Triple<Assembly, Type, Exception>> RemoveDuplicates(IEnumerable<Triple<Assembly, Type, Exception>> allErrors)
        {
            // Exception has reference identity, but we want to collate based on the message contents

            var internedList = new Set<Triple<Assembly, Type, string>>();
            var noDupesList = new List<Triple<Assembly, Type, Exception>>();

            foreach (Triple<Assembly, Type, Exception> t in allErrors)
            {
                Triple<Assembly, Type, string> interned = Triple.Create(
                    t.First, t.Second, string.Intern(t.Third.ToString()));

                if (internedList.Contains(interned)) continue;
                internedList.Add(interned);
                noDupesList.Add(t);
            }

            return noDupesList;
        }

        private void MenuFileViewPluginLoadErrorsClick(object sender, EventArgs e)
        {
            IList<Triple<Assembly, Type, Exception>> allErrors = AppWorkspace.GetEffectLoadErrors();
            IList<Triple<Assembly, Type, Exception>> errors = RemoveDuplicates(allErrors);

            using (var errorsDialog = new Form())
            {
                errorsDialog.Icon = Utility.ImageToIcon(PdnResources.GetImageResource("Icons.MenuFileViewPluginLoadErrorsIcon.png").Reference);
                errorsDialog.Text = PdnResources.GetString("Effects.PluginLoadErrorsDialog.Text");

                var messageLabel = new Label
                                       {
                                           Name = "messageLabel",
                                           Text = PdnResources.GetString("Effects.PluginLoadErrorsDialog.Message.Text")
                                       };

                var errorsBox = new TextBox();
                errorsBox.Font = new Font(FontFamily.GenericMonospace, errorsBox.Font.Size);
                errorsBox.ReadOnly = true;
                errorsBox.Multiline = true;
                errorsBox.ScrollBars = ScrollBars.Vertical;

                var allErrorsText = new StringBuilder();
                string headerTextFormat = PdnResources.GetString("EffectErrorMessage.HeaderFormat");

                for (int i = 0; i < errors.Count; ++i)
                {
                    Assembly assembly = errors[i].First;
                    Type type = errors[i].Second;
                    Exception exception = errors[i].Third;

                    string headerText = string.Format(headerTextFormat, i + 1, errors.Count);
                    string errorText = AppWorkspace.GetLocalizedEffectErrorMessage(assembly, type, exception);

                    allErrorsText.Append(headerText);
                    allErrorsText.Append(Environment.NewLine);
                    allErrorsText.Append(errorText);

                    if (i != errors.Count - 1)
                    {
                        allErrorsText.Append(Environment.NewLine);
                    }
                }

                errorsBox.Text = allErrorsText.ToString();

                errorsDialog.Layout +=
                    delegate
                        {
                        int hMargin = UI.ScaleWidth(8);
                        int vMargin = UI.ScaleHeight(8);
                        int insetWidth = errorsDialog.ClientSize.Width - (hMargin * 2);

                        messageLabel.Location = new Point(hMargin, vMargin);
                        messageLabel.Width = insetWidth;
                        messageLabel.Size = messageLabel.GetPreferredSize(new Size(messageLabel.Width, 1));

                        errorsBox.Location = new Point(hMargin, messageLabel.Bottom + vMargin);
                        errorsBox.Width = insetWidth;
                        errorsBox.Height = errorsDialog.ClientSize.Height - vMargin - errorsBox.Top;
                    };

                errorsDialog.StartPosition = FormStartPosition.CenterParent;
                errorsDialog.ShowInTaskbar = false;
                errorsDialog.MinimizeBox = false;
                errorsDialog.Width *= 2;
                errorsDialog.Size = UI.ScaleSize(errorsDialog.Size);
                errorsDialog.Controls.Add(messageLabel);
                errorsDialog.Controls.Add(errorsBox);

                errorsDialog.ShowDialog(AppWorkspace);
            }
        }

        protected override void OnDropDownOpening(EventArgs e)
        {
            DropDownItems.Clear();

            IList<Triple<Assembly, Type, Exception>> pluginLoadErrors = AppWorkspace.GetEffectLoadErrors();

            DropDownItems.AddRange(GetMenuItemsToAdd(pluginLoadErrors.Count > 0));

            _menuFileNew.Enabled = true;
            _menuFileOpen.Enabled = true;
            _menuFileOpenRecent.Enabled = true;
            _menuFileOpenRecentSentinel.Enabled = true;
            _menuFileAcquire.Enabled = true;
            _menuFileAcquireFromScannerOrCamera.Enabled = true;
            _menuFileExit.Enabled = true;

            if (AppWorkspace.ActiveDocumentWorkspace != null)
            {
                _menuFileSave.Enabled = true;
                _menuFileSaveAs.Enabled = true;
                _menuFileClose.Enabled = true;
                _menuFilePrint.Enabled = true;
            }
            else
            {
                _menuFileSave.Enabled = false;
                _menuFileSaveAs.Enabled = false;
                _menuFileClose.Enabled = false;
                _menuFilePrint.Enabled = false;
            }

            base.OnDropDownOpening(e);
        }

        private void MenuFileOpenClick(object sender, EventArgs e)
        {
            AppWorkspace.PerformAction(new OpenFileAction());
        }

        private static void DoExit()
        {
            Startup.CloseApplication();
        }

        private static void MenuFileExitClick(object sender, EventArgs e)
        {
            DoExit();
        }

        private void MenuFileCloseClick(object sender, EventArgs e)
        {
            if (AppWorkspace.DocumentWorkspaces.Length > 0)
            {
                AppWorkspace.PerformAction(new CloseWorkspaceAction());
            }
            else
            {
                DoExit();
            }
        }

        private void MenuFileSaveAsClick(object sender, EventArgs e)
        {
            if (AppWorkspace.ActiveDocumentWorkspace != null)
            {
                AppWorkspace.ActiveDocumentWorkspace.DoSaveAs();
            }
        }

        private void MenuFileSaveClick(object sender, EventArgs e)
        {
            if (AppWorkspace.ActiveDocumentWorkspace != null)
            {
                AppWorkspace.ActiveDocumentWorkspace.DoSave();
            }
        }

        private void MenuFileAcquireDropDownOpening(object sender, EventArgs e)
        {
            // We only disable the scanner menu item if we know for sure a scanner is not available
            // If WIA isn't available we leave the menu item enabled. That way we can give an
            // informative error message when the user clicks on it and say "scanning requires XP SP1"
            // Otherwise the user is confused and will make scathing posts on our forum.
            bool scannerEnabled = true;

            if (ScanningAndPrinting.IsComponentAvailable)
            {
                if (!ScanningAndPrinting.CanScan)
                {
                    scannerEnabled = false;
                }
            }

            _menuFileAcquireFromScannerOrCamera.Enabled = scannerEnabled;
        }

        private void MenuFilePrintClick(object sender, EventArgs e)
        {
            if (AppWorkspace.ActiveDocumentWorkspace != null)
            {
                AppWorkspace.ActiveDocumentWorkspace.PerformAction(new PrintAction());
            }
        }

/*
        private void MenuFileOpenInNewWindow_Click(object sender, System.EventArgs e)
        {
            string fileName;
            string startingDir = Path.GetDirectoryName(AppWorkspace.ActiveDocumentWorkspace.FilePath);
            DialogResult result = DocumentWorkspace.ChooseFile(AppWorkspace, out fileName, startingDir);

            if (result == DialogResult.OK)
            {
                Startup.StartNewInstance(AppWorkspace, fileName);
            }
        }
*/              //SUSPECTED UNUSED METHOD

/*
        private void MenuFileNewWindow_Click(object sender, System.EventArgs e)
        {
            Startup.StartNewInstance(AppWorkspace, null);
        }
*/              //SUSPECTED UNUSED METHOD

        private void MenuFileOpenRecentDropDownOpening(object sender, EventArgs e)
        {
            AppWorkspace.MostRecentFiles.LoadMruList();
            MostRecentFile[] filesReverse = AppWorkspace.MostRecentFiles.GetFileList();
            var files = new MostRecentFile[filesReverse.Length];
            int i;

            for (i = 0; i < filesReverse.Length; ++i)
            {
                files[files.Length - i - 1] = filesReverse[i];
            }

            foreach (ToolStripItem mi in _menuFileOpenRecent.DropDownItems)
            {
                mi.Click -= MenuFileOpenRecentFileClick;
            }

            _menuFileOpenRecent.DropDownItems.Clear();

            i = 0;

            foreach (MostRecentFile mrf in files)
            {
                string menuName = i < 9 ? "&" : "";

                menuName += (1 + i) + " " + Path.GetFileName(mrf.FileName);
                var mi = new ToolStripMenuItem(menuName);
                mi.Click += MenuFileOpenRecentFileClick;
                mi.ImageScaling = ToolStripItemImageScaling.None;
                mi.Image = (Image)mrf.Thumb.Clone();
                _menuFileOpenRecent.DropDownItems.Add(mi);
                ++i;
            }

            if (_menuFileOpenRecent.DropDownItems.Count == 0)
            {
                var none = new ToolStripMenuItem(PdnResources.GetString("Menu.File.OpenRecent.None")) {Enabled = false};
                _menuFileOpenRecent.DropDownItems.Add(none);
            }
            else
            {
                var separator = new ToolStripSeparator();
                _menuFileOpenRecent.DropDownItems.Add(separator);

                var clearList = new ToolStripMenuItem
                                    {
                                        Text = PdnResources.GetString("Menu.File.OpenRecent.ClearThisList")
                                    };
                _menuFileOpenRecent.DropDownItems.Add(clearList);
                Image deleteIcon = PdnResources.GetImageResource("Icons.MenuEditEraseSelectionIcon.png").Reference;
                clearList.ImageTransparentColor = Utility.TransparentKey;
                clearList.ImageAlign = ContentAlignment.MiddleCenter;
                clearList.ImageScaling = ToolStripItemImageScaling.None;
                int iconSize = AppWorkspace.MostRecentFiles.IconSize;
                var bitmap = new Bitmap(iconSize + 2, iconSize + 2);

                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.Clear(clearList.ImageTransparentColor);

                    var offset = new Point((bitmap.Width - deleteIcon.Width) / 2,
                        (bitmap.Height - deleteIcon.Height) / 2);

                    g.CompositingMode = CompositingMode.SourceCopy;
                    g.DrawImage(deleteIcon, offset.X, offset.Y, deleteIcon.Width, deleteIcon.Height);
                }

                clearList.Image = bitmap;
                clearList.Click += ClearListClick;
            }
        }

        private void MenuFileOpenRecentFileClick(object sender, EventArgs e)
        {
            try
            {
                var mi = (ToolStripMenuItem)sender;
                int spaceIndex = mi.Text.IndexOf(" ");
                string indexString = mi.Text.Substring(1, spaceIndex - 1);
                int index = int.Parse(indexString) - 1;
                MostRecentFile[] recentFiles = AppWorkspace.MostRecentFiles.GetFileList();
                string fileName = recentFiles[recentFiles.Length - index - 1].FileName;
                AppWorkspace.OpenFileInNewWorkspace(fileName);
            }

            catch (Exception)
            {
            }
        }

        private void MenuFileNewClick(object sender, EventArgs e)
        {
            AppWorkspace.PerformAction(new NewImageAction());
        }

        private void MenuFileAcquireFromScannerOrCameraClick(object sender, EventArgs e)
        {
            AppWorkspace.PerformAction(new AcquireFromScannerOrCameraAction());
        }

        private void ClearListClick(object sender, EventArgs e)
        {
            AppWorkspace.PerformAction(new ClearMruListAction());
        }
    }
}
Back to Top