Coding style cleanup in dock.c
[wmaker-crm.git] / WINGs / wprogressindicator.c
bloba897a9ebd208826fb8361c2136761c45dbfd1a65
1 /*
2 * Original idea and implementation by Frederik Schueler <fr.schueler@netsurf.de>
3 * Rewritten by Pascal Hofstee <daeron@windowmaker.info>
4 * - Added options to set min/max values
5 * - centralized drawing into one pain function
6 */
8 #include "WINGsP.h"
10 typedef struct W_ProgressIndicator {
11 W_Class widgetClass;
12 W_View *view;
14 int value;
15 int minValue;
16 int maxValue;
18 void *clientData;
19 } ProgressIndicator;
21 #define DEFAULT_PROGRESS_INDICATOR_WIDTH 276
22 #define DEFAULT_PROGRESS_INDICATOR_HEIGHT 16
24 /* define if only the ticks within the progress region should be displayed */
25 #undef SHOW_PROGRESS_TICKS_ONLY
27 static void didResizeProgressIndicator();
29 W_ViewDelegate _ProgressIndicatorDelegate = {
30 NULL,
31 NULL,
32 didResizeProgressIndicator,
33 NULL,
34 NULL
37 static void destroyProgressIndicator(ProgressIndicator * pPtr);
38 static void paintProgressIndicator(ProgressIndicator * pPtr);
39 static void handleEvents(XEvent * event, void *data);
41 WMProgressIndicator *WMCreateProgressIndicator(WMWidget * parent)
43 ProgressIndicator *pPtr;
45 pPtr = wmalloc(sizeof(ProgressIndicator));
47 pPtr->widgetClass = WC_ProgressIndicator;
49 pPtr->view = W_CreateView(W_VIEW(parent));
50 if (!pPtr->view) {
51 wfree(pPtr);
52 return NULL;
55 pPtr->view->self = pPtr;
57 pPtr->view->delegate = &_ProgressIndicatorDelegate;
59 WMCreateEventHandler(pPtr->view, ExposureMask | StructureNotifyMask, handleEvents, pPtr);
61 W_ResizeView(pPtr->view, DEFAULT_PROGRESS_INDICATOR_WIDTH, DEFAULT_PROGRESS_INDICATOR_HEIGHT);
63 /* Initialize ProgressIndicator Values */
64 pPtr->value = 0;
65 pPtr->minValue = 0;
66 pPtr->maxValue = 100;
68 return pPtr;
71 void WMSetProgressIndicatorMinValue(WMProgressIndicator * progressindicator, int value)
73 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
75 progressindicator->minValue = value;
76 if (progressindicator->value < value) {
77 progressindicator->value = value;
78 if (progressindicator->view->flags.mapped) {
79 paintProgressIndicator(progressindicator);
84 void WMSetProgressIndicatorMaxValue(WMProgressIndicator * progressindicator, int value)
86 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
88 progressindicator->maxValue = value;
89 if (progressindicator->value > value) {
90 progressindicator->value = value;
91 if (progressindicator->view->flags.mapped) {
92 paintProgressIndicator(progressindicator);
97 void WMSetProgressIndicatorValue(WMProgressIndicator * progressindicator, int value)
99 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
101 progressindicator->value = value;
103 /* Check if value is within min/max-range */
104 if (progressindicator->minValue > value)
105 progressindicator->value = progressindicator->minValue;
107 if (progressindicator->maxValue < value)
108 progressindicator->value = progressindicator->maxValue;
110 if (progressindicator->view->flags.mapped) {
111 paintProgressIndicator(progressindicator);
115 int WMGetProgressIndicatorMinValue(WMProgressIndicator * progressindicator)
117 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
119 return progressindicator->minValue;
122 int WMGetProgressIndicatorMaxValue(WMProgressIndicator * progressindicator)
124 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
126 return progressindicator->maxValue;
129 int WMGetProgressIndicatorValue(WMProgressIndicator * progressindicator)
131 CHECK_CLASS(progressindicator, WC_ProgressIndicator);
133 return progressindicator->value;
136 static void didResizeProgressIndicator(W_ViewDelegate * self, WMView * view)
138 WMProgressIndicator *pPtr = (WMProgressIndicator *) view->self;
139 int width = pPtr->view->size.width;
140 int height = pPtr->view->size.height;
142 assert(width > 0);
143 assert(height > 0);
146 static void paintProgressIndicator(ProgressIndicator * pPtr)
148 W_Screen *scr = pPtr->view->screen;
149 GC bgc;
150 GC wgc;
151 GC lgc;
152 GC dgc;
153 WMSize size = pPtr->view->size;
154 int perc, w, h;
155 double unit, i;
156 Pixmap buffer;
158 bgc = WMColorGC(scr->black);
159 wgc = WMColorGC(scr->white);
160 lgc = WMColorGC(scr->gray);
161 dgc = WMColorGC(scr->darkGray);
163 unit = (double)(size.width - 3.0) / 100;
165 buffer = XCreatePixmap(scr->display, pPtr->view->window, size.width, size.height, scr->depth);
167 XFillRectangle(scr->display, buffer, lgc, 0, 0, size.width, size.height);
169 /* Calculate size of Progress to draw and paint ticks */
170 perc = (pPtr->value - pPtr->minValue) * 100 / (pPtr->maxValue - pPtr->minValue);
172 w = (int)((double)(perc * unit));
173 h = size.height - 2;
175 if (w > (size.width - 3))
176 w = size.width - 3;
178 if (w > 0) {
179 XFillRectangle(scr->display, buffer, lgc, 2, 1, w, h);
180 XFillRectangle(scr->display, buffer, scr->stippleGC, 2, 1, w, h);
181 W_DrawRelief(scr, buffer, 2, 1, w, h, WRFlat);
183 /* Draw Progress Marks */
184 i = (5.0 * unit);
186 #ifdef SHOW_PROGRESS_TICKS_ONLY
187 while ((int)i < w + 5) {
188 #else
189 while ((int)i < (size.width - 3)) {
190 #endif
191 XDrawLine(scr->display, buffer, dgc, (int)i + 2, h - 1, i + 2, h - 3);
193 i += (5.0 * unit);
195 #ifdef SHOW_PROGRESS_TICKS_ONLY
196 if ((int)i >= w)
197 break;
198 #endif
200 XDrawLine(scr->display, buffer, dgc, (int)i + 2, h - 1, i + 2, h - 6);
202 i += (5.0 * unit);
206 XDrawLine(scr->display, buffer, bgc, w + 2, 1, w + 2, h + 1);
207 XDrawLine(scr->display, buffer, lgc, 2, h, w + 2, h);
209 XDrawLine(scr->display, buffer, dgc, 0, 0, 0, size.height - 1);
210 XDrawLine(scr->display, buffer, dgc, 0, 0, size.width, 0);
211 XDrawLine(scr->display, buffer, bgc, 1, 1, 1, size.height - 1);
212 XDrawLine(scr->display, buffer, bgc, 1, 1, size.width - 1, 1);
214 XDrawLine(scr->display, buffer, wgc, size.width - 1, 0, size.width - 1, size.height - 1);
215 XDrawLine(scr->display, buffer, wgc, 0, size.height - 1, size.width - 1, size.height - 1);
217 XCopyArea(scr->display, buffer, pPtr->view->window, scr->copyGC, 0, 0, size.width, size.height, 0, 0);
219 XFreePixmap(scr->display, buffer);
222 static void handleEvents(XEvent * event, void *data)
224 ProgressIndicator *pPtr = (ProgressIndicator *) data;
226 CHECK_CLASS(data, WC_ProgressIndicator);
228 switch (event->type) {
229 case Expose:
230 if (event->xexpose.count != 0)
231 break;
232 paintProgressIndicator(pPtr);
233 break;
234 case DestroyNotify:
235 destroyProgressIndicator(pPtr);
236 break;
240 static void destroyProgressIndicator(ProgressIndicator * pPtr)
242 WMRemoveNotificationObserver(pPtr);
244 wfree(pPtr);