wmbattery: fix a couple of potential memory-leaks.
[dockapps.git] / wmmisc / src / dockapp_draw.c
blob2a5a88ed125bdfa772c60e49c720120bfc368448
1 /*
2 * wmmisc - WindowMaker Dockapp for monitoring misc. information.
3 * Copyright (C) 2003-2006 Jesse S. (luxorfalls@sbcglobal.net)
5 * wmmisc is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * wmmisc is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with wmmisc; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <stdio.h>
22 #include <string.h>
24 #include <libdockapp/wmgeneral.h>
25 #include "dockapp_draw.h"
26 #include "dockapp_utils.h"
28 static dockapp_proc_t dockapp_proc;
30 void
31 dockapp_draw_big_digit( unsigned int digit_value,
32 int digit_zero_count,
33 int digit_draw_location_x,
34 int digit_draw_location_y )
36 int digit_value_of_ten = 1;
37 int digit_maximum_value_of_ten = 0;
38 int digit_new_draw_location_x = 0;
40 if ( ( ( DOCKAPP_WIDTH - DOCKAPP_BIG_CHAR_WIDTH ) < digit_draw_location_x
41 || ( DOCKAPP_HEIGHT - DOCKAPP_BIG_CHAR_HEIGHT ) < digit_draw_location_y )
42 || ( 0 > digit_draw_location_x || 0 > digit_draw_location_y ) )
44 fprintf( stderr,
45 "%s: Invalid x,y position: %d,%d\n",
46 __func__,
47 digit_draw_location_x,
48 digit_draw_location_y );
49 return;
52 for ( digit_maximum_value_of_ten = 0;
53 digit_maximum_value_of_ten < digit_zero_count;
54 ++digit_maximum_value_of_ten )
56 digit_value_of_ten *= 10;
59 digit_new_draw_location_x = digit_draw_location_x;
63 copyXPMArea( ( ( ( digit_value / digit_value_of_ten ) % 10 ) *
64 DOCKAPP_BIG_CHAR_WIDTH + DOCKAPP_BIG_DIGIT_X ),
65 DOCKAPP_BIG_DIGIT_Y,
66 DOCKAPP_BIG_CHAR_WIDTH,
67 DOCKAPP_BIG_CHAR_HEIGHT,
68 digit_new_draw_location_x,
69 digit_draw_location_y );
71 digit_value_of_ten /= 10;
72 digit_new_draw_location_x += DOCKAPP_BIG_CHAR_WIDTH;
73 } while ( 0 < digit_value_of_ten );
75 RedrawWindow();
78 void
79 dockapp_draw_small_digit( unsigned int d_value,
80 int d_zero,
81 int digit_draw_location_x,
82 int digit_draw_location_y )
84 int digit_value_of_ten = 1;
85 int digit_maximum_value_of_ten = 0;
86 int digit_new_draw_location_x = 0;
88 if ( ( ( DOCKAPP_WIDTH - DOCKAPP_BIG_CHAR_WIDTH ) < digit_draw_location_x
89 || ( DOCKAPP_HEIGHT - DOCKAPP_BIG_CHAR_HEIGHT ) < digit_draw_location_y )
90 || ( 0 > digit_draw_location_x || 0 > digit_draw_location_y ) )
92 fprintf( stderr,
93 "%s: Invalid x,y position: %d,%d\n",
94 __func__,
95 digit_draw_location_x,
96 digit_draw_location_y );
97 return;
100 for ( digit_maximum_value_of_ten = 0;
101 digit_maximum_value_of_ten < d_zero;
102 ++digit_maximum_value_of_ten )
104 digit_value_of_ten *= 10;
107 digit_new_draw_location_x = digit_draw_location_x;
111 copyXPMArea( ( ( ( d_value / digit_value_of_ten) % 10) *
112 DOCKAPP_SMALL_CHAR_WIDTH + DOCKAPP_SMALL_DIGIT_X ),
113 DOCKAPP_SMALL_DIGIT_Y,
114 DOCKAPP_SMALL_CHAR_WIDTH,
115 DOCKAPP_SMALL_CHAR_HEIGHT,
116 digit_new_draw_location_x,
117 digit_draw_location_y );
119 digit_value_of_ten /= 10;
120 digit_new_draw_location_x += DOCKAPP_SMALL_CHAR_WIDTH;
121 } while ( 0 < digit_value_of_ten );
123 RedrawWindow();
126 void
127 dockapp_draw_big_str( const char* string_to_draw,
128 int digit_draw_location_x,
129 int digit_draw_location_y )
131 int string_length = 0;
132 int string_character_position = 0;
133 int string_character = 0;
134 int digit_new_draw_location_x = 0;
136 string_length = strlen( string_to_draw );
138 if ( ( ( DOCKAPP_WIDTH - DOCKAPP_BIG_CHAR_WIDTH ) < digit_draw_location_x
139 || ( DOCKAPP_HEIGHT - DOCKAPP_BIG_CHAR_HEIGHT ) < digit_draw_location_y )
140 || ( 0 > digit_draw_location_x || 0 > digit_draw_location_y ) )
142 fprintf( stderr,
143 "%s: Invalid x,y position: %d,%d\n",
144 __func__,
145 digit_draw_location_x,
146 digit_draw_location_y );
148 return;
150 else if ( 0 == string_length )
152 fprintf( stderr, "%s: Draw string is empty!\n", __func__ );
153 return;
155 else if ( ( DOCKAPP_WIDTH / DOCKAPP_BIG_CHAR_WIDTH ) < string_length )
157 fprintf( stderr, "%s: Draw string is too long!", __func__ );
158 return;
161 digit_new_draw_location_x = digit_draw_location_x;
163 for ( string_character_position = 0;
164 string_character_position < string_length;
165 ++string_character_position )
167 string_character = dockapp_utils_get_char( string_to_draw[string_character_position] );
169 copyXPMArea( string_character * DOCKAPP_BIG_CHAR_WIDTH + DOCKAPP_BIG_LETTER_X,
170 DOCKAPP_BIG_LETTER_Y,
171 DOCKAPP_BIG_CHAR_WIDTH,
172 DOCKAPP_BIG_CHAR_HEIGHT,
173 digit_new_draw_location_x,
174 digit_draw_location_y );
176 digit_new_draw_location_x += DOCKAPP_BIG_CHAR_WIDTH;
179 RedrawWindow();
182 void
183 dockapp_draw_small_str( const char* string_to_draw,
184 int digit_draw_location_x,
185 int digit_draw_location_y )
187 int string_length = 0;
188 int string_character_position = 0;
189 int string_character = 0;
190 int digit_new_draw_location_x = 0;
192 string_length = strlen( string_to_draw );
194 if ( ( ( DOCKAPP_WIDTH - DOCKAPP_BIG_CHAR_WIDTH ) < digit_draw_location_x
195 || ( DOCKAPP_HEIGHT - DOCKAPP_BIG_CHAR_HEIGHT ) < digit_draw_location_y )
196 || ( 0 > digit_draw_location_x || 0 > digit_draw_location_y ) )
198 fprintf( stderr,
199 "%s: Invalid x,y position: %d,%d\n",
200 __func__,
201 digit_draw_location_x,
202 digit_draw_location_y );
203 return;
205 else if ( 0 == string_length )
207 fprintf( stderr, "%s: Draw string is empty!\n", __func__ );
208 return;
210 else if ( ( DOCKAPP_WIDTH / DOCKAPP_SMALL_CHAR_WIDTH ) < string_length )
212 fprintf( stderr, "%s: Draw string is too long!", __func__ );
213 return;
216 digit_new_draw_location_x = digit_draw_location_x;
218 for (string_character_position = 0;
219 string_character_position < string_length;
220 ++string_character_position )
222 string_character = dockapp_utils_get_char( string_to_draw[string_character_position] );
224 copyXPMArea( string_character * DOCKAPP_SMALL_CHAR_WIDTH + DOCKAPP_SMALL_LETTER_X,
225 DOCKAPP_SMALL_LETTER_Y,
226 DOCKAPP_SMALL_CHAR_WIDTH,
227 DOCKAPP_SMALL_CHAR_HEIGHT,
228 digit_new_draw_location_x,
229 digit_draw_location_y );
231 digit_new_draw_location_x += DOCKAPP_SMALL_CHAR_WIDTH;
234 RedrawWindow();
237 #if 0
238 void
239 dockapp_draw_bar( int bar_draw_width,
240 int bar_draw_x,
241 int bar_draw_y,
242 int bar_x,
243 int bar_y )
245 if ( 0 > bar_draw_width || DOCKAPP_BAR_WIDTH < bar_draw_width )
247 fprintf( stderr, "%s: Invalid bar width!\n", __func__ );
248 return;
250 else if ( ( 0 > bar_draw_x || DOCKAPP_WIDTH < bar_draw_x ) ||
251 ( 0 > bar_draw_y || DOCKAPP_WIDTH < bar_draw_y ) )
253 fprintf( stderr,
254 "%s: Invalid x,y position: %d,%d\n",
255 __func__,
256 bar_draw_x,
257 bar_draw_y );
258 return;
260 else if ( 0 >= bar_x || 0 >= bar_y )
262 fprintf( stderr,
263 "%s: Invalid x,y position: %d,%d\n",
264 __func__,
265 bar_x,
266 bar_y );
267 return;
270 copyXPMArea( bar_x,
271 bar_y,
272 bar_draw_width,
273 DOCKAPP_BAR_HEIGHT,
274 bar_draw_x,
275 bar_draw_y );
277 RedrawWindow();
280 void
281 dockapp_draw_bar_calculate( float draw_size, int bar_draw_x, int bar_draw_y )
283 float draw_percent_f;
284 int draw_percent;
286 draw_percent_f = ( GET_HRS_F( draw_size ) / DOCKAPP_BAR_WIDTH ) * 100.0f;
287 draw_percent = ( int ) nearbyint( draw_percent_f );
289 if ( DOCKAPP_BAR_WIDTH == draw_percent )
291 dockapp_draw_bar( draw_percent,
292 bar_draw_x,
293 bar_draw_y,
294 DOCKAPP_BAR_OFF_X,
295 DOCKAPP_BAR_OFF_Y );
297 else
299 dockapp_draw_bar( draw_percent,
300 bar_draw_x,
301 bar_draw_y,
302 DOCKAPP_BAR_ON_X,
303 DOCKAPP_BAR_ON_Y );
306 #endif
308 void
309 dockapp_draw_data( void )
311 dockapp_proc = dockapp_utils_get_proc();
313 dockapp_draw_small_digit( dockapp_proc.users, 2, 45, 4 );
314 dockapp_draw_small_digit( dockapp_proc.total, 2, 45, 11 );
315 dockapp_draw_small_digit( dockapp_proc.running, 2, 45, 18 );
317 dockapp_draw_small_digit( dockapp_proc.hours, 1, 36, 28 );
318 dockapp_draw_small_digit( dockapp_proc.minutes, 1, 50, 28 );
320 dockapp_draw_small_digit( dockapp_proc.days, 0, 55, 37 );
321 dockapp_draw_small_digit( dockapp_proc.weeks, 2, 45, 44 );
323 dockapp_draw_small_digit( dockapp_proc.load[0], 1, 36, 53 );
324 dockapp_draw_small_digit( dockapp_proc.load[1], 1, 50, 53 );
326 RedrawWindow();