-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigDialog.cs
221 lines (179 loc) · 7.11 KB
/
ConfigDialog.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
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-twainable-plus, an Effect plugin for
// Paint.NET that imports images from TWAIN devices.
//
// Copyright (c) 2014, 2017, 2018 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using PaintDotNet.AppModel;
using PaintDotNet.Effects;
using System.Runtime.InteropServices;
using TwainablePlus.Properties;
namespace TwainablePlus
{
internal partial class ConfigDialog : EffectConfigDialog
{
private Process process;
private static readonly uint ProxyDataMessage = SafeNativeMethods.RegisterWindowMessageW("TwainProxyData");
private IntPtr proxyHWnd;
private bool scanRunning;
public ConfigDialog()
{
InitializeComponent();
this.scanRunning = false;
}
protected override void InitialInitToken()
{
base.theEffectToken = new ConfigToken();
}
protected override void InitDialogFromToken(PaintDotNet.Effects.EffectConfigToken effectTokenCopy)
{
}
protected override void InitTokenFromDialog()
{
}
protected override void OnBackColorChanged(EventArgs e)
{
base.OnBackColorChanged(e);
PluginThemingUtil.UpdateControlBackColor(this);
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
PluginThemingUtil.UpdateControlForeColor(this);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
PluginThemingUtil.EnableEffectDialogTheme(this);
string path = Path.Combine(Path.GetDirectoryName(typeof(ConfigDialog).Assembly.Location), "TwainProxy.exe");
if (File.Exists(path))
{
string hWnd = this.Handle.ToString();
process = Process.Start(path, hWnd);
}
else
{
if (MessageBox.Show(this, Resources.TwainProxyNotFound, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.OK)
{
base.Close();
}
}
this.Cursor = Cursors.AppStarting;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
SafeNativeMethods.PostMessageW(proxyHWnd, NativeConstants.WM_CLOSE, UIntPtr.Zero, IntPtr.Zero);
base.OnFormClosing(e);
}
private void LoadSourcesCompleted(IntPtr data)
{
string[] sources = null;
int defaultIndex = -1;
int count = Marshal.ReadInt32(data);
if (count > 0)
{
sources = new string[count];
int offset = 4;
const string DefaultItemPrefix = "Default:";
for (int i = 0; i < count; i++)
{
int length = Marshal.ReadInt32(data, offset);
offset += 4;
string item = Marshal.PtrToStringUni(new IntPtr(data.ToInt64() + (long)offset), length / 2).TrimEnd('\0'); // 2 bytes per char
offset += length;
if ((defaultIndex == -1) && item.StartsWith(DefaultItemPrefix, StringComparison.Ordinal))
{
item = item.Remove(0, DefaultItemPrefix.Length);
defaultIndex = i;
}
sources[i] = item;
}
}
if (sources != null)
{
this.selectSourceCbo.Items.AddRange(sources);
this.selectSourceCbo.SelectedIndex = defaultIndex;
// Disable the source selection combo box unless there is more than one source available.
this.selectSourceCbo.Enabled = this.selectSourceCbo.Items.Count > 1;
this.acquireBtn.Enabled = true;
this.autoCloseCb.Enabled = true;
}
this.Cursor = Cursors.Default;
}
private void ScanCompleted(IntPtr result)
{
this.scanRunning = false;
this.Cursor = Cursors.Default;
if (result != IntPtr.Zero)
{
base.DialogResult = System.Windows.Forms.DialogResult.OK;
if (autoCloseCb.Checked)
{
base.Close();
}
}
}
private void ScanControlsEnabled(bool enabled)
{
this.selectSourceCbo.Enabled = enabled ? this.selectSourceCbo.Items.Count > 1 : false;
this.acquireBtn.Enabled = enabled;
this.autoCloseCb.Enabled = enabled;
}
private void closeBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void acquireBtn_Click(object sender, System.EventArgs e)
{
if (!scanRunning)
{
this.scanRunning = true;
base.Cursor = Cursors.WaitCursor;
IntPtr source = new IntPtr(selectSourceCbo.SelectedIndex);
SafeNativeMethods.PostMessageW(proxyHWnd, ProxyDataMessage, new UIntPtr(TwainProxy.ProxyDataMessages.StartScan), source);
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == ProxyDataMessage)
{
int action = m.WParam.ToInt32();
if (action == TwainProxy.ProxyDataMessages.GetProxyWindowHandle)
{
this.proxyHWnd = m.LParam;
}
else if (action == TwainProxy.ProxyDataMessages.ScanEnableWindow)
{
ScanControlsEnabled(m.LParam != IntPtr.Zero);
}
else if (action == TwainProxy.ProxyDataMessages.ScanCompletedCallback)
{
ScanCompleted(m.LParam);
}
}
else if (m.Msg == NativeConstants.WM_COPYDATA)
{
if (m.WParam == proxyHWnd)
{
NativeStructs.COPYDATASTRUCT cds = (NativeStructs.COPYDATASTRUCT)m.GetLParam(typeof(NativeStructs.COPYDATASTRUCT));
LoadSourcesCompleted(cds.lpData);
m.Result = new IntPtr(1);
}
}
base.WndProc(ref m);
}
private void donateLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
base.Services.GetService<IShellService>().LaunchUrl(this, "http://forums.getpaint.net/index.php?showtopic=28153");
}
}
}