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
|
<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>InfoBar</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-progressbar.html" title="ProgressBar">
<link rel="next" href="sec-tooltips.html" title="Tooltips">
</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">InfoBar</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-progressbar.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="sec-tooltips.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-infobar"></a>InfoBar</h2></div></div></div>
<p>
An <code class="classname">InfoBar</code> may show small items of information or ask brief questions. Unlike a <code class="classname">Dialog</code>, it appears at the top of the current window instead of opening a new window. Its API is very similar to the <a class="link" href="chapter-dialogs.html" title="Chapter 17. Dialogs">Gtk::Dialog</a> API.</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1InfoBar.html" target="_top">Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="infobar-example"></a>Example</h3></div></div></div>
<p>The <code class="classname">InfoBar</code> widget is deprecated since <span class="application">gtkmm</span> 4.10.
The example shows an info bar consisting of a <code class="classname">Box</code> with
a <code class="classname">Label</code> and a <code class="classname">Button</code>.</p>
<div class="figure">
<a name="figure-infobar"></a><p class="title"><b>Figure 8.8. InfoBar</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/infobar.png" alt="InfoBar"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/infobar" 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 <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
//Signal handlers:
void on_infobar_ok();
void on_button_quit();
void on_button_clear();
void on_textbuffer_changed();
void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section,
const Glib::Error& error);
void set_infobar_background();
//Child widgets:
Gtk::Box m_VBox;
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::TextView m_TextView;
Glib::RefPtr<Gtk::TextBuffer> m_refTextBuffer;
Gtk::Box m_InfoBar;
Gtk::Label m_Message_Label;
Gtk::Button m_Button_OK;
Gtk::Box m_ButtonBox;
Gtk::Button m_Button_Quit;
Gtk::Button m_Button_Clear;
};
#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 <iostream>
// The Gtk::InfoBar widget is deprecated since gtk/gtkmm 4.10.
// A Box with a Label and Button is often a good replacement.
// If you don't care about its background color, it's even easier
// than the InfoBar replacement in this example program.
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL, 6),
m_InfoBar(Gtk::Orientation::HORIZONTAL, 6),
m_Button_OK("_OK", true),
m_ButtonBox(Gtk::Orientation::HORIZONTAL, 6),
m_Button_Quit("_Quit", true),
m_Button_Clear("_Clear", true)
{
set_title("Infobar example");
set_default_size(400, 200);
m_VBox.set_margin(6);
set_child(m_VBox);
// Add the message label to the InfoBar:
m_InfoBar.append(m_Message_Label);
m_Message_Label.set_margin_start(5);
m_Message_Label.set_hexpand(true);
m_Message_Label.set_halign(Gtk::Align::START);
// Add an ok button to the InfoBar:
m_InfoBar.append(m_Button_OK);
m_Button_OK.set_margin(5);
// Add the InfoBar to the vbox:
m_VBox.append(m_InfoBar);
// Create the buffer and set it for the TextView:
m_refTextBuffer = Gtk::TextBuffer::create();
m_TextView.set_buffer(m_refTextBuffer);
// Add the TextView, inside a ScrolledWindow:
m_ScrolledWindow.set_child(m_TextView);
// Show the scrollbars only when they are necessary:
m_ScrolledWindow.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
m_ScrolledWindow.set_expand();
m_VBox.append(m_ScrolledWindow);
// Add button box:
m_VBox.append(m_ButtonBox);
m_ButtonBox.append(m_Button_Clear);
m_ButtonBox.append(m_Button_Quit);
m_Button_Clear.set_hexpand(true);
m_Button_Clear.set_halign(Gtk::Align::END);
// Connect signals:
m_Button_OK.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_infobar_ok) );
m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
m_Button_Clear.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_clear) );
m_refTextBuffer->signal_changed().connect(sigc::mem_fun(*this,
&ExampleWindow::on_textbuffer_changed) );
// Keep the InfoBar hidden until a message needs to be shown:
m_InfoBar.set_visible(false);
set_infobar_background();
// Make the clear button insensitive until text is typed in the buffer. When
// the button is sensitive and it is pressed, the InfoBar is displayed with a
// message.
m_Button_Clear.set_sensitive(false);
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_infobar_ok()
{
// Clear the message and hide the info bar:
m_Message_Label.set_text("");
m_InfoBar.set_visible(false);
}
void ExampleWindow::on_button_quit()
{
set_visible(false);
}
void ExampleWindow::on_button_clear()
{
m_refTextBuffer->set_text("");
m_Message_Label.set_text("Cleared the text.");
m_InfoBar.set_visible(true);
}
void ExampleWindow::on_textbuffer_changed()
{
m_Button_Clear.set_sensitive(m_refTextBuffer->size() > 0);
}
void ExampleWindow::set_infobar_background()
{
// Concerning color in CSS files, see https://www.w3.org/TR/css-color-3.
const std::string css = "#infobarbox { background-color: beige; }";
m_InfoBar.set_name("infobarbox");
auto css_provider = Gtk::CssProvider::create();
Gtk::StyleProvider::add_provider_for_display(
m_InfoBar.get_display(), css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
css_provider->load_from_data(css);
css_provider->signal_parsing_error().connect(
sigc::mem_fun(*this, &ExampleWindow::on_parsing_error));
}
void ExampleWindow::on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section,
const Glib::Error& error)
{
std::cerr << "on_parsing_error(): " << error.what() << std::endl;
if (section)
{
const auto file = section->get_file();
if (file)
{
std::cerr << " URI = " << file->get_uri() << std::endl;
}
auto start_location = section->get_start_location();
auto end_location = section->get_end_location();
std::cerr << " start_line = " << start_location.get_lines()+1
<< ", end_line = " << end_location.get_lines()+1 << std::endl;
std::cerr << " start_position = " << start_location.get_line_chars()
<< ", end_position = " << end_location.get_line_chars() << std::endl;
}
}
</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 <gtkmm/application.h>
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->make_window_and_run<ExampleWindow>(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-progressbar.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="sec-tooltips.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">ProgressBar </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"> Tooltips</td>
</tr>
</table>
</div>
</body>
</html>
|