You are not logged in.
Pages: 1
Looks like Gtk3 doesn't support text inside progress bar.
I prefer the old look, but that is only me.
Devs, with a little juggling it's possible to achieve the old look in Gtk3.
These two little apps do the same: show the progress bar.
https://www.youtube.com/watch?v=Y1s47gceG2U
This is Greybird theme on Debian Buster, which makes the text over the progress so small.
progressbar.c
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib.h>
/* gcc -o progressbar -fPIC progressbar.c `pkg-config --cflags --libs gtk+-3.0` */
guint threadID = 0;
typedef struct {
GtkWidget *progress_bar;
GtkWidget *button1;
} app_widgets;
void destroy_handler (GtkApplication* app, gpointer data)
{
g_application_quit(G_APPLICATION (data));
}
void
end_progress (gpointer user_data)
{
app_widgets *app_wdgts = (app_widgets *) user_data;
gdouble fraction;
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar));
g_print("%.0f%%\n", fraction*100);
}
static gboolean
fill (gpointer user_data)
{
app_widgets *app_wdgts = (app_widgets *) user_data;
gdouble fraction;
/*Get the current progress*/
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar));
if (fraction > 0.99) {
fraction = 0;
}
/*Increase the bar by 10% each time this function is called*/
if (fraction < 0.99)
fraction += 0.1;
/*Fill in the bar with the new fraction*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar),
fraction);
gchar temp[256];
memset(&temp, 0x0, 256);
if (fraction > 0.99) {
snprintf(temp, 255, "Complete: %.0f %%", fraction*100);
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Repeat");
threadID = 0;
}
else {
snprintf(temp, 255, "Working: %.0f %%", fraction*100);
}
gtk_progress_bar_set_text (GTK_PROGRESS_BAR(app_wdgts->progress_bar), temp);
/*Ensures that the fraction stays below 1.0*/
if (fraction < 0.99)
return TRUE;
return FALSE;
}
void
button1_clicked_cb (GtkButton *button,
app_widgets *app_wdgts)
{
if (threadID == 0)
{
threadID = g_timeout_add_full (0, 500, fill,
app_wdgts, end_progress);
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Pause");
}
else
{
GSource *source = g_main_context_find_source_by_id(NULL,
threadID);
if (source)
{
g_source_destroy (source);
}
threadID = 0;
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Continue");
}
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *box;
GtkWidget *button2;
app_widgets *widgets = g_slice_new(app_widgets);
gdouble fraction = 0.0;
/*Create a window with a title, and a default size*/
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Progress Bar");
gtk_window_set_default_size (GTK_WINDOW (window), 420, 60);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 20);
grid = gtk_grid_new ();
gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
gtk_grid_set_column_homogeneous (GTK_GRID (grid),
TRUE);
gtk_grid_set_row_homogeneous (GTK_GRID (grid),
FALSE);
/*Create a progressbar and add it to the window*/
widgets->progress_bar = gtk_progress_bar_new ();
widgets->button1 = gtk_button_new_with_label ("Begin");
button2 = gtk_button_new_with_label ("Cancel");
gtk_progress_bar_set_inverted (GTK_PROGRESS_BAR(widgets->progress_bar), FALSE);
gtk_progress_bar_set_text (GTK_PROGRESS_BAR(widgets->progress_bar),
"Ready");
gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR(widgets->progress_bar),
TRUE);
gtk_grid_attach (GTK_GRID (grid), widgets->progress_bar, 0, 0, 4, 1);
gtk_grid_attach (GTK_GRID (grid), widgets->button1, 2, 1, 1, 1);
gtk_grid_attach_next_to (GTK_GRID (grid), button2, widgets->button1, GTK_POS_RIGHT, 1, 1);
gtk_box_pack_start (GTK_BOX (box), grid, TRUE, TRUE, 12);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_container_set_border_width(GTK_CONTAINER(window),5);
/*Fill in the given fraction of the bar. Has to be between 0.0-1.0 inclusive*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (widgets->progress_bar), fraction);
g_signal_connect (G_OBJECT(window), "destroy",
G_CALLBACK (destroy_handler), app);
g_signal_connect (widgets->button1, "clicked",
G_CALLBACK (button1_clicked_cb), widgets);
g_signal_connect (button2, "clicked",
G_CALLBACK (destroy_handler), app);
gtk_widget_show_all (window);
}
int
main (int argc, char **argv, app_widgets *widgets)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.progressbar", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
g_slice_free(app_widgets, widgets);
return status;
}
progressbar-overlay.c
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib.h>
/* gcc -o progressbar-overlay -fPIC progressbar-overlay.c `pkg-config --cflags --libs gtk+-3.0` */
guint threadID = 0;
typedef struct {
GtkWidget *progress_bar;
GtkWidget *button1;
GtkWidget *progress_label;
GtkStyleProvider *progressbar_style_provider;
} app_widgets;
void destroy_handler (GtkApplication* app, gpointer data)
{
g_application_quit(G_APPLICATION (data));
}
void
end_progress (gpointer user_data)
{
app_widgets *app_wdgts = (app_widgets *) user_data;
gdouble fraction;
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar));
g_print("%.0f%%\n", fraction*100);
}
static gboolean
fill (gpointer user_data)
{
app_widgets *app_wdgts = (app_widgets *) user_data;
gdouble fraction;
/*Get the current progress*/
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar));
if (fraction > 0.99) {
fraction = 0;
}
/*Increase the bar by 10% each time this function is called*/
if (fraction < 0.99)
fraction += 0.1;
/*Fill in the bar with the new fraction*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR(app_wdgts->progress_bar),
fraction);
gchar temp[256];
memset(&temp, 0x0, 256);
if (fraction > 0.99) {
snprintf(temp, 255, "Complete: %.0f %%", fraction*100);
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Repeat");
threadID = 0;
}
else {
snprintf(temp, 255, "Working: %.0f %%", fraction*100);
}
gtk_label_set_text (GTK_LABEL(app_wdgts->progress_label), temp);
/*Ensures that the fraction stays below 1.0*/
if (fraction < 0.99)
return TRUE;
return FALSE;
}
void
button1_clicked_cb (GtkButton *button,
app_widgets *app_wdgts)
{
if (threadID == 0)
{
threadID = g_timeout_add_full (0, 500, fill,
app_wdgts, end_progress);
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Pause");
}
else
{
GSource *source = g_main_context_find_source_by_id(NULL,
threadID);
if (source)
{
g_source_destroy (source);
}
threadID = 0;
gtk_button_set_label (GTK_BUTTON(app_wdgts->button1),
"Continue");
}
}
static void
progress_label_size_allocate (GtkWidget *label,
GdkRectangle *allocation,
gpointer user_data)
{
app_widgets *app_wdgts = (app_widgets *) user_data;
char *css_text;
css_text = g_strdup_printf ("progressbar trough,\n"
"progressbar progress\n"
"{\n"
" min-height: %dpx;\n"
"}\n",
allocation->height);
gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (app_wdgts->progressbar_style_provider),
css_text, -1, NULL);
g_free (css_text);
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkSizeGroup *size_group;
GtkWidget *window;
GtkWidget *grid;
GtkWidget *box;
GtkWidget *button2;
GtkWidget *progress_overlay;
app_widgets *widgets = g_slice_new(app_widgets);
gdouble fraction = 0.0;
/*Create a window with a title, and a default size*/
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Progress Bar");
gtk_window_set_default_size (GTK_WINDOW (window), 420, 60);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 20);
grid = gtk_grid_new ();
gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
gtk_grid_set_column_homogeneous (GTK_GRID (grid),
TRUE);
gtk_grid_set_row_homogeneous (GTK_GRID (grid),
FALSE);
/*Create a progressbar and add it to the window*/
widgets->progress_bar = gtk_progress_bar_new ();
widgets->button1 = gtk_button_new_with_label ("Begin ");
button2 = gtk_button_new_with_label ("Cancel ");
gtk_progress_bar_set_inverted (GTK_PROGRESS_BAR(widgets->progress_bar), FALSE);
gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR(widgets->progress_bar),
FALSE);
progress_overlay = gtk_overlay_new ();
gtk_widget_set_hexpand (progress_overlay, TRUE);
gtk_container_add (GTK_CONTAINER (progress_overlay),
widgets->progress_bar);
widgets->progress_label = gtk_label_new ("Ready ");
gtk_widget_set_valign (widgets->progress_label, GTK_ALIGN_CENTER);
gtk_overlay_add_overlay (GTK_OVERLAY (progress_overlay),
widgets->progress_label);
size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
gtk_size_group_add_widget (size_group, widgets->progress_bar);
gtk_size_group_add_widget (size_group, widgets->progress_label);
widgets->progressbar_style_provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
gtk_style_context_add_provider (gtk_widget_get_style_context (widgets->progress_bar),
widgets->progressbar_style_provider,
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_signal_connect (widgets->progress_label,
"size-allocate",
G_CALLBACK (progress_label_size_allocate),
widgets);
gtk_grid_attach (GTK_GRID (grid), progress_overlay, 0, 0, 4, 1);
gtk_grid_attach (GTK_GRID (grid), widgets->button1, 2, 1, 1, 1);
gtk_grid_attach_next_to (GTK_GRID (grid), button2, widgets->button1, GTK_POS_RIGHT, 1, 1);
gtk_box_pack_start (GTK_BOX (box), grid, TRUE, TRUE, 12);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_container_set_border_width(GTK_CONTAINER(window),5);
/*Fill in the given fraction of the bar. Has to be between 0.0-1.0 inclusive*/
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (widgets->progress_bar), fraction);
g_signal_connect (G_OBJECT(window), "destroy",
G_CALLBACK (destroy_handler), app);
g_signal_connect (widgets->button1, "clicked",
G_CALLBACK (button1_clicked_cb), widgets);
g_signal_connect (button2, "clicked",
G_CALLBACK (destroy_handler), app);
gtk_widget_show_all (window);
}
int
main (int argc, char **argv, app_widgets *widgets)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.progressbar_overlay", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
g_slice_free(app_widgets, widgets);
return status;
}
Last edited by Misko_2083 (2021-03-09 15:02:15)
Do you want to exit the Circus?
https://www.youtube.com/watch?v=ZJwQicZHp_c
Offline
Pages: 1
[ Generated in 0.018 seconds, 8 queries executed - Memory usage: 568.37 KiB (Peak: 597.91 KiB) ]