2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntinternal.h"
24 #include "gntprogressbar.h"
29 typedef struct _GntProgressBarPrivate
33 GntProgressBarOrientation orientation
;
34 } GntProgressBarPrivate
;
36 struct _GntProgressBar
41 #define GNT_PROGRESS_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GNT_TYPE_PROGRESS_BAR, GntProgressBarPrivate))
43 static GntWidgetClass
*parent_class
= NULL
;
47 gnt_progress_bar_draw (GntWidget
*widget
)
49 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (GNT_PROGRESS_BAR (widget
));
51 gint start
, end
, i
, pos
;
54 g_snprintf (progress
, sizeof (progress
), "%.1f%%", priv
->fraction
* 100);
55 color
= gnt_color_pair(GNT_COLOR_NORMAL
);
57 switch (priv
->orientation
) {
58 case GNT_PROGRESS_LEFT_TO_RIGHT
:
59 case GNT_PROGRESS_RIGHT_TO_LEFT
:
60 start
= (priv
->orientation
== GNT_PROGRESS_LEFT_TO_RIGHT
? 0 : (1.0 - priv
->fraction
) * widget
->priv
.width
);
61 end
= (priv
->orientation
== GNT_PROGRESS_LEFT_TO_RIGHT
? widget
->priv
.width
* priv
->fraction
: widget
->priv
.width
);
64 for (i
= 0; i
< widget
->priv
.height
; i
++)
65 mvwhline (widget
->window
, i
, 0, ' ' | color
, widget
->priv
.width
);
68 for (i
= 0; i
< widget
->priv
.height
; i
++)
69 mvwhline (widget
->window
, i
, start
, ACS_CKBOARD
| color
| A_REVERSE
, end
);
72 if (priv
->show_value
) {
73 pos
= widget
->priv
.width
/ 2 - strlen (progress
) / 2;
74 for (i
= 0; i
< progress
[i
]; i
++, pos
++) {
75 wattrset (widget
->window
, color
| ((pos
< start
|| pos
> end
) ? A_NORMAL
: A_REVERSE
));
76 mvwprintw (widget
->window
, widget
->priv
.height
/ 2, pos
, "%c", progress
[i
]);
78 wattrset (widget
->window
, color
);
82 case GNT_PROGRESS_TOP_TO_BOTTOM
:
83 case GNT_PROGRESS_BOTTOM_TO_TOP
:
84 start
= (priv
->orientation
== GNT_PROGRESS_TOP_TO_BOTTOM
? 0 : (1.0 - priv
->fraction
) * widget
->priv
.height
);
85 end
= (priv
->orientation
== GNT_PROGRESS_TOP_TO_BOTTOM
? widget
->priv
.height
* priv
->fraction
: widget
->priv
.height
);
88 for (i
= 0; i
< widget
->priv
.width
; i
++)
89 mvwvline (widget
->window
, 0, i
, ' ' | color
, widget
->priv
.height
);
92 for (i
= 0; i
< widget
->priv
.width
; i
++)
93 mvwvline (widget
->window
, start
, i
, ACS_CKBOARD
| color
| A_REVERSE
, end
);
96 if (priv
->show_value
) {
97 pos
= widget
->priv
.height
/ 2 - strlen (progress
) / 2;
98 for (i
= 0; i
< progress
[i
]; i
++, pos
++) {
99 wattrset (widget
->window
, color
| ((pos
< start
|| pos
> end
) ? A_NORMAL
: A_REVERSE
));
100 mvwprintw (widget
->window
, pos
, widget
->priv
.width
/ 2, "%c\n", progress
[i
]);
102 wattrset (widget
->window
, color
);
107 g_assert_not_reached ();
112 gnt_progress_bar_size_request (GntWidget
*widget
)
114 gnt_widget_set_size (widget
, widget
->priv
.minw
, widget
->priv
.minh
);
118 gnt_progress_bar_class_init (gpointer klass
, gpointer class_data
)
120 GObjectClass
*g_class
= G_OBJECT_CLASS (klass
);
122 parent_class
= GNT_WIDGET_CLASS (klass
);
124 g_type_class_add_private (g_class
, sizeof (GntProgressBarPrivate
));
126 parent_class
->draw
= gnt_progress_bar_draw
;
127 parent_class
->size_request
= gnt_progress_bar_size_request
;
131 gnt_progress_bar_init (GTypeInstance
*instance
, gpointer g_class
)
133 GntWidget
*widget
= GNT_WIDGET (instance
);
134 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (GNT_PROGRESS_BAR (widget
));
136 gnt_widget_set_take_focus (widget
, FALSE
);
137 GNT_WIDGET_SET_FLAGS (widget
, GNT_WIDGET_NO_BORDER
| GNT_WIDGET_NO_SHADOW
| GNT_WIDGET_GROW_X
);
139 widget
->priv
.minw
= 8;
140 widget
->priv
.minh
= 1;
142 priv
->show_value
= TRUE
;
146 gnt_progress_bar_get_type (void)
148 static GType type
= 0;
151 static const GTypeInfo info
= {
152 sizeof (GntProgressBarClass
),
153 NULL
, /* base_init */
154 NULL
, /* base_finalize */
155 gnt_progress_bar_class_init
, /* class_init */
156 NULL
, /* class_finalize */
157 NULL
, /* class_data */
158 sizeof (GntProgressBar
),
160 gnt_progress_bar_init
, /* instance_init */
161 NULL
/* value_table */
164 type
= g_type_register_static (GNT_TYPE_WIDGET
, "GntProgressBar", &info
, 0);
171 gnt_progress_bar_new (void)
173 GntWidget
*widget
= g_object_new (GNT_TYPE_PROGRESS_BAR
, NULL
);
178 gnt_progress_bar_set_fraction (GntProgressBar
*pbar
, gdouble fraction
)
180 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
183 priv
->fraction
= 1.0;
184 else if (fraction
< 0.0)
185 priv
->fraction
= 0.0;
187 priv
->fraction
= fraction
;
189 if ((GNT_WIDGET_FLAGS(pbar
) & GNT_WIDGET_MAPPED
))
190 gnt_widget_draw(GNT_WIDGET(pbar
));
194 gnt_progress_bar_set_orientation (GntProgressBar
*pbar
,
195 GntProgressBarOrientation orientation
)
197 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
198 GntWidget
*widget
= GNT_WIDGET(pbar
);
200 priv
->orientation
= orientation
;
201 if (orientation
== GNT_PROGRESS_LEFT_TO_RIGHT
||
202 orientation
== GNT_PROGRESS_RIGHT_TO_LEFT
) {
203 GNT_WIDGET_SET_FLAGS(pbar
, GNT_WIDGET_GROW_X
);
204 GNT_WIDGET_UNSET_FLAGS(pbar
, GNT_WIDGET_GROW_Y
);
205 widget
->priv
.minw
= 8;
206 widget
->priv
.minh
= 1;
208 GNT_WIDGET_UNSET_FLAGS(pbar
, GNT_WIDGET_GROW_X
);
209 GNT_WIDGET_SET_FLAGS(pbar
, GNT_WIDGET_GROW_Y
);
210 widget
->priv
.minw
= 1;
211 widget
->priv
.minh
= 8;
214 if ((GNT_WIDGET_FLAGS(pbar
) & GNT_WIDGET_MAPPED
))
215 gnt_widget_draw(GNT_WIDGET(pbar
));
219 gnt_progress_bar_set_show_progress (GntProgressBar
*pbar
, gboolean show
)
221 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
222 priv
->show_value
= show
;
226 gnt_progress_bar_get_fraction (GntProgressBar
*pbar
)
228 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
229 return priv
->fraction
;
232 GntProgressBarOrientation
233 gnt_progress_bar_get_orientation (GntProgressBar
*pbar
)
235 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
236 return priv
->orientation
;
240 gnt_progress_bar_get_show_progress (GntProgressBar
*pbar
)
242 GntProgressBarPrivate
*priv
= GNT_PROGRESS_BAR_GET_PRIVATE (pbar
);
243 return priv
->show_value
;