ui.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #define Uses_TKeys
  2. #define Uses_TApplication
  3. #define Uses_TEvent
  4. #define Uses_TRect
  5. #define Uses_TDialog
  6. #define Uses_TStaticText
  7. #define Uses_TButton
  8. #define Uses_TMenuBar
  9. #define Uses_TSubMenu
  10. #define Uses_TMenuItem
  11. #define Uses_TStatusLine
  12. #define Uses_TStatusItem
  13. #define Uses_TStatusDef
  14. #define Uses_TScroller
  15. #define Uses_TDeskTop
  16. #define Uses_TCollection
  17. #define Uses_TScroller
  18. #define Uses_TWindow
  19. #define Uses_ipstream
  20. #define Uses_opstream
  21. #define Uses_TStreamableClass
  22. #include <string>
  23. #include <tvision/tv.h>
  24. class TItemCollection : public TCollection {
  25. public:
  26. TItemCollection(short lim, short delta) : TCollection(lim, delta) {}
  27. virtual void freeItem(void *p) { delete[](char *) p; }
  28. private:
  29. virtual void *readItem(ipstream &) { return 0; }
  30. virtual void writeItem(void *, opstream &) {}
  31. };
  32. class TItemViewer : public TScroller {
  33. public:
  34. char *fileName;
  35. TCollection *fileLines;
  36. Boolean isValid;
  37. TItemViewer(const TRect &bounds, TScrollBar *aHScrollBar,
  38. TScrollBar *aVScrollBar, Boolean left);
  39. ~TItemViewer();
  40. TItemViewer(StreamableInit) : TScroller(streamableInit){};
  41. void draw();
  42. void setState(ushort aState, Boolean enable);
  43. void scrollDraw();
  44. Boolean valid(ushort command);
  45. private:
  46. virtual const char *streamableName() const { return name; }
  47. protected:
  48. virtual void write(opstream &);
  49. virtual void *read(ipstream &);
  50. public:
  51. static const char *const name;
  52. static TStreamable *build();
  53. };
  54. class TItemWindow : public TWindow {
  55. public:
  56. TItemWindow(const char *fileName);
  57. protected:
  58. virtual void sizeLimits(TPoint& min, TPoint& max) override {
  59. TWindow::sizeLimits(min, max);
  60. min.x = size.x/2+10;
  61. };
  62. };
  63. const int maxLineLength = 256;
  64. const char *const TItemViewer::name = "TItemViewer";
  65. TItemViewer::TItemViewer(const TRect &bounds, TScrollBar *aHScrollBar,
  66. TScrollBar *aVScrollBar, Boolean left)
  67. : TScroller(bounds, aHScrollBar, aVScrollBar) {
  68. if (left)
  69. growMode = gfGrowHiY;
  70. else
  71. growMode = gfGrowHiX | gfGrowHiY;
  72. isValid = True;
  73. fileName = 0;
  74. fileLines = new TItemCollection(5, 5);
  75. fileLines->insert(newStr(left ? "Items" : "Categories"));
  76. }
  77. TItemViewer::~TItemViewer() { destroy(fileLines); }
  78. void TItemViewer::draw() {
  79. char *p;
  80. ushort c = getColor(0x0301);
  81. for (short i = 0; i < size.y; i++) {
  82. TDrawBuffer b;
  83. b.moveChar(0, ' ', c, size.x);
  84. if (delta.y + i < fileLines->getCount()) {
  85. p = (char *)(fileLines->at(delta.y + i));
  86. if (p)
  87. b.moveStr(0, p, c, (short)size.x, (short)delta.x);
  88. }
  89. writeBuf(0, i, (short)size.x, 1, b);
  90. }
  91. }
  92. void TItemViewer::scrollDraw() {
  93. TScroller::scrollDraw();
  94. draw();
  95. }
  96. void TItemViewer::setState(ushort aState, Boolean enable) {
  97. TScroller::setState(aState, enable);
  98. if (enable && (aState & sfExposed))
  99. setLimit(limit.x, limit.y);
  100. }
  101. Boolean TItemViewer::valid(ushort) { return isValid; }
  102. void *TItemViewer::read(ipstream &is) {
  103. char *fName;
  104. TScroller::read(is);
  105. delete fName;
  106. return this;
  107. }
  108. void TItemViewer::write(opstream &os) {
  109. TScroller::write(os);
  110. os.writeString(fileName);
  111. }
  112. TStreamable *TItemViewer::build() { return new TItemViewer(streamableInit); }
  113. TStreamableClass RItemView(TItemViewer::name, TItemViewer::build,
  114. __DELTA(TItemViewer));
  115. static short winNumber = 0;
  116. TItemWindow::TItemWindow(const char *fileName)
  117. : TWindow(TProgram::deskTop->getExtent(), fileName, winNumber++),
  118. TWindowInit(&TItemWindow::initFrame) {
  119. options |= ofTileable;
  120. auto bounds = getExtent();
  121. TRect r(bounds.a.x, bounds.a.y, bounds.b.x / 2 + 1, bounds.b.y);
  122. r.grow(-1, -1);
  123. insert(new TItemViewer(r, standardScrollBar(sbHorizontal | sbHandleKeyboard),
  124. standardScrollBar(sbVertical | sbHandleKeyboard),
  125. True));
  126. r = TRect(bounds.b.x / 2, bounds.a.y, bounds.b.x, bounds.b.y);
  127. r.grow(-1, -1);
  128. insert(new TItemViewer(r, standardScrollBar(sbHorizontal | sbHandleKeyboard),
  129. standardScrollBar(sbVertical | sbHandleKeyboard),
  130. False));
  131. }
  132. const int GreetThemCmd = 100;
  133. const int CallListCmd = 101;
  134. class THelloApp : public TApplication {
  135. public:
  136. THelloApp();
  137. virtual void handleEvent(TEvent &event);
  138. static TMenuBar *initMenuBar(TRect);
  139. static TStatusLine *initStatusLine(TRect);
  140. void set_hello(const std::string &new_line) { hello_line = new_line; }
  141. private:
  142. std::string hello_line;
  143. void greetingBox();
  144. };
  145. THelloApp::THelloApp()
  146. : TProgInit(&THelloApp::initStatusLine, &THelloApp::initMenuBar,
  147. &THelloApp::initDeskTop) {}
  148. void THelloApp::greetingBox() {
  149. TDialog *d = new TDialog(TRect(25, 5, 55, 16), "Hello, World!");
  150. d->insert(new TStaticText(TRect(3, 5, 15, 6), hello_line.c_str()));
  151. d->insert(new TButton(TRect(16, 2, 28, 4), "Terrific", cmCancel, bfNormal));
  152. d->insert(new TButton(TRect(16, 4, 28, 6), "Ok", cmCancel, bfNormal));
  153. d->insert(new TButton(TRect(16, 6, 28, 8), "Lousy", cmCancel, bfNormal));
  154. d->insert(new TButton(TRect(16, 8, 28, 10), "Cancel", cmCancel, bfNormal));
  155. deskTop->execView(d);
  156. destroy(d);
  157. }
  158. void THelloApp::handleEvent(TEvent &event) {
  159. TApplication::handleEvent(event);
  160. if (event.what == evCommand) {
  161. switch (event.message.command) {
  162. case GreetThemCmd:
  163. greetingBox();
  164. clearEvent(event);
  165. break;
  166. case CallListCmd:
  167. if (TView *w = validView(new TItemWindow("test")))
  168. deskTop->insert(w);
  169. clearEvent(event);
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. }
  176. TMenuBar *THelloApp::initMenuBar(TRect r) {
  177. r.b.y = r.a.y + 1;
  178. return new TMenuBar(
  179. r, *new TSubMenu("~F~ile", kbAltH) +
  180. *new TMenuItem("~G~reeting...", GreetThemCmd, kbAltG) +
  181. *new TMenuItem("~L~ist...", CallListCmd, kbAltL) + newLine() +
  182. *new TMenuItem("E~x~it", cmQuit, cmQuit, hcNoContext, "Alt-X"));
  183. }
  184. TStatusLine *THelloApp::initStatusLine(TRect r) {
  185. r.a.y = r.b.y - 1;
  186. return new TStatusLine(r,
  187. *new TStatusDef(0, 0xFFFF) +
  188. *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
  189. *new TStatusItem(0, kbF10, cmMenu));
  190. }
  191. #ifndef BINARY
  192. extern "C" int ui_main(char *hello_line) {
  193. #else
  194. int main() {
  195. const char *hello_line = "test line";
  196. #endif
  197. THelloApp helloWorld;
  198. helloWorld.set_hello(hello_line);
  199. helloWorld.run();
  200. return 0;
  201. }