Read and write clipboard (Linux and Windows)
Read and write clipboard (Linux and Windows)
I've been having problems with this for a long time. I remember in the past, I managed to read and write the Windows clipboard and recently, I've been able to read, but not write, X clipboard in GNU/Linux. I find examples online, but they are not at all easy to translate to FreeBasic. What I need is very simple and I'm pretty sure it's a frequent problem. Yet, when looking up in the forum, I find many references to the word "clipboard" that don't quite ask this, so it's like finding a needle in a haystack, ha, ha.
So what I want is to make a function to get a string from the clipboard and another to store a string in the clipboard. I know for X there are three. Enough for me with the one that's called "clipboard", but if I can use the three, much better. I understand that I will have to use different APIs for each, so this is more a Linux AND Windows question than a general question. Can you guys point me to some examples? The simpler the code, the better. Thanks!
So what I want is to make a function to get a string from the clipboard and another to store a string in the clipboard. I know for X there are three. Enough for me with the one that's called "clipboard", but if I can use the three, much better. I understand that I will have to use different APIs for each, so this is more a Linux AND Windows question than a general question. Can you guys point me to some examples? The simpler the code, the better. Thanks!
Re: Read and write clipboard (Linux and Windows)
I know this does not answer your question, but if you're open to some alternative solutions to working directly with the APIs yourself, here's what might be a pretty simple, portable clipboard library written in C that you can probably interface with from FB:
https://github.com/jtanx/libclipboard
That's the way I'd go, rather than much about with libxcb directly.
https://github.com/jtanx/libclipboard
That's the way I'd go, rather than much about with libxcb directly.
Re: Read and write clipboard (Linux and Windows)
Thanks.... but it really is a shame that people have gotten used to immediately rely on third-party software as soon as a difficulty comes up. In the end, this results in nobody really knowing how things work and possibly thousands of projects having a bug because it's in the library. In this case, it should be just about five lines of code to push a string to the clipboard and the same again for reading it. It's a simple memory assignment. How come!
I've solved the GNU/Linux part by just calling the "xclip" external command for now. This is OK because for GNU, my plan is to only use this little project myself, but I'm going to share it with Windows users, so I would like to avoid dependencies for them. The examples (for Windows) I'm reading online appear to reserve some "global" memory. I'm not sure which of these functions and types exist in the "windows.bi" file and what they really mean, so I'm trying to study the thing.
Don't take me wrong. I really appreciate your suggestion. I just don't like the reality we're facing nowadays, that's all.
I've solved the GNU/Linux part by just calling the "xclip" external command for now. This is OK because for GNU, my plan is to only use this little project myself, but I'm going to share it with Windows users, so I would like to avoid dependencies for them. The examples (for Windows) I'm reading online appear to reserve some "global" memory. I'm not sure which of these functions and types exist in the "windows.bi" file and what they really mean, so I'm trying to study the thing.
Don't take me wrong. I really appreciate your suggestion. I just don't like the reality we're facing nowadays, that's all.
Re: Read and write clipboard (Linux and Windows)
Fair enough. I agree that in some ecosystems like node.js vast repos of un-provinanced code libraries are a real liability. But on the other hand the ready availability of code libraries that are open source is a huge advantage that we just didn't have when I started programming years ago. At least not to the same scale, and the idea of keeping the source open was not as common back then. At the very least a library like the clipboard library is small enough to read and understand so you can see how the calls are made.
Re: Read and write clipboard (Linux and Windows)
Here are some clipboard things for Windows.
Code: Select all
#include "windows.bi"
#include "file.bi"
Const w = 800
Const _h = 600
#INCLUDE "fbgfx.bi"
#INCLUDE ONCE "windows.bi"
Function get_clipboard_ansi() As String
If IsClipboardFormatAvailable(CF_TEXT) = 0 Then Return "Error"
If OpenClipboard(0) = 0 Then Return "Error"
Function = *Cast(zstring Ptr,GetClipboardData(CF_TEXT))
CloseClipboard()
End Function
Sub SaveToClipBoard_ansi(Text As String) 'write Text to the clipboard
Var Num=GlobalAlloc(GMEM_MOVEABLE,Len(Text)+1)
memcpy(GlobalLock(num), @text[0], Len(Text)+1)
Var Chars=GlobalLock(Num)
GlobalUnlock(Num)
OpenClipboard(0)
EmptyClipboard()
SetClipboardData(CF_TEXT,Chars)
CloseClipboard()
End Sub
Function Set_Clipboard_bitmap(bitmap As String) As HBITMAP
Dim hBmp As HANDLE
hBmp = LoadImage(0, bitmap, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE)
OpenClipboard(0)
EmptyClipboard()
SetClipboardData(CF_BITMAP, CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG))
CloseClipboard()
Return NULL
End Function
Function Get_Clipboard_bitmap() As HBITMAP
Dim hBmp As HANDLE
If IsClipboardFormatAvailable(CF_BITMAP) = 0 Then
Return NULL
end if
If OpenClipboard(0) = 0 Then
Return NULL
end if
Function = GetClipboardData(CF_BITMAP)
CloseClipboard()
End Function
sub get_bitmap_size(bmp As String,byref hght as long,byref wdth as long) 'fetch bitmap width/height
Open bmp For Binary As #1
Get #1, 19, wdth
Get #1, 23, hght
Close #1
End sub
'demo
Sub drawbitmaptoimage(path As String,x as long=0,y as long=0)
If Fileexists(path) Then
Set_Clipboard_bitmap(path)
windowtitle path
Else
Print path;" not found"
Sleep
End
End If
Dim As BITMAPINFO BitmapInfo
Dim As HDC display_dc = GetDC( NULL )
Dim As HDC bitmap_dc = CreateCompatibleDC( display_dc )
Dim As HBITMAP bitmap = CreateCompatibleBitmap( display_dc, w, _h )
Dim As HGDIOBJ null_bitmap = SelectObject( bitmap_dc, bitmap )
bitmap = Get_Clipboard_bitmap()
dim as long localW,localH
get_Bitmap_Size(path,localW,localH)
Dim As fb.image Ptr LocalImage = Imagecreate( localW, localH)
LocalImage->pitch = LocalImage->Width * 4
With BitmapInfo.bmiHeader
.biSize = Sizeof(BITMAPINFOHEADER)
.biWidth = localW
.biHeight = -localH
.biPlanes = 1
.biBitCount = 32
End With
getdibits( bitmap_dc, bitmap, 0, localH, LocalImage + 1, @BitmapInfo, DIB_RGB_COLORS )
SelectObject( bitmap_dc, null_bitmap )
DeleteDC( display_dc )
DeleteDC( bitmap_dc )
DeleteObject( bitmap )
Put(x, y), LocalImage,trans
End Sub
dim as string s="Please press any key to continue . . ."
SaveToClipBoard_ansi(s)
print get_clipboard_ansi()
sleep
Screenres w, _h, 32,,64
color ,rgb(200,200,200)
cls
'create a silly bitmap
dim as any ptr i=imagecreate(500,500)
for n as long=1 to 50
circle i,(rnd*500,rnd*500),10+rnd*50,rgba(rnd*255,rnd*255,rnd*255,100+rnd*155),,,,f
next n
line i,(0,0)-(499,499),,b
bsave "circles.bmp",i
'=============
Dim As String path="Circles.bmp" 'OR YOUR PATH
drawbitmaptoimage(path,100,50)
Sleep
Re: Read and write clipboard (Linux and Windows)
Thank you so much! Yes, I've seen that, indeed, according to this source, it's super simple to implement clipboard read and write for text, which would be enough for FB, I think. In GNU/Linux X11, I managed to more or less easily read the clipboard. Writing to the clipboard is more complicated because your program has to actively listen to requests and send the text, that is, there is no external clipboard manager in the OS, unless you install one, which by default, is not the case.
I tried to translate some C code to FreeBasic for GNU/Linux clipboard management, but the part that sends the clipboard caused the program to crash and I don't know why. For now, this Windows code is useful enough and I'll keep using "xclip" for GNU/Linux. When I have a more important project to make, I'll try again with that code.
I tried to translate some C code to FreeBasic for GNU/Linux clipboard management, but the part that sends the clipboard caused the program to crash and I don't know why. For now, this Windows code is useful enough and I'll keep using "xclip" for GNU/Linux. When I have a more important project to make, I'll try again with that code.
Re: Read and write clipboard (Linux and Windows)
Commandline utility xsel, via whatever widgetset you use (gtk,qt), perhaps via dbus.
https://newbedev.com/a-command-line-cli ... te-utility
https://newbedev.com/a-command-line-cli ... te-utility
Re: Read and write clipboard (Linux and Windows)
Congratulations. You now have a third party library in your project that you don't understand that has at least three buggy functions in it. Your nigtmares have become real! I hope your other nightmares involve big fluffy dogs or you might just kill us all! :-)xlucas wrote:Thank you so much!
Re: Read and write clipboard (Linux and Windows)
A very supercilious and negative attitude adeyblue.
Freebasic is getting a bit of a bad report abroad, I can see clearly why.
If you think things are buggy why don't you fix them in a Lady like or gentlemanly fashion.
Freebasic is getting a bit of a bad report abroad, I can see clearly why.
If you think things are buggy why don't you fix them in a Lady like or gentlemanly fashion.
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Read and write clipboard (Linux and Windows)
Do you consider the WIN API a third party library? And hey, is anybody here who really understands that API ?adeyblue wrote:You now have a third party library in your project that you don't understand
Re: Read and write clipboard (Linux and Windows)
The WIN API is extremely well documented, and backed by Microsoft. There is an awful lot to criticise (I'm the first to bark at Bill Gates), but I doubt it's comparable to what adeyblue has in mind. The Internet is full of abandoned projects, that's not the case with Windows.grindstone wrote:Do you consider the WIN API a third party library? And hey, is anybody here who really understands that API ?adeyblue wrote:You now have a third party library in your project that you don't understand
Still, I am in strongly in favour of adding set and read clipboard commands to the FB core language. This forum is full of the most exotic debates on how to mimic the latest C/C++ gadget in FB, and how to work around strange compiler errors when using exotic toolchains; I wonder if it wasn't more urgent to add some frequently used BASIC features to FB. Such as the clipboard.
Re: Read and write clipboard (Linux and Windows)
No of course not. But dodicat's code, which is no doubt fairly correct, is the very third-party code library that @xlucas was complaining about. It has the same potential issues as any other code library: it's not well understood by xlucas and there is always a possibility that it has a bug in it. That's the point that @adeyblue was making not so subtly. To complain about the issues with third-party libraries and then to use a third party library is ironic! There's no difference between using @dodicat's code and using the cross-platform library I mentioned earlier. I don't see a problem with using either library.grindstone wrote:Do you consider the WIN API a third party library? And hey, is anybody here who really understands that API ?
-
- Posts: 4310
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Read and write clipboard (Linux and Windows)
That surprised me as it is not something that I expect of adeyblue. Perhaps written at the end of a bad day. I would have said something myself, but I can imagine some saying: "Here we go, deltarho has his woke hat on again".dodicat wrote:A very supercilious and negative attitude adeyblue.
Agreed. PowerBASIC has nine clipboard keywords, Windows based, of course. I don't know when, exactly, they were introduced, but it looks like the first time I used the keywords was in 2008, before which the Windows APIs were used.jj2007 wrote:IMHO a BASIC language in the 21st century should have a few built-in commands for doing this:
Still, I am in strongly in favour of adding set and read clipboard commands to the FB core language.
Most Windows APIs return a boolean with zero as a failure. If we 'hit' the clipboard commands too quickly, they can 'choke'. I learnt that the hard way. I won't bore you with the details, but I was hitting the clipboard like a machine gun. PB's commands return a boolean, so I use this, for example:
Code: Select all
Do
Clipboard Set Text ClipContents, ClipResult
Loop Until IsTrue ClipResult
NB: When publishing applications on the Internet, I check every Windows API's return values. Any failures result in a 'Goto Errortrap' where we are told which API failed. Encrypternet uses 33 such Gotos.
-
- Posts: 564
- Joined: Sep 27, 2016 18:20
- Location: Valencia, Spain
Re: Read and write clipboard (Linux and Windows)
@deltarho[1859]
In AfxWin.inc you have several clipboard functions, among them AfxSetClipboardText and AfxGetClipboardText (both are Unicode aware).
In AfxWin.inc you have several clipboard functions, among them AfxSetClipboardText and AfxGetClipboardText (both are Unicode aware).