Debian Codegraph @

examples/book/clipboard/simple/examplewindow.cc

raw
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
20 ExampleWindow::ExampleWindow()
21 : m_VBox(Gtk::Orientation::VERTICAL),
22 m_Label("Select cells in the grid, click Copy, then open a second "
23 "instance of this example to try pasting the copied data."),
24 m_ButtonA1("A1"), m_ButtonA2("A2"), m_ButtonB1("B1"), m_ButtonB2("B2"),
25 m_Button_Copy("_Copy", /* mnemonic= */ true), m_Button_Paste("_Paste", true)
26 {
27 set_title("Gtk::Clipboard example");
28
29 m_VBox.set_margin(12);
30 set_child(m_VBox);
31
32 m_VBox.append(m_Label);
33
34 //Fill Grid:
35 m_VBox.append(m_Grid);
36 m_Grid.set_expand(true);
37 m_Grid.set_row_homogeneous(true);
38 m_Grid.set_column_homogeneous(true);
39 m_Grid.attach(m_ButtonA1, 0, 0);
40 m_Grid.attach(m_ButtonA2, 1, 0);
41 m_Grid.attach(m_ButtonB1, 0, 1);
42 m_Grid.attach(m_ButtonB2, 1, 1);
43
44 //Add ButtonBox to bottom:
45 m_VBox.append(m_ButtonBox);
46 m_VBox.set_spacing(6);
47
48 //Fill ButtonBox:
49 m_ButtonBox.append(m_Button_Copy);
50 m_Button_Copy.set_hexpand(true);
51 m_Button_Copy.set_halign(Gtk::Align::END);
52 m_Button_Copy.signal_clicked().connect(sigc::mem_fun(*this,
53 &ExampleWindow::on_button_copy) );
54 m_ButtonBox.append(m_Button_Paste);
55 m_Button_Paste.signal_clicked().connect(sigc::mem_fun(*this,
56 &ExampleWindow::on_button_paste) );
57 }
58
59 ExampleWindow::~ExampleWindow()
60 {
61 }
62
63 void ExampleWindow::on_button_copy()
64 {
65 //Build a string representation of the stuff to be copied:
66 //Ideally you would use XML, with an XML parser here:
67 m_strData = m_ButtonA1.get_active() ? "1" : "0";
68 m_strData += m_ButtonA2.get_active() ? "1" : "0";
69 m_strData += m_ButtonB1.get_active() ? "1" : "0";
70 m_strData += m_ButtonB2.get_active() ? "1" : "0";
71
72 // Gdk::Clipboard::set_text() does not take a copy of the text.
73 // The text can only be pasted (in this program or in some other program)
74 // for as long as it exists.
75 get_clipboard()->set_text(m_strData);
76 }
77
78 void ExampleWindow::on_button_paste()
79 {
80 //Tell the clipboard to call our method when it is ready:
81 get_clipboard()->read_text_async(sigc::mem_fun(*this,
82 &ExampleWindow::on_clipboard_text_received));
83 }
84
85 void ExampleWindow::on_clipboard_text_received(Glib::RefPtr<Gio::AsyncResult>& result)
86 {
87 Glib::ustring text;
88 try
89 {
90 text = get_clipboard()->read_text_finish(result);
91 }
92 catch (const Glib::Error& err)
93 {
94 // Print an error about why pasting failed.
95 // Usually you probably want to ignore such failures,
96 // but for demonstration purposes, we show the error.
97 std::cout << "Pasting failed: " << err.what() << std::endl;
98 }
99
100 //See comment in on_button_copy() about this silly clipboard format.
101 if(text.size() >= 4)
102 {
103 m_ButtonA1.set_active( text[0] == '1' );
104 m_ButtonA2.set_active( text[1] == '1' );
105 m_ButtonB1.set_active( text[2] == '1' );
106 m_ButtonB2.set_active( text[3] == '1' );
107 }
108 }