home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / w16gui / popfind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.0 KB  |  131 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*--------------------------------------------------------
  20.    POPFIND.C -- Popup Editor Search and Replace Functions
  21.    (c) Charles Petzold, 1992
  22.   --------------------------------------------------------*/
  23.  
  24. #include <windows.h>
  25. #include <commdlg.h>
  26. #include <string.h>
  27. #define MAX_STRING_LEN   256
  28.  
  29. static char szFindText [MAX_STRING_LEN] ;
  30. static char szReplText [MAX_STRING_LEN] ;
  31.  
  32. HWND PopFindFindDlg (HWND hwnd)
  33.      {
  34.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  35.  
  36.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  37.      fr.hwndOwner        = hwnd ;
  38.      fr.hInstance        = NULL ;
  39.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  40.      fr.lpstrFindWhat    = szFindText ;
  41.      fr.lpstrReplaceWith = NULL ;
  42.      fr.wFindWhatLen     = sizeof (szFindText) ;
  43.      fr.wReplaceWithLen  = 0 ;
  44.      fr.lCustData        = 0 ;
  45.      fr.lpfnHook         = NULL ;
  46.      fr.lpTemplateName   = NULL ;
  47.  
  48.      return FindText (&fr) ;
  49.      }
  50.  
  51. HWND PopFindReplaceDlg (HWND hwnd)
  52.      {
  53.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  54.  
  55.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  56.      fr.hwndOwner        = hwnd ;
  57.      fr.hInstance        = NULL ;
  58.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  59.      fr.lpstrFindWhat    = szFindText ;
  60.      fr.lpstrReplaceWith = szReplText ;
  61.      fr.wFindWhatLen     = sizeof (szFindText) ;
  62.      fr.wReplaceWithLen  = sizeof (szReplText) ;
  63.      fr.lCustData        = 0 ;
  64.      fr.lpfnHook         = NULL ;
  65.      fr.lpTemplateName   = NULL ;
  66.  
  67.      return ReplaceText (&fr) ;
  68.      }
  69.  
  70. BOOL PopFindFindText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE lpfr)
  71.      {
  72.      int         iPos ;
  73.      LOCALHANDLE hLocal ;
  74.      LPSTR       lpstrDoc, lpstrPos ;
  75.  
  76.                // Get a pointer to the edit document
  77.  
  78.      hLocal   = (HWND) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L) ;
  79.      lpstrDoc = (LPSTR) LocalLock (hLocal) ;
  80.  
  81.                // Search the document for the find string
  82.  
  83.      lpstrPos = _fstrstr (lpstrDoc + *piSearchOffset, lpfr->lpstrFindWhat) ;
  84.      LocalUnlock (hLocal) ;
  85.  
  86.                // Return an error code if the string cannot be found
  87.  
  88.      if (lpstrPos == NULL)
  89.           return FALSE ;
  90.  
  91.                // Find the position in the document and the new start offset
  92.  
  93.      iPos = lpstrPos - lpstrDoc ;
  94.      *piSearchOffset = iPos + _fstrlen (lpfr->lpstrFindWhat) ;
  95.  
  96.                // Select the found text
  97.  
  98.      SendMessage (hwndEdit, EM_SETSEL, 0,
  99.                   MAKELONG (iPos, *piSearchOffset)) ;
  100.  
  101.      return TRUE ;
  102.      }
  103.  
  104. BOOL PopFindNextText (HWND hwndEdit, int *piSearchOffset)
  105.      {
  106.      FINDREPLACE fr ;
  107.  
  108.      fr.lpstrFindWhat = szFindText ;
  109.  
  110.      return PopFindFindText (hwndEdit, piSearchOffset, &fr) ;
  111.      }
  112.  
  113. BOOL PopFindReplaceText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE lpfr)
  114.      {
  115.                // Find the text
  116.  
  117.      if (!PopFindFindText (hwndEdit, piSearchOffset, lpfr))
  118.           return FALSE ;
  119.  
  120.                // Replace it
  121.  
  122.      SendMessage (hwndEdit, EM_REPLACESEL, 0, (long) lpfr->lpstrReplaceWith) ;
  123.  
  124.      return TRUE ;
  125.      }
  126.  
  127. BOOL PopFindValidFind (void)
  128.      {
  129.      return *szFindText != '\0' ;
  130.      }
  131.