cwin.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // cwin.hpp
  2. // Header file that defines the class structure and all the
  3. // external variables used in the user-interface support code.
  4. //
  5. // Copyright (C)/©/¸ Codemist Ltd, 1995-2002
  6. /*
  7. * This code may be used and modified, and redistributed in binary
  8. * or source form, subject to the "CCL Public License", which should
  9. * accompany it. This license is a variant on the BSD license, and thus
  10. * permits use of code derived from this in either open and commercial
  11. * projects: but it does require that updates to this code be made
  12. * available back to the originators of the package.
  13. * Before merging other code in with this or linking this code
  14. * with other packages or libraries please check that the license terms
  15. * of the other material are compatible with those of this.
  16. */
  17. /* Signature: 61f1ec82 06-Oct-2002 */
  18. #ifndef header_cwin_hpp
  19. #define header_cwin_hpp 1
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <afxwin.h> // Microsoft Foundation Classes
  26. #include <afxdlgs.h>
  27. #include "cwin.h" // Public interface
  28. #include "cwinres.h" // resource file interface
  29. extern CString mainWindowClass; // a Window Class that I register
  30. #ifdef GRAPHICS_WINDOW
  31. // ButtonPlace records are used to define the shapes that I draw
  32. // in the "viewpoint selection" dialog box.
  33. typedef struct tagButtonPlace
  34. {
  35. unsigned int id:7, x:7, y:7, width:7, flavour:4;
  36. char *text;
  37. } ButtonPlace;
  38. typedef double fourByFour[4][4]; // A transformation matrix.
  39. // The viewpoint window is used to scale, translate and rotate graphical
  40. // objects.
  41. class CViewpointWindow : public CWnd
  42. {
  43. public:
  44. CViewpointWindow();
  45. virtual ~CViewpointWindow();
  46. CFont labelFont, titleFont;
  47. void resetXform(int whence);
  48. void changeXform(double w[4][4]);
  49. void rotateXform(int axis, double angle);
  50. void scaleXform(double factor);
  51. void translateXform(double x, double y);
  52. private:
  53. BOOL OnEraseBkgnd(CDC *dc);
  54. void OnPaint();
  55. void OnSetFocus(CWnd *pOldWnd);
  56. void OnKillFocus(CWnd *pNewWnd);
  57. BOOL hasFocus;
  58. void OnChar(UINT ch, UINT nRepCnt, UINT nFlags);
  59. void OnKeyDown(UINT ch, UINT nRepCnt, UINT nFlags);
  60. void OnLButtonDown(UINT nFlags, CPoint point);
  61. void OnLButtonUp(UINT nFlags, CPoint point);
  62. void OnMouseMove(UINT nFlags, CPoint point);
  63. void OnLButtonDblClk(UINT nFlags, CPoint point);
  64. void setArcPoints();
  65. void LText(CDC *dc, int x1, int y, char *s);
  66. void CText(CDC *dc, int x1, int x2, int y, char *s);
  67. void RText(CDC *dc, int x2, int y, char *s);
  68. const ButtonPlace *FindControl(int x, int y);
  69. int inButton(const ButtonPlace *b, int x, int y);
  70. void SetControl(const ButtonPlace *b, int state);
  71. void DrawButton(CDC *dc, const ButtonPlace *pb);
  72. void ButtonAction(int n);
  73. int dlgWidth, dlgHeight;
  74. int click; // measurement unit in my dialog box
  75. const ButtonPlace *activeButton;
  76. int mouseDrag, mouseX, mouseY;
  77. int moveObject;
  78. char buttonPressed[60];
  79. fourByFour view1, view2;
  80. #define undoMask 31
  81. fourByFour undoStack[32];
  82. int undoCount, undoPoint;
  83. DECLARE_MESSAGE_MAP()
  84. };
  85. extern fourByFour stdView;
  86. typedef struct tagXYZPoint
  87. {
  88. unsigned int x:16; // X coordinate of point
  89. unsigned int y:16; // Y coordinate of point
  90. int z; // Z coordinate of point
  91. signed int nx:10, ny:10, nz:10; // normal to surface at point
  92. unsigned int red:8, green:8, blue:8, // colour to paint surface
  93. spare:8;
  94. } XYZPoint;
  95. // A GraphicsWindow displays a shaded surface
  96. class CGraphicsWindow : public CFrameWnd
  97. {
  98. public:
  99. CGraphicsWindow();
  100. virtual ~CGraphicsWindow();
  101. void InitWindow();
  102. double xform[4][4];
  103. double perspecDistance;
  104. CViewpointWindow viewpointWindow;
  105. int fullRender, fullyRendered;
  106. int ctrlPressed;
  107. private:
  108. void OnPaint();
  109. void OnSize(UINT nType, int cx, int cy);
  110. void OnSetFocus(CWnd *pOldWnd);
  111. void OnKillFocus(CWnd *pNewWnd);
  112. BOOL hasFocus;
  113. void OnChar(UINT ch, UINT nRepCnt, UINT nFlags);
  114. void OnKeyDown(UINT ch, UINT nRepCnt, UINT nFlags);
  115. void OnKeyUp(UINT ch, UINT nRepCnt, UINT nFlags);
  116. void OnLButtonDblClk(UINT nFlags, CPoint point);
  117. void OnLButtonDown(UINT nFlags, CPoint point);
  118. void OnLButtonUp(UINT nFlags, CPoint point);
  119. void OnMButtonDblClk(UINT nFlags, CPoint point);
  120. void OnMButtonDown(UINT nFlags, CPoint point);
  121. void OnMButtonUp(UINT nFlags, CPoint point);
  122. void OnRButtonDblClk(UINT nFlags, CPoint point);
  123. void OnRButtonDown(UINT nFlags, CPoint point);
  124. void OnRButtonUp(UINT nFlags, CPoint point);
  125. void OnMouseMove(UINT nFlags, CPoint point);
  126. void OnNcLButtonDown(UINT nFlags, CPoint point);
  127. void OnNcMButtonDown(UINT nFlags, CPoint point);
  128. void OnNcRButtonDown(UINT nFlags, CPoint point);
  129. void OnSaveAs();
  130. void OnPrint();
  131. void OnCloseButton();
  132. void OnCopy();
  133. void OnClear();
  134. void OnRedraw();
  135. void OnViewpoint();
  136. void OnViewpoint1(UINT a, LONG b);
  137. void OnWireframe();
  138. void OnNoSurface();
  139. void OnSurface();
  140. void OnSquares();
  141. void OnTriangles();
  142. void OnSmooth();
  143. void OnHiSmooth();
  144. void OnWirePreview();
  145. void OnRotLeft();
  146. void OnRotRight();
  147. void OnRotUp();
  148. void OnRotDown();
  149. void OnClockwise();
  150. void OnAntiClockwise();
  151. void OnEnlarge();
  152. void OnShrink();
  153. void PaintOrPrint(CDC *dc, RECT *clip, int width, int height,
  154. CFont *mainfont, CFont *smallfont);
  155. HGLOBAL PaintBitmap(int *totalSize, int *psize);
  156. void initSurface();
  157. void sortTriangles();
  158. void paintTriangles(CDC *dc, RECT *clip, int width, int height);
  159. void MoveTo(CDC *dc, int x, int y);
  160. void LineTo(CDC *dc, int x, int y);
  161. void Rectangle(CDC *dc, int x1, int y1, int x2, int y2);
  162. void SubTriangle(CDC *dc, int x1, int y1, int red1, int green1, int blue1,
  163. int x2, int y2, int red2, int green2, int blue2,
  164. int x3, int y3, int red3, int green3, int blue3,
  165. int n);
  166. BOOL viewpointShown;
  167. int drawWire, drawSurface, wirePreview;
  168. int screenWidth, screenHeight;
  169. int graphicsHeight, graphicsWidth;
  170. DECLARE_MESSAGE_MAP()
  171. };
  172. #endif
  173. // I will support a really crude and simple form of hypertext help, based
  174. // on the style popularised by the GNU texinfo program. To start with I will
  175. // implement this so it has just a simple 80 by 25 text window that can
  176. // display in just a single fixed-pitch font (using inverse video for
  177. // highlighting).
  178. class CHelpWindow : public CFrameWnd
  179. {
  180. public:
  181. CHelpWindow();
  182. CFont helpFont;
  183. int height, width;
  184. char contents[25][80];
  185. int highline, highstart, highend;
  186. private:
  187. void OnPaint();
  188. void OnChar(UINT ch, UINT nRepCnt, UINT nFlags);
  189. void OnKeyDown(UINT ch, UINT nRepCnt, UINT nFlags);
  190. void OnLButtonDown(UINT nFlags, CPoint point);
  191. void OnRedraw();
  192. void OnCopy();
  193. void OnClose();
  194. DECLARE_MESSAGE_MAP()
  195. };
  196. // In a text window I want nine fonts (Courier, Roman and Symbol with
  197. // italic and bold and two sizes). I encapsulate these here mainly so that
  198. // the FontArray destructor can be called automatically for me when my
  199. // window gets destroyed.
  200. class FontHeights
  201. {
  202. public:
  203. int up;
  204. int down;
  205. int height;
  206. unsigned char across[256];
  207. };
  208. class FontArray
  209. {
  210. public:
  211. char baseFace[32];
  212. int boldFlag;
  213. CFont *Courier, *Roman, *Bold, *Italic, *Symbol,
  214. *roman, *bold, *italic, *symbol;
  215. FontHeights HCourier, HRoman, HBold, HItalic, HSymbol,
  216. Hroman, Hbold, Hitalic, Hsymbol;
  217. void InitFont(CDC *dc, const char *name, int weight, int size);
  218. void ChangeFont(CDC *dc, int height, int weight, const char *baseFace);
  219. void DeleteFonts();
  220. };
  221. class TextLine
  222. {
  223. public:
  224. int position; // sum of heights of all previous lines
  225. unsigned int up:8, height:8; // font may not be > 256 pixels high
  226. unsigned int address:16; // text buffer is only 64 Kbytes
  227. int width; // in SILLY cases lines may get very long!
  228. };
  229. extern UINT clipboardformat;
  230. class CMainWindow : public CFrameWnd
  231. {
  232. public:
  233. CMainWindow();
  234. virtual ~CMainWindow();
  235. void InitWindow();
  236. FontArray windowFonts, printerFonts;
  237. #ifdef GRAPHICS_WINDOW
  238. CGraphicsWindow *graphicsWindow;
  239. #endif
  240. CHelpWindow *helpWindow;
  241. int clientWidth, clientHeight;
  242. void cwin_caret_putchar(int c); // for K/B input: insert at caret
  243. void cwin_caret_replacechar(int c); // for K/B input: replace at caret
  244. void cwin_caret_unputchar(); // for K/B input: DELETE before caret
  245. BOOL InsertAtCaret(char *s, int n);
  246. BOOL UnTypeAhead(int ch);
  247. void cwin_putchar(int c); // for program output - always at end
  248. void cwin_unputchar(); // delete at end of buffer
  249. void cwin_puts(const char *s);
  250. void
  251. #ifdef _MSC_VER
  252. __cdecl
  253. #endif
  254. cwin_printf(const char *fmt, ...);
  255. void cwin_vfprintf(const char *fmt, va_list a);
  256. void cwin_ensure_screen(BOOL poll);
  257. int cwin_getchar();
  258. int cwin_getchar_nowait();
  259. void cwin_discard_input();
  260. void cwin_set_prompt(const char *s);
  261. char cwin_prompt_string[32];
  262. void cwin_menus(char **packages, char **switches);
  263. void cwin_report_left(const char *msg);
  264. void cwin_report_mid(const char *msg);
  265. void cwin_report_right(const char *msg);
  266. void cwin_display_date();
  267. SYSTEMTIME titleUpdateTime, lastFlushTime;
  268. BOOL leftSetByUser;
  269. #define LINE_BITS 11
  270. #define LINE_SIZE (1<<LINE_BITS) // Keep up to 2048 lines in buffer
  271. #define LINE_MASK (LINE_SIZE-1)
  272. TextLine lineBuffer[LINE_SIZE];
  273. int lineFirst, lineVisible, lineLast;
  274. void LineSizes();
  275. DWORD windowColour, textColour, highlightColour, highlightTextColour;
  276. BOOL complete;
  277. private:
  278. UINT myTimer;
  279. void OnDestroy();
  280. void OnPaint();
  281. int PaintTextLine(CDC *dc, int x, int topY, int y, int bottomY,
  282. int textp, int width, FontArray *fa, int context);
  283. void OnTimer(UINT timerId);
  284. void OnSize(UINT nType, int cx, int cy);
  285. void OnMove(int x, int y);
  286. void AfterSizeChange();
  287. #define SB_REFRESH_THUMB (-1)
  288. #define SB_FOR_CARET (-2)
  289. void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *PCntl);
  290. void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *PCntl);
  291. void OnSetFocus(CWnd *pOldWnd);
  292. void OnKillFocus(CWnd *pNewWnd);
  293. BOOL hasFocus;
  294. void OnActivate(UINT state, CWnd *pWnd, BOOL minim);
  295. void OnChar(UINT ch, UINT nRepCnt, UINT nFlags);
  296. #define typeAheadBufferSize (1<<11)
  297. #define typeAheadBufferMask (typeAheadBufferSize-1)
  298. unsigned char typeAheadBuffer[typeAheadBufferSize];
  299. int typeAheadP1, typeAheadP2;
  300. #define inputBufferSize typeAheadBufferSize
  301. unsigned char inputBuffer[inputBufferSize];
  302. #define MAX_SAVED_LINES 100
  303. #define MAX_SAVED_CHARS (4*inputBufferSize)
  304. int currentInputLine, savedP1, savedP2, savedFirst, savedLast;
  305. int savedLines[MAX_SAVED_LINES];
  306. unsigned char savedChars[MAX_SAVED_CHARS];
  307. void ReplaceLastLine(unsigned char *s);
  308. int inputP;
  309. BOOL insertMode;
  310. void OnKeyDown(UINT ch, UINT nRepCnt, UINT nFlags);
  311. void MakeSpaceAtCaret(int n);
  312. int FindMouseChar(CPoint point);
  313. void StartSelection();
  314. void ExtendSelection();
  315. void InvalidateSelection(int l1, int x1, int l2, int x2);
  316. void CancelSelection();
  317. int caretWidth;
  318. BOOL selRootValid, caretVisible;
  319. unsigned char *endFontWidths;
  320. unsigned char *caretFontWidths;
  321. unsigned char *icaretFontWidths;
  322. int selFirstX, selStartX, selEndX, caretX, icaretX, endX;
  323. int selFirstLine, selStartLine, selEndLine, caretLine, icaretLine;
  324. int selFirstChar, selStartChar, selEndChar, caretChar, icaretChar;
  325. int selectScrollSpeed, selectScrollCount, selectScrollDirection;
  326. CPoint selectScrollPoint;
  327. int mouseOutside;
  328. BOOL pageMode, pagePaused;
  329. int pageLine;
  330. void OnLButtonDblClk(UINT nFlags, CPoint point);
  331. void OnLButtonDown(UINT nFlags, CPoint point);
  332. void OnLButtonUp(UINT nFlags, CPoint point);
  333. void OnMButtonDblClk(UINT nFlags, CPoint point);
  334. void OnMButtonDown(UINT nFlags, CPoint point);
  335. void OnMButtonUp(UINT nFlags, CPoint point);
  336. void OnRButtonDblClk(UINT nFlags, CPoint point);
  337. void OnRButtonDown(UINT nFlags, CPoint point);
  338. void OnRButtonUp(UINT nFlags, CPoint point);
  339. void OnMouseMove(UINT nFlags, CPoint point);
  340. void OnNcLButtonDown(UINT nFlags, CPoint point);
  341. void OnNcMButtonDown(UINT nFlags, CPoint point);
  342. void OnNcRButtonDown(UINT nFlags, CPoint point);
  343. void WrapTextBuffer(BOOL flag);
  344. void OnRead();
  345. void OnToFile();
  346. void OnExit();
  347. void OnSaveAs();
  348. void OnSaveSel();
  349. void OnPrint();
  350. void OnPrintSel();
  351. void OnCut();
  352. void DeleteSelection();
  353. void OnCopy();
  354. void OnPaste();
  355. char *clipboardInput, *clipboardInputP;
  356. void OnReInput();
  357. void OnSelectAll();
  358. void OnClear();
  359. void OnUndo();
  360. void OnRedraw();
  361. void OnHome();
  362. void OnEnd();
  363. void OnFont();
  364. void OnResetFont();
  365. void OnResetWindow();
  366. void OnInterrupt();
  367. void OnBacktrace();
  368. void OnPageMode();
  369. #ifdef GRAPHICS_WINDOW
  370. void OnGraphics();
  371. void OnGraphics1(UINT a, LONG b);
  372. BOOL graphicsShown;
  373. #endif
  374. BOOL helpShown;
  375. #ifndef COMMON
  376. void OnLoadLibrary(UINT a);
  377. void OnSwitch(UINT a);
  378. #endif
  379. // Elsewhere in this code I use a 16-bit field to identify characters in the
  380. // text buffer, so 64K bytes is the largest I can make it.
  381. #define TEXT_BITS 16
  382. #define TEXT_SIZE (1<<TEXT_BITS) // Keep up to 64K characters in buffer
  383. #define TEXT_MASK (TEXT_SIZE-1)
  384. unsigned char textBuffer[TEXT_SIZE];
  385. int textFirst, textLast;
  386. int textmark;
  387. int inputLineStart;
  388. BOOL trackingSelection;
  389. void ToIcaret();
  390. void MeasureLine(int address, int *up, int *down, FontArray *fa);
  391. int currentFont, currentColour;
  392. unsigned char *currentWidths;
  393. int xOffset; // amount of horizontal scrolling
  394. char mainTitle[84], cLeft[32], cMid[32], cRight[32];
  395. void ReWriteTitleText();
  396. DECLARE_MESSAGE_MAP()
  397. };
  398. #define LineY(n) (lineBuffer[n].position-lineBuffer[lineVisible].position)
  399. #define LineDY(n) lineBuffer[n].height
  400. class CTheApp : public CWinApp
  401. {
  402. public:
  403. BOOL InitInstance();
  404. int Run(); // override top level loop (!)
  405. //!!BOOL OnIdle(LONG lCount);
  406. MSG *msgPtr;
  407. CMainWindow *mainWindow;
  408. void OnHelpContents(); // Help is associated with the application
  409. void OnHelpSearch(); // ... not with any particular instance.
  410. void OnHelpOnHelp();
  411. void OnAbout();
  412. int dynamicCount;
  413. const char *dynamic[IDM_LAST_DYNAMIC-IDM_DYNAMIC_ITEMS];
  414. const char *dynamic_files[IDM_LAST_DYNAMIC-IDM_DYNAMIC_ITEMS];
  415. void OnDynamic(unsigned int commandId);
  416. void cwin_set_help_file(const char *key, const char *path);
  417. DECLARE_MESSAGE_MAP()
  418. };
  419. extern void cwin_poll_window_manager(int waitForEvent);
  420. extern int cwin_main(int argc, char *argv[]);
  421. extern CTheApp theApp;
  422. // I can not find the following an the set of header files provided
  423. // with Watcom C 10.5, but they are in the on-line documentation
  424. #define VK_LEFT 0x25
  425. #define VK_UP 0x26
  426. #define VK_RIGHT 0x27
  427. #define VK_DOWN 0x28
  428. #define VK_SHIFT 0x10
  429. #define VK_CONTROL 0x11
  430. #define VK_X 0x58
  431. #define VK_Y 0x59
  432. #define VK_Z 0x5a
  433. #define VK_NUMPAD2 0x62
  434. #define VK_NUMPAD4 0x64
  435. #define VK_NUMPAD6 0x66
  436. #define VK_NUMPAD8 0x68
  437. #define StdView 0
  438. #define Xaxis 1
  439. #define Yaxis 2
  440. #define Zaxis 3
  441. #define View1 4
  442. #define View2 5
  443. #define Undo 6
  444. // I use various control characters in my display buffer.
  445. // I reserve the range 0x80 to 0x9f for such uses.
  446. #define CH_NULL 0
  447. #define CH_TAB 9
  448. #define CH_ENDLINE 10
  449. #define CH_FORMFEED 12
  450. #define CH_ESCAPE 31
  451. #define CH_SPACE 32 // first regular character
  452. // There is a real ugliness about using characters with the 0x80 bit set,
  453. // in that if I store then in a "char *" buffer when I pull them out they
  454. // may have got sign-extended.
  455. #define CH_COURIER 0x90
  456. #define CH_ROMAN 0x91
  457. #define CH_ITALIC 0x92
  458. #define CH_BOLD 0x93
  459. #define CH_SYMBOL 0x94
  460. #define CH_Roman 0x95
  461. #define CH_Italic 0x96
  462. #define CH_Bold 0x97
  463. #define CH_Symbol 0x98
  464. #define CH_RED 0x99
  465. #define CH_BLUE 0x9a // used to mark start of prompt text
  466. #define CH_PROMPT 0x9a
  467. #define CH_GRAY 0x9b
  468. #define CH_BLACK 0x9c
  469. #define CH_ENDPROMPT 0x9c
  470. #define CH_HIGHLIGHT 0x100 // can never occur in the text buffer
  471. extern void
  472. #ifdef _MSC_VER
  473. __cdecl
  474. #endif
  475. DisplayMsg(char *msg, ...); // Used for debugging
  476. #endif /* header_cwin_hpp */
  477. // End of "cwin.hpp"