]> Shamusworld >> Repos - ttedit/blob - src/editwindow.cpp
Repurposed code in bezier.h/cpp, added middle button scrolling
[ttedit] / src / editwindow.cpp
1 //\r
2 // TTEDIT.CPP - The TrueType Editor\r
3 // by James L. Hammons\r
4 // (C) 2004 Underground Software\r
5 //\r
6 // JLH = James L. Hammons <jlhamm@acm.org>\r
7 //\r
8 // Who  When        What\r
9 // ---  ----------  -------------------------------------------------------------\r
10 // JLH  08/28/2008  Created this file\r
11 //\r
12 \r
13 // FIXED:\r
14 //\r
15 // STILL TO BE DONE:\r
16 //\r
17 // - Fix bug in Glyphpoints when dragging on an empty canvas or loading a font\r
18 // - Fix scrolling, zooming, settings (ini)\r
19 //\r
20 \r
21 // Uncomment this for debugging...\r
22 #define DEBUG\r
23 #define DEBUGFOO            // Various tool debugging...\r
24 #define DEBUGTP                         // Toolpalette debugging...\r
25 \r
26 #include "editwindow.h"\r
27 #include "graphicprimitives.h"\r
28 #include "toolwindow.h"\r
29 #include "debug.h"\r
30 #include "vector.h"\r
31 \r
32 \r
33 BEGIN_EVENT_TABLE(TTEditWindow, wxWindow)\r
34         EVT_PAINT(TTEditWindow::OnPaint)\r
35         EVT_MOUSE_EVENTS(TTEditWindow::OnMouseEvent)\r
36 END_EVENT_TABLE()\r
37 \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
43\r
44         SetCursor(*(app.cur[tool]));\r
45         SetBackgroundColour(wxColour(0xFF, 0xFF, 0xFF));\r
46 \r
47         wxString s;\r
48         s.Printf(_("Zoom: %.2f%%"), scale * 100.0);\r
49         parent->SetStatusText(s, 1);\r
50 }\r
51 \r
52 TTEditWindow::~TTEditWindow(void)\r
53 {\r
54         if (bmp)\r
55                 delete bmp;\r
56 }\r
57 \r
58 void TTEditWindow::OnPaint(wxPaintEvent &e)\r
59 {\r
60         wxPaintDC dc(this);\r
61 //Doesn't do crap!\r
62 //dc.SetBackground(*wxWHITE_BRUSH);\r
63 \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
68 \r
69         dc.SetDeviceOrigin(-offsetX, height - (-offsetY));\r
70         dc.SetAxisOrientation(true, true);\r
71 \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
78 \r
79         dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0xFF), 1, wxDOT)));\r
80 //      dc.DrawLine(0, 0, 10, 10);\r
81 \r
82     // Draw coordinate axes\r
83 \r
84         dc.CrossHair(0, 0);\r
85 \r
86     // Draw points\r
87 \r
88         for(int i=0; i<pts.GetNumPoints(); i++)\r
89         {\r
90                 if (i == ptHighlight)\r
91                 {\r
92                         dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));\r
93 //                      SelectObject(hdc, hRedPen1);\r
94 \r
95                         if (pts.GetOnCurve(i))\r
96                         {\r
97                                 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 7);\r
98                                 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 9);\r
99                         }\r
100                         else\r
101                         {\r
102                                 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 7);\r
103                                 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 9);\r
104                         }\r
105                 }\r
106                 else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)\r
107                 {\r
108                         dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0xAF, 0x00), 1, wxSOLID)));\r
109 //                      SelectObject(hdc, hGreenPen1);\r
110 \r
111                         if (pts.GetOnCurve(i))\r
112                         {\r
113                                 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 7);\r
114                                 DrawSquareDotN(dc, pts.GetX(i), pts.GetY(i), 9);\r
115                         }\r
116                         else\r
117                         {\r
118                                 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 7);\r
119                                 DrawRoundDotN(dc, pts.GetX(i), pts.GetY(i), 9);\r
120                         }\r
121                 }\r
122                 else\r
123                 {\r
124                         dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));\r
125 //                      SelectObject(hdc, hBlackPen1);\r
126 \r
127                         if (pts.GetOnCurve(i))\r
128                                 DrawSquareDot(dc, pts.GetX(i), pts.GetY(i));\r
129                         else\r
130                                 DrawRoundDot(dc, pts.GetX(i), pts.GetY(i));\r
131                 }\r
132 \r
133                 if (tool == TOOLDelPt && i == ptHighlight)\r
134                 {\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
145                 }\r
146         }\r
147 \r
148 //              SelectObject(hdc, hBlackPen1);\r
149         dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));\r
150 \r
151         // Draw curve formed by points\r
152 \r
153         for(int poly=0; poly<pts.GetNumPolys(); poly++)\r
154         {\r
155                 if (pts.GetNumPoints(poly) > 2)\r
156                 {\r
157                         // Initial move...\r
158                         // If it's not on curve, then move to it, otherwise move to last point...\r
159 \r
160                         wxCoord x, y;\r
161         \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
164                         else\r
165                                 x = (wxCoord)pts.GetX(poly, 0), y = (wxCoord)pts.GetY(poly, 0);\r
166         \r
167                         for(int i=0; i<pts.GetNumPoints(poly); i++)\r
168                         {\r
169                                 if (pts.GetOnCurve(poly, i))\r
170 //                                      LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));\r
171                                 {\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
174                                 }\r
175                                 else\r
176                                 {\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
180         \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
184         \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
188         \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
191         \r
192                                         if (pts.GetOnCurve(poly, next))\r
193                                                 i++;                                    // Following point is on curve, so move past it\r
194                                 }\r
195                         }\r
196                 }\r
197         }\r
198 \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
203 }\r
204 \r
205 void TTEditWindow::OnMouseEvent(wxMouseEvent &e)\r
206 {\r
207 #ifdef DEBUGTP\r
208 //printf("!!! This window %s focus...!\n", (HasCapture() ? "has" : "doesn't have"));\r
209 #endif\r
210         if (e.RightDown())\r
211         {\r
212                 // Handle tool palette (NOTE: tool palette deals with RightUp event.)\r
213 \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
221                 CaptureMouse();\r
222         }\r
223         else if (e.RightUp())\r
224         {\r
225                 ToolType newTool = app.toolPalette->FindSelectedTool();//, oldTool = tool;\r
226 \r
227                 // We only change the tool if a new one was actually selected. Otherwise, we do nothing.\r
228                 if (newTool != TOOLNone)\r
229                 {\r
230                         tool = newTool;\r
231 \r
232                         if (tool == TOOLScroll || tool == TOOLZoom || tool == TOOLAddPoly\r
233                                 || tool == TOOLDelPoly)\r
234                                 ptHighlight = -1;\r
235 \r
236                         if (tool == TOOLAddPoly)\r
237                                 polyFirstPoint = true;\r
238                 }\r
239 \r
240                 ReleaseMouse();\r
241                 app.toolPalette->Show(false);\r
242                 SetCursor(*(app.cur[tool]));\r
243         }\r
244         else if (e.LeftDown())\r
245         {\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
249                 {\r
250                         if (pts.GetNumPoints() > 0)\r
251                         {\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
255                                 Refresh();\r
256                         }\r
257                 }\r
258                 else if (tool == TOOLAddPoly)   // "Add Poly" tool\r
259                 {\r
260 #ifdef DEBUGFOO\r
261 WriteLogMsg("Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());\r
262 #endif\r
263                         if (polyFirstPoint)\r
264                         {\r
265                                 polyFirstPoint = false;\r
266                                 pts.AddNewPolyAtEnd();\r
267                         }\r
268 \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
273                         Refresh();\r
274 #ifdef DEBUGFOO\r
275 WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumPoints());\r
276 #endif\r
277                 }\r
278                 else if (tool == TOOLSelect || tool == TOOLPolySelect)\r
279                 {\r
280                         if (pts.GetNumPoints() > 0)\r
281                         {\r
282                                 pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));\r
283                                 WarpPointer(pt.x, pt.y);\r
284 \r
285                                 if (e.ShiftDown() | e.ControlDown())\r
286                                         pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));\r
287                         }\r
288                 }\r
289                 else if (tool == TOOLDelPt)\r
290                 {\r
291                         if (pts.GetNumPoints() > 0)\r
292 //Or could use:\r
293 //                      if (ptHighlight != -1)\r
294                         {\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
298                                 Refresh();\r
299                         }\r
300                 }\r
301         }\r
302         else if (e.LeftUp())\r
303         {\r
304 //              mouseDown = false;\r
305 \r
306                 if (tool == TOOLScroll || tool == TOOLZoom)\r
307                         ReleaseMouse();\r
308         }\r
309         else if (e.Dragging())\r
310         {\r
311                 if (e.RightIsDown())\r
312                 {\r
313                         ToolType newTool = app.toolPalette->FindSelectedTool();\r
314 \r
315                         if (newTool != app.toolPalette->prevTool)\r
316                         {\r
317                                 app.toolPalette->prevTool = newTool;\r
318                                 app.toolPalette->Refresh(false);\r
319                         }\r
320 \r
321                         return;\r
322                 }\r
323 \r
324                 if (e.MiddleIsDown())\r
325                 {\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
330 \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
335                         Refresh();\r
336 \r
337                         return;\r
338                 }\r
339 \r
340 //              if (e.LeftIsDown())\r
341 //              {\r
342 #if 0\r
343                         if (tool == TOOLScroll)\r
344                         {\r
345                             // Extract current point from lParam/calc offset from previous point\r
346 \r
347                                 pt = e.GetPosition();\r
348                                 ptOffset.x = pt.x - ptPrevious.x,\r
349                                 ptOffset.y = pt.y - ptPrevious.y;\r
350 \r
351                                 // NOTE: OffsetViewportOrg operates in DEVICE UNITS...\r
352 \r
353 //Seems there's no equivalent for this in wxWidgets...!\r
354 //!!! FIX !!!\r
355 //                              hdc = GetDC(hWnd);\r
356 //                              OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);\r
357 //                              ReleaseDC(hWnd, hdc);\r
358 \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
361 // Above: DONE\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
366                                 Refresh();\r
367                         }\r
368                         else\r
369 #endif\r
370  if (tool == TOOLAddPt || tool == TOOLAddPoly || tool == TOOLSelect)\r
371                         {\r
372                                 if (tool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.\r
373                                 {\r
374 //temporary, for testing. BTW, Select drag bug is here...!\r
375 #if 0\r
376                                         wxPoint pt2 = GetAdjustedMousePosition(e);\r
377                                         pts.SetXY(ptHighlight, pt2.x, pt2.y);\r
378                                         Refresh();\r
379 #endif\r
380                                 }\r
381                         }\r
382                         else if (tool == TOOLPolySelect)\r
383                         {\r
384                                 if (pts.GetNumPoints() > 0)\r
385                                 {\r
386                                         wxPoint pt2 = GetAdjustedMousePosition(e);\r
387                                         // Should also set onCurve here as well, depending on keystate\r
388 //Or should we?\r
389                                         pts.OffsetPoly(pts.GetPoly(ptHighlight), pt2.x - pts.GetX(ptHighlight), pt2.y - pts.GetY(ptHighlight));\r
390                                         Refresh();\r
391                                 }\r
392                         }\r
393 //              }\r
394 \r
395                 ptPrevious = pt;\r
396         }\r
397         else if (e.Moving())\r
398         {\r
399 //              else    // Moving, not dragging...\r
400 //              {\r
401                         if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt\r
402                                 || tool == TOOLPolySelect)// || tool == TOOLAddPoly)\r
403                         {\r
404                                 wxPoint pt2 = GetAdjustedMousePosition(e);\r
405                                 double closest = 1.0e+99;\r
406 \r
407                                 for(int i=0; i<pts.GetNumPoints(); i++)\r
408                                 {\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
411 \r
412                                         if (dist < closest)\r
413                                                 closest = dist, ptHighlight = i;\r
414                                 }\r
415 \r
416                                 if (ptHighlight != oldPtHighlight)\r
417                                 {\r
418                                         oldPtHighlight = ptHighlight;\r
419                                         Refresh();\r
420                                 }\r
421 \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
428 \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
431 \r
432                                 if (pts.GetNumPoints() > 1 && tool == TOOLAddPt)\r
433                                 {\r
434                                         double smallest = 1.0e+99;\r
435 \r
436                                         for(int i=0; i<pts.GetNumPoints(); i++)\r
437                                         {\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
440 \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
448 \r
449                                                 if (pp < 0.0)\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
455 \r
456 //The answer to the above looks like it might be found here:\r
457 //\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
464 //\r
465 //sx sy 1\r
466 //ex ey 1\r
467 //px py 1\r
468 //\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
471 //\r
472 //0         0         0\r
473 //(ex - sx) (ey - sy) 0\r
474 //(px - sx) (py - sy) 0\r
475 //\r
476 //which greatly simplifies the calculation of the determinant.\r
477 \r
478                                                 if (dist < smallest)\r
479                                                         smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;\r
480                                         }\r
481 \r
482                                         if (ptNextHighlight != oldPtNextHighlight)\r
483                                         {\r
484                                                 oldPtNextHighlight = ptNextHighlight;\r
485                                                 Refresh();\r
486                                         }\r
487                                 }\r
488                         }\r
489 //              }\r
490 \r
491                 ptPrevious = e.GetPosition();\r
492         }\r
493 }\r
494 \r
495 wxPoint TTEditWindow::GetAdjustedMousePosition(wxMouseEvent &e)\r
496 {\r
497         wxCoord width, height;\r
498         wxClientDC dc(this);\r
499 \r
500         dc.GetSize(&width, &height);\r
501         dc.SetDeviceOrigin(-offsetX, height - (-offsetY));\r
502         dc.SetAxisOrientation(true, true);\r
503 \r
504 #if 0\r
505 wxStatusBar * sb = ((wxFrame *)GetParent())->GetStatusBar();\r
506 wxString s;\r
507 s.Printf("Logical mouse pos: %d, %d (%d, %d)", pt.x, pt.y, width, height);\r
508 sb->SetStatusText(s);\r
509 #endif\r
510 \r
511         return e.GetLogicalPosition(dc);\r
512 }\r
513 \r
514 wxPoint TTEditWindow::GetAdjustedClientPosition(wxCoord x, wxCoord y)\r
515 {\r
516         wxCoord width, height;\r
517         wxClientDC dc(this);\r
518 \r
519         dc.GetSize(&width, &height);\r
520         dc.SetDeviceOrigin(-offsetX, height - (-offsetY));\r
521         dc.SetAxisOrientation(true, true);\r
522 \r
523         return wxPoint(dc.LogicalToDeviceX(x), dc.LogicalToDeviceY(y));\r
524 }\r
525 \r
526 \r
527 #if 0\r
528 \r
529 !!! OLD STUFF !!!\r
530 \r
531 \r
532 LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)\r
533 {\r
534         RECT rc1, rc2;\r
535         HDC hdc;\r
536         POINT pt, ptOffset;\r
537         SIZE sz;\r
538         PAINTSTRUCT ps;\r
539 \r
540         switch (msgID)\r
541         {\r
542         case WM_CREATE:\r
543 \r
544                 MiscCenterWnd(hWnd, GetDesktopWindow());\r
545                 InitCommonControls();\r
546                 hStatusBar = CreateStatusWindow(WS_CHILD | WS_VISIBLE, statusBarTxt, hWnd, ID_STATUSBAR);\r
547 \r
548                 if (!hStatusBar)\r
549                         return -1;\r
550 \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
554                 hdc = GetDC(hWnd);\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
559 \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
565 \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
568 \r
569                 if (!hToolBar)\r
570                         return -1;\r
571 \r
572                 CreateNewDoc();\r
573 \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
577                 hdc = GetDC(hWnd);\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
586                 break;\r
587 \r
588         case WM_CLOSE:\r
589 \r
590                 if (SaveChanges())\r
591                 {\r
592                         wpM.length = wpC.length = sizeof(WINDOWPLACEMENT);\r
593                         GetWindowPlacement(hMainWnd, &wpM);\r
594                         GetWindowPlacement(hCharWnd, &wpC);\r
595 \r
596                         if (!IsWindowVisible(hCharWnd))         // Needed because Windows lies about visibility\r
597                                 wpC.showCmd = SW_HIDE;\r
598 \r
599                         hdc = GetDC(hWnd);\r
600                         GetViewportOrgEx(hdc, &ptVPM);\r
601                         ReleaseDC(hWnd, hdc);\r
602 \r
603                         DestroyWindow(hWnd);\r
604                 }\r
605 \r
606                 break;\r
607 \r
608         case WM_DESTROY:\r
609 \r
610                 PostQuitMessage(0);\r
611                 break;\r
612 \r
613         case WM_NCLBUTTONDOWN:\r
614 \r
615                 if (wParam == HTCAPTION)\r
616                 {\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
622                 }\r
623                     \r
624                 // Let Windows do its thing with this msg, or weird things will happen...\r
625 \r
626                 DefWindowProc(hWnd, msgID, wParam, lParam);\r
627                 NCMouseDown = false;\r
628                 break;\r
629 \r
630         case WM_WINDOWPOSCHANGING:\r
631 \r
632                 if (NCMouseDown)\r
633                 {\r
634                         WINDOWPOS * wp = (WINDOWPOS *)lParam;\r
635 \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
639                 }\r
640 \r
641                 return DefWindowProc(hWnd, msgID, wParam, lParam);      // Seems this is needed... Bleah!\r
642 \r
643         case WM_PAINT:\r
644         {\r
645                 hdc = BeginPaint(hWnd, &ps);\r
646 \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
652 \r
653                 // Apparently, you *must* save the individual object types (pen, brush, etc.)\r
654 \r
655                 HGDIOBJ oldPen = SelectObject(hdc, hBluePen1),\r
656                         oldBrush = SelectObject(hdc, hNullBrush);\r
657 \r
658             // Draw coordinate axes\r
659 \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
664 \r
665             // Draw points\r
666 \r
667                 for(int i=0; i<pts.GetNumPoints(); i++)\r
668                 {\r
669                         if (i == ptHighlight)\r
670                         {\r
671                                 SelectObject(hdc, hRedPen1);\r
672 \r
673                                 if (pts.GetOnCurve(i))\r
674                                 {\r
675                                         DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);\r
676                                         DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);\r
677                                 }\r
678                                 else\r
679                                 {\r
680                                         DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);\r
681                                         DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);\r
682                                 }\r
683                         }\r
684                         else if ((i == ptHighlight || i == ptNextHighlight) && currentTool == TOOLAddPt)\r
685                         {\r
686                                 SelectObject(hdc, hGreenPen1);\r
687 \r
688                                 if (pts.GetOnCurve(i))\r
689                                 {\r
690                                         DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 7);\r
691                                         DrawSquareDotN(hdc, pts.GetX(i), pts.GetY(i), 9);\r
692                                 }\r
693                                 else\r
694                                 {\r
695                                         DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 7);\r
696                                         DrawRoundDotN(hdc, pts.GetX(i), pts.GetY(i), 9);\r
697                                 }\r
698                         }\r
699                         else\r
700                         {\r
701                                 SelectObject(hdc, hBlackPen1);\r
702 \r
703                                 if (pts.GetOnCurve(i))\r
704                                         DrawSquareDot(hdc, pts.GetX(i), pts.GetY(i));\r
705                                 else\r
706                                         DrawRoundDot(hdc, pts.GetX(i), pts.GetY(i));\r
707                         }\r
708 \r
709                         if (currentTool == TOOLDelPt && i == ptHighlight)\r
710                         {\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
718                         }\r
719                 }\r
720 \r
721                 SelectObject(hdc, hBlackPen1);\r
722 \r
723                 // Draw curve formed by points\r
724 \r
725                 for(int poly=0; poly<pts.GetNumPolys(); poly++)\r
726                 {\r
727                         if (pts.GetNumPoints(poly) > 2)\r
728                         {\r
729                                 // Initial move...\r
730                                 // If it's not on curve, then move to it, otherwise move to last point...\r
731         \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
734                                 else\r
735                                         MoveToEx(hdc, pts.GetX(poly, 0), pts.GetY(poly, 0), NULL);\r
736         \r
737                                 for(int i=0; i<pts.GetNumPoints(poly); i++)\r
738                                 {\r
739                                         if (pts.GetOnCurve(poly, i))\r
740                                                 LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));\r
741                                         else\r
742                                         {\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
746         \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
750         \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
754         \r
755                                                 Bezier(hdc, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));\r
756         \r
757                                                 if (pts.GetOnCurve(poly, next))\r
758                                                         i++;                                    // Following point is on curve, so move past it\r
759                                         }\r
760                                 }\r
761                         }\r
762                 }\r
763 \r
764                 SelectObject(hdc, oldPen);                              // Restore the stuff we disrupted...\r
765                 SelectObject(hdc, oldBrush);\r
766                 EndPaint(hWnd, &ps);\r
767                 break;\r
768         }\r
769         case WM_SIZE:\r
770 \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
774 \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
779                 break;\r
780 \r
781         case WM_RBUTTONDOWN:\r
782 \r
783                 GetCursorPos(&pt);\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
788                 break;\r
789 \r
790         case WM_LBUTTONDOWN:\r
791 \r
792                 mouseDown = true;\r
793 \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
797                 {\r
798                         if (pts.GetNumPoints() > 0)\r
799                         {\r
800 //Do we really need to put a cap on this???\r
801 //Maybe...\r
802 //                              if (pts.GetNumPoints() < 16)\r
803 //                              {\r
804                                 pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;\r
805                                 hdc = GetDC(hWnd);\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
811 //                              }\r
812                         }\r
813                 }\r
814                 else if (currentTool == TOOLAddPoly)    // "Add Poly" tool\r
815                 {\r
816 #ifdef DEBUGFOO\r
817 wsprintf(strBuf, "Adding point... # polys: %u, # points: %u", pts.GetNumPolys(), pts.GetNumPoints());\r
818 WriteLogMsg(strBuf);\r
819 #endif\r
820                         if (polyFirstPoint)\r
821                         {\r
822                                 polyFirstPoint = false;\r
823                                 pts.AddNewPolyAtEnd();\r
824                         }\r
825 \r
826 //Do we really need to put a cap on this???\r
827 //Maybe...\r
828 //                      if (pts.GetNumPoints() < 16)\r
829 //                      {\r
830                         pt.x = lParam & 0xFFFF, pt.y = lParam >> 16;\r
831                         hdc = GetDC(hWnd);\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
838 //                      }\r
839 #ifdef DEBUGFOO\r
840 wsprintf(strBuf, " --> [# polys: %u, # points: %u]\xD\xA", pts.GetNumPolys(), pts.GetNumPoints());\r
841 WriteLogMsg(strBuf);\r
842 #endif\r
843                 }\r
844                 else if (currentTool == TOOLSelect || currentTool == TOOLPolySelect)\r
845                 {\r
846                         if (pts.GetNumPoints() > 0)\r
847                         {\r
848                                 pt.x = pts.GetX(ptHighlight), pt.y = pts.GetY(ptHighlight);\r
849                                 hdc = GetDC(hWnd);\r
850                                 LPtoDP(hdc, &pt, 1);\r
851                                 ClientToScreen(hWnd, &pt);\r
852                                 SetCursorPos(pt.x, pt.y);\r
853                                 ReleaseDC(hWnd, hdc);\r
854 \r
855                                 if (wParam & (MK_SHIFT | MK_CONTROL))\r
856                                         pts.SetOnCurve(ptHighlight, !pts.GetOnCurve(ptHighlight));\r
857                         }\r
858                 }\r
859                 else if (currentTool == TOOLDelPt)\r
860                 {\r
861                         if (pts.GetNumPoints() > 0)\r
862 //Or could use:\r
863 //                      if (ptHighlight != -1)\r
864                         {\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
869                         }\r
870                 }\r
871 \r
872                 break;\r
873 \r
874         case WM_LBUTTONUP:\r
875 \r
876                 mouseDown = false;\r
877 \r
878                 if (currentTool == TOOLScroll || currentTool == TOOLZoom)\r
879                         ReleaseCapture();\r
880 \r
881                 break;\r
882 \r
883         case WM_MOUSEMOVE:\r
884 \r
885                 SetCursor(hCur[currentTool]);\r
886 \r
887             // Extract current point from lParam/calc offset from previous point\r
888 \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
892 \r
893                 if (mouseDown)\r
894                 {\r
895                         if (currentTool == TOOLScroll)\r
896                         {\r
897                                 // NOTE: OffsetViewportOrg operates in DEVICE UNITS...\r
898 \r
899                                 hdc = GetDC(hWnd);\r
900                                 OffsetViewportOrgEx(hdc, ptOffset.x, ptOffset.y, NULL);\r
901                                 ReleaseDC(hWnd, hdc);\r
902 \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
905 // Above: DONE\r
906 // Then multiply it by the scaling factor. Whee!\r
907 \r
908                                 InvalidateRect(hWnd, NULL, TRUE);\r
909 //                              SendMessage(hWnd, WM_PAINT, NULL, NULL);\r
910                         }\r
911                         else if (currentTool == TOOLAddPt || currentTool == TOOLAddPoly || currentTool == TOOLSelect)\r
912                         {\r
913                                 if (currentTool != TOOLAddPt || pts.GetNumPoints() > 0)//yecch.\r
914                                 {\r
915                                         POINT pt2;\r
916                                         pt2.x = pt.x, pt2.y = pt.y;\r
917                                         // Should also set onCurve here as well, depending on keystate\r
918 //Or should we?\r
919                                         hdc = GetDC(hWnd);\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
924                                 }\r
925                         }\r
926                         else if (currentTool == TOOLPolySelect)\r
927                         {\r
928                                 if (pts.GetNumPoints() > 0)\r
929                                 {\r
930                                         POINT pt2;\r
931                                         pt2.x = pt.x, pt2.y = pt.y;\r
932                                         // Should also set onCurve here as well, depending on keystate\r
933 //Or should we?\r
934                                         hdc = GetDC(hWnd);\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
939                                 }\r
940                         }\r
941                 }\r
942                 else\r
943                 {\r
944                         if (currentTool == TOOLSelect || currentTool == TOOLDelPt || currentTool == TOOLAddPt\r
945                                 || currentTool == TOOLPolySelect)// || currentTool == TOOLAddPoly)\r
946                         {\r
947                                 POINT pt2;\r
948                                 pt2.x = pt.x, pt2.y = pt.y;\r
949                                 hdc = GetDC(hWnd);\r
950                                 DPtoLP(hdc, &pt2, 1);\r
951                                 ReleaseDC(hWnd, hdc);\r
952 \r
953                                 double closest = 1.0e+99;\r
954 \r
955                                 for(int i=0; i<pts.GetNumPoints(); i++)\r
956                                 {\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
959 \r
960                                         if (dist < closest)\r
961                                                 closest = dist, ptHighlight = i;\r
962                                 }\r
963 \r
964                                 if (ptHighlight != oldPtHighlight)\r
965                                 {\r
966                                         oldPtHighlight = ptHighlight;\r
967                                         InvalidateRect(hWnd, NULL, TRUE);\r
968                                 }\r
969 \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
976 \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
979 \r
980                                 if (pts.GetNumPoints() > 1 && currentTool == TOOLAddPt)\r
981                                 {\r
982                                         double smallest = 1.0e+99;\r
983 \r
984                                         for(int i=0; i<pts.GetNumPoints(); i++)\r
985                                         {\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
988 \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
996 \r
997                                                 if (pp < 0.0)\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
1003 \r
1004 //The answer to the above looks like it might be found here:\r
1005 //\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
1012 //\r
1013 //sx sy 1\r
1014 //ex ey 1\r
1015 //px py 1\r
1016 //\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
1019 //\r
1020 //0         0         0\r
1021 //(ex - sx) (ey - sy) 0\r
1022 //(px - sx) (py - sy) 0\r
1023 //\r
1024 //which greatly simplifies the calculation of the determinant.\r
1025 \r
1026                                                 if (dist < smallest)\r
1027                                                         smallest = dist, ptNextHighlight = pts.GetNext(i), ptHighlight = i;\r
1028                                         }\r
1029 \r
1030                                         if (ptNextHighlight != oldPtNextHighlight)\r
1031                                         {\r
1032                                                 oldPtNextHighlight = ptNextHighlight;\r
1033                                                 InvalidateRect(hWnd, NULL, TRUE);\r
1034                                         }\r
1035                                 }\r
1036                         }\r
1037                 }\r
1038 \r
1039                 ptPrevious.x = pt.x, ptPrevious.y = pt.y;\r
1040 \r
1041                 break;\r
1042 \r
1043         case WM_NOTIFY:\r
1044 \r
1045                 if (((NMHDR *)lParam)->code == TTN_NEEDTEXT)\r
1046                 {\r
1047                         LoadString(hInst, ((TOOLTIPTEXT *)lParam)->hdr.idFrom + 0x80, toolTipTxt, 16);\r
1048                         ((TOOLTIPTEXT *)lParam)->lpszText = toolTipTxt;\r
1049                 }\r
1050 \r
1051                 break;\r
1052 \r
1053         case WM_MENUSELECT:\r
1054         {\r
1055                 statusBarTxt[0] = 0;                                    // Clear status bar text\r
1056                 uint16 flags = wParam >> 16;                    // Extract flags\r
1057 \r
1058                 if (!(flags & MFT_SEPARATOR))\r
1059                 {\r
1060                         uint16 id = wParam & 0xFFFF;\r
1061 \r
1062                         if (flags & MF_POPUP)\r
1063                         {\r
1064                                 if (flags & MF_SYSMENU)\r
1065                                         id = IDS_SYSMENU;\r
1066                                 else\r
1067                                         id = IDM_FILEMENU + wParam;\r
1068                         }\r
1069 \r
1070                         LoadString(hInst, id, statusBarTxt, 64);\r
1071                 }\r
1072 \r
1073                 SendMessage(hStatusBar, SB_SETTEXT, 0 + SBT_NOBORDERS, (LPARAM)statusBarTxt);\r
1074                 break;\r
1075         }\r
1076         case WM_COMMAND:\r
1077         {\r
1078                 uint16 cmd = wParam & 0xFFFF;\r
1079 \r
1080                 if (cmd == IDM_NEW)\r
1081                 {\r
1082 //                    call   CmdIDM_NEW\r
1083                 }\r
1084                 else if (cmd == IDM_OPEN)\r
1085                 {\r
1086 //                    call   SaveChanges\r
1087 //                    .IF (eax)\r
1088 //                      movmov ofn.hwndOwner, eax, hMainWnd\r
1089 //                      mov    ofn.Flags, OFN_PATHMUSTEXIST + OFN_FILEMUSTEXIST\r
1090 //                      invoke GetOpenFileName, ADDR ofn\r
1091 //                      .IF (eax)\r
1092 ////////\r
1093 //jmp @F\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
1097 //@@:\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
1101 //\r
1102 //////\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
1108 //                        jmp    Return\r
1109 //                        //��������������������������������������\r
1110 //OpenError:              invoke GetLastError\r
1111 //                        // <<< FILE OPEN ERROR CODE HERE >>>\r
1112 //                      .ENDIF\r
1113 //                      zero   eax      // return FALSE\r
1114 //                    .ENDIF\r
1115                 }\r
1116                 else if (cmd == IDM_SAVEAS)\r
1117                 {\r
1118 //                    and    fFileStatus, NOT NAMEDbit\r
1119 //                    call   CmdIDM_SAVE\r
1120                 }\r
1121                 else if (cmd == IDM_SAVE)\r
1122                 {\r
1123 //                    call   CmdIDM_SAVE\r
1124                 }\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
1130                 {\r
1131                         ShowWindow(hCharWnd, (IsWindowVisible(hCharWnd) ? SW_HIDE : SW_SHOWNOACTIVATE));\r
1132 \r
1133 #ifdef DEBUGFOO\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
1138 #endif\r
1139                 }\r
1140                 else\r
1141                         return DefWindowProc(hWnd, msgID, wParam, lParam);\r
1142 \r
1143                 break;\r
1144         }\r
1145         default:\r
1146                 return DefWindowProc(hWnd, msgID, wParam, lParam);\r
1147         }\r
1148 \r
1149         return 0;\r
1150 }\r
1151 \r
1152 //\r
1153 // Initialize TTF data\r
1154 //\r
1155 void CreateNewDoc(void)\r
1156 {\r
1157 }\r
1158 \r
1159 //\r
1160 // Save changes to document before quitting\r
1161 //\r
1162 bool SaveChanges(void)\r
1163 {\r
1164         return true;\r
1165 }\r
1166 \r
1167 \r
1168 \r
1169 //\r
1170 // ABOUT Dialog WndProc\r
1171 //\r
1172 BOOL CALLBACK AboutProc(HWND hDlg, UINT msgID, WPARAM wParam, LPARAM lParam)\r
1173 {\r
1174         switch (msgID)\r
1175         {\r
1176         case WM_INITDIALOG:\r
1177 \r
1178                 MiscCenterWnd(hDlg, hMainWnd);\r
1179                 break;\r
1180 \r
1181         case WM_COMMAND:\r
1182 \r
1183                 if (wParam == IDOK || wParam == IDCANCEL)\r
1184                         EndDialog(hDlg, TRUE);\r
1185 \r
1186                 break;\r
1187 \r
1188         default:\r
1189                 return FALSE;\r
1190         }\r
1191 \r
1192         return TRUE;\r
1193 }\r
1194 \r
1195 //\r
1196 // Character Window WndProc\r
1197 //\r
1198 WndProcCW       PROC  STDCALL, hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM\r
1199 \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
1212                 .ELSE\r
1213 DefProc:          invoke DefWindowProc, hWnd, uMsg, wParam, lParam\r
1214                 .ENDIF\r
1215                 ret\r
1216 \r
1217 WndProcCW       ENDP\r
1218 \r
1219 //\r
1220 // Character Window WndProc\r
1221 //\r
1222 LRESULT CALLBACK WndProcCW(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)\r
1223 {\r
1224         switch (msgID)\r
1225         {\r
1226         default:\r
1227                 return DefWindowProc(hWnd, msgID, wParam, lParam);\r
1228         }\r
1229 \r
1230         return 0;\r
1231 }\r
1232 \r
1233 \r
1234 // Function prototypes\r
1235 \r
1236 int32 FindSelectedTool(void);\r
1237 \r
1238 //\r
1239 // Tool Palette WndProc\r
1240 //\r
1241 LRESULT CALLBACK WndProcTP(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)\r
1242 {\r
1243         PAINTSTRUCT ps;\r
1244         HDC hdc;\r
1245         POINT pt;\r
1246         static uint32 prevTool = -1;\r
1247 \r
1248         switch (msgID)\r
1249         {\r
1250         case WM_PAINT:\r
1251         {\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
1256                 DeleteDC(newDC);\r
1257 \r
1258 // This is crappy. Find some way to tighten this up!\r
1259                 int32 tool = FindSelectedTool();\r
1260 \r
1261                 if (tool != -1)\r
1262                 {\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
1268                         DeleteDC(newDC);\r
1269                 }\r
1270 \r
1271                 EndPaint(hWnd, &ps);\r
1272                 break;\r
1273         }\r
1274         case WM_MOUSEMOVE:\r
1275         {\r
1276                 int32 tool = FindSelectedTool();\r
1277 \r
1278                 if (tool != prevTool)\r
1279                 {\r
1280                         prevTool = tool;\r
1281                         InvalidateRect(hWnd, NULL, FALSE);\r
1282                 }\r
1283 \r
1284                 break;\r
1285         }\r
1286         case WM_RBUTTONUP:\r
1287         {\r
1288                 int32 tool = FindSelectedTool(), oldTool = currentTool;\r
1289 \r
1290                 if (tool != -1)\r
1291                         currentTool = tool;\r
1292 \r
1293                 if (currentTool != TOOLSelect && currentTool != TOOLDelPt && currentTool != TOOLAddPt\r
1294                         && currentTool != TOOLPolySelect)\r
1295                         ptHighlight = -1;\r
1296 \r
1297                 if (currentTool != oldTool)\r
1298                         InvalidateRect(hMainWnd, NULL, TRUE);\r
1299 \r
1300                 if (currentTool == TOOLAddPoly)\r
1301 #ifdef DEBUGFOO\r
1302 {\r
1303 #endif\r
1304                         polyFirstPoint = true;\r
1305 #ifdef DEBUGFOO\r
1306 wsprintf(strBuf, "--> Selected poly tool, polyFirstPoint is %s\n", polyFirstPoint ? "true" : "false");\r
1307 WriteLogMsg(strBuf);\r
1308 }\r
1309 #endif\r
1310 \r
1311                 ReleaseCapture();\r
1312                 ShowWindow(hToolPalWnd, SW_HIDE);\r
1313                 SetFocus(hMainWnd);                                             // Make sure the main wnd keeps focus!\r
1314 \r
1315                 break;\r
1316         }\r
1317         default:\r
1318                 return DefWindowProc(hWnd, msgID, wParam, lParam);\r
1319         }\r
1320 \r
1321         return 0;\r
1322 }\r
1323 \r
1324 //\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
1327 //\r
1328 int32 FindSelectedTool(void)\r
1329 {\r
1330         POINT pt;\r
1331                 \r
1332         GetCursorPos(&pt);\r
1333         ScreenToClient(hToolPalWnd, &pt);\r
1334 \r
1335         uint32 x = (uint32)pt.x / sizeStamp.x, y = (uint32)pt.y / sizeStamp.y, tool = -1;\r
1336 \r
1337         if (x < 4 && y < 2)\r
1338 //      {\r
1339                 tool = (y * 4) + x;\r
1340 \r
1341 //              if (tool == 7)\r
1342 //                      tool = -1;                                                      // 7 has no tool...\r
1343 //      }\r
1344 \r
1345         return tool;\r
1346 }\r
1347 \r
1348 \r
1349 //\r
1350 // Misc center window\r
1351 //\r
1352 void MiscCenterWnd(HWND hChild, HWND hParent)\r
1353 {\r
1354         RECT parent, child;\r
1355 \r
1356         if (!GetWindowRect(hParent, &parent) || !GetWindowRect(hChild, &child))\r
1357                 return;\r
1358 \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
1361 \r
1362         if (x < 0)\r
1363                 x = 0;\r
1364         else if (x > GetSystemMetrics(SM_CXFULLSCREEN) - (child.right - child.left))\r
1365                 x = GetSystemMetrics(SM_CXFULLSCREEN) - (child.right - child.left);\r
1366 \r
1367         if (y < 0)\r
1368                 y = 0;\r
1369         else if (y > GetSystemMetrics(SM_CYFULLSCREEN) - (child.bottom - child.top))\r
1370                 y = GetSystemMetrics(SM_CYFULLSCREEN) - (child.bottom - child.top);\r
1371 \r
1372         SetWindowPos(hChild, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);\r
1373 }\r
1374 \r
1375 //\r
1376 // Allow only one instance\r
1377 //\r
1378 bool OnlyOneInstance(void)\r
1379 {\r
1380         HWND window = FindWindow(className, NULL);\r
1381 \r
1382         if (window == NULL)\r
1383                 return true;\r
1384 \r
1385         ShowWindow(window, SW_SHOWNORMAL);\r
1386         SetWindowPos(window, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);\r
1387 \r
1388         return false;\r
1389 }\r
1390 \r
1391 //\r
1392 // Load/Allocate all resources\r
1393 //\r
1394 bool LoadResources(void)\r
1395 {\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
1404 \r
1405         BITMAP bm;\r
1406 \r
1407         hBMToolPal1 = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_TOOLPAL1));\r
1408         GetObject(hBMToolPal1, sizeof(bm), &bm);\r
1409 \r
1410         // Set up sizes\r
1411 \r
1412         sizeTPBM.x = bm.bmWidth, sizeTPBM.y = bm.bmHeight;\r
1413         sizeStamp.x = bm.bmWidth / 4, sizeStamp.y = bm.bmHeight / 2;\r
1414 \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
1419 \r
1420         LOGBRUSH lb = { BS_NULL, 0, 0 };\r
1421 \r
1422         hNullBrush = CreateBrushIndirect(&lb);\r
1423 \r
1424         return true;\r
1425 }\r
1426 \r
1427 //\r
1428 // Deallocate all resources\r
1429 //\r
1430 void DeallocateResources(void)\r
1431 {\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
1438 }\r
1439 \r
1440 //\r
1441 // Save all application specific data, so we can pick up where we last left off...\r
1442 //\r
1443 void SaveAppState(void)\r
1444 {\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
1451 \r
1452         SetINIInt("Main", "vpx", ptVPM.x);\r
1453         SetINIInt("Main", "vpy", ptVPM.y);\r
1454 \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
1461 \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
1465 }\r
1466 \r
1467 //\r
1468 // Restore all application specific data previously saved\r
1469 //\r
1470 bool RestoreAppState(void)\r
1471 {\r
1472         InitINIFile();\r
1473 \r
1474         WINDOWPLACEMENT wp;\r
1475         wp.length = sizeof(WINDOWPLACEMENT);\r
1476         GetWindowPlacement(hMainWnd, &wp);\r
1477 \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
1484 \r
1485         SetWindowPlacement(hMainWnd, &wp);\r
1486 \r
1487         HDC hdc;\r
1488         POINT pt;\r
1489         hdc = GetDC(hMainWnd);\r
1490         GetViewportOrgEx(hdc, &pt);\r
1491 \r
1492         pt.x = GetINIInt("Main", "vpx", pt.x);\r
1493         pt.y = GetINIInt("Main", "vpy", pt.y);\r
1494 \r
1495         SetViewportOrgEx(hdc, pt.x, pt.y, NULL);\r
1496         ReleaseDC(hMainWnd, hdc);\r
1497 \r
1498         GetWindowPlacement(hCharWnd, &wp);\r
1499 \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
1506 \r
1507         SetWindowPlacement(hCharWnd, &wp);\r
1508 \r
1509         if (wp.showCmd == SW_HIDE)\r
1510                 SendMessage(hToolBar, TB_SETSTATE, ID_TBCHARWIN, MAKELONG(TBSTATE_ENABLED, 0));\r
1511 \r
1512 //  CString lastFile = theApplicationObject.GetProfileString(version, "currentFile", "");\r
1513 //  int lastChar = theApplicationObject.GetProfileInt(version, "currentChar", 0);\r
1514 //  if (lastFile.GetLength())\r
1515 //  {\r
1516 //    // Attempt to restore the last session by open the last file used, etc...\r
1517 //    if (!pDoc->m_myFont.Load(lastFile))\r
1518 //    {\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
1521 //    }\r
1522 //    else\r
1523 //    {\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
1527 //\r
1528 //      BYTE name[512];\r
1529 //      pDoc->m_myFont.GetCharName(lastChar, name);\r
1530 //      m_wndOwned.SetWindowText((char *)name);\r
1531 //    }\r
1532 //  }\r
1533 \r
1534         return true;\r
1535 }\r
1536 \r
1537 //\r
1538 // Initialization\r
1539 //\r
1540 bool Initialization(void)\r
1541 {\r
1542         WNDCLASSEX wcex;\r
1543 \r
1544         if (!LoadResources())\r
1545                 return false;\r
1546 \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
1557 \r
1558         if (!RegisterClassEx(&wcex))\r
1559                 return false;\r
1560 \r
1561         hMainWnd = CreateWindowEx(NULL, className, className, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,\r
1562                 0, 0, 0x1A0, 0x180, NULL, NULL, hInst, NULL);\r
1563 \r
1564         if (!hMainWnd)\r
1565                 return false;\r
1566 \r
1567         ShowWindow(hMainWnd, nCmdShow);\r
1568         UpdateWindow(hMainWnd);\r
1569 \r
1570         // Character window creation\r
1571 \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
1576 \r
1577         if (!RegisterClassEx(&wcex))\r
1578                 return false;\r
1579 \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
1583 \r
1584         if (!hCharWnd)\r
1585                 return false;\r
1586 \r
1587         ShowWindow(hCharWnd, SW_SHOWNORMAL);\r
1588         UpdateWindow(hCharWnd);\r
1589         SetFocus(hMainWnd);                                                     // Make sure main wnd has focus!\r
1590 \r
1591         // Tool palette window creation\r
1592 \r
1593         wcex.lpfnWndProc = WndProcTP;\r
1594         wcex.lpszClassName = CNToolPal;\r
1595 \r
1596         if (!RegisterClassEx(&wcex))\r
1597                 return false;\r
1598 \r
1599         hToolPalWnd = CreateWindowEx(WS_EX_WINDOWEDGE, CNToolPal, NULL, WS_POPUP,\r
1600                 0, 0, sizeTPBM.x, sizeTPBM.y, hMainWnd, NULL, hInst, NULL);\r
1601 \r
1602         if (!hToolPalWnd)\r
1603                 return false;\r
1604 \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
1609 \r
1610         RestoreAppState();                                                      // Restore app related stuff\r
1611 \r
1612         return true;\r
1613 }\r
1614 \r
1615 #endif\r