home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
odtlktv4.zip
/
ODTLKT
/
TOOLKIT
/
SAMPLES
/
OPENDOC
/
PARTS
/
TTAPE1
/
TEXTDATA.CPP
< prev
next >
Wrap
Text File
|
1995-12-15
|
14KB
|
481 lines
/***********************************************************************
*
* File Name : TEXTDATA.CPP
*
* Description : Implementation of the TextData class. This class both
* maintains and displays the Ticker Tape text.
*
* Notes : N/A
*
* Entry Points: DefaultOutlineFont
* TextData::TextData
* TextData::DrawTape
* TextData::GetTextBuffer
* TextData::GetTextArea
* TextData::SetSize
* TextData::SetTextBuffer
* TextData::SetTextArea
*
* (C) COPYRIGHT International Business Machines Corp. 1995
* All Rights Reserved
* Licensed Materials - Property of IBM
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* DISCLAIMER OF WARRANTIES. The following [enclosed] code is
* sample code created by IBM Corporation. This sample code is not
* part of any standard or IBM product and is provided to you solely
* for the purpose of assisting you in the development of your
* applications. The code is provided "AS IS", without
* warranty of any kind. IBM shall not be liable for any damages
* arising out of your use of the sample code, even if they have been
* advised of the possibility of such damages.
*
***********************************************************************/
#include "textdata.hpp"
#include <stdio.h>
#include <memory.h>
#include <string.h>
/*
*
* Method(s) : DefaultOutlineFont
*
* Description : A utility routine which finds an outline font. The
* font must be proportional so it can be resized in the
* text box and also demonstrate proportional support.
*
* The routine initially attempts to install the "Helv"
* outline font in the Font record within the textArea
* text-maintenance structure.
*
* Parameters : TEXTAREA *textArea - the text maintenance record.
*
* Returns : VOID
*
* Notes : N/A
*/
void DefaultOutlineFont(TEXTAREA *textarea)
{
static FONTMETRICS fm[80]; /* The Font metrics information. */
LONG ltemp = 0;
LONG cFonts = 0;
int i;
/*
* Clear the text area's Font attributes record.
*/
memset(&textarea->fAttr, 0 ,sizeof(FATTRS));
/*
* Load all fonts from the font resource file "helv.fon".
*/
GpiLoadFonts(WinQueryAnchorBlock(HWND_DESKTOP), "helv");
/*
* Retrieve the number of fonts loaded by GpiLoadFonts,
* which have the "helv" typeface name.
*/
cFonts = GpiQueryFonts(textarea->hps, QF_PUBLIC|QF_PRIVATE, "Helv",
<emp, sizeof(FONTMETRICS), (PFONTMETRICS)0);
/*
* Limit the number of fonts retrieved to the number which can
* be stored in the "fm" local variable.
*/
if (cFonts > sizeof(fm)/sizeof(FONTMETRICS))
cFonts = sizeof(fm)/sizeof(FONTMETRICS);
/*
* Retrieve the font metrics for all private fonts having the
* "Helv" typeface name.
*/
GpiQueryFonts(textarea->hps, QF_PUBLIC|QF_PRIVATE, "Helv", &cFonts,
sizeof(FONTMETRICS), fm);
/*
* Find an outline font which is also kerned from the retrieved
* font metrics.
*/
for (i = 0; (!(fm[i].fsDefn&FM_DEFN_OUTLINE)) &&
(!(fm[i].fsType&FM_TYPE_KERNING)) && (i<cFonts); i++);
/*
* Find at least an outline font, if can't find one which is
* both an outline and a kerned font.
*/
if (i == cFonts)
for (i=0; (!(fm[i].fsDefn&FM_DEFN_OUTLINE))&& (i<cFonts); i++);
/*
* If can't find an outline font, use the first font found.
*/
if (i == cFonts)
i = 0;
/*
* Initialize the text area's font metrics record and font
* attributes record with the font information found above.
*/
textarea->fmFont = fm[i];
textarea->fAttr.usRecordLength = sizeof(FATTRS);
textarea->fAttr.lMatch = fm[i].lMatch;
textarea->fAttr.lMatch = 0;
textarea->fAttr.fsFontUse = FATTR_FONTUSE_OUTLINE;
strcpy(textarea->fAttr.szFacename, fm[i].szFacename);
}
/*
*
* Method(s) : TextData::TextData
*
* Description : TextData class constructor. Create and initialize
* the textArea data structure. Initialize the TextData
* class variables.
*
* Parameters : None.
*
* Returns : N/A
*
* Notes : N/A
*/
TextData::TextData (TextData *initData)
{
if (initData != NULL)
{
textBuffer = initData->textBuffer;
sTextFileSize = initData->GetTextFileSize();
textArea = initData->textArea;
sScrollPos = initData->GetScrollPos();
sGap = initData->GetGap();
}
else
{
textBuffer = NULL;
textArea = NULL;
sTextFileSize = 0;
sGap = 0;
sScrollPos = MAXSIZETEXTFILE +1;
}
/*
* Clear the text area data structure.
*/
if (textArea == NULL)
{
textArea = new TEXTAREA;
/*
* Set the text attributes within the text area.
*/
textArea->hps = WinGetScreenPS(HWND_DESKTOP);
textArea->lColorText = CLR_BLACK;
textArea->lBackText = CLR_RED;
textArea->lScrollRate = RATE;
textArea->sNeedToScroll = 1;
/*
* Install an outline font in the text data area.
*/
DefaultOutlineFont(textArea);
WinReleasePS(textArea->hps);
}
}
/*
*
* Method(s) : TextData::SetSize
*
* Description : Set the size of the textArea's rectlBox.
*
* Parameters : RECTL *rclFrame - new rectangle box size.
*
* Returns : VOID
*
* Notes : N/A
*/
void TextData::SetSize(RECTL *rclFrame)
{
memcpy(&textArea->rectlBox, rclFrame, sizeof(RECTL));
}
/*
*
* Method(s) : TextData::DrawTape
*
* Description : Draw the text within the textArea's rectangle box.
* Draw the text so that the height of the text fills
* the height of the box.
*
* Parameters HPS hps - PM presentation screen handle.
*
* Returns : VOID
*
* Notes : N/A
*/
void TextData::DrawTape(HPS hps)
{
SIZEF size; /* Character-box size in world coordinates. */
LONG lcid = 1; /* Identifier for font. */
POINTL ptl;
/*
* The number of characters which can fit within the text
* area's rectangle. This is recalculated to get the number
* of characters to write with GpiStringAt.
*/
short sNumCharInBox = 0;
/*
* Create the logical font identifed by lcid and the contents
* of the text area's FATTRS structure.
*/
GpiCreateLogFont(hps, (PSTR8)0, lcid, &textArea->fAttr);
/*
* The size values are shifted to make them FIXED.
*/
size.cy = MAKEFIXED(textArea->rectlBox.yTop, 0); /* Height. */
size.cx = MAKEFIXED(textArea->rectlBox.yTop, 0); /* Width. */
/*
* Set the size of character-box to the specified size value.
* Set the logical font identified by lcid.
*/
GpiSetCharBox(hps, &size);
GpiSetCharSet(hps, lcid);
/*
* Get the font metrics for the set character-box size and
* logical font.
*/
GpiQueryFontMetrics(hps, sizeof(textArea->fmFont), &textArea->fmFont);
/*
* If the text is to displayed at a new scroll position then do
* the following - which updates the "Gap" and "Scroll Position."
*/
if (textArea->sNeedToScroll || (sScrollPos > MAXSIZETEXTFILE))
{
if (textArea->sNeedToScroll)
{
/*
* Set color to background color for erasing previous text.
*/
GpiSetColor(hps, textArea->lBackText);
/*
* Erase previous text by filling box with background color.
*/
POINTL drawPtl;
drawPtl.x = 0L;
drawPtl.y = 0L;
GpiMove(hps, &drawPtl);
drawPtl.x = textArea->rectlBox.xRight;
drawPtl.y = textArea->rectlBox.yTop;
GpiBox(hps, DRO_FILL, &drawPtl, 0L, 0L);
}
/*
* If the Scroll Position is past the end of the text then
* set the Scroll Position to the first character in the
* text, and the starting text position to past the right
* end of the text area's rectangle.
*/
if (sScrollPos >= sTextFileSize)
{
sScrollPos = 0;
sGap = textArea->rectlBox.xRight - textArea->fmFont.lAveCharWidth;
}
/*
* If starting text position, or "Gap", is past the left side
* of the text area's rectangle, decrease "Gap" by the average
* width of one character. The "Gap" can't be negative.
*/
if (sGap > -textArea->fmFont.lAveCharWidth)
{
sGap -= textArea->fmFont.lAveCharWidth;
}
else
{
/*
* Otherwise, the text is scrolling past the left end
* of the rectangle. Increase the "Scroll Position",
* or postion of first character of text to display.
*/
++ sScrollPos;
}
/*
* Make sure the text is not written to the left of origin.
*/
if (sGap < -textArea->fmFont.lAveCharWidth)
sGap = -textArea->fmFont.lAveCharWidth;
}
/*
* Get the total number of characters that can fit in the box.
*/
sNumCharInBox = ((textArea->rectlBox.xRight - sGap)/
textArea->fmFont.lAveCharWidth) + 2;
/*
* Decrement the result above by the number of characters in the
* text from scroll position to the end of the string.
*/
if (sNumCharInBox > (sTextFileSize - sScrollPos))
sNumCharInBox = sTextFileSize - sScrollPos;
/*
* Center the text vertically.
*/
ptl.y = textArea->fmFont.lLowerCaseDescent;
/*
* Set the starting position of text within the Ticker Tape window.
*/
ptl.x = sGap;
/*
* Set the foreground color for writing the current text.
*/
GpiSetColor(hps, textArea->lColorText);
/*
* Write the Ticker Tape text to the presentation screen.
*/
GpiCharStringAt(hps,
(PPOINTL)&ptl,
(SHORT)sNumCharInBox,
textBuffer +sScrollPos);
}
/*
*
* Method(s) : TextData::GetTextBuffer
*
* Description : Get the Ticker Tape text stored in the text buffer.
*
* Parameters : int bufferIndex - starting position within buffer.
*
* Returns : char * - reference to the textBuffer string.
*
* Notes : N/A
*/
CHAR *TextData::GetTextBuffer(int bufferIndex) const
{
if (textBuffer != NULL)
return &textBuffer[bufferIndex];
else
return NULL;
}
/*
*
* Method(s) : TextData::SetTextBuffer
*
* Description : Set the text buffer to the new Ticker Tape text.
*
* Parameters : CHAR *text - reference to the new text.
* SHORT sInitGap - Initial position of text within the
* Ticker Tape window.
* SHORT sInitScrollPos - Position of first character of
* text to display within the
* Ticker Tape window.
*
* Returns : SHORT - 1, if text buffer set successful.
* - 0, if text buffer set failed.
*
* Notes : N/A
*/
SHORT TextData::SetTextBuffer(CHAR *text, SHORT sInitGap,
SHORT sInitScrollPos)
{
SHORT success; /* temporary boolean for returning success of */
/* setting the textBuffer to the new text. */
/*
* Delete the previous text stored in textBuffer.
*/
if (textBuffer != NULL) delete [] textBuffer;
sTextFileSize = strlen(text);
/*
* Re-create a new textBuffer and copy the new text into it.
*/
textBuffer = new CHAR [sTextFileSize+1];
if (textBuffer != NULL)
{
strcpy(textBuffer, text);
/*
* Initialize the location of the text, and the location of the
* first character to display, within the Ticker Tape window.
*/
sGap = sInitGap;
sScrollPos = sInitScrollPos;
success = 1;
}
else
{
/*
* Failed to allocate enough memory for the text buffer.
* Reset text buffer, buffer length, and scroll parameters.
*/
textBuffer = new CHAR [1];
textBuffer[0] = 0;
sTextFileSize = 0;
sGap = 0;
sScrollPos = 0;
success = 0;
}
return success;
}
/*
*
* Method(s) : TextData::GetTextArea
*
* Description : Get the Ticker Tape text attributes stored in the
* text area.
*
* Parameters : TEXTAREA &textOut - the text attributes stored in
* the text area.
*
* Returns : VOID
*
* Notes : N/A
*/
void TextData::GetTextArea(TEXTAREA &textOut)
{
memcpy(&textOut, textArea, sizeof(TEXTAREA));
}
/*
*
* Method(s) : TextData::SetTextArea
*
* Description : Set the Ticker Tape text attributes stored in the
* text area.
*
* Parameters : TEXTAREA textIn - the new text attributes.
*
* Returns : VOID
*
* Notes : N/A
*/
void TextData::SetTextArea(TEXTAREA textIn)
{
memcpy(textArea, &textIn, sizeof(TEXTAREA));
}