File: sec-clipboard-examples.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (458 lines) | stat: -rw-r--r-- 14,742 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Examples</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-clipboard.html" title="Chapter 20. The Clipboard">
<link rel="prev" href="sec-clipboard-paste.html" title="Paste">
<link rel="next" href="chapter-printing.html" title="Chapter 21. Printing">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Examples</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-clipboard-paste.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 20. The Clipboard</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-printing.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-clipboard-examples"></a>Examples</h2></div></div></div>


<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-clipboard-example-simple"></a>Simple</h3></div></div></div>

<p>
This example allows copy and pasting of application-specific data, using the
standard text format. Although this is simple, it's not ideal because it does
not identify the <code class="classname">Clipboard</code> data as being of a particular
type.
</p>

<div class="figure">
<a name="figure-clipboard-simple"></a><p class="title"><b>Figure 20.1. Clipboard - Simple</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/clipboard_simple.png" alt="Clipboard - Simple"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/clipboard/simple/" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_button_copy();
  void on_button_paste();

  void on_clipboard_text_received(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result);

  Glib::ustring m_strData;

  //Child widgets:
  Gtk::Box m_VBox;

  Gtk::Label m_Label;

  Gtk::Grid m_Grid;
  Gtk::ToggleButton m_ButtonA1, m_ButtonA2, m_ButtonB1, m_ButtonB2;

  Gtk::Box m_ButtonBox;
  Gtk::Button m_Button_Copy, m_Button_Paste;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Label("Select cells in the grid, click Copy, then open a second "
        "instance of this example to try pasting the copied data."),
  m_ButtonA1("A1"), m_ButtonA2("A2"), m_ButtonB1("B1"), m_ButtonB2("B2"),
  m_Button_Copy("_Copy", /* mnemonic= */ true), m_Button_Paste("_Paste", true)
{
  set_title("Gtk::Clipboard example");

  m_VBox.set_margin(12);
  set_child(m_VBox);

  m_VBox.append(m_Label);

  //Fill Grid:
  m_VBox.append(m_Grid);
  m_Grid.set_expand(true);
  m_Grid.set_row_homogeneous(true);
  m_Grid.set_column_homogeneous(true);
  m_Grid.attach(m_ButtonA1, 0, 0);
  m_Grid.attach(m_ButtonA2, 1, 0);
  m_Grid.attach(m_ButtonB1, 0, 1);
  m_Grid.attach(m_ButtonB2, 1, 1);

  //Add ButtonBox to bottom:
  m_VBox.append(m_ButtonBox);
  m_VBox.set_spacing(6);

  //Fill ButtonBox:
  m_ButtonBox.append(m_Button_Copy);
  m_Button_Copy.set_hexpand(true);
  m_Button_Copy.set_halign(Gtk::Align::END);
  m_Button_Copy.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_button_copy) );
  m_ButtonBox.append(m_Button_Paste);
  m_Button_Paste.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_button_paste) );
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_copy()
{
  //Build a string representation of the stuff to be copied:
  //Ideally you would use XML, with an XML parser here:
  m_strData = m_ButtonA1.get_active() ? "1" : "0";
  m_strData += m_ButtonA2.get_active() ? "1" : "0";
  m_strData += m_ButtonB1.get_active() ? "1" : "0";
  m_strData += m_ButtonB2.get_active() ? "1" : "0";

  // Gdk::Clipboard::set_text() does not take a copy of the text.
  // The text can only be pasted (in this program or in some other program)
  // for as long as it exists.
  get_clipboard()-&gt;set_text(m_strData);
}

void ExampleWindow::on_button_paste()
{
  //Tell the clipboard to call our method when it is ready:
  get_clipboard()-&gt;read_text_async(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_clipboard_text_received));
}

void ExampleWindow::on_clipboard_text_received(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result)
{
  Glib::ustring text;
  try
  {
    text = get_clipboard()-&gt;read_text_finish(result);
  }
  catch (const Glib::Error&amp; err)
  {
    // Print an error about why pasting failed.
    // Usually you probably want to ignore such failures,
    // but for demonstration purposes, we show the error.
    std::cout &lt;&lt; "Pasting failed: " &lt;&lt; err.what() &lt;&lt; std::endl;
  }

  //See comment in on_button_copy() about this silly clipboard format.
  if(text.size() &gt;= 4)
  {
    m_ButtonA1.set_active( text[0] == '1' );
    m_ButtonA2.set_active( text[1] == '1' );
    m_ButtonB1.set_active( text[2] == '1' );
    m_ButtonB2.set_active( text[3] == '1' );
  }
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char *argv[])
{
  // Gio::Application::Flags::NON_UNIQUE because it shall be possible to run
  // several instances of this application simultaneously.
  auto app = Gtk::Application::create(
    "org.gtkmm.example", Gio::Application::Flags::NON_UNIQUE);

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>


</div>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="sec-clipboard-example-ideal"></a>Ideal</h3></div></div></div>

<p>This is like the simple example, but it
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">Defines a custom clipboard target, though the format is still text.</li>
<li class="listitem">It uses the <code class="methodname">Gdk::ContentFormats::signal_changed()</code>
  signal and disables the Paste button if it can't use anything on the clipboard.</li>
</ol></div>
<p>
</p>

<div class="figure">
<a name="figure-clipboard-ideal"></a><p class="title"><b>Figure 20.2. Clipboard - Ideal</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/clipboard_ideal.png" alt="Clipboard - Ideal"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/clipboard/ideal/" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_button_copy();
  void on_button_paste();

  void on_clipboard_content_changed();

  void on_clipboard_received(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result);
  void on_clipboard_received_status(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result);

  void update_paste_status(); //Disable the paste button if there is nothing to paste.

  Glib::ustring m_strData;

  //Child widgets:
  Gtk::Box m_VBox;

  Gtk::Label m_Label;

  Gtk::Grid m_Grid;
  Gtk::ToggleButton m_ButtonA1, m_ButtonA2, m_ButtonB1, m_ButtonB2;

  Gtk::Box m_ButtonBox;
  Gtk::Button m_Button_Copy, m_Button_Paste;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;iostream&gt;
#include &lt;string&gt;

namespace
{

const char example_format_custom[] = "gtkmmclipboardexample";

} // anonymous namespace


ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Label("Select cells in the grid, click Copy, then open a second instance "
          "of this example to try pasting the copied data.\nOr try pasting the "
          "text representation into gedit."),
  m_ButtonA1("A1"), m_ButtonA2("A2"), m_ButtonB1("B1"), m_ButtonB2("B2"),
  m_Button_Copy("_Copy", /* mnemonic= */ true), m_Button_Paste("_Paste", true)
{
  set_title("Gtk::Clipboard example");

  m_VBox.set_margin(12);
  set_child(m_VBox);

  m_VBox.append(m_Label);

  //Fill Grid:
  m_VBox.append(m_Grid);
  m_Grid.set_expand(true);
  m_Grid.set_row_homogeneous(true);
  m_Grid.set_column_homogeneous(true);
  m_Grid.attach(m_ButtonA1, 0, 0);
  m_Grid.attach(m_ButtonA2, 1, 0);
  m_Grid.attach(m_ButtonB1, 0, 1);
  m_Grid.attach(m_ButtonB2, 1, 1);

  //Add ButtonBox to bottom:
  m_VBox.append(m_ButtonBox);
  m_VBox.set_spacing(6);

  //Fill ButtonBox:
  m_ButtonBox.append(m_Button_Copy);
  m_Button_Copy.set_hexpand(true);
  m_Button_Copy.set_halign(Gtk::Align::END);
  m_Button_Copy.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_button_copy) );
  m_ButtonBox.append(m_Button_Paste);
  m_Button_Paste.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_button_paste) );

  //Connect a signal handler that will be called when the contents of
  //the clipboard change.
  get_clipboard()-&gt;signal_changed().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_clipboard_content_changed));

  update_paste_status();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_copy()
{
  //Build a string representation of the stuff to be copied:
  //Ideally you would use XML, with an XML parser here:
  m_strData = example_format_custom;
  m_strData += m_ButtonA1.get_active() ? " A1" : "";
  m_strData += m_ButtonA2.get_active() ? " A2" : "";
  m_strData += m_ButtonB1.get_active() ? " B1" : "";
  m_strData += m_ButtonB2.get_active() ? " B2" : "";

  // Gdk::Clipboard::set_text() does not take a copy of the text.
  // The text can only be pasted (in this program or in some other program)
  // for as long as it exists.
  get_clipboard()-&gt;set_text(m_strData);
}

void ExampleWindow::on_button_paste()
{
  //Tell the clipboard to call our method when it is ready:
  get_clipboard()-&gt;read_text_async(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_clipboard_received));
}

void ExampleWindow::on_clipboard_content_changed()
{
  update_paste_status();
}

void ExampleWindow::on_clipboard_received(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result)
{
  Glib::ustring text;
  try
  {
    text = get_clipboard()-&gt;read_text_finish(result);
  }
  catch (const Glib::Error&amp; err)
  {
    // Print an error about why pasting failed.
    // Usually you probably want to ignore such failures,
    // but for demonstration purposes, we show the error.
    std::cout &lt;&lt; "Pasting failed: " &lt;&lt; err.what() &lt;&lt; std::endl;
  }

  if (text.find(example_format_custom) == 0)
  {
    // It's the expected format.
    m_ButtonA1.set_active(text.find("A1") != std::string::npos);
    m_ButtonA2.set_active(text.find("A2") != std::string::npos);
    m_ButtonB1.set_active(text.find("B1") != std::string::npos);
    m_ButtonB2.set_active(text.find("B2") != std::string::npos);
  }
  else
  {
    // Unexpected format. Disable the Paste button.
    std::cout &lt;&lt; "Unexpected pasted text: \"" &lt;&lt; text &lt;&lt; "\"" &lt;&lt; std::endl;
    m_Button_Paste.set_sensitive(false);
  }
}

void ExampleWindow::update_paste_status()
{
  // Disable the paste button if there is nothing to paste.
  get_clipboard()-&gt;read_text_async(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_clipboard_received_status));
}

void ExampleWindow::on_clipboard_received_status(Glib::RefPtr&lt;Gio::AsyncResult&gt;&amp; result)
{
  Glib::ustring text;
  try
  {
    text = get_clipboard()-&gt;read_text_finish(result);
  }
  catch (const Glib::Error&amp;)
  {
  }

  const bool bPasteIsPossible = text.find(example_format_custom) == 0;

  // Enable/Disable the Paste button appropriately:
  m_Button_Paste.set_sensitive(bPasteIsPossible);
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char *argv[])
{
  // Gio::Application::Flags::NON_UNIQUE because it shall be possible to run
  // several instances of this application simultaneously.
  auto app = Gtk::Application::create(
    "org.gtkmm.example", Gio::Application::Flags::NON_UNIQUE);

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>


</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-clipboard-paste.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-clipboard.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-printing.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Paste </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 21. Printing</td>
</tr>
</table>
</div>
</body>
</html>