-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfancyTips.c
2302 lines (2056 loc) · 70.4 KB
/
fancyTips.c
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
// -----------------------------------------------------------------------
// T O P O F F I L E
// -----------------------------------------------------------------------
// MIT License:
//
// Copyright 2023 Steven Valliere
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// -----------------------------------------------------------------------
// ------------------------------------------------------------------------
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
// ------------------------------------------------------------------------
#pragma warning( disable : 4100 ) // unreferenced formal parameter
#pragma warning( disable : 4200 ) // nonstandard extension used : zero-sized array in struct/union
#pragma warning( disable : 4210 ) // nonstandard extension used: function given file scope
#include "fancyTips.h"
// -- Functions from multimon.c --
extern void mm_GetProcs( void );
extern BOOL mm_GetCurrentScreenSize( HWND hwnd, SIZE *ps );
extern BOOL mm_GetCurrentScreenInfo( HWND hwnd, POINT *pp, SIZE *ps );
extern BOOL mm_GetScreenRectFromPoint( POINT *pp, RECT *prc );
#if defined(FT_DECLSPEC)
#undef FT_DECLSPEC
#endif
#if defined(_DLL)
#include "resource.h"
extern HINSTANCE ghDLL;
#define FT_DECLSPEC __declspec(dllexport)
#endif
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
static void dbg_printf( const char *szFormat, ... )
{
char szLine[1024];
va_list args;
va_start( args, szFormat );
{
vsnprintf( szLine, sizeof(szLine), szFormat, args );
}
va_end( args );
OutputDebugString( szLine );
return;
}
//------------------------------------------------------------------------------
static LPSTR _GetSystemErrorText( LPSTR lpMsgBuf, size_t sMsgBufLen, DWORD dwErrorCode )
{
char *cp;
DWORD dwLen;
if( sMsgBufLen )
{
dwLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwErrorCode,
MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
lpMsgBuf,
(DWORD)sMsgBufLen,
NULL );
if( dwLen )
{
// Remove any trailing whitespace/cr/lf
cp = lpMsgBuf + (dwLen - 1);
while( dwLen && isspace( (BYTE)(*cp) ) )
{
*cp = '\0';
cp--;
dwLen--;
}
}
else
{
char szTemp[256];
sprintf_s( szTemp, sizeof(szTemp), "%#8.8x (%u): Unknown system error code",
dwErrorCode, dwErrorCode );
sprintf_s( lpMsgBuf, sMsgBufLen, "%s", szTemp );
}
return lpMsgBuf;
}
return NULL;
}
// ------------------------------------------------------------------------
#if !defined(HANDLE_WM_MOUSEHOVER)
// ------------------------------------------------------------------------
// Missing WM_MOUSEHOVER message cracker...
// ------------------------------------------------------------------------
/* void Cls_OnMouseHover(HWND hwnd, UINT state, int x, int y) */
#define HANDLE_WM_MOUSEHOVER(hwnd, wParam, lParam, fn) \
((fn)((hwnd),(UINT)(wParam),(int)(short)LOWORD(lParam),(int)(short)HIWORD(lParam)), 0L)
#define FORWARD_WM_MOUSEHOVER(hwnd, state, x, y, fn) \
(void)(fn)((hwnd), WM_MOUSEHOVER,(WPARAM)(UINT)(state), MAKELPARAM((cx),(cy)))
#endif
#if !defined(HANDLE_WM_MOUSELEAVE)
// ------------------------------------------------------------------------
// Missing WM_MOUSELEAVE message cracker...
// ------------------------------------------------------------------------
/* void Cls_OnMouseLeave(HWND hwnd) */
#define HANDLE_WM_MOUSELEAVE(hwnd, wParam, lParam, fn) \
((fn)((hwnd)), 0L)
#define FORWARD_WM_MOUSELEAVE(hwnd, state, x, y, fn) \
(void)(fn)((hwnd), WM_MOUSELEAVE, 0, 0 )
#endif
// ------------------------------------------------------------------------
#define MaxFancyFonts 16
#define MaxFancyName 32
#define MaxFancyTabs 32
#define MaxFancyControls 32
#define FancySegmentChunk 32
#define MaxFancySegLength 1024
#define DefFancyTab 8
#define DefFancyMargin 8
// ------------------------------------------------------------------------
typedef struct FancyFont_s
{
char szName[MaxFancyName];
LOGFONT lf;
TEXTMETRIC tm;
HFONT hf;
int tabChars;
int tab[MaxFancyTabs];
int ctab;
} FancyFont;
typedef enum FancyControl_e
{
fcNone,
fcUnknown = fcNone,
fcFgColor,
fcBgColor,
fcFont,
fcAlign,
fcColumn,
fcBreak,
fcLeftAt,
fcCenterAt,
fcRightAt,
fcDecimalAt,
fcNoEscape,
fcEnd = 0x08000
} FancyCtrlType;
typedef struct FancyControl_s
{
FancyCtrlType eType; // Control type
DWORD val; // New control value
DWORD prev; // Previous value, restored when this control is closed
} FancyControl;
// Special values for iAt positions
#define ffAtMask 0x0000FFFF
#define ffAtPrevPos 0x0000FFFF
#define ffAtCenter 0x0000FFFE
#define IsAtPrevPos(iAt) (ffAtPrevPos==((iAt)&ffAtMask))
#define IsAtCenter(iAt) (ffAtCenter==((iAt)&ffAtMask))
#define IsAtFraction(iAt) (0!=((iAt)&0x00008000))
#define MakeAtFraction(denom,numer) ((WORD)(0x00008000|MAKEWORD((numer),(denom))))
#define GetAtNumer(iAt) (HIBYTE((iAt)&0x7FFF))
#define GetAtDenom(iAt) (LOBYTE((iAt)&0x7FFF))
typedef struct FancyLineSegment_s
{
UINT align;
int iAlign;
FancyCtrlType eAt;
int iAt;
BOOL bInPosTag;
FancyFont *pff;
POINT pAnchor;
RECT r;
SIZE s;
COLORREF crFg;
COLORREF crBg;
char sz[MaxFancySegLength];
int len;
BOOL bEndOfLine;
} FancyLineSegment;
typedef struct FancyData_s
{
HINSTANCE hinst;
HWND hOwner;
HWND hTip;
HWND hwnd;
POINT pTip;
RECT rTip;
BOOL bVisible;
int idTip;
volatile BOOL bTipReady; // TRUE when the tip is ready to be drawn, false while tip is being assembled
HDC hdc;
HBITMAP hbmNew;
HBITMAP hbmPrev;
RECT rBuf;
HBRUSH hbrBg;
SIZE sIcon; // Icon size to use for custom icons
FancyFont f[MaxFancyFonts];
int cf; // Number of fonts that have been registered
int leftEdge;
int xMarg;
int yMarg;
COLORREF crDefFg; // Default foreground
COLORREF crDefBg; // Default background
COLORREF crFg; // Current foreground
COLORREF crBg; // Current background
BOOL bNoEscape; // TRUE when escape character processing is disabled
BOOL bInPosTag; // TRUE when we are processing within a position tag
int iAlign; // Current text alignment
int iFont; // Currently selected font
int iAt; // Current fancy alignment position
int iAtPrev; // Previous fancy alignment position
FancyCtrlType eAt; // Current fancy alignment
POINT p; // Current location where text emission begins
SIZE sLine; // Current height and width of the text
SIZE sMax; // Total height and width of the text
FancyControl fc[MaxFancyControls];
int cfc; // Fancy control 'stack'
FancyLineSegment *seg;
int cSeg;
int maxSegments;
} FancyData;
#define IsEndSymbol( sym )((0==((sym)&fcEnd))?FALSE:TRUE)
#define MAKEDWORD(L,H)((DWORD)(((WORD)(((DWORD_PTR)(L)) & 0xffff)) |((DWORD)((WORD)(((DWORD_PTR)(H)) & 0xffff))) << 16))
//-------------------------------------------------------------------------
static int _FileExists( const char *szfqn )
{
DWORD dw;
dw = GetFileAttributes( szfqn );
if( INVALID_FILE_ATTRIBUTES == dw )
{
SetLastError( ERROR_FILE_NOT_FOUND );
return 0;
}
else if( dw & (FILE_ATTRIBUTE_DEVICE // object is a device
|FILE_ATTRIBUTE_DIRECTORY // object is a directory
|FILE_ATTRIBUTE_OFFLINE) ) // object is offline
{
SetLastError( ERROR_FILE_EXISTS );
return -1;
}
SetLastError( ERROR_SUCCESS );
return 1;
}
//-------------------------------------------------------------------------
static struct ColorNameTable_s
{
char *sz;
COLORREF cr;
} s_ColorName[] =
{
{ "AliceBlue" , RGB(0xF0,0xF8,0xFF) },
{ "AntiqueWhite" , RGB(0xFA,0xEB,0xD7) },
{ "Aqua" , RGB(0x00,0xFF,0xFF) },
{ "Aquamarine" , RGB(0x70,0xDB,0x93) },
{ "Azure" , RGB(0xF0,0xFF,0xFF) },
{ "Beige" , RGB(0xF5,0xF5,0xDC) },
{ "Bisque" , RGB(0xFF,0xE4,0xC4) },
{ "Black" , RGB(0x00,0x00,0x00) },
{ "BlanchedAlmond" , RGB(0xFF,0xFF,0xCD) },
{ "Blue" , RGB(0x00,0x00,0xFF) },
{ "BlueBlack" , RGB(0x2F,0x2F,0x4F) },
{ "BlueViolet" , RGB(0x9E,0x5E,0x9E) },
{ "BrightGreenYellow" , RGB(0xAD,0xFF,0x2F) },
{ "Brown" , RGB(0xA5,0x2A,0x2A) },
{ "Burlywood" , RGB(0xDE,0xB8,0x87) },
{ "CadetBlue" , RGB(0x5F,0x9E,0xA0) },
{ "Caucasian" , RGB(0xFF,0xA0,0x7A) },
{ "Chartreuse" , RGB(0x7F,0xFF,0x00) },
{ "Chocolate" , RGB(0xD2,0x69,0x1E) },
{ "Coral" , RGB(0xFF,0x7F,0x50) },
{ "CornflowerBlue" , RGB(0x64,0x95,0xED) },
{ "Cornsilk" , RGB(0xFF,0xF8,0xDC) },
{ "Crimson" , RGB(0xDC,0x14,0x3C) },
{ "Cyan" , RGB(0x00,0x80,0x80) },
{ "DarkBlue" , RGB(0x00,0x00,0x80) },
{ "DarkCornflowerBlue" , RGB(0x42,0x42,0x6E) },
{ "DarkCyan" , RGB(0x00,0x8B,0x8B) },
{ "DarkGoldenrod" , RGB(0xB8,0x86,0x0B) },
{ "DarkGray" , RGB(0xA9,0xA9,0xA9) },
{ "DarkGreen" , RGB(0x00,0x64,0x00) },
{ "DarkIndianRed" , RGB(0x4F,0x2F,0x2F) },
{ "DarkKhaki" , RGB(0xBD,0xB7,0x6B) },
{ "DarkMagenta" , RGB(0x8B,0x00,0x8B) },
{ "DarkOliveGreen" , RGB(0x55,0x6B,0x2F) },
{ "DarkOrange" , RGB(0xFF,0x8C,0x00) },
{ "DarkOrchid" , RGB(0x99,0x32,0xCC) },
{ "DarkRed" , RGB(0x8B,0x00,0x00) },
{ "DarkSalmon" , RGB(0xE9,0x96,0x7A) },
{ "DarkSeaGreen" , RGB(0x8F,0xBC,0x8F) },
{ "DarkSlateBlue" , RGB(0x48,0x3D,0x8B) },
{ "DarkSlateGray" , RGB(0x2F,0x4F,0x4F) },
{ "DarkSlateGrey" , RGB(0x2F,0x4F,0x4F) },
{ "DarkTurquoise" , RGB(0x70,0x93,0xDB) },
{ "DarkViolet" , RGB(0x94,0x00,0xD3) },
{ "DeepPink" , RGB(0xFF,0x14,0x93) },
{ "DeepSkyBlue" , RGB(0x00,0xBF,0xFF) },
{ "DGray" , RGB(0x40,0x40,0x40) },
{ "DimGray" , RGB(0x54,0x54,0x54) },
{ "DimGrey" , RGB(0x54,0x54,0x54) },
{ "DodgerBlue" , RGB(0x1E,0x90,0xFF) },
{ "FadedBlueViolet" , RGB(0x8A,0x2B,0xE2) },
{ "Firebrick" , RGB(0x8E,0x23,0x23) },
{ "FloralWhite" , RGB(0xFF,0xFA,0xF0) },
{ "ForestGreen" , RGB(0x23,0x8E,0x23) },
{ "Fuchsia" , RGB(0xFF,0x00,0xFF) },
{ "Gainsboro" , RGB(0xDC,0xDC,0xDC) },
{ "GhostWhite" , RGB(0xF8,0xF8,0xFF) },
{ "Gold" , RGB(0xCC,0x7E,0x31) },
{ "Goldenrod" , RGB(0xDA,0xA5,0x20) },
{ "Gray" , RGB(0x80,0x80,0x80) },
{ "Green" , RGB(0x00,0x80,0x00) },
{ "GreenYellow" , RGB(0x93,0xDB,0x70) },
{ "Grey" , RGB(0xBF,0xBF,0xBF) },
{ "Honeydew" , RGB(0xF0,0xFF,0xF0) },
{ "HotPink" , RGB(0xFF,0x69,0xB4) },
{ "IndianRed" , RGB(0xCD,0x5C,0x5C) },
{ "Indigo" , RGB(0x4B,0x00,0x82) },
{ "Ivory" , RGB(0xFF,0xF0,0xF0) },
{ "Khaki" , RGB(0x9E,0x9E,0x5E) },
{ "Lavender" , RGB(0xE6,0xE6,0xFA) },
{ "LavenderBlush" , RGB(0xFF,0xF0,0xF5) },
{ "Lawngreen" , RGB(0x7C,0xFC,0x00) },
{ "LBlack" , RGB(0x20,0x20,0x20) },
{ "LBlue" , RGB(0x00,0x00,0xFF) },
{ "LCyan" , RGB(0x00,0xFF,0xFF) },
{ "LemonChiffon" , RGB(0xFF,0xFA,0xCD) },
{ "LGray" , RGB(0xC0,0xC0,0xC0) },
{ "LGreen" , RGB(0x00,0xFF,0x00) },
{ "LightAquamarine" , RGB(0x7F,0xFF,0xD4) },
{ "LightBlue" , RGB(0xAD,0xD8,0xE6) },
{ "LightCoral" , RGB(0xF0,0x80,0x80) },
{ "LightCyan" , RGB(0xE0,0xFF,0xFF) },
{ "LightFirebrick" , RGB(0xB2,0x22,0x22) },
{ "LightGold" , RGB(0xFF,0xD7,0x00) },
{ "LightGoldenrodYellow", RGB(0xFA,0xFA,0xD2) },
{ "LightGray" , RGB(0xA8,0xA8,0xA8) },
{ "LightGreen" , RGB(0x90,0xEE,0x90) },
{ "LightGrey" , RGB(0xA8,0xA8,0xA8) },
{ "LightKhaki" , RGB(0xF0,0xE6,0x8C) },
{ "LightPink" , RGB(0xFF,0xC0,0xCB) },
{ "LightSalmon" , RGB(0xFA,0x80,0x72) },
{ "LightSeaGreen" , RGB(0x20,0xB2,0xAA) },
{ "LightSienna" , RGB(0xA0,0x52,0x2D) },
{ "LightSkyBlue" , RGB(0x87,0xCE,0xEB) },
{ "LightSlateGray" , RGB(0x77,0x88,0x99) },
{ "LightSteelBlue" , RGB(0x8E,0x8E,0xBC) },
{ "LightTurquoise" , RGB(0xAC,0xE9,0xE9) },
{ "LightViolet" , RGB(0xEE,0x82,0xEE) },
{ "LightWheat" , RGB(0xF5,0xDE,0xB3) },
{ "LightYellow" , RGB(0xFF,0xFF,0xE0) },
{ "Lime" , RGB(0x00,0xFF,0x00) },
{ "LimeGreen" , RGB(0x31,0xCC,0x31) },
{ "Linen" , RGB(0xFA,0xF0,0xE6) },
{ "LMagenta" , RGB(0xFF,0x00,0xFF) },
{ "LOrange" , RGB(0xFF,0xBE,0x7D) },
{ "LRed" , RGB(0xFF,0x00,0x00) },
{ "LWhite" , RGB(0xFF,0xFF,0xFF) },
{ "LYellow" , RGB(0xFF,0xFF,0x00) },
{ "Magenta" , RGB(0x80,0x00,0x80) },
{ "Maroon" , RGB(0x8E,0x23,0x6B) },
{ "MediumAquamarine" , RGB(0x31,0xCC,0x99) },
{ "MediumBlue" , RGB(0x31,0x31,0xCC) },
{ "MediumForestGreen" , RGB(0x6B,0x8E,0x23) },
{ "MediumGoldenrod" , RGB(0xE9,0xE9,0xAC) },
{ "MediumOrchid" , RGB(0xBA,0x55,0xD3) },
{ "MediumPurple" , RGB(0x93,0x70,0xDB) },
{ "MediumSeaBreen" , RGB(0x3C,0xB3,0x71) },
{ "MediumSeaGreen" , RGB(0x42,0x6E,0x42) },
{ "MediumSlateBlue" , RGB(0x7B,0x68,0xEE) },
{ "MediumSpringGreen" , RGB(0x00,0xFA,0x9A) },
{ "MediumTan" , RGB(0xDB,0x93,0x70) },
{ "MediumTurquoise" , RGB(0x48,0xD1,0xCC) },
{ "MediumVioletRed" , RGB(0xC7,0x15,0x85) },
{ "MidnightBlue" , RGB(0x19,0x19,0x70) },
{ "MintCream" , RGB(0xF5,0xFF,0xFA) },
{ "MistyRose" , RGB(0xFF,0xE4,0xE1) },
{ "Moccasin" , RGB(0xFF,0xE4,0xB5) },
{ "NavajoWhite" , RGB(0xFF,0xDE,0xAD) },
{ "Navy" , RGB(0x23,0x23,0x8E) },
{ "NavyBlue" , RGB(0x23,0x23,0x8E) },
{ "Oldlace" , RGB(0xFD,0xF5,0xE6) },
{ "Olive" , RGB(0x80,0x80,0x00) },
{ "Olivedrab" , RGB(0x6B,0x8E,0x23) },
{ "Orange" , RGB(0xFF,0xA5,0x00) },
{ "OrangeRed" , RGB(0xFF,0x45,0x00) },
{ "Orchid" , RGB(0xDA,0x70,0xD6) },
{ "PaleGoldenrod" , RGB(0xEE,0xE8,0xAA) },
{ "PaleGreen" , RGB(0x98,0xFB,0x98) },
{ "PaleTurquoise" , RGB(0xAF,0xEE,0xEE) },
{ "PaleVioletred" , RGB(0xDB,0x70,0x93) },
{ "PapayaWhip" , RGB(0xFF,0xEF,0xD5) },
{ "PeachPuff" , RGB(0xFF,0xEF,0xD5) },
{ "Peru" , RGB(0xCD,0x85,0x3F) },
{ "Pink" , RGB(0xBC,0x8E,0x8E) },
{ "Plum" , RGB(0xDD,0xA0,0xDD) },
{ "PowderBlue" , RGB(0xB0,0xE0,0xE6) },
{ "Purple" , RGB(0x80,0x00,0x80) },
{ "Red" , RGB(0x80,0x00,0x00) },
{ "RosyBrown" , RGB(0xBC,0x8F,0x8F) },
{ "RoyalBlue" , RGB(0x41,0x69,0xE1) },
{ "SaddleBrown" , RGB(0x8B,0x45,0x13) },
{ "Salmon" , RGB(0x6E,0x42,0x42) },
{ "SandyBrown" , RGB(0xF4,0xA4,0x60) },
{ "SeaGreen" , RGB(0x23,0x8E,0x6B) },
{ "Seashell" , RGB(0xFF,0xF5,0xEE) },
{ "Sienna" , RGB(0x8E,0x6B,0x23) },
{ "Silver" , RGB(0xC0,0xC0,0xC0) },
{ "SkyBlue" , RGB(0x31,0x99,0xCC) },
{ "SlateBlue" , RGB(0x00,0x7E,0xFF) },
{ "SlateGray" , RGB(0x70,0x80,0x90) },
{ "Snow" , RGB(0xFF,0xFA,0xFA) },
{ "SpringGreen" , RGB(0x00,0xFF,0x7F) },
{ "SteelBlue" , RGB(0x23,0x6B,0x8E) },
{ "Tan" , RGB(0xD2,0xB4,0x8C) },
{ "Teal" , RGB(0x00,0x80,0x80) },
{ "Thistle" , RGB(0xD8,0xBF,0xD8) },
{ "Tomato" , RGB(0xFD,0x63,0x47) },
{ "Turquoise" , RGB(0x40,0xE0,0xD0) },
{ "Violet" , RGB(0x4F,0x2F,0x4F) },
{ "VioletRed" , RGB(0xCC,0x31,0x99) },
{ "Wheat" , RGB(0xD8,0xD8,0xBF) },
{ "White" , RGB(0x80,0x80,0x80) },
{ "WhiteSmoke" , RGB(0xF5,0xF5,0xF5) },
{ "Yellow" , RGB(0x80,0x80,0x00) },
{ "YellowGreen" , RGB(0x99,0xCC,0x31) },
};
static int s_nColors = sizeof(s_ColorName) / sizeof(s_ColorName[0]);
// ------------------------------------------------------------------------
static BYTE _CharToHex( char ch )
{
switch( ch )
{
case '0': return 0x00;
case '1': return 0x01;
case '2': return 0x02;
case '3': return 0x03;
case '4': return 0x04;
case '5': return 0x05;
case '6': return 0x06;
case '7': return 0x07;
case '8': return 0x08;
case '9': return 0x09;
case 'A': return 0x0A;
case 'B': return 0x0B;
case 'C': return 0x0C;
case 'D': return 0x0D;
case 'E': return 0x0E;
case 'F': return 0x0F;
case 'a': return 0x0A;
case 'b': return 0x0B;
case 'c': return 0x0C;
case 'd': return 0x0D;
case 'e': return 0x0E;
case 'f': return 0x0F;
}
return 0;
}
// ------------------------------------------------------------------------
// String compare ignoring whitespace, punctuation and case
static int _strispccmp( char *a, char *b )
{
// First, parameter safety...
if( !a && !b ) return 0; // both strings NULL
if( !a ) return +1; // B greater than A
if( !b ) return -1; // A greater than B
// Now, compare strings, ignoring case *AND* non-alpha-numeric chars
while( *a && *b )
{
// Skip non-alpha-numeric chars
while( *a && (isspace((BYTE)(*a)) || ispunct((BYTE)(*a))) )
{
a++;
}
while( *b && (isspace((BYTE)(*b)) || ispunct((BYTE)(*b))) )
{
b++;
}
// Are we done?
if( !(*a) || !(*b) )
{ // One of the strings has ended, we're outta here.
break;
}
if( tolower((BYTE)(*a)) != tolower((BYTE)(*b)) )
{ // Characters are different, we're done here.
break;
}
// Next char in each string
a++;
b++;
}
return tolower((BYTE)(*a)) - tolower((BYTE)(*b));
}
//------------------------------------------------------------------------
static COLORREF _StringToColor( char *sz, COLORREF crDefault )
{
union bydw_u
{
DWORD dw;
BYTE by[4];
} hex = {0};
COLORREF cr;
int cv[3];
int c;
char *cp;
char *cpDelim;
char *cpContext;
char szColor[256];
cr = crDefault;
if( sz && (*sz) )
{ // There's a string included, check it out.
if( ('#' == *sz) && *(sz+1) )
{
switch( strlen(sz) )
{
case 4: // HTML / CSS - style color string "#rgb"
hex.by[2] = (_CharToHex( sz[1] ) << 4) | _CharToHex( sz[1] );
hex.by[1] = (_CharToHex( sz[2] ) << 4) | _CharToHex( sz[2] );
hex.by[0] = (_CharToHex( sz[3] ) << 4) | _CharToHex( sz[3] );
cr = RGB( hex.by[2], hex.by[1], hex.by[0] );
break;
case 7: // Assume HTML/CSS-style color string "#rrggbb"
hex.dw = strtoul( sz+1, NULL, 16 );
cr = RGB( hex.by[2], hex.by[1], hex.by[0] );
break;
}
}
else if( !_strnicmp( sz, "rgb", 3 ) )
{ // work in a WRITABLE buffer for strtok
snprintf( szColor, sizeof(szColor), "%.*s", (int)sizeof(szColor)-1, sz );
sz = szColor;
memset( cv, 0, sizeof(cv) );
cpDelim = "() ,";
cp = strtok_s( sz+3, cpDelim, &cpContext );
if( cp )
{
c = 0;
do
{
cv[c++] = strtol( cp, NULL, 0 );
cp = strtok_s( NULL, cpDelim, &cpContext );
}while( cp && (c < 3) );
}
cr = RGB( cv[0], cv[1], cv[2] );
}
else // See if we know the name...
{
for( c = 0; c < s_nColors; c++ )
{
if( !_strispccmp( s_ColorName[c].sz, sz ) )
{
cr = s_ColorName[c].cr;
break;
}
}
}
}
return cr;
}
// -------------------------------------------------------------------------
static DWORD _GetFancyFont( FancyData *pfd, char *szVal )
{
char *ep;
int f = 0;
int x;
if( isdigit( *szVal ) )
{
f = strtol( szVal, &ep, 10 );
if( ep && *ep )
{
goto FontName;
}
if( (f < 0) || (pfd->cf <= f) )
{ // Out of range, use default font
f = 0;
}
}
else
{
FontName:
for( f = 0, x = 0; x < pfd->cf; x++ )
{
if( !_stricmp( szVal, pfd->f[x].szName ) )
{
f = x;
break;
}
}
}
return f;
}
// -------------------------------------------------------------------------
static DWORD _GetFancyAlign( char *szVal )
{
if( !_stricmp( szVal, "left" ) )
{
return TA_LEFT;
}
if( !_stricmp( szVal, "center" ) )
{
return TA_CENTER;
}
if( !_stricmp( szVal, "right" ) )
{
return TA_RIGHT;
}
return TA_LEFT;
}
// -------------------------------------------------------------------------
static void _CreateFancyFont( HWND hwnd, FancyFont *pff, LOGFONT *plf, char *szName )
{
HDC hdc;
HFONT hf;
pff->lf = *plf;
pff->hf = CreateFontIndirect( &pff->lf );
strcpy_s( pff->szName, sizeof(pff->szName), szName );
hdc = GetDC( hwnd );
{
hf = SelectObject( hdc, pff->hf );
GetTextMetrics( hdc, &pff->tm );
SelectObject( hdc, hf );
}
ReleaseDC( hwnd, hdc );
// Set default tab stops for the new font
for( pff->ctab = 0; pff->ctab < MaxFancyTabs; pff->ctab++ )
{
pff->tab[pff->ctab] = (pff->ctab * 8) * pff->tm.tmAveCharWidth;
}
return;
}
// -------------------------------------------------------------------------
static FancyCtrlType _StrToCtrlType( FancyData *pfd, char *szCtrl )
{
FancyCtrlType eType = fcUnknown;
BOOL bEndSymbol;
int f;
FancyFont *pff;
bEndSymbol = ( '/' == *szCtrl );
if( bEndSymbol )
{
szCtrl++;
}
if( !_stricmp( szCtrl, "ff" ) )
{
eType = fcFont;
}
else if( !_stricmp( szCtrl, "fg" ) )
{
eType = fcFgColor;
}
else if( !_stricmp( szCtrl, "bg" ) )
{
eType = fcBgColor;
}
else if( !_stricmp( szCtrl, "sc" ) )
{
eType = fcColumn;
}
else if( !_stricmp( szCtrl, "a" ) )
{
eType = fcAlign;
}
else if( !_stricmp( szCtrl, "l@" ) )
{
eType = fcLeftAt;
}
else if( !_stricmp( szCtrl, "c@" ) )
{
eType = fcCenterAt;
}
else if( !_stricmp( szCtrl, "r@" ) )
{
eType = fcRightAt;
}
else if( !_stricmp( szCtrl, "d@" ) )
{
eType = fcDecimalAt;
}
else if( !_stricmp( szCtrl, "br" ) )
{
eType = fcBreak;
}
else if( !_stricmp( szCtrl, "ne" ) )
{
eType = fcNoEscape;
}
else if( NULL != pfd ) // Look for a font name, which we will also allow as a tag
{
pff = pfd->f;
for( f = 0; f < pfd->cf; f++ )
{
if( !_stricmp( szCtrl, pff[f].szName ) )
{
eType = fcFont;
break;
}
}
}
if( bEndSymbol )
{
eType |= fcEnd;
}
return eType;
}
// -------------------------------------------------------------------------
static int _GetAtValue( FancyCtrlType eType, FancyData *pfd, char *szVal )
{
int denom;
int numer;
int val;
char *ep;
UNREFERENCED_PARAMETER(eType);
if( '^' == *szVal )
{ // @ center
//val = pfd->xMarg + ((pfd->sMax.cx - pfd->xMarg) / 2);
val = ffAtCenter;
}
else if( '*' == *szVal )
{ // most recently generated location
//val = pfd->iAtPrev;
val = ffAtPrevPos;
}
else if( strchr( szVal, '/' ) )
{ // @ fractional part of current width
numer = strtol( szVal, &ep, 10 );
if( ep && ('/' == *ep) )
{
denom = strtol( ep+1, NULL, 10 ) ;
//val = pfd->xMarg + MulDiv( pfd->sMax.cx - pfd->xMarg, denom, numer );
val = (int)MakeAtFraction( numer, denom );
}
else
{ // Missing numerator means we'll center by default
//val = pfd->xMarg + ((pfd->sMax.cx - pfd->xMarg) / 2);
val = ffAtCenter;
}
}
else
{ // @ specific column
//val = strtol( szVal, NULL, 10 ) * pfd->f[pfd->iFont].tm.tmAveCharWidth;
val = strtol( szVal, NULL, 10 );
}
pfd->iAtPrev = val;
return val;
}
// -------------------------------------------------------------------------
static int _GetFancyCtrlType( FancyData *pfd, FancyControl *pfc, char **sz )
{
char szCtrl[256];
char szVal[256];
char *cpTok;
char *cp;
BOOL bDefaultValue = FALSE;
cp = *sz;
cpTok = szCtrl;
while( *cp && ('>' != *cp) )
{
switch( *cp )
{
case '\n' : // Error! not allowed within control symbol
dbg_printf( "FancyTips: Newline not allowed within control symbol\n" );
return -1;
case '=' : // Control=value separator
case ':' : // Control:value separator
*cpTok = '\0';
cpTok = szVal; // Switch to collecting the value
cp++;
if( '>' == *cp )
{ // There's no value present
bDefaultValue = TRUE;
szVal[0] = '\0';
continue;
}
break;
}
*cpTok = *cp;
cp++;
cpTok++;
}
*cpTok = '\0';
cp++;
*sz = cp;
// Determine control type and value...
pfc->eType = _StrToCtrlType( pfd, szCtrl );
switch( pfc->eType )
{
case fcNoEscape: pfc->val = TRUE; break; // Any value provided will be silently ignored
case fcFont:
if( _stricmp( szCtrl, "ff" ) )
{ // This was a font name, move the tag to the value
strcpy_s( szVal, sizeof(szVal), szCtrl );
bDefaultValue = FALSE;
}
// else this was a <ff> tag, so choose the default font
case fcFgColor:
case fcBgColor:
case fcColumn:
case fcAlign:
case fcLeftAt:
case fcCenterAt:
case fcRightAt:
case fcDecimalAt:
case fcBreak:
if( bDefaultValue )
{
switch( pfc->eType )
{
case fcFont: pfc->val = 0; break;
case fcFgColor: pfc->val = pfd->crDefFg; break;
case fcBgColor: pfc->val = pfd->crDefBg; break;
case fcColumn: pfc->val = pfd->p.x; break;
case fcAlign: pfc->val = TA_LEFT; break;
case fcLeftAt: pfc->val = pfd->p.x; break;
case fcDecimalAt:
case fcCenterAt: pfc->val = (pfd->p.x + pfd->sMax.cx) / 2; break;
case fcRightAt: pfc->val = pfd->sMax.cx; break;
}
}
else
{
switch( pfc->eType )
{
case fcFont: pfc->val = _GetFancyFont( pfd, szVal ); break;
case fcFgColor: pfc->val = (DWORD)_StringToColor( szVal, pfd->crDefFg ); break;
case fcBgColor: pfc->val = (DWORD)_StringToColor( szVal, pfd->crDefBg ); break;
case fcColumn: pfc->val = strtoul( szVal, NULL, 10 ); break;
case fcAlign: pfc->val = _GetFancyAlign( szVal ); break;
case fcLeftAt:
case fcDecimalAt:
case fcCenterAt:
case fcRightAt: pfc->val = _GetAtValue( pfc->eType, pfd, szVal ); break;
}
}
break;
case fcUnknown:
dbg_printf( "FancyTips: Unknown control type '%s' (end)\n", szCtrl );
return -1;
case fcUnknown|fcEnd:
dbg_printf( "FancyTips: Unknown control type '%s'\n", szCtrl );
return -1;
}
return 0;
}
//-------------------------------------------------------------------------
static void _FancyTextOut( HDC hdc, int x, int y, BYTE byCharSet, UINT uOpts,
LPRECT pr, char *str, int len, INT *lpDx )
{
RECT r = *pr;
r.top -= 1;
y += 1;
if( OEM_CHARSET == byCharSet )
{ // Use ASCII text emitter
ExtTextOutA( hdc, x, y, uOpts, &r, str, len, lpDx );
}
else
{ // Use WIDE text emitter
WCHAR wsz[1024];
OemToCharBuffW( str, wsz, len );
ExtTextOutW( hdc, x, y, uOpts, &r, wsz, len, lpDx );
}
return;
}
// -------------------------------------------------------------------------
static void _NextFancySegment( FancyData *pfd )
{
pfd->cSeg += 1;
if( pfd->cSeg == pfd->maxSegments )
{
pfd->maxSegments += FancySegmentChunk;
pfd->seg = (FancyLineSegment *)realloc( pfd->seg, pfd->maxSegments * sizeof(FancyLineSegment) );
}
return;
}
// -------------------------------------------------------------------------
static void _CollectTextBlock( FancyData *pfd, char *sz, int len, BOOL bEndOfLine )
{
char *cp;
int dot;
if( 0 < len )
{
pfd->seg[pfd->cSeg].align = (fcNone == pfd->seg[pfd->cSeg].eAt) ? pfd->seg[pfd->cSeg].iAlign : TA_LEFT;
pfd->seg[pfd->cSeg].iAlign = pfd->iAlign;
pfd->seg[pfd->cSeg].iAt = pfd->iAt;
pfd->seg[pfd->cSeg].bInPosTag = pfd->bInPosTag;
pfd->seg[pfd->cSeg].pff = &pfd->f[pfd->iFont];
pfd->seg[pfd->cSeg].crFg = pfd->crFg;
pfd->seg[pfd->cSeg].crBg = pfd->crBg;
pfd->seg[pfd->cSeg].bEndOfLine = bEndOfLine;
if( pfd->bInPosTag && (fcNone == pfd->eAt) )
{
pfd->seg[pfd->cSeg].eAt = pfd->seg[pfd->cSeg-1].eAt;
}
else
{
pfd->seg[pfd->cSeg].eAt = pfd->eAt;
}
if( fcDecimalAt == pfd->eAt )
{ // Collect as two parts
cp = strchr( sz, '.' );
if( cp )
{
dot = (int)(cp - sz);
pfd->seg[pfd->cSeg].align = TA_LEFT;
pfd->seg[pfd->cSeg].len = dot;
pfd->seg[pfd->cSeg].eAt = fcRightAt;
strncpy_s( pfd->seg[pfd->cSeg].sz, MaxFancySegLength, sz, pfd->seg[pfd->cSeg].len );
pfd->seg[pfd->cSeg].bEndOfLine = FALSE;
_NextFancySegment( pfd );