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
|
<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>Custom CSS Names</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-customwidgets.html" title="Chapter 28. Custom Widgets">
<link rel="prev" href="sec-custom-widgets.html" title="Custom Widgets">
<link rel="next" href="chapter-multi-threaded-programs.html" title="Chapter 29. Multi-threaded programs">
</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">Custom CSS Names</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-custom-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 28. Custom Widgets</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-multi-threaded-programs.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-custom-css-names"></a>Custom CSS Names</h2></div></div></div>
<p>Many aspects of the look of widgets are controlled by CSS (Cascading Style
Sheet) files. With CSS files you can choose color, font, line thickness, etc.
If you give some widgets their own names or their own CSS classes, you can define
CSS rules that apply only to those widgets, for instance certain buttons,
without affecting other similar widgets.</p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="css-widget-class-name"></a>CSS Node Name, Widget Name, CSS Class Name</h3></div></div></div>
<p>There are three ways of referring from a widget to data in a CSS file:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p><code class="methodname">gtk_widget_class_set_css_name()</code>
can only be called from the class init function. It sets the CSS node name
of all instances of a class (a GType). See the <a class="link" href="sec-custom-widgets.html#custom-init-functions" title="Class Init and Instance Init Functions">
Class Init and Instance Init Functions</a> section.</p></li>
<li class="listitem"><p><code class="methodname">Gtk::Widget::set_name()</code>
can be called from a C++ constructor. It sets the name of
a widget instance.</p></li>
<li class="listitem"><p><code class="methodname">Gtk::Widget::add_class_name()</code>
can be called from a C++ constructor. It adds the name of a CSS class,
used by a widget instance.</p></li>
</ul></div>
<p>
The following example shows a button with its own CSS node name, a label with
a widget name and a label that uses its own CSS class.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="custom-style-information"></a>Custom Style Information</h3></div></div></div>
<p>To add a style sheet to an application, use one of the
<code class="methodname">Gtk::CssProvider::load_from_*()</code> methods.
Then add it with <code class="methodname">Gtk::StyleProvider::add_provider_for_display()</code>
(available since <span class="application">gtkmm</span> 4.10) or <code class="methodname">Gtk::StyleContext::add_provider_for_display()</code>.
<code class="classname">Gtk::StyleContext</code> also contains methods to read some style
information, but this class is deprecated since <span class="application">gtkmm</span> 4.10.</p>
<p>CSS files are described in the documentation of GTK.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="custom-css-name-example"></a>Example</h3></div></div></div>
<p>This example implements a button and two labels with custom style information.</p>
<div class="figure">
<a name="figure-custom-css-name"></a><p class="title"><b>Figure 28.3. Custom CSS Name</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/custom_css_name.png" alt="Custom CSS Name"></div>
</div>
</div>
</div>
<br class="figure-break">
<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/custom/custom_css_name/" 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>
#include "mybutton.h"
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
// Signal handlers:
void on_button_quit();
static void on_parsing_error(const Glib::RefPtr<const Gtk::CssSection>& section,
const Glib::Error& error);
// Child widgets:
Gtk::Box m_VBox;
MyButton m_Button_Child;
Gtk::Label m_Label_Child1;
Gtk::Label m_Label_Child2;
Gtk::Box m_ButtonBox;
Gtk::Button m_Button_Quit;
Glib::RefPtr<Gtk::CssProvider> m_refCssProvider;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">mybutton.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_MYBUTTON_H
#define GTKMM_EXAMPLE_MYBUTTON_H
#include <gtkmm/button.h>
#include "myextrainit.h"
class MyButton : public MyExtraInit, public Gtk::Button
{
public:
MyButton(const Glib::ustring& label, bool mnemonic=false);
~MyButton() override;
};
#endif //GTKMM_EXAMPLE_MYBUTTON_H
</code></pre>
<p>File: <code class="filename">myextrainit.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLE_MYEXTRAINIT_H
#define GTKMM_EXAMPLE_MYEXTRAINIT_H
#include <glibmm/extraclassinit.h>
#include <glibmm/ustring.h>
// Calls gtk_widget_class_set_css_name() in the class init function.
class MyExtraInit : public Glib::ExtraClassInit
{
public:
MyExtraInit(const Glib::ustring& css_name);
private:
Glib::ustring m_css_name;
};
#endif //GTKMM_EXAMPLE_MYEXTRAINIT_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>
#include <gtkmm/version.h>
#define HAS_STYLE_PROVIDER_ADD_PROVIDER_FOR_DISPLAY GTKMM_CHECK_VERSION(4,9,1)
ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
m_Button_Child("Button with custom CSS node name"),
m_Label_Child1("Label with custom name"),
m_Label_Child2("Label with custom CSS class"),
m_Button_Quit("Quit")
{
set_title("Custom CSS name example");
set_default_size(400, 200);
m_VBox.set_margin(10);
m_VBox.set_spacing(5);
set_child(m_VBox);
// There are 3 ways to set custom CSS data for a widget:
// 1. The CSS name can be set for a GType with gtk_widget_class_set_css_name().
// 2. The CSS name can be set for a widget instance with gtk_widget_set_name()
// (Gtk::Widget::set_name()).
// 3. A CSS class can be set for a widget instance with
// gtk_widget_add_css_class() (Gtk::Widget::add_css_class()).
//
// gtk_widget_class_set_css_name(), if used, must be called in the class init
// function. It has not been wrapped in a C++ function.
// Gtk::Widget::set_name() and Gtk::Widget::add_css_class() can be called in
// a C++ constructor.
// CSS widget name, which must be used in the CSS file, preceded by "#".
m_Label_Child1.set_name("my-label1");
// CSS class name, which must be used in the CSS file, preceded by ".".
m_Label_Child2.add_css_class("my-label2");
// Add the child widgets to m_VBox:
m_VBox.append(*Gtk::make_managed<Gtk::Label>("Ordinary label"));
m_VBox.append(m_Button_Child);
m_VBox.append(m_Label_Child1);
m_VBox.append(m_Label_Child2);
m_VBox.set_expand();
m_VBox.append(m_ButtonBox);
m_ButtonBox.append(m_Button_Quit);
m_ButtonBox.set_margin(6);
m_Button_Quit.set_hexpand(true);
m_Button_Quit.set_halign(Gtk::Align::END);
m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
// Load extra CSS file.
m_refCssProvider = Gtk::CssProvider::create();
#if HAS_STYLE_PROVIDER_ADD_PROVIDER_FOR_DISPLAY
Gtk::StyleProvider::add_provider_for_display(get_display(), m_refCssProvider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#else
Gtk::StyleContext::add_provider_for_display(get_display(), m_refCssProvider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
#endif
m_refCssProvider->signal_parsing_error().connect(
[](const auto& section, const auto& error)
{ on_parsing_error(section, error); }
);
m_refCssProvider->load_from_path("custom_gtkmm.css");
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit()
{
set_visible(false);
}
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>
<p>File: <code class="filename">mybutton.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "mybutton.h"
#include <gtk/gtk.h> // For GTK_IS_BUTTON()
#include <iostream>
MyButton::MyButton(const Glib::ustring& label, bool mnemonic)
:
// The GType name will actually be gtkmm__CustomObject_MyButton
Glib::ObjectBase("MyButton"),
MyExtraInit("my-button"), // CSS node name, which must be used in the CSS file.
Gtk::Button(label, mnemonic)
{
// This shows the GType name:
std::cout << "GType name: " << G_OBJECT_TYPE_NAME(gobj()) << std::endl;
// This shows that the GType still derives from GtkButton:
std::cout << "Gtype is a GtkButton?: " << GTK_IS_BUTTON(gobj()) << std::endl;
// The CSS name can be set either
// - for a GType (in this case for your custom class) with gtk_widget_class_set_css_name(), or
// - for a widget instance with gtk_widget_set_name() (Gtk::Widget::set_name()).
//
// gtk_widget_class_set_css_name(), if used, must be called in the class init function.
// It has not been wrapped in a C++ function.
// Gtk::Widget::set_name() can be called in a C++ constructor.
//
// Another alternative: The custom button inherits the CSS name "button" from
// GtkButton. That name can be used in the CSS file.
}
MyButton::~MyButton()
{
}
</code></pre>
<p>File: <code class="filename">myextrainit.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "myextrainit.h"
#include <gtkmm/widget.h>
#include <gtk/gtk.h>
namespace
{
using BaseObjectType = GtkWidget;
using BaseClassType = GtkWidgetClass;
// These callback functions are called from GLib (a C library).
// They shall have C linkage. (Many compilers accept callback functions
// with C++ linkage, but such a program has undefined behavior.)
//
// If you want the functions with C linkage to have internal linkage,
// they must be declared 'static', even though they are defined in an anonymous
// namespace. The compiler respects namespace declarations of functions
// with C linkage, but the linker does not.
extern "C"
{
// Extra class init function.
static void class_init_function(void* g_class, void* class_data)
{
g_return_if_fail(GTK_IS_WIDGET_CLASS(g_class));
const auto klass = static_cast<BaseClassType*>(g_class);
const auto css_name = static_cast<Glib::ustring*>(class_data);
gtk_widget_class_set_css_name(klass, css_name->c_str());
}
// Extra instance init function.
static void instance_init_function(GTypeInstance* instance, void* /* g_class */)
{
g_return_if_fail(GTK_IS_WIDGET(instance));
// Nothing to do here.
// This extra instance init function just shows how such a function can
// be added to a custom widget, if necessary.
}
} // extern "C"
} // anonymous namespace
MyExtraInit::MyExtraInit(const Glib::ustring& css_name)
:
Glib::ExtraClassInit(class_init_function, &m_css_name, instance_init_function),
m_css_name(css_name)
{
}
</code></pre>
<p>File: <code class="filename">custom_gtkmm.css</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">/* Example of a CSS style sheet. */
my-button {
background-color: red;
color: blue;
padding: 10px 15px 20px 5px; /* top right bottom left */
border: solid black 2px;
border-radius: 8px;
}
my-button:active {
background-color: rgb(0,255,0);
}
#my-label1 {
background-color: rgb(180,180,180);
color: rgb(0,255,255);
padding: 10px 15px 10px 25px; /* top right bottom left */
}
#my-label1:hover {
background-color: rgb(220,220,220);
}
.my-label2 {
background-color: rgb(180,180,100);
color: black;
padding: 5px 15px 5px 25px; /* top right bottom left */
}
.my-label2:hover {
background-color: rgb(220,220,140);
}
</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-custom-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-customwidgets.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-multi-threaded-programs.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Custom Widgets </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 29. Multi-threaded programs</td>
</tr>
</table>
</div>
</body>
</html>
|