home *** CD-ROM | disk | FTP | other *** search
/ TopWare Tools / TOOLS.iso / tools / top1089 / files1.lzh / LOCALOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-29  |  5.5 KB  |  187 lines

  1. /*-------------------------------------------------
  2.    LOCALOPT.C -- Extension DLL for E! - version 1.0
  3.  
  4.    To compile: nmake /f localopt.mak
  5.  
  6.    Once compiled, copy LOCALOPT.EWD to your USER directory.
  7.  
  8.    To use this DLL simply load it from the user menu or add its name to the
  9.    list of autoloaded Extension DLLs by using the Autoload dialog box from
  10.    the User Menu of E!. That's all.
  11.  
  12.    LocalOpt allows to toggle 3 important local options (AutoInsert,
  13.    AutoTab and AutoIndent) from the User Menu or more usefully with
  14.    a keystroke instead of setting these values from the Local
  15.    Options dialog box.
  16.  
  17.    The options are changed for the current Editor only.
  18.  
  19.    If you want to attach these functions to keystrokes, please use
  20.    Routine Id = 1 for the AutoInsert toggle, RoutineId = 2 for the
  21.    AutoTab toggle and RoutineId = 3 for the AutoIndent toggle in the
  22.    Keyboard Assignment dialog box.
  23.  
  24.    If you are not interested in having the menu items defined by LocalOpt
  25.    appear in the User Menu, create a [localopt] section in EW.INI and
  26.    add the following entry:
  27.  
  28.       addtomenu=no
  29.  
  30.    This way, you'll only be able to assign the different LocalOpt routines
  31.    to a keystroke but the User Menu will not be affected. In that case,
  32.    the FuncExitHook is useless and will not be installed.
  33.  
  34.    If you change these options from the Local Options dialog box, the
  35.    User Menu will keep in synch.
  36.  
  37.    This is a sample code. It will be easy to apply this code to other
  38.    options, either Local or Global.
  39.  
  40.   ------------------------------------------------*/
  41.  
  42. #include <windows.h>
  43. #include "ewapi.h"
  44. #include "string.h"
  45.  
  46. #define nInsertRoutineId 1
  47. #define nTabRoutineId     2
  48. #define nIndentRoutineId 3
  49.  
  50. long lInsertEntryId, lTabEntryId, lIndentEntryId;
  51.  
  52. char *pszInsertSuffix = "AutoInsert";
  53. char *pszTabSuffix = "AutoTab";
  54. char *pszIndentSuffix = "AutoIndent";
  55.  
  56. char *pszEnablePrefix = "Enable ";
  57. char *pszDisablePrefix = "Disable ";
  58.  
  59. char szInsertTitle[20];
  60. char szTabTitle[20];
  61. char szIndentTitle[20];
  62.  
  63. BOOL bAddToMenu;
  64. char *pszDefOption = "yes";
  65.  
  66.  
  67.  
  68. void MakeTitles()
  69. // Prepare title strings for insertion in the User Menu
  70.  
  71. {
  72.   if  (EWGetEditorLocalFlag(EWLclbAutoInsert))
  73.     _fstrcpy(szInsertTitle, pszDisablePrefix);
  74.   else
  75.     _fstrcpy(szInsertTitle, pszEnablePrefix);
  76.   _fstrcat(szInsertTitle, pszInsertSuffix);
  77.  
  78.   if  (EWGetEditorLocalFlag(EWLclbAutoTab))
  79.     _fstrcpy(szTabTitle, pszDisablePrefix);
  80.   else
  81.     _fstrcpy(szTabTitle, pszEnablePrefix);
  82.   _fstrcat(szTabTitle, pszTabSuffix);
  83.  
  84.   if  (EWGetEditorLocalFlag(EWLclbAutoIndent))
  85.     _fstrcpy(szIndentTitle, pszDisablePrefix);
  86.   else
  87.     _fstrcpy(szIndentTitle, pszEnablePrefix);
  88.   _fstrcat(szIndentTitle, pszIndentSuffix);
  89. }
  90.  
  91. void RemoveMenuItems()
  92. // Delete items from the User Menu
  93.  
  94. {
  95.   if (lInsertEntryId)
  96.     EWRemoveMenuEntry(lInsertEntryId);
  97.   if (lTabEntryId)
  98.     EWRemoveMenuEntry(lTabEntryId);
  99.   if (lIndentEntryId)
  100.     EWRemoveMenuEntry(lIndentEntryId);
  101. }
  102.  
  103. void AddMenuItems()
  104. // Add Items to the User Menu
  105.  
  106. {
  107.   RemoveMenuItems();
  108.   MakeTitles();
  109.   lInsertEntryId = EWAddMenuEntry("localopt", szInsertTitle, 0, EWMNU_Extension, nInsertRoutineId);
  110.   lTabEntryId = EWAddMenuEntry("localopt", szTabTitle, 0, EWMNU_Extension, nTabRoutineId);
  111.   lIndentEntryId = EWAddMenuEntry("localopt", szIndentTitle, 0, EWMNU_Extension, nIndentRoutineId);
  112. }
  113.  
  114. int FAR PASCAL _export FuncExitHook(unsigned int command, int FAR* pRetCode)
  115. // This hook allows to keep the Local Options dialog box and the User Menu
  116. // in synch. After the ew_MLSet command has been executed (Open the Local
  117. // Options dialog box), this hook is called by E!.
  118.  
  119. {
  120.   if  (command == ew_MLSet)
  121.     AddMenuItems();
  122.   return(0);
  123. }
  124.  
  125. int FAR PASCAL _export EWExecute(unsigned int RoutineId)
  126. // EWExecute uses the Menu Dispatching feature to decide which toggle has
  127. // to be changed.
  128.  
  129. {
  130.   switch (RoutineId) {
  131.     case nInsertRoutineId: // AutoInsert toggle selected in the User Menu
  132.     {
  133.       EWSetEditorLocalFlag(EWLclbAutoInsert, !(BOOL)EWGetEditorLocalFlag(EWLclbAutoInsert));
  134.       break;
  135.     }
  136.     case nTabRoutineId:    // AutoTab toggle selected in the User Menu
  137.     {
  138.       EWSetEditorLocalFlag(EWLclbAutoTab, !(BOOL)EWGetEditorLocalFlag(EWLclbAutoTab));
  139.       break;
  140.     }
  141.     case nIndentRoutineId: // AutoIndent toggle selected in the User Menu
  142.     {
  143.       EWSetEditorLocalFlag(EWLclbAutoIndent, !(BOOL)EWGetEditorLocalFlag(EWLclbAutoIndent));
  144.       break;
  145.     }
  146.     default:  // We do have a serious problem!
  147.       return(ewerr_EXTFAILED);
  148.   }
  149.   if (bAddToMenu)
  150.     AddMenuItems(); // Update User Menu items.
  151.   return(0);
  152. }
  153.  
  154. int FAR PASCAL _export WEP(int nParameter)
  155.  
  156. {
  157.   if (bAddToMenu)
  158.   {
  159.     // On Exit remove User Menu items...
  160.     RemoveMenuItems();
  161.     // ...and unregister the hook (otherwise a GPF is guaranteed)
  162.     EWRemoveHook(EWHook_FunctionExit, FuncExitHook);
  163.   }
  164.   return(1);
  165. }
  166.  
  167. int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize,
  168.             LPSTR lpszCmdLine)
  169. {
  170.   char szOptionValue[4];
  171.  
  172.   if (wHeapSize > 0)
  173.     UnlockData (0) ;
  174.   // Check EW.INI to see whether User Menu Insertion is allowed
  175.   GetPrivateProfileString("localopt", "addtomenu", pszDefOption, szOptionValue, 4, "ew.ini");
  176.   bAddToMenu = (BOOL)(_fstricmp(szOptionValue, pszDefOption) == 0);
  177.   if (bAddToMenu)
  178.   {
  179.     // On entry, add the User Menu items...
  180.     AddMenuItems();
  181.     // ... and install the function exit hook.
  182.     EWSetHook(EWHook_FunctionExit, FuncExitHook);
  183.   }
  184.   return 1 ;
  185. }
  186.  
  187.