home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MESSAGE / MESSAGE.CPP next >
C/C++ Source or Header  |  1996-02-09  |  6KB  |  324 lines

  1.  
  2. #include <owl\owlpch.h>
  3. #include <owl\applicat.h>
  4. #include <owl\framewin.h>
  5. #include <owl\dc.h>
  6. #include <owl\scroller.h>
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12.  
  13. #include "message.h"
  14.  
  15. class _TMessageWindow : public TFrameWindow
  16. {
  17.     private:
  18.         int        line ;
  19.         int        FontV ;
  20.         int        MaxLine ;
  21.         char    **TextBuf ;
  22.         char    *LineBuf ;
  23.  
  24.         // イベント処理関数
  25.         BOOL    Create();
  26.         void    EvSize( UINT, TSize& );
  27.         void    EvKeyDown( UINT key, UINT count, UINT flags );
  28.         void    Paint( TDC& dc, BOOL, TRect& );
  29.  
  30.         // 初期化
  31.         _TMessageWindow( const char *str, int, int, int, int, int );
  32.         ~_TMessageWindow();
  33.  
  34.         // 表示
  35.         void    PutString( const char *str );
  36.         DECLARE_RESPONSE_TABLE( _TMessageWindow );
  37.  
  38.     friend    class TMessageWindow ;
  39. };
  40.  
  41. const    int        MAX_COLS = 255 ;
  42.  
  43. DEFINE_RESPONSE_TABLE1( _TMessageWindow, TFrameWindow )
  44.     EV_WM_KEYDOWN,
  45.     EV_WM_SIZE,
  46. END_RESPONSE_TABLE;
  47.  
  48. _TMessageWindow    *_win= NULL ;
  49.  
  50.  
  51. _TMessageWindow::_TMessageWindow(    const char *str,
  52.                                 int maxline,
  53.                                 int x, int y,
  54.                                 int h, int v )
  55. : TFrameWindow( 0, str )
  56. {
  57.     line = 0 ;
  58.     LineBuf = new char [MAX_COLS+1] ;
  59.     LineBuf[0] = '\0' ;
  60.     MaxLine = maxline ;
  61.     TextBuf = new char* [MaxLine] ;
  62.     Attr.X = x ;
  63.     Attr.Y = y ;
  64.     Attr.W = h ;
  65.     Attr.H = v ;
  66.     _win = this;
  67. }
  68.  
  69. _TMessageWindow::~_TMessageWindow()
  70. {
  71.     delete LineBuf ;
  72.     delete TextBuf ;
  73.     _win = NULL;
  74.  
  75.     LineBuf = 0 ;
  76.     TextBuf = 0 ;
  77. }
  78.  
  79. //    初期化
  80. BOOL    _TMessageWindow::Create()
  81. {
  82.     // フォントの縦サイズを得る
  83.     TClientDC    tdc( *this );
  84.     TSize size = tdc.GetTextExtent( " ", 1 );
  85.     FontV = size.cy ;
  86.  
  87.     // 縦スクロールバーを表示
  88.     Attr.Style |= WS_VSCROLL ;
  89.     Attr.H = Attr.H / FontV * FontV ;
  90.  
  91.     // 基本クラスの Create()
  92.     TFrameWindow::Create();
  93.  
  94.     // スクロールバーの設定
  95.     Scroller = new TScroller( this, 32, FontV, MAX_COLS, MaxLine );
  96.     Scroller->AutoOrg = FALSE;
  97.     Scroller->SetPageSize();
  98.  
  99.     // スクロール範囲の設定
  100.     TRect    rClient = GetClientRect();
  101.     Scroller->SetRange( MAX_COLS, MaxLine - rClient.bottom / FontV );
  102.  
  103.     return TRUE ;
  104. }
  105.  
  106. //    サイズ変更
  107. void    _TMessageWindow::EvSize( UINT, TSize& size )
  108. {
  109.     size.cy = size.cy / FontV * FontV ;
  110.     if ( Scroller )
  111.     {
  112.         Scroller->SetPageSize();
  113.         Scroller->SetRange( MAX_COLS, MaxLine - size.cy / FontV );
  114.     }
  115. }
  116.  
  117. //    キー入力
  118. void    _TMessageWindow::EvKeyDown( UINT key, UINT, UINT )
  119. {
  120.     switch( key )
  121.     {
  122.         case VK_UP:
  123.             Scroller->VScroll( SB_LINEUP, 0 );
  124.             break ;
  125.         case VK_DOWN:
  126.             Scroller->VScroll( SB_LINEDOWN, 0 );
  127.             break ;
  128.         case VK_PRIOR:  // Page up.
  129.             Scroller->VScroll( SB_PAGEUP, 0 );
  130.             break;
  131.         case VK_NEXT:   // Page down.
  132.             Scroller->VScroll( SB_PAGEDOWN, 0 );
  133.             break;
  134.     }
  135. }
  136.  
  137. //    再描画
  138. void    _TMessageWindow::Paint( TDC& dc, BOOL, TRect& )
  139. {
  140.     TRect    rClient = GetClientRect();
  141.     Scroller->BeginView( dc, rClient );
  142.  
  143.     int        yStart = (int)( Scroller->YPos );
  144.     int        yEnd = yStart + rClient.bottom / FontV + 1;
  145.     if ( yEnd > line )
  146.         yEnd = line ;
  147.  
  148.     int        yLine = 0 ;
  149.     for( int i = yStart ; i < yEnd ; i++ )
  150.     {
  151.         dc.TextOut( 0, yLine, TextBuf[i] );
  152.         yLine += FontV ;
  153.     }
  154.     if ( yEnd == line && LineBuf[0] != '\0' )
  155.     {
  156.         dc.TextOut( 0, yLine, LineBuf );
  157.     }
  158.     Scroller->EndView();
  159. }
  160.  
  161. //    文字列表示
  162. void    _TMessageWindow::PutString( const char *str )
  163. {
  164.     const char    *src = str ;
  165.     TRect    rClient = GetClientRect();
  166.  
  167.     TClientDC    tdc( *this );
  168.     Scroller->BeginView( tdc, rClient );
  169.  
  170.     while( *src != '\0' )
  171.     {
  172.         int     len = strlen( LineBuf );
  173.         char    *dst = LineBuf + len ;
  174.         
  175.         while( len < MAX_COLS && *src != '\n' && *src != '\0' )
  176.         {
  177.             *dst++ = *src++ ;
  178.             len++ ;
  179.         }
  180.         *dst = '\0' ;
  181.  
  182.         int        yLine = (int)( line - Scroller->YPos ) * FontV ;
  183.         tdc.TextOut( 0, yLine, LineBuf );
  184.         
  185.         if ( *src != '\0' )
  186.         {
  187.             if ( line < MaxLine )
  188.             {
  189.                 TextBuf[line] = new char [len+1] ;
  190.                 strcpy( TextBuf[line], LineBuf );
  191.                 LineBuf[0] = '\0' ;
  192.                 line++ ;
  193.             }
  194.             else
  195.             {
  196.                 delete TextBuf[0] ;
  197.                 for( int i = 0 ; i < MaxLine - 1 ; i++ )
  198.                     TextBuf[i] = TextBuf[i+1] ;
  199.                 TextBuf[MaxLine-1] = new char [len+1] ;
  200.                 strcpy( TextBuf[MaxLine-1], LineBuf );
  201.                 LineBuf[0] = '\0' ;
  202.  
  203.                 TRegion    dmy1 ;
  204.                 TRect    dmy2 ;
  205.                 tdc.ScrollDC( 0, - FontV, rClient, rClient, dmy1, dmy2 );
  206.                 tdc.TextRect( 0, yLine, MAX_COLS, yLine+FontV );
  207.             }
  208.         }
  209.         if ( *src == '\n' )
  210.             src++ ;
  211.     }
  212.  
  213.     int        yEnd = (int)Scroller->YPos + rClient.bottom / FontV ;
  214.     if ( line > yEnd )
  215.     {
  216.         Scroller->ScrollBy( 0, line - yEnd );
  217.     }
  218.     Scroller->EndView();
  219. }
  220.  
  221.  
  222. //
  223. //        Message Window クラス
  224. //
  225.  
  226. TMessageWindow    *_MessageWindow = NULL ;
  227.  
  228. TMessageWindow*    MessageWindow()
  229. {
  230.     return _MessageWindow ;
  231. }
  232.  
  233. TMessageWindow::TMessageWindow(    const char *str,
  234.                                 int maxline,
  235.                                 int x, int y,
  236.                                 int h, int v )
  237. {
  238.     win = new _TMessageWindow( str, maxline, x, y, h, v );
  239.     _MessageWindow = this ;
  240. }
  241.  
  242. TMessageWindow::~TMessageWindow()
  243. {
  244.     delete win ;
  245.     _MessageWindow = NULL;
  246. }
  247.  
  248. int        TMessageWindow::Create()
  249. {
  250.     if (_win != NULL) {
  251.         return win->Create();
  252.     } else {
  253.         return FALSE;
  254.     }
  255. }
  256.  
  257. void    TMessageWindow::Printf( const char *fmt, ... )
  258. {
  259.     char    buf[256] ;
  260.  
  261.     va_list    argptr ;
  262.     va_start( argptr, fmt );
  263.     vsprintf( buf, fmt, argptr );
  264.     va_end( argptr );
  265.  
  266.     if (_win != NULL)
  267.         win->PutString( buf );
  268. }
  269.  
  270. TMessageWindow&    TMessageWindow::operator<<( const char* str )
  271. {
  272.     if (_win != NULL)
  273.         win->PutString( str );
  274.     return *this ;
  275. }
  276.  
  277. TMessageWindow&    TMessageWindow::operator<<( int a )
  278. {
  279.     Printf( "%d", a );
  280.     return *this ;
  281. }
  282.  
  283. TMessageWindow&    TMessageWindow::operator<<( float a )
  284. {
  285.     Printf( "%f", a );
  286.     return *this ;
  287. }
  288.  
  289. TMessageWindow&    TMessageWindow::operator<<( void *ptr )
  290. {
  291.     Printf( "%lX", ptr );
  292.     return *this ;
  293. }
  294.  
  295. void    wprintf( const char *fmt, ... )
  296. {
  297.     char    buf[256] ;
  298.  
  299.     va_list    argptr ;
  300.     va_start( argptr, fmt );
  301.     vsprintf( buf, fmt, argptr );
  302.     va_end( argptr );
  303.  
  304.     if (_win != NULL && _MessageWindow != NULL) {
  305.         _MessageWindow->Printf( buf );
  306.     }
  307. }
  308.  
  309. int printf(const char *fmt, ...)
  310. {
  311.     char    buf[256] ;
  312.  
  313.     va_list    argptr ;
  314.     va_start( argptr, fmt );
  315.     vsprintf( buf, fmt, argptr );
  316.     va_end( argptr );
  317.  
  318.     if (_win != NULL && _MessageWindow != NULL) {
  319.         _MessageWindow->Printf( buf );
  320.     }
  321.     return 0;
  322. }
  323.  
  324.