forked from cwc/web-page-screensaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefsByScreenUserControl.cs
119 lines (108 loc) · 4.2 KB
/
PrefsByScreenUserControl.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Web_Page_Screensaver
{
using pl.polidea.lab.Web_Page_Screensaver;
public partial class PrefsByScreenUserControl : UserControl
{
public PrefsByScreenUserControl()
{
InitializeComponent();
}
private void MoveAllSelectedUrlsDown_Click(object sender, EventArgs e)
{
// TODO: make button grey out when none selected OR when all selected ones are in a bunch at the bottom.
bool gapFound = false;
// scan from the bottom up, but don't start moving selected items down until
// we find an unselected gap to move items(s) down in to.
for (int i = lvUrls.Items.Count - 1; i >= 0; i--)
{
if (lvUrls.Items[i].Selected)
{
if (gapFound)
{
Swap(lvUrls.Items, i, i + 1);
}
}
else
{
gapFound = true;
}
}
// 'select'ing the list makes sure the selections of items within it are being displayed.
// Otherwise the button becomes the 'selected' control and the selections within the list
// are invisible or hard to see.
lvUrls.Select();
}
private void MoveAllSelectedUrlsUp_Click(object sender, EventArgs e)
{
// TODO: make button grey out when none selected OR when all selected ones are in a bunch at the top.
bool gapFound = false;
// scan through for all selected, but don't start moving selected items up until
// we find an unselected gap to move items(s) up in to.
for (int i = 0; i < lvUrls.Items.Count; i++)
{
if (lvUrls.Items[i].Selected)
{
if (gapFound)
{
Swap(lvUrls.Items, i, i - 1);
}
}
else
{
gapFound = true;
}
}
// 'select'ing the list makes sure the selections of items within it are being displayed.
// Otherwise the button becomes the 'selected' control and the selections within the list
// are invisible or hard to see.
lvUrls.Select();
}
private void DeleteAllSelectedUrls_Click(object sender, EventArgs e)
{
// TODO: undo capability?
// TODO: make button grey out when no selection.
// work from the bottom up, deleting any we find
for (int i = lvUrls.Items.Count - 1; i >= 0; i--)
{
if (lvUrls.Items[i].Selected)
{
lvUrls.Items[i].Remove();
}
}
}
private void addUrlButton_Click(object sender, EventArgs e)
{
ListViewItem item = lvUrls.Items.Add(String.Empty);
item.BeginEdit();
}
/// <summary>
/// Swap the positions of 2 items in a ListViewItemCollection, maintaining Selected status.
/// Any indexes given outside the bounds of the list are treated as if they point to
/// the nearest end of the list.
/// </summary>
private static void Swap(ListView.ListViewItemCollection itemsList, int indexA, int indexB)
{
var a = Math.Min(itemsList.Count - 1, Math.Max(0, indexA));
var b = Math.Min(itemsList.Count - 1, Math.Max(0, indexB));
if (a != b)
{
var itemA = (ListViewItem)itemsList[a].Clone();
bool itemASelected = itemsList[a].Selected;
var itemB = (ListViewItem)itemsList[b].Clone();
bool itemBSelected = itemsList[b].Selected;
itemsList[a] = itemB;
itemsList[a].Selected = itemBSelected;
itemsList[b] = itemA;
itemsList[b].Selected = itemASelected;
}
}
}
}