home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vos2-121.zip / v / texted / vedcmdw.cpp < prev    next >
C/C++ Source or Header  |  1998-10-13  |  15KB  |  566 lines

  1. //=======================================================================
  2. //@V@:Note: This file generated by vgen V1.00 (12:04:59 PM 23 Sep 1996).
  3. //    vedcmdw.cpp:    Source for vedCmdWindow class
  4. //=======================================================================
  5.  
  6. #include <v/vnotice.h>    // for vNoticeDialog
  7. #include <v/vkeys.h>    // to map keys
  8. #include <v/vfilesel.h>
  9. #include <v/vfontsel.h>
  10. #include <v/vutil.h>
  11. #include <v/vynreply.h>
  12. #include <v/vicon.h>
  13.  
  14. #include "vedapp.h"
  15. #include "vseeci.h"    // command interp
  16. #include "vedcmdw.h"    // our header
  17. #include "vedcnv.h"     // canvas
  18.  
  19.  
  20. //    Start defines for the main window with 100
  21.  
  22. //@V@:BeginIDs
  23.     enum {
  24.     m_FirstCmd = 100,         // Dummy Command
  25.     m_Options,            // set options
  26.         m_View,                // view a file R/O
  27.         m_EditHelp,
  28.     lblLineCol,            // Status Bar
  29.     lblIns,
  30.     lblSBlank,
  31.     lblMsg,
  32.     blkLast                // Last item
  33.       };
  34. //@V@:EndIDs
  35.  
  36. //@V@:BeginPulldownMenu FileMenu
  37.     static vMenu FileMenu[] =
  38.       {
  39.     {"&New", M_New, isSens, notChk, noKeyLbl, noKey, noSub},
  40.     {"&Open...", M_Open, isSens, notChk, noKeyLbl, noKey, noSub},
  41.         {"&View...", m_View, isSens, notChk, noKeyLbl, noKey, noSub},
  42.     {"&Save", M_Save, isSens, notChk, noKeyLbl, noKey, noSub},
  43.     {"Save &as...", M_SaveAs, isSens, notChk, noKeyLbl, noKey, noSub},
  44.     {"&Close", M_CloseFile, isSens, notChk, noKeyLbl, noKey, noSub},
  45.         {"Save and Close", M_SaveClose, isSens, notChk, noKeyLbl, noKey, noSub},
  46.     {"-", M_Line, notSens, notChk, noKeyLbl, noKey, noSub},
  47.     {"E&xit", M_Exit, isSens, notChk, noKeyLbl, noKey, noSub},
  48.     {NULL}
  49.       };
  50. //@V@:EndPulldownMenu
  51.     static vMenu EditMore[] =
  52.       {
  53.     {"Delete Line", edLineDelete, isSens,notChk,"^K",noKey,noSub},
  54.         {"Delete To Front of Line", edLineDeleteFront, isSens,notChk,"^ABkSp",noKey,noSub},
  55.     {"Delete To End of Line", edLineDeleteToEnd, isSens,notChk,"^ADel",noKey,noSub},
  56.     {"Fold case", edCharFoldCase, isSens,notChk,"Shift-^C",noKey,noSub},
  57.     {"Goto line", edLineGoto, isSens,notChk,"^G",noKey,noSub},
  58.     {"Ins Char Value", edCharInsert, isSens,notChk,"^AIns",noKey,noSub},
  59.     {"Open blank line", edLineOpen, isSens,notChk,"^O",noKey,noSub},
  60.     {"Repaint screen", edVerify, isSens,notChk,"^Av",noKey,noSub},
  61.         {NULL}
  62.       };
  63.  
  64. //@V@:BeginPulldownMenu EditMenu
  65.     static vMenu EditMenu[] =
  66.       {
  67.     {"Cut  ", M_Cut, notSens, notChk, "^X", noKey, noSub},
  68.     {"Copy ", M_Copy, notSens, notChk, "^C", noKey, noSub},
  69.     {"Paste", M_Paste, notSens, notChk, "^V", noKey, noSub},
  70.     {"-", M_Line, notSens, notChk, noKeyLbl, noKey, noSub},
  71.     {"Find", edFind, isSens,notChk,noKeyLbl,noKey,noSub},
  72.     {"Find Next", edFindNext, isSens,notChk,noKeyLbl,noKey,noSub},
  73.     {"Find Matching Paren", edBalMatch, isSens,notChk,noKeyLbl,noKey,noSub},
  74.     {"-", M_Line, notSens, notChk, noKeyLbl, noKey, noSub},
  75.         {"Edit Help", m_EditHelp, isSens, notChk, noKeyLbl, noKey, noSub},
  76.  
  77.     {NULL}
  78.       };
  79. //@V@:EndPulldownMenu
  80.  
  81. //@V@:BeginPulldownMenu OptMenu
  82.     static vMenu OptMenu[] =
  83.       {
  84.     {"Font", M_Font,isSens,notChk,noKeyLbl,noKey,noSub},
  85.     {NULL}
  86.       };
  87. //@V@:EndPulldownMenu
  88.  
  89. //@V@:BeginMenu StandardMenu
  90.     static vMenu StandardMenu[] =
  91.       {
  92.     {"&File", M_File, isSens, notUsed, notUsed, noKey, &FileMenu[0]},
  93.     {"&Edit", M_Edit, isSens, notUsed, notUsed, noKey, &EditMenu[0]},
  94.     {"&Options", m_Options, isSens, notUsed, notUsed, noKey, &OptMenu[0]},
  95.     {NULL}
  96.       };
  97. //@V@:EndMenu
  98.  
  99. //vbm1
  100. #define open_width 16
  101. #define open_height 16
  102. static unsigned char open_bits[] = {
  103.  0x00,0x00,0x00,0x0e,0x00,0x51,0x00,0x60,0x00,0x70,0x1c,0x00,0xe2,0x07,0x02,
  104.  0x04,0x02,0x04,0xc2,0x7f,0xa2,0x2a,0x52,0x15,0xaa,0x0a,0x56,0x05,0xfc,0x03,
  105.  0x00,0x00};
  106.     static vIcon openI(&open_bits[0],open_height,open_width);
  107.  
  108. //vbm1
  109. #define find_width 16
  110. #define find_height 16
  111. static unsigned char find_bits[] = {
  112.  0x00,0x00,0xf0,0x01,0x08,0x02,0x44,0x04,0xa2,0x08,0x12,0x09,0xf2,0x09,0x12,
  113.  0x09,0x14,0x05,0x08,0x0e,0xf0,0x1d,0x00,0x38,0x00,0x70,0x00,0x60,0x00,0x00,
  114.  0x00,0x00};
  115.     static vIcon findI(&find_bits[0],find_height,find_width);
  116.  
  117. //vbm1
  118. #define findagn_width 16
  119. #define findagn_height 16
  120. static unsigned char findagn_bits[] = {
  121.  0x00,0x00,0xf0,0x01,0x08,0x02,0x44,0x04,0xa2,0x08,0x12,0x09,0xf2,0x09,0x12,
  122.  0x09,0x14,0x05,0x08,0x0e,0xf0,0x1d,0x66,0x38,0xcc,0x70,0x98,0x61,0xcc,0x00,
  123.  0x66,0x00};
  124.     static vIcon findagnI(&findagn_bits[0],findagn_height,findagn_width);
  125.  
  126. //@V@:BeginCmdPane ToolBar
  127.     static CommandObject ToolBar[] =
  128.       {
  129.     {C_IconButton,M_Open,0,"Open",&openI,CA_None,isSens,NoFrame,0,0,0,"Open File"},
  130.     {C_Blank,99,0," ",NoList,CA_None,isSens,NoFrame,0,0},
  131.     {C_IconButton,edFind,0,"Find",&findI,CA_None,isSens,NoFrame,0,0,0,"Find"},
  132.     {C_IconButton,edFindNext,0,"Find Next",&findagnI,CA_None,isSens,NoFrame,0,0,0,"Find Next"},
  133.     {C_EndOfList,0,0,0,0,CA_None,0,0,0}
  134.       };
  135. //@V@:EndCmdPane
  136.  
  137. //@V@:BeginStatPane StatBar
  138.     static vStatus StatBar[] =
  139.       {
  140.     {"1/1      ", lblLineCol, CA_None, isSens, 0},
  141.     {"   Insert   ", lblIns, CA_None, isSens, 0},
  142.     {" ", lblSBlank, CA_NoBorder, isSens, 0},
  143.     // handle messages up to 40 chars
  144.     {"                                        ", lblMsg, CA_NoBorder, isSens, 0},
  145.     {0,0,0,0,0}
  146.       };
  147. //@V@:EndStatPane
  148.  
  149.  
  150.     static int filterIndex = 0;
  151.     static char* filter[] =
  152.       {
  153.         "*",
  154.         "*.txt",
  155.         "*.c;*.cpp;*.cxx;*.h",
  156.         0
  157.       };
  158.  
  159.  
  160. //====================>>> vedCmdWindow::vedCmdWindow <<<====================
  161.   vedCmdWindow::vedCmdWindow(char* name, int width, int height) :
  162.     vCmdWindow(name, width, height)
  163.   {
  164.     UserDebug1(Constructor,"vedCmdWindow::vedCmdWindow(%s) Constructor\n",name)
  165.  
  166.     // local inits
  167.  
  168.     fileName[0] = 0;
  169.  
  170.     // The Menu Bar
  171.     vedMenu = new vMenuPane(StandardMenu);
  172.     AddPane(vedMenu);
  173.  
  174.     // The Command Pane
  175.     vedCmdPane = new vCommandPane(ToolBar);
  176.     AddPane(vedCmdPane);
  177.  
  178.     // The Canvas
  179.     vedCanvas = new vedTextEditor((VCmdWindow*)this);
  180.     AddPane(vedCanvas);
  181.  
  182.     if (((vedApp*)theApp)->GetEmulation() == See)
  183.       {
  184.         vedCI = (vTextEdCmdInterp*) new vSeeCI(vedCanvas, this); // override interp
  185.                         // ed canvas will do delete at end
  186.         vedCanvas->ChangeCmdInterp(vedCI);
  187.       }
  188.     else
  189.     vedCI = 0;
  190.  
  191.     // The Status Bar
  192.     vedStatus = new vStatusPane(StatBar);
  193.     AddPane(vedStatus);
  194.  
  195.     // Associated dialogs
  196.  
  197.  
  198.     // Show Window
  199.  
  200.     ShowWindow();
  201.     vedCanvas->ShowVScroll(1);
  202. #ifdef V_VersionX
  203.     vedFont.SetFontValues(vfFixed,12);    // use Courier rathern than default
  204. #else
  205. #ifdef DEVEL
  206.     vedFont.SetFontValues(vfFixed,10,vfNormal,vfBold);    // use Courier rathern than default
  207. #else
  208.     vedFont.SetFontValues(vfFixed,10);    // use Courier rathern than default
  209. #endif
  210. #endif
  211.     vedCanvas->SetFont(vedFont);
  212.     vedCanvas->SetTextRowsCols(24,80);
  213.     (vedCanvas->GetCmdInterp())->InitCmdInterp();
  214.  
  215.   }
  216.  
  217. //====================>>> vedCmdWindow::~vedCmdWindow <<<====================
  218.   vedCmdWindow::~vedCmdWindow()
  219.   {
  220.     UserDebug(Destructor,"vedCmdWindow::~vedCmdWindow() destructor\n")
  221.  
  222.     // Now put a delete for each new in the constructor.
  223.  
  224.     delete vedMenu;
  225.     delete vedCanvas;
  226.     delete vedCmdPane;
  227.     delete vedStatus;
  228.   }
  229.  
  230. //====================>>> vedCmdWindow::ChangeLoc <<<====================
  231.   void vedCmdWindow::ChangeLoc(long line, int col)
  232.   {
  233.     char buff[20];
  234.     char colbuff[10];
  235.     char outbuff[20];
  236.     int ll;
  237.  
  238.     LongToStr(line,buff);
  239.  
  240.     IntToStr(col,colbuff);
  241.  
  242.     // center the line/col string
  243.  
  244.     int totalLen = strlen(buff) + strlen(colbuff) + 1;
  245.  
  246.     int add = (9 - totalLen) / 2;
  247.  
  248.     for (ll = 0 ; ll < add ; ++ll)
  249.     outbuff[ll] = ' ';
  250.     outbuff[ll] = 0;
  251.  
  252.     strcat(outbuff,buff); strcat(outbuff,"/");
  253.     strcat(outbuff,colbuff);
  254.  
  255.     // pad with trailing blanks so X doesn't shift things
  256.  
  257.     for (ll = strlen(outbuff) ; ll < 9 ; ++ll)
  258.     outbuff[ll] = ' ';
  259.     outbuff[ll] = 0;
  260.     SetString(lblLineCol,outbuff);
  261.  
  262.   }
  263.  
  264. //====================>>> vedCmdWindow::ChangeInsMode <<<====================
  265.   void vedCmdWindow::ChangeInsMode(int IsInsMode, char* msg)
  266.   {
  267.     if (msg != 0 && *msg != 0)
  268.         SetString(lblIns,msg);
  269.     else if ((vedCanvas->GetEdState()).readOnly)
  270.         SetString(lblIns,"  View  ");
  271.     else if (IsInsMode)
  272.     SetString(lblIns," Insert ");
  273.     else
  274.     SetString(lblIns,"Overtype");
  275.   }
  276.  
  277. //====================>>> vedCmdWindow::StatusMessage <<<====================
  278.   void vedCmdWindow::StatusMessage(char *msg)
  279.   {
  280.     char lineout[42];
  281.     int ix;
  282.  
  283.     // copy up to 40 chars
  284.     for (ix = 0 ; ix < 40 && msg[ix] ; ++ix)
  285.     lineout[ix] = msg[ix];
  286.  
  287.     // pad with trailing blanks
  288.     for ( ; ix < 40 ; ++ix)
  289.     lineout[ix] = ' ';
  290.  
  291.     lineout[ix] = 0;
  292.  
  293.     SetString(lblMsg,lineout);
  294.   }
  295.  
  296. //====================>>> vedCmdWindow::ErrorMsg <<<====================
  297.   void vedCmdWindow::ErrorMsg(char *str)
  298.   {
  299.       StatusMessage(str);
  300.   }
  301.  
  302. //====================>>> vedCmdWindow::KeyIn <<<====================
  303.   void vedCmdWindow::KeyIn(vKey keysym, unsigned int shift)
  304.   {
  305.     StatusMessage(" ");
  306.     if (!vedCanvas->EditKeyIn(keysym, shift))
  307.     vCmdWindow::KeyIn(keysym, shift);
  308.   }
  309.  
  310. //==============>>> vedCmdWindow::CheckClose <<<================
  311.   int vedCmdWindow::CheckClose(int ask)
  312.   {
  313.     // Checks to see if the user wants to save changes, if changes have been
  314.     // done, before it closes the application.
  315.     // Return 1 if ok to close now, 0 to abort
  316.  
  317.     if (vedCanvas->Changed())    // changes have been made
  318.       {
  319.     if (ask)
  320.       {
  321.         vYNReplyDialog ynr(this);
  322.         int ans = ynr.AskYN("Save new or changed file?");
  323.         if (ans == 0)
  324.           {
  325.         return 1;           // don't want to save
  326.           }
  327.         if (ans == -1)          // cancel
  328.         return 0;
  329.       }
  330.  
  331.         // ok, want to save changed file
  332.         if (*fileName)                    // have a name
  333.           {
  334.             if (!vedCanvas->SaveFile(fileName))        // Save in fileName
  335.               {
  336.                 vNoticeDialog note(this);       // for user notification
  337.                 note.Notice("Unable to save file");
  338.                 return 0;
  339.               }
  340.           }
  341.         else                            // need to request name
  342.           {
  343.             vFileSelect fsel(this);     // V file select dialog
  344.  
  345.             if (!fsel.FileSelectSave("Save file as",
  346.                 fileName,99,filter,filterIndex) || !*fileName)
  347.             return 0;
  348.  
  349.             if (!vedCanvas->SaveFile(fileName))        // Save in fileName
  350.               {
  351.                 vNoticeDialog note(this);       // for user notification
  352.                 note.Notice("Unable to save file");
  353.                 return 0;
  354.               }
  355.           }
  356.       }
  357.     return 1;
  358.   }
  359.  
  360. //====================>>> vedCmdWindow::WindowCommand <<<====================
  361.   void vedCmdWindow::WindowCommand(ItemVal id, ItemVal val, CmdType cType)
  362.   {
  363.     // Default: route menu and toolbar commands here
  364.  
  365.     UserDebug1(CmdEvents,"vedCmdWindow:WindowCommand(%d)\n",id)
  366.  
  367.  
  368.     StatusMessage(" ");
  369.     switch (id)
  370.       {
  371.     //@V@:Case M_New
  372.     case M_New:
  373.       {
  374.             vedCmdWindow* ncmdw;;
  375.             ncmdw = (vedCmdWindow*) theApp->NewAppWin(0,"V Text Editor", 100, 50);
  376.             if (ncmdw)        // If a new window, raise it
  377.               {
  378.                 ncmdw->RaiseWindow();
  379.               }
  380.         break;
  381.       }    //@V@:EndCase
  382.  
  383.     //@V@:Case M_Open
  384.         case m_View:
  385.     case M_Open:
  386.       {
  387.         char name[100] = ""; // start out with null name
  388.         vFileSelect fsel(this);     // make an instance
  389.  
  390.         int oans = fsel.FileSelect("Open file",name,99,filter,filterIndex);
  391.  
  392.         if (oans)
  393.           {
  394.         vedCmdWindow* cmdw = this;
  395.         if (*fileName != 0 || vedCanvas->Changed()) // already have file open
  396.           {
  397.             cmdw = (vedCmdWindow*) theApp->NewAppWin(0,"V Text Editor", 100, 50);
  398.             if (!cmdw)
  399.             break;
  400.                   }
  401.         if (!cmdw->OpenFile(name,(id == m_View)) )
  402.           {
  403.             vNoticeDialog note(this);
  404.             note.Notice("Unable to open specified file.");
  405.             if (cmdw != this)        // A new window?
  406.             delete cmdw;
  407.                     break;
  408.           }
  409.         else
  410.           {
  411.             if (cmdw != this)        // If a new window, raise it
  412.             cmdw->RaiseWindow();
  413.           }
  414.           }
  415.  
  416.         break;
  417.       }    //@V@:EndCase
  418.  
  419.     //@V@:Case M_Save
  420.     case M_Save:
  421.       {
  422.         CheckClose(0);
  423.         break;
  424.       }    //@V@:EndCase
  425.  
  426.     //@V@:Case M_SaveAs
  427.     case M_SaveAs:
  428.       {
  429.             vFileSelect fsel(this);     // V file select dialog
  430.  
  431.             if (!fsel.FileSelectSave("Save file as",
  432.                 fileName,99,filter,filterIndex) || !*fileName)
  433.             break;
  434.  
  435.             if (!vedCanvas->SaveFile(fileName))        // Save in fileName
  436.               {
  437.                 vNoticeDialog note(this);       // for user notification
  438.                 note.Notice("Unable to save file");
  439.               }
  440.         else
  441.         SetTitle(fileName);
  442.         break;
  443.       }    //@V@:EndCase
  444.  
  445.     //@V@:Case M_CloseFile
  446.     case M_CloseFile:
  447.       {
  448.         if (!CheckClose())
  449.         break;
  450.         *fileName = 0;            // no file now
  451.         CloseWin();
  452.         break;
  453.       }    //@V@:EndCase
  454.  
  455.     //@V@:Case m_SaveClose
  456.     case M_SaveClose:
  457.       {
  458.         if (!CheckClose(0))
  459.         break;
  460.         CloseWin();
  461.         break;
  462.       }    //@V@:EndCase
  463.  
  464.  
  465.     //@V@:Case M_Exit
  466.     case M_Exit:
  467.       {
  468.         theApp->Exit();
  469.         break;
  470.       }    //@V@:EndCase
  471.  
  472.  
  473.     //@V@:Case M_Font
  474.     case M_Font:
  475.       {
  476.         vFontSelect fd(this);
  477.  
  478.         (void) fd.FontSelect(vedFont);
  479.         vedCanvas->SetFont(vedFont);
  480.         break;
  481.       }    //@V@:EndCase
  482.  
  483.  
  484.     //@V@:Case M_Cut
  485.     case M_Cut:
  486.       {
  487.         vedCanvas->EditCommand(edCut,0);
  488.         break;
  489.       }    //@V@:EndCase
  490.  
  491.     //@V@:Case M_Copy
  492.     case M_Copy:
  493.       {
  494.         vedCanvas->EditCommand(edCopy,0);
  495.         break;
  496.       }    //@V@:EndCase
  497.  
  498.     //@V@:Case M_Paste
  499.     case M_Paste:
  500.       {
  501.         vedCanvas->EditCommand(edPaste,0);
  502.         break;
  503.       }    //@V@:EndCase
  504.  
  505.     //@V@:Case M_SetEmulation
  506.     case M_SetEmulation:
  507.       {
  508.         if ((Emulation) val == See)
  509.           {
  510.         vedCI = (vTextEdCmdInterp*) new vSeeCI(vedCanvas, this); // override interp
  511.         // ed canvas will do delete at end
  512.           }
  513.         else
  514.         vedCI = 0;
  515.  
  516.         vedCanvas->ChangeCmdInterp(vedCI);
  517.         (vedCanvas->GetCmdInterp())->InitCmdInterp();
  518.       }
  519.  
  520.     default:    // route unhandled commands through editor
  521.       {
  522.         if (vedCanvas->EditCommand(id, 1) < 0)
  523.         vCmdWindow::WindowCommand(id, val, cType);
  524.         break;
  525.       }
  526.       }
  527.   }
  528.  
  529. //====================>>> vedCmdWindow::OpenFile <<<====================
  530.   int vedCmdWindow::OpenFile(char* name, int ReadOnly)
  531.   {
  532.     if (!name || !*name)
  533.     return 0;
  534.  
  535.     if (!vedCanvas->ReadFile(name,1))
  536.       {
  537.     vNoticeDialog note(this);       // for user notification
  538.     note.Notice("Unable to open specified file");
  539.     return 0;
  540.       }
  541.     SetTitle(name);
  542.     strcpy(fileName,name);
  543.  
  544.     char* CExts[] = {".cpp", ".CPP", ".C", ".c", ".cxx", ".CXX",
  545.                ".h", ".H", ".hpp", ".HPP", ""};
  546.  
  547.     edState curState = vedCanvas->GetEdState();
  548.     curState.readOnly = ReadOnly;
  549.     curState.ftype = Text;            // assume Text
  550.     for (int ix = 0 ; *CExts[ix] ; ++ix)    // check C extensions
  551.       {
  552.         if (strstr(name,CExts[ix]) != 0)    // found one!
  553.           {
  554.             curState.ftype = CPP;
  555.             break;
  556.           }
  557.       }
  558.  
  559.     vedCanvas->SetEdState(curState);
  560.     vedCanvas->Verify();
  561.     
  562.     // ChangeInsMode(1);
  563.  
  564.     return 1;
  565.   }
  566.