File: vmemdc.cpp

package info (click to toggle)
libv1.22 1.22-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,380 kB
  • ctags: 9,765
  • sloc: cpp: 58,097; ansic: 6,814; makefile: 1,597; java: 515; sh: 24
file content (145 lines) | stat: -rw-r--r-- 4,282 bytes parent folder | download | duplicates (2)
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
//===============================================================
// vMemoryDC - a memory drawing canvas - Windows
//
// Copyright (C) 1995,1996, 1997, 1998  Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================

#include <v/vwin32.h>
#include <v/vmemdc.h>
#include <v/vapp.h>

//================>>> vMemoryDC::vMemoryDC <<<========================
  vMemoryDC::vMemoryDC(int width, int height)
  {

    // Mem canvases are implemented with bitmaps. Most of the
    // windows books I've seen don't seem to explain bitmaps
    // very well. The idea is this. You create a bitmap (_memBM) that
    // is compatible with some DC - like the app's frame DC for our
    // purposes. This gives the bitmap a nice color map. Then, the
    // tricky part. To use a bitmap, you have to select it into
    // a DC. So, you create a new DC (_hdc) , also compatible with
    // the app frame DC. Then you select the bitmap into the new
    // DC. Now you can draw into the new DC. When done drawing,
    // you can release/delete the memory DC until you draw again.
    // When you are ALL done, finally free the bitmap. Now isn't
    // that all kind of a wierd way of doing things?

    _physHeight = height;		// V 1.15a
    _physWidth = width;

    SysDebug(Constructor,"vMemoryDC::vMemoryDC() constructor\n")

    if (_physWidth == 0)
	_physWidth = theApp->DefaultWidth();
    if (_physHeight == 0)
	_physHeight = theApp->DefaultHeight();

    _memBM = 0;		// Not created yet
    _hdc = 0;
    _oldBM = 0;
  }

//================>>> vMemoryDC::~vMemoryDC <<<========================
  vMemoryDC::~vMemoryDC()
  {
    SysDebug(Destructor,"vMemoryDC::~vMemoryDC() destructor\n")

    if (!_memBM)
	return;

    ::DeleteObject(_memBM);
    _memBM = 0;

  }

//====================>>> vMemoryDC::GetHDC <<<========================
  void vMemoryDC::GetHDC() VCONST
  {
    // Create the DC to draw into

    HDC appDC = ::GetDC(theApp->winHwnd());	// DC of the app

    if (_memBM == 0)	// Need to create a bitmap
      {
	// Now create a bitmap
	_memBM = ::CreateCompatibleBitmap(appDC,_physWidth,_physHeight);
      }

    if (_memBM == 0)
      {
	::ReleaseDC(theApp->winHwnd(),appDC);
	return;
      }

    // Now, create an HDC for the bitmap, compatible with the app DC

    _hdc = ::CreateCompatibleDC(appDC);
    ::ReleaseDC(theApp->winHwnd(),appDC);	// Done with app's dc

    _oldBM = static_cast<HBITMAP>(::SelectObject(_hdc,_memBM)); // Select into _hdc

    // We are now ready to use _hdc!
  }

//================>>> vMemoryDC::ReleaseHDC <<<========================
  void vMemoryDC::ReleaseHDC() VCONST
  {
    if (_hdc == 0)
	return;
    ::SelectObject(_hdc,_oldBM);	// reselect old bm
    ::DeleteDC(_hdc);			// free the DC
  }

//=====================>>> vMemoryDC::Clear <<<==========================
  void vMemoryDC::Clear(void)
  {
    ClearRect(0,0,_physWidth, _physHeight);
  }

//==================>>> vMemoryDC::ClearRect <<<==========================
  void vMemoryDC::ClearRect(int left, int top, int width, int height)
  {
    // Clear a rectangluar area starting at left,top of width and height

    if (height == 0 || width == 0)
	return;

    GetHDC();				// Get a DC to use
    ::SetBkColor(_hdc, _canvasBG);

    HBRUSH brush = ::CreateSolidBrush(_canvasBG);
    RECT rect;

    rect.top = top; rect.left = left;
    rect.bottom = top+height; rect.right = left + width;

    ::FillRect(_hdc, &rect, brush);
    ::DeleteObject(brush);		// need to free the resource!

    ReleaseHDC();			// Release the DC
  }

//================>>> vMemoryDC::SetBackground <<<==========================
  void vMemoryDC::SetBackground(VCONST vColor& color)
  {

   _canvasBG = color.pixel();		// retrieve X pixel value

    GetHDC();
    ::SetBkColor(_hdc, _canvasBG);
    ReleaseHDC();
  }

//======================>>> vMemoryDC::SetFont <<<===========================
  void vMemoryDC::SetFont(VCONST vFont& vf)
  {
    // Change the font associated with this window.

    _font = vf;
  }