| 1 |
/* gtkmm example Copyright (C) 2002 gtkmm development team |
| 2 |
* |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "examplewindow.h" |
| 18 |
#include <iostream> |
| 19 |
#include <string> |
| 20 |
|
| 21 |
namespace |
| 22 |
{ |
| 23 |
|
| 24 |
const char example_format_custom[] = "gtkmmclipboardexample"; |
| 25 |
|
| 26 |
} // anonymous namespace |
| 27 |
|
| 28 |
|
| 29 |
ExampleWindow::ExampleWindow() |
| 30 |
: m_VBox(Gtk::Orientation::VERTICAL), |
| 31 |
m_Label("Select cells in the grid, click Copy, then open a second instance " |
| 32 |
"of this example to try pasting the copied data.\nOr try pasting the " |
| 33 |
"text representation into gedit."), |
| 34 |
m_ButtonA1("A1"), m_ButtonA2("A2"), m_ButtonB1("B1"), m_ButtonB2("B2"), |
| 35 |
m_Button_Copy("_Copy", /* mnemonic= */ true), m_Button_Paste("_Paste", true) |
| 36 |
{ |
| 37 |
set_title("Gtk::Clipboard example"); |
| 38 |
|
| 39 |
m_VBox.set_margin(12); |
| 40 |
set_child(m_VBox); |
| 41 |
|
| 42 |
m_VBox.append(m_Label); |
| 43 |
|
| 44 |
//Fill Grid: |
| 45 |
m_VBox.append(m_Grid); |
| 46 |
m_Grid.set_expand(true); |
| 47 |
m_Grid.set_row_homogeneous(true); |
| 48 |
m_Grid.set_column_homogeneous(true); |
| 49 |
m_Grid.attach(m_ButtonA1, 0, 0); |
| 50 |
m_Grid.attach(m_ButtonA2, 1, 0); |
| 51 |
m_Grid.attach(m_ButtonB1, 0, 1); |
| 52 |
m_Grid.attach(m_ButtonB2, 1, 1); |
| 53 |
|
| 54 |
//Add ButtonBox to bottom: |
| 55 |
m_VBox.append(m_ButtonBox); |
| 56 |
m_VBox.set_spacing(6); |
| 57 |
|
| 58 |
//Fill ButtonBox: |
| 59 |
m_ButtonBox.append(m_Button_Copy); |
| 60 |
m_Button_Copy.set_hexpand(true); |
| 61 |
m_Button_Copy.set_halign(Gtk::Align::END); |
| 62 |
m_Button_Copy.signal_clicked().connect(sigc::mem_fun(*this, |
| 63 |
&ExampleWindow::on_button_copy) ); |
| 64 |
m_ButtonBox.append(m_Button_Paste); |
| 65 |
m_Button_Paste.signal_clicked().connect(sigc::mem_fun(*this, |
| 66 |
&ExampleWindow::on_button_paste) ); |
| 67 |
|
| 68 |
//Connect a signal handler that will be called when the contents of |
| 69 |
//the clipboard change. |
| 70 |
get_clipboard()->signal_changed().connect(sigc::mem_fun(*this, |
| 71 |
&ExampleWindow::on_clipboard_content_changed)); |
| 72 |
|
| 73 |
update_paste_status(); |
| 74 |
} |
| 75 |
|
| 76 |
ExampleWindow::~ExampleWindow() |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
void ExampleWindow::on_button_copy() |
| 81 |
{ |
| 82 |
//Build a string representation of the stuff to be copied: |
| 83 |
//Ideally you would use XML, with an XML parser here: |
| 84 |
m_strData = example_format_custom; |
| 85 |
m_strData += m_ButtonA1.get_active() ? " A1" : ""; |
| 86 |
m_strData += m_ButtonA2.get_active() ? " A2" : ""; |
| 87 |
m_strData += m_ButtonB1.get_active() ? " B1" : ""; |
| 88 |
m_strData += m_ButtonB2.get_active() ? " B2" : ""; |
| 89 |
|
| 90 |
// Gdk::Clipboard::set_text() does not take a copy of the text. |
| 91 |
// The text can only be pasted (in this program or in some other program) |
| 92 |
// for as long as it exists. |
| 93 |
get_clipboard()->set_text(m_strData); |
| 94 |
} |
| 95 |
|
| 96 |
void ExampleWindow::on_button_paste() |
| 97 |
{ |
| 98 |
//Tell the clipboard to call our method when it is ready: |
| 99 |
get_clipboard()->read_text_async(sigc::mem_fun(*this, |
| 100 |
&ExampleWindow::on_clipboard_received)); |
| 101 |
} |
| 102 |
|
| 103 |
void ExampleWindow::on_clipboard_content_changed() |
| 104 |
{ |
| 105 |
update_paste_status(); |
| 106 |
} |
| 107 |
|
| 108 |
void ExampleWindow::on_clipboard_received(Glib::RefPtr<Gio::AsyncResult>& result) |
| 109 |
{ |
| 110 |
Glib::ustring text; |
| 111 |
try |
| 112 |
{ |
| 113 |
text = get_clipboard()->read_text_finish(result); |
| 114 |
} |
| 115 |
catch (const Glib::Error& err) |
| 116 |
{ |
| 117 |
// Print an error about why pasting failed. |
| 118 |
// Usually you probably want to ignore such failures, |
| 119 |
// but for demonstration purposes, we show the error. |
| 120 |
std::cout << "Pasting failed: " << err.what() << std::endl; |
| 121 |
} |
| 122 |
|
| 123 |
if (text.find(example_format_custom) == 0) |
| 124 |
{ |
| 125 |
// It's the expected format. |
| 126 |
m_ButtonA1.set_active(text.find("A1") != std::string::npos); |
| 127 |
m_ButtonA2.set_active(text.find("A2") != std::string::npos); |
| 128 |
m_ButtonB1.set_active(text.find("B1") != std::string::npos); |
| 129 |
m_ButtonB2.set_active(text.find("B2") != std::string::npos); |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
// Unexpected format. Disable the Paste button. |
| 134 |
std::cout << "Unexpected pasted text: \"" << text << "\"" << std::endl; |
| 135 |
m_Button_Paste.set_sensitive(false); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void ExampleWindow::update_paste_status() |
| 140 |
{ |
| 141 |
// Disable the paste button if there is nothing to paste. |
| 142 |
get_clipboard()->read_text_async(sigc::mem_fun(*this, |
| 143 |
&ExampleWindow::on_clipboard_received_status)); |
| 144 |
} |
| 145 |
|
| 146 |
void ExampleWindow::on_clipboard_received_status(Glib::RefPtr<Gio::AsyncResult>& result) |
| 147 |
{ |
| 148 |
Glib::ustring text; |
| 149 |
try |
| 150 |
{ |
| 151 |
text = get_clipboard()->read_text_finish(result); |
| 152 |
} |
| 153 |
catch (const Glib::Error&) |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
const bool bPasteIsPossible = text.find(example_format_custom) == 0; |
| 158 |
|
| 159 |
// Enable/Disable the Paste button appropriately: |
| 160 |
m_Button_Paste.set_sensitive(bPasteIsPossible); |
| 161 |
} |