/* $Id: menu.cpp 2.4 1995/07/20 11:24:33 leon Exp leon $
 *
 *      GISEL - G-CODE ISEL CNC driver
 *
 *      Copyright (c) 1994 LECAD
 *      All Rights Reserved.
 *
 */

#define Uses_TApplication

#define Uses_TMenuBar

#define Uses_TRect

#define Uses_TSubMenu

#define Uses_fpstream

#define Uses_TKeys

#define Uses_TMenuItem

#define Uses_TStatusLine

#define Uses_TStatusItem

#define Uses_TStatusDef

#define Uses_TPoint

#define Uses_TEditor

#define Uses_MsgBox

#define Uses_TFileDialog

#define Uses_TDeskTop

#define Uses_TTerminal


#include <tvision\tv.h>


#include "gisel.h"

#include "giselhlp.h"


#include <stdarg.h>

#include <strstrea.h>

#include <iomanip.h>


/**
 ** Initiates application menu bar
 **/
TMenuBar *TGIselApp::initMenuBar( TRect r )
{

      TSubMenu& sub1 =
        *new TSubMenu( "~\360~", 0, hcSystem) +
        *new TMenuItem( "~T~erminal", cmTerminal, kbNoKey, hcTerminal ) +
        *new TMenuItem( "Controller ~L~og", cmLogCmd, kbNoKey, hcControllerLog ) +
        *new TMenuItem( "~C~alculator", cmCalculator, kbNoKey, hcCalculator); 

      TSubMenu& sub2 = *new TSubMenu( "~F~ile", kbAltF ) +
        *new TMenuItem( "~P~review", cmPreview, kbNoKey, hcSystem) + 
        *new TMenuItem( "~O~pen", cmOpen, kbF3, hcFOpen, "F3") +
        *new TMenuItem( "~N~ew", cmNew, kbNoKey ) +
        *new TMenuItem( "~S~ave", cmSave, kbF2, hcNoContext, "F2" ) +
        *new TMenuItem( "S~a~ve as...", cmSaveAs, kbNoKey ) +
        *new TMenuItem( "~C~lose", cmClose, kbCtrlF4, hcWClose, "Ctrl-F4" ) +
             newLine() +
        *new TMenuItem( "~C~hange dir...", cmChangeDrct, kbNoKey, hcFChangeDir ) +
        *new TMenuItem( "~D~OS shell", cmDosShell, kbNoKey, hcFDosShell ) +
        *new TMenuItem( "E~x~it", cmQuit, kbAltX, hcFExit,  "Alt-X" );

      TSubMenu& sub3 = *new TSubMenu( "~E~dit", kbAltE ) +
        *new TMenuItem( "~U~ndo", cmUndo, kbNoKey ) +
             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" ) +
        *new TMenuItem( "~S~how clipboard", cmShowClip, kbNoKey ) +
             newLine() +
        *new TMenuItem( "~C~lear", cmClear, kbCtrlDel, hcNoContext, "Ctrl-Del" );

      TSubMenu& sub4 = *new TSubMenu( "~S~earch", kbAltS ) +
        *new TMenuItem( "~F~ind...", cmFind, kbNoKey ) +
        *new TMenuItem( "~R~eplace...", cmReplace, kbNoKey ) +
        *new TMenuItem( "~S~earch again", cmSearchAgain, kbNoKey );


      TSubMenu& sub5 = *new TSubMenu( "~A~ction", kbAltA ) +
        *new TMenuItem( "~M~anual motion", cmManualMotion, kbNoKey, hcManualMotion ) +
        *new TMenuItem( "~J~ob manager", cmJobManagerCmd, kbNoKey, hcJobManager ) +
        *new TMenuItem( "~P~osition", cmPositionCmd, kbNoKey, hcPosition ) +
        *new TMenuItem( "~Z~ero Absolute", cmZeroAbsolute, kbNoKey, hcZeroAbsolute ) +
        *new TMenuItem( "~H~ome motors", cmHomeCmd, kbNoKey, hcHomeMotors );

      TSubMenu& sub6 = *new TSubMenu( "~T~ranslate", kbAltT , hcTranslate) +
        *new TMenuItem( "~F~ile...", cmTranslateFile, kbNoKey, hcTranslateFile) +
        *new TMenuItem( "~B~uffer", cmTranslateFile, kbNoKey, hcTranslateBuffer);


      TSubMenu& sub7 = *new TSubMenu( "~O~ptions", kbAltO ) +
        *new TMenuItem( "~C~omm Settings...", cmComSetup, kbNoKey, hcCommSettings ) +
        *new TMenuItem( "S~e~ttings...", cmSettings, kbNoKey, hcSettings ) +
        *new TMenuItem( "~H~oming speed...", cmHomingSpeed, kbNoKey, hcHomingSpeed ) +
        *new TMenuItem( "~L~imits...", cmHomingSpeed, kbNoKey, hcLimits ) +
        *new TMenuItem( "~M~achining...", cmMachining, kbNoKey, hcMachining ) +
         newLine() +
        *new TMenuItem( "~S~ave settings", cmSaveSettings, kbNoKey) +
        *new TMenuItem( "Save ~d~esktop", cmSaveCmd, kbNoKey) +
        *new TMenuItem( "~R~etrieve desktop", cmRestoreCmd, kbNoKey);


      TSubMenu& sub8 = *new TSubMenu( "~W~indows", kbAltW ) +
        *new TMenuItem( "~S~ize/move",cmResize, kbCtrlF5, hcWSizeMove, "Ctrl-F5" ) +
        *new TMenuItem( "~Z~oom", cmZoom, kbF5, hcWZoom, "F5" ) +
        *new TMenuItem( "~T~ile", cmTile, kbNoKey, hcWTile ) +
        *new TMenuItem( "C~a~scade", cmCascade, kbNoKey, hcWCascade ) +
        *new TMenuItem( "~N~ext", cmNext, kbF6, hcNoContext, "F6" ) +
        *new TMenuItem( "~P~revious", cmPrev, kbShiftF6, hcNoContext, "Shift-F6" ) +
        *new TMenuItem( "~C~lose", cmClose, kbCtrlF4, hcWClose, "Ctrl-F4" );

      TSubMenu& sub9 = *new TSubMenu( "~H~elp", kbAltH ) +
        *new TMenuItem( "I~n~tro", cmIntro, kbNoKey, hcIntro ) +
        *new TMenuItem( "~I~ndex", cmIndex, kbNoKey, hcIndex ) +
        *new TMenuItem( "~A~bout...", cmAboutBox, kbNoKey, hcSAbout);

    r.b.y = r.a.y + 1;
    return new TMenuBar( r, sub1 + sub2 + sub3 + sub4 
                        + sub5 + sub6 + sub7 + sub8 + sub9);
}

/**
 ** Initializes application status line (bottom line)
 **/
TStatusLine *TGIselApp::initStatusLine( TRect r )
{
    r.a.y = r.b.y-1;
    return new TStatusLine( r,
        *new TStatusDef( 0, 0xFFFF ) +
            *new TStatusItem( "~F1~ Help", kbF1, cmHelp ) +
            *new TStatusItem( "~Ctrl-F4~ Close", kbCtrlF4, cmClose ) +
            *new TStatusItem( "~F5~ Zoom", kbF5, cmZoom ) +
            *new TStatusItem( "~F6~ Next", kbF6, cmNext ) +
            *new TStatusItem( "~F10~ Menu", kbF10, cmMenu ) +
            *new TStatusItem( 0, kbCtrlF5, cmResize )
            );

}

/**
 ** Opens dialog box wit out of memory indicator
 **/
void TGIselApp::outOfMemory()
{
    messageBox("Not enough memory for this operation.", mfError | mfOKButton );
}

typedef char *_charPtr;
typedef TPoint *PPoint;

#pragma warn -rvl


ushort doEditDialog( int dialog, ... )
{
    va_list arg;

    char buf[80];
    ostrstream os( buf, sizeof( buf ) );
    switch( dialog )
        {
        case edOutOfMemory:
            return messageBox( "Not enough memory for this operation",
                               mfError | mfOKButton );
        case edReadError:
            {
            va_start( arg, dialog );
            os << "Error reading file " << va_arg( arg, _charPtr )
               << "." << ends;
            va_end( arg );
            return messageBox( buf, mfError | mfOKButton );
            }
        case edWriteError:
            {
            va_start( arg, dialog );
            os << "Error writing file " << va_arg( arg,_charPtr )
               << "." << ends;
            va_end( arg );
            return messageBox( buf, mfError | mfOKButton );
            }
        case edCreateError:
            {
            va_start( arg, dialog );
            os << "Error creating file " << va_arg( arg, _charPtr )
               << "." << ends;
            va_end( arg );
            return messageBox( buf, mfError | mfOKButton );
            }
        case edSaveModify:
            {
            va_start( arg, dialog );
            os << va_arg( arg, _charPtr )
               << " has been modified. Save?" << ends;
            va_end( arg );
            return messageBox( buf, mfInformation | mfYesNoCancel );
            }
        case edSaveUntitled:
            return messageBox( "Save untitled file?",
                               mfInformation | mfYesNoCancel );
        case edSaveAs:
            {
            va_start( arg, dialog );
            return execDialog( new TFileDialog( "*.*",
                                                "Save file as",
                                                "~N~ame",
                                                fdOKButton,
                                                101 ), va_arg( arg, _charPtr ) );
            }

        case edFind:
            {
            va_start( arg, dialog );
            return execDialog( createFindDialog(), va_arg( arg, _charPtr ) );
            }

        case edSearchFailed:
            return messageBox( "Search string not found.",
                               mfError | mfOKButton );
        case edReplace:
            {
            va_start( arg, dialog );
            return execDialog( createReplaceDialog(), va_arg( arg, _charPtr ) );
            }

        case edReplacePrompt:
            //  Avoid placing the dialog on the same line as the cursor

            TRect r( 0, 1, 40, 8 );
            r.move( (TProgram::deskTop->size.x-r.b.x)/2, 0 );
            TPoint t = TProgram::deskTop->makeGlobal( r.b );
            t.y++;
            va_start( arg, dialog );
            TPoint *pt = va_arg( arg, PPoint );
            if( pt->y <= t.y )
                r.move( 0, TProgram::deskTop->size.y - r.b.y - 2 );
            va_end( arg );
            return messageBoxRect( r, "Replace this occurence?",
                                   mfYesNoCancel | mfInformation );

        }
}

#pragma warn .rvl






syntax highlighted by Code2HTML, v. 0.9.1