EditorGadget - reset the style of the copied text.
EditorGadget - reset the style of the copied text.
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 ?
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.
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.
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.
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.
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.
So why not using a shortcut on the gadget to avoid that behavior?
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()
Code: Select all
AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, 1) ;
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: EditorGadget - reset the style of the copied text.
I was experimenting with the clipboard and completely by accident came up with this solution to this problem:
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 !
Can someone explain why this happens ?
Axolotl, thanks for your reply !
It seems like we were writing almost simultaneously.
Please, read what I wrote just above.
Code: Select all
;...
Case #PB_Event_Menu
Select EventMenu()
Case #mPaste
Debug "Paste"
tmp$ = GetClipboardText()
SetClipboardText(tmp$)
SendMessage_(hWnd, #WM_PASTE, 0, 0)
;...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 !
Can someone explain why this happens ?
Axolotl, thanks for your reply !
It seems like we were writing almost simultaneously.
Please, read what I wrote just above.
Re: EditorGadget - reset the style of the copied text.
RTF Editor Gadget
Drop down menu support
MS Windows short cut commands support
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
Egypt my love
Re: EditorGadget - reset the style of the copied text.
Well, I am glad you found a solution.
Sounds strange, and I cannot explain that.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: EditorGadget - reset the style of the copied text.
ZX80,
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.
Code: Select all
The control pastes the first available format that it recognizes, which presumably is the most descriptive format.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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Re: EditorGadget - reset the style of the copied text.
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 ?
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 ?
Re: EditorGadget - reset the style of the copied text.
Axolotl
normeus
How come when I paste it into Word the style is still the same ?
Me too !Sounds strange, and I cannot explain that.
normeus
Okay ! Did you read my message above ? There I actually rewrite the clipboard data. Text without formatting !When you use PB's GetClipboardText(), you are getting 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.
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.
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.
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.
Norm.
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
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Re: EditorGadget - reset the style of the copied text.
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.
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.
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 !... so if you paste into MS Word it will use the formatting it has in memory.
I thought that the content was stored in its raw form and the description of that content was stored separately.When you copy it back into the clipboard, you are copying it to the memory slot for text not the one for formatted text.
Re: EditorGadget - reset the style of the copied text.
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.
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
Egypt my love


