January 22, 2019, 06:13:46 PM

Author Topic: Porting the "clipboard example" to Blitzmax-NG (Win64)  (Read 432 times)

Offline fielder

  • Jr. Member
  • **
  • Posts: 52
Porting the "clipboard example" to Blitzmax-NG (Win64)
« on: December 16, 2018, 11:27:41 AM »
i'm again here... on these days i'm trying to "port" some old textual apps to 64 bit platform (Windows)
today i found that i have to remove all "clipboard" functions from sources because the Extern "win32" of all my applications can't be "processed" correctly in 64bit mode.

here the standard win-clipboard management for Win-Blitzmax
http://mojolabs.nz/posts.php?topic=58004

Code: [Select]
Strict

Extern "Win32"
Function OpenClipboard%(hwnd%)
Function CloseClipboard%()
Function EmptyClipboard%()
Function IsClipboardFormatAvailable%(format%)
Function GetClipboardData:Byte Ptr(Format:Int)
Function SetClipboardData(format%, hMem:Byte Ptr)
Function GlobalAlloc(Flags:Int, Bytes:Int)
Function GlobalFree(Mem:Int)
Function GlobalLock:Byte Ptr(Mem:Int)
Function GlobalUnlock(Mem:Int)
End Extern

' -----------------------------------------------

Function TextFromClipboard:String()
Const CF_TEXT%=$1
If Not OpenClipboard(0) Return ""
Local TextBuf:Byte Ptr = GetClipboardData(CF_TEXT)
CloseClipboard()
Return String.FromCString(TextBuf)
End Function

Function TextToClipboard(txt:String)
Const CF_TEXT%=$1
Const GMEM_MOVEABLE%=$2
Const GMEM_DDESHARE%=$2000
If txt$="" Return
Local TextBuf:Byte Ptr = Txt.ToCString()
Local Memblock:Int = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, txt.Length+1)
Local DataBuf:Byte Ptr = GlobalLock(Memblock)
MemCopy DataBuf, TextBuf, Txt.length
If OpenClipboard(0)
EmptyClipboard
SetClipboardData CF_TEXT, DataBuf
CloseClipboard
EndIf
GlobalUnlock Memblock
GlobalFree Memblock
End Function

' test

Print "Clipboard Test."
Print "==============="
Print "Enter a message for the clipboard."
Print "Alternatively, leave BLANK to read clipboard."

Local a:String

a=Input$(">")

If a=""
a=TextFromClipboard()
Print a
Else
TextToClipboard a
Print "Text sent to clipboard. Open NotePad and paste!"
EndIf

sorry again for all my requests.... but i very want to switch to NG...

Offline Henri

  • Full Member
  • ***
  • Posts: 179
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #1 on: December 16, 2018, 01:31:39 PM »
Hi,

I've only commented the external functions, as they are already declared somewhere else. Also I added cast from Int to Size_T. This is due to a fact that Size_T can hold 64-bit value, but Int is only 32-bit. For the same reason, memory-handles are byte ptr's.

Code: BlitzMax
  1.  
  2. SuperStrict
  3.  
  4. Rem
  5. Extern "Win32"
  6.         Function OpenClipboard%(hwnd%)
  7.         Function CloseClipboard%()
  8.         Function EmptyClipboard%()
  9.         Function IsClipboardFormatAvailable%(format%)
  10.         Function GetClipboardData:Byte Ptr(Format:Int)
  11.         Function SetClipboardData(format%, hMem:Byte Ptr)
  12.         Function GlobalAlloc:Byte Ptr(Flags:Int, Bytes:Int)
  13.         Function GlobalFree(Mem:Byte Ptr)
  14.         Function GlobalLock:Byte Ptr(Mem:Byte Ptr)
  15.         Function GlobalUnlock(Mem:Byte Ptr)
  16. End Extern
  17. EndRem
  18.  
  19. ' -----------------------------------------------
  20.  
  21. Function TextFromClipboard:String()
  22.         Const CF_TEXT%=$1
  23.         If Not OpenClipboard(0) Return ""
  24.         Local TextBuf:Byte Ptr = GetClipboardData(CF_TEXT)
  25.         CloseClipboard()
  26.         Return String.FromCString(TextBuf)
  27. End Function
  28.        
  29. Function TextToClipboard(txt:String)
  30.         Const CF_TEXT%=$1
  31.         Const GMEM_MOVEABLE%=$2
  32.         Const GMEM_DDESHARE%=$2000
  33.         If txt$="" Return
  34.         Local TextBuf:Byte Ptr = Txt.ToCString()
  35.         Local Memblock:Byte Ptr = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, Size_T( txt.Length+1 ))
  36.         Local DataBuf:Byte Ptr = GlobalLock(Memblock)
  37.         MemCopy DataBuf, TextBuf, Size_T( Txt.length )
  38.         If OpenClipboard(0)
  39.                 EmptyClipboard
  40.                 SetClipboardData CF_TEXT, DataBuf
  41.                 CloseClipboard
  42.         EndIf
  43.         GlobalUnlock Memblock
  44.         GlobalFree Memblock
  45. End Function
  46.  
  47. ' test
  48.  
  49. Print "Clipboard Test."
  50. Print "==============="
  51. Print "Enter a message for the clipboard."
  52. Print "Alternatively, leave BLANK to read clipboard."
  53.  
  54. Local a:String
  55.  
  56. a=Input$(">")
  57.  
  58. If a=""
  59.         a=TextFromClipboard()
  60.         Print a
  61. Else
  62.         TextToClipboard a
  63.         Print "Text sent to clipboard. Open NotePad and paste!"
  64. EndIf
  65.  


Ps. Observation: When I use Blitzmax code-tag, there is no select link.
- Got 01100011 problems, but the bit ain't 00000001

Offline col

  • Sr. Member
  • ****
  • Posts: 407
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #2 on: December 16, 2018, 02:14:05 PM »
Hiya all,

Quote
For the same reason, memory-handles are byte ptr's.
Correct, and for you records hwnd is also a handle that fits this specification.

Code: BlitzMax
  1. Function OpenClipboard%(hwnd:Byte Ptr)
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Henri

  • Full Member
  • ***
  • Posts: 179
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #3 on: December 16, 2018, 02:53:22 PM »
Yes, this is true. I was lazy and didn't bother to fix the definition, because I commented that part out, due to it being unnecessary for NG.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline fielder

  • Jr. Member
  • **
  • Posts: 52
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #4 on: December 19, 2018, 08:45:23 PM »
Hi,

I've only commented the external functions, as they are already declared somewhere else. Also I added cast from Int to Size_T. This is due to a fact that Size_T can hold 64-bit value, but Int is only 32-bit. For the same reason, memory-handles are byte ptr's.

Code: BlitzMax
  1.  
  2.  

Ps. Observation: When I use Blitzmax code-tag, there is no select link.

Very thank you Henri!

Offline Brucey

  • Jr. Member
  • **
  • Posts: 58
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #5 on: December 20, 2018, 07:20:29 AM »
FYI, I've added a cross-platform clipboard module ( https://github.com/maxmods/bah.mod/tree/master/clipboard.mod ) which should help a little.
Tested on win32/linux. I'll check it on macOS later.

Merry Holidays! :-)

Offline Derron

  • Hero Member
  • *****
  • Posts: 1557
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #6 on: December 20, 2018, 09:50:29 AM »
Think "clipboard" (and OS paths) belong to system-relevant modules - as "MaxGUI". In other words: it should be provided as a base module in BlitzMax.


Thanks for wrapping the lib.

bye
Ron

Offline Brucey

  • Jr. Member
  • **
  • Posts: 58
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #7 on: December 20, 2018, 04:05:00 PM »
Yes, it had crossed my mind at the time.

Maybe someone can raise a bmx-ng issue suggesting various mods for inclusion as a "built-in" module - perhaps not brl or pub, but something else.

Offline Derron

  • Hero Member
  • *****
  • Posts: 1557
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #8 on: December 20, 2018, 07:56:22 PM »
Maybe someone can raise a bmx-ng issue suggesting various mods for inclusion as a "built-in" module - perhaps not brl or pub, but something else.

Done.


bye
Ron

Offline fielder

  • Jr. Member
  • **
  • Posts: 52
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #9 on: December 21, 2018, 09:11:16 AM »
FYI, I've added a cross-platform clipboard module ( https://github.com/maxmods/bah.mod/tree/master/clipboard.mod ) which should help a little.
Tested on win32/linux. I'll check it on macOS later.

Merry Holidays! :-)

WOW! great :)

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 315
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #10 on: December 21, 2018, 11:35:07 PM »
Wow, nice work Brucey.

I always wanted a cross-platform clipboard, great news for Blitzmax editor apps. :)

Offline fielder

  • Jr. Member
  • **
  • Posts: 52
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #11 on: December 23, 2018, 10:04:08 AM »
FYI, I've added a cross-platform clipboard module ( https://github.com/maxmods/bah.mod/tree/master/clipboard.mod ) which should help a little.
Someone tested it on MacOS?

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 315
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #12 on: December 24, 2018, 01:29:39 AM »
Quote
Someone tested it on MacOS?
Yes, it worked in NG and BRL/OS Blitzmax on Mac 10.10.
It also worked in NG on 64 bit Linux (Ubuntu) but I haven't tested on 32 bit Linux [edit: yes it works].
And it worked in NG and BRL/OS Blitzmax on Win7 64 bit.

@Brucey: I noticed that when you end the program the text is still in the clipboard on Win/Mac but gets erased on Linux. This also happens in MaxIDE but not other programs like Gedit, so it must be a Blitzmax quirk and not a Linux one. Other than that the module seems perfect.

Offline Derron

  • Hero Member
  • *****
  • Posts: 1557
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #13 on: December 24, 2018, 09:16:19 AM »
That is not really a bug but a behaviour of the "X Server". It relies on the application to provide the "clipboard data". At least it was this way some "years" ago - dunno if it is the same now.

--- here someone states the same:
https://wiki.ubuntu.com/ClipboardPersistence


bye
Ron

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 315
Re: Porting the "clipboard example" to Blitzmax-NG (Win64)
« Reply #14 on: December 24, 2018, 04:20:22 PM »
Thanks Derron, that's great, I thought it might be the way Xserver works.

The interesting thing is there is a nice Gtk solution - set Gtk.Clipboard.set_can_store when the clipboard is "owned" and then save with gtk_clipboard_store (or Gtk.Clipboard.store) before quitting.

Unfortunately these functions aren't found in the clipboard library Brucey wrapped, so probably not worth the time trying to add it.