Porting the "clipboard example" to Blitzmax-NG (Win64)

Started by fielder, December 16, 2018, 11:27:41

Previous topic - Next topic

fielder

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

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...

Henri

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) Select


SuperStrict

Rem
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:Byte Ptr(Flags:Int, Bytes:Int)
Function GlobalFree(Mem:Byte Ptr)
Function GlobalLock:Byte Ptr(Mem:Byte Ptr)
Function GlobalUnlock(Mem:Byte Ptr)
End Extern
EndRem

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

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:Byte Ptr = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, Size_T( txt.Length+1 ))
Local DataBuf:Byte Ptr = GlobalLock(Memblock)
MemCopy DataBuf, TextBuf, Size_T( 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



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

col

Hiya all,

QuoteFor 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) Select
Function OpenClipboard%(hwnd:Byte Ptr)
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Henri

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

fielder

Quote from: Henri on December 16, 2018, 13:31:39
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) Select




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

Very thank you Henri!

Brucey

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! :-)

Derron

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

Brucey

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.

Derron

Quote from: Brucey on December 20, 2018, 16:05:00
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

fielder

Quote from: Brucey on December 20, 2018, 07:20:29
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 :)

markcwm

Wow, nice work Brucey.

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

fielder

#11
Quote from: Brucey on December 20, 2018, 07:20:29
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?

markcwm

#12
QuoteSomeone 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.

Derron

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

markcwm

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.