9 #define YUILogComponent "gtk"
10 #include <yui/Libyui_config.h>
15 std::string size_stdform (YFileSize_t size)
17 long double mantissa = size;
19 for (; mantissa/1024 > 1; unit++)
22 const char *unit_str =
"";
25 case 0: unit_str = _(
"B");
break;
26 case 1: unit_str = _(
"KB");
break;
27 case 2: unit_str = _(
"MB");
break;
28 case 3: unit_str = _(
"GB");
break;
29 case 4: unit_str = _(
"TB");
break;
30 default: mantissa = 0;
break;
33 gchar *text = g_strdup_printf (
"%.1f %s", (
float) mantissa, unit_str);
34 std::string str (text);
39 #include "YProgressBar.h"
44 YGProgressBar (YWidget *parent,
const std::string &label,
int maxValue)
45 : YProgressBar (NULL, label, maxValue)
49 ,
YGLabeledWidget (
this, parent, label, YD_VERT, GTK_TYPE_PROGRESS_BAR, NULL)
53 virtual void setValue (
int value)
55 YProgressBar::setValue (value);
56 GtkProgressBar *bar = GTK_PROGRESS_BAR (getWidget());
57 float fraction = CLAMP ((
float) value / maxValue(), 0, 1);
58 gtk_progress_bar_set_fraction (bar, fraction);
60 char *text = g_strdup_printf (
"%d %%", (
int) (fraction*100));
61 gtk_progress_bar_set_text (bar, text);
63 gtk_progress_bar_set_show_text(bar,
true);
65 gtk_main_iteration_do(
false);
68 virtual unsigned int getMinSize (YUIDimension dim)
69 {
return dim == YD_HORIZ ? 200 : 0; }
71 YGLABEL_WIDGET_IMPL (YProgressBar)
74 YProgressBar *YGWidgetFactory::createProgressBar (YWidget *parent,
const std::string &label,
80 #include "YDownloadProgress.h"
88 const std::string &filename, YFileSize_t expectedFileSize)
89 : YDownloadProgress (NULL, label, filename, expectedFileSize)
90 ,
YGLabeledWidget (
this, parent, label, YD_HORIZ, GTK_TYPE_PROGRESS_BAR, NULL)
92 timeout_id = g_timeout_add (250, timeout_cb,
this);
97 g_source_remove (timeout_id);
100 virtual void setExpectedSize (YFileSize_t size)
102 YDownloadProgress::setExpectedSize (size);
106 static gboolean timeout_cb (
void *pData)
109 GtkProgressBar *bar = GTK_PROGRESS_BAR (pThis->getWidget());
111 gtk_progress_bar_set_fraction (bar, pThis->currentPercent() / 100.0);
112 if (pThis->expectedSize() > 0) {
113 std::string current (size_stdform (pThis->currentFileSize()));
114 std::string total (size_stdform (pThis->expectedSize()));
115 std::string text = current +
" " + _(
"of") +
" " + total;
116 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (bar), text.c_str());
121 YGLABEL_WIDGET_IMPL (YDownloadProgress)
124 YDownloadProgress *YGOptionalWidgetFactory::createDownloadProgress (YWidget *parent,
125 const std::string &label,
const std::string &filename, YFileSize_t expectedFileSize)
128 #include "ygtkratiobox.h"
129 #include "YMultiProgressMeter.h"
135 : YMultiProgressMeter (NULL, dim, maxValues)
137 horizontal() ? YGTK_TYPE_RATIO_HBOX : YGTK_TYPE_RATIO_VBOX, NULL)
139 ygtk_ratio_box_set_spacing (YGTK_RATIO_BOX (getWidget()), 2);
140 for (
int s = 0; s < segments(); s++) {
141 GtkWidget *bar = gtk_progress_bar_new();
142 gtk_orientable_set_orientation(GTK_ORIENTABLE(bar), horizontal() ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL);
143 gtk_progress_bar_set_inverted(GTK_PROGRESS_BAR (bar), horizontal() ? FALSE : TRUE);
145 const int min_size = 5;
147 gtk_widget_set_size_request (bar, min_size, -1);
149 gtk_widget_set_size_request (bar, -1, min_size);
150 ygtk_ratio_box_pack (YGTK_RATIO_BOX (getWidget()), bar, getSegmentWeight (s));
153 ygtk_adj_size_set_max (YGTK_ADJ_SIZE (m_adj_size), horizontal() ? 200 : 0,
154 horizontal() ? 0 : 200);
155 gtk_widget_show_all (getWidget());
158 virtual void doUpdate()
160 GList* children = gtk_container_get_children (GTK_CONTAINER (getWidget()));
162 for (GList *i = children; i && s < segments(); i = i->next, s++) {
163 GtkProgressBar *bar = GTK_PROGRESS_BAR (i->data);
164 gtk_progress_bar_set_fraction (bar, getSegmentValue (s));
166 g_list_free (children);
169 int getSegmentWeight (
int n)
172 n = (segments() - n) - 1;
175 float getSegmentValue (
int n)
178 n = (segments() - n) - 1;
179 if (currentValue (n) < 0)
181 return 1.0 - (((float) currentValue (n)) / maxValue (n));
184 YGWIDGET_IMPL_COMMON (YMultiProgressMeter)
187 YMultiProgressMeter *YGOptionalWidgetFactory::createMultiProgressMeter (YWidget *parent,
188 YUIDimension dim,
const std::vector <float> &maxValues)
191 #include "YBusyIndicator.h"
197 #define PULSE_INTERVAL 100
198 #define PULSE_STEP 0.050
202 guint pulse_timeout_id;
206 YGBusyIndicator (YWidget *parent,
const std::string &label,
int timeout)
207 : YBusyIndicator (NULL, label, timeout)
209 GTK_TYPE_PROGRESS_BAR,
"pulse-step", PULSE_STEP, NULL)
211 pulse_timeout_id = 0;
220 alive_timeout = timeout();
221 if (!pulse_timeout_id)
222 pulse_timeout_id = g_timeout_add (PULSE_INTERVAL, pulse_timeout_cb,
this);
228 if (pulse_timeout_id) {
229 g_source_remove (pulse_timeout_id);
230 pulse_timeout_id = 0;
235 virtual void setAlive (
bool alive)
237 YBusyIndicator::setAlive (alive);
238 alive ? pulse() : stop();
242 static gboolean pulse_timeout_cb (
void *pData)
245 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pThis->getWidget()));
246 pThis->alive_timeout -= PULSE_INTERVAL;
247 if (pThis->alive_timeout <= 0) {
248 pThis->pulse_timeout_id = 0;
254 YGLABEL_WIDGET_IMPL (YBusyIndicator)
257 YBusyIndicator *YGWidgetFactory::createBusyIndicator (YWidget *parent,
const std::string &label,
int timeout)