Info & Rules (http://en.sfml-dev.org/forums/index.php?topic=21541.0) about this sub-forum
Note: Just to prevent potential confusion, this subforum is not just dedicated to SFML 3 discussions, but it's open to any SFML development topics like the this one right here.
Clipboard support has been a long requested feature (thread #1 (http://en.sfml-dev.org/forums/index.php?topic=1112.0), thread #2 (http://en.sfml-dev.org/forums/index.php?topic=12251.0)) for many obvious reasons. In the past we had one attempt (forum post (http://en.sfml-dev.org/forums/index.php?topic=16462.0), pull request (https://github.com/SFML/SFML/pull/715)) at getting it implemented, but the discussion dragged on for a while and after the API discussion was mostly finished, nobody took it up and actually implemented the suggestion, until now...
In the past few weeks Ricardo (aka Ricky) has been working on a new implementation: https://github.com/SFML/SFML/pull/1204
His implementation seems to mostly resemble what had been discussed and is up to date with SFML's code base (switch back to Xlib).
While this thread can be considered a continuation of Aster's thread (http://en.sfml-dev.org/forums/index.php?topic=16462.0), I think starting fresh makes more time. I will try to summarize previous discussions.
I also like to mainly focus on the implementation bits and less about how the functions should be named, as this is a minor detail and will easily distract from the important discussion points.
Proposed API
// Get the content of the clipboard as string data
sf::String str = sf::Clipboard::getString();
// Set the content of the clipboard as string data
sf::Clipboard::setString(const sf::String& str);
Requirements
- Linux (X11) requires access to a window's event processing and thus requires the use of a window.
- Windows claims in MSDN (https://msdn.microsoft.com/de-de/library/windows/desktop/ms649048(v=vs.85).aspx) to require a window handle to set the clipboard content, but in practice it seems to also work with a handle of NULL. For comparison, SDL and GLFW provide a window handle.
- OS X and iOS (https://stackoverflow.com/questions/6167557/get-string-from-nspasteboard/6167746#6167746) don't require a window at all.
- Android doesn't seem to require a window either.
Previous Decisions
- The clipboard should not be part of the sf::Window API, but it's own class.
- The use of a window is an implementation detail and should not be reflected in the public API.
Current implementation
- For Linux, the implementation uses a dedicated, hidden and lazy-loaded window with its own busy-waiting thread to handle the events.
- For Windows, there's no window handle used, instead NULL is passed to OpenClipboard.
Questions
- Is the Linux implementation acceptable for us?
- For Linux, should we instead try to use the global vector of windows that already exist in the window class? And how do we get access to the event processing of the given window?
- For Windows, there's currently no window handle used, should the implementation be adapted to use a window handle? And how should the clipboard class get access to an active window handle?
- For Linux and Windows, what should happen if there's no active window available?
Example code
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window/Clipboard.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(420, 380), "SFML Clipboard");
sf::Font font;
// Replace with your font file
if(!font.loadFromFile("./YourFont.ttf"))
{
std::cerr << "Failed to load font file." << std::endl;
return -1;
}
sf::Text text("Clipboard!", font);
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::KeyPressed)
{
if(event.key.control && event.key.code == sf::Keyboard::V)
text.setString(sf::Clipboard::getString());
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num1))
sf::Clipboard::setString("This is the first test!");
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num2))
sf::Clipboard::setString(L"Hello world, Καλημέρα κόσμε");
window.clear();
window.draw(text);
window.display();
}
}