Page 1 of 2

EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 7:21 am
by ZX80
Hi, all.

I have an editor in my window that has a font assigned to it that I defined. I didn't realize it would be used in this way. Problem: A person writes text in a third-party text editor, such as Word, and then pastes it into my program. The font changes according to how it was selected in Word. How can I reset the font style so it displays as I intend ?

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 7:50 am
by BarryG
The EditorGadget uses rich text, so it will paste in the style of text it was copied (like from Word). If you don't want that, you need to paste the text from Word as plain text into the EditorGadget, so it uses your EditorGadget font instead.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 8:52 am
by ZX80
Good time, BarryG.

Yes, you are absolutely right. I think so too. If I understand you correctly, you recommend disabling the advanced features of the gadget. Yes, I know it's off by default. To enable it, you need to use a special API function. These are obvious things. That would be too easy. While I'm writing this, have you had any ideas ? Well, it was included for a reason ! The problem or trick is that I need this functionality (such as rich text). In some cases, I need to highlight or mark certain sections of text.

So...
I repeat my question: how to do this ?
I think this is possible with the help of another, additional and imaginary editor gadget. It will never be shown. And it should have the "rich text" setting turned off. Further... When pasting formatted text into the main editor, you need to catch this event using a callback function. We don't insert this text into the main gadget yet, but we keep it in memory. We need to insert this text into the gadget without the "rich text" setting. This will remove all formatting and styles. And finally the final stage... Copy text from a hidden gadget to the main one. But this seems too confusing.. so I ask:

Is there a shorter and better solution to this problem ?

P.S. I can't predict in advance how users will insert text. Even this incident confused me because I hadn't even thought about it. However, I need to plan for this scenario, too.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 2:53 pm
by Axolotl
Perhaps I don't understand it correctly.
I tested it with RTF Text from WordPad (I don't use Word, etc..)
On my windows computer I have the situation you described if I use the Control+V Key-shortcut.
With the following procedures I get the plain text.

Code: Select all

GetClipboardText() 
SetGadgetText() 
So why not using a shortcut on the gadget to avoid that behavior?

Code: Select all

    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, 1)  ; 

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 2:55 pm
by ZX80
I was experimenting with the clipboard and completely by accident came up with this solution to this problem:

Code: Select all

;...

Case  #PB_Event_Menu
  Select EventMenu()
    Case #mPaste
      Debug "Paste"
      tmp$ = GetClipboardText()
      SetClipboardText(tmp$)
      SendMessage_(hWnd, #WM_PASTE, 0, 0)
      
;...
I don't understand how it works, but it works !
This resets the text styles, but the most interesting thing is that if you now paste the text from the buffer back into Word, all the styles are in place ! This is just some kind of magic ! :shock:

Can someone explain why this happens ?


Axolotl, thanks for your reply !
It seems like we were writing almost simultaneously. :D
Please, read what I wrote just above.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 3:18 pm
by RASHAD
RTF Editor Gadget
Drop down menu support
MS Windows short cut commands support

Code: Select all

OpenWindow(0, 200, 50, 640, 400,"RTF Load,Save & Print", #PB_Window_SystemMenu|#PB_Window_ScreenCentered |  #PB_Window_SizeGadget)
CreatePopupImageMenu(0,#PB_Menu_ModernLook) 
MenuItem(0, "&Open")
OpenSubMenu("&Save As..")
MenuItem( 1, "RTF File")
MenuItem( 2, "Plain Text")
CloseSubMenu()
MenuBar() 
MenuItem(3, "&Cut")
MenuItem(4, "&Copy")
MenuItem(5, "&Paste")
MenuBar()
MenuItem(6, "&Print")
MenuBar()
MenuItem(7, "&Quit")


EditorGadget(0, 10, 10, 620,350)
ButtonGadget(1,10,370,100,24,"Add Text")
SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a=a+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20  Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(Len(a.s)+1)
PokeS(*MemoryBuffer, a.s, Len(a.s)+1,#PB_Ascii)  
SendMessage_(GadgetID(0),#EM_REPLACESEL,0,PeekS(*MemoryBuffer ,Len(a.s)+1,#PB_Unicode))   

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 0
          ;           FileName$ = OpenFileRequester("", "", "RTF (*.rtf|*.rtf|Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
          ;           If FileName$
          ;             ;loadFile(FileName$)
          ;           EndIf          
        Case 1
          ;             FileName$ = SaveFileRequester("", "", "RTF (*.rtf|*.rtf|All files (*.*)|*.*", 0)
          ;             If FileName$            
          ;               ;SaveRTF(GadgetID(0),  FileName$)
          ;             EndIf            
        Case 2
          ;             FileName$ = SaveFileRequester("", "", "Text (*.txt)|*.txt|All files (*.*)|*.*", 0)
          ;             If FileName$
          ;               ;SaveText(GadgetID(0),  FileName$)
          ;             EndIf            
        Case 3
          SendMessage_(GadgetID(0),#WM_CUT,0,0)            
        Case 4
          ClearClipboard()
          SendMessage_(GadgetID(0),#WM_COPY,0,0)              
        Case 5
           SendMessage_(GadgetID(0),#WM_PASTE,0,0)         
        Case 6
          ;PrintRichText(WindowID(0), GetModuleHandle_(0), GadgetID(0), 10, 10, 20, 20)         
        Case 7
          Quit = 1          
      EndSelect
      
    Case #WM_RBUTTONDOWN
      DisplayPopupMenu(0, WindowID(0))
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          
      EndSelect
  EndSelect
Until Quit = 1
End  

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 3:26 pm
by Axolotl
Well, I am glad you found a solution.
ZX80 wrote: Wed Nov 12, 2025 2:55 pm Can someone explain why this happens ?
Sounds strange, and I cannot explain that.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 4:12 pm
by normeus
ZX80,

Code: Select all

The control pastes the first available format that it recognizes, which presumably is the most descriptive format.
From:
https://learn.microsoft.com/en-us/windo ... operations

The clipboard has the text with and without formatting
When you use PB's GetClipboardText(), you are getting text without formatting.


Norm.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 4:14 pm
by ZX80
RASHAD, thank you for your reply.

But my task is not about the capabilities of EditorGadget. Okay, let me explain again.
Run your code and you will see the red bold text "RTF". Then copy it to the clipboard and paste it into MS Word. The text will be exactly the same in Word. Excellent ! Imagine that someone has typed text in Word and now wants to transfer it to your window. So the question was how to make the initially red and bold text normal. So that it matches your settings and preferences. Okay. Let's say we did this by simply reading the buffer and assigning its contents to a string variable. However, I showed this above.

And now the most interesting part.
It would seem that all the styles have been removed, but if you now return to the MS Word window and try to paste text from the clipboard into it, then... You will be surprised to find that the original text style (red and bold) has been pasted.

The question: How is this possible ? :shock:

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 4:19 pm
by ZX80
Axolotl
Sounds strange, and I cannot explain that.
Me too ! :D


normeus
When you use PB's GetClipboardText(), you are getting text without formatting.
Okay ! Did you read my message above ? There I actually rewrite the clipboard data. Text without formatting !
How come when I paste it into Word the style is still the same ?

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 4:31 pm
by ZX80
RASHAD

Since you've touched on the topic of the pop-up menu, I have another question:

Is it possible to increase the size of the system popup menu window ?
I came across your code somewhere for increasing the font size in this menu. This is great !
But now another problem has arisen: the text of many menu items no longer fits there. :(

Please understand, I am writing this for a person with glasses and he is asking for a larger font.

Thanks in advance !

P.S.
to everyone: Sorry for three messages in a row. Yes, I know I can edit, but this makes it easier to understand when replying to multiple people.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 6:44 pm
by normeus
ZX80,
When windows pastes something, it chooses the best clip for the context, so if you paste into MS Word it will use the formatting it has in memory.
Axolotl's suggestion and yours works because you are requesting text without formatting using "GetClipboardText()".
When you copy it back into the clipboard, you are copying it to the memory slot for text not the one for formatted text.

There is one clipboard but depending on the context img,txt,etc they are saved in different locations.

Code: Select all

#mPasteText =1


If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 306, 133)
  AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, #mPasteText)  ; 
  For a = 0 To 2
    AddGadgetItem(0, a, "Text you have displayed" )
  Next
  Repeat 
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case   #mPasteText
            AddGadgetItem(0, -1, GetClipboardText() ) 
            ;get clipboardtext()  requests text from the clipboard, NOT "The best clip for the context"
        EndSelect
      Case  #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
Norm.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 7:47 pm
by ZX80
normeus

Thanks for your reply.
I think I'm starting to understand what you wanted to say. I guess this can be compared to a keyboard layout. Windows remembers the keyboard layout state for each window. It turns out that the same story applies to the clipboard. Thus, it turns out that in order to remove this effect, you need to clear the clipboard context. It is not enough to simply rewrite its contents. I don't know. I might be wrong.
... so if you paste into MS Word it will use the formatting it has in memory.
If I understand correctly, this memory refers specifically to the clipboard and is a different piece of memory than where the clipboard contents are stored. This is important !
When you copy it back into the clipboard, you are copying it to the memory slot for text not the one for formatted text.
I thought that the content was stored in its raw form and the description of that content was stored separately.

Re: EditorGadget - reset the style of the copied text.

Posted: Wed Nov 12, 2025 11:31 pm
by BarryG
ZX80 wrote: Wed Nov 12, 2025 4:14 pmThe question: How is this possible ? :shock:
The clipboard can actually hold multiple formats, and Windows chooses the best one for the current paste. It's nothing new.

Re: EditorGadget - reset the style of the copied text.

Posted: Thu Nov 13, 2025 1:29 am
by RASHAD

Code: Select all

OpenWindow(0, 200, 50, 640, 400,"RTF Load,Save & Print", #PB_Window_SystemMenu|#PB_Window_ScreenCentered |  #PB_Window_SizeGadget)
CreatePopupImageMenu(0,#PB_Menu_ModernLook) 
MenuItem(0, "&Open")
OpenSubMenu("&Save As..")
MenuItem( 1, "RTF File")
MenuItem( 2, "Plain Text")
CloseSubMenu()
MenuBar() 
MenuItem(3, "&Cut")
MenuItem(4, "&Copy")
OpenSubMenu("&Paste")
MenuItem( 5, "RTF")
MenuItem( 6, "Plain")
CloseSubMenu()
MenuBar()
MenuItem(7, "&Print")
MenuBar()
MenuItem(8, "&Quit")

EditorGadget(0, 10, 10, 620,350)
ButtonGadget(1,10,370,100,24,"Add Text")
SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a=a+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20  Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(Len(a.s)+1)
PokeS(*MemoryBuffer, a.s, Len(a.s)+1,#PB_Ascii)  
SendMessage_(GadgetID(0),#EM_REPLACESEL,0,PeekS(*MemoryBuffer ,Len(a.s)+1,#PB_Unicode))   
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V,10)
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()         
        Case 3
          SendMessage_(GadgetID(0),#WM_CUT,0,0)            
        Case 4
          ClearClipboard()
          SendMessage_(GadgetID(0),#WM_COPY,0,0)              
        Case 5
          SendMessage_(GadgetID(0),#WM_PASTE,0,0)         
        Case 6,10
          Text$ = GetClipboardText()
          AddGadgetItem(0,-1,text$)
        Case 7                 
        Case 8
          Quit = 1          
      EndSelect
      
    Case #WM_RBUTTONDOWN
      DisplayPopupMenu(0, WindowID(0))
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          
      EndSelect
  EndSelect
Until Quit = 1
End