File: sec-tooltips.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 (288 lines) | stat: -rw-r--r-- 9,556 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
<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>Tooltips</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-misc-widgets.html" title="Chapter 8. Miscellaneous Widgets">
<link rel="prev" href="sec-infobar.html" title="InfoBar">
<link rel="next" href="chapter-container-widgets.html" title="Chapter 9. Container Widgets">
</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">Tooltips</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-infobar.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 8. Miscellaneous Widgets</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-container-widgets.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-tooltips"></a>Tooltips</h2></div></div></div>


<p>
Tooltips are the little information windows that pop up when you leave your
pointer over a widget for a few seconds. Use
<code class="methodname">set_tooltip_text()</code> to set a text string as a tooltip
on any <code class="classname">Widget</code>.
<code class="classname">Gtk::Tooltip</code> is used for more advanced tooltip usage,
such as showing an image as well as text.
</p>

<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Widget.html" target="_top">Widget Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Tooltip.html" target="_top">Tooltip Reference</a></p>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="tooltip-example"></a>Example</h3></div></div></div>


<div class="figure">
<a name="figure-tooltip"></a><p class="title"><b>Figure 8.9. Tooltip</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/tooltip.png" alt="Tooltip"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/tooltips" 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:

  //Methods:
  void prepare_textview();
  void connect_signals();

  //Signal handlers:
  void on_markup_checkbutton_toggled();
  bool on_textview_query_tooltip(int x, int y, bool keyboard_tooltip, const Glib::RefPtr&lt;Gtk::Tooltip&gt;&amp; tooltip);
  bool on_button_query_tooltip(int x, int y, bool keyboard_tooltip, const Glib::RefPtr&lt;Gtk::Tooltip&gt;&amp; tooltip);

  //Child widgets:
  Gtk::Box m_vbox;

  Gtk::CheckButton m_checkbutton;
  Gtk::Label m_label;

  Gtk::ScrolledWindow m_scrolled_window;
  Gtk::TextView m_text_view;
  Glib::RefPtr&lt;Gtk::TextBuffer&gt; m_ref_text_buffer;
  Glib::RefPtr&lt;Gtk::TextTag&gt; m_ref_bold_tag;

  Gtk::Button m_button;
  Gtk::Box m_button_tooltip_widget;
};

#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;vector&gt;

const Glib::ustring app_title = "gtkmm tooltips example";
const Glib::ustring non_markedup_tip = "A tooltip without markup.";
const Glib::ustring markedup_tip = "&lt;i&gt;Markup&lt;/i&gt; in a tooltip.";

ExampleWindow::ExampleWindow()
  :
  m_vbox(Gtk::Orientation::VERTICAL, 3),
  m_checkbutton("Click to alternate markup in tooltip"),
  m_label("A label"),
  m_button("Button with a custom tooltip widget")
{
  //Set up window and the top-level container:
  set_title(app_title);

  m_vbox.set_margin(10);
  set_child(m_vbox);

  //Check button with markup in tooltip:
  m_checkbutton.set_tooltip_text(non_markedup_tip);
  m_vbox.append(m_checkbutton);

  //Label:
  m_label.set_tooltip_text("Another tooltip");
  m_vbox.append(m_label);

  //Textview:
  prepare_textview();

  //Button:
  //When only connecting to the query-tooltip signal, and not using any
  //of set_tooltip_text() or set_tooltip_markup(), we need to explicitly
  //tell GTK that the widget has a tooltip which we'll show.
  m_button.set_has_tooltip();
  m_vbox.append(m_button);

  //Button's custom tooltip widget:
  auto label = Gtk::make_managed&lt;Gtk::Label&gt;("A label in a custom tooltip widget");
  m_button_tooltip_widget.append(*label);

  connect_signals();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::prepare_textview()
{
  Gtk::TextIter iter;
  std::vector&lt;Glib::RefPtr&lt;Gtk::TextTag&gt;&gt; tags;

  //Set up a scrolled window:
  m_scrolled_window.set_child(m_text_view);
  m_scrolled_window.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
  m_scrolled_window.set_expand();
  m_vbox.append(m_scrolled_window);

  //Create a text buffer with some text:
  m_ref_text_buffer = Gtk::TextBuffer::create();

  iter = m_ref_text_buffer-&gt;end();
  m_ref_text_buffer-&gt;insert(iter, "Hover over the text ");

  //Insert some text with a tag.
  //In the tooltip signal handler below, we will show a tooltip
  //when mouse pointer is above this tagged text.
  m_ref_bold_tag = m_ref_text_buffer-&gt;create_tag("bold");
  m_ref_bold_tag-&gt;set_property("weight", Pango::Weight::BOLD);

  tags.push_back(m_ref_bold_tag);

  iter = m_ref_text_buffer-&gt;end();
  m_ref_text_buffer-&gt;insert_with_tags(iter, "in bold", tags);

  iter = m_ref_text_buffer-&gt;end();
  m_ref_text_buffer-&gt;insert(iter, " to see its tooltip");

  m_text_view.set_buffer(m_ref_text_buffer);

  m_text_view.set_size_request(320, 50);

  //When only connecting to the query-tooltip signal, and not using any
  //of set_tooltip_text() or set_tooltip_markup(), we need to explicitly
  //tell GTK that the widget has a tooltip which we'll show.
  m_text_view.set_has_tooltip();
}

void ExampleWindow::connect_signals()
{
  m_checkbutton.signal_toggled().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_markup_checkbutton_toggled));

  m_text_view.signal_query_tooltip().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_textview_query_tooltip), true);

  m_button.signal_query_tooltip().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_query_tooltip), true);
}

void ExampleWindow::on_markup_checkbutton_toggled()
{
  if (m_checkbutton.get_active())
  {
    m_checkbutton.set_tooltip_markup(markedup_tip);
  }
  else
  {
    m_checkbutton.set_tooltip_markup(non_markedup_tip);
  }
}

bool ExampleWindow::on_textview_query_tooltip(int x, int y, bool keyboard_tooltip, const Glib::RefPtr&lt;Gtk::Tooltip&gt;&amp; tooltip)
{
  Gtk::TextIter iter;

  if (keyboard_tooltip)
  {
    int offset = m_ref_text_buffer-&gt;property_cursor_position().get_value();
    iter = m_ref_text_buffer-&gt;get_iter_at_offset(offset);
  }
  else
  {
    int mouse_x, mouse_y, trailing;
    m_text_view.window_to_buffer_coords(Gtk::TextWindowType::TEXT,
                                        x, y, mouse_x, mouse_y);
    m_text_view.get_iter_at_position(iter, trailing, mouse_x, mouse_y);
  }

  if (!iter.has_tag(m_ref_bold_tag))
    return false;

  //Show a tooltip if the cursor or mouse pointer is over the text
  //with the specific tag:
  tooltip-&gt;set_markup("&lt;b&gt;Information&lt;/b&gt; attached to a text tag");
  tooltip-&gt;set_icon("dialog-information");
  return true;
}

bool ExampleWindow::on_button_query_tooltip(int, int, bool, const Glib::RefPtr&lt;Gtk::Tooltip&gt;&amp; tooltip)
{
  tooltip-&gt;set_custom(m_button_tooltip_widget);
  return true;
}
</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[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //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-infobar.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-misc-widgets.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-container-widgets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">InfoBar </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 9. Container Widgets</td>
</tr>
</table>
</div>
</body>
</html>