-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrmMain.cs
1219 lines (1090 loc) · 41.1 KB
/
frmMain.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using RAD.ClipMon.Win32;
using RAD.Windows;
namespace pospi.CP1252
{
/// <summary>
/// Code Page 1252 Fixer
/// Copyright (c) Sam Pospischil 2011
/// <br/>
/// Based on Clipboard Monitor Example
/// Copyright (c) Ross Donald 2003
/// ross@radsoftware.com.au
/// http://www.radsoftware.com.au
/// <br/>
/// Monitors the clipboard for changes, and replaces characters from Windows CP 1252
/// with those from the low ASCII range. Configurable via tray menu as follows:
/// (also a :TODO: list)
/// - Toggle replace 'smart quotes' (accented single and double quotes, long dashes and ellipsis)
/// - Toggle replace smart quotes as HTML entites, preserving them rather than converting down
/// - Toggle replace any characters which have named HTML entities
/// - Toggle replace all high-ASCII characters with entity numbers
/// - Toggle automatic conversion of RTF text to plaintext
/// </summary>
/// <remarks>
/// </remarks>
public class frmMain : System.Windows.Forms.Form
{
#region Character Mappings
// not used like the others, this overrides character matches in RTF text
Dictionary<int, String> rtfSpecials = new Dictionary<int, String>()
{
{0x2013, "\\endash "},
{0x2014, "\\emdash "},
{8194, "\\enspace "},
{8195, "\\emspace "},
{0x2022, "\\bullet "},
{0x2018, "\\lquote "},
{0x2019, "\\rquote "},
{0x201C, "\\ldblquote "},
{0x201D, "\\rdblquote "},
{0x2026, "\\'85"}, // ellipsis
{0x201A, "\\'82"}, // second
{0x201E, "\\'84"}, // minute
{160, "\\~ "}, //
{173, "\\- "}, // ­
// no non-breaking hyphen equivalent in HTML
{8204, "\\zwnj "}, // ‌
{8205, "\\zwj "} // ‍
};
Dictionary<int, String> smartQuotesTranslate = new Dictionary<int, String>()
{
{ 0x201A, ","},
{ 0x201E, ",,"},
{ 0x2018, "\'"},
{ 0x2019, "\'"},
{ 0x201C, "\""},
{ 0x201D, "\""},
{ 0x2022, "-"},
{ 0x2013, "-"},
{ 0x2014, "-"},
{ 0x2026, "..."}
};
Dictionary<int, String> smartQuotesEscape = new Dictionary<int, String>()
{
{ 0x201A, "‚"},
{ 0x201E, "„"},
{ 0x2018, "‘"},
{ 0x2019, "’"},
{ 0x201C, "“"},
{ 0x201D, "”"},
{ 0x2022, "•"},
{ 0x2013, "–"},
{ 0x2014, "—"},
{ 0x2026, "…"},
{ 188, "¼"}, // we also escape fractions as they are auto-inserted by Word
{ 189, "½"},
{ 190, "¾"}
};
Dictionary<int, String> namedEntityEscape = new Dictionary<int, String>()
{
{ 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, "ÿ"},
{ 402, "ƒ"},
{ 913, "Α"},
{ 914, "Β"},
{ 915, "Γ"},
{ 916, "Δ"},
{ 917, "Ε"},
{ 918, "Ζ"},
{ 919, "Η"},
{ 920, "Θ"},
{ 921, "Ι"},
{ 922, "Κ"},
{ 923, "Λ"},
{ 924, "Μ"},
{ 925, "Ν"},
{ 926, "Ξ"},
{ 927, "Ο"},
{ 928, "Π"},
{ 929, "Ρ"},
{ 931, "Σ"},
{ 932, "Τ"},
{ 933, "Υ"},
{ 934, "Φ"},
{ 935, "Χ"},
{ 936, "Ψ"},
{ 937, "Ω"},
{ 945, "α"},
{ 946, "β"},
{ 947, "γ"},
{ 948, "δ"},
{ 949, "ε"},
{ 950, "ζ"},
{ 951, "η"},
{ 952, "θ"},
{ 953, "ι"},
{ 954, "κ"},
{ 955, "λ"},
{ 956, "μ"},
{ 957, "ν"},
{ 958, "ξ"},
{ 959, "ο"},
{ 960, "π"},
{ 961, "ρ"},
{ 962, "ς"},
{ 963, "σ"},
{ 964, "τ"},
{ 965, "υ"},
{ 966, "φ"},
{ 967, "χ"},
{ 968, "ψ"},
{ 969, "ω"},
{ 977, "ϑ"},
{ 978, "ϒ"},
{ 982, "ϖ"},
//{ 8226, "•"},
//{ 8230, "…"},
{ 8242, "′"},
{ 8243, "″"},
{ 8254, "‾"},
{ 8260, "⁄"},
{ 8472, "℘"},
{ 8465, "ℑ"},
{ 8476, "ℜ"},
{ 8482, "™"},
{ 8501, "ℵ"},
{ 8592, "←"},
{ 8593, "↑"},
{ 8594, "→"},
{ 8595, "↓"},
{ 8596, "↔"},
{ 8629, "↵"},
{ 8656, "⇐"},
{ 8657, "⇑"},
{ 8658, "⇒"},
{ 8659, "⇓"},
{ 8660, "⇔"},
{ 8704, "∀"},
{ 8706, "∂"},
{ 8707, "∃"},
{ 8709, "∅"},
{ 8711, "∇"},
{ 8712, "∈"},
{ 8713, "∉"},
{ 8715, "∋"},
{ 8719, "∏"},
{ 8721, "∑"},
{ 8722, "−"},
{ 8727, "∗"},
{ 8730, "√"},
{ 8733, "∝"},
{ 8734, "∞"},
{ 8736, "∠"},
{ 8743, "∧"},
{ 8744, "∨"},
{ 8745, "∩"},
{ 8746, "∪"},
{ 8747, "∫"},
{ 8756, "∴"},
{ 8764, "∼"},
{ 8773, "≅"},
{ 8776, "≈"},
{ 8800, "≠"},
{ 8801, "≡"},
{ 8804, "≤"},
{ 8805, "≥"},
{ 8834, "⊂"},
{ 8835, "⊃"},
{ 8836, "⊄"},
{ 8838, "⊆"},
{ 8839, "⊇"},
{ 8853, "⊕"},
{ 8855, "⊗"},
{ 8869, "⊥"},
{ 8901, "⋅"},
{ 8968, "⌈"},
{ 8969, "⌉"},
{ 8970, "⌊"},
{ 8971, "⌋"},
{ 9001, "⟨"},
{ 9002, "⟩"},
{ 9674, "◊"},
{ 9824, "♠"},
{ 9827, "♣"},
{ 9829, "♥"},
{ 9830, "♦"},
{ 338, "Œ"},
{ 339, "œ"},
{ 352, "Š"},
{ 353, "š"},
{ 376, "Ÿ"},
{ 710, "ˆ"},
{ 732, "˜"},
{ 8194, " "},
{ 8195, " "},
{ 8201, " "},
{ 8204, "‌"},
{ 8205, "‍"},
{ 8206, "‎"},
{ 8207, "‏"},
//{ 8211, "–"},
//{ 8212, "—"},
//{ 8216, "‘"},
//{ 8217, "’"},
//{ 8218, "‚"},
//{ 8220, "“"},
//{ 8221, "”"},
//{ 8222, "„"},
{ 8224, "†"},
{ 8225, "‡"},
{ 8240, "‰"},
{ 8249, "‹"},
{ 8250, "›"},
{ 8364, "€"}
};
#endregion
#region Constants
#endregion
#region Fields
private System.ComponentModel.IContainer components;
private System.Windows.Forms.MainMenu menuMain;
private System.Windows.Forms.RichTextBox ctlClipboardText;
protected System.Windows.Forms.ContextMenu cmnuTray;
private System.Windows.Forms.MenuItem itmExit;
private System.Windows.Forms.MenuItem itmHide;
private System.Windows.Forms.MenuItem itmSep2;
private NotifyIcon notifyIcon1;
private System.Windows.Forms.MenuItem itmSep1;
private MenuItem toggleEnabled;
private LinkLabel linkLabel1;
private ToolTip toolTip1;
#endregion
#region Constructors
public frmMain()
{
InitializeComponent();
notifyIcon1.Visible = true;
}
#endregion
#region Properties - Public
#endregion
#region Properties - Private
private bool enabled = true; // turns everything on/off
private bool _copiedRTF = false; // true if RTF was copied rather than text
private String _rawClip = ""; // raw clipboard data
private String _modifiedClip; // clipboard text with replacements made
private bool clipInserting = false; // true when pasting BACK into clipboard
// an option in the context menu along with its current status
private struct optionFlag
{
public bool active;
public String text;
public String shorttext;
public String desc;
public MenuItem menuItem;
public CheckBox buttonToggle;
}
// context menu options and app state
private optionFlag[] CMOptions = new optionFlag[] {
new optionFlag() {
active = true,
text = "Replace \'smart quotes\'",
shorttext = " -> \"",
desc = "Replaces quote characters added by MS Word, some versions\nof Outlook and others with ASCII equivalent quotes. This replaces\nsingle and double quotes, dashes, bulletpoints and the ellipsis character."
},
new optionFlag() {
active = false,
text = "Keep \'smart quotes\' as entities",
shorttext = " -> &“",
desc = "Replaces single and double quotes, dashes, bulletpoints\nand the ellipsis character with HTML entity references.\nUse this to preserve these characters in an ISO-8859-1\nencoded website rather than converting them."
},
new optionFlag() {
active = true,
text = "Replace all named HTML entities",
shorttext = "Named",
desc = "Any characters (except for double quotes and HTML brackets)\nwith named HTML entities are converted to those entity codes."
},
new optionFlag() {
active = true,
text = "Convert all high ASCII to numbered entities" ,
shorttext = "Numbered",
desc = "Any characters above the standard ASCII range (0x9F) are\nreplaced with HTML entity numbers.\nThis will ensure any strange characters (even Chinese, Korean etc)\nare preserved in an ISO-8859-1 document."
},
new optionFlag() {
active = false,
text = "Convert rich text to plaintext",
shorttext = "RTF -> Plain",
desc = "Convert text copied with formatting to be plaintext.\nMost often the text will be converted on pasting into\na plaintext application, but you can force this here if you wish."
}
};
private IntPtr _ClipboardViewerNext;
#endregion
#region Methods - Private
/// <summary>
/// Register this form as a Clipboard Viewer application
/// </summary>
private void RegisterClipboardViewer()
{
_ClipboardViewerNext = RAD.ClipMon.Win32.User32.SetClipboardViewer(this.Handle);
}
/// <summary>
/// Remove this form from the Clipboard Viewer list
/// </summary>
private void UnregisterClipboardViewer()
{
RAD.ClipMon.Win32.User32.ChangeClipboardChain(this.Handle, _ClipboardViewerNext);
}
/// <summary>
/// Search the clipboard contents for configured characters and translate them
/// </summary>
/// <param name="iData">The current clipboard contents</param>
/// <returns>true if characters were replaced, false otherwise</returns>
private bool replaceQuotes()
{
bool found = false;
_modifiedClip = _rawClip;
if (CMOptions[4].active) // convert to plaintext
{
_modifiedClip = convertRTFToString(_rawClip);
}
if (CMOptions[0].active) // replace smart quotes
{
Dictionary<int, String> replacementsToRun;
if (CMOptions[1].active) // replace them with entity refs instead of low-ASCII equivalents
{
replacementsToRun = smartQuotesEscape;
}
else
{
replacementsToRun = smartQuotesTranslate;
}
if (runReplacements(replacementsToRun))
{
found = true;
}
}
if (CMOptions[2].active) // replace named entities
{
if (runReplacements(namedEntityEscape))
{
found = true;
}
}
if (CMOptions[3].active) // replace all high ASCII
{
if (replaceHighASCII())
{
found = true;
}
}
//Console.WriteLine(_rawClip + "\n\n" + _modifiedClip);
return found;
}
private bool runReplacements(Dictionary<int, String> repls)
{
bool found = false;
foreach (KeyValuePair<int, String> replacement in repls)
{
String changed;
String search = Char.ConvertFromUtf32(replacement.Key);
if (_copiedRTF && !CMOptions[4].active)
{
if (rtfSpecials.ContainsKey(replacement.Key)) // rtf special-escaped characters
{
search = rtfSpecials[replacement.Key];
}
else if (replacement.Key > 0x80) // rtf high-ascii
{
search = "\\\'" + replacement.Key.ToString("x"); // rtf escape sequences are in lowercase
}
changed = _modifiedClip.Replace(search, replacement.Value);
} else {
changed = _modifiedClip.Replace(search, replacement.Value);
}
if (!found && changed != _modifiedClip)
{
found = true;
}
_modifiedClip = changed;
}
return found;
}
private bool replaceHighASCII()
{
bool found = false;
for (int i = 0; i < _modifiedClip.Length; ++i)
{
char c = _modifiedClip[i];
if (c > 0x9F)
{
found = true;
String entityCode = "&#" + (int)(c) + ";";
_modifiedClip = _modifiedClip.Remove(i, 1);
_modifiedClip = _modifiedClip.Insert(i, entityCode);
// we could increment i here but there's no real need since anything we add won't be bad
}
}
return found;
}
private bool importClipData(IDataObject iData)
{
//
// If it is not text then quit
// cannot search bitmap etc
//
try
{
String oldClip = _rawClip;
if (iData.GetDataPresent(DataFormats.Rtf))
{
_rawClip = normaliseRtf((string)iData.GetData(DataFormats.Rtf));
_copiedRTF = true;
setNotificationTooltip("RTF copied");
}
else if (iData.GetDataPresent(DataFormats.UnicodeText))
{
_rawClip = (string)iData.GetData(DataFormats.UnicodeText);
_copiedRTF = false;
setNotificationTooltip("Text copied");
}
if (_rawClip == null || _rawClip == "") // nothing was on the clipboard
{
_rawClip = oldClip;
return false;
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return false;
}
// some applications have VERY SILLY rtf encoding (looking at you, Word)
// Parsing it via a RichTextBox first cleans these ones up a great deal.
private String normaliseRtf(String str)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = (string)str;
return (string)rtBox.Rtf;
}
catch (Exception e)
{
return str;
}
}
// convert RTF data to plainText using a RichTextBox
private String convertRTFToString(String str)
{
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
try
{
rtBox.Rtf = (string)str;
}
catch (Exception e)
{
return str; // already a plaintext string
}
return rtBox.Text;
}
private void setNotificationTooltip(String tt)
{
notifyIcon1.Text = tt;
}
/// <summary>
/// Show the clipboard contents in the window
/// and show the notification balloon if a link is found
/// </summary>
private void ProcessClipboard()
{
if (!enabled)
{
return;
}
//
// Data on the clipboard uses the
// IDataObject interface
//
IDataObject iData = new DataObject();
try
{
iData = Clipboard.GetDataObject();
}
catch (System.Runtime.InteropServices.ExternalException externEx)
{
// Copying a field definition in Access 2002 causes this sometimes?
Debug.WriteLine("InteropServices.ExternalException: {0}", externEx.Message);
return;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
bool textDataPresent = importClipData(iData); // this also assigns _rawClip and _copiedRTF
// show current clipboard text even if there was no match
if (textDataPresent)
{
if (_copiedRTF)
{
try
{
ctlClipboardText.Rtf = _rawClip; // but show the original RTF formatted string where appropriate
}
catch (Exception e)
{
// sometimes this fails when converting from RTF to plaintext and back again
ctlClipboardText.Text = _rawClip;
}
}
else
{
ctlClipboardText.Text = _rawClip;
}
}
if (textDataPresent && replaceQuotes())
{
bool rtfOutput = _copiedRTF && !CMOptions[4].active;
// bad quotes were found and have be purged, so update the text with the fixed one
if (rtfOutput)
{
ctlClipboardText.Rtf = _modifiedClip;
} else {
ctlClipboardText.Text = _modifiedClip;
}
trayMessage("Characters fixed", "Click to view the clipboard contents", ToolTipIcon.Info);
clipInserting = true;
storeToClipboard(iData, _modifiedClip, rtfOutput);
clipInserting = false;
}
}
private void storeToClipboard(IDataObject ido, String value, bool asRTF)
{
String err = null;
IDataObject newdo = new DataObject();
if (asRTF)
{
// also set clipboard text property
String temp = convertRTFToString(_modifiedClip);
newdo.SetData(DataFormats.UnicodeText, false, temp);
newdo.SetData(DataFormats.Rtf, true, _modifiedClip);
}
else
{
newdo.SetData(DataFormats.Rtf, false, null);
newdo.SetData(DataFormats.UnicodeText, true, _modifiedClip);
}
try
{
Clipboard.SetDataObject(newdo, true, 5, 250);
}
catch (ExternalException e)
{
err = "The clipboard was in use by another application and could not be written to.\nClick to copy the modified text manually.";
}
catch (System.Threading.ThreadStateException e)
{
err = "The clipboard cannot be pasted to as you are not running this application in single-threaded apartment mode.";
}
if (err != null)
{
trayMessage("Character fix failed", err, ToolTipIcon.Error);
}
}
private void trayMessage(String title, String message, ToolTipIcon icon)
{
notifyIcon1.ShowBalloonTip(
1000,
title,
message,
icon
);
}
#endregion
#region Methods - Public
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
protected override void WndProc(ref Message m)
{
switch ((RAD.ClipMon.Win32.Msgs)m.Msg)
{
//
// The WM_DRAWCLIPBOARD message is sent to the first window
// in the clipboard viewer chain when the content of the
// clipboard changes. This enables a clipboard viewer
// window to display the new content of the clipboard.
//
case RAD.ClipMon.Win32.Msgs.WM_DRAWCLIPBOARD:
Debug.WriteLine("WindowProc DRAWCLIPBOARD: " + m.Msg, "WndProc");
if (!clipInserting)
{
ProcessClipboard();
}
//
// Each window that receives the WM_DRAWCLIPBOARD message
// must call the SendMessage function to pass the message
// on to the next window in the clipboard viewer chain.
//
RAD.ClipMon.Win32.User32.SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
break;
//
// The WM_CHANGECBCHAIN message is sent to the first window
// in the clipboard viewer chain when a window is being
// removed from the chain.
//
case RAD.ClipMon.Win32.Msgs.WM_CHANGECBCHAIN:
Debug.WriteLine("WM_CHANGECBCHAIN: lParam: " + m.LParam, "WndProc");
// When a clipboard viewer window receives the WM_CHANGECBCHAIN message,
// it should call the SendMessage function to pass the message to the
// next window in the chain, unless the next window is the window
// being removed. In this case, the clipboard viewer should save
// the handle specified by the lParam parameter as the next window in the chain.
//
// wParam is the Handle to the window being removed from
// the clipboard viewer chain
// lParam is the Handle to the next window in the chain
// following the window being removed.
if (m.WParam == _ClipboardViewerNext)
{
//
// If wParam is the next clipboard viewer then it
// is being removed so update pointer to the next
// window in the clipboard chain
//
_ClipboardViewerNext = m.LParam;
}
else
{
RAD.ClipMon.Win32.User32.SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
}
break;
default:
//
// Let the form process the messages that we are
// not interested in
//
base.WndProc(ref m);
break;
}
}
#endregion
#region Event Handlers - Menu
private void itmExit_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void itmHide_Click(object sender, System.EventArgs e)
{
toggleWindow();
}
// toggle replacements on/off
private void menuItem1_Click(object sender, EventArgs e)
{
MenuItem clicked = (System.Windows.Forms.MenuItem)sender;
enabled = !enabled;
clicked.Checked = enabled;
for (int i = 0; i < CMOptions.Length; ++i)
{
CMOptions[i].menuItem.Enabled = enabled;
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
toggleWindow(); // toggle window when double clicked
}
}
private void itmCancelMenu_Click(object sender, System.EventArgs e)
{
// Do nothing - Cancel the menu
}
private void frmMain_Resize(object sender, System.EventArgs e)
{
if ((this.WindowState == FormWindowState.Minimized) && (this.Visible == true))
{
// hide when minimised
this.Visible = false;
itmHide.Text = "Show";
}
}
private void clipboardOptionChange(object sender, EventArgs e)
{
MenuItem clicked = (System.Windows.Forms.MenuItem)sender;
for (int i = 0; i < CMOptions.Length; ++i)
{
if (CMOptions[i].text == clicked.Text)
{
clicked.Checked = !clicked.Checked;
setOption(ref CMOptions[i], clicked.Checked);
}
}
ProcessClipboard();
}
// can't be bothered making this generic, to be honest :p
private void clipboardOptionBtnChange(object sender, EventArgs e)
{
CheckBox clicked = (System.Windows.Forms.CheckBox)sender;
for (int i = 0; i < CMOptions.Length; ++i)
{
if (CMOptions[i].shorttext == clicked.Text)
{
setOption(ref CMOptions[i], clicked.Checked);
}
}
ProcessClipboard();
}
private void setOption(ref optionFlag option, bool enabled)
{
option.active = enabled;
option.menuItem.Checked = enabled;
option.buttonToggle.Checked = enabled;
}
#endregion
#region Event Handlers - Internal
private void frmMain_Load(object sender, System.EventArgs e)
{
// Help for the options
toolTip1 = new ToolTip();
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 500;
toolTip1.ReshowDelay = 500;
this.cmnuTray.MenuItems.Clear();
for (int i = 0; i < CMOptions.Length; ++i)
{
optionFlag option = CMOptions[i];
// add system tray menuitem
MenuItem it = new System.Windows.Forms.MenuItem(option.text);
it.Checked = option.active;
it.Click += new EventHandler(clipboardOptionChange);
this.cmnuTray.MenuItems.Add(it);
// add form button
CheckBox btn = new System.Windows.Forms.CheckBox();
btn.Appearance = System.Windows.Forms.Appearance.Button;
btn.Location = new System.Drawing.Point(8, 8 + i * 32);
btn.Name = "btn" + i;
btn.Size = new System.Drawing.Size(95, 23);
btn.TabIndex = 1;
btn.UseVisualStyleBackColor = true;
btn.Text = option.shorttext;
toolTip1.SetToolTip(btn, option.desc);
btn.Checked = option.active;
btn.Click += new EventHandler(clipboardOptionBtnChange);
this.Controls.Add(btn);
CMOptions[i].menuItem = it;
CMOptions[i].buttonToggle = btn;
}
this.cmnuTray.MenuItems.Add(itmSep1);
this.cmnuTray.MenuItems.Add(itmHide);
this.cmnuTray.MenuItems.Add(toggleEnabled);
this.cmnuTray.MenuItems.Add(itmSep2);
this.cmnuTray.MenuItems.Add(itmExit);
// add link URLs
this.linkLabel1.Links.Add(0, 38, "pospi.spadgos.com/projects/cp1252fixer");
loadPrefs();
RegisterClipboardViewer();
// hide window when loading up
this.Visible = false;
}
private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnregisterClipboardViewer();