Page 1 of 1

Strange behavior of Editor with the WordWrap flag

Posted: Mon Oct 06, 2025 7:01 pm
by ZX80
Hi, all.

I need to code an editor so that each line contains no more than n characters. For this I used a monospaced font and calculated the required number of pixels. I also set the 'WordWrap' flag. But it works strangely. This very simple code does not work as expected. It only uses the carry flag. Also in the debug window I expected to get what I see in the editor, but this is not the case at all. I think a workaround is to count the number of lines in the editor and then read them one by one, adding #CRLS$ between them. But why doesn't the GetGadgetText() command do this ? I don't know if this is a bug.

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133, #PB_Editor_WordWrap)
  SendMessage_(eWnd, #EM_SETTEXTMODE, #TM_RICHTEXT, 0)
  
  For a = 0 To 5
    AddGadgetItem(0, a, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
  
  Debug GetGadgetText(0)  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
If it's important, then PB version 6.20 (x86).
Can anyone confirm the same editor behavior ?
Thank you.

Re: Strange behavior of Editor with the WordWrap flag

Posted: Mon Oct 06, 2025 7:47 pm
by Piero
I really hope someone will help you, because 6.20 (x86) must be very rare (also considering these awful times) and I'm on Mac.

Re: Strange behavior of Editor with the WordWrap flag

Posted: Mon Oct 06, 2025 8:44 pm
by zikitrake
The same thing happens in PB 6.30 beta 3.

Image

Re: Strange behavior of Editor with the WordWrap flag

Posted: Mon Oct 06, 2025 9:08 pm
by RASHAD
Windows only
Editor Gadget limit characters per line

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133, #PB_Editor_WordWrap)
  SendMessage_(GadgetID(0), #EM_SETMARGINS, #EC_LEFTMARGIN, 0|0<<16)
  SendMessage_(GadgetID(0), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|60<<16) ;60 Characters per  line
  
  For a = 0 To 5
    AddGadgetItem(0, a, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
  
  count = CountGadgetItems(0)
  For line = 0 To count
    Debug GetGadgetItemText(0,line)
  Next  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 8:25 am
by ZX80
Thanks to everyone who responded.

Dear RASHAD !
I know about this API and it does something completely different from what you say. These are just indents (left and right). But even in this case, the editor still behaves very strangely. It corrupts the line. I'll probably have to use Axolotl's wonderful code for this case. There the position of the cursor carriage was calculated "on the fly". Let me repeat, in my opinion, the WordWrap flag in the editor does not work correctly.

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 9:07 am
by RASHAD
Your problem is with your loop

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133,#PB_Editor_WordWrap)
  ;SendMessage_(GadgetID(0),#EM_SETTARGETDEVICE,0, 0)
  SendMessage_(GadgetID(0), #EM_SETMARGINS, #EC_LEFTMARGIN, 0|0<<16)
  SendMessage_(GadgetID(0), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|40<<16) ;60 Characters per  line
  
  For a = 0 To 5
    AddGadgetItem(0, -1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
  
  count = CountGadgetItems(0)
  For line = 0 To count
    Debug GetGadgetItemText(0,line)
  Next  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 9:17 am
by ZX80
RASHAD

Code: Select all

count = CountGadgetItems(0)
  For line = 0 To count
    Debug GetGadgetItemText(0,line)
  Next  
Yes, this is exactly the workaround I used, I talked about it above. But this is only to obtain the text in its original form. However, the main problem is still not solved.

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 9:53 am
by Jacobus
Hello, Try with this version of the API. This works with PB 6.30B3

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133,#PB_Editor_WordWrap)
  SendMessage_(GadgetID(0), #EM_SETTARGETDEVICE, 0, Flag | 1)
  For a = 0 To 5
    AddGadgetItem(0, -1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
  count = CountGadgetItems(0)
  For line = 0 To count
    Debug GetGadgetItemText(0,line)
  Next    
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 10:12 am
by RASHAD
I can't understand exactly your aim
Next limit the no. of characters per line to 20
You can change it
The debugger and the gadget display the same

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133,#ES_MULTILINE)
  
  For a = 0 To 5
    text$ = RSet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk",20)
    AddGadgetItem(0, -1, text$)
  Next
  
  count = CountGadgetItems(0)
  For line = 0 To count
    Debug GetGadgetItemText(0,line)
  Next  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 10:39 am
by pfnuesu
ZX80 wrote: Wed Oct 08, 2025 9:17 am Yes, this is exactly the workaround I used, I talked about it above. But this is only to obtain the text in its original form. However, the main problem is still not solved.
Just to make sure: did you see this?

Code: Select all

For a = 0 To 5
    AddGadgetItem(0, a, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
  
Changed to

Code: Select all

For a = 0 To 5
    AddGadgetItem(0, -1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk")
  Next
Do you absolutely need to be able to insert a line at a specific location?

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 11:10 am
by ZX80
Jacobus
Thanks, but horizontal scrolling wasn't my goal. Quite the opposite, actually.

RASHAD
My goal is to ensure that text wraps correctly in the editor by words. In the literal sense of the word. The text in the example is for demonstration purposes only. In fact, the text will be typed by the operator and I cannot say in advance its content and composition. But there is a clear condition: the number of characters in each line, for example 80.
For this reason, I think I can't do without this code. I just thought the WordWrap flag would do the job. Unfortunately, this is not the case. Also It would be nice to add a character limit on each line of the editor as a standard feature.
Now... I have to monitor the carriage position every time I type something. For example, the previous word ended at position 78. Next comes a space. This is the separator / delimiter. This is already the 79th character on the current line. Then I continue typing the next word. Let it consist of five letters. As you can remember, I have place only for one char. It can be only an indefinite 'a' article. Our word is five letters long, and I type it in full without thinking. What should the editor do in this case ?
Right ! As soon as the carriage position exceeds the set limit, you need to find the nearest space on the left. The part that was already printed on the first line should move to the second line, and a line feed and newline character (#CRLF$) should be added in its place. In my understanding, this is how the WordWrap flag should work. This is just a detailed description.
The thing is, I also wanted to provide the ability to paste text into the editor. And this process should also follow the rules mentioned above.

Added:
What if the word is more than 80 characters ? Will this be an endless line break ?
Then you need to trim the line artificially, to 80 characters.

pfnuesu
Just to make sure: did you see this?
Yes, I saw it.
Do you absolutely need to be able to insert a line at a specific location?
That would be great.

Re: Strange behavior of Editor with the WordWrap flag

Posted: Wed Oct 08, 2025 3:05 pm
by Jacobus
In this way, simply, it can't allow you to do what you want?

Code: Select all

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  
  EditorGadget(1, 8, 8, 306, 133, #PB_Editor_WordWrap)
   
  For a = 0 To 10 Step 2
    Debug Str(a)
    AddGadgetItem(1, a, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk") 
  Next

    Debug GetGadgetText(1)

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Or do a routine based on the following principle: (Simplified code for example)

Code: Select all

; Global Chaine$ = "0123456789ABCDEFGHIJKLMNOPQRST" ;30
 Global Chaine$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk" ;49
; Global Chaine$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk=0123456789=0123456789" ;71
; Global Chaine$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!abcdefghijk=0123456789=0123456789$ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;98

If OpenWindow(0, 0, 0, 322, 150, "Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  
  EditorGadget(1, 8, 8, 306, 133, #PB_Editor_WordWrap)
   
  Res = Len(Chaine$)
  If Res <= 100 And Res > 80
    For a = 0 To 10 Step 3 ;3-line display
      AddGadgetItem(1, a, Chaine$) 
    Next
  ElseIf Res <= 80 And Res > 30
    For a = 0 To 10 Step 2 ;2-line display
      AddGadgetItem(1, a, Chaine$) 
    Next
  ElseIf Res <= 30
    For a = 0 To 10 ; 1 line
      AddGadgetItem(1, a, Chaine$) 
    Next
  EndIf 
   
    Debug GetGadgetText(1)

  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf