Linux and clipboard - c++

In Linux after text selecting it copies to buffer, so than we could paste it by clicking middle button of the mouse. I think that there is a special buffer for this thing. I want to use it. How could i get data of selected text?
OS: Linux
Programming language: c++
Own libraries: Qt
Thanks.

Just a more accurate answer than Paul Dixon's that answers your needs:
QClipboard* clipboard = QApplication::clipboard();
QString selectedText = clipboard->text(QClipboard::Selection);

You need to distinguish between selection and clipboard. The QClipboard documentation has this in the Notes for X11 Users section:
The X11 Window System has the concept
of a separate selection and clipboard.
When text is selected, it is
immediately available as the global
mouse selection. The global mouse
selection may later be copied to the
clipboard. By convention, the middle
mouse button is used to paste the
global mouse selection.
With QClipboard::Mode you can select which type (clipboard or selection) you want to access. The important part is that you need to be aware about the difference between selection and clipboard.

If you're using Qt, have you read the fine manual page on QClipboard?
QClipboard *clipboard = QApplication::clipboard();
QString clipboardText = clipboard->text();

the system that actually handles the selection and pasting system is X11 Windows. When you e.g paint some text in your favorite editor, the application sends and X11 request which tells to the X11 server that you have an active selection. If you then click the middle mouse button somewhere, the X11 server queries the application which told the server about the selection for the actual contents. Then the contents are forwarded to the receiving application.
Libraries like Qt provide wrappers for this mechanism, but the underlying mechanism is X11.

Related

How to change selected text without mouse button release

I am writing small c++ application using QT creator and i have a problem, I want to get selecting text from any application, I am using to this QClipboard library (SIGNAL(selectionChanged())), but it doesn't work properly admittedly i am getting selected text, but only after I release a mouse button. I would like to get selected text in "real time" without mouse button up. Is there any simple way to do it?
first you need to add this header file: QClipboard
then...
connect(qApp->clipboard, SIGNAL(selectionChanged), this, SLOT(your_slot()));
void your_slot() {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(qApp->clipboard->text());
}
http://doc.qt.io/qt-5/qclipboard.html#selectionChanged
It looks like that is only supported well on X11, like Linux.
If you are interested in tracking mouse selections in your program in realtime, you can look directly at the mouse events or keyboard events, or the Rich Text Processing framework (QTextCursor).
You could also send a copy call while the mouse is down on a timer, and then look at the dataChanged signal.
Hope that helps.

Paste From QT Window to Another Application Input Widget

I am very new to C++ programming and the bulk of my program will be using the QT libraries. However, there is one part where I believe I will need to use Win32.
The scenario I want to code for is as follows:
I will have a QT application running. I want to be able to take some text which has been typed into a TextBox on the QT Window and paste that text into a TextBox in another application e.g. the address bar of Chrome, the address bar of Windows Explorer.
I want to be able to do that as a response to a button click on the QT Window. So, it would all happen in 3 steps. For example:
User types text into QT Window;
User places cursor in address bar of Chrome (Browser);
User clicks button on Window which pastes text into address bar of Chrome.
A nudge in the right direction would be most appreciated.
Edit - Additional Info
The application I’m building is a self-set assignment. I want to build a clipboard manager, similar to this old Delphi application http://www.joejoesoft.com/vcms/97/ . It will run in the system tray, in a minimised state.
The user, will put their focus into a text input in some application
which is running on their Windows machine e.g. Notepad.
Then, they will hit a hot key combination which will open a form (my QT Window.
The application will have been collecting clips as the user presses Ctrl-C (or by right-clicking) and those will be listed in that QT Form (just like the app in the link above).
The user then clicks on the particular item that they want paste and it will be pasted into the original input that they had put the cursor into.
Further Edit - further info
I'll break step 4 into a couple of sub-steps as it is causing confusion:
The user then clicks on the particular item that they want paste
Focus changes from QT Window back to the window of the other Win32 application which originally had focus
Content is pasted into the input control which now has the focus
I pretty much know how I can gather up items when the user copies things. But I have no idea how I will paste from my application to the target application.
Cheers

Adding custom controls to a console window

Is it possible to add custom controls to a console window? You can use GetConsoleWindow() to get the window's handle, and then add your own menu items or consume all its events. I can't find any examples of people adding extra controls though.
I am developing a small, high performance serial terminal app. It is a console application - RichTextBox is too slow and has issues that make it unsuitable for VT100 terminal emulation.
I want to add some little graphics to show the state of the serial control lines (RTS/CTS/DTR/RI etc.) and perhaps a capture on/off toggle button. Ideally I'd like to add them to the window title bar. Bitmaps are all that are required.
After lots of research I found that it isn't easy, or even possible really.
You can add controls to the window with CreateWindow(), but of course only in the client area which is taken up entirely by the console text box. However, you can at least create floating controls that way, which hover over the text to provide status info etc.
You can put controls in the window borders but only with some hacking on XP or a new API that was introduced with Vista. The problem with this API is that it requires you to draw your own program icon and title text, and the console window doesn't seem to cope with it very well.
You can't add your own menu items because the console window doesn't pass the messages.
In the end I used the function keys for everything and gave a status indication by changing the console window icon.

Can i use Global System Hooks to capture which file was clicked on?

I am new to Windows programming, mostly done Java(Java SE, Java ME, Android, Java EE), so be detailed and gentle.
I want to capture "the name of the file/path that was clicked in windows, like clicking a file on the desktop"?
Further research http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET, which is a small c#/c++ nice app that uses Global System Hooks, to capture mouse events such as coordinates,clicks,etc.
So what is the right API or Global System Hook that captures events on file icons?
There is no single API that provides that level of detail.
The WH_MOUSE and WH_MOUSE_LL hooks of SetWindowsHookEx(), or the WM_INPUT message delivered by RegisterRawInputDevices(), can tell when the mouse is being intereacted with, and the GetCursorPos() function can tell you where the mouse cursor is located onscreen at the time of a click, but it cannot tell you what it is clicking on. You have to figure that out manually.
For instance, the Desktop is implemented as a ListView control, so you can use the WindowFromPoint() and GetDesktopWindow() functions to check if the mouse is located at coordinates corresponding to the desktop window itself instead of an application window, and if so then use the LVM_HITTEST and LVM_GETITEM messages to determine which icon onthe desktop is being clicked on and extract its display text. Then use the SHGetDesktopFolder() function and the IShellFolder interface, or the SHParseDisplayName() function, to parse that text and see if it returns a PIDL that represents a path/file, and if so then use SHGetPathFromIDList() to get the actual path/file name.
If you want to do the same thing with the Windows Explorer app, it gets a bit more complicated. Use WindowFromPoint(), GetWindowThreadProcessId(), OpenProcess(), and EnumProcessModules() to determine if the mouse is over the Windows Explorer app. However, its UI changes from on Windows version to the next, but the jist is that you have to manually locate the focused control via AttachThreadInput() and GetActiveWindow(), check if it is a TreeView/ListView control, and if so then use control-specific messages to get information about the item/icon underneath the mouse cursor coordinates, and use IShellFolder again to figure out what the text of that item/icon actually represents.
Shell programming is very complex system and not for the feint of heart to interact with. So you need to ask yourself, why do you need this information in the first place?

Manipulating scrollbars in third-party application

I need to create an application which do the following:
At the beginning we have notepad window open with a lot of text in it.
Our application must scroll through this file and take notepad window screenshot after each scroll action.
I've tried to achieve this using SBM_GETRANGE, SBM_GETRANGE, SBM_SETPOS but it does not work for me.
Please note that emulating keyboard events (e.g. PageDown, PageUp) is not an option for me because this application should also work with other applications which may not support keyboard shortcuts for manipulating scrolls.
Thanks.
Don't try to manipulate the scrollbar directly - instead SetFocus() to the text window, then send Page Down messages. If there are applications where you must manipulate the scrollbar, you should get its window handle and send the messages there.