Clipboard and non breaking space

Just starting out? Need help? Post your questions and find answers here.
archeus
New User
New User
Posts: 4
Joined: Fri Aug 19, 2022 10:53 am

Clipboard and non breaking space

Post by archeus »

Hey folks,

I start my journey with purebasic. I coded a small utility that retrieves the content of the clipboard and replaces some characters with others (html entities).

It works very well except for non-breaking-space.

While testing I realized that the problem seems to come from the retrieval of the text content in the clipboard : the non-breaking-space are replaced by spaces automatically.

Code: Select all

Debug "Char 160 : " + FindString(GetClipboardText(), Chr(160))
Debug "Char A0 : " + FindString(GetClipboardText(), Chr($A0))
I've read tons of discussions about unicode etc but I'm getting more and more confused and nothing I've tried works...

For your information, in the ide, my files are encoded in utf8. I'm running windows 10 as OS.

Does anyone have a suggestion?
User avatar
NicTheQuick
Addict
Addict
Posts: 1223
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Clipboard and non breaking space

Post by NicTheQuick »

Are you sure the non-breaking spaces are really copied to the clipboard in the first place?
I am asking this because in the last company I worked we had problems with copying non-breaking spaces from the browser too and all that because there was was a really annoying bug on all Mozilla products open for 16 years regarding this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=359303
It is closed now but I don't know if it find its way to production yet.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Axolotl
Enthusiast
Enthusiast
Posts: 435
Joined: Wed Dec 31, 2008 3:36 pm

Re: Clipboard and non breaking space

Post by Axolotl »

Hey archeus,
welcome to PB.
The problem is definitely not on the PB side.

If you want check it by yourself, you can use the small test app I put together as a proof of concept

Code: Select all

; -----------------------------------------------------------------------------
;                           ClipBoard Handling
; -----------------------------------------------------------------------------

EnableExplicit 
DebugLevel 9  

#Caption = "Example of Clipboard " 
#TimerID = 1 

Enumeration EWindow 1 
  #WndMain 
EndEnumeration 

Enumeration EGadget 1 
  #ChkDirectUpdate 
  #ChkWatchClipboard 

  #StrText
  #StrHex 
EndEnumeration

; -----------------------------------------------------------------------------
Define s_hNextClipboardListener = 0 


; -----------------------------------------------------------------------------
Procedure ClipboardListener(Window, State) 
  Shared s_hNextClipboardListener 

  Select State
    Case 0 
      If s_hNextClipboardListener <> 0                          :Debug #PB_Compiler_Procedure + "() --> exit ", 9 
        ChangeClipboardChain_(WindowID(Window), s_hNextClipboardListener) 
        s_hNextClipboardListener = 0        
      EndIf
      
    Case 1
      If s_hNextClipboardListener = 0                           :Debug #PB_Compiler_Procedure + "() --> init ", 9 
        ; so, now we are informed of clipboard changes 
        s_hNextClipboardListener = SetClipboardViewer_(WindowID(Window)) 
      EndIf
  EndSelect 
 ;ProcedureReturn State 
EndProcedure 


; -----------------------------------------------------------------------------

Procedure.s TextToHex(Text.s) 
  Protected result.s, n, txlen , cnt  

  If Text <> "" 
    txlen = Len(Text) 
    result = ""
    For n = 1 To txlen 
      result + RSet(Hex(Asc(Mid(Text, n, 1))), 4, "0") + " " 
      cnt + 1 
      If cnt >= 16 
        result + #CRLF$ 
        cnt = 0 
      EndIf 
    Next 
  EndIf 
  ProcedureReturn result 
EndProcedure ;() 


; -----------------------------------------------------------------------------
Procedure MainWindow_OnCallBack(hWnd, uMsg, wParam, lParam)  
  Shared s_hNextClipboardListener 
  Protected result = #PB_ProcessPureBasicEvents 
  Protected txt.s, t.s

  Select uMsg 
    Case #WM_CHANGECBCHAIN                                     :Debug "#WM_CHANGECBCHAIN", 9 
      If wParam = s_hNextClipboardListener
        s_hNextClipboardListener = lParam 
      ElseIf s_hNextClipboardListener <> 0
        SendMessage_(s_hNextClipboardListener, uMsg, wParam, lParam)
      EndIf
      result = 1

    Case #WM_DRAWCLIPBOARD                                     :Debug "#WM_DRAWCLIPBOARD", 9 
      txt = GetClipboardText() ; Show new content 

      SetGadgetText(#StrText, txt) 
      SetGadgetText(#StrHex, TextToHex(txt)) 

      SendMessage_(s_hNextClipboardListener, uMsg, wParam, lParam) 
      result = 1
  EndSelect 
  ProcedureReturn result 
EndProcedure 


; -----------------------------------------------------------------------------

Procedure OnEventSizeWindow() 
  Protected wndW, wndH

  wndW = WindowWidth(#WndMain) 
  wndH = WindowHeight(#WndMain) - 32
  wndH / 2 
  ResizeGadget(#StrText, #PB_Ignore, #PB_Ignore, wndW, wndH-2) 
  ResizeGadget(#StrHex, #PB_Ignore, wndH+34, wndW, wndH-2) 
EndProcedure 

Procedure main()
  Protected quitLoop, wndW, wndH, flags  
  Protected hFont 
  Protected counter 

  hFont  = FontID(LoadFont(#PB_Any, "Consolas", 8)) 
  flags = #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_Invisible
  wndW = 640 : wndH = 320
  If OpenWindow(#WndMain, 200, 200, wndW, wndH, #Caption, flags)  
    CheckBoxGadget(#ChkDirectUpdate, 208, 8, 160, 18, "Direct Update") 
    CheckBoxGadget(#ChkWatchClipboard, 8, 8, 160, 20, "Watch Clipboard") 
   
    wndH - 32 
    wndH / 2  
    flags = #ES_MULTILINE | #ES_AUTOVSCROLL | #WS_VSCROLL | #WS_HSCROLL | 
           ;#PB_String_BorderLess | 
           ;#PB_String_ReadOnly | 
            0 
   
    StringGadget(#StrText, 0, 32, wndW, wndH-2, "", flags) 
    SetGadgetFont(#StrText, hFont)

    StringGadget(#StrHex, 0, wndH + 34, 640, wndH-2, "", flags) 
    SetGadgetFont(#StrHex, hFont) 

    BindEvent(#PB_Event_SizeWindow, @OnEventSizeWindow(), #WndMain) 
    SetWindowCallback(@MainWindow_OnCallBack(), #WndMain) 
    HideWindow(#WndMain, 0)

    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_Timer 
          counter + 1 
          SetClipboardText("Space   Space        . counting = " + counter) 
          Beep_(80, 80) 

        Case #PB_Event_CloseWindow
          quitLoop = #True

        Case #PB_Event_Gadget
          Select EventGadget()
            Case #ChkDirectUpdate 
              If GetGadgetState(#ChkDirectUpdate) 
                counter = 0 
                AddWindowTimer(#WndMain, 1, 1000) 
              Else 
                RemoveWindowTimer(#WndMain, 1) 
              EndIf 

            Case #ChkWatchClipboard 
              ClipboardListener(#WndMain, GetGadgetState(#ChkWatchClipboard)) 
              
          EndSelect
      EndSelect
    Until quitLoop
  EndIf
  ProcedureReturn 0
EndProcedure

End main()
Mostly running PureBasic <latest stable version and current alpha/beta> (x64) on Windows 11 Home
archeus
New User
New User
Posts: 4
Joined: Fri Aug 19, 2022 10:53 am

Re: Clipboard and non breaking space

Post by archeus »

Thanks to you, NicTheQuick and Axolotl.

You were both right.

My IDE was not sending a non-breaking space to the clipboard, but a single space.

I made the mistake of using MS word as the editor to generate the non-breaking space and make the "copy".

The Axolotl code snippet showed me the problem immediately.

Thank you both for your quick and really helpful answers.
Post Reply