Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:DIPLOM2 / DIPLOMA
.CPP#include <owl\applicat.h>
#include <owl\framewin.h>
#include <owl\color.h>
#include <owl\dc.h>
#include <owl\gdiobjec.h>
#include <owl\scroller.h>
#include <owl\opensave.h>
#include <classlib\arrays.h>
#include <classlib\stdtempl.h>
#include <stdio.h>
#include <cstring.h>
#include <string.h>
#include <mem.h>
#pragma hdrstop
#include "diploma.h"
#include "diploma.rh"
//--------------------------------------------------------------------------
// The application's main window
//--------------------------------------------------------------------------
class TChartWin: public TFrameWindow {
public:
TChartWin(TWindow* parent, const char far* title);
~TChartWin();
protected:
void SetupWindow();
void AdjustSettings();
BOOL ReadFile();
void EvSize(UINT sizeType, TSize& size);
void EvKeyDown(UINT key, UINT repeatCount, UINT flags);
void Paint(TDC& dc, BOOL erase, TRect& rect);
private:
string faylik;
TIArrayAsVector<string> sArray;
TColor fontColor;
TFont* font;
string* filename;
long width, height;
int numRows, numCols, rowSep, colSep, glyphW, glyphH, fontPointSize;
DECLARE_RESPONSE_TABLE(TChartWin);
};
DEFINE_RESPONSE_TABLE1(TChartWin, TFrameWindow)
EV_WM_SIZE,
EV_WM_KEYDOWN,
END_RESPONSE_TABLE;
// Constructor
TChartWin::TChartWin(TWindow* parent, const char far* title)
: TFrameWindow(parent, title),
TWindow(parent, title),
sArray(SARRAY_HIGH, SARRAY_LOW, SARRAY_DELTA)
{
AssignMenu(ID_MENU);
Attr.Style |= WS_VSCROLL | WS_HSCROLL;
Scroller = new TScroller(this, 0, 0, 0, 0);
fontColor = ::GetSysColor(COLOR_WINDOWTEXT);
faylik = "readme.txt";
fontPointSize = 10;
width = height = 0;
numRows = numCols = 0;
rowSep = colSep = 0;
glyphH = glyphW = 0;
font = 0;
filename = new string(faylik);
if (!ReadFile())
numRows = 0;
}
// Destructor
TChartWin::~TChartWin()
{
delete font;
delete filename;
}
//----------------------------------------------------------
// Perform window initializations
//----------------------------------------------------------
void
TChartWin::SetupWindow()
{
TFrameWindow::SetupWindow();
int YUnits = (new TClientDC(*this))->GetDeviceCaps(LOGPIXELSY);
font = new TFont(
"Courier New Bold",
-::MulDiv(fontPointSize, YUnits, 72),
0, 0, 0,
FW_NORMAL,
FIXED_PITCH,
FALSE, FALSE, FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY
);
AdjustSettings();
}
//----------------------------------------------------------
// Ajust document settings and scrollers
//----------------------------------------------------------
void
TChartWin::AdjustSettings()
{
TRect r;
long h, w;
TEXTMETRIC tm;
TClientDC* dc;
dc = new TClientDC(*this);
dc->SelectObject(*font);
dc->GetTextMetrics(tm);
colSep = dc->GetTextCharacterExtra();
delete dc;
glyphW = tm.tmMaxCharWidth;
glyphH = tm.tmHeight;
rowSep = tm.tmExternalLeading;
width = colSep + ((numCols + 1) * (glyphW + colSep));
height = rowSep + ((numRows + 1) * (glyphH + rowSep));
Scroller->SetUnits(glyphW + colSep, glyphH + rowSep);
GetClientRect(r);
w = width - (r.right - r.left);
h = height - (r.bottom - r.top);
w /= Scroller->XUnit;
h /= Scroller->YUnit;
Scroller->SetRange(w, h);
SetScrollPos(SB_HORZ, (int)Scroller->XPos, TRUE);
SetScrollPos(SB_VERT, (int)Scroller->YPos, TRUE);
}
//----------------------------------------------------------
// Read file (filename) into string array (sArray)
//----------------------------------------------------------
BOOL
TChartWin::ReadFile()
{
FILE *fp;
char buffer[256];
int len;
fp = fopen(filename->c_str(), "r");
if (!fp) return FALSE;
sArray.Flush();
numRows = numCols = 0;
while (fgets(buffer, 256, fp)) {
len = strlen(buffer);
if (len > 0 && buffer[len - 1] == 0x0A)
buffer[--len] = 0;
if (len > numCols)
numCols = len;
sArray.Add(new string(buffer));
numRows++;
}
fclose(fp);
return TRUE;
}
//----------------------------------------------------------
// Respond to WM_SIZE messages
//----------------------------------------------------------
void
TChartWin::EvSize(UINT sizeType, TSize& size)
{
TFrameWindow::EvSize(sizeType, size);
if (!IsIconic()) {
AdjustSettings();
Invalidate(FALSE);
UpdateWindow();
}
}
//----------------------------------------------------------
// Respond to WM_KEYDOWN messages
//----------------------------------------------------------
void
TChartWin::EvKeyDown(
UINT key, UINT /*repeatCount*/, UINT /*flags*/)
{
int xLine = Scroller->XLine;
int yLine = Scroller->YLine;
int yPage = Scroller->YPage;
switch (key) {
case VK_UP:
Scroller->ScrollBy(0, -yLine);
break;
case VK_DOWN:
Scroller->ScrollBy(0, yLine);
break;
case VK_LEFT:
Scroller->ScrollBy(-xLine, 0);
break;
case VK_RIGHT:
Scroller->ScrollBy(xLine, 0);
break;
case VK_PRIOR:
Scroller->ScrollBy(0, -yPage);
break;
case VK_NEXT:
Scroller->ScrollBy(0, yPage);
break;
case VK_HOME:
Scroller->ScrollBy(0, 0);
break;
case VK_END:
Scroller->ScrollBy(0, numRows);
break;
default: DefaultProcessing();
}
}
//----------------------------------------------------------
// Display text in window
//----------------------------------------------------------
void
TChartWin::Paint(TDC& dc, BOOL /*erase*/, TRect& /*rect*/)
{
TRect r;
int startRow, endRow, row;
dc.SetTextColor(fontColor);
dc.SetBkColor(::GetSysColor(COLOR_WINDOW));
dc.SelectObject(*font);
dc.GetClipBox(r);
startRow = max(0, r.top / Scroller->YUnit - 1);
endRow = min(numRows, r.bottom / Scroller->XUnit + 1);
for (row = startRow; row < endRow; ++row) {
dc.TextOut(colSep, rowSep + Scroller->YUnit * row, sArray[row]->c_str(), sArray[row]->length());
}
dc.RestoreFont();
}
//----------------------------------------------------------
// The application class
//----------------------------------------------------------
class TMainWindow: public TApplication {
public:
TMainWindow(const char far* name)
: TApplication(name) {};
void InitMainWindow();
};
// Initialize the program's main window
void
TMainWindow::InitMainWindow()
{
MainWindow = new TChartWin(0, " Main program ");
}
#pragma argsused
// Main program
int
OwlMain(int argc, char* argv[])
{
TMainWindow app("vot");
return app.Run();
}