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 >
Wrap
C/C++ Source or Header
|
1996-02-09
|
6KB
|
324 lines
#include <owl\owlpch.h>
#include <owl\applicat.h>
#include <owl\framewin.h>
#include <owl\dc.h>
#include <owl\scroller.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "message.h"
class _TMessageWindow : public TFrameWindow
{
private:
int line ;
int FontV ;
int MaxLine ;
char **TextBuf ;
char *LineBuf ;
// イベント処理関数
BOOL Create();
void EvSize( UINT, TSize& );
void EvKeyDown( UINT key, UINT count, UINT flags );
void Paint( TDC& dc, BOOL, TRect& );
// 初期化
_TMessageWindow( const char *str, int, int, int, int, int );
~_TMessageWindow();
// 表示
void PutString( const char *str );
DECLARE_RESPONSE_TABLE( _TMessageWindow );
friend class TMessageWindow ;
};
const int MAX_COLS = 255 ;
DEFINE_RESPONSE_TABLE1( _TMessageWindow, TFrameWindow )
EV_WM_KEYDOWN,
EV_WM_SIZE,
END_RESPONSE_TABLE;
_TMessageWindow *_win= NULL ;
_TMessageWindow::_TMessageWindow( const char *str,
int maxline,
int x, int y,
int h, int v )
: TFrameWindow( 0, str )
{
line = 0 ;
LineBuf = new char [MAX_COLS+1] ;
LineBuf[0] = '\0' ;
MaxLine = maxline ;
TextBuf = new char* [MaxLine] ;
Attr.X = x ;
Attr.Y = y ;
Attr.W = h ;
Attr.H = v ;
_win = this;
}
_TMessageWindow::~_TMessageWindow()
{
delete LineBuf ;
delete TextBuf ;
_win = NULL;
LineBuf = 0 ;
TextBuf = 0 ;
}
// 初期化
BOOL _TMessageWindow::Create()
{
// フォントの縦サイズを得る
TClientDC tdc( *this );
TSize size = tdc.GetTextExtent( " ", 1 );
FontV = size.cy ;
// 縦スクロールバーを表示
Attr.Style |= WS_VSCROLL ;
Attr.H = Attr.H / FontV * FontV ;
// 基本クラスの Create()
TFrameWindow::Create();
// スクロールバーの設定
Scroller = new TScroller( this, 32, FontV, MAX_COLS, MaxLine );
Scroller->AutoOrg = FALSE;
Scroller->SetPageSize();
// スクロール範囲の設定
TRect rClient = GetClientRect();
Scroller->SetRange( MAX_COLS, MaxLine - rClient.bottom / FontV );
return TRUE ;
}
// サイズ変更
void _TMessageWindow::EvSize( UINT, TSize& size )
{
size.cy = size.cy / FontV * FontV ;
if ( Scroller )
{
Scroller->SetPageSize();
Scroller->SetRange( MAX_COLS, MaxLine - size.cy / FontV );
}
}
// キー入力
void _TMessageWindow::EvKeyDown( UINT key, UINT, UINT )
{
switch( key )
{
case VK_UP:
Scroller->VScroll( SB_LINEUP, 0 );
break ;
case VK_DOWN:
Scroller->VScroll( SB_LINEDOWN, 0 );
break ;
case VK_PRIOR: // Page up.
Scroller->VScroll( SB_PAGEUP, 0 );
break;
case VK_NEXT: // Page down.
Scroller->VScroll( SB_PAGEDOWN, 0 );
break;
}
}
// 再描画
void _TMessageWindow::Paint( TDC& dc, BOOL, TRect& )
{
TRect rClient = GetClientRect();
Scroller->BeginView( dc, rClient );
int yStart = (int)( Scroller->YPos );
int yEnd = yStart + rClient.bottom / FontV + 1;
if ( yEnd > line )
yEnd = line ;
int yLine = 0 ;
for( int i = yStart ; i < yEnd ; i++ )
{
dc.TextOut( 0, yLine, TextBuf[i] );
yLine += FontV ;
}
if ( yEnd == line && LineBuf[0] != '\0' )
{
dc.TextOut( 0, yLine, LineBuf );
}
Scroller->EndView();
}
// 文字列表示
void _TMessageWindow::PutString( const char *str )
{
const char *src = str ;
TRect rClient = GetClientRect();
TClientDC tdc( *this );
Scroller->BeginView( tdc, rClient );
while( *src != '\0' )
{
int len = strlen( LineBuf );
char *dst = LineBuf + len ;
while( len < MAX_COLS && *src != '\n' && *src != '\0' )
{
*dst++ = *src++ ;
len++ ;
}
*dst = '\0' ;
int yLine = (int)( line - Scroller->YPos ) * FontV ;
tdc.TextOut( 0, yLine, LineBuf );
if ( *src != '\0' )
{
if ( line < MaxLine )
{
TextBuf[line] = new char [len+1] ;
strcpy( TextBuf[line], LineBuf );
LineBuf[0] = '\0' ;
line++ ;
}
else
{
delete TextBuf[0] ;
for( int i = 0 ; i < MaxLine - 1 ; i++ )
TextBuf[i] = TextBuf[i+1] ;
TextBuf[MaxLine-1] = new char [len+1] ;
strcpy( TextBuf[MaxLine-1], LineBuf );
LineBuf[0] = '\0' ;
TRegion dmy1 ;
TRect dmy2 ;
tdc.ScrollDC( 0, - FontV, rClient, rClient, dmy1, dmy2 );
tdc.TextRect( 0, yLine, MAX_COLS, yLine+FontV );
}
}
if ( *src == '\n' )
src++ ;
}
int yEnd = (int)Scroller->YPos + rClient.bottom / FontV ;
if ( line > yEnd )
{
Scroller->ScrollBy( 0, line - yEnd );
}
Scroller->EndView();
}
//
// Message Window クラス
//
TMessageWindow *_MessageWindow = NULL ;
TMessageWindow* MessageWindow()
{
return _MessageWindow ;
}
TMessageWindow::TMessageWindow( const char *str,
int maxline,
int x, int y,
int h, int v )
{
win = new _TMessageWindow( str, maxline, x, y, h, v );
_MessageWindow = this ;
}
TMessageWindow::~TMessageWindow()
{
delete win ;
_MessageWindow = NULL;
}
int TMessageWindow::Create()
{
if (_win != NULL) {
return win->Create();
} else {
return FALSE;
}
}
void TMessageWindow::Printf( const char *fmt, ... )
{
char buf[256] ;
va_list argptr ;
va_start( argptr, fmt );
vsprintf( buf, fmt, argptr );
va_end( argptr );
if (_win != NULL)
win->PutString( buf );
}
TMessageWindow& TMessageWindow::operator<<( const char* str )
{
if (_win != NULL)
win->PutString( str );
return *this ;
}
TMessageWindow& TMessageWindow::operator<<( int a )
{
Printf( "%d", a );
return *this ;
}
TMessageWindow& TMessageWindow::operator<<( float a )
{
Printf( "%f", a );
return *this ;
}
TMessageWindow& TMessageWindow::operator<<( void *ptr )
{
Printf( "%lX", ptr );
return *this ;
}
void wprintf( const char *fmt, ... )
{
char buf[256] ;
va_list argptr ;
va_start( argptr, fmt );
vsprintf( buf, fmt, argptr );
va_end( argptr );
if (_win != NULL && _MessageWindow != NULL) {
_MessageWindow->Printf( buf );
}
}
int printf(const char *fmt, ...)
{
char buf[256] ;
va_list argptr ;
va_start( argptr, fmt );
vsprintf( buf, fmt, argptr );
va_end( argptr );
if (_win != NULL && _MessageWindow != NULL) {
_MessageWindow->Printf( buf );
}
return 0;
}