Author Topic: Find/Replace Dialog  (Read 3543 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Find/Replace Dialog
« on: May 01, 2012, 02:10:53 AM »
This was quite tricky to get working correctly. The FindReplace dialog is modeless and  requires an extended message loop, to divert dialog messages.

Code: [Select]
 sys bRet
  '
  '
  while bRet := GetMessage @wm, 0, 0, 0
    if bRet = -1
      'show an error message
      exit do
    elseif not IsDialogMessage hDlgFr,@wm
      TranslateMessage @wm
      DispatchMessage @wm
    end if
  wend

Here is the include file

Code: [Select]

'no case sensitivity

//Flags

% FR_DOWN                         0x00000001
% FR_WHOLEWORD                    0x00000002
% FR_MATCHCASE                    0x00000004
% FR_FINDNEXT                     0x00000008
% FR_REPLACE                      0x00000010
% FR_REPLACEALL                   0x00000020
% FR_DIALOGTERM                   0x00000040
% FR_SHOWHELP                     0x00000080
% FR_ENABLEHOOK                   0x00000100
% FR_ENABLETEMPLATE               0x00000200
% FR_NOUPDOWN                     0x00000400
% FR_NOMATCHCASE                  0x00000800
% FR_NOWHOLEWORD                  0x00001000
% FR_ENABLETEMPLATEHANDLE         0x00002000
% FR_HIDEUPDOWN                   0x00004000
% FR_HIDEMATCHCASE                0x00008000
% FR_HIDEWHOLEWORD                0x00010000
% FR_RAW                          0x00020000
% FR_MATCHDIAC                    0x20000000
% FR_MATCHKASHIDA                 0x40000000
% FR_MATCHALEFHAMZA               0x80000000


// strings used to obtain unique window message for communication
// between dialog and caller

% LBSELCHSTRINGA  "commdlg_LBSelChangedNotify"
% SHAREVISTRINGA  "commdlg_ShareViolation"
% FILEOKSTRINGA   "commdlg_FileNameOK"
% COLOROKSTRINGA  "commdlg_ColorOK"
% SETRGBSTRINGA   "commdlg_SetRGBColor"
% HELPMSGSTRINGA  "commdlg_help"
% FINDMSGSTRINGA  "commdlg_FindReplace"


type FINDREPLACE
  dword  lStructSize
  sys    hwndOwner
  sys    hInstance
  dword  Flags
  char*  lpstrFindWhat
  char*  lpstrReplaceWith
  word   wFindWhatLen
  word   wReplaceWithLen
  long   lCustData
  sys    lpfnHook
  char*  lpTemplateName
end type

Declare GetModuleHandle       lib "kernel32.dll" alias "GetModuleHandleA" (optional char*n) as sys
Declare ReplaceText           Lib "comdlg32.dll" Alias "ReplaceTextA" (FINDREPLACE*fr) As sys
Declare CommDlgExtendedError  Lib "comdlg32.dll" () as dword
Declare IsDialogMessage       Lib "user32.dll" (sys hDlg,lpMsg) as sys
Declare RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" (char* lpStrMsg) as sys


Function FindReplaceDialog(char *f,*r, sys le, Hwnd, Flags) as sys
'====================

static FINDREPLACE  fr

fr.lStructSize      = sizeof FINDREPLACE
fr.hwndOwner        = hwnd
fr.hInstance        = GetModuleHandle
fr.Flags            = Flags
fr.lpstrFindWhat    = f
fr.lpstrReplaceWith = r
fr.wFindWhatLen     = le
fr.wReplaceWithLen  = le
fr.lCustData        = 0 'lparam
fr.lpfnHook         = 0
fr.lpTemplateName   = null

sys retval = ReplaceText(fr)
if retval=0 then print "Dialog Error " CommDlgExtendedError
return retval
'
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms646835(v=vs.85).aspx
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms646946(v=vs.85).aspx
'
'#recordof FINDREPLACE
'
End Sub

and here is examples/GUI/FindReplace.o2bas

Code: [Select]
 $ filename "t.exe"
  '#include "../../inc/RTL32.inc"
  '#include "../../inc/RTL64.inc"
  #include "../../inc/MinWin.inc"

  #include "DialogFindReplace.inc"


  #lookahead ' for procedures
  s=error()
  '
  if s then
    print s
    goto endprog
  end if




 

  '=========
  'MAIN CODE
  '=========
  
  dim cmdline as asciiz ptr, inst as sys
  &cmdline=GetCommandLine
  inst=GetModuleHandle 0
  '
  'WINDOWS
  '-------
  '
  WinMain inst,0,cmdline,SW_NORMAL
  
  endprog:

  'RELEASE SYSTEM RESOURCES
  '---------------------

  FreeMinWinLibraries

  '===========
  'END OF MAIN
  '===========

  dim as sys hDlgFr

  '--------------------------------------------------------------------
  Function WinMain(sys inst, prevInst, asciiz*cmdline, sys show) as sys
  '====================================================================


  WndClass wc
  MSG      wm

  sys hwnd, wwd, wht, wtx, wty, tax

  wc.style = CS_HREDRAW or CS_VREDRAW
  wc.lpfnWndProc = @WndProc
  wc.cbClsExtra =0
  wc.cbWndExtra =0    
  wc.hInstance =inst
  wc.hIcon=LoadIcon 0, IDI_APPLICATION
  wc.hCursor=LoadCursor 0,IDC_ARROW
  wc.hbrBackground = GetStockObject WHITE_BRUSH
  wc.lpszMenuName =null
  wc.lpszClassName = strptr "Demo"

  RegisterClass @wc
 
  Wwd = 320 : Wht = 200
  Tax = GetSystemMetrics SM_CXSCREEN
  Wtx = (Tax - Wwd) /2
  Tax = GetSystemMetrics SM_CYSCREEN
  Wty = (Tax - Wht) /2
 
  hwnd = CreateWindowEx 0,wc.lpszClassName,"OXYGEN BASIC",WS_OVERLAPPEDWINDOW,Wtx,Wty,Wwd,Wht,0,0,inst,0
  ShowWindow hwnd,SW_SHOW
  UpdateWindow hwnd
  '
  sys bRet
  '
  '
  while bRet := GetMessage @wm, 0, 0, 0
    if bRet = -1
      'show an error message
      exit do
    elseif not IsDialogMessage hDlgFr,@wm
      TranslateMessage @wm
      DispatchMessage @wm
    end if
  wend

  End Function



  dim as rect crect 'for WndProc and TimerProc

  '------------------------------------------------------------------
  function WndProc ( sys hWnd, wMsg, wParam, lparam ) as sys callback
  '==================================================================

    static sys         hdc
    static String      txt="Find and Replace Test", frs=""
    static PaintStruct Paintst
    static sys         DlgFrMsg, flags
    static char        f[100],r[100]
 
    '==========
    select wMsg
    '==========
        
      '--------------
      case WM_CREATE
      '=============

      GetClientRect  hWnd,&cRect
      DlgFrMsg=RegisterWindowMessage FINDMSGSTRINGA
      hDlgFr=FindReplaceDialog(f,r,96,hwnd,flags)

      '--------------  
      case WM_DESTROY
      '===============
          
      PostQuitMessage 0
        
      '------------
      case WM_PAINT
      '============


      'TEXT
      'http://msdn.microsoft.com/en-us/library/dd144821(v=VS.85).aspx

      'DRAWING AND PAINTING
      'http://msdn.microsoft.com/en-us/library/dd162760(v=VS.85).aspx

      GetClientRect  hWnd,&cRect
      hDC=BeginPaint hWnd,&Paintst
      'style
      '0x20 DT_SINGLELINE
      '0x04 DT_VCENTER
      '0x01 DT_CENTER
      '0x25

       SetBkColor   hdc,yellow
       SetTextColor hdc,red
       DrawText hDC,strptr txt,-1,&cRect,0x25
       EndPaint hWnd,&Paintst
        
      '--------------  
      case WM_KEYDOWN
      '==============

      '============            
      Select wParam
      '============

Case 27 : SendMessage hwnd, WM_CLOSE, 0, 0            'ESCAPE
        Case 82 : hDlgFr=FindReplaceDialog(f,r,96,hwnd,flags) 'R

      End Select
      

      '------------  
      case DlgFrMsg
      '============

      FINDREPLACE* fr
      @fr=lparam
      frs=fr.lpstrFindWhat

      switch bit fr.flags
      '
      case FR_DIALOGTERM
        hDlgFr = 0
        fr.lpstrFindWhat=""
        fr.lpstrReplaceWith=""
        fr.flags=0
        frs="Press R for Find/Replace Dialog"
      case FR_MATCHCASE
         frs+=" MATCHCASE"
      case FR_WHOLEWORD
         frs+=" WHOLEWORD"
      case FR_FINDNEXT
         frs+=" FINDNEXT"
      case FR_REPLACE
         frs+=" REPLACE"
      case FR_REPLACEALL
         frs+=" REPLACEALL"
      end select
      '
      txt=frs
      InvalidateRect hWnd, @crect

      '--------        
      case else
      '========
          
        function=DefWindowProc hWnd,wMsg,wParam,lParam
        
    end select

  end function ' WndProc


This is included in the latest Oxygen.

Charles