2 // TTEDIT.CPP - The TrueType Editor
\r
3 // by James L. Hammons
\r
4 // (C) 2004 Underground Software
\r
6 // JLH = James L. Hammons <jlhamm@acm.org>
\r
9 // --- ---------- -------------------------------------------------------------
\r
10 // JLH 08/28/2008 Created this file
\r
15 // STILL TO BE DONE:
\r
17 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font
\r
18 // - Fix scrolling, zooming, settings (ini)
\r
21 // Uncomment this for debugging...
\r
23 #define DEBUGFOO // Various tool debugging...
\r
24 #define DEBUGTP // Toolpalette debugging...
\r
26 #include "editwindow.h"
\r
27 #include "graphicprimitives.h"
\r
28 #include "toolwindow.h"
\r
33 BEGIN_EVENT_TABLE(TTEditWindow, wxWindow)
\r
34 EVT_PAINT(TTEditWindow::OnPaint)
\r
35 EVT_MOUSE_EVENTS(TTEditWindow::OnMouseEvent)
\r
38 TTEditWindow::TTEditWindow(wxFrame * parent, const wxPoint &pos, const wxSize &size, long style):
\r
39 wxWindow(parent, -1, pos, size, style | wxFULL_REPAINT_ON_RESIZE),
\r
40 app(wxGetApp()), scale(1.0), offsetX(-10), offsetY(-10), tool(TOOLSelect),
\r
41 ptHighlight(-1), oldPtHighlight(-1), ptNextHighlight(-1), oldPtNextHighlight(-1),
\r
42 polyFirstPoint(true), bmp(NULL)
\r
44 SetCursor(*(app.cur[tool]));
\r
45 SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));
\r
48 s.Printf(_("Zoom: %.2f%%"), scale * 100.0);
\r
49 parent->SetStatusText(s, 1);
\r
52 TTEditWindow::~TTEditWindow(void)
\r
58 void TTEditWindow::OnPaint(wxPaintEvent &e)
\r
62 //dc.SetBackground(*wxWHITE_BRUSH);
\r
64 // Due to the screwiness of wxWidgets coord system, the origin is ALWAYS
\r
65 // the upper left corner--regardless of axis orientation, etc...
\r
66 wxCoord width, height;
\r
67 dc.GetSize(&width, &height);
\r
69 dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
\r
70 dc.SetAxisOrientation(true, true);
\r
72 // Scrolling can be done by using OffsetViewportOrgEx
\r
73 // Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
\r
74 // you'd use: % = ViewportExt / WindowExt
\r
75 // But it makes the window look like crap: fuggetuboutit.
\r
76 // Instead, we have to scale EVERYTHING by hand. Crap!
\r
77 // It's not *that* bad, but not as convenient either...
\r
79 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0xFF), 1, wxDOT)));
\r
80 // dc.DrawLine(0, 0, 10, 10);
\r
82 // Draw coordinate axes
\r
88 for(int i=0; i<pts.GetNumPoints(); i++)
\r
90 if (i == ptHighlight)
\r
92 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
\r
93 // SelectObject(hdc, hRedPen1);
\r
95 if (pts.GetOnCurve(i))
\r
97 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 7);
\r
98 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 9);
\r
102 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 7);
\r
103 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 9);
\r
106 else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)
\r
108 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0xAF, 0x00), 1, wxSOLID)));
\r
109 // SelectObject(hdc, hGreenPen1);
\r
111 if (pts.GetOnCurve(i))
\r
113 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 7);
\r
114 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 9);
\r
118 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 7);
\r
119 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 9);
\r
124 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
\r
125 // SelectObject(hdc, hBlackPen1);
\r
127 if (pts.GetOnCurve(i))
\r
128 DrawSquareDot(dc, pts.GetX(i), pts.GetY(i));
\r
130 DrawRoundDot(dc, pts.GetX(i), pts.GetY(i));
\r
133 if (tool == TOOLDelPt && i == ptHighlight)
\r
135 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
\r
136 // SelectObject(hdc, hRedPen1);
\r
137 // MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5, NULL);
\r
138 // LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) + 5);
\r
139 // LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5);//Lameness!
\r
140 // MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5, NULL);
\r
141 // LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) - 5);
\r
142 // LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5);//More lameness!!
\r
143 dc.DrawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5);
\r
144 dc.DrawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5);
\r
148 // SelectObject(hdc, hBlackPen1);
\r
149 dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
\r
151 // Draw curve formed by points
\r
153 for(int poly=0; poly<pts.GetNumPolys(); poly++)
\r
155 if (pts.GetNumPoints(poly) > 2)
\r
158 // If it's not on curve, then move to it, otherwise move to last point...
\r
162 if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
\r
163 x = (wxCoord)pts.GetX(poly, pts.GetNumPoints(poly) - 1), y = (wxCoord)pts.GetY(poly, pts.GetNumPoints(poly) - 1);
\r
165 x = (wxCoord)pts.GetX(poly, 0), y = (wxCoord)pts.GetY(poly, 0);
\r
167 for(int i=0; i<pts.GetNumPoints(poly); i++)
\r
169 if (pts.GetOnCurve(poly, i))
\r
170 // LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
\r
172 dc.DrawLine(x, y, pts.GetX(poly, i), pts.GetY(poly, i));
\r
173 x = (wxCoord)pts.GetX(poly, i), y = (wxCoord)pts.GetY(poly, i);
\r
177 uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
\r
178 float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
\r
179 nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
\r
181 if (!pts.GetOnCurve(poly, prev))
\r
182 px = (px + pts.GetX(poly, i)) / 2.0f,
\r
183 py = (py + pts.GetY(poly, i)) / 2.0f;
\r
185 if (!pts.GetOnCurve(poly, next))
\r
186 nx = (nx + pts.GetX(poly, i)) / 2.0f,
\r
187 ny = (ny + pts.GetY(poly, i)) / 2.0f;
\r
189 Bezier(dc, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
\r
190 x = (wxCoord)nx, y = (wxCoord)ny;
\r
192 if (pts.GetOnCurve(poly, next))
\r
193 i++; // Following point is on curve, so move past it
\r
199 // SelectObject(hdc, oldPen); // Restore the stuff we disrupted...
\r
200 dc.SetPen(wxNullPen);
\r
201 // SelectObject(hdc, oldBrush);
\r
202 // EndPaint(hWnd, &ps);
\r
205 void TTEditWindow::OnMouseEvent(wxMouseEvent &e)
\r
208 //printf("!!! This window %s focus...!\n", (HasCapture() ? "has" : "doesn't have"));
\r
212 // Handle tool palette (NOTE: tool palette deals with RightUp event.)
\r
214 wxPoint pt = ClientToScreen(e.GetPosition());
\r
215 app.toolPalette->Move(pt);
\r
216 app.toolPalette->Show(true);
\r
217 SetCursor(*wxSTANDARD_CURSOR);
\r
218 app.toolPalette->SetCursor(*wxSTANDARD_CURSOR);
\r
219 app.toolPalette->prevTool = TOOLSelect;
\r
220 app.toolPalette->Refresh(false);
\r
223 else if (e.RightUp())
\r
225 ToolType newTool = app.toolPalette->FindSelectedTool();//, oldTool = tool;
\r
227 // We only change the tool if a new one was actually selected. Otherwise, we do nothing.
\r
228 if (newTool != TOOLNone)
\r
232 if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly
\r
233 || tool == TOOLDelPoly)
\r
236 if (tool == TOOLAddPoly)
\r
237 polyFirstPoint = true;
\r
241 app.toolPalette->Show(false);
\r
242 SetCursor(*(app.cur[tool]));
\r
244 else if (e.LeftDown())
\r
246 if (tool == TOOLScroll || tool == TOOLZoom)
\r
247 CaptureMouse(); // Make sure we capture the mouse when in scroll/zoom mode
\r
248 else if (tool == TOOLAddPt) // "Add Point" tool
\r
250 if (pts.GetNumPoints() > 0)
\r
252 wxPoint pt = GetAdjustedMousePosition(e);
\r
253 pts.InsertPoint(pts.GetNext(ptHighlight), pt.x, pt.y, (e.ShiftDown() | e.ControlDown() ? false : true));
\r
254 ptHighlight = ptNextHighlight;
\r
258 else if (tool == TOOLAddPoly) // "Add Poly" tool
\r
261 WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
\r
263 if (polyFirstPoint)
\r
265 polyFirstPoint = false;
\r
266 pts.AddNewPolyAtEnd();
\r
269 wxPoint pt = GetAdjustedMousePosition(e);
\r
270 // Append a point to the end of the structure
\r
271 pts += IPoint(pt.x, pt.y, (e.ShiftDown() | e.ControlDown() ? false : true));
\r
272 ptHighlight = pts.GetNumPoints() - 1;
\r
275 WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());
\r
278 else if (tool == TOOLSelect || tool == TOOLPolySelect)
\r
280 if (pts.GetNumPoints() > 0)
\r
282 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
\r
283 WarpPointer(pt.x, pt.y);
\r
285 if (e.ShiftDown() | e.ControlDown())
\r
286 pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
\r
289 else if (tool == TOOLDelPt)
\r
291 if (pts.GetNumPoints() > 0)
\r
293 // if (ptHighlight != -1)
\r
295 //This assumes that WM_MOUSEMOVE happens before this!
\r
296 //The above commented out line should take care of this contingency... !!! FIX !!!
\r
297 pts.DeletePoint(ptHighlight);
\r
302 else if (e.LeftUp())
\r
304 // mouseDown = false;
\r
306 if (tool == TOOLScroll || tool == TOOLZoom)
\r
309 else if (e.Dragging())
\r
311 if (e.RightIsDown())
\r
313 ToolType newTool = app.toolPalette->FindSelectedTool();
\r
315 if (newTool != app.toolPalette->prevTool)
\r
317 app.toolPalette->prevTool = newTool;
\r
318 app.toolPalette->Refresh(false);
\r
324 if (e.MiddleIsDown())
\r
326 // Calc offset from previous point
\r
327 pt = e.GetPosition();
\r
328 ptOffset.x = pt.x - ptPrevious.x,
\r
329 ptOffset.y = pt.y - ptPrevious.y;
\r
331 // Then multiply it by the scaling factor. Whee!
\r
332 // This looks wacky because we're using screen coords for the offset...
\r
333 // Otherwise, we would subtract both offsets!
\r
334 offsetX -= ptOffset.x, offsetY += ptOffset.y;
\r
340 // if (e.LeftIsDown())
\r
343 if (tool == TOOLScroll)
\r
345 // Extract current point from lParam/calc offset from previous point
\r
347 pt = e.GetPosition();
\r
348 ptOffset.x = pt.x - ptPrevious.x,
\r
349 ptOffset.y = pt.y - ptPrevious.y;
\r
351 // NOTE: OffsetViewportOrg operates in DEVICE UNITS...
\r
353 //Seems there's no equivalent for this in wxWidgets...!
\r
355 // hdc = GetDC(hWnd);
\r
356 // OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);
\r
357 // ReleaseDC(hWnd, hdc);
\r
359 // this shows that it works, so the logic above must be faulty...
\r
360 // And it is. It should convert the coords first, then do the subtraction to figure the offset...
\r
362 // Then multiply it by the scaling factor. Whee!
\r
363 // This looks wacky because we're using screen coords for the offset...
\r
364 // Otherwise, we would subtract both offsets!
\r
365 offsetX -= ptOffset.x, offsetY += ptOffset.y;
\r
370 if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)
\r
372 if (tool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
\r
374 //temporary, for testing. BTW, Select drag bug is here...!
\r
376 wxPoint pt2 = GetAdjustedMousePosition(e);
\r
377 pts.SetXY(ptHighlight, pt2.x, pt2.y);
\r
382 else if (tool == TOOLPolySelect)
\r
384 if (pts.GetNumPoints() > 0)
\r
386 wxPoint pt2 = GetAdjustedMousePosition(e);
\r
387 // Should also set onCurve here as well, depending on keystate
\r
389 pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x - pts.GetX(ptHighlight), pt2.y - pts.GetY(ptHighlight));
\r
397 else if (e.Moving())
\r
399 // else // Moving, not dragging...
\r
401 if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
\r
402 || tool == TOOLPolySelect)// || tool == TOOLAddPoly)
\r
404 wxPoint pt2 = GetAdjustedMousePosition(e);
\r
405 double closest = 1.0e+99;
\r
407 for(int i=0; i<pts.GetNumPoints(); i++)
\r
409 double dist = ((pt2.x - pts.GetX(i)) * (pt2.x - pts.GetX(i)))
\r
410 + ((pt2.y - pts.GetY(i)) * (pt2.y - pts.GetY(i)));
\r
412 if (dist < closest)
\r
413 closest = dist, ptHighlight = i;
\r
416 if (ptHighlight != oldPtHighlight)
\r
418 oldPtHighlight = ptHighlight;
\r
422 // What follows here looks like voodoo, but is really simple. What we do is
\r
423 // check to see if the mouse point has a perpendicular intersection with any of
\r
424 // the line segments. If it does, calculate the length of the perpendicular
\r
425 // and choose the smallest length. If there is no perpendicular, then choose the
\r
426 // length of line connecting the closer of either the first endpoint or the
\r
427 // second and choose the smallest of those.
\r
429 // There is one bit of math that looks like voodoo to me ATM--will explain once
\r
430 // I understand it better (the calculation of the length of the perpendicular).
\r
432 if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)
\r
434 double smallest = 1.0e+99;
\r
436 for(int i=0; i<pts.GetNumPoints(); i++)
\r
438 int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
\r
439 p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
\r
441 vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x, pt2.y, 0, p1x, p1y, 0),
\r
442 v2(pt2.x, pt2.y, 0, p2x, p2y, 0);
\r
443 double pp = ls.dot(v1) / ls.length(), dist;
\r
444 // Geometric interpretation:
\r
445 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
\r
446 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
\r
447 // then the perpendicular lies beyond the 2nd endpoint.
\r
450 dist = v1.length();
\r
451 else if (pp > ls.length())
\r
452 dist = v2.length();
\r
453 else // distance = ?Det?(ls, v1) / |ls|
\r
454 dist = fabs((ls.x * v1.y - v1.x * ls.y) / ls.length());
\r
456 //The answer to the above looks like it might be found here:
\r
458 //If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
\r
459 //intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
\r
460 //{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
\r
461 //computed by first computing the area of the triangle the three points form, then dividing by the
\r
462 //length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
\r
463 //triangle formed by three points is the determinant of the following matrix:
\r
469 //(???) By translating the start point to the origin, this can be rewritten as:
\r
470 //By subtracting row 1 from all rows, you get the following:
\r
473 //(ex - sx) (ey - sy) 0
\r
474 //(px - sx) (py - sy) 0
\r
476 //which greatly simplifies the calculation of the determinant.
\r
478 if (dist < smallest)
\r
479 smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
\r
482 if (ptNextHighlight != oldPtNextHighlight)
\r
484 oldPtNextHighlight = ptNextHighlight;
\r
491 ptPrevious = e.GetPosition();
\r
495 wxPoint TTEditWindow::GetAdjustedMousePosition(wxMouseEvent &e)
\r
497 wxCoord width, height;
\r
498 wxClientDC dc(this);
\r
500 dc.GetSize(&width, &height);
\r
501 dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
\r
502 dc.SetAxisOrientation(true, true);
\r
505 wxStatusBar * sb = ((wxFrame *)GetParent())->GetStatusBar();
\r
507 s.Printf("Logical mouse pos: %d, %d (%d, %d)", pt.x, pt.y, width, height);
\r
508 sb->SetStatusText(s);
\r
511 return e.GetLogicalPosition(dc);
\r
514 wxPoint TTEditWindow::GetAdjustedClientPosition(wxCoord x, wxCoord y)
\r
516 wxCoord width, height;
\r
517 wxClientDC dc(this);
\r
519 dc.GetSize(&width, &height);
\r
520 dc.SetDeviceOrigin(-offsetX, height - (-offsetY));
\r
521 dc.SetAxisOrientation(true, true);
\r
523 return wxPoint(dc.LogicalToDeviceX(x), dc.LogicalToDeviceY(y));
\r
532 LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
\r
536 POINT pt, ptOffset;
\r
544 MiscCenterWnd(hWnd, GetDesktopWindow());
\r
545 InitCommonControls();
\r
546 hStatusBar = CreateStatusWindow(WS_CHILD | WS_VISIBLE, statusBarTxt, hWnd, ID_STATUSBAR);
\r
551 // clean this crap up!
\r
552 // well, the only crappy thing here is using a POINT as an int array, but otherwise, this is OK
\r
553 wsprintf(strBuf, zoom, 1000);
\r
555 GetTextExtentPoint32(hdc, strBuf, lstrlen(strBuf), &sz);
\r
556 ReleaseDC(hWnd, hdc);
\r
557 zoomWndWidth = sz.cx;
\r
558 wsprintf(strBuf, zoom, 100);
\r
560 GetClientRect(hWnd, &rc1);
\r
561 pt.x = rc1.right - zoomWndWidth, pt.y = -1;
\r
562 SendMessage(hStatusBar, SB_SETPARTS, 2, (LPARAM)&pt);
\r
563 SendMessage(hStatusBar, SB_SETTEXT, (0 | SBT_NOBORDERS), (LPARAM)statusBarTxt);
\r
564 SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)strBuf);
\r
566 hToolBar = CreateToolbarEx(hWnd, WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS,
\r
567 IDR_TOOLBAR1, 3, hInst, IDB_TOOLBAR1, tbButtons, 4, 16, 16, 16, 16, sizeof(TBBUTTON));
\r
574 // The following can only be done because we use a private DC (using "CS_OWNDC")
\r
575 // (Is that true???)
\r
576 // Set the mapping to draw the character so it fits in the viewport...
\r
578 GetClientRect(hWnd, &rc1);
\r
579 GetClientRect(hStatusBar, &rc2);
\r
580 rc1.bottom -= rc2.bottom;
\r
581 SetMapMode(hdc, MM_ISOTROPIC);
\r
582 SetWindowExtEx(hdc, rc1.right, rc1.bottom, NULL);
\r
583 SetViewportExtEx(hdc, rc1.right, -rc1.bottom, NULL);
\r
584 SetViewportOrgEx(hdc, 0, rc1.bottom, NULL);
\r
585 ReleaseDC(hWnd, hdc);
\r
592 wpM.length = wpC.length = sizeof(WINDOWPLACEMENT);
\r
593 GetWindowPlacement(hMainWnd, &wpM);
\r
594 GetWindowPlacement(hCharWnd, &wpC);
\r
596 if (!IsWindowVisible(hCharWnd)) // Needed because Windows lies about visibility
\r
597 wpC.showCmd = SW_HIDE;
\r
600 GetViewportOrgEx(hdc, &ptVPM);
\r
601 ReleaseDC(hWnd, hdc);
\r
603 DestroyWindow(hWnd);
\r
610 PostQuitMessage(0);
\r
613 case WM_NCLBUTTONDOWN:
\r
615 if (wParam == HTCAPTION)
\r
617 NCMouseDown = true;
\r
618 GetWindowRect(hMainWnd, &rc1);
\r
619 GetWindowRect(hCharWnd, &rc2);
\r
620 ptWinOffset.x = rc2.left - rc1.left;
\r
621 ptWinOffset.y = rc2.top - rc1.top;
\r
624 // Let Windows do its thing with this msg, or weird things will happen...
\r
626 DefWindowProc(hWnd, msgID, wParam, lParam);
\r
627 NCMouseDown = false;
\r
630 case WM_WINDOWPOSCHANGING:
\r
634 WINDOWPOS * wp = (WINDOWPOS *)lParam;
\r
636 if (wp->hwnd == hMainWnd && !(wp->flags & SWP_NOMOVE))
\r
637 SetWindowPos(hCharWnd, 0, wp->x + ptWinOffset.x, wp->y + ptWinOffset.y,
\r
638 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
\r
641 return DefWindowProc(hWnd, msgID, wParam, lParam); // Seems this is needed... Bleah!
\r
645 hdc = BeginPaint(hWnd, &ps);
\r
647 // Scrolling can be done by using OffsetViewportOrgEx
\r
648 // Scaling can be done by adjusting SetWindowExtEx (it's denominator of txform)
\r
649 // you'd use: % = ViewportExt / WindowExt
\r
650 // But it makes the window look like crap: fuggetuboutit.
\r
651 // Instead, we have to scale EVERYTHING by hand. Crap!
\r
653 // Apparently, you *must* save the individual object types (pen, brush, etc.)
\r
655 HGDIOBJ oldPen = SelectObject(hdc, hBluePen1),
\r
656 oldBrush = SelectObject(hdc, hNullBrush);
\r
658 // Draw coordinate axes
\r
660 MoveToEx(hdc, 0, -32000, NULL);
\r
661 LineTo(hdc, 0, 32000);
\r
662 MoveToEx(hdc, -32000, 0, NULL);
\r
663 LineTo(hdc, 32000, 0);
\r
667 for(int i=0; i<pts.GetNumPoints(); i++)
\r
669 if (i == ptHighlight)
\r
671 SelectObject(hdc, hRedPen1);
\r
673 if (pts.GetOnCurve(i))
\r
675 DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
\r
676 DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
\r
680 DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
\r
681 DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
\r
684 else if ((i == ptHighlight || i == ptNextHighlight) && currentTool == TOOLAddPt)
\r
686 SelectObject(hdc, hGreenPen1);
\r
688 if (pts.GetOnCurve(i))
\r
690 DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
\r
691 DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
\r
695 DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);
\r
696 DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);
\r
701 SelectObject(hdc, hBlackPen1);
\r
703 if (pts.GetOnCurve(i))
\r
704 DrawSquareDot(hdc, pts.GetX(i), pts.GetY(i));
\r
706 DrawRoundDot(hdc, pts.GetX(i), pts.GetY(i));
\r
709 if (currentTool == TOOLDelPt && i == ptHighlight)
\r
711 SelectObject(hdc, hRedPen1);
\r
712 MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5, NULL);
\r
713 LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) + 5);
\r
714 LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5);//Lameness!
\r
715 MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5, NULL);
\r
716 LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) - 5);
\r
717 LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5);//More lameness!!
\r
721 SelectObject(hdc, hBlackPen1);
\r
723 // Draw curve formed by points
\r
725 for(int poly=0; poly<pts.GetNumPolys(); poly++)
\r
727 if (pts.GetNumPoints(poly) > 2)
\r
730 // If it's not on curve, then move to it, otherwise move to last point...
\r
732 if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
\r
733 MoveToEx(hdc, pts.GetX(poly, pts.GetNumPoints(poly) - 1), pts.GetY(poly, pts.GetNumPoints(poly) - 1), NULL);
\r
735 MoveToEx(hdc, pts.GetX(poly, 0), pts.GetY(poly, 0), NULL);
\r
737 for(int i=0; i<pts.GetNumPoints(poly); i++)
\r
739 if (pts.GetOnCurve(poly, i))
\r
740 LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
\r
743 uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
\r
744 float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
\r
745 nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
\r
747 if (!pts.GetOnCurve(poly, prev))
\r
748 px = (px + pts.GetX(poly, i)) / 2.0f,
\r
749 py = (py + pts.GetY(poly, i)) / 2.0f;
\r
751 if (!pts.GetOnCurve(poly, next))
\r
752 nx = (nx + pts.GetX(poly, i)) / 2.0f,
\r
753 ny = (ny + pts.GetY(poly, i)) / 2.0f;
\r
755 Bezier(hdc, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
\r
757 if (pts.GetOnCurve(poly, next))
\r
758 i++; // Following point is on curve, so move past it
\r
764 SelectObject(hdc, oldPen); // Restore the stuff we disrupted...
\r
765 SelectObject(hdc, oldBrush);
\r
766 EndPaint(hWnd, &ps);
\r
771 // Apparently this is needed since these windows don't update themselves.
\r
772 SendMessage(hStatusBar, msgID, wParam, lParam);
\r
773 SendMessage(hToolBar, msgID, wParam, lParam);
\r
775 // This is needed to make the 2nd status pane visible
\r
776 GetClientRect(hWnd, &rc1);
\r
777 pt.x = rc1.right - zoomWndWidth, pt.y = -1;
\r
778 SendMessage(hStatusBar, SB_SETPARTS, 2, (LPARAM)&pt);
\r
781 case WM_RBUTTONDOWN:
\r
784 SetWindowPos(hToolPalWnd, 0, pt.x, pt.y, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
\r
785 SetFocus(hToolPalWnd);
\r
786 SetCapture(hToolPalWnd); // Ensure tool palette gets RButtonUp
\r
787 SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW))); // Tool pallete has "regular" cursor
\r
790 case WM_LBUTTONDOWN:
\r
794 if (currentTool == TOOLScroll || currentTool == TOOLZoom)
\r
795 SetCapture(hWnd); // Make sure we capture the mouse when in scroll/zoom mode
\r
796 else if (currentTool == TOOLAddPt) // "Add Point" tool
\r
798 if (pts.GetNumPoints() > 0)
\r
800 //Do we really need to put a cap on this???
\r
802 // if (pts.GetNumPoints() < 16)
\r
804 pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
\r
806 DPtoLP(hdc, &pt, 1);
\r
807 pts.InsertPoint(pts.GetNext(ptHighlight), pt.x, pt.y, (wParam & (MK_SHIFT | MK_CONTROL) ? false : true));
\r
808 ptHighlight = ptNextHighlight;
\r
809 ReleaseDC(hWnd, hdc);
\r
810 InvalidateRect(hWnd, NULL, TRUE);
\r
814 else if (currentTool == TOOLAddPoly) // "Add Poly" tool
\r
817 wsprintf(strBuf, "Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());
\r
818 WriteLogMsg(strBuf);
\r
820 if (polyFirstPoint)
\r
822 polyFirstPoint = false;
\r
823 pts.AddNewPolyAtEnd();
\r
826 //Do we really need to put a cap on this???
\r
828 // if (pts.GetNumPoints() < 16)
\r
830 pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
\r
832 DPtoLP(hdc, &pt, 1);
\r
833 ReleaseDC(hWnd, hdc);
\r
834 // Append a point to the end of the structure
\r
835 pts += IPoint(pt.x, pt.y, (wParam & (MK_SHIFT | MK_CONTROL) ? false : true));
\r
836 ptHighlight = pts.GetNumPoints() - 1;
\r
837 InvalidateRect(hWnd, NULL, TRUE);
\r
840 wsprintf(strBuf, " --> [# polys: %u, # points: %u]\xD\xA", pts.GetNumPolys(), pts.GetNumPoints());
\r
841 WriteLogMsg(strBuf);
\r
844 else if (currentTool == TOOLSelect || currentTool == TOOLPolySelect)
\r
846 if (pts.GetNumPoints() > 0)
\r
848 pt.x = pts.GetX(ptHighlight), pt.y = pts.GetY(ptHighlight);
\r
850 LPtoDP(hdc, &pt, 1);
\r
851 ClientToScreen(hWnd, &pt);
\r
852 SetCursorPos(pt.x, pt.y);
\r
853 ReleaseDC(hWnd, hdc);
\r
855 if (wParam & (MK_SHIFT | MK_CONTROL))
\r
856 pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));
\r
859 else if (currentTool == TOOLDelPt)
\r
861 if (pts.GetNumPoints() > 0)
\r
863 // if (ptHighlight != -1)
\r
865 //This assumes that WM_MOUSEMOVE happens before this!
\r
866 //The above commented out line should take care of this contingency... !!! FIX !!!
\r
867 pts.DeletePoint(ptHighlight);
\r
868 InvalidateRect(hWnd, NULL, TRUE);
\r
878 if (currentTool == TOOLScroll || currentTool == TOOLZoom)
\r
885 SetCursor(hCur[currentTool]);
\r
887 // Extract current point from lParam/calc offset from previous point
\r
889 pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;
\r
890 ptOffset.x = pt.x - ptPrevious.x,
\r
891 ptOffset.y = pt.y - ptPrevious.y;
\r
895 if (currentTool == TOOLScroll)
\r
897 // NOTE: OffsetViewportOrg operates in DEVICE UNITS...
\r
900 OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);
\r
901 ReleaseDC(hWnd, hdc);
\r
903 // this shows that it works, so the logic above must be faulty...
\r
904 // And it is. It should convert the coords first, then do the subtraction to figure the offset...
\r
906 // Then multiply it by the scaling factor. Whee!
\r
908 InvalidateRect(hWnd, NULL, TRUE);
\r
909 // SendMessage(hWnd, WM_PAINT, NULL, NULL);
\r
911 else if (currentTool == TOOLAddPt || currentTool == TOOLAddPoly || currentTool == TOOLSelect)
\r
913 if (currentTool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.
\r
916 pt2.x = pt.x, pt2.y = pt.y;
\r
917 // Should also set onCurve here as well, depending on keystate
\r
920 DPtoLP(hdc, &pt2, 1);
\r
921 pts.SetXY(ptHighlight, pt2.x, pt2.y);
\r
922 ReleaseDC(hWnd, hdc);
\r
923 InvalidateRect(hWnd, NULL, TRUE);
\r
926 else if (currentTool == TOOLPolySelect)
\r
928 if (pts.GetNumPoints() > 0)
\r
931 pt2.x = pt.x, pt2.y = pt.y;
\r
932 // Should also set onCurve here as well, depending on keystate
\r
935 DPtoLP(hdc, &pt2, 1);
\r
936 pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x - pts.GetX(ptHighlight), pt2.y - pts.GetY(ptHighlight));
\r
937 ReleaseDC(hWnd, hdc);
\r
938 InvalidateRect(hWnd, NULL, TRUE);
\r
944 if (currentTool == TOOLSelect || currentTool == TOOLDelPt || currentTool == TOOLAddPt
\r
945 || currentTool == TOOLPolySelect)// || currentTool == TOOLAddPoly)
\r
948 pt2.x = pt.x, pt2.y = pt.y;
\r
950 DPtoLP(hdc, &pt2, 1);
\r
951 ReleaseDC(hWnd, hdc);
\r
953 double closest = 1.0e+99;
\r
955 for(int i=0; i<pts.GetNumPoints(); i++)
\r
957 double dist = ((pt2.x - pts.GetX(i)) * (pt2.x - pts.GetX(i)))
\r
958 + ((pt2.y - pts.GetY(i)) * (pt2.y - pts.GetY(i)));
\r
960 if (dist < closest)
\r
961 closest = dist, ptHighlight = i;
\r
964 if (ptHighlight != oldPtHighlight)
\r
966 oldPtHighlight = ptHighlight;
\r
967 InvalidateRect(hWnd, NULL, TRUE);
\r
970 // What follows here looks like voodoo, but is really simple. What we do is
\r
971 // check to see if the mouse point has a perpendicular intersection with any of
\r
972 // the line segments. If it does, calculate the length of the perpendicular
\r
973 // and choose the smallest length. If there is no perpendicular, then choose the
\r
974 // length of line connecting the closer of either the first endpoint or the
\r
975 // second and choose the smallest of those.
\r
977 // There is one bit of math that looks like voodoo to me ATM--will explain once
\r
978 // I understand it better (the calculation of the length of the perpendicular).
\r
980 if (pts.GetNumPoints() > 1 && currentTool == TOOLAddPt)
\r
982 double smallest = 1.0e+99;
\r
984 for(int i=0; i<pts.GetNumPoints(); i++)
\r
986 int32 p1x = pts.GetX(i), p1y = pts.GetY(i),
\r
987 p2x = pts.GetX(pts.GetNext(i)), p2y = pts.GetY(pts.GetNext(i));
\r
989 vector ls(p2x, p2y, 0, p1x, p1y, 0), v1(pt2.x, pt2.y, 0, p1x, p1y, 0),
\r
990 v2(pt2.x, pt2.y, 0, p2x, p2y, 0);
\r
991 double pp = ls.dot(v1) / ls.length(), dist;
\r
992 // Geometric interpretation:
\r
993 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
\r
994 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
\r
995 // then the perpendicular lies beyond the 2nd endpoint.
\r
998 dist = v1.length();
\r
999 else if (pp > ls.length())
\r
1000 dist = v2.length();
\r
1001 else // distance = ?Det?(ls, v1) / |ls|
\r
1002 dist = abs((ls.x * v1.y - v1.x * ls.y) / ls.length());
\r
1004 //The answer to the above looks like it might be found here:
\r
1006 //If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
\r
1007 //intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
\r
1008 //{e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
\r
1009 //computed by first computing the area of the triangle the three points form, then dividing by the
\r
1010 //length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
\r
1011 //triangle formed by three points is the determinant of the following matrix:
\r
1017 //(???) By translating the start point to the origin, this can be rewritten as:
\r
1018 //By subtracting row 1 from all rows, you get the following:
\r
1021 //(ex - sx) (ey - sy) 0
\r
1022 //(px - sx) (py - sy) 0
\r
1024 //which greatly simplifies the calculation of the determinant.
\r
1026 if (dist < smallest)
\r
1027 smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;
\r
1030 if (ptNextHighlight != oldPtNextHighlight)
\r
1032 oldPtNextHighlight = ptNextHighlight;
\r
1033 InvalidateRect(hWnd, NULL, TRUE);
\r
1039 ptPrevious.x = pt.x, ptPrevious.y = pt.y;
\r
1045 if (((NMHDR *)lParam)->code == TTN_NEEDTEXT)
\r
1047 LoadString(hInst, ((TOOLTIPTEXT *)lParam)->hdr.idFrom + 0x80, toolTipTxt, 16);
\r
1048 ((TOOLTIPTEXT *)lParam)->lpszText = toolTipTxt;
\r
1053 case WM_MENUSELECT:
\r
1055 statusBarTxt[0] = 0; // Clear status bar text
\r
1056 uint16 flags = wParam >> 16; // Extract flags
\r
1058 if (!(flags & MFT_SEPARATOR))
\r
1060 uint16 id = wParam & 0xFFFF;
\r
1062 if (flags & MF_POPUP)
\r
1064 if (flags & MF_SYSMENU)
\r
1067 id = IDM_FILEMENU + wParam;
\r
1070 LoadString(hInst, id, statusBarTxt, 64);
\r
1073 SendMessage(hStatusBar, SB_SETTEXT, 0 + SBT_NOBORDERS, (LPARAM)statusBarTxt);
\r
1078 uint16 cmd = wParam & 0xFFFF;
\r
1080 if (cmd == IDM_NEW)
\r
1082 // call CmdIDM_NEW
\r
1084 else if (cmd == IDM_OPEN)
\r
1086 // call SaveChanges
\r
1088 // movmov ofn.hwndOwner, eax, hMainWnd
\r
1089 // mov ofn.Flags, OFN_PATHMUSTEXIST + OFN_FILEMUSTEXIST
\r
1090 // invoke GetOpenFileName, ADDR ofn
\r
1094 //szDMsg1a BYTE "Could not open the file (GetOpenFileName)...", 0
\r
1095 //szDMsg1b BYTE "Open error!", 0
\r
1096 //szDMsg1c BYTE "About to attempt to open file...", 0
\r
1098 ////invoke MessageBox, hWnd, ADDR szDMsg1a, ADDR szDMsg1b, MB_ICONERROR or MB_OK
\r
1099 //invoke MessageBox, hMainWnd, ADDR szDMsg1c, ADDR szFile, MB_ICONERROR or MB_OK
\r
1100 // invoke LoadTTF, ADDR szFile
\r
1103 // // <<< FILE OPEN CODE HERE >>>
\r
1104 // or fFileStatus, NAMEDbit
\r
1105 // and fFileStatus, NOT CHANGEDbit
\r
1106 // call NewWindowName
\r
1107 // mov eax, TRUE // return TRUE
\r
1109 // //��������������������������������������
\r
1110 //OpenError: invoke GetLastError
\r
1111 // // <<< FILE OPEN ERROR CODE HERE >>>
\r
1113 // zero eax // return FALSE
\r
1116 else if (cmd == IDM_SAVEAS)
\r
1118 // and fFileStatus, NOT NAMEDbit
\r
1119 // call CmdIDM_SAVE
\r
1121 else if (cmd == IDM_SAVE)
\r
1123 // call CmdIDM_SAVE
\r
1125 else if (cmd == IDM_ABOUT)
\r
1126 DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ABOUT), hMainWnd, AboutProc, NULL);
\r
1127 else if (cmd == IDM_EXIT)
\r
1128 SendMessage(hWnd, WM_CLOSE, 0, 0);
\r
1129 else if (cmd == ID_TBCHARWIN)
\r
1131 ShowWindow(hCharWnd, (IsWindowVisible(hCharWnd) ? SW_HIDE : SW_SHOWNOACTIVATE));
\r
1134 wpC.length = sizeof(WINDOWPLACEMENT);
\r
1135 GetWindowPlacement(hCharWnd, &wpC);
\r
1136 wsprintf(strBuf, "Char window showCmd = %08X...\n", wpC.showCmd);
\r
1137 WriteLogMsg(strBuf);
\r
1141 return DefWindowProc(hWnd, msgID, wParam, lParam);
\r
1146 return DefWindowProc(hWnd, msgID, wParam, lParam);
\r
1153 // Initialize TTF data
\r
1155 void CreateNewDoc(void)
\r
1160 // Save changes to document before quitting
\r
1162 bool SaveChanges(void)
\r
1170 // ABOUT Dialog WndProc
\r
1172 BOOL CALLBACK AboutProc(HWND hDlg, UINT msgID, WPARAM wParam, LPARAM lParam)
\r
1176 case WM_INITDIALOG:
\r
1178 MiscCenterWnd(hDlg, hMainWnd);
\r
1183 if (wParam == IDOK || wParam == IDCANCEL)
\r
1184 EndDialog(hDlg, TRUE);
\r
1196 // Character Window WndProc
\r
1198 WndProcCW PROC STDCALL, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
\r
1200 mov eax, uMsg // pickup our message
\r
1201 .IF (eax == WM_PAINT)
\r
1202 mov eax, 0 // Non-sense... (placeholder!)
\r
1203 // Scan conversion etc. goes here...
\r
1204 .ELSEIF (eax == WM_LBUTTONDOWN)
\r
1205 invoke SetCapture, hCharWnd
\r
1206 .ELSEIF (eax == WM_LBUTTONUP)
\r
1207 invoke ReleaseCapture
\r
1208 invoke SetFocus, hMainWnd // Make sure the main wnd keeps focus!
\r
1209 .ELSEIF (eax == WM_NCLBUTTONDOWN)
\r
1210 invoke DefWindowProc, hWnd, uMsg, wParam, lParam // Let it do its thing
\r
1211 invoke SetFocus, hMainWnd // Make sure the main wnd keeps focus!
\r
1213 DefProc: invoke DefWindowProc, hWnd, uMsg, wParam, lParam
\r
1220 // Character Window WndProc
\r
1222 LRESULT CALLBACK WndProcCW(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
\r
1227 return DefWindowProc(hWnd, msgID, wParam, lParam);
\r
1234 // Function prototypes
\r
1236 int32 FindSelectedTool(void);
\r
1239 // Tool Palette WndProc
\r
1241 LRESULT CALLBACK WndProcTP(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
\r
1246 static uint32 prevTool = -1;
\r
1252 hdc = BeginPaint(hWnd, &ps);
\r
1253 HDC newDC = CreateCompatibleDC(NULL);
\r
1254 SelectObject(newDC, hBMToolPal1);
\r
1255 BitBlt(hdc, 0, 0, sizeTPBM.x, sizeTPBM.y, newDC, 0, 0, SRCCOPY);
\r
1258 // This is crappy. Find some way to tighten this up!
\r
1259 int32 tool = FindSelectedTool();
\r
1263 newDC = CreateCompatibleDC(NULL);
\r
1264 SelectObject(newDC, hBMToolPal1);
\r
1265 //need ul corner of bitmap, ul corner of dest, width/height
\r
1266 pt.x = sizeStamp.x * (tool & 0x03), pt.y = sizeStamp.y * (tool >> 2);
\r
1267 BitBlt(hdc, pt.x, pt.y, sizeStamp.x, sizeStamp.y, newDC, pt.x, pt.y, NOTSRCCOPY);
\r
1271 EndPaint(hWnd, &ps);
\r
1274 case WM_MOUSEMOVE:
\r
1276 int32 tool = FindSelectedTool();
\r
1278 if (tool != prevTool)
\r
1281 InvalidateRect(hWnd, NULL, FALSE);
\r
1286 case WM_RBUTTONUP:
\r
1288 int32 tool = FindSelectedTool(), oldTool = currentTool;
\r
1291 currentTool = tool;
\r
1293 if (currentTool != TOOLSelect && currentTool != TOOLDelPt && currentTool != TOOLAddPt
\r
1294 && currentTool != TOOLPolySelect)
\r
1297 if (currentTool != oldTool)
\r
1298 InvalidateRect(hMainWnd, NULL, TRUE);
\r
1300 if (currentTool == TOOLAddPoly)
\r
1304 polyFirstPoint = true;
\r
1306 wsprintf(strBuf, "--> Selected poly tool, polyFirstPoint is %s\n", polyFirstPoint ? "true" : "false");
\r
1307 WriteLogMsg(strBuf);
\r
1312 ShowWindow(hToolPalWnd, SW_HIDE);
\r
1313 SetFocus(hMainWnd); // Make sure the main wnd keeps focus!
\r
1318 return DefWindowProc(hWnd, msgID, wParam, lParam);
\r
1325 // Find which tool we're pointing at
\r
1326 // Use: xcoord = mouse.x / (bmsize.x/4), ycoord = mouse.y / (bmsize.y/2)
\r
1328 int32 FindSelectedTool(void)
\r
1332 GetCursorPos(&pt);
\r
1333 ScreenToClient(hToolPalWnd, &pt);
\r
1335 uint32 x = (uint32)pt.x / sizeStamp.x, y = (uint32)pt.y / sizeStamp.y, tool = -1;
\r
1337 if (x < 4 && y < 2)
\r
1339 tool = (y * 4) + x;
\r
1342 // tool = -1; // 7 has no tool...
\r
1350 // Misc center window
\r
1352 void MiscCenterWnd(HWND hChild, HWND hParent)
\r
1354 RECT parent, child;
\r
1356 if (!GetWindowRect(hParent, &parent) || !GetWindowRect(hChild, &child))
\r
1359 int32 x = parent.left + (((parent.right - parent.left) - (child.right - child.left)) / 2),
\r
1360 y = parent.top + (((parent.bottom - parent.top) - (child.bottom - child.top)) / 2);
\r
1364 else if (x > GetSystemMetrics(SM_CXFULLSCREEN) - (child.right - child.left))
\r
1365 x = GetSystemMetrics(SM_CXFULLSCREEN) - (child.right - child.left);
\r
1369 else if (y > GetSystemMetrics(SM_CYFULLSCREEN) - (child.bottom - child.top))
\r
1370 y = GetSystemMetrics(SM_CYFULLSCREEN) - (child.bottom - child.top);
\r
1372 SetWindowPos(hChild, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
\r
1376 // Allow only one instance
\r
1378 bool OnlyOneInstance(void)
\r
1380 HWND window = FindWindow(className, NULL);
\r
1382 if (window == NULL)
\r
1385 ShowWindow(window, SW_SHOWNORMAL);
\r
1386 SetWindowPos(window, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
\r
1392 // Load/Allocate all resources
\r
1394 bool LoadResources(void)
\r
1396 hCur[0] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR1));
\r
1397 hCur[1] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR2));
\r
1398 hCur[2] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR3));
\r
1399 hCur[3] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR4));
\r
1400 hCur[4] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR5));
\r
1401 hCur[5] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR6));
\r
1402 hCur[6] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR7));
\r
1403 hCur[7] = LoadCursor(hInst, MAKEINTRESOURCE(IDC_CURSOR8));
\r
1407 hBMToolPal1 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TOOLPAL1));
\r
1408 GetObject(hBMToolPal1, sizeof(bm), &bm);
\r
1412 sizeTPBM.x = bm.bmWidth, sizeTPBM.y = bm.bmHeight;
\r
1413 sizeStamp.x = bm.bmWidth / 4, sizeStamp.y = bm.bmHeight / 2;
\r
1415 hBluePen1 = CreatePen(PS_DOT, 1, 0x00FF0000);
\r
1416 hRedPen1 = CreatePen(PS_SOLID, 1, 0x000000FF);
\r
1417 hGreenPen1 = CreatePen(PS_SOLID, 1, 0x0000AF00);
\r
1418 hBlackPen1 = CreatePen(PS_SOLID, 1, 0x00000000);
\r
1420 LOGBRUSH lb = { BS_NULL, 0, 0 };
\r
1422 hNullBrush = CreateBrushIndirect(&lb);
\r
1428 // Deallocate all resources
\r
1430 void DeallocateResources(void)
\r
1432 DeleteObject(hBMToolPal1);
\r
1433 DeleteObject(hBluePen1);
\r
1434 DeleteObject(hRedPen1);
\r
1435 DeleteObject(hGreenPen1);
\r
1436 DeleteObject(hBlackPen1);
\r
1437 DeleteObject(hNullBrush);
\r
1441 // Save all application specific data, so we can pick up where we last left off...
\r
1443 void SaveAppState(void)
\r
1445 SetINIInt("Main", "flags", wpM.flags);
\r
1446 SetINIInt("Main", "showCmd", wpM.showCmd);
\r
1447 SetINIInt("Main", "x1", wpM.rcNormalPosition.left);
\r
1448 SetINIInt("Main", "y1", wpM.rcNormalPosition.top);
\r
1449 SetINIInt("Main", "x2", wpM.rcNormalPosition.right);
\r
1450 SetINIInt("Main", "y2", wpM.rcNormalPosition.bottom);
\r
1452 SetINIInt("Main", "vpx", ptVPM.x);
\r
1453 SetINIInt("Main", "vpy", ptVPM.y);
\r
1455 SetINIInt("Char", "flags", wpC.flags);
\r
1456 SetINIInt("Char", "showCmd", wpC.showCmd);
\r
1457 SetINIInt("Char", "x1", wpC.rcNormalPosition.left);
\r
1458 SetINIInt("Char", "y1", wpC.rcNormalPosition.top);
\r
1459 SetINIInt("Char", "x2", wpC.rcNormalPosition.right);
\r
1460 SetINIInt("Char", "y2", wpC.rcNormalPosition.bottom);
\r
1462 // Need to write out currently opened font, character looking at, other misc. crap
\r
1463 // SetINIString("Main", "currentFile", pDoc->GetPathName());
\r
1464 // SetINIInt("Main", "currentChar", pDoc->character_num);
\r
1468 // Restore all application specific data previously saved
\r
1470 bool RestoreAppState(void)
\r
1474 WINDOWPLACEMENT wp;
\r
1475 wp.length = sizeof(WINDOWPLACEMENT);
\r
1476 GetWindowPlacement(hMainWnd, &wp);
\r
1478 wp.flags = GetINIInt("Main", "flags", wp.flags);
\r
1479 wp.showCmd = GetINIInt("Main", "showCmd", wp.showCmd);
\r
1480 wp.rcNormalPosition.left = GetINIInt("Main", "x1", wp.rcNormalPosition.left);
\r
1481 wp.rcNormalPosition.top = GetINIInt("Main", "y1", wp.rcNormalPosition.top);
\r
1482 wp.rcNormalPosition.right = GetINIInt("Main", "x2", wp.rcNormalPosition.right);
\r
1483 wp.rcNormalPosition.bottom = GetINIInt("Main", "y2", wp.rcNormalPosition.bottom);
\r
1485 SetWindowPlacement(hMainWnd, &wp);
\r
1489 hdc = GetDC(hMainWnd);
\r
1490 GetViewportOrgEx(hdc, &pt);
\r
1492 pt.x = GetINIInt("Main", "vpx", pt.x);
\r
1493 pt.y = GetINIInt("Main", "vpy", pt.y);
\r
1495 SetViewportOrgEx(hdc, pt.x, pt.y, NULL);
\r
1496 ReleaseDC(hMainWnd, hdc);
\r
1498 GetWindowPlacement(hCharWnd, &wp);
\r
1500 wp.flags = GetINIInt("Char", "flags", wp.flags);
\r
1501 wp.showCmd = GetINIInt("Char", "showCmd", wp.showCmd);
\r
1502 wp.rcNormalPosition.left = GetINIInt("Char", "x1", wp.rcNormalPosition.left);
\r
1503 wp.rcNormalPosition.top = GetINIInt("Char", "y1", wp.rcNormalPosition.top);
\r
1504 wp.rcNormalPosition.right = GetINIInt("Char", "x2", wp.rcNormalPosition.right);
\r
1505 wp.rcNormalPosition.bottom = GetINIInt("Char", "y2", wp.rcNormalPosition.bottom);
\r
1507 SetWindowPlacement(hCharWnd, &wp);
\r
1509 if (wp.showCmd == SW_HIDE)
\r
1510 SendMessage(hToolBar, TB_SETSTATE, ID_TBCHARWIN, MAKELONG(TBSTATE_ENABLED, 0));
\r
1512 // CString lastFile = theApplicationObject.GetProfileString(version, "currentFile", "");
\r
1513 // int lastChar = theApplicationObject.GetProfileInt(version, "currentChar", 0);
\r
1514 // if (lastFile.GetLength())
\r
1516 // // Attempt to restore the last session by open the last file used, etc...
\r
1517 // if (!pDoc->m_myFont.Load(lastFile))
\r
1519 // // Err, make sure you can support any allegations you make below, buddy!
\r
1520 // AfxMessageBox("The last file opened with TTF Edit\n\rseems to have been moved or deleted.");
\r
1524 // pDoc->m_myFont.SetGlyph(lastChar); // Set TTF object to last used char
\r
1525 // pDoc->character_num = lastChar;
\r
1526 // pDoc->SetPathName(lastFile);
\r
1528 // BYTE name[512];
\r
1529 // pDoc->m_myFont.GetCharName(lastChar, name);
\r
1530 // m_wndOwned.SetWindowText((char *)name);
\r
1540 bool Initialization(void)
\r
1544 if (!LoadResources())
\r
1547 RtlFillMemory(&wcex, sizeof(WNDCLASSEX), 0);
\r
1548 wcex.cbSize = sizeof(WNDCLASSEX);
\r
1549 wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
\r
1550 wcex.lpfnWndProc = WndProc;
\r
1551 wcex.hInstance = hInst;
\r
1552 wcex.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON));
\r
1553 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
\r
1554 wcex.lpszMenuName = MAKEINTRESOURCE(IDM_MENU);
\r
1555 wcex.lpszClassName = className;
\r
1556 wcex.hIconSm = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 16, 16, NULL);
\r
1558 if (!RegisterClassEx(&wcex))
\r
1561 hMainWnd = CreateWindowEx(NULL, className, className, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
\r
1562 0, 0, 0x1A0, 0x180, NULL, NULL, hInst, NULL);
\r
1567 ShowWindow(hMainWnd, nCmdShow);
\r
1568 UpdateWindow(hMainWnd);
\r
1570 // Character window creation
\r
1572 wcex.lpfnWndProc = WndProcCW;
\r
1573 wcex.lpszMenuName = NULL;
\r
1574 wcex.lpszClassName = CNCharWin;
\r
1575 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // Owned windows have "regular" cursors
\r
1577 if (!RegisterClassEx(&wcex))
\r
1580 hCharWnd = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW, CNCharWin,
\r
1581 curCharName, WS_POPUP | WS_CAPTION | WS_VISIBLE | WS_THICKFRAME,
\r
1582 100, 100, 120, 120, hMainWnd, NULL, hInst, NULL);
\r
1587 ShowWindow(hCharWnd, SW_SHOWNORMAL);
\r
1588 UpdateWindow(hCharWnd);
\r
1589 SetFocus(hMainWnd); // Make sure main wnd has focus!
\r
1591 // Tool palette window creation
\r
1593 wcex.lpfnWndProc = WndProcTP;
\r
1594 wcex.lpszClassName = CNToolPal;
\r
1596 if (!RegisterClassEx(&wcex))
\r
1599 hToolPalWnd = CreateWindowEx(WS_EX_WINDOWEDGE, CNToolPal, NULL, WS_POPUP,
\r
1600 0, 0, sizeTPBM.x, sizeTPBM.y, hMainWnd, NULL, hInst, NULL);
\r
1605 // Note: A better way to handle this would be to have a sub that registers ALL
\r
1606 // classes beforehand and passes a TRUE/FALSE back depending on whether or not
\r
1607 // all the classes were able to be registered or not, THEN create the windows
\r
1608 // and controls...
\r
1610 RestoreAppState(); // Restore app related stuff
\r