home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / MAJ / 1513 / WEEXTSRC.ZI_ / WE_EXT.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  13KB  |  424 lines

  1. /*-------------------------------------------------------------------------*\
  2.  |                                                                         |
  3.  |                                                                         |
  4.  |  WE_EXT.C - A Sample DLL Extension Processor for WinEdit                |
  5.  |                                                                         |
  6.  |                                                                         |
  7. \*-------------------------------------------------------------------------*/
  8. #define STRICT
  9. #define _WINDLL
  10. #include <windows.h>
  11. #include "we_ext.h"
  12. #include "private.h"
  13. #include <string.h>
  14.  
  15. #define NOREF(a)  {a=a;}
  16.  
  17. HMENU hTrackMenu;
  18. BOOL bWait=TRUE;
  19. BOOL bCapture=TRUE;
  20. char szCommand[256];
  21.  
  22. /*
  23.  * JLD-12/9/92 Added this to setup the default GREP string, which recurs if
  24.  * no work is under the cursor.  See line 204.
  25.  */
  26.  
  27. char GLOB_GrepCmd[256] = {"tee.com fgrep.com -M %s *.c"};
  28.  
  29. #ifndef WIN32
  30. #define APIENTRY WINAPI
  31. #endif
  32.  
  33. #if !defined(LONG2POINT)
  34. #define LONG2POINT(l,pt) ((pt).x=(SHORT)LOWORD(l), (pt).y=(SHORT)HIWORD(l))
  35. #endif
  36.  
  37. /*-------------------------------------------------------------------------*\
  38.  |                                                                         |
  39.  |  Function:   WE_ExtensionProc                                           |
  40.  |                                                                         |
  41.  |  Purpose:    WinEdit calls this function with the WEN_* messages and    |
  42.  |              whenever a user-defined menu item or accelerator is        |
  43.  |              accessed.                                                  |
  44.  |                                                                         |
  45.  |  Parameters: HWND hWnd   - WinEdit's window handle                      |
  46.  |                                                                         |
  47.  |              UINT wParam - Message ID.  If wParam is >= WE_EXTFIRST,    |
  48.  |                            the DLL is being requested to perform the    |
  49.  |                            user-defined action.                         |
  50.  |                                                                         |
  51. \*-------------------------------------------------------------------------*/
  52. UINT APIENTRY WE_ExtensionProc(HWND hWnd,     /* WinEdit's window handle */
  53.                                  HANDLE hInst,  /* instance identifier     */
  54.                                  UINT wParam,   /* command ID              */
  55.                                  LONG lParam)   /* additional information  */
  56.    {
  57.  
  58.    switch (wParam)
  59.       {
  60.  
  61.       case WEN_LOADMENU:
  62.  
  63.          /*  This is the menu WinEdit will display when there
  64.           *  is at least one document window open.  Return NULL
  65.           *  to use the default WinEdit menu.
  66.           *
  67.           */
  68.  
  69.          return (UINT)LoadMenu(hInst, "MyMenu");
  70.          break;
  71.  
  72.       case WEN_LOADSHORTMENU:
  73.  
  74.          /*  this is the menu WinEdit will display when there
  75.              *  are no document windows open.  Return NULL
  76.              *  to use the default WinEdit menu.
  77.              *
  78.              */
  79.    
  80.             return (UINT)LoadMenu(hInst, "MyShortMenu");
  81.             return 0;
  82.             break;
  83.    
  84.       case WEN_LOADACCELS:
  85.  
  86.          /*  To add accelerators for new commands, load your
  87.           *  own accelerator table here.  Return NULL to
  88.           *  use the default WinEdit accelerators.
  89.           *
  90.           */
  91.  
  92.          return (UINT)LoadAccelerators (hInst,"MyAccels");
  93.          break;
  94.  
  95.       case WEN_GETWINDOWMENU:
  96.  
  97.          /*  WinEdit needs the handle of the submenu to
  98.           *  append MDI document names to.  The hWnd parameter
  99.           *  is used to send the handle to the main menu.
  100.           *  This message will not be sent if you return
  101.           *  NULL to the WEN_LOADMENU message.
  102.           */
  103.          return (UINT)GetSubMenu ((HMENU)hWnd, WINDOWMENU);
  104.          break;
  105.  
  106.       case WEN_RBUTTONDOWNS:
  107.          edHelpKeyWord(hWnd);
  108.          break;
  109.  
  110.       case WEN_RBUTTONDOWN:
  111.  
  112.          if (!hTrackMenu)
  113.             {
  114.  
  115.             hTrackMenu = CreatePopupMenu();
  116.             if (!hTrackMenu)
  117.                break;
  118.  
  119.  /* JLD 12/9/92 - Changed these labels to the Ctrl-V,X,Z, etc. which matches the 
  120.   *               current pull-down labels.
  121.   *               Also changed F3 to Previous error, F4 to next error, which seems
  122.   *               more consistent.  I left Re-Do as Ctrl-Backspace. 
  123.   *               Added Close to this menu.
  124.   *               Added Ctrl L as a synonym for Ctrl-F5.  (similar to Wordstar)
  125.   */ 
  126.                
  127.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEOPEN,"&Open...\tF3");
  128.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILESAVE,"&Save\tF2");                 
  129.             AppendMenu(hTrackMenu,MF_STRING,IDM_WINDOWCLOSE,"&Close\tCtrl+F4");                
  130.             AppendMenu(hTrackMenu,MF_STRING,IDM_FILEPRINT,"&Print\tF9");                
  131.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  132.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITUNDO,"&Undo\tCtrl+Z");           
  133.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITREDO,"&Redo\tCtrl+BkSp");          
  134.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  135.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCUT,"Cu&t\tCtrl+X");     
  136.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITCOPY,"&Copy\tCtrl+C");            
  137.             AppendMenu(hTrackMenu,MF_STRING,IDM_EDITPASTE,"&Paste\tCtrl+V");          
  138.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  139.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHFIND,"&Find...\tF5");              
  140.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXT,"&Repeat Last Find\tCtrl+F5|L");
  141.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHCHANGE,"&Change...\tF6");            
  142.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  143.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHPREVERR,"&Previous Error\tShift+F3");    
  144.             AppendMenu(hTrackMenu,MF_STRING,IDM_SEARCHNEXTERR,"&Next Error\tShift+F4");
  145.             AppendMenu(hTrackMenu,MF_SEPARATOR,0,0L);
  146.             AppendMenu(hTrackMenu,MF_STRING,IDM_HELPKEYWORDS,"&Key Word Help\tShift+F1");  
  147.             }
  148.             
  149.          if (hTrackMenu)
  150.             {
  151.             POINT pt;
  152. #ifdef WIN32
  153.             LONG2POINT((LPARAM)lParam,pt);
  154. #else
  155.             pt = MAKEPOINT(lParam);
  156. #endif
  157.             TrackPopupMenu(hTrackMenu,0,pt.x-10,pt.y-6,0,hWnd,0L);
  158.             }
  159.             
  160.          return TRUE;   
  161.          break;
  162.  
  163.       case WEN_END:
  164.  
  165.          /*  WinEdit is shutting down.  Do any clean-up processing
  166.           *  here.
  167.           */
  168.          if (hTrackMenu)
  169.             {
  170.             DestroyMenu(hTrackMenu);
  171.             hTrackMenu = (HMENU)NULL;
  172.             }
  173.          return TRUE;
  174.          break;
  175.  
  176.       case WEN_INITMENU:
  177.  
  178.          /*  This message is sent before showing any drop down
  179.           *  menu items.  Respond by setting any checkmarks,
  180.           *  graying any inapplicable items, etc.
  181.           *
  182.           */
  183.          return InitMenu(hWnd);
  184.          break;
  185.  
  186.  
  187.       /*  You can define your own commands in the range
  188.        *  WE_EXTFIRST to WE_EXTLAST that can be attached to
  189.        *  menu items or accelerators.
  190.        */
  191.  
  192.       case EXT_GREP:
  193.          {
  194.          char szWord[64];
  195.          edEditGetCurrentWord(hWnd,szWord,63);
  196.          if (szWord[0])
  197.             wsprintf(szCommand,"tee.com fgrep.com -M %s *.c",(LPSTR)szWord);
  198.          else
  199.             wsprintf(szCommand,GLOB_GrepCmd);  /* JLD 12/9/92 see beginning of file */
  200.  
  201. /* JLD 12/9/92 this *remembers* the last string if no word is selected */
  202.             
  203.          if (DialogBox(hInst,"CommandBox",hWnd,CommandDlgProc))
  204.             edRunCommand(hWnd, bWait, bCapture, szCommand);
  205.          strcpy(GLOB_GrepCmd,szCommand);  /* JLD 12/9/92 save current into the GLOB var */
  206.          return TRUE;
  207.          break;
  208.          }
  209. /* JLD 12/9/92 the following now provide braces for if and for, similar to the switch */
  210.  
  211.       case EXT_IF:
  212.          {
  213.          /* Ctrl+I:  'C' template for   if (  )
  214.                                         {
  215.                                         
  216.                                         }
  217.           */
  218.          UINT wColNo;
  219.          UINT wLineNo;
  220.  
  221.          edEditInsertString(hWnd,"if (  )\r{\r\r}");
  222.  
  223.          wColNo  = edGetColumnNumber(hWnd);
  224.          wLineNo = edGetLineNumber(hWnd);
  225.  
  226.          edEditGoToColumn(hWnd,wColNo+4);
  227.  
  228.          edEditGoToLine(hWnd,wLineNo-3);
  229.  
  230.          return TRUE;
  231.          break;
  232.          }
  233.  
  234.  
  235.       case EXT_FOR:         
  236.          {
  237.          /* Ctrl+F:  'C' template for   for ( ; ; )
  238.  
  239.           */
  240.          UINT wColNo;
  241.          UINT wLineNo;
  242.  
  243.          edEditInsertString(hWnd,"for ( ; ; )\r{\r\r}");
  244.  
  245.          wColNo  = edGetColumnNumber(hWnd);
  246.          wLineNo = edGetLineNumber(hWnd);
  247.  
  248.          edEditGoToColumn(hWnd,wColNo+5);
  249.  
  250.          edEditGoToLine(hWnd,wLineNo-3);
  251.  
  252.  
  253.          return TRUE;
  254.          break;
  255.          }
  256.  
  257.       case EXT_SWITCH:
  258.          {
  259.          /* Ctrl+S:  'C' template for   switch (  )
  260.                                            {
  261.  
  262.                                            }
  263.           */
  264.          UINT wColNo;
  265.          UINT wLineNo;
  266.  
  267.          edEditInsertString(hWnd,"switch (  )\r   {\r\r}");
  268.  
  269.          wColNo  = edGetColumnNumber(hWnd);
  270.          wLineNo = edGetLineNumber(hWnd);
  271.  
  272.          edEditGoToColumn(hWnd,wColNo+5);
  273.  
  274.          edEditGoToLine(hWnd,wLineNo-3);
  275.  
  276.          return TRUE;
  277.          break;
  278.          }
  279.  
  280.       default:
  281.  
  282.          /* return NULL to all messages not processed. */
  283.  
  284.          break;
  285.  
  286.       }  /* end switch (wParam) */
  287.  
  288.    return 0;
  289.  
  290.    }
  291.  
  292. #ifdef WIN32
  293. INT  APIENTRY LibMain(HANDLE hInst, DWORD ul_reason_being_called, LPVOID lpReserved)
  294. {
  295.    return 1;
  296.    UNREFERENCED_PARAMETER(ul_reason_being_called);
  297.    UNREFERENCED_PARAMETER(lpReserved);
  298. }
  299. #else
  300. BOOL APIENTRY LibMain(HANDLE hInstance, UINT wDataSeg, UINT cbHeap, LPSTR lpszCmdLine)
  301.    {
  302.    return TRUE;
  303.  
  304.    NOREF(hInstance);
  305.    NOREF(wDataSeg);
  306.    NOREF(cbHeap);
  307.    NOREF(lpszCmdLine);
  308.    }
  309. #endif
  310.  
  311. int FAR PASCAL WEP(int nParameter)
  312.    {
  313.    return TRUE;
  314.  
  315.    NOREF(nParameter);
  316.    }
  317.  
  318.  
  319. BOOL APIENTRY  CommandDlgProc(HWND hDlg, UINT msg, UINT wParam, LONG lParam)
  320.    {
  321.    switch (msg)
  322.       {
  323.       case WM_INITDIALOG:
  324.          CheckDlgButton(hDlg,IDD_WAIT,bWait);
  325.          CheckDlgButton(hDlg,IDD_CAPTURE,bCapture);
  326.          SetDlgItemText(hDlg,IDD_COMMAND,szCommand);
  327.  
  328.       case WM_COMMAND:
  329.          switch(wParam)
  330.             {
  331.             case IDOK:
  332.                bWait = IsDlgButtonChecked(hDlg,IDD_WAIT);
  333.                bCapture = IsDlgButtonChecked(hDlg,IDD_CAPTURE);
  334.                if (GetDlgItemText(hDlg,IDD_COMMAND,szCommand,sizeof(szCommand)))
  335.                   {
  336.                   EndDialog(hDlg,TRUE);
  337.                   break;
  338.                   }
  339.                /* else fall through */
  340.  
  341.             case IDCANCEL:
  342.                EndDialog(hDlg,FALSE);
  343.                break;
  344.  
  345.             default:
  346.                return (FALSE);
  347.             }
  348.          break;
  349.  
  350.       default:
  351.          return(FALSE);
  352.       }
  353.    return (TRUE);
  354.  
  355.    NOREF(lParam);
  356.    }
  357.  
  358.  
  359. int InitMenu(HWND hWnd)
  360.    {
  361.    UINT wStatus;
  362.    HMENU hCurrentMenu;
  363.    POINT ptStart,ptEnd;
  364.  
  365.    hCurrentMenu = GetMenu(hWnd);
  366.  
  367.    /*  if there is a current selection, enable the cut & copy
  368.     *  commands.
  369.     */
  370.    wStatus = (UINT)edGetSelectionState(hWnd, &ptStart, &ptEnd);
  371.    if (!wStatus)
  372.       wStatus = MF_GRAYED;
  373.    else
  374.       wStatus = MF_ENABLED;
  375.    EnableMenuItem(hCurrentMenu, IDM_EDITCUT,  wStatus);
  376.    EnableMenuItem(hCurrentMenu, IDM_EDITCOPY, wStatus);
  377.  
  378.    /*  if there is text on the clipboard, enable the paste
  379.     *  command.
  380.     */
  381.    if (OpenClipboard(hWnd))
  382.       {
  383.       if (IsClipboardFormatAvailable(CF_TEXT)
  384.             || IsClipboardFormatAvailable(CF_OEMTEXT))
  385.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_ENABLED);
  386.       else
  387.          EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  388.       CloseClipboard();
  389.       }
  390.    else
  391.       EnableMenuItem(hCurrentMenu, IDM_EDITPASTE, MF_GRAYED);
  392.  
  393.    /* set the Undo, Redo, Insert, and WordWrap menu items */
  394.    wStatus = (UINT)edGetUndoState(hWnd);
  395.    if (!wStatus)
  396.       wStatus = MF_GRAYED;
  397.    else
  398.       wStatus = MF_ENABLED;
  399.    EnableMenuItem(hCurrentMenu, IDM_EDITUNDO, wStatus);
  400.  
  401.    wStatus = (UINT)edGetRedoState(hWnd);
  402.    if (!wStatus)
  403.       wStatus = MF_GRAYED;
  404.    else
  405.       wStatus = MF_ENABLED;
  406.    EnableMenuItem(hCurrentMenu, IDM_EDITREDO, wStatus);
  407.  
  408.    wStatus = (UINT)edGetWordWrapState(hWnd);
  409.    if (!wStatus)
  410.       wStatus = MF_UNCHECKED;
  411.    else
  412.       wStatus = MF_CHECKED;
  413.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEWRAP, MF_BYCOMMAND|wStatus);
  414.  
  415.    wStatus = (UINT)edGetInsertState(hWnd);
  416.    if (!wStatus)
  417.       wStatus = MF_UNCHECKED;
  418.    else
  419.       wStatus = MF_CHECKED;
  420.    CheckMenuItem (hCurrentMenu, IDM_EDITTOGGLEINS, MF_BYCOMMAND|wStatus);
  421.  
  422.    return TRUE;   /* we handled it, don't return 0 */
  423.    }
  424.