Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

715 lines
13 KiB

  1. #include "gdiptest.h"
  2. #include <commctrl.h>
  3. extern const TCHAR* formatExtList =
  4. _T("CPP files\0*.cpp\0"
  5. "Java files\0*.Java\0"
  6. "VML files\0*.vml\0"
  7. "All files\0*.*\0");
  8. extern const TCHAR* defaultFormatExt = _T("cpp");
  9. //*******************************************************************
  10. //
  11. // TestDraw
  12. //
  13. //
  14. //
  15. //*******************************************************************
  16. VOID TestDraw::AddPoint(HWND hwnd, Point pt)
  17. {
  18. if (!curShape)
  19. {
  20. // no current shape, create one of appropriate type
  21. curShape = TestShape::CreateNewShape(shapeType);
  22. curShape->Initialize(NULL);
  23. // save copy of brush & pen in shape
  24. curShape->SetBrush(curBrush->Clone());
  25. curShape->SetPen(curPen->Clone());
  26. }
  27. else if (curShape->IsComplete())
  28. {
  29. TestShape *lastShape = curShape;
  30. // add current shape to shape stack
  31. shapeStack.Push(curShape);
  32. // create blank shape of this type
  33. curShape = TestShape::CreateNewShape(shapeType);
  34. curShape->Initialize(lastShape);
  35. // save copy of brush & pen in shape
  36. curShape->SetBrush(curBrush->Clone());
  37. curShape->SetPen(curPen->Clone());
  38. }
  39. curShape->AddPoint(hwnd, pt);
  40. }
  41. BOOL TestDraw::DoneShape(HWND hwnd)
  42. {
  43. // we are at regular end point regardless
  44. if (!curShape->IsComplete())
  45. {
  46. // if point can't be treated as an 'end point' then treat
  47. // it as a regular control point
  48. curShape->DoneShape(hwnd);
  49. }
  50. return curShape->IsComplete();
  51. }
  52. BOOL TestDraw::EndPoint(HWND hwnd, Point pt)
  53. {
  54. AddPoint(hwnd, pt);
  55. return DoneShape(hwnd);
  56. }
  57. BOOL TestDraw::RemovePoint(HWND hwnd)
  58. {
  59. if (!curShape || (curShape && !curShape->RemovePoint(hwnd)))
  60. {
  61. if (shapeStack.GetCount() > 0)
  62. {
  63. // the shape is empty, delete it
  64. delete curShape;
  65. curShape = shapeStack.Pop();
  66. // !! reset menu option for current shape
  67. UpdateStatus();
  68. return curShape->RemovePoint(hwnd);
  69. }
  70. }
  71. return FALSE;
  72. }
  73. VOID TestDraw::Draw(HWND hwnd)
  74. {
  75. PAINTSTRUCT ps;
  76. RECT rt;
  77. HDC hdc;
  78. // !!! when CopyPixels work, cache the graphics up to the
  79. // last shape. We blit that, then the new shape begins.
  80. // posts a WM_ERASEBKGND message
  81. hdc = BeginPaint(hwnd, &ps);
  82. /////////////////////////////////////////////////////
  83. // GDI+ code BEGINS
  84. ////////////////////////////////////////////////////
  85. Graphics *g = new Graphics(hwnd);
  86. GetClientRect(hwnd, &rt);
  87. ERectangle rect(rt.left,
  88. rt.top,
  89. rt.right-rt.left,
  90. rt.bottom-rt.top
  91. -18); // for status window
  92. // set appropriate clip region
  93. if (useClip)
  94. {
  95. Region region(rect);
  96. region.And(clipRegion);
  97. g->SetClip(&region);
  98. }
  99. else
  100. {
  101. g->SetClip(rect);
  102. }
  103. g->SetRenderingHint(antiAlias);
  104. g->SetWorldTransform(worldMatrix);
  105. // !!! iterate through stack of shapes.
  106. // because of alpha we can't just redraw the last shape,
  107. // otherwise we will blend alpha with ourself and whatever
  108. // else is under us. clear the rectangle and redraw all.
  109. if (redrawAll)
  110. {
  111. INT count = shapeStack.GetCount();
  112. INT pos;
  113. for (pos=0; pos<count; pos++)
  114. {
  115. TestShape *shape = shapeStack[pos];
  116. if (!shape->GetDisabled())
  117. {
  118. // shape must be complete
  119. ASSERT(shape->IsComplete());
  120. shape->DrawShape(g);
  121. if (keepControlPoints)
  122. shape->DrawPoints(g);
  123. }
  124. }
  125. }
  126. if (curShape)
  127. {
  128. if (curShape->IsComplete())
  129. {
  130. curShape->DrawShape(g);
  131. if (keepControlPoints)
  132. goto DrawCurShape;
  133. }
  134. else
  135. {
  136. DrawCurShape:
  137. g->SetClip(rect);
  138. curShape->DrawPoints(g);
  139. }
  140. }
  141. delete g;
  142. ////////////////////////////////////////////////////
  143. // GDI+ code ENDS.
  144. ////////////////////////////////////////////////////
  145. UpdateStatus();
  146. EndPaint(hwnd, &ps);
  147. }
  148. VOID TestDraw::SetClipRegion(HWND hwnd)
  149. {
  150. if (!curShape)
  151. {
  152. WarningBox(_T("Create at least one shape first!"));
  153. return;
  154. }
  155. // we want to allow the very last shape to clip
  156. // so we add it in here to the stack list.
  157. if (curShape->IsComplete())
  158. {
  159. TestShape *lastShape = curShape;
  160. // add current shape to shape stack
  161. shapeStack.Push(curShape);
  162. // create blank shape of this type
  163. curShape = TestShape::CreateNewShape(shapeType);
  164. curShape->Initialize(lastShape);
  165. // save copy of brush & pen in shape
  166. curShape->SetBrush(curBrush->Clone());
  167. curShape->SetPen(curPen->Clone());
  168. }
  169. // notice we repeatly initialize even though we only
  170. // created this once...
  171. clipShapeRegion->Initialize(&shapeStack, curShape, useClip);
  172. if (clipShapeRegion->ChangeSettings(hwnd))
  173. {
  174. delete clipRegion;
  175. clipRegion = clipShapeRegion->GetClipRegion();
  176. useClip = clipShapeRegion->GetClipBool();
  177. SetMenuCheckCmd(hwnd,
  178. MenuOtherPosition,
  179. IDM_USECLIP,
  180. useClip);
  181. // force redraw of all stacked shapes w/new clip region
  182. InvalidateRect(hwnd, NULL, TRUE);
  183. UpdateWindow(hwnd);
  184. }
  185. UpdateStatus();
  186. }
  187. VOID TestDraw::RememberPoint(Point pt)
  188. {
  189. remPoint = pt;
  190. }
  191. VOID TestDraw::MoveControlPoint(Point pt)
  192. {
  193. // find shape that hits control point.
  194. Point *hitpt = NULL;
  195. if (curShape)
  196. {
  197. if (curShape->MoveControlPoint(remPoint, pt))
  198. return;
  199. INT count = shapeStack.GetCount();
  200. INT pos;
  201. for (pos = count-1; pos>=0; pos--)
  202. {
  203. TestShape* shape = shapeStack[pos];
  204. if (shape->MoveControlPoint(remPoint, pt))
  205. return;
  206. }
  207. }
  208. // nothing moved
  209. WarningBeep();
  210. }
  211. VOID TestDraw::ChangeBrush(HWND hwnd, INT type)
  212. {
  213. TestBrush *newBrush = NULL;
  214. if (curBrush && (type == curBrush->GetType()))
  215. {
  216. // same brush type
  217. // !!! change brush color in middle of drawing?
  218. TestBrush* newBrush = curBrush->Clone();
  219. if (newBrush->ChangeSettings(hwnd))
  220. {
  221. delete curBrush;
  222. curBrush = newBrush;
  223. }
  224. }
  225. else
  226. {
  227. // new brush type
  228. SetMenuCheckPos(hwnd,
  229. MenuBrushPosition,
  230. curBrush->GetType(),
  231. FALSE);
  232. newBrush = TestBrush::CreateNewBrush(type);
  233. if (!newBrush)
  234. {
  235. return;
  236. }
  237. newBrush->Initialize();
  238. // keep or discard changed brush settings
  239. if (newBrush->ChangeSettings(hwnd))
  240. {
  241. delete curBrush;
  242. curBrush = newBrush;
  243. // !!! change brush color in middle of drawing
  244. }
  245. else
  246. {
  247. delete newBrush;
  248. }
  249. SetMenuCheckPos(hwnd,
  250. MenuBrushPosition,
  251. curBrush->GetType(),
  252. TRUE);
  253. }
  254. if (curShape && curShape->GetCount() == 0)
  255. {
  256. curShape->SetBrush(curBrush->Clone());
  257. }
  258. UpdateStatus();
  259. }
  260. VOID TestDraw::ChangePen(HWND hwnd)
  261. {
  262. TestPen *newPen = NULL;
  263. if (curPen)
  264. {
  265. TestPen *newPen = curPen->Clone();
  266. if (newPen->ChangeSettings(hwnd))
  267. {
  268. delete curPen;
  269. curPen = newPen;
  270. }
  271. }
  272. else
  273. {
  274. newPen = new TestPen();
  275. newPen->Initialize();
  276. // !!! change pen in middle of drawing?
  277. if (newPen->ChangeSettings(hwnd))
  278. {
  279. delete curPen;
  280. curPen = newPen;
  281. }
  282. else
  283. {
  284. delete newPen;
  285. }
  286. }
  287. if (curShape && curShape->GetCount() == 0)
  288. {
  289. curShape->SetPen(curPen->Clone());
  290. }
  291. UpdateStatus();
  292. }
  293. VOID TestDraw::ChangeShape(HWND hwnd, INT type)
  294. {
  295. TestShape *shape;
  296. shape = TestShape::CreateNewShape(type);
  297. shape->Initialize(curShape);
  298. // save copy of brush & pen in shape
  299. shape->SetBrush(curBrush->Clone());
  300. shape->SetPen(curPen->Clone());
  301. if (shape->ChangeSettings(hwnd))
  302. {
  303. SetMenuCheckPos(hwnd,
  304. MenuShapePosition,
  305. shapeType,
  306. FALSE);
  307. shapeType = type;
  308. // if shape can be completed, complete it, otherwise
  309. // destroy the shape.
  310. if (curShape)
  311. {
  312. if (!curShape->IsComplete())
  313. {
  314. curShape->DoneShape(hwnd);
  315. }
  316. if (!curShape->IsComplete() || curShape->IsEmpty())
  317. {
  318. delete curShape;
  319. curShape = NULL;
  320. }
  321. }
  322. if (curShape)
  323. shapeStack.Add(curShape);
  324. curShape = shape;
  325. SetMenuCheckPos(hwnd,
  326. MenuShapePosition,
  327. shapeType,
  328. TRUE);
  329. // removing last incomplete shape, redraw the window.
  330. // OR completed shape, redraw the window
  331. InvalidateRect(hwnd, NULL, TRUE);
  332. UpdateWindow(hwnd);
  333. }
  334. else
  335. {
  336. delete shape;
  337. }
  338. UpdateStatus();
  339. }
  340. VOID TestDraw :: UpdateStatus(HWND hwnd)
  341. {
  342. if (hwnd)
  343. {
  344. if (hwndStatus)
  345. {
  346. // destroy previous window
  347. DestroyWindow(hwndStatus);
  348. hwndStatus = NULL;
  349. }
  350. // we only want to destroy this window
  351. if (hwnd == (HWND)-1)
  352. return;
  353. hwndStatus = CreateStatusWindow(WS_CHILD | WS_VISIBLE,
  354. _T(""),
  355. hwnd,
  356. 0); // never used?
  357. }
  358. if (hwndStatus)
  359. {
  360. TCHAR str[MAX_PATH];
  361. _stprintf(&str[0],"[Last shape: %s] [Brush: %s] [Number of Points: %d]",
  362. shapeList[inverseShapeValue[GetShapeType()]],
  363. brushList[inverseBrushValue[GetBrushType()]],
  364. curShape ? curShape->GetCount() : 0);
  365. // !! for some reason, DrawStatusText didn't work here...
  366. // wouldn't it just call SendMessage() ?!?
  367. SendMessage(hwndStatus, SB_SETTEXT, 0 | 0, (LPARAM)(LPTSTR)str);
  368. }
  369. }
  370. VOID TestDraw :: SaveAsFile(HWND hWnd)
  371. {
  372. static TCHAR fname[MAX_PATH] = _T("");
  373. OPENFILENAME ofn =
  374. {
  375. sizeof(OPENFILENAME),
  376. hWnd,
  377. 0,
  378. formatExtList,
  379. NULL,
  380. 0,
  381. 1,
  382. &fname[0],
  383. MAX_PATH-1,
  384. NULL,
  385. 0,
  386. NULL,
  387. NULL,
  388. OFN_PATHMUSTEXIST,
  389. 0,
  390. 0,
  391. defaultFormatExt,
  392. NULL,
  393. NULL,
  394. NULL
  395. };
  396. if ((GetSaveFileName(&ofn) == TRUE) &&
  397. fname[0] != '\0')
  398. {
  399. OutputFile* outfile = OutputFile::CreateOutputFile(fname);
  400. if (outfile)
  401. {
  402. outfile->GraphicsProcedure();
  403. outfile->BeginIndent();
  404. outfile->GraphicsDeclaration();
  405. outfile->BlankLine();
  406. outfile->SetMatrixDeclaration(_T("g"),
  407. _T("SetWorldTransform"),
  408. _T("worldMatrix"),
  409. worldMatrix);
  410. INT count = shapeStack.GetCount();
  411. for (INT pos=0; pos<count; pos++)
  412. {
  413. TestShape *shape = shapeStack[pos];
  414. ASSERT(shape->IsComplete());
  415. outfile->BlankLine();
  416. outfile->BeginIndent();
  417. shape->AddToFile(outfile);
  418. outfile->EndIndent();
  419. }
  420. if (curShape && curShape->IsComplete())
  421. {
  422. outfile->BlankLine();
  423. outfile->BeginIndent();
  424. curShape->AddToFile(outfile);
  425. outfile->EndIndent();
  426. }
  427. outfile->EndIndent();
  428. delete outfile;
  429. WarningBox(_T("Graphics source code saved."));
  430. }
  431. else
  432. WarningBox(_T("Can't create file for writing."));
  433. }
  434. UpdateStatus();
  435. }
  436. //*******************************************************************
  437. //
  438. // TestGradDraw
  439. //
  440. //
  441. //
  442. //*******************************************************************
  443. VOID TestGradDraw::AddPoint(HWND hwnd, Point pt)
  444. {
  445. gradShape->AddPoint(hwnd, pt);
  446. }
  447. BOOL TestGradDraw::DoneShape(HWND hwnd)
  448. {
  449. // pop-up dialog box to configure point parameters
  450. gradShape->DoneShape(hwnd);
  451. return TRUE;
  452. }
  453. BOOL TestGradDraw::EndPoint(HWND hwnd, Point pt)
  454. {
  455. return gradShape->EndPoint(hwnd, pt);
  456. }
  457. BOOL TestGradDraw::RemovePoint(HWND hwnd)
  458. {
  459. return gradShape->RemovePoint(hwnd);
  460. }
  461. VOID TestGradDraw::Draw(HWND hwnd)
  462. {
  463. PAINTSTRUCT ps;
  464. RECT rt;
  465. HDC hdc;
  466. // !!! when CopyPixels work, cache the graphics up to the
  467. // last shape. We blit that, then the new shape begins.
  468. hdc = BeginPaint(hwnd, &ps);
  469. /////////////////////////////////////////////////////
  470. // GDI+ code BEGINS
  471. ////////////////////////////////////////////////////
  472. Graphics *g = new Graphics(hwnd);
  473. GetClientRect(hwnd, &rt);
  474. ERectangle rect(rt.left,
  475. rt.top,
  476. rt.right-rt.left,
  477. rt.bottom-rt.top);
  478. g->SetClip(rect);
  479. g->SetRenderingHint(TRUE);
  480. gradShape->DrawShape(g);
  481. gradShape->DrawPoints(g);
  482. delete g;
  483. ////////////////////////////////////////////////////
  484. // GDI+ code ENDS.
  485. ////////////////////////////////////////////////////
  486. EndPaint(hwnd, &ps);
  487. }
  488. VOID TestGradDraw::SetClipRegion(HWND hwnd)
  489. {
  490. }
  491. VOID TestGradDraw::RememberPoint(Point pt)
  492. {
  493. remPoint = pt;
  494. }
  495. VOID TestGradDraw::MoveControlPoint(Point pt)
  496. {
  497. if (!gradShape->MoveControlPoint(remPoint, pt))
  498. WarningBeep();
  499. }
  500. VOID TestGradDraw :: UpdateStatus(HWND hwnd)
  501. {
  502. }
  503. VOID TestGradDraw :: SaveAsFile(HWND hwnd)
  504. {
  505. }
  506. BOOL TestGradDraw :: ChangeSettings(HWND hwndParent)
  507. {
  508. HWND hWnd;
  509. MSG msg;
  510. HMENU hMenu = LoadMenu(hInst,
  511. MAKEINTRESOURCE(IDR_GRADBRUSH));
  512. hWnd = CreateWindow(
  513. szWindowClass,
  514. _T("Gradient Brush Shape"),
  515. WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  516. CW_USEDEFAULT,
  517. CW_USEDEFAULT,
  518. 300,
  519. 200,
  520. (HWND)hwndParent,
  521. (HMENU)hMenu, // menu handle
  522. (HINSTANCE)hInst,
  523. (LPVOID)(static_cast<TestDrawInterface*>(this)));
  524. if (!hWnd)
  525. {
  526. return FALSE;
  527. }
  528. ShowWindow(hWnd, SW_SHOWNORMAL);
  529. UpdateWindow(hWnd);
  530. HACCEL hAccelTable = LoadAccelerators(hInst, (LPCTSTR)IDC_GDIPTEST);
  531. // Main message loop:
  532. while (GetMessage(&msg, NULL, 0, 0))
  533. {
  534. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  535. {
  536. TranslateMessage(&msg);
  537. DispatchMessage(&msg);
  538. }
  539. }
  540. DeleteObject(hAccelTable);
  541. return msg.wParam;
  542. }
  543. VOID TestGradDraw::Initialize()
  544. {
  545. DebugBreak();
  546. }
  547. VOID TestGradDraw::Initialize(TestGradShape *newGradShape)
  548. {
  549. gradShape = newGradShape;
  550. }
  551. VOID TestGradDraw::Reset(HWND hwnd)
  552. {
  553. NotImplementedBox();
  554. }
  555. VOID TestGradDraw::Instructions(HWND hwnd)
  556. {
  557. NotImplementedBox();
  558. }