tags:

views:

457

answers:

4

Hi. 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, console-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.

+4  A: 

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

Seth
Heh, thanks for the tip about Macs :). I've never really used one (well, once, but just for video editing). I hope the maintainer of the Mac port will be able to get around this :D. Oh, and in Linux, you copy just by highlighting the text :D.
mingos
@mingos Keep in mind that Linux might have multiple clipboards. There's the "highlight text" clipboard provided by the X server, and then the desktop environment (KDE, Gnome, etc) might provide its own Ctrl+C-style clipboard which may or may not share contents with the X clipboard.
Tyler McHenry
I believe relying on the X clipboard is the best way to go in my case, as it *should* require a single implementation for KDE, Gnome, XFCE and whatnot... at least I think so...
mingos
+4  A: 

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.

Tyler McHenry
Well, I seem to have stumbled upon some C code that is supposed to make the clipboard work in Windows: http://www.daniweb.com/code/snippet217173.html --- it needs no extra libraries and with a preprocessor #ifdef, I should be able to make it a Windows-specific part of the code. I don't know whether this snippet will work at all (I hope so), I'm still looking for something similar for Linux...
mingos
+3  A: 

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.

Chris T
This is precisely what I was thinking about. I just want to know whether there's a way of tackling this in C, and with no dependencies. I don't want to link to an extra library just for clipboard support. I think it's doable in Windows, after #including windows.h, but I'm still trying to figure out how to do that in Linux... Anyway, thanks for your response.
mingos
+1  A: 

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.

tommieb75
Hey, xclip seems interesting, I'll check out how they did it there. Thanks for the suggestion!
mingos