home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
14_5.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-26
|
6KB
|
197 lines
#include <owl\framewin.h>
#include <owl\applicat.h>
#include <owl\dc.h>
#include <mem.h>
#pragma hdrstop
#include "real.rh"
const int maxLines = 25;
const int maxWidth = 80;
const int maxData = maxLines * (maxWidth + 1);
class BaseWindow : public TWindow
{
protected:
int currentLine; // line being typed in now
int lineLen[maxLines]; // length of each line
char *linePtrs[maxLines]; // string for each line
BOOL isMinimized; // TRUE if window is an icon
TSize windowSize; // structure with size in pixels
void EvChar(UINT key, UINT repeatCount, UINT flags);
void Paint(TDC& dc, BOOL, TRect&);
void EvSize(UINT sizeType, TSize& size);
void CmAbout();
void CmClear();
// Menu choice, end the program
void CmFileExit() { PostQuitMessage(0); }
public:
BaseWindow(TWindow *parent = 0);
~BaseWindow() {}
DECLARE_RESPONSE_TABLE(BaseWindow); // says we'll have a
}; // response table
DEFINE_RESPONSE_TABLE1(BaseWindow, TWindow)
EV_WM_CHAR,
EV_WM_SIZE,
EV_COMMAND( CM_ABOUT, CmAbout),
EV_COMMAND( CM_FILEEXIT, CmFileExit),
EV_COMMAND( CM_CLEAR, CmClear),
END_RESPONSE_TABLE;
class MyApp : public TApplication
{
public:
MyApp() : TApplication() {}
void InitMainWindow();
};
BaseWindow::BaseWindow(TWindow *parent)
{
int lineNum;
Init(parent, 0, 0);
linePtrs[0] = new char[maxData]; // allocate edit buffer
lineLen[0] = currentLine = 0;
// apportion the buffer out to the line pointer array
for (lineNum = 1; lineNum < maxLines; ++lineNum)
{
linePtrs[lineNum] = linePtrs[lineNum - 1] + maxWidth;
lineLen[lineNum] = 0;
}
}
// Menu choice, display an About box,
// use a message box to do it
void
BaseWindow::CmAbout()
{
MessageBox("Teach Yourself BC++ 4 in 21 Days", "About");
}
// Menu choice, clear the display
void
BaseWindow::CmClear()
{
for (int lineNum = 0; lineNum < maxLines; ++lineNum)
lineLen[lineNum] = 0; // empty all lines
currentLine = 0; // move back to top line
Invalidate(); // window is invalid, repaint
}
// this is called whenever the window changes size
void
BaseWindow::EvSize(UINT sizeType, TSize& size)
{
if (sizeType == SIZE_MINIMIZED) // if shrunk to icon
isMinimized = TRUE;
else
{
windowSize = size; // save window size
isMinimized = FALSE;
}
}
// called when time to update (paint) the screen
void
BaseWindow::Paint(TDC& dc, BOOL, TRect&)
{
int lineNum; // line number to write
int yPos; // vertical position on screen
int displayedLines; // number of linePtrs in this window
TSize textSize; // used to get char height in pixels
if (isMinimized) // don't write to an icon
return;
// get char sizes so that height is saved
textSize = dc.GetTextExtent("W", 1);
displayedLines = windowSize.cy / textSize.cy;
if (displayedLines > maxLines)
displayedLines = maxLines;
for (lineNum = yPos = 0; lineNum < displayedLines; ++lineNum)
{
if (lineLen[lineNum] > 0) // if any text on the line
dc.TextOut(0, yPos, linePtrs[lineNum], lineLen[lineNum]);
yPos += textSize.cy; // adjust screen line position
}
}
// called when a normal key is pressed
void
BaseWindow::EvChar(UINT key, UINT repeatCount, UINT)
{
BOOL invalidDisplay = FALSE;
BOOL eraseBackground = FALSE;
while (repeatCount--)
{
if ((key >= ' ') && (key <= '~')) // if a printable key
{
if (currentLine >= maxLines) // if buffer full
{
MessageBeep(-1); // complain
break;
}
else // else
{ // add char
linePtrs[currentLine][lineLen[currentLine]] = (char) key;
if (++lineLen[currentLine] >= maxWidth)
++currentLine;
invalidDisplay = TRUE;
}
}
else if (key == '\b') // rubout, delete char
{
if (currentLine >= maxLines)
break;
else if (lineLen[currentLine] == 0)
{
if (currentLine > 0)
--currentLine;
}
else
--lineLen[currentLine];
invalidDisplay = eraseBackground = TRUE;
}
else if (key == '\r') // if carriage return (Enter key)
++currentLine;
}
if (currentLine >= maxLines)
currentLine = maxLines - 1;
if (invalidDisplay) // if buffer has changed
Invalidate(eraseBackground); // force window repaint
}
void
MyApp::InitMainWindow()
{
SetMainWindow(new TFrameWindow(0, "Program 14.5",
new BaseWindow()));
GetMainWindow()->AssignMenu("MENU_1");
}
int
OwlMain(int, char **)
{
return MyApp().Run();
}