WINGs: Added 'const' attribute to function 'WMCreateHashTable'
[wmaker-crm.git] / WINGs / wprogressindicator.c
blob374df5e05da475b4363936a538df3e0ec28c9828
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(W_ViewDelegate * self, WMView * view);
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 /* Parameter not used, but tell the compiler that it is ok */
143 (void) self;
145 assert(width > 0);
146 assert(height > 0);
149 static void paintProgressIndicator(ProgressIndicator * pPtr)
151 W_Screen *scr = pPtr->view->screen;
152 GC bgc;
153 GC wgc;
154 GC lgc;
155 GC dgc;
156 WMSize size = pPtr->view->size;
157 int perc, w, h;
158 double unit, i;
159 Pixmap buffer;
161 bgc = WMColorGC(scr->black);
162 wgc = WMColorGC(scr->white);
163 lgc = WMColorGC(scr->gray);
164 dgc = WMColorGC(scr->darkGray);
166 unit = (double)(size.width - 3.0) / 100;
168 buffer = XCreatePixmap(scr->display, pPtr->view->window, size.width, size.height, scr->depth);
170 XFillRectangle(scr->display, buffer, lgc, 0, 0, size.width, size.height);
172 /* Calculate size of Progress to draw and paint ticks */
173 perc = (pPtr->value - pPtr->minValue) * 100 / (pPtr->maxValue - pPtr->minValue);
175 w = (int)((double)(perc * unit));
176 h = size.height - 2;
178 if (w > (size.width - 3))
179 w = size.width - 3;
181 if (w > 0) {
182 XFillRectangle(scr->display, buffer, lgc, 2, 1, w, h);
183 XFillRectangle(scr->display, buffer, scr->stippleGC, 2, 1, w, h);
184 W_DrawRelief(scr, buffer, 2, 1, w, h, WRFlat);
186 /* Draw Progress Marks */
187 i = (5.0 * unit);
189 #ifdef SHOW_PROGRESS_TICKS_ONLY
190 while ((int)i < w + 5) {
191 #else
192 while ((int)i < (size.width - 3)) {
193 #endif
194 XDrawLine(scr->display, buffer, dgc, (int)i + 2, h - 1, i + 2, h - 3);
196 i += (5.0 * unit);
198 #ifdef SHOW_PROGRESS_TICKS_ONLY
199 if ((int)i >= w)
200 break;
201 #endif
203 XDrawLine(scr->display, buffer, dgc, (int)i + 2, h - 1, i + 2, h - 6);
205 i += (5.0 * unit);
209 XDrawLine(scr->display, buffer, bgc, w + 2, 1, w + 2, h + 1);
210 XDrawLine(scr->display, buffer, lgc, 2, h, w + 2, h);
212 XDrawLine(scr->display, buffer, dgc, 0, 0, 0, size.height - 1);
213 XDrawLine(scr->display, buffer, dgc, 0, 0, size.width, 0);
214 XDrawLine(scr->display, buffer, bgc, 1, 1, 1, size.height - 1);
215 XDrawLine(scr->display, buffer, bgc, 1, 1, size.width - 1, 1);
217 XDrawLine(scr->display, buffer, wgc, size.width - 1, 0, size.width - 1, size.height - 1);
218 XDrawLine(scr->display, buffer, wgc, 0, size.height - 1, size.width - 1, size.height - 1);
220 XCopyArea(scr->display, buffer, pPtr->view->window, scr->copyGC, 0, 0, size.width, size.height, 0, 0);
222 XFreePixmap(scr->display, buffer);
225 static void handleEvents(XEvent * event, void *data)
227 ProgressIndicator *pPtr = (ProgressIndicator *) data;
229 CHECK_CLASS(data, WC_ProgressIndicator);
231 switch (event->type) {
232 case Expose:
233 if (event->xexpose.count != 0)
234 break;
235 paintProgressIndicator(pPtr);
236 break;
237 case DestroyNotify:
238 destroyProgressIndicator(pPtr);
239 break;
243 static void destroyProgressIndicator(ProgressIndicator * pPtr)
245 WMRemoveNotificationObserver(pPtr);
247 wfree(pPtr);