@@ -1321,7 +1321,11 @@ curses_init_pair(VALUE obj, VALUE pair, VALUE f, VALUE b)
13211321{
13221322 /* may have to raise exception on ERR */
13231323 curses_stdscr ();
1324+ #ifdef HAVE_INIT_EXTENDED_PAIR
1325+ return (init_extended_pair (NUM2INT (pair ), NUM2INT (f ), NUM2INT (b )) == OK ) ? Qtrue : Qfalse ;
1326+ #else
13241327 return (init_pair (NUM2INT (pair ),NUM2INT (f ),NUM2INT (b )) == OK ) ? Qtrue : Qfalse ;
1328+ #endif
13251329}
13261330
13271331/*
@@ -1345,8 +1349,13 @@ curses_init_color(VALUE obj, VALUE color, VALUE r, VALUE g, VALUE b)
13451349{
13461350 /* may have to raise exception on ERR */
13471351 curses_stdscr ();
1352+ #ifdef HAVE_INIT_EXTENDED_COLOR
1353+ return (init_extended_color (NUM2INT (color ), NUM2INT (r ),
1354+ NUM2INT (g ), NUM2INT (b )) == OK ) ? Qtrue : Qfalse ;
1355+ #else
13481356 return (init_color (NUM2INT (color ),NUM2INT (r ),
13491357 NUM2INT (g ),NUM2INT (b )) == OK ) ? Qtrue : Qfalse ;
1358+ #endif
13501359}
13511360
13521361/*
@@ -1397,11 +1406,22 @@ curses_colors(VALUE obj)
13971406static VALUE
13981407curses_color_content (VALUE obj , VALUE color )
13991408{
1400- short r ,g ,b ;
1401-
14021409 curses_stdscr ();
1403- color_content (NUM2INT (color ),& r ,& g ,& b );
1404- return rb_ary_new3 (3 ,INT2FIX (r ),INT2FIX (g ),INT2FIX (b ));
1410+ #ifdef HAVE_EXTENDED_COLOR_CONTENT
1411+ {
1412+ int r , g , b ;
1413+ if (extended_color_content (NUM2INT (color ), & r , & g , & b ) == ERR )
1414+ return Qnil ;
1415+ return rb_ary_new3 (3 , INT2FIX (r ), INT2FIX (g ), INT2FIX (b ));
1416+ }
1417+ #else
1418+ {
1419+ short r , g , b ;
1420+ if (color_content (NUM2INT (color ), & r , & g , & b ) == ERR )
1421+ return Qnil ;
1422+ return rb_ary_new3 (3 , INT2FIX (r ), INT2FIX (g ), INT2FIX (b ));
1423+ }
1424+ #endif
14051425}
14061426
14071427
@@ -1430,11 +1450,22 @@ curses_color_pairs(VALUE obj)
14301450static VALUE
14311451curses_pair_content (VALUE obj , VALUE pair )
14321452{
1433- short f ,b ;
1434-
14351453 curses_stdscr ();
1436- pair_content (NUM2INT (pair ),& f ,& b );
1437- return rb_ary_new3 (2 ,INT2FIX (f ),INT2FIX (b ));
1454+ #ifdef HAVE_EXTENDED_PAIR_CONTENT
1455+ {
1456+ int f , b ;
1457+ if (extended_pair_content (NUM2INT (pair ), & f , & b ) == ERR )
1458+ return Qnil ;
1459+ return rb_ary_new3 (2 , INT2FIX (f ), INT2FIX (b ));
1460+ }
1461+ #else
1462+ {
1463+ short f , b ;
1464+ if (pair_content (NUM2INT (pair ), & f , & b ) == ERR )
1465+ return Qnil ;
1466+ return rb_ary_new3 (2 , INT2FIX (f ), INT2FIX (b ));
1467+ }
1468+ #endif
14381469}
14391470
14401471/*
@@ -1465,6 +1496,41 @@ curses_pair_number(VALUE obj, VALUE attrs)
14651496 curses_stdscr ();
14661497 return INT2FIX (PAIR_NUMBER (NUM2CHTYPE (attrs )));
14671498}
1499+
1500+ /*
1501+ * Document-method: Curses.support_extended_colors?
1502+ *
1503+ * Returns +true+ if the ncurses library was compiled with extended color
1504+ * support (i.e., init_extended_pair, init_extended_color, etc. are available),
1505+ * +false+ otherwise.
1506+ */
1507+ static VALUE
1508+ curses_support_extended_colors (VALUE obj )
1509+ {
1510+ #if defined(HAVE_INIT_EXTENDED_PAIR ) && defined(HAVE_INIT_EXTENDED_COLOR ) && \
1511+ defined(HAVE_EXTENDED_COLOR_CONTENT ) && defined(HAVE_EXTENDED_PAIR_CONTENT )
1512+ return Qtrue ;
1513+ #else
1514+ return Qfalse ;
1515+ #endif
1516+ }
1517+
1518+ /*
1519+ * Document-method: Curses.reset_color_pairs
1520+ *
1521+ * Resets all color pairs to undefined. Requires ncurses 6.1+.
1522+ */
1523+ #ifdef HAVE_RESET_COLOR_PAIRS
1524+ static VALUE
1525+ curses_reset_color_pairs (VALUE obj )
1526+ {
1527+ curses_stdscr ();
1528+ reset_color_pairs ();
1529+ return Qnil ;
1530+ }
1531+ #else
1532+ #define curses_reset_color_pairs rb_f_notimplement
1533+ #endif
14681534#endif /* USE_COLOR */
14691535
14701536#ifdef USE_MOUSE
@@ -2717,7 +2783,7 @@ window_setscrreg(VALUE obj, VALUE top, VALUE bottom)
27172783#endif
27182784}
27192785
2720- #if defined(USE_COLOR ) && defined(HAVE_WCOLOR_SET )
2786+ #if defined(USE_COLOR ) && ( defined(HAVE_WCOLOR_SET ) || defined( HAVE_WATTR_SET ) )
27212787/*
27222788 * Document-method: Curses::Window.color_set
27232789 * call-seq: color_set(col)
@@ -2729,13 +2795,28 @@ static VALUE
27292795window_color_set (VALUE obj , VALUE col )
27302796{
27312797 struct windata * winp ;
2732- int res ;
27332798
27342799 GetWINDOW (obj , winp );
2735- res = wcolor_set (winp -> window , NUM2INT (col ), NULL );
2736- return (res == OK ) ? Qtrue : Qfalse ;
2800+ #if defined(HAVE_WATTR_SET ) && defined(HAVE_WATTR_GET )
2801+ /* Use wattr_set to support pair numbers > 255; preserve existing attrs. */
2802+ {
2803+ attr_t attrs ;
2804+ #ifdef NCURSES_PAIRS_T
2805+ NCURSES_PAIRS_T current_pair ;
2806+ #else
2807+ short current_pair ;
2808+ #endif
2809+ if (wattr_get (winp -> window , & attrs , & current_pair , NULL ) == ERR )
2810+ return Qfalse ;
2811+ return (wattr_set (winp -> window , attrs , NUM2INT (col ), NULL ) == OK ) ? Qtrue : Qfalse ;
2812+ }
2813+ #elif defined(HAVE_WATTR_SET )
2814+ return (wattr_set (winp -> window , 0 , NUM2INT (col ), NULL ) == OK ) ? Qtrue : Qfalse ;
2815+ #else
2816+ return (wcolor_set (winp -> window , NUM2INT (col ), NULL ) == OK ) ? Qtrue : Qfalse ;
2817+ #endif
27372818}
2738- #endif /* defined(USE_COLOR) && defined(HAVE_WCOLOR_SET) */
2819+ #endif /* defined(USE_COLOR) && ( defined(HAVE_WCOLOR_SET) || defined(HAVE_WATTR_SET) ) */
27392820
27402821/*
27412822 * Document-method: Curses::Window.scroll
@@ -5071,6 +5152,8 @@ Init_curses(void)
50715152 rb_define_module_function (mCurses , "pair_content" , curses_pair_content , 1 );
50725153 rb_define_module_function (mCurses , "color_pair" , curses_color_pair , 1 );
50735154 rb_define_module_function (mCurses , "pair_number" , curses_pair_number , 1 );
5155+ rb_define_module_function (mCurses , "support_extended_colors?" , curses_support_extended_colors , 0 );
5156+ rb_define_module_function (mCurses , "reset_color_pairs" , curses_reset_color_pairs , 0 );
50745157#endif /* USE_COLOR */
50755158#ifdef USE_MOUSE
50765159 rb_define_module_function (mCurses , "getmouse" , curses_getmouse , 0 );
@@ -5203,9 +5286,9 @@ Init_curses(void)
52035286 rb_define_method (cWindow , "move" , window_move , 2 );
52045287 rb_define_method (cWindow , "move_relative" , window_move_relative , 2 );
52055288 rb_define_method (cWindow , "setpos" , window_setpos , 2 );
5206- #if defined(USE_COLOR ) && defined(HAVE_WCOLOR_SET )
5289+ #if defined(USE_COLOR ) && ( defined(HAVE_WCOLOR_SET ) || defined( HAVE_WATTR_SET ) )
52075290 rb_define_method (cWindow , "color_set" , window_color_set , 1 );
5208- #endif /* USE_COLOR && HAVE_WCOLOR_SET */
5291+ #endif /* USE_COLOR && ( HAVE_WCOLOR_SET || HAVE_WATTR_SET) */
52095292 rb_define_method (cWindow , "cury" , window_cury , 0 );
52105293 rb_define_method (cWindow , "curx" , window_curx , 0 );
52115294 rb_define_method (cWindow , "maxy" , window_maxy , 0 );
0 commit comments