Browse Source

- proper layout formatted soft label array, with justification formatted labels

- some minor preventatives
tags/v0.9.3
Dan Lynch 18 years ago
parent
commit
890dc758e8
1 changed files with 68 additions and 15 deletions
  1. 68
    15
      src/hci/mucurses/slk.c

+ 68
- 15
src/hci/mucurses/slk.c View File

23
 };
23
 };
24
 
24
 
25
 struct _softlabelkeys {
25
 struct _softlabelkeys {
26
-	struct _softlabel fkeys[12];
26
+	struct _softlabel *fkeys;
27
 	attr_t attrs;
27
 	attr_t attrs;
28
 	/* Soft label layout format
28
 	/* Soft label layout format
29
 	   0: 3-2-3
29
 	   0: 3-2-3
41
 
41
 
42
 struct _softlabelkeys *slks;
42
 struct _softlabelkeys *slks;
43
 
43
 
44
+static unsigned short pos_x;
45
+
44
 /*
46
 /*
45
   I either need to break the primitives here, or write a collection of
47
   I either need to break the primitives here, or write a collection of
46
   functions specifically for SLKs that directly access the screen
48
   functions specifically for SLKs that directly access the screen
52
 	stdscr->scr->movetoyx( stdscr->scr, LINES, 0 );
54
 	stdscr->scr->movetoyx( stdscr->scr, LINES, 0 );
53
 }
55
 }
54
 
56
 
57
+static void _print_label ( struct _softlabel sl ) {
58
+	unsigned short i = 0;
59
+	int space_ch;
60
+	char *str = malloc((size_t)slks->max_label_len);
61
+
62
+	space_ch = ' ';
63
+
64
+	// protect against gaps in the soft label keys array
65
+	if ( sl.label == NULL ) {
66
+		memset( str, space_ch, (size_t)(slks->max_label_len) );
67
+	} else {
68
+		/* we need to pad the label with varying amounts of leading
69
+		   pad depending on the format of the label */
70
+		if ( sl.fmt == 1 ) {
71
+			memset( str, space_ch, 
72
+				(size_t)(slks->max_label_len 
73
+					 - strlen(sl.label)) / 2 );
74
+		}
75
+		if ( sl.fmt == 2 ) {
76
+			memset( str, space_ch,
77
+				(size_t)(slks->max_label_len 
78
+					 - strlen(sl.label)) );
79
+		}
80
+		strcat(str,sl.label);
81
+		
82
+		// post-padding
83
+		memset(str+strlen(str), space_ch,
84
+		       (size_t)(slks->max_label_len - strlen(str)) );
85
+		str[slks->max_label_len] = '\0';
86
+	}
87
+
88
+	// print the formatted label
89
+	for ( ; i < slks->max_label_len; i++ ) {
90
+		stdscr->scr->putc( stdscr->scr, (chtype)str[i] | slks->attrs );
91
+	}
92
+}
93
+
55
 /**
94
 /**
56
  * Return the attribute used for the soft function keys
95
  * Return the attribute used for the soft function keys
57
  *
96
  *
149
  * @ret rc	return status code
188
  * @ret rc	return status code
150
  */
189
  */
151
 int slk_clear ( void ) {
190
 int slk_clear ( void ) {
191
+	chtype space_ch;
152
 	if ( slks == NULL )
192
 	if ( slks == NULL )
153
 		return ERR;
193
 		return ERR;
154
-	return 0;
194
+
195
+	_movetoslk();
196
+	pos_x = 0;
197
+	space_ch = (chtype)' ' | slks->attrs;
198
+
199
+	for ( ; pos_x < COLS; pos_x++ )
200
+		stdscr->scr->putc( stdscr->scr, space_ch );
201
+
202
+	return OK;
155
 }
203
 }
156
 
204
 
157
 /**
205
 /**
211
 	// determine maximum label length and major space size
259
 	// determine maximum label length and major space size
212
 	available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
260
 	available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
213
 	slks->max_label_len = available_width / nblocks;
261
 	slks->max_label_len = available_width / nblocks;
214
-	slks->maj_space_len = ( available_width % nblocks ) / nmaj;
262
+	slks->maj_space_len = MIN_SPACE_SIZE + 
263
+		( available_width % nblocks ) / nmaj;
215
 	slks->num_spaces = nmaj;
264
 	slks->num_spaces = nmaj;
265
+	slks->num_labels = nblocks;
266
+	slks->fkeys = calloc( nblocks, sizeof(struct _softlabel) );
216
 
267
 
217
 	// strip a line from the screen
268
 	// strip a line from the screen
218
 	LINES -= 1;
269
 	LINES -= 1;
242
 	unsigned short i, j,
293
 	unsigned short i, j,
243
 		*next_space, *last_space;
294
 		*next_space, *last_space;
244
 	chtype space_ch;
295
 	chtype space_ch;
245
-	char c;
246
 
296
 
247
-	if ( slks == NULL ) 
297
+	if ( slks == NULL )
248
 		return ERR;
298
 		return ERR;
249
 
299
 
300
+	pos_x = 0;
301
+
250
 	_movetoslk();
302
 	_movetoslk();
251
 
303
 
252
-	space_ch = (int)' ' | slks->attrs;
304
+	space_ch = (chtype)' ' | slks->attrs;
253
 	next_space = &(slks->spaces[0]);
305
 	next_space = &(slks->spaces[0]);
254
 	last_space = &(slks->spaces[slks->num_spaces-1]);
306
 	last_space = &(slks->spaces[slks->num_spaces-1]);
255
 
307
 
256
 	for ( i = 0; i < slks->num_labels ; i++ ) {
308
 	for ( i = 0; i < slks->num_labels ; i++ ) {
257
-		while ( ( c = *(slks->fkeys[i].label++) ) != '\0' ) {
258
-			stdscr->scr->putc( stdscr->scr, (int)c | slks->attrs );
259
-		}
309
+		_print_label( slks->fkeys[i] );
310
+		pos_x += slks->max_label_len;
311
+
260
 		if ( i == *next_space ) {
312
 		if ( i == *next_space ) {
261
-			for ( j = 0; j < slks->maj_space_len; j++ )
313
+			for ( j = 0; j < slks->maj_space_len; j++, pos_x++ )
262
 				stdscr->scr->putc( stdscr->scr, space_ch );
314
 				stdscr->scr->putc( stdscr->scr, space_ch );
263
 			if ( next_space < last_space )
315
 			if ( next_space < last_space )
264
 				next_space++;
316
 				next_space++;
265
 		} else {
317
 		} else {
266
-			stdscr->scr->putc( stdscr->scr, space_ch );
318
+			if ( pos_x < COLS )
319
+				stdscr->scr->putc( stdscr->scr, space_ch );
320
+			pos_x++;
267
 		}
321
 		}
268
 	}
322
 	}
269
 
323
 
281
 int slk_set ( int labnum, const char *label, int fmt ) {
335
 int slk_set ( int labnum, const char *label, int fmt ) {
282
 	if ( slks == NULL ) 
336
 	if ( slks == NULL ) 
283
 		return ERR;
337
 		return ERR;
284
-	if ( (unsigned short)labnum > 12 )
338
+	if ( (unsigned short)labnum >= slks->num_labels )
285
 		return ERR;
339
 		return ERR;
286
 	if ( (unsigned short)fmt >= 3 )
340
 	if ( (unsigned short)fmt >= 3 )
287
 		return ERR;
341
 		return ERR;
288
-	if ( strlen(label) > slks->max_label_len )
289
-		return ERR;
290
 
342
 
291
-	strcpy( slks->fkeys[labnum].label, label );
343
+	slks->fkeys[labnum].label = malloc((size_t)slks->max_label_len + 1);
344
+	strncpy(slks->fkeys[labnum].label, label, (size_t)slks->max_label_len);
292
 	slks->fkeys[labnum].fmt = fmt;
345
 	slks->fkeys[labnum].fmt = fmt;
293
 
346
 
294
 	return OK;
347
 	return OK;

Loading…
Cancel
Save