Fix history combobox show twice item
[TortoiseGit.git] / src / Utils / MiscUI / MyMemDC.h
blob925ee1cbb3168f7c1c0dd5b8304e4f70817d25ba
1 #pragma once
3 //////////////////////////////////////////////////
4 // CMyMemDC - memory DC
5 //
6 // Author: Keith Rule
7 // Email: keithr@europa.com
8 // Copyright 1996-1997, Keith Rule
9 //
10 // You may freely use or modify this code provided this
11 // Copyright is included in all derived versions.
13 // History - 10/3/97 Fixed scrolling bug.
14 // Added print support.
15 // 25 feb 98 - fixed minor assertion bug
17 // This class implements a memory Device Context
19 #ifdef _MFC_VER
20 class CMyMemDC : public CDC
22 public:
23 // constructor sets up the memory DC
24 CMyMemDC(CDC* pDC, bool bTempOnly = false, int nOffset = 0) : CDC()
26 ASSERT(pDC != NULL);
28 m_pDC = pDC;
29 m_pOldBitmap = NULL;
30 m_bMyMemDC = ((!pDC->IsPrinting()) && (!GetSystemMetrics(SM_REMOTESESSION)));
31 m_bTempOnly = bTempOnly;
33 if (m_bMyMemDC) // Create a Memory DC
35 pDC->GetClipBox(&m_rect);
36 CreateCompatibleDC(pDC);
37 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width() - nOffset, m_rect.Height());
38 m_pOldBitmap = SelectObject(&m_bitmap);
39 SetWindowOrg(m_rect.left, m_rect.top);
41 else // Make a copy of the relevant parts of the current DC for printing
43 m_bPrinting = pDC->m_bPrinting;
44 m_hDC = pDC->m_hDC;
45 m_hAttribDC = pDC->m_hAttribDC;
48 FillSolidRect(m_rect, pDC->GetBkColor());
51 CMyMemDC(CDC* pDC, const CRect* pRect) : CDC()
53 ASSERT(pDC != NULL);
55 // Some initialization
56 m_pDC = pDC;
57 m_pOldBitmap = NULL;
58 m_bMyMemDC = !pDC->IsPrinting();
59 m_bTempOnly = false;
61 // Get the rectangle to draw
62 if (pRect == NULL) {
63 pDC->GetClipBox(&m_rect);
64 } else {
65 m_rect = *pRect;
68 if (m_bMyMemDC) {
69 // Create a Memory DC
70 CreateCompatibleDC(pDC);
71 pDC->LPtoDP(&m_rect);
73 m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
74 m_pOldBitmap = SelectObject(&m_bitmap);
76 SetMapMode(pDC->GetMapMode());
78 SetWindowExt(pDC->GetWindowExt());
79 SetViewportExt(pDC->GetViewportExt());
81 pDC->DPtoLP(&m_rect);
82 SetWindowOrg(m_rect.left, m_rect.top);
83 } else {
84 // Make a copy of the relevant parts of the current DC for printing
85 m_bPrinting = pDC->m_bPrinting;
86 m_hDC = pDC->m_hDC;
87 m_hAttribDC = pDC->m_hAttribDC;
90 // Fill background
91 FillSolidRect(m_rect, pDC->GetBkColor());
94 // Destructor copies the contents of the mem DC to the original DC
95 ~CMyMemDC()
97 if (m_bMyMemDC) {
98 // Copy the off screen bitmap onto the screen.
99 if (!m_bTempOnly)
100 m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
101 this, m_rect.left, m_rect.top, SRCCOPY);
103 //Swap back the original bitmap.
104 SelectObject(m_pOldBitmap);
105 } else {
106 // All we need to do is replace the DC with an illegal value,
107 // this keeps us from accidentally deleting the handles associated with
108 // the CDC that was passed to the constructor.
109 m_hDC = m_hAttribDC = NULL;
113 // Allow usage as a pointer
114 CMyMemDC* operator->() {return this;}
116 // Allow usage as a pointer
117 operator CMyMemDC*() {return this;}
119 private:
120 CBitmap m_bitmap; // Off screen bitmap
121 CBitmap* m_pOldBitmap; // bitmap originally found in CMyMemDC
122 CDC* m_pDC; // Saves CDC passed in constructor
123 CRect m_rect; // Rectangle of drawing area.
124 BOOL m_bMyMemDC; // TRUE if CDC really is a Memory DC.
125 BOOL m_bTempOnly; // Whether to copy the contents on the real DC on destroy
127 #else
128 class CMyMemDC
130 public:
132 // constructor sets up the memory DC
133 CMyMemDC(HDC hDC, bool bTempOnly = false, int nOffset = 0)
135 m_hDC = hDC;
136 m_hOldBitmap = NULL;
137 m_bTempOnly = bTempOnly;
139 GetClipBox(m_hDC, &m_rect);
140 m_hMyMemDC = ::CreateCompatibleDC(m_hDC);
141 m_hBitmap = CreateCompatibleBitmap(m_hDC, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top);
142 m_hOldBitmap = (HBITMAP)SelectObject(m_hMyMemDC, m_hBitmap);
143 SetWindowOrgEx(m_hMyMemDC, m_rect.left, m_rect.top, NULL);
146 // Destructor copies the contents of the mem DC to the original DC
147 ~CMyMemDC()
149 if (m_hMyMemDC) {
150 // Copy the off screen bitmap onto the screen.
151 if (!m_bTempOnly)
152 BitBlt(m_hDC, m_rect.left, m_rect.top, m_rect.right-m_rect.left, m_rect.bottom-m_rect.top, m_hMyMemDC, m_rect.left, m_rect.top, SRCCOPY);
154 //Swap back the original bitmap.
155 SelectObject(m_hMyMemDC, m_hOldBitmap);
156 DeleteObject(m_hBitmap);
157 DeleteDC(m_hMyMemDC);
158 } else {
159 // All we need to do is replace the DC with an illegal value,
160 // this keeps us from accidentally deleting the handles associated with
161 // the CDC that was passed to the constructor.
162 DeleteObject(m_hBitmap);
163 DeleteDC(m_hMyMemDC);
164 m_hMyMemDC = NULL;
168 // Allow usage as a pointer
169 operator HDC() {return m_hMyMemDC;}
170 private:
171 HBITMAP m_hBitmap; // Off screen bitmap
172 HBITMAP m_hOldBitmap; // bitmap originally found in CMyMemDC
173 HDC m_hDC; // Saves CDC passed in constructor
174 HDC m_hMyMemDC; // our own memory DC
175 RECT m_rect; // Rectangle of drawing area.
176 bool m_bTempOnly; // Whether to copy the contents on the real DC on destroy
179 #endif