Change Gtk::ProgressBar appearance by providing custom CSS (#7)

In Gtk3 the progress bar height is fixed and defined by the CSS theme in
use.  Changing the widget allocation size does nothing, it is always
rendered the same way.

In many themes, including Adwaita, the progressbar is very, very thin.
Provide custom CSS to specify a height of 8 pixels.

The CSS source string has to be differentiated for Gtk pre and post
3.20, because Gtk 3.20 introduced some breaking changes in the way CSS
is handled.

References:

[1] Migrating from GTK+ 2.x to GTK+ 3 - Parsing of custom resources
    https://developer.gnome.org/gtk3/stable/gtk-migrating-GtkStyleContext-parsing.html

[2] Gtk3 Reference Documentation - Changes in GTK+ 3.20
    https://developer.gnome.org/gtk3/stable/ch32s10.html

[3] Gnome/HowDoI - Custom Style
    https://wiki.gnome.org/HowDoI/CustomStyle

Closes #7 - Port to Gtk3
This commit is contained in:
Luca Bacci 2019-01-22 15:45:58 +01:00 committed by Mike Fleetwood
parent 6c47561bf6
commit 6aba93d8c0
2 changed files with 29 additions and 0 deletions

View File

@ -64,6 +64,7 @@ private:
void create_format_menu_add_item( FSType filesystem, bool activate );
void init_device_info() ;
void init_hpaned_main() ;
void add_custom_css();
void refresh_combo_devices() ;
void show_pulsebar( const Glib::ustring & status_message ) ;

View File

@ -48,6 +48,7 @@
#include "../config.h"
#include <string.h>
#include <gtkmm/cssprovider.h>
#include <gtkmm/aboutdialog.h>
#include <gtkmm/messagedialog.h>
#include <gtkmm/radiobuttongroup.h>
@ -141,6 +142,8 @@ Win_GParted::Win_GParted( const std::vector<Glib::ustring> & user_devices )
//make sure harddisk information is closed..
hpaned_main .get_child1() ->hide() ;
add_custom_css();
}
Win_GParted::~Win_GParted()
@ -669,6 +672,31 @@ void Win_GParted::init_hpaned_main()
hpaned_main .pack2( *scrollwindow, true, true );
}
void Win_GParted::add_custom_css()
{
Glib::RefPtr<Gdk::Screen> default_screen = Gdk::Screen::get_default();
Glib::RefPtr<Gtk::CssProvider> provider = Gtk::CssProvider::create();
Glib::ustring custom_css;
if (gtk_get_minor_version() >= 20)
custom_css = "progressbar progress, trough { min-height: 8px; }";
else
custom_css = "GtkProgressBar { -GtkProgressBar-min-horizontal-bar-height: 8px; }";
try
{
provider->load_from_data(custom_css);
}
catch (Glib::Error& e)
{
std::cerr << e.what() << std::endl;
}
Gtk::StyleContext::add_provider_for_screen(default_screen,
provider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
void Win_GParted::refresh_combo_devices()
{
// Temporarily block the on change callback while re-creating the device list