-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEVICE.H
More file actions
778 lines (677 loc) · 19.1 KB
/
DEVICE.H
File metadata and controls
778 lines (677 loc) · 19.1 KB
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
/*
Project: Windows Class Library
Module: Device.H
Description: Graphic devices
Author: Martin Gäckler
Address: Hofmannsthalweg 14, A-4030 Linz
Web: https://www.gaeckler.at/
Copyright: (c) 1988-2025 Martin Gäckler
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, version 3.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
THIS SOFTWARE IS PROVIDED BY Martin Gäckler, Linz, Austria ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef WL_DEVICE_H
#define WL_DEVICE_H
// --------------------------------------------------------------------- //
// ----- switches ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#ifndef STRICT
#define STRICT 1
#endif
// --------------------------------------------------------------------- //
// ----- includes ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable: 4986 4820 4668 )
#endif
#include <windows.h>
#ifdef _MSC_VER
# pragma warning( pop )
#endif
#include <winlib/basicwin.h>
#include <winlib/font.h>
#include <winlib/pen.h>
#include <winlib/brush.h>
#include <winlib/bitmap.h>
#include <winlib/palette.h>
// --------------------------------------------------------------------- //
// ----- imported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module switches ----------------------------------------------- //
// --------------------------------------------------------------------- //
#ifdef __BORLANDC__
# pragma option -RT-
# pragma option -b
# pragma option -a4
# pragma option -pc
#endif
namespace winlib
{
// --------------------------------------------------------------------- //
// ----- constants ----------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- macros -------------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- type definitions ---------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class definitions --------------------------------------------- //
// --------------------------------------------------------------------- //
class Device
{
public:
enum HorizontalAlignment{ haLeft = TA_LEFT, haCenter = TA_CENTER, haRight = TA_RIGHT };
enum VerticalAlignment{ vaTop = TA_TOP, vaBaseline = TA_BASELINE , vaBottom = TA_BOTTOM };
protected:
HDC m_device;
private:
Pen m_pen;
Brush m_brush;
Font m_font;
int m_tabWidth;
GdiHandle<HRGN> m_clipRegion;
private:
// disallow
Device( const Device &src );
const Device & operator = ( const Device &src );
private:
void connect()
{
m_brush.connect( m_device );
m_pen.connect( m_device );
m_font.connect( m_device );
}
protected:
/*
the device is used for drawing on a printer or memory device
*/
Device( HDC context ) : m_brush( context ), m_pen( context ), m_font( context )
{
m_device = context;
setTextAlignment( haLeft, vaTop );
}
//public:
friend class MemoryDevice;
friend class Bitmap;
operator HDC () const
{
return m_device;
}
public:
~Device()
{
if( m_device )
{
DeleteDC( m_device );
}
}
operator bool () const
{
return m_device != nullptr;
}
bool operator ! () const
{
return m_device == nullptr;
}
void setTabWidth( int numChars )
{
TEXTMETRIC tm;
GetTextMetrics( m_device, &tm );
m_tabWidth = numChars * tm.tmAveCharWidth;
}
int getTabWidth() const
{
return m_tabWidth;
}
void printText( const char *text ) const;
int getCharWidth() const;
int getNumLines() const;
int getLineHeight() const;
void moveTo( int x, int y ) const
{
MoveToEx( m_device, x, y, nullptr );
}
void moveTo( const Point &point ) const
{
moveTo( point.x, point.y );
}
void getPosition( int *x, int *y ) const
{
Point pos;
GetCurrentPositionEx( m_device, &pos );
if( x )
*x = pos.x;
if( y )
*y = pos.y;
}
void getTextExtentWithBreak( const char *text, Size *size ) const;
void getTextExtent( const char *text, size_t len, Size *size ) const;
void getTextExtent( const char *text, size_t startPos, size_t len, Size *size ) const
{
getTextExtent( text+startPos, len, size );
}
void getTextExtent( char text, Size *size ) const
{
getTextExtent( &text, 1, size );
}
void getTextExtent( const STRING &text, Size *size ) const
{
getTextExtent( text, text.strlen(), size );
}
int getxOffset() const
{
return GetDeviceCaps( m_device, PHYSICALOFFSETX );
}
int getyOffset() const
{
return GetDeviceCaps( m_device, PHYSICALOFFSETY );
}
int getxResolution() const
{
return GetDeviceCaps( m_device, LOGPIXELSX );
}
int getyResolution() const
{
return GetDeviceCaps( m_device, LOGPIXELSY );
}
/*
brush
*/
void selectBrush( const Brush &brush )
{
m_brush = brush;
m_brush.connect( m_device );
}
Brush &getBrush()
{
return m_brush;
}
/*
pen
*/
void selectPen( const Pen &pen )
{
m_pen = pen;
m_pen.connect( m_device );
}
Pen &getPen()
{
return m_pen;
}
/*
font
*/
void selectFont()
{
if( !m_font.isAssigned() )
m_font.create();
}
void selectFont( const Font &newFont )
{
m_font = newFont;
m_font.connect( m_device );
selectFont();
}
void setTextColor( COLORREF color )
{
SetTextColor( m_device, color );
}
void setTextColor( unsigned char red, unsigned char green, unsigned char blue )
{
setTextColor( RGB( red, green, blue ) );
}
void setBackgroundColor( COLORREF color, int mode = OPAQUE )
{
SetBkMode( m_device, mode );
SetBkColor( m_device, color );
}
void setBackgroundColor( unsigned char red, unsigned char green, unsigned char blue, int mode = OPAQUE )
{
setBackgroundColor( RGB( red, green, blue ), mode );
}
void clrBackgroundColor()
{
SetBkMode( m_device, TRANSPARENT );
}
const Font &getFont() const
{
return m_font;
}
Font &getFont()
{
return m_font;
}
void setDefaultFont()
{
SelectObject( m_device, GetStockObject( ANSI_VAR_FONT ) );
}
int setLetterSpacing( int letterSpace )
{
return SetTextCharacterExtra( m_device, letterSpace );
}
/*
paint operations
*/
void polyline( const Point *points, int cnt ) const
{
Polyline( m_device, points, cnt );
}
template <typename ContainerT>
void polyline( const ContainerT &points ) const
{
polyline( points.getDataBuffer(), int(points.size()) );
}
void polygon( const Point *points, int cnt ) const
{
Polygon( m_device, points, cnt );
}
template <typename ContainerT>
void polygon( const ContainerT &points ) const
{
polygon( points.getDataBuffer(), int(points.size()) );
}
void lineTo( int x, int y ) const
{
LineTo( m_device, x, y );
}
void lineTo( const Point &point ) const
{
lineTo( point.x, point.y );
}
void line( int x1, int y1, int x2, int y2 ) const
{
moveTo( x1, y1 );
lineTo( x2, y2 );
}
void line( const Point &start, const Point &end ) const
{
moveTo( start );
lineTo( end );
}
void verticalLine( int x, int y1, int y2 )
{
moveTo( x, y1 );
lineTo( x, y2 );
}
void horizontalLine( int x1, int x2, int y )
{
moveTo( x1, y );
lineTo( x2, y );
}
void ellipse( int left, int top, int right, int bottom ) const
{
Ellipse( m_device, left, top, right, bottom );
}
void ellipse( int x, int y, int radius ) const
{
Ellipse( m_device, x-radius, y-radius, x+radius, y+radius );
}
void pie(int x, int y, int radius, double startAngle, double endAngle );
void pie(const Point ¢er, int radius, double startAngle, double endAngle )
{
pie(center.x, center.y, radius, startAngle, endAngle );
}
void rectangle( int left, int top, int right, int bottom ) const
{
Rectangle( m_device, left, top, right, bottom );
}
void rectangle( const RectBorder &rect ) const
{
Rectangle( m_device, rect.left, rect.top, rect.right, rect.bottom );
}
void textOut( int x, int y, const STRING &text )
{
moveTo( x, y );
TextOut(
m_device,
x, y,
text,
int(text.strlen())
);
}
void drawText( const char *text, size_t len, const RectBorder &rect, unsigned format )
{
// SetTextAlign( device, TA_TOP|TA_LEFT|TA_NOUPDATECP );
moveTo( rect.left, rect.top );
DrawText( m_device, text, int(len), const_cast<RectBorder *>(&rect), format );
}
void drawText( const STRING &text, const RectBorder &rect, unsigned format )
{
drawText( text, text.strlen(), rect, format );
}
/*
draw modes
*/
int setROP2( int drawMode ) const
{
return SetROP2( m_device, drawMode );
}
void setTextAlignment( HorizontalAlignment ha, VerticalAlignment va ) const
{
SetTextAlign( m_device, int(ha)|int(va)|TA_UPDATECP );
}
void removeClipping()
{
SelectClipRgn( m_device, nullptr );
}
void setClipping( int left, int top, int right, int bottom )
{
m_clipRegion = CreateRectRgn( left, top, right, bottom );
SelectClipRgn( m_device, m_clipRegion.getHandle() );
}
/*
palette
*/
void selectPalette( const Palette &palette )
{
SelectPalette( m_device, palette, true );
RealizePalette( m_device );
}
/*
bitmaps
*/
void setDIBits( const Bitmap &bitmap, unsigned uStartScan, unsigned numLines, void *data, unsigned color )
{
SetDIBits(
m_device, bitmap,
uStartScan, numLines,
data,
bitmap.getInfo(), color
);
}
void drawBitmap( int x, int y, const Bitmap &bitmap );
void setPixel( int x, int y, COLORREF color ) const
{
SetPixel( m_device, x,y, color );
}
void setPixel( int x, int y, unsigned char red, unsigned char green, unsigned char blue ) const
{
setPixel( x,y, RGB(red, green, blue) );
}
/*
calculating # pixels with int source
*/
int pointToPixel( int ptValue ) const
{
return MulDiv(ptValue, GetDeviceCaps(m_device, LOGPIXELSY), 72);
}
int picaToPixel( int pcValue ) const
{
return MulDiv(pcValue, GetDeviceCaps(m_device, LOGPIXELSY), 6);
}
int cmToPixel( int cmValue ) const
{
return MulDiv(cmValue, GetDeviceCaps(m_device, LOGPIXELSY)*100, 254);
}
int mmToPixel( int mmValue ) const
{
return MulDiv(mmValue, GetDeviceCaps(m_device, LOGPIXELSY)*10, 254);
}
int inToPixel( int inValue ) const
{
return inValue * GetDeviceCaps(m_device, LOGPIXELSY);
}
/*
calculating # pixels with double values
*/
int pointToPixel( double ptValue ) const
{
return int(ptValue * double(GetDeviceCaps(m_device, LOGPIXELSY)) / 72.0 +0.5);
}
int picaToPixel( double pcValue ) const
{
return int(pcValue * double(GetDeviceCaps(m_device, LOGPIXELSY)) / 6.0 +0.5);
}
int cmToPixel( double cmValue ) const
{
return int(cmValue * double(GetDeviceCaps(m_device, LOGPIXELSY)) / 2.54 +0.5);
}
int mmToPixel( double mmValue ) const
{
return int(mmValue * double(GetDeviceCaps(m_device, LOGPIXELSY)) / 25.4 +0.5);
}
int inToPixel( double inValue ) const
{
return int(inValue * double(GetDeviceCaps(m_device, LOGPIXELSY)) + 0.5);
}
/*
calculating pixels to other dimensions
*/
double pixelToPoint( int pxValue ) const
{
return (double)pxValue * 72.0 / (double)GetDeviceCaps(m_device, LOGPIXELSY);
}
double pixelToPica( int pxValue ) const
{
return (double)pxValue * 6.0 / (double)GetDeviceCaps(m_device, LOGPIXELSY);
}
double pixelToCM( int pxValue ) const
{
return (double)pxValue * 2.54 / (double)GetDeviceCaps(m_device, LOGPIXELSY);
}
double pixelToMM( int pxValue ) const
{
return (double)pxValue * 25.4 / (double)GetDeviceCaps(m_device, LOGPIXELSY);
}
double pixelToIN( int pxValue ) const
{
return (double)pxValue / (double)GetDeviceCaps(m_device, LOGPIXELSY);
}
};
class WindowDevice : public Device
{
protected:
const BasicWindow *m_window;
WindowDevice( const BasicWindow *theWindow, HDC context )
: Device( context ), m_window( theWindow )
{}
};
class DrawDevice : public WindowDevice
{
public:
DrawDevice( const BasicWindow *theWindow )
: WindowDevice( theWindow, theWindow->getDC() )
{
selectFont( theWindow->getFont() );
}
~DrawDevice()
{
m_window->releaseDC( m_device );
m_device = nullptr;
}
};
class RepaintDevice : public WindowDevice
{
PAINTSTRUCT m_ps;
friend class CallbackWindow;
private:
RepaintDevice( const BasicWindow *theWindow )
: WindowDevice( theWindow, theWindow->beginPaint( &m_ps ) )
{}
~RepaintDevice()
{
m_window->endPaint( &m_ps );
m_device = nullptr;
}
};
class BackgroundDevice : public WindowDevice
{
public:
BackgroundDevice( const BasicWindow *theWindow, HDC context )
: WindowDevice( theWindow, context )
{}
~BackgroundDevice()
{
m_device = nullptr; // don't delete
}
};
class MemoryDevice : public Device
{
const Device &m_target;
Bitmap m_bitmap;
public:
MemoryDevice( const Device &target, const Size &size )
: Device( CreateCompatibleDC ( target ) ),
m_target( target ),
m_bitmap( *this )
{
m_bitmap.create( target, size );
}
MemoryDevice( const Device &target, const Bitmap &bitmap )
: Device( CreateCompatibleDC ( target ) ),
m_target( target ),
m_bitmap( bitmap )
{
m_bitmap.connect( *this );
}
void drawToWindow()
{
BitBlt( m_target, 0, 0, m_bitmap.getWidth(), m_bitmap.getHeight(), *this, 0, 0, SRCCOPY );
}
#if 0
void drawScaledToWindow()
{
SetMapMode( *this, GetMapMode( target ));
POINT corners[2];
corners[0].x = 0;
corners[0].y = 0;
corners[1].x = bitmap.getWidth();
corners[1].y = bitmap.getHeight();
DPtoLP( target, corners, arraySize( corners ) );
BitBlt(
target, 0, 0, corners[1].x, corners[1].y,
*this, corners[0].x, corners[0].y, SRCCOPY
);
}
#endif
};
class PrintDevice : public Device
{
public:
PrintDevice( HDC context ) : Device( context ) {}
void startDocument( const char *file ) const
{
DOCINFO dInfo;
memset( &dInfo, 0, sizeof( dInfo ) );
dInfo.cbSize = sizeof( dInfo );
dInfo.lpszDocName = file;
StartDoc( m_device, &dInfo );
}
void startPage() const
{
StartPage( m_device );
}
void endPage() const
{
EndPage( m_device );
}
void endDocument() const
{
EndDoc( m_device );
}
void printText( const char *document, const char *text ) const;
};
class MetaDevice : public Device
{
HMETAFILE m_metaFile;
public:
MetaDevice( const char *fileName = nullptr ) : Device( CreateMetaFile( fileName ) )
{
m_metaFile = nullptr;
}
~MetaDevice()
{
// if( m_metaFile ) // not yet
// DeleteMetaFile( m_metaFile );
}
HMETAFILE createFile()
{
if( !m_metaFile )
m_metaFile = CloseMetaFile( m_device );
return m_metaFile;
}
};
struct PRINTER
{
STRING printerName;
STRING portName;
};
typedef gak::Array<PRINTER> PRINTER_ARRAY;
int GetListOfPrinters( PRINTER_ARRAY &list );
// --------------------------------------------------------------------- //
// ----- exported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module static data -------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static data --------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- prototypes ---------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module functions ---------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class inlines ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class constructors/destructors -------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class static functions ---------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class privates ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class protected ----------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class virtuals ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class publics ------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- entry points -------------------------------------------------- //
// --------------------------------------------------------------------- //
inline void Device::drawBitmap( int x, int y, const Bitmap &bitmap )
{
MemoryDevice hDCmem( *this, bitmap );
BitBlt(
*this, x, y, bitmap.getWidth(), bitmap.getHeight(),
hDCmem, 0, 0, SRCCOPY
);
}
inline void Bitmap::create( const Device &targetDevice, int width, int height )
{
create ( HDC(targetDevice), width, height );
}
} // namespace winlib
#ifdef __BORLANDC__
# pragma option -RT.
# pragma option -b.
# pragma option -a.
# pragma option -p.
#endif
#endif // WL_DEVICE_H