1 /* SPDX-FileCopyrightText: 2005 - Paolo Maggi
2 * SPDX-FileCopyrightText: 2016 - Sébastien Wilmet <swilmet@gnome.org>
3 * SPDX-License-Identifier: LGPL-3.0-or-later
4 */
5
6 /* Modified version of GeditProgressInfoBar. */
7
8 #include "config.h"
9 #include "tepl-progress-info-bar.h"
10 #include <glib/gi18n-lib.h>
11
12 enum
13 {
14 PROP_0,
15 PROP_HAS_CANCEL_BUTTON,
16 N_PROPERTIES
17 };
18
19 static GParamSpec *properties[N_PROPERTIES];
20
21 struct _TeplProgressInfoBar
22 {
23 TeplInfoBar parent_instance;
24
25 GtkLabel *label;
26 GtkProgressBar *progress_bar;
27 };
28
G_DEFINE_TYPE(TeplProgressInfoBar,_tepl_progress_info_bar,TEPL_TYPE_INFO_BAR)29 G_DEFINE_TYPE (TeplProgressInfoBar, _tepl_progress_info_bar, TEPL_TYPE_INFO_BAR)
30
31 static void
32 set_has_cancel_button (TeplProgressInfoBar *info_bar,
33 gboolean has_cancel_button)
34 {
35 if (has_cancel_button)
36 {
37 gtk_info_bar_add_button (GTK_INFO_BAR (info_bar),
38 _("_Cancel"),
39 GTK_RESPONSE_CANCEL);
40 }
41 }
42
43 static void
_tepl_progress_info_bar_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)44 _tepl_progress_info_bar_set_property (GObject *object,
45 guint prop_id,
46 const GValue *value,
47 GParamSpec *pspec)
48 {
49 TeplProgressInfoBar *bar = TEPL_PROGRESS_INFO_BAR (object);
50
51 switch (prop_id)
52 {
53 case PROP_HAS_CANCEL_BUTTON:
54 set_has_cancel_button (bar, g_value_get_boolean (value));
55 break;
56
57 default:
58 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
59 break;
60 }
61 }
62
63 static void
_tepl_progress_info_bar_class_init(TeplProgressInfoBarClass * klass)64 _tepl_progress_info_bar_class_init (TeplProgressInfoBarClass *klass)
65 {
66 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67
68 object_class->set_property = _tepl_progress_info_bar_set_property;
69
70 properties[PROP_HAS_CANCEL_BUTTON] =
71 g_param_spec_boolean ("has-cancel-button",
72 "Has Cancel Button",
73 "",
74 TRUE,
75 G_PARAM_WRITABLE |
76 G_PARAM_CONSTRUCT_ONLY |
77 G_PARAM_STATIC_STRINGS);
78
79 g_object_class_install_properties (object_class, N_PROPERTIES, properties);
80 }
81
82 static void
_tepl_progress_info_bar_init(TeplProgressInfoBar * info_bar)83 _tepl_progress_info_bar_init (TeplProgressInfoBar *info_bar)
84 {
85 GtkGrid *vgrid;
86 GtkWidget *content_area;
87
88 vgrid = GTK_GRID (gtk_grid_new ());
89 gtk_orientable_set_orientation (GTK_ORIENTABLE (vgrid), GTK_ORIENTATION_VERTICAL);
90 gtk_grid_set_row_spacing (vgrid, 6);
91
92 info_bar->label = tepl_info_bar_create_label ();
93 gtk_container_add (GTK_CONTAINER (vgrid),
94 GTK_WIDGET (info_bar->label));
95
96 info_bar->progress_bar = GTK_PROGRESS_BAR (gtk_progress_bar_new ());
97 gtk_widget_set_hexpand (GTK_WIDGET (info_bar->progress_bar), TRUE);
98 gtk_container_add (GTK_CONTAINER (vgrid),
99 GTK_WIDGET (info_bar->progress_bar));
100
101 content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
102 gtk_container_add (GTK_CONTAINER (content_area),
103 GTK_WIDGET (vgrid));
104
105 gtk_widget_show_all (GTK_WIDGET (vgrid));
106 }
107
108 TeplProgressInfoBar *
_tepl_progress_info_bar_new(const gchar * markup,gboolean has_cancel_button)109 _tepl_progress_info_bar_new (const gchar *markup,
110 gboolean has_cancel_button)
111 {
112 TeplProgressInfoBar *info_bar;
113
114 g_return_val_if_fail (markup != NULL, NULL);
115
116 info_bar = g_object_new (TEPL_TYPE_PROGRESS_INFO_BAR,
117 "has-cancel-button", has_cancel_button,
118 NULL);
119
120 _tepl_progress_info_bar_set_markup (info_bar, markup);
121
122 return info_bar;
123 }
124
125 void
_tepl_progress_info_bar_set_markup(TeplProgressInfoBar * info_bar,const gchar * markup)126 _tepl_progress_info_bar_set_markup (TeplProgressInfoBar *info_bar,
127 const gchar *markup)
128 {
129 g_return_if_fail (TEPL_IS_PROGRESS_INFO_BAR (info_bar));
130 g_return_if_fail (markup != NULL);
131
132 gtk_label_set_markup (info_bar->label, markup);
133 }
134
135 void
_tepl_progress_info_bar_set_text(TeplProgressInfoBar * info_bar,const gchar * text)136 _tepl_progress_info_bar_set_text (TeplProgressInfoBar *info_bar,
137 const gchar *text)
138 {
139 g_return_if_fail (TEPL_IS_PROGRESS_INFO_BAR (info_bar));
140 g_return_if_fail (text != NULL);
141
142 gtk_label_set_text (info_bar->label, text);
143 }
144
145 void
_tepl_progress_info_bar_set_fraction(TeplProgressInfoBar * info_bar,gdouble fraction)146 _tepl_progress_info_bar_set_fraction (TeplProgressInfoBar *info_bar,
147 gdouble fraction)
148 {
149 g_return_if_fail (TEPL_IS_PROGRESS_INFO_BAR (info_bar));
150
151 gtk_progress_bar_set_fraction (info_bar->progress_bar, fraction);
152 }
153
154 void
_tepl_progress_info_bar_pulse(TeplProgressInfoBar * info_bar)155 _tepl_progress_info_bar_pulse (TeplProgressInfoBar *info_bar)
156 {
157 g_return_if_fail (TEPL_IS_PROGRESS_INFO_BAR (info_bar));
158
159 gtk_progress_bar_pulse (info_bar->progress_bar);
160 }
161