开发者

OS independent clipboard copy/paste text in C

开发者 https://www.devze.com 2022-12-20 11:52 出处:网络
I\'m working on a project that\'s supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.

I'm working on a project that's supposed to work on both Windows and Linux (with an unofficial Mac port as well) that emulates a true colour system console.

My problem is that recently there appeared a request for textfield support (yes, consol开发者_如何学Ce-based) and it would be cool to add the possibility of copying text to clipboard and pasting from it. Is there a way of achieving this that will:

  • be done in C (not C++),
  • work in both Windows and in Linux (preprocessor macros are an option if there's no platform-independent code),
  • require no extra libraries to link to?

Thanks in advance for your help.


If you're not using a cross platform UI library (like wx or something), then it sounds like you're just going to have to write native clipboard code for each platform you want to support.

Remember, on Macintoshes, you copy with Command-C, not Ctrl+C :)


The clipboard is inherently an operating system defined concept. The C language itself has no knowledge of what a clipboard is or how to operate on it. You must either interface directly with the OS, or use a portability library that does this on your behalf. There is no way around this.


Personally I would define my your own function

getClipboardText();

That is defined in two different header files (linux_clipboard.h, windows_clipboard.h, etc) and then do pre-proccessor stuff to load the appropriate one accordingly. I don't really code in C/C++ so I'm sorry if that didn't make any sense or is bad practice but that's how I'd go about doing this.

#if WIN32
#include windows_clipboard.h
#endif

That sort of thing

Remember: For linux you have to deal with different window managers (Gnome, KDE) all with different ways of managing the clipboard. Keep this in mind when designing your app.


You may be able to communicate to the clipboard by using xclip. You can use this python script here to do this job via communicating with 'dcop' and 'klipper' here. That is for KDE, I do not know how it would be done under GNOME... You may also be able to do this independantly of either GNOME/KDE by using DBUS, although I cannot say 100% confidently on that either...

Just be aware, that for a truly cross-platform job, you have to take into account of the different GUI's such as under Linux, X is the main window manager interface and either GNOME/KDE sits on top of it..I am not singling out other GUI's such as FluxBox, WindowMaker to name but a few, and that there will be a lot of platform dependant code, and also in conjunction, you will be dealing with Windows clipboard as well..all in all, a big integrated code...

Have you not considered looking at the raw X programming API for clipboard support? Maybe that might be better as I would imagine, GNOME/KDE etc are using the X's API to do the clipboard work...if that is confirmed, then the work would be cut out and be independant of the major GUI interfaces...(I hope that would be the case as it would make life easier for your project!)

Perhaps using compile-time switches, for each platform...WIN, KDE, GNOME, MAC or use the one that is already pre-defined..

Hope this helps, Best regards, Tom.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号