forked from ShareX/ShareX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawableContainerList.cs
673 lines (633 loc) · 22.1 KB
/
DrawableContainerList.cs
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Greenshot.Drawing.Fields;
using Greenshot.Memento;
using Greenshot.Plugin;
using Greenshot.Plugin.Drawing;
using GreenshotPlugin.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Windows.Forms;
namespace Greenshot.Drawing
{
/// <summary>
/// Dispatches most of a DrawableContainer's public properties and methods to a list of DrawableContainers.
/// </summary>
[Serializable]
public class DrawableContainerList : List<IDrawableContainer>
{
private static readonly ComponentResourceManager editorFormResources = new ComponentResourceManager(typeof(ImageEditorForm));
public Guid ParentID
{
get;
private set;
}
public DrawableContainerList()
{
}
public DrawableContainerList(Guid parentID)
{
ParentID = parentID;
}
public EditStatus Status
{
get
{
return this[Count - 1].Status;
}
set
{
foreach (var dc in this)
{
dc.Status = value;
}
}
}
public List<IDrawableContainer> AsIDrawableContainerList()
{
List<IDrawableContainer> interfaceList = new List<IDrawableContainer>();
foreach (IDrawableContainer container in this)
{
interfaceList.Add(container);
}
return interfaceList;
}
/// <summary>
/// Gets or sets the selection status of the elements.
/// If several elements are in the list, true is only returned when all elements are selected.
/// </summary>
public bool Selected
{
get
{
bool ret = true;
foreach (var dc in this)
{
ret &= dc.Selected;
}
return ret;
}
set
{
foreach (var dc in this)
{
dc.Selected = value;
}
}
}
/// <summary>
/// Gets or sets the parent control of the elements in the list.
/// If there are several elements, the parent control of the last added is returned.
/// </summary>
public ISurface Parent
{
get
{
if (Count > 0)
{
return this[Count - 1].Parent;
}
return null;
}
set
{
ParentID = value.ID;
foreach (var drawableContainer in this)
{
var dc = (DrawableContainer)drawableContainer;
dc.Parent = value;
}
}
}
/// <summary>
/// Make a following bounds change on this containerlist undoable!
/// </summary>
/// <param name="allowMerge">true means allow the moves to be merged</param>
public void MakeBoundsChangeUndoable(bool allowMerge)
{
List<IDrawableContainer> movingList = new List<IDrawableContainer>();
Surface surface = null;
foreach (DrawableContainer dc in this)
{
movingList.Add(dc);
surface = dc._parent;
}
if (movingList.Count > 0 && surface != null)
{
surface.MakeUndoable(new DrawableContainerBoundsChangeMemento(movingList), allowMerge);
}
}
/// <summary>
/// Apply matrix to all elements
/// </summary>
public void Transform(Matrix matrix)
{
// Track modifications
bool modified = false;
Invalidate();
foreach (var dc in this)
{
dc.Transform(matrix);
modified = true;
}
// Invalidate after
Invalidate();
// If we moved something, tell the surface it's modified!
if (modified)
{
Parent.Modified = true;
}
}
/// <summary>
/// Moves all elements in the list by the given amount of pixels.
/// </summary>
/// <param name="dx">pixels to move horizontally</param>
/// <param name="dy">pixels to move vertically</param>
public void MoveBy(int dx, int dy)
{
// Track modifications
bool modified = false;
// Invalidate before moving, otherwise the old locations aren't refreshed
Invalidate();
foreach (var dc in this)
{
dc.Left += dx;
dc.Top += dy;
modified = true;
}
// Invalidate after
Invalidate();
// If we moved something, tell the surface it's modified!
if (modified)
{
Parent.Modified = true;
}
}
/// <summary>
/// Hides the grippers of all elements in the list.
/// </summary>
public void HideGrippers()
{
foreach (var dc in this)
{
dc.HideGrippers();
dc.Invalidate();
}
}
/// <summary>
/// Shows the grippers of all elements in the list.
/// </summary>
public void ShowGrippers()
{
foreach (var dc in this)
{
dc.ShowGrippers();
dc.Invalidate();
}
}
/// <summary>
/// Indicates whether on of the elements is clickable at the given location
/// </summary>
/// <param name="x">x coordinate to be checked</param>
/// <param name="y">y coordinate to be checked</param>
/// <returns>true if one of the elements in the list is clickable at the given location, false otherwise</returns>
public bool ClickableAt(int x, int y)
{
bool ret = false;
foreach (var dc in this)
{
ret |= dc.ClickableAt(x, y);
}
return ret;
}
/// <summary>
/// retrieves the topmost element being clickable at the given location
/// </summary>
/// <param name="x">x coordinate to be checked</param>
/// <param name="y">y coordinate to be checked</param>
/// <returns>the topmost element from the list being clickable at the given location, null if there is no clickable element</returns>
public IDrawableContainer ClickableElementAt(int x, int y)
{
for (int i = Count - 1; i >= 0; i--)
{
if (this[i].ClickableAt(x, y))
{
return this[i];
}
}
return null;
}
/// <summary>
/// Dispatches OnDoubleClick to all elements in the list.
/// </summary>
public void OnDoubleClick()
{
foreach (var drawableContainer in this)
{
var dc = (DrawableContainer)drawableContainer;
dc.OnDoubleClick();
}
}
/// <summary>
/// Check if there are any intersecting filters, if so we need to redraw more
/// </summary>
/// <param name="clipRectangle"></param>
/// <returns>true if an filter intersects</returns>
public bool HasIntersectingFilters(Rectangle clipRectangle)
{
foreach (var dc in this)
{
if (dc.DrawingBounds.IntersectsWith(clipRectangle) && dc.hasFilters && dc.Status == EditStatus.IDLE)
{
return true;
}
}
return false;
}
/// <summary>
/// Check if any of the drawableContainers are inside the rectangle
/// </summary>
/// <param name="clipRectangle"></param>
/// <returns></returns>
public bool IntersectsWith(Rectangle clipRectangle)
{
foreach (var dc in this)
{
if (dc.DrawingBounds.IntersectsWith(clipRectangle))
{
return true;
}
}
return false;
}
/// <summary>
/// Triggers all elements in the list ot be redrawn.
/// </summary>
/// <param name="g">the to the bitmap related Graphics object</param>
/// <param name="bitmap">Bitmap to draw</param>
/// <param name="renderMode">the rendermode in which the element is to be drawn</param>
/// <param name="clipRectangle"></param>
public void Draw(Graphics g, Bitmap bitmap, RenderMode renderMode, Rectangle clipRectangle)
{
foreach (var drawableContainer in this)
{
var dc = (DrawableContainer)drawableContainer;
if (dc.DrawingBounds.IntersectsWith(clipRectangle))
{
dc.DrawContent(g, bitmap, renderMode, clipRectangle);
}
}
}
/// <summary>
/// Pass the field changed event to all elements in the list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void HandleFieldChangedEvent(object sender, FieldChangedEventArgs e)
{
foreach (var drawableContainer in this)
{
var dc = (DrawableContainer)drawableContainer;
dc.HandleFieldChanged(sender, e);
}
}
/// <summary>
/// Invalidate the bounds of all the DC's in this list
/// </summary>
public void Invalidate()
{
foreach (var dc in this)
{
dc.Invalidate();
}
}
/// <summary>
/// Indicates whether the given list of elements can be pulled up,
/// i.e. whether there is at least one unselected element higher in hierarchy
/// </summary>
/// <param name="elements">list of elements to pull up</param>
/// <returns>true if the elements could be pulled up</returns>
public bool CanPullUp(DrawableContainerList elements)
{
if (elements.Count == 0 || elements.Count == Count)
{
return false;
}
foreach (var element in elements)
{
if (IndexOf(element) < Count - elements.Count)
{
return true;
}
}
return false;
}
/// <summary>
/// Pulls one or several elements up one level in hierarchy (z-index).
/// </summary>
/// <param name="elements">list of elements to pull up</param>
public void PullElementsUp(DrawableContainerList elements)
{
for (int i = Count - 1; i >= 0; i--)
{
var dc = this[i];
if (!elements.Contains(dc))
{
continue;
}
if (Count > i + 1 && !elements.Contains(this[i + 1]))
{
SwapElements(i, i + 1);
}
}
}
/// <summary>
/// Pulls one or several elements up to the topmost level(s) in hierarchy (z-index).
/// </summary>
/// <param name="elements">of elements to pull to top</param>
public void PullElementsToTop(DrawableContainerList elements)
{
var dcs = ToArray();
for (int i = 0; i < dcs.Length; i++)
{
var dc = dcs[i];
if (!elements.Contains(dc))
{
continue;
}
Remove(dc);
Add(dc);
Parent.Modified = true;
}
}
/// <summary>
/// Indicates whether the given list of elements can be pushed down,
/// i.e. whether there is at least one unselected element lower in hierarchy
/// </summary>
/// <param name="elements">list of elements to push down</param>
/// <returns>true if the elements could be pushed down</returns>
public bool CanPushDown(DrawableContainerList elements)
{
if (elements.Count == 0 || elements.Count == Count)
{
return false;
}
foreach (var element in elements)
{
if (IndexOf(element) >= elements.Count)
{
return true;
}
}
return false;
}
/// <summary>
/// Pushes one or several elements down one level in hierarchy (z-index).
/// </summary>
/// <param name="elements">list of elements to push down</param>
public void PushElementsDown(DrawableContainerList elements)
{
for (int i = 0; i < Count; i++)
{
var dc = this[i];
if (!elements.Contains(dc))
{
continue;
}
if ((i > 0) && !elements.Contains(this[i - 1]))
{
SwapElements(i, i - 1);
}
}
}
/// <summary>
/// Pushes one or several elements down to the bottommost level(s) in hierarchy (z-index).
/// </summary>
/// <param name="elements">of elements to push to bottom</param>
public void PushElementsToBottom(DrawableContainerList elements)
{
var dcs = ToArray();
for (int i = dcs.Length - 1; i >= 0; i--)
{
var dc = dcs[i];
if (!elements.Contains(dc))
{
continue;
}
Remove(dc);
Insert(0, dc);
Parent.Modified = true;
}
}
/// <summary>
/// swaps two elements in hierarchy (z-index),
/// checks both indices to be in range
/// </summary>
/// <param name="index1">index of the 1st element</param>
/// <param name="index2">index of the 2nd element</param>
private void SwapElements(int index1, int index2)
{
if (index1 < 0 || index1 >= Count || index2 < 0 || index2 >= Count || index1 == index2)
{
return;
}
var dc = this[index1];
this[index1] = this[index2];
this[index2] = dc;
Parent.Modified = true;
}
/// <summary>
/// Add items to a context menu for the selected item
/// </summary>
/// <param name="menu"></param>
/// <param name="surface"></param>
public virtual void AddContextMenuItems(ContextMenuStrip menu, Surface surface)
{
bool push = surface.Elements.CanPushDown(this);
bool pull = surface.Elements.CanPullUp(this);
ToolStripMenuItem item;
// Pull "up"
if (pull)
{
item = new ToolStripMenuItem("Up to top");
item.Click += delegate
{
surface.Elements.PullElementsToTop(this);
surface.Elements.Invalidate();
};
menu.Items.Add(item);
item = new ToolStripMenuItem("Up one level");
item.Click += delegate
{
surface.Elements.PullElementsUp(this);
surface.Elements.Invalidate();
};
menu.Items.Add(item);
}
// Push "down"
if (push)
{
item = new ToolStripMenuItem("Down to bottom");
item.Click += delegate
{
surface.Elements.PushElementsToBottom(this);
surface.Elements.Invalidate();
};
menu.Items.Add(item);
item = new ToolStripMenuItem("Down one level");
item.Click += delegate
{
surface.Elements.PushElementsDown(this);
surface.Elements.Invalidate();
};
menu.Items.Add(item);
}
// Duplicate
item = new ToolStripMenuItem("Duplicate selected element");
item.Click += delegate
{
DrawableContainerList dcs = this.Clone();
dcs.Parent = surface;
dcs.MoveBy(10, 10);
surface.AddElements(dcs);
surface.DeselectAllElements();
surface.SelectElements(dcs);
};
menu.Items.Add(item);
// Copy
item = new ToolStripMenuItem("Copy");
item.Image = (Image)editorFormResources.GetObject("copyToolStripMenuItem.Image");
item.Click += delegate
{
ClipboardHelper.SetClipboardData(typeof(DrawableContainerList), this);
};
menu.Items.Add(item);
// Cut
item = new ToolStripMenuItem("Cut");
item.Image = (Image)editorFormResources.GetObject("btnCut.Image");
item.Click += delegate
{
ClipboardHelper.SetClipboardData(typeof(DrawableContainerList), this);
List<DrawableContainer> containersToDelete = new List<DrawableContainer>();
foreach (var drawableContainer in this)
{
var container = (DrawableContainer)drawableContainer;
containersToDelete.Add(container);
}
foreach (var container in containersToDelete)
{
surface.RemoveElement(container, true);
}
};
menu.Items.Add(item);
// Delete
item = new ToolStripMenuItem("Delete");
item.Image = (Image)editorFormResources.GetObject("removeObjectToolStripMenuItem.Image");
item.Click += delegate
{
List<DrawableContainer> containersToDelete = new List<DrawableContainer>();
foreach (var drawableContainer in this)
{
var container = (DrawableContainer)drawableContainer;
containersToDelete.Add(container);
}
foreach (DrawableContainer container in containersToDelete)
{
surface.RemoveElement(container, true);
}
};
menu.Items.Add(item);
// Reset
bool canReset = false;
foreach (var drawableContainer in this)
{
var container = (DrawableContainer)drawableContainer;
if (container.HasDefaultSize)
{
canReset = true;
}
}
if (canReset)
{
item = new ToolStripMenuItem("Reset size");
//item.Image = ((System.Drawing.Image)(editorFormResources.GetObject("removeObjectToolStripMenuItem.Image")));
item.Click += delegate
{
foreach (var drawableContainer in this)
{
var container = (DrawableContainer)drawableContainer;
if (!container.HasDefaultSize)
{
continue;
}
Size defaultSize = container.DefaultSize;
container.Invalidate();
container.MakeBoundsChangeUndoable(false);
container.Width = defaultSize.Width;
container.Height = defaultSize.Height;
container.Invalidate();
}
};
menu.Items.Add(item);
}
}
public virtual void ShowContextMenu(MouseEventArgs e, Surface surface)
{
bool hasMenu = false;
foreach (var drawableContainer in this)
{
var container = (DrawableContainer)drawableContainer;
if (!container.HasContextMenu)
{
continue;
}
hasMenu = true;
break;
}
if (hasMenu)
{
ContextMenuStrip menu = new ContextMenuStrip();
AddContextMenuItems(menu, surface);
if (menu.Items.Count > 0)
{
menu.Show(surface, e.Location);
while (true)
{
if (menu.Visible)
{
Application.DoEvents();
Thread.Sleep(100);
}
else
{
menu.Dispose();
break;
}
}
}
}
}
}
}