openpdn /src/DocumentStrip.cs

Language C# Lines 606
MD5 Hash 1eb24b9f6a662eb66dd5cf8a7f2db863 Estimated Cost $12,720 (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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/////////////////////////////////////////////////////////////////////////////////
// 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 System.Linq;
using PaintDotNet.Base;
using PaintDotNet.SystemLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace PaintDotNet
{
    internal class DocumentStrip
        : ImageStrip,
          IDocumentList
    {
        private object _thumbsLock = new object();

        private readonly List<DocumentWorkspace> _documents = new List<DocumentWorkspace>();
        private readonly List<Item> _documentButtons = new List<Item>();
        private readonly Dictionary<DocumentWorkspace, Item> _dw2Button = new Dictionary<DocumentWorkspace, Item>();
        private Dictionary<DocumentWorkspace, RenderArgs> _thumbs = new Dictionary<DocumentWorkspace, RenderArgs>();
        private ThumbnailManager _thumbnailManager;
        private DocumentWorkspace _selectedDocument;
        private bool _ensureSelectedIsVisible = true;
        private int _suspendThumbnailUpdates;

        public void SuspendThumbnailUpdates()
        {
            ++_suspendThumbnailUpdates;
        }

        public void ResumeThumbnailUpdates()
        {
            --_suspendThumbnailUpdates;
        }

        public void SyncThumbnails()
        {
            if (_thumbnailManager.IsDisposed) return;
            using (new WaitCursorChanger(this))
            {
                _thumbnailManager.DrainQueue();
            }

            Refresh();
        }

        public int ThumbnailUpdateLatency
        {
            get
            {
                return _thumbnailManager.UpdateLatency;
            }

            set
            {
                _thumbnailManager.UpdateLatency = value;
            }
        }

        public bool EnsureSelectedIsVisible
        {
            get
            {
                return _ensureSelectedIsVisible;
            }

            set
            {
                if (_ensureSelectedIsVisible == value) return;
                _ensureSelectedIsVisible = value;
                PerformLayout();
            }
        }

        public DocumentWorkspace[] DocumentList
        {
            get
            {
                return _documents.ToArray();
            }
        }

        public Image[] DocumentThumbnails
        {
            get
            {
                SyncThumbnails();

                var thumbnails = new Image[_documents.Count];

                for (int i = 0; i < thumbnails.Length; ++i)
                {
                    DocumentWorkspace dw = _documents[i];
                    RenderArgs ra;

                    if (!_thumbs.TryGetValue(dw, out ra))
                    {
                        thumbnails[i] = null;
                    }
                    else
                    {
                        thumbnails[i] = ra.Bitmap;
                    }
                }

                return thumbnails;
            }
        }

        public int DocumentCount
        {
            get
            {
                return _documents.Count;
            }
        }

        public event EventHandler DocumentListChanged;
        protected virtual void OnDocumentListChanged()
        {
            if (DocumentListChanged != null)
            {
                DocumentListChanged(this, EventArgs.Empty);
            }
        }

        public DocumentWorkspace SelectedDocument
        {
            get
            {
                return _selectedDocument;
            }

            set
            {
                if (!_documents.Contains(value))
                {
                    throw new ArgumentException("DocumentWorkspace isn't being tracked by this instance of DocumentStrip");
                }

                if (_selectedDocument == value) return;
                SelectDocumentWorkspace(value);
                OnDocumentClicked(value, DocumentClickAction.Select);
                Refresh();
            }
        }

        public int SelectedDocumentIndex
        {
            get
            {
                return _documents.IndexOf(_selectedDocument);
            }
        }

        public override Size GetPreferredSize(Size proposedSize)
        {
            Size itemSize = ItemSize;
            int preferredWidth;

            if (ItemCount == 0)
            {
                preferredWidth = 0;
            }
            else
            {
                preferredWidth = itemSize.Width * DocumentCount;
            }

            var preferredSize = new Size(preferredWidth, itemSize.Height);
            return preferredSize;
        }

        private bool OnDigitHotKeyPressed(Keys keys)
        {
            // strip off the Alt and Control stuff
            keys &= ~Keys.Alt;
            keys &= ~Keys.Control;

            if (keys < Keys.D0 || keys > Keys.D9)
            {
                return false;
            }

            int digit = (int)keys - (int)Keys.D0;
            int index;

            if (digit == 0)
            {
                index = 9;
            }
            else
            {
                index = digit - 1;
            }

            if (index < _documents.Count)
            {
                PerformItemClick(index, ItemPart.Image, MouseButtons.Left);
                return true;
            }
            return false;
        }

        public DocumentStrip()
        {
            PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.Tab, OnNextTabHotKeyPressed);
            PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.PageDown, OnNextTabHotKeyPressed);
            PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.Shift | Keys.Tab, OnPreviousTabHotKeyPressed);
            PdnBaseForm.RegisterFormHotKey(Keys.Control | Keys.PageUp, OnPreviousTabHotKeyPressed);

            _thumbnailManager = new ThumbnailManager(this); // allow for a 1px black border

            InitializeComponent();

            for (int i = 0; i <= 9; ++i)
            {
                Keys digit = Utility.LetterOrDigitCharToKeys((char)(i + '0'));
                PdnBaseForm.RegisterFormHotKey(Keys.Control | digit, OnDigitHotKeyPressed);
                PdnBaseForm.RegisterFormHotKey(Keys.Alt | digit, OnDigitHotKeyPressed);
            }

            ShowCloseButtons = true;
        }

        private bool OnNextTabHotKeyPressed(Keys keys)
        {
            bool processed = NextTab();
            return processed;
        }

        private bool OnPreviousTabHotKeyPressed(Keys keys)
        {
            bool processed = PreviousTab();
            return processed;
        }

        public bool NextTab()
        {
            bool changed = false;

            if (_selectedDocument != null)
            {
                int currentIndex = _documents.IndexOf(_selectedDocument);
                int newIndex = (currentIndex + 1) % _documents.Count;
                SelectedDocument = _documents[newIndex];
                changed = true;
            }

            return changed;
        }

        public bool PreviousTab()
        {
            bool changed = false;

            if (_selectedDocument != null)
            {
                int currentIndex = _documents.IndexOf(_selectedDocument);
                int newIndex = (currentIndex + (_documents.Count - 1)) % _documents.Count;
                SelectedDocument = _documents[newIndex];
                changed = true;
            }

            return changed;
        }

        private void InitializeComponent()
        {
            Name = "DocumentStrip";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                while (_documents.Count > 0)
                {
                    RemoveDocumentWorkspace(_documents[_documents.Count - 1]);
                }

                if (_thumbnailManager != null)
                {
                    _thumbnailManager.Dispose();
                    _thumbnailManager = null;
                }

                foreach (RenderArgs ra in _thumbs.Keys.Select(dw => _thumbs[dw]))
                {
                    ra.Dispose();
                }

                _thumbs.Clear();
                _thumbs = null;
            }

            base.Dispose(disposing);
        }

        protected new void OnScrollArrowClicked(ArrowDirection arrowDirection)
        {
            int sign = 0;

            switch (arrowDirection)
            {
                case ArrowDirection.Left:
                    sign = -1;
                    break;

                case ArrowDirection.Right:
                    sign = +1;
                    break;
            }

            int delta = ItemSize.Width;

            ScrollOffset += sign * delta;

            base.OnScrollArrowClicked(arrowDirection);
        }
        
        protected override void OnItemClicked(Item item, ItemPart itemPart, MouseButtons mouseButtons)
        {
            var dw = item.Tag as DocumentWorkspace;

            if (dw != null)
            {
                switch (itemPart)
                {
                    case ItemPart.None:
                        // do nothing
                        break;

                    case ItemPart.CloseButton:
                        if (mouseButtons == MouseButtons.Left)
                        {
                            OnDocumentClicked(dw, DocumentClickAction.Close);
                        }
                        break;

                    case ItemPart.Image:
                        switch (mouseButtons)
                        {
                            case MouseButtons.Left:
                                SelectedDocument = dw;
                                break;
                            case MouseButtons.Right: //TODO: Right-click menu
                                break;
                        }
                        break;

                    default:
                        throw new InvalidEnumArgumentException();
                }
            }

            base.OnItemClicked(item, itemPart, mouseButtons);
        }

        public event EventHandler<EventArgs<Pair<DocumentWorkspace, DocumentClickAction>>> DocumentClicked;
        protected virtual void OnDocumentClicked(DocumentWorkspace dw, DocumentClickAction action)
        {
            if (DocumentClicked != null)
            {
                DocumentClicked(this, new EventArgs<Pair<DocumentWorkspace, DocumentClickAction>>(
                    Pair.Create(dw, action)));
            }
        }

        public void UnlockDocumentWorkspaceDirtyValue(DocumentWorkspace unlockMe)
        {
            Item docItem = _dw2Button[unlockMe];
            docItem.UnlockDirtyValue();
        }

        public void LockDocumentWorkspaceDirtyValue(DocumentWorkspace lockMe, bool forceDirtyValue)
        {
            Item docItem = _dw2Button[lockMe];
            docItem.LockDirtyValue(forceDirtyValue);
        }

        public void AddDocumentWorkspace(DocumentWorkspace addMe)
        {
            _documents.Add(addMe);

            var docButton = new Item {Image = null, Tag = addMe};

            AddItem(docButton);
            _documentButtons.Add(docButton);

            addMe.CompositionUpdated += WorkspaceCompositionUpdated;

            _dw2Button.Add(addMe, docButton);

            if (addMe.Document != null)
            {
                QueueThumbnailUpdate(addMe);
                docButton.Dirty = addMe.Document.Dirty;
                addMe.Document.DirtyChanged += DocumentDirtyChanged;
            }

            addMe.DocumentChanging += WorkspaceDocumentChanging;
            addMe.DocumentChanged += WorkspaceDocumentChanged;

            OnDocumentListChanged();
        }

        private void WorkspaceDocumentChanging(object sender, EventArgs<Document> e)
        {
            if (e.Data != null)
            {
                e.Data.DirtyChanged -= DocumentDirtyChanged;
            }
        }

        private void WorkspaceDocumentChanged(object sender, EventArgs e)
        {
            var dw = (DocumentWorkspace)sender;
            Item docButton = _dw2Button[dw];

            if (dw.Document != null)
            {
                docButton.Dirty = dw.Document.Dirty;
                dw.Document.DirtyChanged += DocumentDirtyChanged;
            }
            else
            {
                docButton.Dirty = false;
            }
        }

        private void DocumentDirtyChanged(object sender, EventArgs e)
        {
            foreach (Item docButton in
                from t in _documents where ReferenceEquals(sender, t.Document) select _dw2Button[t])
            {
                docButton.Dirty = ((Document)sender).Dirty;
            }
        }

        private void WorkspaceCompositionUpdated(object sender, EventArgs e)
        {
            var dw = (DocumentWorkspace)sender;
            QueueThumbnailUpdate(dw);
        }

        public void RemoveDocumentWorkspace(DocumentWorkspace removeMe)
        {
            removeMe.CompositionUpdated -= WorkspaceCompositionUpdated;

            if (_selectedDocument == removeMe)
            {
                _selectedDocument = null;
            }

            removeMe.DocumentChanging -= WorkspaceDocumentChanging;
            removeMe.DocumentChanged -= WorkspaceDocumentChanged;

            if (removeMe.Document != null)
            {
                removeMe.Document.DirtyChanged -= DocumentDirtyChanged;
            }

            _documents.Remove(removeMe);
            _thumbnailManager.RemoveFromQueue(removeMe);

            Item docButton = _dw2Button[removeMe];
            RemoveItem(docButton);
            _dw2Button.Remove(removeMe);
            _documentButtons.Remove(docButton);

            if (_thumbs.ContainsKey(removeMe))
            {
                RenderArgs thumbRa = _thumbs[removeMe];
                Surface surface = thumbRa.Surface;
                thumbRa.Dispose();
                _thumbs.Remove(removeMe);
                surface.Dispose();
            }

            OnDocumentListChanged();
        }

        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);

            if (!_ensureSelectedIsVisible || (Focused || LeftScrollButton.Focused || RightScrollButton.Focused)) return;
            int index = _documents.IndexOf(_selectedDocument);
            EnsureItemFullyVisible(index);
        }

        public void SelectDocumentWorkspace(DocumentWorkspace selectMe)
        {
            UI.SuspendControlPainting(this);

            _selectedDocument = selectMe;

            if (_thumbs.ContainsKey(selectMe))
            {
                RenderArgs thumb = _thumbs[selectMe];
                Bitmap bitmap = thumb.Bitmap;
            }
            else
            {
                QueueThumbnailUpdate(selectMe);
            }

            foreach (Item docItem in _documentButtons)
            {
                if ((docItem.Tag as DocumentWorkspace) == selectMe)
                {
                    EnsureItemFullyVisible(docItem);
                    docItem.Checked = true;
                }
                else
                {
                    docItem.Checked = false;
                }
            }

            UI.ResumeControlPainting(this);
            Invalidate(true);
        }

        public void RefreshThumbnail(DocumentWorkspace dw)
        {
            if (_documents.Contains(dw))
            {
                QueueThumbnailUpdate(dw);
            }
        }

        public void RefreshAllThumbnails()
        {
            foreach (DocumentWorkspace dw in _documents)
            {
                QueueThumbnailUpdate(dw);
            }
        }

        private void OnThumbnailUpdated(DocumentWorkspace dw)
        {
            // We must double check that the DW is still around, because there's a chance
            // that the DW was been removed while the thumbnail was being rendered.
            if (!_dw2Button.ContainsKey(dw)) return;
            Item docButton = _dw2Button[dw];
            RenderArgs docRA = _thumbs[dw];
            docButton.Image = docRA.Bitmap;
            docButton.Update();
        }

        public void QueueThumbnailUpdate(DocumentWorkspace dw)
        {
            if (_suspendThumbnailUpdates <= 0)
            {
                _thumbnailManager.QueueThumbnailUpdate(dw, PreferredImageSize.Width - 2, OnThumbnailRendered);
            }
        }

        private void OnThumbnailRendered(object sender, EventArgs<Pair<IThumbnailProvider, Surface>> e)
        {
            RenderArgs ra = null;
            var dw = (DocumentWorkspace)e.Data.First;

            var desiredSize = new Size(e.Data.Second.Width + 2, e.Data.Second.Height + 2);

            if (_thumbs.ContainsKey(dw))
            {
                ra = _thumbs[dw];

                if (ra.Size != desiredSize)
                {
                    ra.Dispose();
                    ra = null;

                    _thumbs.Remove(dw);
                }
            }
            
            if (ra == null)
            {
                var surface = new Surface(desiredSize);
                ra = new RenderArgs(surface);
                _thumbs.Add(dw, ra);
            }

            ra.Surface.Clear(ColorBgra.Black);
            ra.Surface.CopySurface(e.Data.Second, new Point(1, 1));
            e.Data.Second.Dispose();

            OnThumbnailUpdated(dw);
        }
    }
}
Back to Top