home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / msgcls / tlnmsg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-30  |  8.2 KB  |  292 lines

  1. /**********************************************************************
  2.  * File Name:   TLNMSG.CPP                                            *
  3.  * Date:        01/29/92                                              *
  4.  *                                                                    *
  5.  * Description: Implements a scrollable message box to post messages  *
  6.  * to the user.  Call "postMsg(const char *)" with the text you want  *
  7.  * inserted into the message window.  If the message window doesn't   *
  8.  * exist, it is created.  The message is then posted to the end       *
  9.  * of the list.                                                       *
  10.  * Implements an information box to give the user current status      *
  11.  * (similar to Borlands "Compiling" and "Linking" information boxes.  *
  12.  * Call "postInfo(int, const char *)" with the line number and the    *
  13.  * information string.  If the information box doesn't exist, it is   *
  14.  * created.  Pass a negative line number to close the box.  The last  *
  15.  * line is reserved for a user prompt, eg, "Press Escape to Continue" *
  16.  * and is displayed as Cyan on a Blue background.  NOTE: This view    *
  17.  * is NOT MODAL therefore it doesn't call getEvent!  The code that is *
  18.  * posting messages needs to handle events such as Ctrl-Break or the  *
  19.  * user pressing escape.                                              *
  20.  *                                                                    *
  21.  * Revisions:                                                         *
  22.  *                                                                    *
  23.  **********************************************************************/
  24.  
  25. #include <string.h>
  26.  
  27. #include "tlnmsg.h"
  28. #include "const.h"     // for command constants
  29.  
  30. // ********************************************************************
  31.  
  32. TMsgListViewer::TMsgListViewer( const TRect& bounds, ushort aNumCols,
  33.                   TScrollBar * aHScrollBar, TScrollBar * aVScrollBar ) :
  34.   TListViewer ( bounds, aNumCols, aHScrollBar, aVScrollBar )
  35. {
  36.   // expand and shrink to the right and bottom
  37.  
  38.   dragMode = dmDragGrow;
  39.   growMode = gfGrowHiY | gfGrowHiX;
  40.  
  41.   items = new TNSCollection(6, 1);
  42.   setRange(0);
  43. }
  44.  
  45. void TMsgListViewer:: getText( char * dest, short item, short maxLen )
  46. {
  47.   * dest = EOS;
  48.  
  49.   if (items != 0) {
  50.     strncpy(dest, (const char *) (items -> at(item)), maxLen);
  51.     dest[maxLen] = 0;
  52.   }
  53. }
  54.  
  55. void TMsgListViewer:: insert(const char * msg)
  56. {
  57.   items -> insert( newStr( (const char *) msg) );
  58.   setRange(items -> getCount());
  59.   drawView();
  60. }
  61.  
  62. // map into the 1 - 5 into 9 - 13 of the owner (TMsgWindow)
  63.  
  64. #define cpMsgList "\x09\x0A\x0B\x0C\x0D"
  65.  
  66. TPalette& TMsgListViewer::getPalette() const
  67. {
  68.   static TPalette palette( cpMsgList, sizeof(cpMsgList) - 1);
  69.   return palette;
  70. }
  71.  
  72. // ********************************************************************
  73.  
  74. TlnMsgWindow::TlnMsgWindow(const TRect& bounds) :
  75.   TWindow(bounds, "Message", wnNoNumber),
  76.   TWindowInit( &TlnMsgWindow::initFrame)
  77. {
  78.   TScrollBar * vBar;
  79.   TScrollBar * hBar;
  80.  
  81.   // Initialize the window
  82.   dragMode = dmLimitAll;
  83.  
  84.   palette = wpCyanWindow;
  85.  
  86.  
  87.   vBar = standardScrollBar( sbVertical | sbHandleKeyboard );
  88.   hBar = standardScrollBar( sbHorizontal | sbHandleKeyboard );
  89.  
  90.   TRect r = getExtent();
  91.   r.a.x = 1; r.a.y = 1; r.b.x -= 1; r.b.y -= 1;
  92.   msgViewer = new TMsgListViewer(r, 1, hBar, vBar);
  93.   insert(msgViewer);
  94. }
  95.  
  96. TlnMsgWindow::~TlnMsgWindow()
  97. {
  98. }
  99.  
  100. void TlnMsgWindow::handleEvent(TEvent& event)
  101. {
  102.   TWindow::handleEvent(event);
  103.  
  104.   if (event.what == evBroadcast)
  105.     switch(event.message.command) {
  106.       case cmFindMsgBox :               // return a pointer to self
  107.         clearEvent(event);
  108.         break;
  109.       case cmInsMsgBox :               // insert a message into box
  110.         msgViewer -> insert((const char *) event.message.infoPtr);
  111.         clearEvent(event);
  112.         drawView();
  113.         break;
  114.     }
  115. }
  116.  
  117. // This will map colors 9 - 13 into the ListViewer Palette
  118.  
  119. #define cpMsgWindow "\x10\x11\x12\x13\x14\x15\x16\x17\x39\x3A\x3B\x3C\x3D"
  120.  
  121. TPalette& TlnMsgWindow::getPalette() const
  122. {
  123.   static TPalette palette( cpMsgWindow, sizeof(cpMsgWindow) - 1);
  124.   return palette;
  125. }
  126.  
  127. // ********************************************************************
  128.  
  129. void postMsg(const char * msg)
  130. {
  131.    TlnMsgWindow * wPtr;
  132.    TRect r;
  133.  
  134.    wPtr = (TlnMsgWindow *)
  135.      message(TProgram::deskTop, evBroadcast, cmFindMsgBox, 0);
  136.  
  137.    if (wPtr == 0) {
  138.       // Create the message window
  139.       r = TProgram::deskTop -> getExtent();
  140.       r.a.y = r.b.y - 6;
  141.       TProgram::deskTop -> insert(wPtr = new TlnMsgWindow(r));
  142.    }
  143.  
  144.    message(wPtr, evBroadcast, cmInsMsgBox, (void *) msg);
  145. }
  146.  
  147. // ********************************************************************
  148.  
  149. TStaticPrompt::TStaticPrompt(TRect& bounds, const char * text) :
  150.   TStaticText(bounds, text)
  151. {
  152. }
  153.  
  154. // map the palette into the 9th entry of TlnInfoWindow
  155. #define cpStaticPrompt "\x09"
  156.  
  157. TPalette& TStaticPrompt::getPalette() const
  158. {
  159.   static TPalette palette( cpStaticPrompt, sizeof(cpStaticPrompt) - 1);
  160.   return palette;
  161. }
  162.  
  163.  
  164. // ********************************************************************
  165.  
  166. TlnInfoWindow::TlnInfoWindow( const TRect& bounds,
  167.                               const char * aTitle,
  168.                               short aNumber) :
  169.   TWindow (bounds, aTitle, aNumber),
  170.   TWindowInit(&TlnInfoWindow::initFrame)
  171. {
  172.   int i;
  173.  
  174.   // don't allow move, grow, close or zoom
  175.   flags = 0;
  176.  
  177.   // center it in the group
  178.   options |= ofCentered;
  179.  
  180.   // Buffer the screen writes to prevent flicker
  181.   // Buffer will be automatically deleted if low on memory
  182.   options |= ofBuffered;
  183.  
  184.   count = 0;
  185.   for (i = 0; i < sizeof(items) / sizeof(TStaticText *); ++i)
  186.     items[i] = NULL;
  187.  
  188.   // turn off the mouse event queue
  189.   TEventQueue::suspend();
  190.  
  191. }
  192.  
  193. TlnInfoWindow::~TlnInfoWindow()
  194. {
  195.   // Turn the mouse event queue back on
  196.   TEventQueue::resume();
  197. }
  198.  
  199. void TlnInfoWindow::handleEvent(TEvent& event)
  200. {
  201.   // all events get cleared by this function to allow
  202.   // "modal" operation without stealing the processor.
  203.  
  204.   int i, lastLine;
  205.  
  206.   if (event.what == evBroadcast)
  207.  
  208.     switch (event.message.command) {
  209.  
  210.       case cmFindInfoBox :
  211.         clearEvent(event);
  212.         break;
  213.  
  214.       case cmInsInfoBox :
  215.         InfoData * p = (InfoData *) event.message.infoPtr;
  216.         TRect r = getExtent();
  217.         lastLine = r.b.y - 2;
  218.         i = p -> line;
  219.         if (i > r.b.y - r.a.y - 1)
  220.           break;
  221.  
  222.         // lock all screen writes till buffer updated
  223.  
  224.         lock();
  225.         if (items[i] != NULL) {
  226.           remove(items[i]);
  227.           destroy(items[i]);
  228.         }
  229.  
  230.  
  231.         r.a.y = i; r.b.y = i+1;
  232.  
  233.         if  (i == lastLine) {
  234.           r.a.x += 1; r.b.x -= 1;
  235.           items[i] = new TStaticPrompt( r, p -> text );
  236.         } else {
  237.           r.a.x += 2; r.b.x -= 1;
  238.           items[i] = new TStaticText ( r, p -> text);
  239.         }
  240.  
  241.         insert(items[i]);
  242.  
  243.         // unlock screen writes and redraw the buffer
  244.         unlock();
  245.  
  246.         clearEvent(event);
  247.         break;
  248.     } // endswitch
  249.  
  250. }
  251.  
  252. // add 0x14 to the end of the normal gray window palette.  This
  253. // maps into "Scrollbar Reserved", Background = 1 (Blue), Foreground
  254. // = 3 (Cyan)
  255. //                       1   2   3   4   5   6   7   8   9
  256. #define cpInfoWindow "\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x14"
  257.  
  258. TPalette& TlnInfoWindow::getPalette() const
  259. {
  260.   static TPalette palette( cpInfoWindow, sizeof(cpInfoWindow) - 1);
  261.   return palette;
  262. }
  263.  
  264.  
  265. // ********************************************************************
  266.  
  267. void postInfo(int line, const char * text)
  268. {
  269.    TlnInfoWindow * wPtr;
  270.    TRect r;
  271.    InfoData data;
  272.  
  273.    wPtr = (TlnInfoWindow *)
  274.      message(TProgram::deskTop, evBroadcast, cmFindInfoBox, 0);
  275.  
  276.    if ((line < 0) && (wPtr != 0)){
  277.      TProgram::deskTop -> destroy(wPtr);
  278.      return;
  279.    }
  280.  
  281.    if (wPtr == 0) {
  282.       // Create the information window
  283.       TProgram::deskTop ->
  284.         insert(wPtr = new TlnInfoWindow( TRect(0, 0, 40, 12),
  285.                                          "Information", wnNoNumber));
  286.    }
  287.  
  288.    data.line = line;
  289.    data.text = text;
  290.    message(wPtr, evBroadcast, cmInsInfoBox, (void *) &data);
  291. }
  292.