home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CFGED1B.ZIP / CFGEDSRC.ZIP / LBMAIN.CC < prev    next >
C/C++ Source or Header  |  1992-08-29  |  13KB  |  454 lines

  1. // this might look like 'C', but it's really  -*-c++-*-
  2. /* lbmain.cc
  3.  *
  4.  * Implementation of class MainBrowserDlg for CFGED
  5.  *
  6.  * Language        : C++
  7.  * Operating System: OS/2 V2.0 and higher
  8.  * Compiler        : GNU GCC V2.1 and higher
  9.  *
  10.  *
  11.  * $Id: lbmain.cc,v 1.3 1992/08/29 16:12:03 gruen Exp $
  12.  * $Log: lbmain.cc,v $
  13. // Revision 1.3  1992/08/29  16:12:03  gruen
  14. // Fixed several bugs. This will become the official beta-release.
  15. //
  16. // Revision 1.2  1992/08/09  21:55:25  gruen
  17. // beta release 1.0 of cfged. Changed the architecture: now using a dialog
  18. // box as main window. As an result the accelerators now work. As goodie
  19. // there is a new special-browser for HELP and last but not least the
  20. // warning at the end of the program occures only if there not saved
  21. // changes to the configuration.
  22. //
  23. // Revision 1.1  1992/07/17  00:23:31  gruen
  24. // Initial revision
  25. //
  26.  *
  27.  * Copyright (c) 1992 Lutz Grueneberg
  28.  *
  29.  * This library is free software; you can redistribute it and/or modify
  30.  * it under the terms of the GNU Library General Public License as
  31.  * published by the Free Software Foundation; either version 2 of the
  32.  * License, or (at your option) any later version.  This library is
  33.  * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  34.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  35.  * A PARTICULAR PURPOSE.  See the GNU Library General Public License for
  36.  * more details. You should have received a copy of the GNU Library
  37.  * General Public License along with this library; if not, write to the
  38.  * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  39.  */
  40. #define INCL_PM
  41. #include <os2.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <pmviews/pmviews.h>
  46.  
  47. #include "cfgfile.h"
  48. #include "lbmain.h"
  49. #include "maindlg.h"
  50. #include "prompter.h"
  51. #include "pmctool.h"
  52. #include "sbrowser.h"
  53. #include "cfged.h"
  54. #include "about.h"
  55.  
  56. BOOL MainBrowserDlg::fChanged = FALSE; // no change by default 
  57.  
  58.      MainBrowserDlg::MainBrowserDlg( HWND hwndOwnerNew, 
  59.                     USHORT  idResNew,
  60.                                     HWND hwndParentNew=HWND_DESKTOP)
  61. :( hwndOwnerNew, idResNew, hwndParentNew) {
  62.           
  63.   plbMain = new ListBox( this, MAIN_LB);
  64.   pbtEdit   = new CntrlWindow( this, EDIT_BUTT);
  65.   pbtBrowse = new CntrlWindow( this, BROWSE_BUTT);
  66.   pbtInsert = new CntrlWindow( this, INSERT_BUTT);
  67.   pbtAppend = new CntrlWindow( this, APPEND_BUTT);
  68.   pbtDelete = new CntrlWindow( this, DELETE_BUTT);
  69.   pLdCfgDlg = NULL;
  70. }
  71.  
  72. VOID MainBrowserDlg::setState( int stateNew) {
  73.   switch( stateNew) {
  74.   case 0:     
  75.     plbMain->enable( TRUE);
  76.     pbtEdit->enable( FALSE);
  77.     pbtBrowse->enable( FALSE);
  78.     pbtInsert->enable( FALSE);
  79.     pbtAppend->enable( FALSE);
  80.     pbtDelete->enable( FALSE);
  81.     break;
  82.  
  83.   case 1:
  84.     plbMain->enable( TRUE);
  85.     pbtEdit->enable( TRUE);
  86.     pbtBrowse->enable( TRUE);
  87.     pbtInsert->enable( TRUE);
  88.     pbtAppend->enable( TRUE);
  89.     pbtDelete->enable( TRUE);
  90.     break;
  91.  
  92.   case 2:            // all disabled 
  93.     plbMain->enable( FALSE);
  94.     pbtEdit->enable( FALSE);
  95.     pbtBrowse->enable( FALSE);
  96.     pbtInsert->enable( FALSE);
  97.     pbtAppend->enable( FALSE);
  98.     pbtDelete->enable( FALSE);
  99.     break;
  100.   }
  101. }
  102.  
  103. VOID MainBrowserDlg::loadCfgList( VOID){
  104.   /* query the right CONFIG.SYS */
  105.   if( !pLdCfgDlg) 
  106.     pLdCfgDlg = new LoadConfigDlg( queryHwnd(), LDCFG_DLG);
  107.   pLdCfgDlg->create();
  108.   /* read the CONFIG.SYS */
  109.   cfile.clear();
  110.   cfile.read( (char*)(*(pLdCfgDlg->pSelected)));
  111.   cfile.printToListBox( plbMain);
  112.   setState(0);
  113. }
  114.  
  115. VOID MainBrowserDlg::saveCfgList( VOID){
  116.   CHAR   szBuf[300];
  117.   String str("c:\\config.old");
  118.  
  119.   sprintf( szBuf, "%s%s%s%s",
  120.       "Do you really want to save the actual configuration ",
  121.       "to the file '",
  122.       (char*)(*(pLdCfgDlg->pSelected)),
  123.       "'?");
  124.   if( PmUserRequest( HWND_DESKTOP, szBuf)) {
  125.     str[0] = (*(pLdCfgDlg->pSelected))[0];
  126.     cfile.save( (char*)(*(pLdCfgDlg->pSelected)), (char*)str);
  127.     sprintf( szBuf, "%s%s%s%s%s%s", 
  128.         "The actual configuration was stored to the file '",
  129.         (char*)(*(pLdCfgDlg->pSelected)),
  130.         "'.\nThe old configuration was saved to the file '",
  131.         (char*)str,
  132.             "'.\nThe changed version of the configuration takes",
  133.         "affect after the next shutdown!");
  134.     PmUserMessage( HWND_DESKTOP, szBuf);
  135.     fChanged = FALSE;        // clear change flag
  136.   }
  137.  
  138.   cfile.printToListBox( plbMain);
  139.   setState(0);
  140. }
  141.  
  142. VOID MainBrowserDlg::editEntry( VOID)
  143. { SHORT iSelect;
  144.   SHORT iTopIndex;
  145.   
  146.   // make shure that there is a selection
  147.   if( plbMain->querySelection( LIT_NONE) == LIT_NONE) return;
  148.   PrompterDlg Prompter( queryHwnd(), PR_DLG);
  149.   String test = "Hallo";
  150.   CHAR   *szComment="Edit the line, press OK to accept the change.";
  151.   iTopIndex = plbMain->queryTopIndex();
  152.   // find the selected line
  153.   Prompter.setString
  154.     (cfile.retrieve(iSelect=plbMain->querySelection( LIT_NONE)));
  155.   Prompter.setComment( szComment);
  156.   Prompter.create();        // control goes to dialog
  157.   if( Prompter.fChanged) fChanged = TRUE;
  158.   cfile.printToListBox( plbMain);
  159.   plbMain->selectItem( iSelect, TRUE);
  160.   plbMain->setTopIndex( iTopIndex);
  161.   setState( 1);
  162. }
  163.  
  164.  
  165. VOID MainBrowserDlg::browseEntry( SHORT iItem)
  166. { String    *pString;
  167.   String    strgNew;
  168.   StrDLList strList;
  169.   String    strgComment;
  170.   String    strgRemainer;
  171.   String*   pstrgTemp;
  172.   BOOL      fSemicolon = FALSE;
  173.  
  174.   // build list
  175.   pString = cfile.retrieve( iItem);
  176.   // verify browsability
  177.   if( !pString->contains( "=")) {
  178.     PmUserMessage( HWND_DESKTOP, 
  179.           "This entry can't be browsed.\nUse EDIT instead!");
  180.     return;
  181.   }
  182.   strgComment  = pString->before( "=");
  183.   strgRemainer = pString->after( "=");
  184.   while( strgRemainer.contains( ";")){
  185.     pstrgTemp = new String( strgRemainer.before( ";"));
  186.     strList.append( *pstrgTemp);
  187.     strgRemainer = strgRemainer.after( ";");
  188.   }
  189.   if( strgRemainer.length()) {
  190.     pstrgTemp = new String( strgRemainer);
  191.     strList.append( *pstrgTemp);
  192.   }
  193.   else fSemicolon = TRUE;
  194.   // display Dialog
  195.   SingleBrowser sb( queryHwnd(), SB_DLG, "SingleBrowser",
  196.            (char*)strgComment, &strList);
  197.   sb.create();
  198.   // assemble string
  199.   strgNew = strgComment;
  200.   strgNew += "=";
  201.   for(Pix i = strList.first();
  202.       i != 0;
  203.       strList.next(i)) {
  204.     if( i != strList.first()) strgNew += ";";
  205.     strgNew += strList(i);
  206.   }
  207.   if( fSemicolon) strgNew+=';';
  208.   if( strgNew != *pString) fChanged = TRUE;
  209.   *pString = strgNew;
  210. }
  211.     
  212. MRESULT MainBrowserDlg::msgInitDlg( HWND hwnd, USHORT msg, 
  213.                    MPARAM mp1, MPARAM mp2, BOOL *fDoDefault) {
  214.   SIZEL     sizl;
  215.   POINTL    ptl = {0,0};
  216.   StdWindow *pSW;
  217.   
  218.   //pSW->show( TRUE);        // display the complete window
  219.  
  220.   /* loadCfgList to ListBox */
  221.   loadCfgList();
  222.   //setPos( &ptl);
  223.   /* SetUp the position */
  224.   //pSW = queryOwnerWindow();
  225.   //querySize( &sizl);
  226.   //pSW->setClientPos( &ptl, &sizl);
  227.   return 0;
  228. }
  229.  
  230. MRESULT MainBrowserDlg::msgControl( HWND hwnd, USHORT msg, 
  231.                    MPARAM mp1, MPARAM mp2, BOOL *fDoDefault) {
  232.   USHORT usID;
  233.  
  234.   usID = SHORT1FROMMP(mp1);
  235.   *fDoDefault = TRUE;
  236.  
  237.   switch (SHORT2FROMMP(mp1)) {   
  238.   case LN_ENTER:  /* Auswahl eines Item oder Double-Click */
  239.     editEntry();
  240.     *fDoDefault = FALSE;
  241.     return 0;
  242.  
  243.   case LN_SELECT: /* selection of a item of the list */
  244.     setState( 1);
  245.     *fDoDefault = FALSE;
  246.     return 0;
  247.   }
  248.   return 0;
  249. }
  250.  
  251. // this method reacts on the click to buttons
  252. MRESULT MainBrowserDlg::msgCommand( HWND hwnd, USHORT msg, 
  253.                    MPARAM mp1, MPARAM mp2, BOOL *fDoDefault) {
  254.   if( SHORT1FROMMP( mp2) == CMDSRC_PUSHBUTTON) {   
  255.     switch (SHORT1FROMMP(mp1)) {   
  256.     case EDIT_BUTT:        // OK Button Pressed
  257.       editEntry();
  258.       *fDoDefault = FALSE;
  259.       return 0;               
  260.    
  261.     case BROWSE_BUTT:        // BROWSE Button Pressed
  262.       { SHORT iSelect;
  263.     SHORT iTopIndex;
  264.  
  265.     iTopIndex = plbMain->queryTopIndex();
  266.     browseEntry( iSelect = plbMain->querySelection( LIT_NONE));
  267.     cfile.printToListBox( plbMain);
  268.     plbMain->setTopIndex( iTopIndex);
  269.     plbMain->selectItem( iSelect, TRUE);
  270.     setState(1);
  271.       }
  272.       *fDoDefault = FALSE;
  273.       return 0;
  274.  
  275.     case INSERT_BUTT:        // INSERT Button pressed
  276.       {    PrompterDlg Prompter( queryHwnd(), PR_DLG);
  277.     SHORT i = plbMain->querySelection( LIT_NONE);// retrieve active Element
  278.     String strNew = "";
  279.     Prompter.setString( &strNew);
  280.     Prompter.setComment( "Enter new property:");
  281.     Prompter.create();
  282.     if( strNew.length()){
  283.       String *pStrg = new String( strNew);
  284.       cfile.strList.ins_before( (Pix)(cfile.retrievePix( i)), *pStrg);
  285.       fChanged = TRUE;
  286.     }
  287.     SHORT j = plbMain->queryTopIndex();
  288.     cfile.printToListBox( plbMain);
  289.     plbMain->setTopIndex(j);
  290.     plbMain->selectItem( i, TRUE);
  291.       }
  292.       *fDoDefault = FALSE;
  293.       return 0;
  294.  
  295.     case APPEND_BUTT:
  296.       // First this was implemented as an append at the end of the list.
  297.       // But this wasn't intuitive and so I've changed it. Now append means
  298.       // an insertion of an item after the actual one.
  299.       {    PrompterDlg Prompter( queryHwnd(), PR_DLG);
  300.     SHORT i = plbMain->querySelection( LIT_NONE);// retrieve active Element
  301.     String strNew = "";
  302.     Prompter.setString( &strNew);
  303.     Prompter.setComment( "Enter new property:");
  304.     Prompter.create();
  305.     if( strNew.length()){
  306.       String *pStrg = new String( strNew);
  307.       cfile.strList.ins_after( (Pix)(cfile.retrievePix( i)), *pStrg);
  308.       fChanged = TRUE;
  309.       SHORT j = plbMain->queryTopIndex();
  310.       cfile.printToListBox( plbMain);
  311.       plbMain->setTopIndex(j);
  312.       plbMain->selectItem( i+1, TRUE);
  313.     } else {
  314.       SHORT j = plbMain->queryTopIndex();
  315.       cfile.printToListBox( plbMain);
  316.       plbMain->setTopIndex(j);
  317.       plbMain->selectItem( i, TRUE);
  318.     }
  319.       }
  320.       *fDoDefault = FALSE;
  321.       return 0;
  322.  
  323.     case DELETE_BUTT:
  324.       {    SHORT iTopIndex;
  325.  
  326.     iTopIndex = plbMain->queryTopIndex();
  327.     SHORT i = plbMain->querySelection( LIT_NONE); // retrieve active
  328.     Pix p = i ? (Pix)cfile.retrievePix(i-1) : NULL;
  329.     cfile.strList.del_after( p);
  330.     fChanged = TRUE;
  331.     cfile.printToListBox( plbMain);
  332.     plbMain->setTopIndex( iTopIndex);
  333.     setState(0);
  334.       }
  335.       *fDoDefault = FALSE;
  336.       return 0;
  337.     }
  338.   }
  339.   if( SHORT1FROMMP( mp2) == CMDSRC_MENU||
  340.       SHORT1FROMMP( mp2) == CMDSRC_ACCELERATOR) {   
  341.     switch (SHORT1FROMMP(mp1)) {   
  342.     case IDM_OPEN:        // Save Menu selected
  343.       loadCfgList();
  344.       *fDoDefault = FALSE;
  345.       return 0;
  346.  
  347.     case IDM_EXIT:
  348.       WinPostMsg( hwnd, WM_CLOSE, 0L, 0L); // post WM_CLOSE message to yourself
  349.       *fDoDefault = FALSE;
  350.       return 0;
  351.  
  352.     case IDM_SAVE:        // Save Menu selected
  353.       saveCfgList();
  354.       *fDoDefault = FALSE;
  355.       return 0;
  356.  
  357.     case IDM_ABOUT:        // OK Button Pressed
  358.       AboutDlg about( queryHwnd(), ABOUT_DLG);
  359.       about.create();        // blocks until OK
  360.       *fDoDefault = FALSE;
  361.       return 0;
  362.  
  363.     case IDM_PATH:        // Browse Path button pressed
  364.       {
  365.     Regex rp( "\\(SET[ \n\t]+PATH[ \n\t]*=\\)");
  366.     browseVariable( &rp);
  367.       }
  368.       *fDoDefault = FALSE;
  369.       return 0;
  370.  
  371.     case IDM_DPATH:        // Browse DataPath button pressed
  372.       {
  373.     Regex rp( "\\(SET[ \n\t]+DPATH[ \n\t]*=\\)");
  374.     browseVariable( &rp);
  375.       }
  376.       *fDoDefault = FALSE;
  377.       return 0;
  378.  
  379.     case IDM_LIBPATH:        // Browse LibraryPath button pressed
  380.       {
  381.     Regex rp( "\\(LIBPATH[ \n\t]*=\\)");
  382.     browseVariable( &rp);
  383.       }
  384.       *fDoDefault = FALSE;
  385.       return 0;
  386.  
  387.     case IDM_HELP:        // Browse HelpPath button pressed
  388.       {
  389.     Regex rp( "\\(SET[ \n\t]+HELP[ \n\t]*=\\)");
  390.     browseVariable( &rp);
  391.       }
  392.       *fDoDefault = FALSE;
  393.       return 0;
  394.  
  395.     }
  396.   }
  397.   return 0;
  398. }
  399.  
  400. MRESULT MainBrowserDlg::msgClose(HWND hwnd, USHORT msg, 
  401.              MPARAM mp1, MPARAM mp2, BOOL *fDoDefault)
  402. {
  403.   *fDoDefault = FALSE;        // no default processing required
  404.   if( fChanged) {        // last chance to save your work
  405.     if( PmUserRequest( HWND_DESKTOP, 
  406.                "You have changed, but not saved the configuration.\nDo you really want to exit the OS/2 configuration editor?")) {
  407.       // closing the application is realized by quitting the message queue
  408.       WinPostMsg( hwnd, WM_QUIT, 0L, 0L); // close this application 
  409.     }
  410.   }
  411.   else WinPostMsg( hwnd, WM_QUIT, 0L, 0L); // close this application
  412.   return 0;
  413. }
  414.  
  415. MRESULT MainBrowserDlg::msgActivate( HWND hwnd, USHORT msg, 
  416.                      MPARAM mp1, MPARAM mp2, BOOL *fDoDefault)
  417. {
  418.   //DosBeep( 100, 100);
  419.   //PmUserMessage( "%d %d", (ULONG)hwnd, (ULONG)queryHwnd());
  420.   //if( hwnd == queryHwnd()) DosBeep( 150, 100);
  421.   //WinEnableWindow( hwnd, (BOOL) SHORT1FROMMP(mp1));
  422.   if( (BOOL) SHORT1FROMMP(mp1)) {
  423.     if( plbMain->querySelection(LIT_NONE) == LIT_NONE) setState( 0);
  424.     else setState(1);
  425.   }
  426.   else setState( 2);
  427.   *fDoDefault = TRUE;
  428.   return 0;
  429. }
  430.  
  431.  
  432.  
  433. VOID MainBrowserDlg::browseVariable( Regex *pr)
  434. {
  435.   String str;
  436.   SHORT  j;
  437.   Pix    i;
  438.  
  439.   for(i = cfile.strList.last(), j = cfile.strList.length() - 1;
  440.       i != 0 && j >= 0;
  441.       cfile.strList.prev(i), j--) {
  442.     str = cfile.strList(i); // copy entry
  443.     str = upcase( str);          // set upcase
  444.     if( str.contains( *pr)) {
  445.       // browse entry
  446.       browseEntry( j);
  447.       cfile.printToListBox( plbMain);
  448.       setState(0);
  449.       break;
  450.     }
  451.   }
  452. }
  453.  
  454.