Browse Source

- implemented printw (formatted string output, a la printf)

- implemented hline/vline (part of wborder family)
- screen cursor now relocates at same time as window cursor when
  restoring after a non-wrapping function
tags/v0.9.3
Dan Lynch 18 years ago
parent
commit
bef8874842
2 changed files with 217 additions and 77 deletions
  1. 103
    7
      src/core/curses.c
  2. 114
    70
      src/include/curses.h

+ 103
- 7
src/core/curses.c View File

@@ -103,6 +103,7 @@ static void _wputstr ( WINDOW *win, const char *str, int wrap, int n ) {
103 103
 static void _restore_curs_pos ( WINDOW *win, struct cursor_pos *pos ){
104 104
 	win->curs_y = pos->y;
105 105
 	win->curs_x = pos->x;
106
+	win->scr->movetoyx ( win->scr, win->curs_y, win->curs_x );
106 107
 }
107 108
 
108 109
 /**
@@ -285,6 +286,37 @@ WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
285 286
 	return win;
286 287
 }
287 288
 
289
+
290
+struct printw_context {
291
+	struct printf_context ctx;
292
+	WINDOW *win;
293
+};
294
+
295
+static void _printw_handler ( struct printf_context *ctx, unsigned int c ) {
296
+	struct printw_context *wctx =
297
+		container_of ( ctx, struct printw_context, ctx );
298
+
299
+	_wputch( wctx->win, c | wctx->win->attrs, WRAP );
300
+}
301
+
302
+/**
303
+ * Print formatted output in a window
304
+ *
305
+ * @v *win	subject window
306
+ * @v *fmt	formatted string
307
+ * @v varglist	argument list
308
+ * @ret rc	return status code
309
+ */
310
+int vw_printw ( WINDOW *win, const char *fmt, va_list varglist ) {
311
+	struct printw_context wctx = {
312
+		.win = win,
313
+		.ctx = { .handler = _printw_handler, },
314
+	};
315
+
316
+	vcprintf ( &(wctx.ctx), fmt, varglist );
317
+	return OK;
318
+}
319
+
288 320
 /**
289 321
  * Add a single-byte character and rendition to a window and advance
290 322
  * the cursor
@@ -373,7 +405,8 @@ int wattrset ( WINDOW *win, int attrs ) {
373 405
  * @v *opts	undefined (for future implementation)
374 406
  * @ret rc	return status cude
375 407
  */
376
-int wattr_get ( WINDOW *win, attr_t *attrs, short *pair, void *opts ) {
408
+int wattr_get ( WINDOW *win, attr_t *attrs, short *pair, 
409
+		void *opts __unused ) {
377 410
 	*attrs = win->attrs & A_ATTRIBUTES;
378 411
 	*pair = (short)(( win->attrs & A_COLOR ) >> CPAIR_SHIFT);
379 412
 	return OK;
@@ -387,7 +420,8 @@ int wattr_get ( WINDOW *win, attr_t *attrs, short *pair, void *opts ) {
387 420
  * @v *opts	undefined (for future implementation)
388 421
  * @ret rc	return status code
389 422
  */
390
-int wattr_off ( WINDOW *win, attr_t attrs, void *opts ) {
423
+int wattr_off ( WINDOW *win, attr_t attrs, 
424
+		void *opts __unused ) {
391 425
 	wattroff( win, attrs );
392 426
 	return OK;
393 427
 }
@@ -400,7 +434,8 @@ int wattr_off ( WINDOW *win, attr_t attrs, void *opts ) {
400 434
  * @v *opts	undefined (for future implementation)
401 435
  * @ret rc	return status code
402 436
  */
403
-int wattr_on ( WINDOW *win, attr_t attrs, void *opts ) {
437
+int wattr_on ( WINDOW *win, attr_t attrs, 
438
+	       void *opts __unused ) {
404 439
 	wattron( win, attrs );
405 440
 	return OK;
406 441
 }
@@ -414,7 +449,8 @@ int wattr_on ( WINDOW *win, attr_t attrs, void *opts ) {
414 449
  * @v *opts	undefined (for future implementation)
415 450
  * @ret rc	return status code
416 451
  */
417
-int wattr_set ( WINDOW *win, attr_t attrs, short cpair, void *opts ) {
452
+int wattr_set ( WINDOW *win, attr_t attrs, short cpair, 
453
+		void *opts __unused ) {
418 454
 	wattrset( win, attrs | ( ( (unsigned short)cpair ) << CPAIR_SHIFT ) );
419 455
 	return OK;
420 456
 }
@@ -472,9 +508,9 @@ int wclrtobot ( WINDOW *win ) {
472 508
 	struct cursor_pos pos;
473 509
 
474 510
 	_store_curs_pos( win, &pos );
475
-	while ( win->curs_y + win->curs_x ) {
511
+	do {
476 512
 		_wputch( win, (unsigned)' ', WRAP );
477
-	}
513
+	} while ( win->curs_y + win->curs_x );
478 514
 	_restore_curs_pos( win, &pos );
479 515
 
480 516
 	return OK;
@@ -506,7 +542,8 @@ int wclrtoeol ( WINDOW *win ) {
506 542
  * @v *opts			undefined (for future implementation)
507 543
  * @ret rc			return status code
508 544
  */
509
-int wcolour_set ( WINDOW *win, short colour_pair_number, void *opts ) {
545
+int wcolour_set ( WINDOW *win, short colour_pair_number, 
546
+		  void *opts __unused ) {
510 547
 	if ( ( unsigned short )colour_pair_number > COLORS )
511 548
 		return ERR;
512 549
 
@@ -544,3 +581,62 @@ int wdeleteln ( WINDOW *win ) {
544 581
 	wclrtoeol( win );
545 582
 	return OK;
546 583
 }
584
+
585
+/**
586
+ * Create a horizontal line in a window
587
+ *
588
+ * @v *win	subject window
589
+ * @v ch	rendition and character
590
+ * @v n		max number of chars (wide) to render
591
+ * @ret rc	return status code
592
+ */
593
+int whline ( WINDOW *win, chtype ch, int n ) {
594
+	struct cursor_pos pos;
595
+
596
+	_store_curs_pos ( win, &pos );
597
+	while ( ( win->curs_x - win->width ) && n-- ) {
598
+		_wputch ( win, ch, NOWRAP );
599
+	}
600
+	_restore_curs_pos ( win, &pos );
601
+
602
+	return OK;
603
+}
604
+
605
+/**
606
+ * Print formatted output to a window
607
+ *
608
+ * @v *win	subject window
609
+ * @v *fmt	formatted string
610
+ * @v ...	string arguments
611
+ * @ret rc	return status code
612
+ */
613
+int wprintw ( WINDOW *win, const char *fmt, ... ) {
614
+	va_list args;
615
+	int i;
616
+
617
+	va_start ( args, fmt );
618
+	i = vw_printw ( win, fmt, args );
619
+	va_end ( args );
620
+	return i;
621
+}
622
+
623
+/**
624
+ * Create a vertical line in a window
625
+ *
626
+ * @v *win	subject window
627
+ * @v ch	rendition and character
628
+ * @v n		max number of lines to render
629
+ * @ret rc	return status code
630
+ */
631
+int wvline ( WINDOW *win, chtype ch, int n ) {
632
+	struct cursor_pos pos;
633
+
634
+	_store_curs_pos ( win, &pos );
635
+	while ( ( win->curs_y - win->height ) && n-- ) {
636
+		_wputch ( win, ch, NOWRAP );
637
+		wmove( win, ++(win->curs_y), pos.x);
638
+	}
639
+	_restore_curs_pos ( win, &pos );
640
+
641
+	return OK;
642
+}

+ 114
- 70
src/include/curses.h View File

@@ -293,7 +293,7 @@ extern int def_shell_mode ( void );
293 293
 extern int delay_output ( int );
294 294
 /*extern int delch ( void );*/
295 295
 /*extern int deleteln ( void );*/
296
-extern void delscreen ( SCREEN * ); 
296
+extern void delscreen ( SCREEN * );
297 297
 extern int delwin ( WINDOW * );
298 298
 extern WINDOW *derwin ( WINDOW *, int, int, int, int );
299 299
 /*extern int doupdate ( void );*/
@@ -314,21 +314,21 @@ extern int halfdelay ( int );
314 314
 extern bool has_colors ( void );
315 315
 extern bool has_ic ( void );
316 316
 extern bool has_il ( void );
317
-extern int hline ( chtype, int );
317
+//extern int hline ( chtype, int );
318 318
 extern void idcok ( WINDOW *, bool );
319 319
 extern int idlok ( WINDOW *, bool );
320 320
 extern void immedok ( WINDOW *, bool );
321
-extern chtype inch ( void );
322
-extern int inchnstr ( chtype *, int );
323
-extern int inchstr ( chtype * );
321
+/*extern chtype inch ( void );*/
322
+/*extern int inchnstr ( chtype *, int );*/
323
+/*extern int inchstr ( chtype * );*/
324 324
 extern WINDOW *initscr ( void );
325 325
 extern int init_color ( short, short, short, short );
326 326
 extern int init_pair ( short, short, short );
327
-extern int innstr ( char *, int );
328
-extern int insch ( chtype );
329
-extern int insnstr ( const char *, int );
330
-extern int insstr ( const char * );
331
-extern int instr ( char * );
327
+//extern int innstr ( char *, int );
328
+//extern int insch ( chtype );
329
+//extern int insnstr ( const char *, int );
330
+//extern int insstr ( const char * );
331
+//extern int instr ( char * );
332 332
 extern int intrflush ( WINDOW *, bool );
333 333
 extern bool isendwin ( void );
334 334
 extern bool is_linetouched ( WINDOW *, int );
@@ -351,18 +351,18 @@ extern int mvderwin ( WINDOW *, int, int );
351 351
 extern int mvgetch ( int, int );
352 352
 extern int mvgetnstr ( int, int, char *, int );
353 353
 extern int mvgetstr ( int, int, char * );
354
-extern int mvhline ( int, int, chtype, int );
355
-extern chtype mvinch ( int, int );
356
-extern int mvinchnstr ( int, int, chtype *, int );
357
-extern int mvinchstr ( int, int, chtype * );
358
-extern int mvinnstr ( int, int, char *, int );
359
-extern int mvinsch ( int, int, chtype );
360
-extern int mvinsnstr ( int, int, const char *, int );
361
-extern int mvinsstr ( int, int, const char * );
362
-extern int mvinstr ( int, int, char * );
363
-extern int mvprintw ( int, int, char *,  ... );
354
+//extern int mvhline ( int, int, chtype, int );
355
+//extern chtype mvinch ( int, int );
356
+/*extern int mvinchnstr ( int, int, chtype *, int );*/
357
+/*extern int mvinchstr ( int, int, chtype * );*/
358
+/*extern int mvinnstr ( int, int, char *, int );*/
359
+//extern int mvinsch ( int, int, chtype );
360
+//extern int mvinsnstr ( int, int, const char *, int );
361
+//extern int mvinsstr ( int, int, const char * );
362
+//extern int mvinstr ( int, int, char * );
363
+//extern int mvprintw ( int, int, char *,  ... );
364 364
 extern int mvscanw ( int, int, char *, ... );
365
-extern int mvvline ( int, int, chtype, int );
365
+//extern int mvvline ( int, int, chtype, int );
366 366
 /*extern int mvwaddch ( WINDOW *, int, int, const chtype );*/
367 367
 /*extern int mvwaddchnstr ( WINDOW *, int, int, const chtype *, int );*/
368 368
 /*extern int mvwaddchstr ( WINDOW *, int, int, const chtype * );*/
@@ -372,19 +372,19 @@ extern int mvvline ( int, int, chtype, int );
372 372
 extern int mvwgetch ( WINDOW *, int, int );
373 373
 extern int mvwgetnstr ( WINDOW *, int, int, char *, int );
374 374
 extern int mvwgetstr ( WINDOW *, int, int, char * );
375
-extern int mvwhline ( WINDOW *, int, int, chtype, int );
375
+//extern int mvwhline ( WINDOW *, int, int, chtype, int );
376 376
 extern int mvwin ( WINDOW *, int, int );
377
-extern chtype mvwinch ( WINDOW *, int, int );
378
-extern int mvwinchnstr ( WINDOW *, int, int, chtype *, int );
379
-extern int mvwinchstr ( WINDOW *, int, int, chtype * );
380
-extern int mvwinnstr ( WINDOW *, int, int, char *, int );
381
-extern int mvwinsch ( WINDOW *, int, int, chtype );
382
-extern int mvwinsnstr ( WINDOW *, int, int, const char *, int );
383
-extern int mvwinsstr ( WINDOW *, int, int, const char * );
384
-extern int mvwinstr ( WINDOW *, int, int, char * );
385
-extern int mvwprintw ( WINDOW *, int, int, char *, ... );
377
+//extern chtype mvwinch ( WINDOW *, int, int );
378
+//extern int mvwinchnstr ( WINDOW *, int, int, chtype *, int );
379
+//extern int mvwinchstr ( WINDOW *, int, int, chtype * );
380
+//extern int mvwinnstr ( WINDOW *, int, int, char *, int );
381
+//extern int mvwinsch ( WINDOW *, int, int, chtype );
382
+//extern int mvwinsnstr ( WINDOW *, int, int, const char *, int );
383
+//extern int mvwinsstr ( WINDOW *, int, int, const char * );
384
+//extern int mvwinstr ( WINDOW *, int, int, char * );
385
+//extern int mvwprintw ( WINDOW *, int, int, char *, ... );
386 386
 extern int mvwscanw ( WINDOW *, int, int, char *, ... );
387
-extern int mvwvline ( WINDOW *, int, int, chtype, int );
387
+//extern int mvwvline ( WINDOW *, int, int, chtype, int );
388 388
 extern int napms ( int );
389 389
 extern WINDOW *newpad ( int, int );
390 390
 extern WINDOW *newwin ( int, int, int, int );
@@ -461,11 +461,11 @@ extern int vid_attr ( attr_t, short, void * );
461 461
 extern int vidattr ( chtype );
462 462
 extern int vid_puts ( attr_t, short, void *, int  ( *) ( int) );
463 463
 extern int vidputs ( chtype, int  ( *) ( int) );
464
-extern int vline ( chtype, int );
465
-extern int vwprintw ( WINDOW *, char *, va_list * );
466
-extern int vw_printw ( WINDOW *, char *, va_list * );
467
-extern int vwscanw ( WINDOW *, char *, va_list * );
468
-extern int vw_scanw ( WINDOW *, char *, va_list * );
464
+//extern int vline ( chtype, int );
465
+//extern int vwprintw ( WINDOW *, const char *, va_list );
466
+extern int vw_printw ( WINDOW *, const char *, va_list );
467
+extern int vwscanw ( WINDOW *, char *, va_list );
468
+extern int vw_scanw ( WINDOW *, char *, va_list );
469 469
 extern int waddch ( WINDOW *, const chtype );
470 470
 extern int waddchnstr ( WINDOW *, const chtype *, int );
471 471
 /*extern int waddchstr ( WINDOW *, const chtype * );*/
@@ -494,17 +494,17 @@ extern int wgetch ( WINDOW * );
494 494
 extern int wgetnstr ( WINDOW *, char *, int );
495 495
 extern int wgetstr ( WINDOW *, char * );
496 496
 extern int whline ( WINDOW *, chtype, int );
497
-extern chtype winch ( WINDOW * );
498
-extern int winchnstr ( WINDOW *, chtype *, int );
499
-extern int winchstr ( WINDOW *, chtype * );
500
-extern int winnstr ( WINDOW *, char *, int );
501
-extern int winsch ( WINDOW *, chtype );
502
-extern int winsnstr ( WINDOW *, const char *, int );
503
-extern int winsstr ( WINDOW *, const char * );
504
-extern int winstr ( WINDOW *, char * );
497
+//extern chtype winch ( WINDOW * );
498
+//extern int winchnstr ( WINDOW *, chtype *, int );
499
+//extern int winchstr ( WINDOW *, chtype * );
500
+//extern int winnstr ( WINDOW *, char *, int );
501
+//extern int winsch ( WINDOW *, chtype );
502
+//extern int winsnstr ( WINDOW *, const char *, int );
503
+//extern int winsstr ( WINDOW *, const char * );
504
+//extern int winstr ( WINDOW *, char * );
505 505
 extern int wmove ( WINDOW *, int, int );
506 506
 extern int wnoutrefresh ( WINDOW * );
507
-extern int wprintw ( WINDOW *, char *, ... );
507
+extern int wprintw ( WINDOW *, const char *, ... );
508 508
 /*extern int wredrawln ( WINDOW *, int, int );*/
509 509
 /*extern int wrefresh ( WINDOW * );*/
510 510
 extern int wscanw ( WINDOW *, char *, ... );
@@ -582,68 +582,112 @@ static inline int deleteln ( void ) {
582 582
 	return wdeleteln( stdscr );
583 583
 }
584 584
 
585
+static inline int hline ( chtype ch, int n ) {
586
+	return whline ( stdscr, ch, n );
587
+}
588
+
585 589
 static inline int move ( int y, int x ) {
586 590
 	return wmove ( stdscr, y, x );
587 591
 }
588 592
 
589 593
 static inline int mvaddch ( int y, int x, const chtype ch ) {
590
-	return ( wmove ( stdscr, y, x ) == ERR 
591
-		 ? ERR : waddch( stdscr, ch ) );
594
+	return ( wmove ( stdscr, y, x ) == OK
595
+		 ? waddch( stdscr, ch ) : ERR );
592 596
 }
593 597
 
594 598
 static inline int mvaddchnstr ( int y, int x, const chtype *chstr, int n ) {
595
-	return ( wmove ( stdscr, y, x ) == ERR
596
-		 ? ERR : waddchnstr ( stdscr, chstr, n ) );
599
+	return ( wmove ( stdscr, y, x ) == OK
600
+		 ? waddchnstr ( stdscr, chstr, n ) : ERR );
597 601
 }
598 602
 
599 603
 static inline int mvaddchstr ( int y, int x, const chtype *chstr ) {
600
-	return ( wmove ( stdscr, y, x ) == ERR
601
-		 ? ERR : waddchnstr ( stdscr, chstr, -1 ) );
604
+	return ( wmove ( stdscr, y, x ) == OK
605
+		 ? waddchnstr ( stdscr, chstr, -1 ) : ERR );
602 606
 }
603 607
 
604 608
 static inline int mvaddnstr ( int y, int x, const char *str, int n ) {
605
-	return ( wmove ( stdscr, y, x ) == ERR
606
-		 ? ERR : waddnstr ( stdscr, str, n ) );
609
+	return ( wmove ( stdscr, y, x ) == OK
610
+		 ? waddnstr ( stdscr, str, n ) : ERR );
607 611
 }
608 612
 
609 613
 static inline int mvaddstr ( int y, int x, const char *str ) {
610
-	return ( wmove ( stdscr, y, x ) == ERR
611
-		 ? ERR : waddnstr ( stdscr, str, -1 ) );
614
+	return ( wmove ( stdscr, y, x ) == OK
615
+		 ? waddnstr ( stdscr, str, -1 ) : ERR );
612 616
 }
613 617
 
614 618
 static inline int mvdelch ( int y, int x ) {
615
-	return ( wmove ( stdscr, y, x ) == ERR
616
-		 ? ERR : wdelch ( stdscr ) );
619
+	return ( wmove ( stdscr, y, x ) == OK
620
+		 ? wdelch ( stdscr ) : ERR );
621
+}
622
+
623
+static inline int mvhline ( int y, int x, chtype ch, int n ) {
624
+	return ( wmove ( stdscr, y, x ) == OK
625
+		 ? whline ( stdscr, ch, n ) : ERR );
626
+}
627
+
628
+// OK, so maybe a few I did with macros...
629
+#define mvprintw( y, x, fmt, ... ) \
630
+	( wmove(stdscr,(y),(x)) == OK \
631
+	  ? wprintw(stdscr,(fmt), ## __VA_ARGS__ : ERR )
632
+
633
+static inline int mvvline ( int y, int x, chtype ch, int n ) {
634
+	return ( wmove ( stdscr, y, x ) == OK
635
+		 ? wvline ( stdscr, ch, n ) : ERR );
617 636
 }
618 637
 
619 638
 static inline int mvwaddch ( WINDOW *win, int y, int x, const chtype ch ) {
620
-	return ( wmove( win, y, x ) == ERR 
621
-		 ? ERR : waddch ( win, ch ) );
639
+	return ( wmove( win, y, x ) == OK
640
+		 ? waddch ( win, ch ) : ERR );
622 641
 }
623 642
 
624 643
 static inline int mvwaddchnstr ( WINDOW *win, int y, int x, const chtype *chstr, int n ) {
625
-	return ( wmove ( win, y, x ) == ERR 
626
-		 ? ERR : waddchnstr ( win, chstr, n ) );
644
+	return ( wmove ( win, y, x ) == OK
645
+		 ? waddchnstr ( win, chstr, n ) : ERR );
627 646
 }
628 647
 
629 648
 static inline int mvwaddchstr ( WINDOW *win, int y, int x, const chtype *chstr ) {
630
-	return ( wmove ( win, y, x ) == ERR 
631
-		 ? ERR : waddchnstr ( win, chstr, -1 ) );
649
+	return ( wmove ( win, y, x ) == OK
650
+		 ? waddchnstr ( win, chstr, -1 ) : ERR );
632 651
 }
633 652
 
634 653
 static inline int mvwaddnstr ( WINDOW *win, int y, int x, const char *str, int n ) {
635
-	return ( wmove ( win, y, x ) == ERR
636
-		 ? ERR : waddnstr ( win, str, n ) );
654
+	return ( wmove ( win, y, x ) == OK
655
+		 ? waddnstr ( win, str, n ) : ERR );
637 656
 }
638 657
 
639 658
 static inline int mvwaddstr ( WINDOW *win, int y, int x, const char *str ) {
640
-	return ( wmove ( win, y, x ) == ERR
641
-		 ? ERR : waddnstr ( win, str, -1 ) );
659
+	return ( wmove ( win, y, x ) == OK
660
+		 ? waddnstr ( win, str, -1 ) : ERR );
642 661
 }
643 662
 
644 663
 static inline int mvwdelch ( WINDOW *win, int y, int x ) {
645
-	return ( wmove ( win, y, x ) == ERR
646
-		 ? ERR : wdelch ( win ) );
664
+	return ( wmove ( win, y, x ) == OK
665
+		 ? wdelch ( win ) : ERR );
666
+}
667
+
668
+static inline int mvwhline ( WINDOW *win, int y, int x, chtype ch, int n ) {
669
+	return ( wmove ( win, y, x ) == OK
670
+		 ? whline ( win, ch, n ) : ERR );
671
+}
672
+
673
+#define mvwprintw( win, y, x, fmt, ... ) \
674
+	( wmove((win),(y),(x)) == OK \
675
+	  ? wprintw((win),(fmt), ## __VA_ARGS__) : ERR )
676
+
677
+static inline int mvwvline ( WINDOW *win, int y, int x, chtype ch, int n ) {
678
+	return ( wmove ( win, y, x ) == OK
679
+		 ? wvline ( win, ch, n ) : ERR );
680
+}
681
+
682
+#define printw( fmt, ... ) wprintw(stdscr,(fmt), ## __VA_ARGS__ )
683
+
684
+static inline int vline ( chtype ch, int n ) {
685
+	return wvline ( stdscr, ch, n );
686
+}
687
+
688
+// marked for removal
689
+static inline int vwprintw ( WINDOW *win, const char *fmt, va_list varglist ) {
690
+	return vw_printw ( win, fmt, varglist );
647 691
 }
648 692
 
649 693
 static inline int waddchstr ( WINDOW *win, const chtype *chstr ) {

Loading…
Cancel
Save