• Welcome to PlanetSquires Forums.
 

Does anyone have a sample - RichEdit FindText in FF 3.70

Started by John Montenigro, May 17, 2023, 01:03:39 AM

Previous topic - Next topic

John Montenigro

I'm still using FF 3.70, PB 10.04, and JR includes III_107

Learning to use RichEdit control, and having trouble with FindText.

The attached file contains what I've got so far during text/experimentation.
I've deleted a lot of my test/proof stuff, but you'll see a smattering of the kinds of things I've been trying.

If I enter just one character like "x" (as in "text"), it'll return the proper position (11).
But if I enter "xt", it returns -1 (not found).
Either way, the call does not change values in  ft.chrg.cpMin or ft.chrg.cpMax

I found code examples on the PB forums, and when compiled directly in the PB IDE they ran properly.
So I know I must be mixing PB and special FF calls, but I just cannot see the conflicts.
FF only offers 2 RichEdit functions (SetText and GetText), and I'd love to be able to add Find and RepeatFind to the .INC.



You cannot view this attachment.   (of course, the extension is really .frm, but changed here to .txt to enable upload...)

I'd sure appreciate anyone taking a look and seeing what I'm doing wrong.

Thanks!
-John


John Montenigro

In order to complete the record...  I did a lot of editing to the file before getting it to work, so I can't document exactly what conditions changed. BUT, I do know that the key to finally getting things working involved changing one message from %EM_whatever to %EM_whateverW    :o

I know that's not the best documentation, but it's at least a clue for future readers.

I found a hint, plus working code, in a POFFS message thread by Chris Holbrook:

'MESSAGE http://www.powerbasic.com/support/pbforums/showthread.php?t=038397
'FORUM  PBWin
'TOPIC  Richedit50W search & replace  (038397)
The message was dated 2008-08-29 04:59AM and for search purposes, the function is named:  bFindTextInSelection

POFFS turns out to have several dozen discussions on RichEdit, so that's my next study material.

Onward!

Best regards,
-John

John Montenigro

Here's the edited file that works, contains a lot of my observations and remaining questions... 

(I couldn't find the UPLOAD button, so I'm dumping the file as CODE:

#FireFly_Form#  Version=3.70
Locked=0
ClientOffset=0
ClientWidth=580
ClientHeight=492


[ControlType] Form | PropertyCount=24 | zorder=1 | tabindex=0 |
name=frmPopup
classstyles=CS_VREDRAW, CS_HREDRAW, CS_DBLCLKS
windowstyles=WS_POPUP, WS_THICKFRAME, WS_CAPTION, WS_SYSMENU, WS_MINIMIZEBOX, WS_MAXIMIZEBOX, WS_CLIPSIBLINGS, WS_CLIPCHILDREN, WS_VISIBLE|WS_EX_WINDOWEDGE, WS_EX_CONTROLPARENT, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
backbitmap=
backbitmapmode=0 - Tiled
backcolor=SYS,15
caption=For review
export=False
height=531
icon=
left=0
mdichild=False
minwidth=0
minheight=0
maxwidth=0
maxheight=0
startupposition=1 - Centered
tabcontrolchild=False
tabcontrolchildautosize=False
tag=
tag2=
top=0
width=596
windowstate=0 - Normal

[ControlType] TextBox | PropertyCount=22 | zorder=2 | tabindex=17 |
name=txtMsg1
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, ES_LEFT, ES_MULTILINE, ES_AUTOHSCROLL, ES_AUTOVSCROLL, ES_READONLY|WS_EX_CLIENTEDGE, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
backcolor=SYS,5
controlindex=0
font=Courier New,9,0
fontupgrade=False
forecolor=SYS,8
height=54
left=10
locked=False
leftmargin=0
maxlength=0
rightmargin=0
resizerules=
seltext=False
text=
tag=
tag2=
tooltip=
tooltipballoon=False
top=9
width=365

[ControlType] TextBox | PropertyCount=22 | zorder=3 | tabindex=16 |
name=txtMsg2
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, ES_LEFT, ES_MULTILINE, ES_AUTOHSCROLL, ES_AUTOVSCROLL, ES_READONLY|WS_EX_CLIENTEDGE, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
backcolor=SYS,5
controlindex=0
font=Courier New,9,0
fontupgrade=False
forecolor=SYS,8
height=36
left=8
locked=False
leftmargin=0
maxlength=0
rightmargin=0
resizerules=
seltext=False
text=
tag=
tag2=
tooltip=
tooltipballoon=False
top=109
width=365

[ControlType] CommandButton | PropertyCount=17 | zorder=4 | tabindex=15 |
name=cmdCLOSE
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=CLOSE
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=20
left=514
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=456
width=50

[ControlType] CommandButton | PropertyCount=17 | zorder=5 | tabindex=14 |
name=cmdSave
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=SAVE TEXTS
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=20
left=432
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=456
width=70

[ControlType] CommandButton | PropertyCount=17 | zorder=1 | tabindex=13 |
name=cmdMoveTextFromMsg2ToMsg1
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Copy txt up
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=25
left=8
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=74
width=83

[ControlType] RichEdit | PropertyCount=19 | zorder=2 | tabindex=12 |
name=reText1
windowstyles=WS_CHILD, WS_VISIBLE, WS_HSCROLL, WS_VSCROLL, WS_TABSTOP, ES_LEFT, ES_MULTILINE, ES_AUTOHSCROLL, ES_AUTOVSCROLL, ES_WANTRETURN|WS_EX_CLIENTEDGE, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=173
left=9
locked=False
leftmargin=0
maxlength=0
rightmargin=0
resizerules=
text=
tag=
tag2=
tooltip=
tooltipballoon=False
top=209
width=365

[ControlType] External {5D834536-1276-7999-766C-398746547920} | PropertyCount=30 | zorder=1 | tabindex=11 |
name=FireTextBox1
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, WS_CLIPSIBLINGS, WS_CLIPCHILDREN|WS_EX_LEFT, WS_EX_LTRREADING
about=
custom=
designstyle=0 - Traditional
backcolor=QB,16777215
backhotcolor=QB,16777215
controlindex=0
font=Tahoma,-11,0,0,0,400,0,0,0,0,0,0,0,0
height=28
locked=False
left=8
leftmargin=0
linehotcolor=QB,8388608
linenormalcolor=QB,8421504
readonly=0 - False
resizerules=FL,FT,FL,FT
rightmargin=0
rightaligntext=0 - False
seltext=0 - False
showselector=0 - False
showtriangle=1 - True
tag=
tag2=
text=
textcolor=QB,8388608
texthotcolor=QB,8388608
textdisablecolor=QB,8421504
top=163
width=171

[ControlType] Label | PropertyCount=20 | zorder=1 | tabindex=9 |
name=Label1
windowstyles=WS_CHILD, WS_VISIBLE, WS_CLIPSIBLINGS, WS_CLIPCHILDREN, SS_LEFT, SS_NOTIFY|WS_EX_LEFT, WS_EX_LTRREADING
backcolor=SYS,15
backstyle=1 - Opaque
caption=RichText
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
forecolor=SYS,8
height=12
identifier=True
left=9
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=197
width=45

[ControlType] Label | PropertyCount=20 | zorder=2 | tabindex=10 |
name=Label2
windowstyles=WS_CHILD, WS_VISIBLE, WS_CLIPSIBLINGS, WS_CLIPCHILDREN, SS_LEFT, SS_NOTIFY|WS_EX_LEFT, WS_EX_LTRREADING
backcolor=SYS,15
backstyle=1 - Opaque
caption=FireText
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
forecolor=SYS,8
height=14
identifier=True
left=8
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=150
width=45

[ControlType] CommandButton | PropertyCount=17 | zorder=3 | tabindex=8 |
name=cmdCopy
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Copy
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=388
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=211
width=54

[ControlType] CommandButton | PropertyCount=17 | zorder=4 | tabindex=4 |
name=cmdPaste
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Paste
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=447
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=211
width=54

[ControlType] CommandButton | PropertyCount=17 | zorder=5 | tabindex=3 |
name=cmdUndo
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Undo
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=505
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=211
width=54

[ControlType] CommandButton | PropertyCount=17 | zorder=6 | tabindex=2 |
name=cmdNextURL
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=NextURL
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=505
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=324
width=54

[ControlType] CommandButton | PropertyCount=17 | zorder=7 | tabindex=7 |
name=cmdFind
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Find
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=505
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=246
width=54

[ControlType] CommandButton | PropertyCount=17 | zorder=8 | tabindex=6 |
name=cmdRepeat
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Repeat
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=505
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=266
width=54

[ControlType] TextBox | PropertyCount=22 | zorder=9 | tabindex=5 |
name=txtFindTEXT
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, ES_LEFT, ES_AUTOHSCROLL|WS_EX_CLIENTEDGE, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
backcolor=SYS,5
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
forecolor=SYS,8
height=24
left=391
locked=False
leftmargin=0
maxlength=0
rightmargin=0
resizerules=
seltext=False
text=
tag=
tag2=
tooltip=
tooltipballoon=False
top=253
width=104

[ControlType] TextBox | PropertyCount=22 | zorder=10 | tabindex=1 |
name=txtFindURL
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, ES_LEFT, ES_AUTOHSCROLL|WS_EX_CLIENTEDGE, WS_EX_LEFT, WS_EX_LTRREADING, WS_EX_RIGHTSCROLLBAR
backcolor=SYS,5
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
forecolor=SYS,8
height=24
left=391
locked=False
leftmargin=0
maxlength=0
rightmargin=0
resizerules=
seltext=False
text=
tag=
tag2=
tooltip=
tooltipballoon=False
top=312
width=104

[ControlType] CommandButton | PropertyCount=17 | zorder=11 | tabindex=0 |
name=cmdFindURL
windowstyles=WS_CHILD, WS_VISIBLE, WS_TABSTOP, BS_TEXT, BS_PUSHBUTTON, BS_NOTIFY, BS_CENTER, BS_VCENTER|WS_EX_LEFT, WS_EX_LTRREADING
cancel=False
caption=Find URL
controlindex=0
font=Segoe UI,9,0
fontupgrade=False
height=19
left=505
locked=False
resizerules=
tag=
tag2=
tooltip=
tooltipballoon=False
top=304
width=54

[AllCode]

'--------------------------------------------------------------------------------
Function FRMPOPUP_WM_CREATE ( _
                            hWndForm As Dword, _      ' handle of Form
                            ByVal UserData As Long _  ' optional user defined Long value
                            ) As Long
   Local lRet As Long
   Local m As String

   'during DEVTEST, just get some text into the RE control:
   m = "There is text in the textbox, and more text on the " & $CrLf & "floor. Search for THE and see if it hits."
   lRet = SendMessage (HWND_FRMPOPUP_RETEXT1, %WM_SETTEXT, 0, StrPtr(m))      ' I'm not liking the FF_RichEdit_SetText function...  

   'limit the range?   
'   SendMessage HWND_FRMPOPUP_RETEXT1, %EM_SETSEL, 20, 30   
'although this does not SHOW what's selected, it certainly does get set!
'we have proof when we see the Find routine DEVTEST messages showing the RT
    
   SetFocus HWND_FRMPOPUP_txtFindText  'for convenience during DEVTEST
                                                  
End Function


'--------------------------------------------------------------------------------
Function FRMPOPUP_CMDCLOSE_BN_CLICKED ( _
                                   ControlIndex     As Long,  _  ' index in Control Array
                                   hWndForm         As Dword, _  ' handle of Form
                                   hWndControl      As Dword, _  ' handle of Control
                                   idButtonControl  As Long   _  ' identifier of button
                                   ) As Long
   'Font End gfhCourierNew
   'etc...
   '
   SaveWindowPosition(gsINI_File, hWndForm, "frmPopup")    ' (use a literal for FormName)
   '
'+++++++++++++++++++++++++++++++++
   'close all window, files, etc.
   FF_CloseForm hWndForm       
  
End Function


'--------------------------------------------------------------------------------
Function FRMPOPUP_CMDFIND_BN_CLICKED ( _
                                     ControlIndex     As Long,  _  ' index in Control Array
                                     hWndForm         As Dword, _  ' handle of Form
                                     hWndControl      As Dword, _  ' handle of Control
                                     idButtonControl  As Long   _  ' identifier of button
                                     ) As Long
   FindTextInRichEdit   ' not the best name, but won't conflict w JR includes or FF_ stuff...

   'I noticed a glitch w repeated Finds... could not find . or , or a single space... re-opened frmPopup and it was fine. Why the glitch?
                                   
End Function


Function FindTextInRichEdit () As Long
                                    
   Local sUserText As String
   Local wMainString As wString      ' plain text wasn't working, trying wide
   Local lRet, lStart, lEnd, lTextLength As Long
   Local ft As FindTextExW                      ' need to assess the diffs between the Ex and the ExW versions
   Local psUserText As Asciz Ptr

   'Be sure both the main box and the search box have text
   '======================================================
   'check the Find box
   '--------------------
   'get the user's search string:  
   sUserText = FF_TextBox_GetText(HWND_FRMPOPUP_txtFindTEXT)  
   If Len(sUserText) = 0 Then   
      ? "HWND_FRMPOPUP_txtFindTEXT is blank!" ,, "DEVTEST - Textbox"
      Exit Function  
   End If 

   'check the main box - may be empty, have text, or have user-selected text...
   '------------------
   'if the RE has selected text, get just that; otherwise get all of it.
   '----------------------------------------------------------------------
   ' Originally, we grabbed this in order to do a PB INSTR search, but now that
   ' we know about %EM_FINDTEXTEXW, it's just for avoiding an empty text error.
   '----------------------------------------------------------------------
   wMainString = RichEdit_GetSelText (HWND_FRMPOPUP_RETEXT1)   ' no "selected" status flag, so just try it.  
   If wMainString = "" Then 
      wMainString = RichEdit_GetText (HWND_FRMPOPUP_RETEXT1)   ' nothing was selected, so now get all of what's there, if anything.     
   End If
   If Len(wMainString) = 0 Then 
      '? "HWND_FRMPOPUP_RETEXT1 is blank!" ,, "DEVTEST - RichEdit"
      ? "There's no text to search!" ,, "RichText"          ' RichText is currently the name of the RichEdit control...
      Exit Function  
   End If 
 
   'do the search
   '=============  
   lRet = RE_FindTextInSelection (HWND_FRMPOPUP_RETEXT1, sUserText, lStart, lEnd)     ' needs a param for lStartAtPos... 
   If lRet Then ? "Found text is selected from:"  & Str$(lStart) & "  to: " & Str$(lEnd),,"RichText FIND" 
  
   'ZCZC  ZCZC  ZCZC  need to learn how to set up for a "Find Next", using the last found position of this text...     ****************** see below
   'got to put lStart +1 as the new start... where? must need a call like SetSel...
  
End Function


Function RE_FindTextInSelection(hRich As Dword, sSearchFor As String, lFoundAt As Long, lFoundLength As Long ) As Long   'needs param for "lStartAtPos"   
'from:  RichEdit_FindText_test.bas  by Chris Holbrook
'from POFFS:  line 2847237                         w lot of jhm edits
'
   Local ftexw             As FINDTEXTEXW
   Local lRet, lLineNDX    As Long
   Local sGiven            As String
   Local psGiven           As Asciz Ptr
  
   sGiven   = UCode$(sSearchFor)
   psGiven  = StrPtr(sGiven)

   ' set search range = whole buffer
   ftexw.chrg.cpMin = 0                   ' for a "FindNext" routine, set this to lStartAtPos  (or lRet+1)
   ftexw.chrg.cpMax = SendMessage (hrich, %EM_GETLIMITTEXT, 0, 0)     ' this message is not in the Laurence Johnson reference... see MSDN
   ftexw.lpStrText = psGiven

#if 0    ' nope! These are NOT the "found" start/end values! They are the range that was searched!  
  ? "searchstring = '" & searchstr & "' " & $CrLf & _
    "selection range: " & Str$(ftexw.chrg.cpMin) & $Tab & Str$(ftexw.chrg.cpMax),%MB_TaskModal 
#endif   

   lRet = SendMessage(hRich, %EM_FINDTEXTEXW, %FR_DOWN, VarPtr(ftexw))
   lFoundAt       = ftexw.chrgText.cpMin
   lFoundLength   = ftexw.chrgText.cpMax    
   If lRet <> -1 Then    
     
      lLineNDX = SendMessage(hRich, %EM_EXSETSEL, 0, VarPtr(ftexw.chrgText))  ' the lParam is the CharRANGE structure (start and end = min/max)
      '  supposedly returns the zero-based index of the line.
      '? "zero-based index of the line in which the text was found: " & $CrLf & $Tab & Str$(lLineNDX),,"DEVTEST"    NOPE!!!!  WAY too high!!!
      'BUT it is used for a side effect having something to do with setting the text highlighting.
     
      SendMessage hRich, %EM_HIDESELECTION, 0, 0      
      ' the wParam is the Hide flag: 0=show, otherwise=hide
      ' the lParam is the ChangeStyle flag: 0=temporarily shown or hidden, otherwise ??? (the Help is confusing...)  
      Function = lRet   ' we return the 0-based position of the first character in the match.  

   Else
      Function = %FALSE
      ? "Did NOT find the text: " & sSearchFor,,"Find failed!"
   End If
End Function
'------------------------------------------------------------------