cwin.hpp 16 KB

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