Back to index

lightning-sunbird  0.9+nobinonly
popfind.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*--------------------------------------------------------
00039    POPFIND.C -- Popup Editor Search and Replace Functions
00040    (c) Charles Petzold, 1992
00041   --------------------------------------------------------*/
00042 
00043 #include <windows.h>
00044 #include <commdlg.h>
00045 #include <string.h>
00046 #define MAX_STRING_LEN   256
00047 
00048 static char szFindText [MAX_STRING_LEN] ;
00049 static char szReplText [MAX_STRING_LEN] ;
00050 
00051 HWND PopFindFindDlg (HWND hwnd)
00052      {
00053      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
00054 
00055      fr.lStructSize      = sizeof (FINDREPLACE) ;
00056      fr.hwndOwner        = hwnd ;
00057      fr.hInstance        = NULL ;
00058      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
00059      fr.lpstrFindWhat    = szFindText ;
00060      fr.lpstrReplaceWith = NULL ;
00061      fr.wFindWhatLen     = sizeof (szFindText) ;
00062      fr.wReplaceWithLen  = 0 ;
00063      fr.lCustData        = 0 ;
00064      fr.lpfnHook         = NULL ;
00065      fr.lpTemplateName   = NULL ;
00066 
00067      return FindText (&fr) ;
00068      }
00069 
00070 HWND PopFindReplaceDlg (HWND hwnd)
00071      {
00072      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
00073 
00074      fr.lStructSize      = sizeof (FINDREPLACE) ;
00075      fr.hwndOwner        = hwnd ;
00076      fr.hInstance        = NULL ;
00077      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
00078      fr.lpstrFindWhat    = szFindText ;
00079      fr.lpstrReplaceWith = szReplText ;
00080      fr.wFindWhatLen     = sizeof (szFindText) ;
00081      fr.wReplaceWithLen  = sizeof (szReplText) ;
00082      fr.lCustData        = 0 ;
00083      fr.lpfnHook         = NULL ;
00084      fr.lpTemplateName   = NULL ;
00085 
00086      return ReplaceText (&fr) ;
00087      }
00088 
00089 BOOL PopFindFindText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE lpfr)
00090      {
00091      int         iPos ;
00092      LOCALHANDLE hLocal ;
00093      LPSTR       lpstrDoc, lpstrPos ;
00094 
00095                // Get a pointer to the edit document
00096 
00097      hLocal   = (HWND) SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L) ;
00098      lpstrDoc = (LPSTR) LocalLock (hLocal) ;
00099 
00100                // Search the document for the find string
00101 
00102      lpstrPos = _fstrstr (lpstrDoc + *piSearchOffset, lpfr->lpstrFindWhat) ;
00103      LocalUnlock (hLocal) ;
00104 
00105                // Return an error code if the string cannot be found
00106 
00107      if (lpstrPos == NULL)
00108           return FALSE ;
00109 
00110                // Find the position in the document and the new start offset
00111 
00112      iPos = lpstrPos - lpstrDoc ;
00113      *piSearchOffset = iPos + _fstrlen (lpfr->lpstrFindWhat) ;
00114 
00115                // Select the found text
00116 
00117      SendMessage (hwndEdit, EM_SETSEL, 0,
00118                   MAKELONG (iPos, *piSearchOffset)) ;
00119 
00120      return TRUE ;
00121      }
00122 
00123 BOOL PopFindNextText (HWND hwndEdit, int *piSearchOffset)
00124      {
00125      FINDREPLACE fr ;
00126 
00127      fr.lpstrFindWhat = szFindText ;
00128 
00129      return PopFindFindText (hwndEdit, piSearchOffset, &fr) ;
00130      }
00131 
00132 BOOL PopFindReplaceText (HWND hwndEdit, int *piSearchOffset, LPFINDREPLACE lpfr)
00133      {
00134                // Find the text
00135 
00136      if (!PopFindFindText (hwndEdit, piSearchOffset, lpfr))
00137           return FALSE ;
00138 
00139                // Replace it
00140 
00141      SendMessage (hwndEdit, EM_REPLACESEL, 0, (long) lpfr->lpstrReplaceWith) ;
00142 
00143      return TRUE ;
00144      }
00145 
00146 BOOL PopFindValidFind (void)
00147      {
00148      return *szFindText != '\0' ;
00149      }