瀏覽代碼

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

- some minor preventatives
tags/v0.9.3
Dan Lynch 18 年之前
父節點
當前提交
890dc758e8
共有 1 個文件被更改,包括 68 次插入15 次删除
  1. 68
    15
      src/hci/mucurses/slk.c

+ 68
- 15
src/hci/mucurses/slk.c 查看文件

@@ -23,7 +23,7 @@ struct _softlabel {
23 23
 };
24 24
 
25 25
 struct _softlabelkeys {
26
-	struct _softlabel fkeys[12];
26
+	struct _softlabel *fkeys;
27 27
 	attr_t attrs;
28 28
 	/* Soft label layout format
29 29
 	   0: 3-2-3
@@ -41,6 +41,8 @@ struct _softlabelkeys {
41 41
 
42 42
 struct _softlabelkeys *slks;
43 43
 
44
+static unsigned short pos_x;
45
+
44 46
 /*
45 47
   I either need to break the primitives here, or write a collection of
46 48
   functions specifically for SLKs that directly access the screen
@@ -52,6 +54,43 @@ static void _movetoslk ( void ) {
52 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 95
  * Return the attribute used for the soft function keys
57 96
  *
@@ -149,9 +188,18 @@ int slk_attr_set ( const attr_t attrs, short colour_pair_number,
149 188
  * @ret rc	return status code
150 189
  */
151 190
 int slk_clear ( void ) {
191
+	chtype space_ch;
152 192
 	if ( slks == NULL )
153 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,8 +259,11 @@ int slk_init ( int fmt ) {
211 259
 	// determine maximum label length and major space size
212 260
 	available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
213 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 264
 	slks->num_spaces = nmaj;
265
+	slks->num_labels = nblocks;
266
+	slks->fkeys = calloc( nblocks, sizeof(struct _softlabel) );
216 267
 
217 268
 	// strip a line from the screen
218 269
 	LINES -= 1;
@@ -242,28 +293,31 @@ int slk_restore ( void ) {
242 293
 	unsigned short i, j,
243 294
 		*next_space, *last_space;
244 295
 	chtype space_ch;
245
-	char c;
246 296
 
247
-	if ( slks == NULL ) 
297
+	if ( slks == NULL )
248 298
 		return ERR;
249 299
 
300
+	pos_x = 0;
301
+
250 302
 	_movetoslk();
251 303
 
252
-	space_ch = (int)' ' | slks->attrs;
304
+	space_ch = (chtype)' ' | slks->attrs;
253 305
 	next_space = &(slks->spaces[0]);
254 306
 	last_space = &(slks->spaces[slks->num_spaces-1]);
255 307
 
256 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 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 314
 				stdscr->scr->putc( stdscr->scr, space_ch );
263 315
 			if ( next_space < last_space )
264 316
 				next_space++;
265 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,14 +335,13 @@ int slk_restore ( void ) {
281 335
 int slk_set ( int labnum, const char *label, int fmt ) {
282 336
 	if ( slks == NULL ) 
283 337
 		return ERR;
284
-	if ( (unsigned short)labnum > 12 )
338
+	if ( (unsigned short)labnum >= slks->num_labels )
285 339
 		return ERR;
286 340
 	if ( (unsigned short)fmt >= 3 )
287 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 345
 	slks->fkeys[labnum].fmt = fmt;
293 346
 
294 347
 	return OK;

Loading…
取消
儲存