]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/lib_slk.c
ncurses 4.2
[ncurses.git] / ncurses / lib_slk.c
index b1124e8539e86c369e6b5b51a3758ebc3e021852..64a943a2a620cb2ea5922d5f62856b299ee6c89b 100644 (file)
@@ -1,23 +1,35 @@
-
-/***************************************************************************
-*                            COPYRIGHT NOTICE                              *
-****************************************************************************
-*                ncurses is copyright (C) 1992-1995                        *
-*                          Zeyd M. Ben-Halim                               *
-*                          zmbenhal@netcom.com                             *
-*                          Eric S. Raymond                                 *
-*                          esr@snark.thyrsus.com                           *
-*                                                                          *
-*        Permission is hereby granted to reproduce and distribute ncurses  *
-*        by any means and for any fee, whether alone or as part of a       *
-*        larger distribution, in source or in binary form, PROVIDED        *
-*        this notice is included with any such distribution, and is not    *
-*        removed from any of its header files. Mention of ncurses in any   *
-*        applications linked with it is highly appreciated.                *
-*                                                                          *
-*        ncurses comes AS IS with no warranty, implied or expressed.       *
-*                                                                          *
-***************************************************************************/
+/****************************************************************************
+ * Copyright (c) 1998 Free Software Foundation, Inc.                        *
+ *                                                                          *
+ * Permission is hereby granted, free of charge, to any person obtaining a  *
+ * copy of this software and associated documentation files (the            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
+
+/****************************************************************************
+ *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
+ *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ ****************************************************************************/
 
 /*
  *     lib_slk.c
 #include <ctype.h>
 #include <term.h>      /* num_labels, label_*, plab_norm */
 
-MODULE_ID("$Id: lib_slk.c,v 1.11 1997/01/18 23:06:32 tom Exp $")
-
-#define MAX_SKEY_OLD      8    /* count of soft keys */
-#define MAX_SKEY_LEN_OLD   8   /* max length of soft key text */
-#define MAX_SKEY_PC       12    /* This is what most PC's have */
-#define MAX_SKEY_LEN_PC    5
+MODULE_ID("$Id: lib_slk.c,v 1.14 1998/02/11 12:13:56 tom Exp $")
 
-#define MAX_SKEY      (SLK_STDFMT ? MAX_SKEY_OLD : MAX_SKEY_PC)
-#define MAX_SKEY_LEN  (SLK_STDFMT ? MAX_SKEY_LEN_OLD : MAX_SKEY_LEN_PC)
 /*
  * We'd like to move these into the screen context structure, but cannot,
  * because slk_init() is called before initscr()/newterm().
  */
-int _nc_slk_format;                    /* one more than format specified in slk_init() */
-
-static chtype _slk_attr = A_STANDOUT;  /* soft label attribute */
-static SLK *_slk;
-static void slk_paint_info(WINDOW *win);
-
-/*
- * Fetch the label text.
- */
-
-char *
-slk_label(int n)
-{
-       T(("slk_label(%d)", n));
-
-       if (SP->_slk == NULL || n < 1 || n > SP->_slk->labcnt)
-               return NULL;
-       return(SP->_slk->ent[n-1].text);
-}
-
-/*
- * Write the soft labels to the soft-key window.
- */
-
-static void
-slk_intern_refresh(SLK *slk)
-{
-int i;
-       for (i = 0; i < slk->labcnt; i++) {
-               if (slk->dirty || slk->ent[i].dirty) {
-                       if (slk->ent[i].visible) {
-#ifdef num_labels
-                               if (num_labels > 0 && SLK_STDFMT)
-                               {
-                                 if (i < num_labels) {
-                                   TPUTS_TRACE("plab_norm");
-                                   putp(tparm(plab_norm, i, slk->win,slk->ent[i].form_text));
-                                 }
-                               }
-                               else
-#endif /* num_labels */
-                               {
-                                       wmove(slk->win,SLK_LINES-1,slk->ent[i].x);
-                                       wattrset(slk->win,_slk_attr);
-                                       waddnstr(slk->win,slk->ent[i].form_text, MAX_SKEY_LEN);
-                                       /* if we simulate SLK's, it's looking much more
-                                          natural to use the current ATTRIBUTE also
-                                          for the label window */
-                                       wattrset(slk->win,stdscr->_attrs);
-                               }
-                       }
-                       slk->ent[i].dirty = FALSE;
-               }
-       }
-       slk->dirty = FALSE;
-
-#ifdef num_labels
-       if (num_labels > 0)
-           if (slk->hidden)
-           {
-               TPUTS_TRACE("label_off");
-               putp(label_off);
-           }
-           else
-           {
-               TPUTS_TRACE("label_on");
-               putp(label_on);
-           }
-#endif /* num_labels */
-}
-
-/*
- * Refresh the soft labels.
- */
-
-int
-slk_noutrefresh(void)
-{
-       T(("slk_noutrefresh()"));
-
-       if (SP->_slk == NULL)
-               return(ERR);
-       if (SP->_slk->hidden)
-               return(OK);
-       slk_intern_refresh(SP->_slk);
-       return(wnoutrefresh(SP->_slk->win));
-}
-
-/*
- * Refresh the soft labels.
- */
-
-int
-slk_refresh(void)
-{
-       T(("slk_refresh()"));
-
-       if (SP->_slk == NULL)
-               return(ERR);
-       if (SP->_slk->hidden)
-               return(OK);
-       slk_intern_refresh(SP->_slk);
-       return(wrefresh(SP->_slk->win));
-}
-
-/*
- * Restore the soft labels on the screen.
- */
-
-int
-slk_restore(void)
-{
-       T(("slk_restore()"));
-
-       if (SP->_slk == NULL)
-               return(ERR);
-       SP->_slk->hidden = FALSE;
-       SP->_slk->dirty = TRUE;
-       /* we have to repaint info line eventually */
-       slk_paint_info(SP->_slk->win); 
-       return slk_refresh();
-}
-
-/*
- * Set soft label text.
- */
-
-int
-slk_set(int i, const char *astr, int format)
-{
-SLK *slk = SP->_slk;
-size_t len;
-const char *str = astr;
-const char *p;
-
-       T(("slk_set(%d, \"%s\", %d)", i, str, format));
-
-       if (slk == NULL || i < 1 || i > slk->labcnt || format < 0 || format > 2)
-               return(ERR);
-       if (str == NULL)
-               str = "";
-
-       while (isspace(*str)) str++; /* skip over leading spaces  */
-       p = str;
-       while (isprint(*p)) p++;     /* The first non-print stops */
-
-       --i; /* Adjust numbering of labels */
-
-       len = (size_t)(p - str);
-       if (len > (unsigned)slk->maxlen)
-         len = slk->maxlen;
-       if (len==0)
-         slk->ent[i].text[0] = 0;
-       else
-         (void) strncpy(slk->ent[i].text, str, len);
-       memset(slk->ent[i].form_text,' ', (unsigned)slk->maxlen);
-       slk->ent[i].text[slk->maxlen] = 0;
-       /* len = strlen(slk->ent[i].text); */
-
-       switch(format) {
-       case 0: /* left-justified */
-               memcpy(slk->ent[i].form_text,
-                      slk->ent[i].text,
-                      len);
-               break;
-       case 1: /* centered */
-               memcpy(slk->ent[i].form_text+(slk->maxlen - len)/2,
-                      slk->ent[i].text,
-                      len);
-               break;
-       case 2: /* right-justified */
-               memcpy(slk->ent[i].form_text+ slk->maxlen - len,
-                      slk->ent[i].text,
-                      len);
-               break;
-       }
-       slk->ent[i].form_text[slk->maxlen] = 0;
-       slk->ent[i].dirty = TRUE;
-       return(OK);
-}
-
-/*
- * Force the code to believe that the soft keys have been changed.
- */
-
-int
-slk_touch(void)
-{
-       T(("slk_touch()"));
-
-       if (SP->_slk == NULL)
-               return(ERR);
-       SP->_slk->dirty = TRUE;
-       return(OK);
-}
-
-/*
- * Remove soft labels from the screen.
- */
-
-int
-slk_clear(void)
-{
-       T(("slk_clear()"));
-
-       if (SP->_slk == NULL)
-               return(ERR);
-       SP->_slk->hidden = TRUE;
-       /* For simulated SLK's it's looks much more natural to
-          inherit those attributes from the standard screen */
-       SP->_slk->win->_bkgd  = stdscr->_bkgd;
-       SP->_slk->win->_attrs = stdscr->_attrs;
-       werase(SP->_slk->win);
-       return wrefresh(SP->_slk->win);
-}
+int _nc_slk_format;  /* one more than format specified in slk_init() */
 
 /*
  * Paint the info line for the PC style SLK emulation.
- * 
+ *
  */
-
 static void
 slk_paint_info(WINDOW *win)
 {
   if (win && _nc_slk_format==4)
     {
       int i;
-  
+
       mvwhline (win,0,0,0,getmaxx(win));
       wmove (win,0,0);
-      
-      for (i = 0; i < _slk->maxlab; i++) {
+
+      for (i = 0; i < SP->_slk->maxlab; i++) {
        if (win && _nc_slk_format==4)
          {
-           mvwaddch(win,0,_slk->ent[i].x,'F');
+           mvwaddch(win,0,SP->_slk->ent[i].x,'F');
            if (i<9)
              waddch(win,'1'+i);
            else
@@ -293,7 +83,6 @@ slk_paint_info(WINDOW *win)
  * Initialize soft labels.
  * Called from newterm()
  */
-
 int
 _nc_slk_initialize(WINDOW *stwin, int cols)
 {
@@ -302,53 +91,53 @@ char *p;
 
        T(("slk_initialize()"));
 
-       if (_slk)
+       if (SP->_slk)
          { /* we did this already, so simply return */
-           SP->_slk = _slk;
            return(OK);
          }
        else
-         if ((SP->_slk = _slk = typeCalloc(SLK, 1)) == 0)
+         if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
            return(ERR);
 
-       _slk->ent = NULL;
-       _slk->buffer = NULL;
+       SP->_slk->ent    = NULL;
+       SP->_slk->buffer = NULL;
+       SP->_slk->attr   = A_STANDOUT;
 
 #ifdef num_labels
-       _slk->maxlab = (num_labels > 0) ? num_labels : MAX_SKEY;
-       _slk->maxlen = (num_labels > 0) ? label_width * label_height : MAX_SKEY_LEN;
-       _slk->labcnt = (_slk->maxlab < MAX_SKEY) ? MAX_SKEY : _slk->maxlab;
+       SP->_slk->maxlab = (num_labels > 0) ? num_labels : MAX_SKEY;
+       SP->_slk->maxlen = (num_labels > 0) ? label_width * label_height : MAX_SKEY_LEN;
+       SP->_slk->labcnt = (SP->_slk->maxlab < MAX_SKEY) ? MAX_SKEY : SP->_slk->maxlab;
 #else
-       _slk->labcnt = _slk->maxlab = MAX_SKEY;
-       _slk->maxlen = MAX_SKEY_LEN;
+       SP->_slk->labcnt = SP->_slk->maxlab = MAX_SKEY;
+       SP->_slk->maxlen = MAX_SKEY_LEN;
 #endif /* num_labels */
 
-       _slk->ent = typeCalloc(slk_ent, _slk->labcnt);
-       if (_slk->ent == NULL)
+       SP->_slk->ent = typeCalloc(slk_ent, SP->_slk->labcnt);
+       if (SP->_slk->ent == NULL)
          goto exception;
 
-       p = _slk->buffer = (char*) calloc(2*_slk->labcnt,(1+_slk->maxlen));
-       if (_slk->buffer == NULL)
+       p = SP->_slk->buffer = (char*) calloc(2*SP->_slk->labcnt,(1+SP->_slk->maxlen));
+       if (SP->_slk->buffer == NULL)
          goto exception;
 
-       for (i = 0; i < _slk->labcnt; i++) {
-               _slk->ent[i].text = p;
-               p += (1 + _slk->maxlen);
-               _slk->ent[i].form_text = p;
-               p += (1 + _slk->maxlen);
-               memset(_slk->ent[i].form_text, ' ', (unsigned)_slk->maxlen);
-               _slk->ent[i].visible = (i < _slk->maxlab);
+       for (i = 0; i < SP->_slk->labcnt; i++) {
+               SP->_slk->ent[i].text = p;
+               p += (1 + SP->_slk->maxlen);
+               SP->_slk->ent[i].form_text = p;
+               p += (1 + SP->_slk->maxlen);
+               memset(SP->_slk->ent[i].form_text, ' ', (unsigned)(SP->_slk->maxlen));
+               SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
        }
        if (_nc_slk_format >= 3) /* PC style */
          {
-           int gap = (cols - 3 * (3 + 4*_slk->maxlen))/2;
+           int gap = (cols - 3 * (3 + 4*SP->_slk->maxlen))/2;
 
            if (gap < 1)
              gap = 1;
 
-           for (i = x = 0; i < _slk->maxlab; i++) {
-             _slk->ent[i].x = x;
-             x += _slk->maxlen;
+           for (i = x = 0; i < SP->_slk->maxlab; i++) {
+             SP->_slk->ent[i].x = x;
+             x += SP->_slk->maxlen;
              x += (i==3 || i==7) ? gap : 1;
            }
            if (_nc_slk_format == 4)
@@ -356,26 +145,26 @@ char *p;
          }
        else {
          if (_nc_slk_format == 2) {    /* 4-4 */
-           int gap = cols - (_slk->maxlab * _slk->maxlen) - 6;
+           int gap = cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 6;
 
            if (gap < 1)
                        gap = 1;
-           for (i = x = 0; i < _slk->maxlab; i++) {
-             _slk->ent[i].x = x;
-             x += _slk->maxlen;
+           for (i = x = 0; i < SP->_slk->maxlab; i++) {
+             SP->_slk->ent[i].x = x;
+             x += SP->_slk->maxlen;
              x += (i == 3) ? gap : 1;
            }
          }
          else
            {
              if (_nc_slk_format == 1) { /* 1 -> 3-2-3 */
-               int gap = (cols - (_slk->maxlab * _slk->maxlen) - 5) / 2;
+               int gap = (cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 5) / 2;
 
                if (gap < 1)
                  gap = 1;
-               for (i = x = 0; i < _slk->maxlab; i++) {
-                 _slk->ent[i].x = x;
-                 x += _slk->maxlen;
+               for (i = x = 0; i < SP->_slk->maxlab; i++) {
+                 SP->_slk->ent[i].x = x;
+                 x += SP->_slk->maxlen;
                  x += (i == 2 || i == 4) ? gap : 1;
                }
              }
@@ -383,16 +172,16 @@ char *p;
                goto exception;
            }
        }
-       _slk->dirty = TRUE;
-       if ((_slk->win = stwin) == NULL)
+       SP->_slk->dirty = TRUE;
+       if ((SP->_slk->win = stwin) == NULL)
        {
        exception:
-               if (_slk)
+               if (SP->_slk)
                {
-                  FreeIfNeeded(_slk->buffer);
-                  FreeIfNeeded(_slk->ent);
-                  free(_slk);
-                  SP->_slk = _slk = (SLK*)0;
+                  FreeIfNeeded(SP->_slk->buffer);
+                  FreeIfNeeded(SP->_slk->ent);
+                  free(SP->_slk);
+                  SP->_slk = (SLK*)0;
                   return(ERR);
                }
        }
@@ -400,44 +189,21 @@ char *p;
        return(OK);
 }
 
+
 /*
- * Initialize soft labels.  Called by the user before initscr().
+ * Restore the soft labels on the screen.
  */
-
 int
-slk_init(int format)
-{
-       if (format < 0 || format > 3)
-               return(ERR);
-       _nc_slk_format = 1 + format;
-       return(OK);
-}
-
-/* Functions to manipulate the soft-label attribute */
-
-int
-slk_attrset(const attr_t attr)
-{
-    _slk_attr = attr;
-    return(OK);
-}
-
-int
-slk_attron(const attr_t attr)
+slk_restore(void)
 {
-    toggle_attr_on(_slk_attr,attr);
-    return(OK);
-}
+       T((T_CALLED("slk_restore()")));
 
-int
-slk_attroff(const attr_t attr)
-{
-    toggle_attr_off(_slk_attr,attr);
-    return(OK);
-}
+       if (SP->_slk == NULL)
+               return(ERR);
+       SP->_slk->hidden = FALSE;
+       SP->_slk->dirty = TRUE;
+       /* we have to repaint info line eventually */
+       slk_paint_info(SP->_slk->win);
 
-attr_t
-slk_attr(void)
-{
-  return _slk_attr;
+       returnCode(slk_refresh());
 }