|
Post by honky on Feb 10, 2023 12:22:53 GMT
Good morning, Can we count the number of characters at the same time as we write them in a texteditor; The goal is to make an automatic line break (return at line) every N characters. Thank you for.
|
|
|
Post by plus on Feb 10, 2023 19:20:18 GMT
There is always Len(text$) for counting chars. And you don't have to worry about backspace and deletes.
|
|
|
Post by honky on Feb 11, 2023 8:22:29 GMT
Ok, but to get len() we have to query (instruction) And to have it in real time (continuously), we have to query in a loop. But when we are in a loop, we cannot do anything else (write in a texeditor). Perhaps by querying through a timer? But how ? Thank you for.
|
|
|
Post by honky on Feb 11, 2023 10:45:38 GMT
Texteditor no like do return at line
'texteditor #w.entr, ...ect... c=n '----n= n charact /line timer 400, [count] [count] scan #w.entr, "!contents? car$": carac=len(car$) if carac = c then '#w.entr, chr$(13) '<---no work '#w.entr, chr$(13)+chr$(10) '<---no work '#w.entr, " "+chr$(13)+chr$(10)+"" '<---no work playwave "2.wav", async '<----WORK ! c=c+n end if wait
|
|
|
Post by plus on Feb 11, 2023 16:32:14 GMT
Yuck don't like timer idea.
How were you going to count characters except from inside a loop?
I forgot, is there a keypress event for texteditor?
If so, that is where you check length of text and when new stuff reaches width limit add chr$(10) to the text to start a new line? And reset NewText$ variable to "" to start "recount" with Len(). So setup a global NewText$ variable to track new text being added to textEditor.
If not, no key press event for texteditor ??, gotta say there should be! But there must be.
|
|
|
Post by honky on Feb 11, 2023 17:42:40 GMT
I couldn't find any key event detection for the texteditor. chr$(10) has no effect (which is verified because it is in the same "if" as the "playwave" that works).
|
|
|
Post by Rod on Feb 11, 2023 18:33:00 GMT
Not a timer but a hidden graphicbox or textbox can capture the input and allow editing prior to display. Try searching hidden textbox hidden graphicbox.
|
|
|
Post by plus on Feb 11, 2023 20:18:36 GMT
honky, Here is alternate idea Take the entire contents of TextEditor, wrap the strings to a given width and then replace the text with the word wrapped strings.
|
|
|
Post by tsh73 on Feb 11, 2023 21:04:16 GMT
honky, I'm afraid if you want to programmatically change JB text editor to more advanced "text editor with word wrap", this just will not work. In JB, we have no events from texteditor, at all It have no idea about current caret position so you cannot wrap text and set the insert position to right place.
So just say what exactly you are trying to do?
|
|
|
Post by cundo on Feb 11, 2023 21:13:10 GMT
But we have to use a timer to get the text while writing . iirc
|
|
|
Post by plus on Feb 11, 2023 23:20:39 GMT
global Xmax, Ymax ' Word Wrap 40 Chars Test by b+ 2023-02-11 Xmax = 800 Ymax = 600
WindowWidth = Xmax + 8 WindowHeight = Ymax + 32 UpperLeftX = (DisplayWidth - Xmax) / 2 'or delete if Xmax is 1200 or above UpperLeftY = (DisplayHeight - Ymax) / 2 'or delete if Ymax is 700 or above
'nomainwin TextEditor #1.te, 20, 20, 560, 560 Button #1.btn, "Wrap 40 Chars", Wrap, UL, 600, 20, 180, 50 open "Word wrap 40 Chars Test" for graphics_nsb_nf as #1 '<======================= title PRINT #1, "TRAPCLOSE [QUIT]" PRINT #1, "home" PRINT #1, "down"
s$ = "honky," s$ = s$ + " I'm afraid if you want to programmatically change JB text editor to more advanced 'text editor with word wrap', this just will not work." s$= s$ + " In JB, we have no events from texteditor, at all. It have no idea about current caret position so you cannot wrap text and set the insert position to right place." s$ = s$ + " So just say what exactly you are trying to do?" print "Like this!" call printWrap s$, 40, 3, 10 print #1.te, "!contents s$" wait
[Wrap] #1.te, "!contents? ss$" 'notice ss$ check we got it b$ = "" 'reset for next batch Do ss$ = wrap$(ss$, 40, st$) 'print ss$ ' check progress in Main b$ = b$ + ss$ + chr$(10) ss$ = st$ Loop Until Len(ss$) = 0 #1.te, "!contents b$" wait
[QUIT] CLOSE #1 END 'This function returns the first segment of string less than or equal to maxLen AND 'the remainder of the string (if any) is returned in tail$ to be used by wrap$ again Function wrap$ (s$, maxLen, byref tail$) If Len(s$) > maxLen Then p = maxLen + 1 While Mid$(s$, p, 1) <> " " And p > 1 p = p - 1 Wend If p = 1 Then wrap$ = Mid$(s$, 1, maxLen): tail$ = Mid$(s$, maxLen + 1) Else wrap$ = Mid$(s$, 1, p - 1): tail$ = Mid$(s$, p + 1) End If Else wrap$ = s$: tail$ = "" End If End Function
'this sub uses the wrap$ function to print out a string with row and column offsets Sub printWrap s$, maxLen, rowOffset, colOffset ss$ = s$ 'work with copy Do Locate colOffset, rowOffset + i: Print wrap$(ss$, maxLen, st$) ss$ = st$ i = i + 1 Loop Until Len(ss$) = 0 End Sub
|
|
|
Post by plus on Feb 11, 2023 23:59:59 GMT
Oh say maybe you want to actually add more text and wrap again! Uh oh! Here is a partial fix: global Xmax, Ymax ' Word Wrap 40 Chars Test by b+ 2023-02-11 Xmax = 800 Ymax = 600
WindowWidth = Xmax + 8 WindowHeight = Ymax + 32 UpperLeftX = (DisplayWidth - Xmax) / 2 'or delete if Xmax is 1200 or above UpperLeftY = (DisplayHeight - Ymax) / 2 'or delete if Ymax is 700 or above
'nomainwin TextEditor #1.te, 20, 20, 560, 560 Button #1.btn, "Wrap 40 Chars", Wrap, UL, 600, 20, 180, 50 open "Word wrap 40 Chars Test" for graphics_nsb_nf as #1 '<======================= title PRINT #1, "TRAPCLOSE [QUIT]" PRINT #1, "home" PRINT #1, "down"
s$ = "honky," s$ = s$ + " I'm afraid if you want to programmatically change JB text editor to more advanced 'text editor with word wrap', this just will not work." s$= s$ + " In JB, we have no events from texteditor, at all. It have no idea about current caret position so you cannot wrap text and set the insert position to right place." s$ = s$ + " So just say what exactly you are trying to do?" print "Like this!" call printWrap s$, 40, 3, 10 print #1.te, "!contents s$" [waiting] wait
[Wrap] #1.te, "!contents? ss$" 'notice ss$ check we got it ss$ = Filter$(ss$) b$ = "" 'reset for next batch Do ss$ = wrap$(ss$, 40, st$) 'print ss$ ' check progress in Main b$ = b$ + ss$ + chr$(13) + chr$(10) ss$ = trim$(st$) Loop Until Len(ss$) = 0 #1.te, "!contents b$" goto [waiting] wait
[QUIT] CLOSE #1 END 'This function returns the first segment of string less than or equal to maxLen AND 'the remainder of the string (if any) is returned in tail$ to be used by wrap$ again Function wrap$ (s$, maxLen, byref tail$) If Len(s$) > maxLen Then p = maxLen + 1 While Mid$(s$, p, 1) <> " " And p > 1 p = p - 1 Wend If p = 1 Then wrap$ = Mid$(s$, 1, maxLen): tail$ = Mid$(s$, maxLen + 1) Else wrap$ = Mid$(s$, 1, p - 1): tail$ = Mid$(s$, p + 1) End If Else wrap$ = s$: tail$ = "" End If End Function
'this sub uses the wrap$ function to print out a string with row and column offsets Sub printWrap s$, maxLen, rowOffset, colOffset ss$ = s$ 'work with copy Do Locate colOffset, rowOffset + i: Print wrap$(ss$, maxLen, st$) ss$ = st$ i = i + 1 Loop Until Len(ss$) = 0 End Sub
Function Filter$(s$) for i = 1 to len(s$) if asc(mid$(s$, i, 1)) >= 32 then b$ = b$ + mid$(s$, i, 1) else b$ = b$ + " " next Filter$ = b$ end function
Attachments:
|
|
|
Post by honky on Feb 12, 2023 10:23:37 GMT
@: "ROD": The texteditor is in a graphicbox. But when we are in the texeditor, we are not in the graphicbox. I tried to do with the graphicbox alone, but then there are big problems with the capitals (upper case).
@: "plus": This supposes waiting for the text to be complete (click on button to start formatting). But in a text editor, when we go over to the right, the text goes out of the field.
@ "tsh73": It's an automatic line break that I'm trying to do. I put a "ding" (playwave) when the character count is reached, and it works (more or less).
|
|
|
Post by Rod on Feb 12, 2023 11:33:54 GMT
An automatic line break does not exist for the texteditor. The control is designed for manual user input, they simply press enter to insert a line break and get a new line. If you want to send text to the control from your program you can insert line breaks in the text. But as Anatoly has pointed out you will not be able to automate this process real time while the user is keying in. You can in Liberty using the API. But there is a difference.
Text that has line breaks cannot be reformatted or redisplayed in a different sized control easily. The usual solution is to leave the text free of line breaks and for the control to wrap the text. Then the user can resize the control or place the text in another control and it will flow in and fill the available space evenly. So line breaks are not always desirable. More info about what you are doing with the text would help.
Notice that the random text I include in this demo has no line breaks and that the forum display box wraps it according to display size, it will be one long line in the IDE. But the demo can break it at whatever point for the texteditor. You can be more sophisticated and break on words but for this demo its just in chunks.
nomainwin texteditor #main.te 0, 0, 200, 200 t$="The goal of this tutorial is to help you develop applications using Google Cloud Vision API Document Text Detection. It assumes you are familiar with basic programming constructs and techniques, but even if you are a beginning programmer, you should be able to follow along and run this tutorial without difficulty, then use the Cloud Vision API reference documentation to create basic applications." button #main.next, "20 Characters", [w20], LR, 100, 30 button #main.first, "30 Characters", [w30], LR, 250, 30 open "Text" for window as #main #main "trapclose [quit]" wait
[w20] for n = 1 to len(t$) step 20 o$=o$+mid$(t$,n,20)+chr$(10) next #main.te "!cls" #main.te o$ wait
[w30] for n = 1 to len(t$) step 30 o$=o$+mid$(t$,n,30)+chr$(10) next #main.te "!cls" #main.te o$ wait
[quit] close #main end
|
|
|
Post by Rod on Feb 12, 2023 11:53:52 GMT
Answering the point about the caret position, you can insert that with !Select col row and !Lines n string$ returns line by line text but it is not a solution that helps with real time text input. This code finds all instances of "is" in the text and inserts the cursor at that point to highlight the find.
nomainwin texteditor #main.te 0, 0, 200, 200 button #main.next, "Find Next", [find], LR, 100, 30 button #main.first, "Find First", [first], LR, 250, 30 open "Text" for window as #main #main "trapclose [quit]" t$(1)="this is line one." t$(2)="this is line two." t$(3)="this is line three." search$="is" numLines=3 l=1 i=1
[first] l=1 i=1
[find] i=instr(t$(l),search$,i) #main.te "!cls" for n = 1 to numLines if n<>l then #main.te t$(n) else if i then temp$=left$(t$(l),i-1)+right$(t$(l),len(t$(l))-i-len(search$)+1) #main.te temp$ #main.te "!select ";i;" ";n #main.te "!insert search$" i=i+1 else #main.te t$(n) i=1 l=l+1 i=instr(t$(l),search$,i) end if end if next wait
[quit] close #main end
|
|