home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- // The following example routines have been provided by the Technical
- // Support staff at Borland International. They are provided as a
- // courtesy and not as part of a Borland product, and as such, are
- // provided without the assurance of technical support or any specific
- // guarantees.
- //========================================================================
- //
- // myapp.cpp: The member function definition for TMyApp.
- //
-
- #define Uses_TApplication
- #define Uses_TRect
- #define Uses_TKeys
- #define Uses_TMenuBar
- #define Uses_TMenuItem
- #define Uses_TSubMenu
- #define Uses_TStatusLine
- #define Uses_TStatusDef
- #define Uses_TStatusItem
- #define Uses_TEvent
- #define Uses_TDialog
- #define Uses_TStaticText
- #define Uses_TButton
- #define Uses_TDeskTop
- #define Uses_TWindow
- #define Uses_TStreamable
- #define Uses_fpstream
- #define Uses_MsgBox
- #include <tv.h>
-
- __link( RWindow ); // TWindow is used.
- __link( RStaticText ); // TStaticText is used.
-
- #include <strstrea.h> // String stream library
- #include <stdlib.h> // Standard library
- #include <stdio.h> // For remove()
- #include <dir.h> // For findfirst()
-
- #include "gadgets.h" // TClockView and THeapView
- #include "cmds.h" // Command set
- #include "myapp.h" // Class definitions
- #include "funkview.h" // Class definitions
-
-
- //
- // TMyApp - Constructor.
- //
-
- TMyApp::TMyApp() :
- TProgInit( &TMyApp::initStatusLine,
- &TMyApp::initMenuBar,
- &TMyApp::initDeskTop )
- {
- TRect r = getExtent(); // Create the clock view.
- r.a.x = r.b.x - 9; r.b.y = r.a.y + 1;
- clock = new TClockView( r );
- insert(clock);
-
- r = getExtent(); // Create the heap view.
- r.a.x = r.b.x - 13; r.a.y = r.b.y - 1;
- heap = new THeapView( r );
- insert(heap);
- }
-
-
- //
- // initMenuBar - Initialize the menu bar. It will be called by the virtual base
- // TProgInit constructor.
- //
-
- TMenuBar *TMyApp::initMenuBar( TRect bounds )
- {
- bounds.b.y = bounds.a.y + 1;
-
- TMenuBar *mainMenu = new TMenuBar (bounds,
- *new TSubMenu ("~≡~", kbAltSpace ) + // ≡ is \xF0
- *new TMenuItem ("~A~bout...", cmAboutCmd, kbAltA, hcNoContext, "Alt-A") +
- newLine() +
- *new TMenuItem ("~E~xit", cmQuit, kbAltE, hcNoContext, "Alt-E") +
- *new TSubMenu("~W~indow", kbAltW ) +
- *new TMenuItem("~O~pen", cmOpenCmd, kbF2, hcNoContext, "F2") +
- *new TMenuItem("~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3") +
- newLine() +
- *new TMenuItem("~F~unky View", cmFunkCmd, kbNoKey) +
- *new TSubMenu("~O~ptions", kbAltO ) +
- *new TMenuItem("~S~ave desktop", cmSaveCmd, kbNoKey) +
- *new TMenuItem("~R~estore desktop", cmRestoreCmd, kbNoKey)
- );
-
- return( mainMenu );
- }
-
-
- //
- // initStatusLine - Initialize the status line. It will be called by the
- // virtual base TProgInit constructor.
- //
-
- TStatusLine *TMyApp::initStatusLine( TRect bounds )
- {
- bounds.a.y = bounds.b.y - 1;
-
- TStatusLine *statusLine = new TStatusLine (bounds,
- *new TStatusDef(0, 0xFFFF) +
- *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem("~Alt-A~ About", kbAltA, cmAboutCmd) +
- *new TStatusItem("~F2~ Open", kbF2, cmOpenCmd) +
- *new TStatusItem("~Alt-F3~ Close", kbF3, cmClose)
- );
-
- return (statusLine);
- }
-
-
- //
- // handleEvent - Need to handle the event for the menu and status line choice
- // cmAboutCmd.
- //
-
- void TMyApp::handleEvent(TEvent& event)
- {
- TApplication::handleEvent(event);
-
- switch(event.what)
- {
- case evCommand: // handle COMMAND events
- {
- switch(event.message.command)
- {
- case cmAboutCmd:
- {
- aboutDlg(); // Bring up the dialog box
- break;
- }
-
- case cmOpenCmd:
- {
- window(); // Bring up the window
- break;
- }
-
- case cmFunkCmd:
- {
- funkView(); // Bring up funky view
- break;
- }
-
- case cmSaveCmd:
- {
- saveDeskTop();
- break;
- }
-
- case cmRestoreCmd:
- {
- restoreDeskTop();
- break;
- }
-
- default:
- return;
- }
- clearEvent(event); // Clear the event we handled
- }
- }
- }
-
-
- //
- // aboutDlg - Creates a about dialog box and execute the dialog box.
- //
-
- void TMyApp::aboutDlg()
- {
- TDialog *aboutDlgBox = new TDialog(TRect(0, 0, 47, 10), "About");
- char temp[200];
-
- //
- // Use the strstream library to put the ABOUT text into a string. We
- // don't need << operators on the intermediate lines because the compiler
- // will concatenate the strings automatically. The ^C character begining
- // each line will force that line to be centered. The ' ' character between
- // each pair of newlines is to avoid a quirk that two adjacent newlines are
- // treated as one.
- //
-
- ostrstream(temp,200) << "\003streams.cpp\n \n"
- "\003C++ Version 1.5\n \n"
- "\003Copyright (C) Borland International, 1991."
- "\n \n"
- << ends;
-
- aboutDlgBox->insert( new TStaticText(TRect(2,2,45,9), temp) );
- aboutDlgBox->insert( new TButton(TRect(18,10,29,12), "OK", cmOK, bfDefault) );
-
- aboutDlgBox->options |= ofCentered; // Centered on the screen
-
- deskTop->execView( aboutDlgBox ); // Bring up the box as modal
-
- destroy( aboutDlgBox ); // Destroy the box
- }
-
-
- //
- // window() - Open a window with a view for my name in inside.
- //
-
- void TMyApp::window()
- {
- unsigned short x,y;
-
- x = random(79-30);
- y = random(23-7);
-
- TWindow *win = new TWindow(TRect(x,y,x+30,y+7), "Static Text", wnNoNumber);
-
- win->insert(new TStaticText(TRect(3,2,27,6),"\x3This space intentionally\n\x3left blank"));
-
- deskTop->insert(win);
- }
-
-
- //
- // idle() - handles updating the heap and clock views when the application
- // is not doing anything else.
- //
-
- void TMyApp::idle()
- {
- TProgram::idle();
- clock->update();
- heap->update();
- }
-
-
- static char *sayings[] = { " 42 ",
- " what do you get when you ",
- " multiply 7 by 9? ",
- " What is the answer to the ",
- " to the great question? "
- };
- const int numSayings = sizeof(sayings) / sizeof(char *);
-
- void TMyApp::funkView()
- {
- unsigned short x, y;
-
- x = random(79-FUNK_WIDTH);
- y = random(23-FUNK_HEIGHT);
-
- FunkyWindow *v = new FunkyWindow( TRect(x, y, x+FUNK_WIDTH, y+FUNK_HEIGHT),
- sayings[random(numSayings)]
- );
- deskTop->insert( v );
- }
-
-
- #define DESKTOP_FILE "stream.dst"
-
- // Helper functions for implementation of Save/Restore desktop.
-
- //
- // writeView() function ( writes a view object to a resource file )
- //
-
- static void writeView(TView *p, void *strm)
- {
- fpstream *s = (fpstream *) strm;
- if (p != TProgram::deskTop->last)
- *s << p;
- }
-
-
- //
- // closeView() function
- //
-
- static void closeView(TView *p, void *p1)
- {
- message(p, evCommand, cmClose, p1);
- }
-
-
- //
- // restoreDeskTop() function ( restores the previously stored DeskTop )
- //
-
- void TMyApp::restoreDeskTop()
- {
- struct ffblk ffblk;
-
- if (findfirst(DESKTOP_FILE, &ffblk, 0))
- messageBox("Could not find desktop file", mfOKButton | mfError);
- else
- {
- fpstream *f = new fpstream(DESKTOP_FILE, ios::in|ios::binary);
- if( !f )
- messageBox("Could not open desktop file", mfOKButton | mfError);
- else
- {
- TMyApp::loadDeskTop(*f);
- if( !f )
- messageBox("Error reading desktop file", mfOKButton | mfError);
- }
- delete f;
- }
- }
-
-
- //
- // loadDeskTop() function
- //
-
- void TMyApp::loadDeskTop(fpstream &s)
- {
- TView *p;
-
- if (deskTop->valid(cmClose))
- {
- deskTop->forEach(::closeView, 0); // Clear the desktop
- do
- {
- s >> p;
- deskTop->insertBefore(validView(p), deskTop->last);
- }
- while (p != 0);
- }
- }
-
-
- //
- // saveDeskTop() function ( saves the DeskTop by calling storeDeskTop function )
- //
-
- void TMyApp::saveDeskTop()
- {
- fpstream *f = new fpstream(DESKTOP_FILE, ios::out|ios::binary);
-
- if( f )
- {
- TMyApp::storeDeskTop(*f);
- if( !f )
- {
- messageBox("Could not create desktop file STREAM.DST", mfOKButton | mfError);
- delete f;
- ::remove(DESKTOP_FILE);
- return;
- }
- }
- delete f;
- }
-
-
- //
- // storeDeskTop() function ( stores the DeskTop in a resource file )
- //
-
- void TMyApp::storeDeskTop(fpstream& s) // Lesson J
- {
- deskTop->forEach(::writeView, &s);
- s << 0;
- }
-