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.
- //========================================================================
- // TVSAMPLE.CPP: Simple Turbo Vision File Editor
- // which demonstrates the use of a Clipboard in an Editor
- // (Cut, Paste, and Copy)
-
- #include <stdarg.h>
- #include <dir.h>
- #include <limits.h>
-
- #define Uses_MsgBox
- #define Uses_TApplication
- #define Uses_TCommandSet
- #define Uses_TDeskTop
- #define Uses_TDialog
- #define Uses_TEditor
- #define Uses_TEditWindow
- #define Uses_TEvent
- #define Uses_TFileDialog
- #define Uses_TFileEditor
- #define Uses_TKeys
- #define Uses_TMenuBar
- #define Uses_TMenuItem
- #define Uses_TProgram
- #define Uses_TStatusDef
- #define Uses_TStatusItem
- #define Uses_TStatusLine
- #define Uses_TSubMenu
- #include <tv.h> // include the defined classes
-
- #include "clokview.h" // for on-screen clock class
- #include "heapview.h" // for on-screen heap monitor
-
- // Command constants used by TVSAMPLE
- const unsigned cmAboutBox = 100;
- const unsigned cmNewFile = 101;
- const unsigned hcEditorOpen = 0x1000;
-
- // Function prototype for the standard editor dialog function
- ushort edDialog(int what, ...);
-
- //////////////////////////////////////////// MyTEditWindow ///////////////
- class MyTEditWindow : public TEditWindow {
- public:
- MyTEditWindow(const TRect &, const char *, int);
- virtual void sizeLimits(TPoint & min, TPoint & max);
- };
-
-
- // This is the main application class. By deriving from TApplication,
- // our application inherits most of the built in functionality of
- // Turbo Vision.
- //////////////////////////////////////////// TSampleApp //////////////////
- class TSampleApp : public TApplication {
- public:
- TSampleApp();
- static TMenuBar *initMenuBar(TRect r);
- static TStatusLine *initStatusLine(TRect r);
- void handleEvent(TEvent&);
- void idle(void);
- void updateCommands(void);
- void newFile(char *aName);
- Boolean closeEditor(Boolean destroyIt);
- void openFile(void);
- static void outOfMemory(void);
-
- protected:
- MyTEditWindow *theEditor;
- MyTEditWindow *theClipboard;
- Boolean editorOpen;
- TClockView *clock;
- };
-
- /*=========================================[ MyTEditWindow ]==============
- constructor -
-
- "Creates a 'TEditWindow' object that will edit the given file name
- with windows number 'aNumber'. Initializes a framed, tileable
- window with scroll bars and an indicator. If 'filename' is 0, it
- is assumed to be an untitled file. If 'editor' is equal to
- 'clipboard', the editor is assumed to be the clipboard.
- */
- MyTEditWindow::MyTEditWindow(const TRect & bounds,
- const char *fileName,
- int aNumber)
- :TEditWindow(bounds, fileName, aNumber),
- TWindowInit(&MyTEditWindow::initFrame) {
- return;
- }
-
- /*========================================== MyTEditWindow ===============
- sizeLimits - sets maximum vertical and horizontal height of the
- edit window.
-
- "Note that you do not have to call 'sizeLimits' explicitly. You
- just override it, and it will be called at the appropriate times.
- You set the limits, and the view know the appropriate times to
- check them. Since 'sizelimits' is virtual, the correct version for
- each view type is always called." TVUG pg 55
- */
-
- void MyTEditWindow::sizeLimits(TPoint & min, TPoint & max) {
- min.x = 20;
- min.y = 10;
-
- max.x = SHRT_MAX;
- max.y = SHRT_MAX;
- }
-
- /*=========================================[ TSampleApp ]=================
- constructor - do initialization of application-specific variables
- */
-
- TSampleApp::TSampleApp(void)
- :TProgInit(&TSampleApp::initStatusLine,
- &TSampleApp::initMenuBar,
- &TSampleApp::initDeskTop) {
-
- editorOpen = False;
-
- 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); // Insert clock into the desktop
-
- // Next, the clipboard is created. The clipboard is necessary to
- // support Cut, Paste, Copy, etc. in this application
- r = deskTop->getExtent();
- theClipboard = new MyTEditWindow(r, 0, wnNoNumber);
-
- if (validView(theClipboard)) {
- TEditor::clipboard = (TEditor *) theClipboard->editor;
- TEditor::clipboard->canUndo = False;
- theClipboard->hide();
- deskTop->insert(theClipboard);
- }
- }
-
- /*========================================== TSampleApp ==================
- initMenuBar - construct the application's menu
- */
-
- TMenuBar *TSampleApp::initMenuBar(TRect r) {
-
- TSubMenu& sub1 =
- *new TSubMenu("~\360~", kbAltSpace, hcNoContext) +
- *new TMenuItem("~A~bout...", cmAboutBox, kbNoKey, hcNoContext, 0);
-
- TSubMenu& sub2 =
- *new TSubMenu("~F~ile", kbAltF, hcNoContext) +
- *new TMenuItem("~N~ew", cmNewFile, kbNoKey, hcNoContext, 0) +
- newLine() +
- *new TMenuItem("~O~pen...", cmFileOpen, kbF3, hcNoContext, "F3") +
- *new TMenuItem("~S~ave", cmSave, kbF2, hcNoContext, "F2") +
- *new TMenuItem("Save ~a~s...", cmSaveAs, kbNoKey, hcNoContext, 0) +
- newLine() +
- *new TMenuItem("E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X");
-
- TSubMenu& sub3 =
- *new TSubMenu("~E~dit", kbAltE, hcNoContext)+
- *new TMenuItem("~U~ndo", cmUndo, kbNoKey, hcNoContext, 0) +
- newLine() +
- *new TMenuItem("Cu~t~", cmCut, kbShiftDel, hcNoContext, "Shift-Del") +
- *new TMenuItem("~C~opy", cmCopy, kbCtrlIns, hcNoContext, "Ctrl-Ins") +
- *new TMenuItem("~P~aste", cmPaste, kbShiftIns, hcNoContext, "Shift-Ins") +
- newLine() +
- *new TMenuItem("C~l~ear", cmClear, kbDel, hcNoContext, "Del");
-
- TSubMenu& sub4 =
- *new TSubMenu("~W~indow", kbAltW, hcNoContext)+
- *new TMenuItem("~R~esize", cmResize, kbCtrlF5, hcNoContext, "Ctrl-F5") +
- *new TMenuItem("~Z~oom", cmZoom, kbF5, hcNoContext, "F5") +
- newLine() +
- *new TMenuItem("~C~lose", cmClose, kbAltF3, hcNoContext, "Alt-F3");
-
- r.b.y = r.a.y + 1;
- return new TMenuBar(r, sub1 + sub2 + sub3 + sub4);
- }
-
- /*========================================== TSampleApp ==================
- initStatusLine - construct the application's status line(s). Note that
- the TStatusDef class specifies a help-context range
- for which a particular status line should be displayed.
- Thus, this application will display two different
- status lines depending on which help-context is active.
- */
-
- TStatusLine *TSampleApp::initStatusLine(TRect r) {
-
- r.a.y = r.b.y - 1;
-
- TStatusDef& statLine1 =
- *new TStatusDef(hcEditorOpen, hcEditorOpen) +
- *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem("~Alt-F3~ Close", kbAltF3, cmClose) +
- *new TStatusItem("~F2~ Save", kbF2, cmSave) +
- *new TStatusItem("~F3~ Open", kbF3, cmFileOpen) +
- *new TStatusItem(0, kbF10, cmMenu);
-
- TStatusDef& statLine2 =
- *new TStatusDef(0, 0xFFFF) +
- *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem(0, kbAltF3, cmClose) +
- *new TStatusItem(0, kbF10, cmMenu);
-
- return new TStatusLine(r, statLine1 + statLine2);
- }
-
- /*========================================== TSampleApp ==================
- idle - called by Turbo Vision during idle periods. It is used to
- update the clock view.
- */
-
- void TSampleApp::idle(void) {
-
- TApplication::idle();
- clock->update();
- }
-
- /*========================================== TSampleApp ==================
- updateCommands - called to determine if the File|Save and the
- File|Save as menu options should be available.
- */
-
- void TSampleApp::updateCommands(void) {
- TCommandSet cs;
-
- cs += cmSave;
- cs += cmSaveAs;
- if (editorOpen)
- deskTop->enableCommands(cs);
- else
- deskTop->disableCommands(cs);
- }
-
- /*========================================== TSampleApp ==================
- newFile - creates a new file editor (after closing any other open
- file editor) and inserts it into the desktop (makes it
- visible).
- */
-
- void TSampleApp::newFile(char *aName) {
-
- if (!closeEditor(True)) return;
-
- TRect r = deskTop->getExtent();
-
- r.a.x++;
- r.b.x -= 3;
- r.b.y -= 3;
- theEditor = new MyTEditWindow(r, aName, wnNoNumber);
- if (validView(theEditor)) {
- theEditor->helpCtx = hcEditorOpen;
- theEditor->editor->editorFlags &= ~(efBackupFiles);
- theEditor->editor->editorDialog = edDialog;
- deskTop->insert(theEditor);
- editorOpen = True;
- }
- updateCommands();
- }
-
- /*========================================== TSampleApp ==================
- closeEditor - called to close any open file editor. This function
- prompts the user to verify this action if there are un-
- saved changes present in the editor.
- */
-
- Boolean TSampleApp::closeEditor(Boolean destroyIt) {
- Boolean retVal = True;
-
- if (editorOpen)
- if (theEditor->editor->modified)
- retVal =
- (messageBox("\003You will lose your work if you proceed.",
- mfConfirmation | mfOKCancel) == cmOK) ? True : False;
- if (retVal) {
- if (destroyIt)
- if (editorOpen)
- destroy(theEditor);
- editorOpen = False;
- updateCommands();
- }
- return retVal;
- }
-
- /*========================================== TSampleApp ==================
- openFile - displays the standard file open dialog box. If a file is
- selected from the dialog, it is loaded.
- */
-
- void TSampleApp::openFile(void) {
- ushort control = cmCancel;
- char buf[MAXPATH];
-
- TFileDialog *fd =
- new TFileDialog("*.*", "Load a file", "~N~ame", fdOpenButton, 1);
- if (validView(fd)) control = execView(fd);
- if (control == cmFileOpen || control == cmOK) {
- if (closeEditor(True)) {
- fd->getFileName(buf);
- newFile(buf);
- }
- }
- destroy(fd);
- }
-
- /*========================================== TSampleApp ==================
- outOfMemory - Global out-of-memory handler
- */
-
- void TSampleApp::outOfMemory(void) {
-
- messageBox("\003Not enough memory to complete operation.",
- mfOKButton | mfError);
- }
-
-
- /*========================================== TSampleApp ==================
- handleEvent - sample application's handleEvent function. This
- function is used to implement the commands unique to
- this program. Turbo Vision calls this function whenever
- there is a message to be processed.
- */
-
- void TSampleApp::handleEvent(TEvent& event) {
-
- if (event.what & evCommand) {
- if ((event.message.command == cmClose) && (editorOpen))
- if (!closeEditor(False))
- clearEvent(event);
- }
-
- TApplication::handleEvent(event); // Call the original handleEvent
-
- if (event.what == evCommand) {
- switch (event.message.command) { // This might be a user command
- case cmNewFile:
- newFile(0);
- break;
-
- case cmFileOpen:
- openFile();
- break;
-
- case cmAboutBox:
- messageBox(mfInformation | mfOKButton,
- "\003Simple Turbo Vision Program\n\003\n" \
- "\003(C) 1991 Borland International");
- break;
-
- default:
- return;
- }
- clearEvent(event);
- }
- }
-
- /*------------------------------------------------------------------------
- execDialog - used to automatically handle:
- 1) the upload of program data to a dialog box
- 2) the modal execution of the dialog box
- 3) the download of data from the dialog box to the
- program
- 4) the destruction of the dialog box
- */
-
- ushort execDialog(TDialog *d, void *data) {
-
- TView *p = TProgram::application->validView(d);
- if (p == 0)
- return cmCancel;
- else {
- if (data != 0) p->setData(data);
- ushort result = TProgram::deskTop->execView(p);
- if (result != cmCancel && data != 0) p->getData(data);
- TObject::destroy(p);
- return result;
- }
- }
-
- /*------------------------------------------------------------------------
- edDialog - handles messages sent by the editor window. The editor
- window will call this function to handle certain special
- case situations (such as requests for file save and error
- conditions).
- */
-
- typedef char *_charPtr;
- ushort edDialog(int what, ...) {
- va_list arg;
-
- switch(what) {
- case edOutOfMemory:
- TSampleApp::outOfMemory();
- break;
-
- case edReadError:
- case edWriteError:
- case edCreateError:
- messageBox(mfOKButton | mfError, "\003Error during file I/O.");
- break;
-
- case edSaveAs:
- va_start(arg, what);
- return execDialog(new TFileDialog("*.*",
- "Save file as",
- "~N~ame",
- fdOKButton,
- 2), va_arg(arg, _charPtr));
- }
- return cmCancel;
- }
-
- //************************************************************************
- void main(int argc, char *argv[]) {
- TSampleApp myIDE; // construct application
- char fName[MAXPATH]; // buffer for file name
- char *theFile = NULL; // pointer to file name
-
- // If argument, it's assumed to be a file name.
- if (argc > 1) {
- strcpy(fName, argv[1]); // load into buffer
- fexpand(fName); //
- if (validFileName(fName)) // Check if it exists. If so...
- theFile = fName; // set pointer to it.
- }
-
- myIDE.newFile(theFile); //
- myIDE.run();
- }
-