wxWidgets: Threading, and using the Clipboard
Two for one in this tutorial. We're going to create a thread using wxWidgets, and make it monitor the clipboard for text. Then, if we detect a change, we're going to add the new contents to a list. It's a relatively simple process, but unfortunately wxWidgets isn't the most documented GUI toolkit out there. So, let's get started. I'm gonna pile all the code into a single file instead of breaking my classes up into seperate files, just for ease of navigation in this tutorial. The code is relatively short anyway, with only 62 lines. First off, as with any other program, we're going to want to get our includes done. We'll be needing three: The standard wx header, the clipboard header, and the thread header.#include <wx/wx.h> // standard wx header #include <wx/clipbrd.h> // clipboard - we'll monitor the clipboard for text #include <wx/thread.h> // include threads!
ClipLogger class - this will inherit wxThread, and have two variables: One to hold the most recent text that we got from the clipboard, and one which is a pointer to a wxListBox object -- the object on the main window which we'll be updating.
class ClipLogger : public wxThread { public: ClipLogger(wxListBox*); private: void* Entry(); // the entry point to the thread wxString LatestText; // store the last text so we can check for changes wxListBox* list; // the list to update on a text change };
ClipLogger::ClipLogger(wxListBox* l) { this->list = l; }
Entry() method of the class, like so:
void* ClipLogger::Entry() {
wxTextDataObject temp;while (true) {
wxSleep(int time), where time is the length of time to sleep in seconds.
wxSleep(1);
temp variable
- Update the list and remember this is the most recent (so as not to constantly add the same data to the list)
- Close the clipboard
if (wxTheClipboard->Open()) { // try to open the clipboard if (wxTheClipboard->IsSupported(wxDF_TEXT)) { // if the clipboard contains text wxTheClipboard->GetData(temp); // get the data from the clipboard if (this->LatestText != temp.GetText()) { // if it's changed, we want to update if (temp.GetText() != wxT("")) { // if it's not an empty string this->LatestText = temp.GetText(); // update the "LatestText" this->list->Append(temp.GetText()); // append to the list } } } } wxTheClipboard->Close(); // close the clipboard
} }
class threaded_app : public wxApp { public: bool OnInit(void); };
OnInit() should be nothing new at this point either.
bool threaded_app::OnInit(void) { // quickly create a wxFrame to display a window wxFrame* f = new wxFrame(NULL, wxID_ANY, wxT("Threaded App!")); // add a list to the frame we created wxListBox* list = new wxListBox(f, wxID_ANY); // display the frame f->Show(true);
OnInit() that we need to do is actually create an instance of our thread and run it, like so:
// pass the list to the clipboard monitor so it knows what to update ClipLogger* cl = new ClipLogger(list); // construct our thread cl->Create(); // we have to create a thread before we can run it cl->Run(); // run our thread
OnInit() and IMPLEMENT_APP:
return true; } IMPLEMENT_APP(threaded_app)
#include <wx/wx.h> // standard wx header #include <wx/clipbrd.h> // clipboard - we'll monitor the clipboard for text #include <wx/thread.h> // include threads! class ClipLogger : public wxThread { public: ClipLogger(wxListBox*); private: void* Entry(); // the entry point to the thread wxString LatestText; // store the last text so we can check for changes wxListBox* list; // the list to update on a text change }; ClipLogger::ClipLogger(wxListBox* l) { this->list = l; } void* ClipLogger::Entry() { wxTextDataObject temp; // create a "wxTextDataObject" to get the info from the clipboard while (true) { // our thread will loop wxSleep(1); // sleep for 1 second, make the thread less cpu-hungry if (wxTheClipboard->Open()) { // try to open the clipboard if (wxTheClipboard->IsSupported(wxDF_TEXT)) { // if the clipboard contains text wxTheClipboard->GetData(temp); // get the data from the clipboard if (this->LatestText != temp.GetText()) { // if it's changed, we want to update if (temp.GetText() != wxT("")) { // if it's not an empty string this->LatestText = temp.GetText(); // update the "LatestText" this->list->Append(temp.GetText()); // append to the list } } } } wxTheClipboard->Close(); // close the clipboard } } class threaded_app : public wxApp { public: bool OnInit(void); }; bool threaded_app::OnInit(void) { // quickly create a wxFrame to display a window wxFrame* f = new wxFrame(NULL, wxID_ANY, wxT("Threaded App!")); // add a list to the frame we created wxListBox* list = new wxListBox(f, wxID_ANY); // display the frame f->Show(true); // pass the list to the clipboard monitor so it knows what to update ClipLogger* cl = new ClipLogger(list); // construct our thread cl->Create(); // we have to create a thread before we can run it cl->Run(); // run our thread return true; } IMPLEMENT_APP(threaded_app)
Popularity: 54% [?]