Mercurial > vba-clojure
diff src/win32/PaletteViewControl.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/PaletteViewControl.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,384 @@ 1.4 +// PaletteViewControl.cpp : implementation file 1.5 +// 1.6 + 1.7 +#include "stdafx.h" 1.8 +#include "PaletteViewControl.h" 1.9 + 1.10 +#include "../common/Util.h" 1.11 + 1.12 +bool PaletteViewControl::isRegistered = false; 1.13 + 1.14 +///////////////////////////////////////////////////////////////////////////// 1.15 +// PaletteViewControl 1.16 + 1.17 +PaletteViewControl::PaletteViewControl() 1.18 +{ 1.19 + memset(&bmpInfo.bmiHeader, 0, sizeof(bmpInfo.bmiHeader)); 1.20 + 1.21 + bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); 1.22 + bmpInfo.bmiHeader.biWidth = 256; 1.23 + bmpInfo.bmiHeader.biHeight = -256; 1.24 + bmpInfo.bmiHeader.biPlanes = 1; 1.25 + bmpInfo.bmiHeader.biBitCount = 24; 1.26 + bmpInfo.bmiHeader.biCompression = BI_RGB; 1.27 + data = (u8 *)malloc(3 * 256 * 256); 1.28 + 1.29 + w = 256; 1.30 + h = 256; 1.31 + 1.32 + colors = 256; 1.33 + 1.34 + paletteAddress = 0; 1.35 + 1.36 + ZeroMemory(palette, 512); 1.37 + 1.38 + selected = -1; 1.39 + registerClass(); 1.40 +} 1.41 + 1.42 +PaletteViewControl::~PaletteViewControl() 1.43 +{ 1.44 + if(data) 1.45 + free(data); 1.46 +} 1.47 + 1.48 + 1.49 +BEGIN_MESSAGE_MAP(PaletteViewControl, CWnd) 1.50 + //{{AFX_MSG_MAP(PaletteViewControl) 1.51 + ON_WM_LBUTTONDOWN() 1.52 + ON_WM_ERASEBKGND() 1.53 + ON_WM_PAINT() 1.54 + //}}AFX_MSG_MAP 1.55 + END_MESSAGE_MAP() 1.56 + 1.57 + 1.58 + ///////////////////////////////////////////////////////////////////////////// 1.59 +// PaletteViewControl message handlers 1.60 + 1.61 +void PaletteViewControl::init(int c, int w, int h) 1.62 +{ 1.63 + this->w = w; 1.64 + this->h = h; 1.65 + this->colors = c; 1.66 + 1.67 + bmpInfo.bmiHeader.biWidth = w; 1.68 + bmpInfo.bmiHeader.biHeight = -h; 1.69 +} 1.70 + 1.71 + 1.72 +bool PaletteViewControl::saveAdobe(const char *name) 1.73 +{ 1.74 + FILE *f = fopen(name, "wb"); 1.75 + 1.76 + if(!f) 1.77 + return false; 1.78 + 1.79 + for(int i = 0; i < colors; i++) { 1.80 + u16 c = palette[i]; 1.81 + int r = (c & 0x1f) << 3; 1.82 + int g = (c & 0x3e0) >> 2; 1.83 + int b = (c & 0x7c00) >> 7; 1.84 + 1.85 + u8 data[3] = { r, g, b }; 1.86 + fwrite(data, 1, 3, f); 1.87 + } 1.88 + if(colors < 256) { 1.89 + for(int i = colors; i < 256; i++) { 1.90 + u8 data[3] = { 0, 0, 0 }; 1.91 + fwrite(data, 1, 3, f); 1.92 + } 1.93 + } 1.94 + fclose(f); 1.95 + 1.96 + return true; 1.97 +} 1.98 + 1.99 + 1.100 +bool PaletteViewControl::saveMSPAL(const char *name) 1.101 +{ 1.102 + FILE *f = fopen(name, "wb"); 1.103 + 1.104 + if(!f) 1.105 + return false; 1.106 + 1.107 + u8 data[4] = { 'R', 'I', 'F', 'F' }; 1.108 + 1.109 + fwrite(data, 1, 4, f); 1.110 + utilPutDword(data, 256 * 4 + 16); 1.111 + fwrite(data, 1, 4, f); 1.112 + u8 data3[4] = { 'P', 'A', 'L', ' ' }; 1.113 + fwrite(data3, 1, 4, f); 1.114 + u8 data4[4] = { 'd', 'a', 't', 'a' }; 1.115 + fwrite(data4, 1, 4, f); 1.116 + utilPutDword(data, 256*4+4); 1.117 + fwrite(data, 1, 4, f); 1.118 + utilPutWord(&data[0], 0x0300); 1.119 + utilPutWord(&data[2], 256); // causes problems if not 16 or 256 1.120 + fwrite(data, 1, 4, f); 1.121 + 1.122 + for(int i = 0; i < colors; i++) { 1.123 + u16 c = palette[i]; 1.124 + int r = (c & 0x1f) << 3; 1.125 + int g = (c & 0x3e0) >> 2; 1.126 + int b = (c & 0x7c00) >> 7; 1.127 + 1.128 + u8 data7[4] = { r, g, b, 0 }; 1.129 + fwrite(data7, 1, 4, f); 1.130 + } 1.131 + if(colors < 256) { 1.132 + for(int i = colors; i < 256; i++) { 1.133 + u8 data7[4] = { 0, 0, 0, 0 }; 1.134 + fwrite(data7, 1, 4, f); 1.135 + } 1.136 + } 1.137 + fclose(f); 1.138 + 1.139 + return true; 1.140 +} 1.141 + 1.142 + 1.143 +bool PaletteViewControl::saveJASCPAL(const char *name) 1.144 +{ 1.145 + FILE *f = fopen(name, "wb"); 1.146 + 1.147 + if(!f) 1.148 + return false; 1.149 + 1.150 + fprintf(f, "JASC-PAL\r\n0100\r\n256\r\n"); 1.151 + 1.152 + for(int i = 0; i < colors; i++) { 1.153 + u16 c = palette[i]; 1.154 + int r = (c & 0x1f) << 3; 1.155 + int g = (c & 0x3e0) >> 2; 1.156 + int b = (c & 0x7c00) >> 7; 1.157 + 1.158 + fprintf(f, "%d %d %d\r\n", r, g, b); 1.159 + } 1.160 + if(colors < 256) { 1.161 + for(int i = colors; i < 256; i++) 1.162 + fprintf(f, "0 0 0\r\n"); 1.163 + } 1.164 + fclose(f); 1.165 + 1.166 + return true; 1.167 +} 1.168 + 1.169 +void PaletteViewControl::setPaletteAddress(int address) 1.170 +{ 1.171 + paletteAddress = address; 1.172 +} 1.173 + 1.174 + 1.175 +void PaletteViewControl::setSelected(int s) 1.176 +{ 1.177 + selected = s; 1.178 + InvalidateRect(NULL, FALSE); 1.179 +} 1.180 + 1.181 + 1.182 +void PaletteViewControl::render(u16 color, int x, int y) 1.183 +{ 1.184 + u8 *start = data + y*16*w*3 + x*16*3; 1.185 + int skip = w*3-16*3; 1.186 + 1.187 + int r = (color & 0x1f) << 3; 1.188 + int g = (color & 0x3e0) >> 2; 1.189 + int b = (color & 0x7c00) >> 7; 1.190 + 1.191 + for(int i = 0; i < 16; i++) { 1.192 + *start++ = b; 1.193 + *start++ = g; 1.194 + *start++ = r; 1.195 + 1.196 + *start++ = b; 1.197 + *start++ = g; 1.198 + *start++ = r; 1.199 + 1.200 + *start++ = b; 1.201 + *start++ = g; 1.202 + *start++ = r; 1.203 + 1.204 + *start++ = b; 1.205 + *start++ = g; 1.206 + *start++ = r; 1.207 + 1.208 + *start++ = b; 1.209 + *start++ = g; 1.210 + *start++ = r; 1.211 + 1.212 + *start++ = b; 1.213 + *start++ = g; 1.214 + *start++ = r; 1.215 + 1.216 + *start++ = b; 1.217 + *start++ = g; 1.218 + *start++ = r; 1.219 + 1.220 + *start++ = b; 1.221 + *start++ = g; 1.222 + *start++ = r; 1.223 + 1.224 + *start++ = b; 1.225 + *start++ = g; 1.226 + *start++ = r; 1.227 + 1.228 + *start++ = b; 1.229 + *start++ = g; 1.230 + *start++ = r; 1.231 + 1.232 + *start++ = b; 1.233 + *start++ = g; 1.234 + *start++ = r; 1.235 + 1.236 + *start++ = b; 1.237 + *start++ = g; 1.238 + *start++ = r; 1.239 + 1.240 + *start++ = b; 1.241 + *start++ = g; 1.242 + *start++ = r; 1.243 + 1.244 + *start++ = b; 1.245 + *start++ = g; 1.246 + *start++ = r; 1.247 + 1.248 + *start++ = b; 1.249 + *start++ = g; 1.250 + *start++ = r; 1.251 + 1.252 + *start++ = b; 1.253 + *start++ = g; 1.254 + *start++ = r; 1.255 + 1.256 + start += skip; 1.257 + } 1.258 +} 1.259 + 1.260 +void PaletteViewControl::refresh() 1.261 +{ 1.262 + updatePalette(); 1.263 + int sw = w/16; 1.264 + int sh = h/16; 1.265 + for(int i = 0; i < colors; i++) { 1.266 + render(palette[i], i & (sw-1), i/sw); 1.267 + } 1.268 + InvalidateRect(NULL, FALSE); 1.269 +} 1.270 + 1.271 +void PaletteViewControl::OnLButtonDown(UINT nFlags, CPoint point) 1.272 +{ 1.273 + int x = point.x; 1.274 + int y = point.y; 1.275 + RECT rect; 1.276 + GetClientRect(&rect); 1.277 + int h = rect.bottom - rect.top; 1.278 + int w = rect.right - rect.left; 1.279 + int sw = (this->w/16); 1.280 + int sh = (this->h/16); 1.281 + int mult = w / sw; 1.282 + int multY = h / sh; 1.283 + 1.284 + setSelected(x/mult + (y/multY)*sw); 1.285 + 1.286 + GetParent()->SendMessage(WM_PALINFO, 1.287 + palette[x/mult+(y/multY)*sw], 1.288 + paletteAddress+(x/mult+(y/multY)*sw)); 1.289 +} 1.290 + 1.291 +BOOL PaletteViewControl::OnEraseBkgnd(CDC* pDC) 1.292 +{ 1.293 + return TRUE; 1.294 +} 1.295 + 1.296 + 1.297 +void PaletteViewControl::OnPaint() 1.298 +{ 1.299 + CPaintDC dc(this); // device context for painting 1.300 + 1.301 + RECT rect; 1.302 + GetClientRect(&rect); 1.303 + int w = rect.right - rect.left; 1.304 + int h = rect.bottom - rect.top; 1.305 + 1.306 + CDC memDC; 1.307 + memDC.CreateCompatibleDC(&dc); 1.308 + CBitmap bitmap, *pOldBitmap; 1.309 + bitmap.CreateCompatibleBitmap(&dc, w, h); 1.310 + pOldBitmap = memDC.SelectObject(&bitmap); 1.311 + 1.312 + StretchDIBits(memDC.GetSafeHdc(), 1.313 + 0, 1.314 + 0, 1.315 + w, 1.316 + h, 1.317 + 0, 1.318 + 0, 1.319 + this->w, 1.320 + this->h, 1.321 + data, 1.322 + &bmpInfo, 1.323 + DIB_RGB_COLORS, 1.324 + SRCCOPY); 1.325 + int sw = this->w / 16; 1.326 + int sh = this->h / 16; 1.327 + int mult = w / sw; 1.328 + int multY = h / sh; 1.329 + CPen pen; 1.330 + pen.CreatePen(PS_SOLID, 1, RGB(192,192,192)); 1.331 + CPen *old = memDC.SelectObject(&pen); 1.332 + int i; 1.333 + for(i = 1; i < sh; i++) { 1.334 + memDC.MoveTo(0, i * multY); 1.335 + memDC.LineTo(w, i * multY); 1.336 + } 1.337 + for(i = 1; i < sw; i++) { 1.338 + memDC.MoveTo(i * mult, 0); 1.339 + memDC.LineTo(i * mult, h); 1.340 + } 1.341 + memDC.DrawEdge(&rect, EDGE_SUNKEN, BF_RECT); 1.342 + memDC.SelectObject(old); 1.343 + pen.DeleteObject(); 1.344 + 1.345 + if(selected != -1) { 1.346 + pen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); 1.347 + old = memDC.SelectObject(&pen); 1.348 + 1.349 + int startX = (selected & (sw-1))*mult+1; 1.350 + int startY = (selected / sw)*multY+1; 1.351 + int endX = startX + mult-2; 1.352 + int endY = startY + multY-2; 1.353 + 1.354 + memDC.MoveTo(startX, startY); 1.355 + memDC.LineTo(endX, startY); 1.356 + memDC.LineTo(endX, endY); 1.357 + memDC.LineTo(startX, endY); 1.358 + memDC.LineTo(startX, startY-1); 1.359 + 1.360 + memDC.SelectObject(old); 1.361 + pen.DeleteObject(); 1.362 + } 1.363 + 1.364 + dc.BitBlt(0,0,w,h, 1.365 + &memDC,0,0,SRCCOPY); 1.366 + 1.367 + memDC.SelectObject(pOldBitmap); 1.368 + bitmap.DeleteObject(); 1.369 + memDC.DeleteDC(); 1.370 +} 1.371 + 1.372 +void PaletteViewControl::registerClass() 1.373 +{ 1.374 + if(!isRegistered) { 1.375 + WNDCLASS wc; 1.376 + ZeroMemory(&wc, sizeof(wc)); 1.377 + wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS; 1.378 + wc.lpfnWndProc = (WNDPROC)::DefWindowProc; 1.379 + wc.hInstance = AfxGetInstanceHandle(); 1.380 + wc.hCursor = LoadCursor(NULL, IDC_ARROW); 1.381 + wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH); 1.382 + wc.lpszMenuName = NULL; 1.383 + wc.lpszClassName = "VbaPaletteViewControl"; 1.384 + AfxRegisterClass(&wc); 1.385 + isRegistered = true; 1.386 + } 1.387 +}