chiark / gitweb /
Imported Upstream version 1.0.0
[e16] / src / progress.c
1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "E.h"
24 #include "eobj.h"
25 #include "iclass.h"
26 #include "progress.h"
27 #include "tclass.h"
28 #include "xwin.h"
29
30 struct _progressbar {
31    EObj               *win;
32    EObj               *n_win;
33    EObj               *p_win;
34    int                 w, h;
35    int                 value;
36    ImageClass         *ic;
37    TextClass          *tc, *tnc;
38 };
39
40 static int          pnum = 0;
41 static Progressbar **plist = NULL;
42
43 Progressbar        *
44 ProgressbarCreate(const char *name, int w, int h)
45 {
46    Progressbar        *p;
47    int                 x, y, tw, th;
48    EImageBorder       *pad;
49
50    p = ECALLOC(Progressbar, 1);
51    pnum++;
52    plist = EREALLOC(Progressbar *, plist, pnum);
53    plist[pnum - 1] = p;
54
55    p->ic = ImageclassAlloc("PROGRESS_BAR", 1);
56    p->tc = TextclassAlloc("PROGRESS_TEXT", 1);
57    p->tnc = TextclassAlloc("PROGRESS_TEXT_NUMBER", 1);
58
59    pad = ImageclassGetPadding(p->ic);
60    TextSize(p->tc, 0, 0, 0, name, &tw, &th, 0);
61    if (h < th + pad->top + pad->bottom)
62       h = th + pad->top + pad->bottom;
63
64    p->w = w;
65    p->h = h;
66    p->value = 0;
67
68    x = (WinGetW(VROOT) - w) / 2;
69    y = 32 + (pnum * h * 2);
70
71    p->win = EobjWindowCreate(EOBJ_TYPE_MISC, x, y, w - (h * 5), h, 1, name);
72    p->n_win =
73       EobjWindowCreate(EOBJ_TYPE_MISC, x + w - (h * 5), y, (h * 5), h, 1, "pn");
74    p->p_win = EobjWindowCreate(EOBJ_TYPE_MISC, x, y + h, 1, h, 1, "pp");
75    if (!p->win || !p->n_win || !p->p_win)
76      {
77         ProgressbarDestroy(p);
78         return NULL;
79      }
80    p->win->shadow = 1;
81    p->n_win->shadow = 1;
82    p->p_win->shadow = 1;
83
84    return p;
85 }
86
87 void
88 ProgressbarDestroy(Progressbar * p)
89 {
90    int                 i, j, dy;
91
92    ProgressbarHide(p);
93
94    dy = 2 * p->h;
95    EobjWindowDestroy(p->win);
96    EobjWindowDestroy(p->n_win);
97    EobjWindowDestroy(p->p_win);
98
99    for (i = 0; i < pnum; i++)
100      {
101         if (plist[i] != p)
102            continue;
103
104         for (j = i; j < pnum - 1; j++)
105           {
106              Progressbar        *pp;
107
108              pp = plist[j + 1];
109              plist[j] = pp;
110              EobjMove(pp->win, EobjGetX(pp->win), EobjGetY(pp->win) - dy);
111              EobjMove(pp->n_win, EobjGetX(pp->n_win), EobjGetY(pp->n_win) - dy);
112              EobjMove(pp->p_win, EobjGetX(pp->p_win), EobjGetY(pp->p_win) - dy);
113           }
114         break;
115      }
116
117    ImageclassFree(p->ic);
118    TextclassFree(p->tc);
119    TextclassFree(p->tnc);
120
121    Efree(p);
122
123    pnum--;
124    if (pnum <= 0)
125      {
126         pnum = 0;
127         _EFREE(plist);
128      }
129    else
130      {
131         plist = EREALLOC(Progressbar *, plist, pnum);
132      }
133 }
134
135 void
136 ProgressbarSet(Progressbar * p, int progress)
137 {
138    int                 w;
139    char                s[64];
140    EImageBorder       *pad;
141
142    if (progress == p->value)
143       return;
144
145    p->value = progress;
146    w = (p->value * p->w) / 100;
147    if (w < 1)
148       w = 1;
149    if (w > p->w)
150       w = p->w;
151    Esnprintf(s, sizeof(s), "%i%%", p->value);
152
153    EobjResize(p->p_win, w, p->h);
154    ImageclassApply(p->ic, EobjGetWin(p->p_win), 1, 0, STATE_NORMAL, ST_SOLID);
155    EobjShapeUpdate(p->p_win, 0);
156
157    pad = ImageclassGetPadding(p->ic);
158    EClearWindow(EobjGetWin(p->n_win));
159    TextDraw(p->tnc, EobjGetWin(p->n_win), None, 0, 0, STATE_CLICKED, s,
160             pad->left, pad->top, p->h * 5 - (pad->left + pad->right),
161             p->h - (pad->top + pad->bottom), p->h - (pad->top + pad->bottom),
162             TextclassGetJustification(p->tnc));
163    /* Hack - We may not be running in the event loop here */
164    EobjDamage(p->n_win);
165
166    EobjsRepaint();
167 }
168
169 void
170 ProgressbarShow(Progressbar * p)
171 {
172    EImageBorder       *pad;
173
174    ImageclassApply(p->ic, EobjGetWin(p->win), 0, 0, STATE_NORMAL, ST_SOLID);
175    ImageclassApply(p->ic, EobjGetWin(p->n_win), 0, 0, STATE_CLICKED, ST_SOLID);
176    ImageclassApply(p->ic, EobjGetWin(p->p_win), 1, 0, STATE_NORMAL, ST_SOLID);
177
178    EobjMap(p->win, 0);
179    EobjMap(p->n_win, 0);
180    EobjMap(p->p_win, 0);
181
182    pad = ImageclassGetPadding(p->ic);
183    TextDraw(p->tc, EobjGetWin(p->win), None, 0, 0, STATE_NORMAL,
184             EobjGetName(p->win), pad->left, pad->top,
185             p->w - (p->h * 5) - (pad->left + pad->right),
186             p->h - (pad->top + pad->bottom), p->h - (pad->top + pad->bottom),
187             TextclassGetJustification(p->tnc));
188
189    EobjsRepaint();
190 }
191
192 void
193 ProgressbarHide(Progressbar * p)
194 {
195    EobjUnmap(p->win);
196    EobjUnmap(p->n_win);
197    EobjUnmap(p->p_win);
198 }