⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 show.c

📁 Unix环境下的curses编程库。
💻 C
📖 第 1 页 / 共 4 页
字号:
#include <malloc.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <curses.h>

#include <sys/types.h>
#include <sys/timeb.h>
#include <signal.h>

#define EnterHand

#define SET_MYNODELAY(f) fcntl(f,F_SETFL,fcntl(f,F_GETFL,0)|O_NDELAY)
#define CLR_MYNODELAY(f) fcntl(f,F_SETFL,fcntl(f,F_GETFL,0)&~O_NDELAY)

#define ComMaxRow     540
#define MaxSingleItem 720
#define ItemConLen     20
#define MaxWindowNum   20
#define Enter        0x0d
#define BackSpace    0x08

typedef enum { UnknownWin, PureTextWin, SingleItemWin } EWinType;

typedef struct {
    EWinType nWinType;
    union {
        struct {
            char _pure_text[80*ComMaxRow+2];  /* all contents */
            short _text_attrs[ComMaxRow];     /* the attribute for row */
        } _text_con;
        struct {
            short _item_valid;
            char _item_id[ItemConLen], _item_sts[ItemConLen];
            short _item_id_attrs, _item_sts_attrs;
        } _item_con[MaxSingleItem];
    } _all_content;
#define sWinContent	_all_content._text_con._pure_text
#define nTextAttrs(i)	_all_content._text_con._text_attrs[(i)]
#define ItemWhole(i)	_all_content._item_con[(i)]
#define nItemValid(i)	_all_content._item_con[(i)]._item_valid
#define sItemId(i)	_all_content._item_con[(i)]._item_id
#define sItemSts(i)	_all_content._item_con[(i)]._item_sts
#define nItemIdAttrs(i)	_all_content._item_con[(i)]._item_id_attrs
#define nItemStsAttrs(i)	_all_content._item_con[(i)]._item_sts_attrs
    int  nSubWinMaxRow, nSubWinMaxCol,/* the size of sub window */
         nMaxTotalRow,  /* max row */
         nStartRow,     /* the start row of contents */
         nCurrentRow,   /* the last row of contents, count from 0 */
         nCurWinFirstRow,/* the position of sub window in contents*/
         nCurSerno;     /* the last serno of message, count from 0 */
    int  nScrollSignPos; /* the position of scroll bar */
    WINDOW *sWin,  /* sub window for showing data */
           *pWin;  /* main window which include sub window, border*/
    int  nWidthPerItem;
} ShowWindow;

ShowWindow *g_apWindow[MaxWindowNum+2]={NULL}, /*array of all windows*/
     *g_pErrMsgWindow,  /* this window for show error messages*/
     *g_pPromptWindow,  /* this window for command prompt*/
     *g_pCurWindow,      /* current window */
     *g_pCurMainWindow;    /* current main window */
int g_nHaveInCurses=0, g_nHaveInColors=0,
    g_nShowImmediate=1,
    g_nNoDelay=0;

 /* this parameters for showing main information
    reqiure: NumberPerLine<=6 && LinesPerItem>=2 */
#define NumberPerLine     5
#define LinesPerItem      3

void displayShowWindow( ShowWindow *tempWindow );

/**************************************************************
 * Name: getoutString(char *dst, char *src, int nLeftLen,     *
 *                    int nLength, int nManner);              *
 * Description: get a string dst from string src,             *
 *                  the length of dst is nLength-nLeftLen     *
 * Inputs: char *dst         . the destination string.        *
 *         char *src         . the source string.             *
 *         int nLeftLen      . the start position of          *
 *                                 source string.             *
 *         int nLength       . the length of get-out.         *
 *         int nManner       . the manner of get-out.         *
 * Output: none.                                              *
 * Return: none.                                              *
 * Cautions: none.                                            *
 **************************************************************/
int getoutString( char *dst, char *src, int nLeftLen,
                  int nLength, int nManner )
{
    int hz,length,i,j;

    length = nLength-nLeftLen;
    if ( length<=0 || src==NULL || dst==NULL )
        return 0;
    j = hz = 0;
    while ( j<length && src[j]!='\0' )
    {
        if ( (int)src[j]<32 && (int)src[j]>0 )
            dst[j] = '?';
        else
            dst[j] = src[j];
        if ( (int) src[j]<0 || (int) src[j]>=128 )
        {
            hz = 1-hz;
        }
        else
        {
            if ( hz==1 )
                hz = 0;
        }
        j++;
    }
    if ( hz==1 )
        j--;
    i = j;
    if ( nManner & 0x0002 )
    {
        for ( ; i<length; i++ )
            dst[i] = ' ';
    }
    if ( nManner & 0x0001 )
    {
        dst[i] = '\0';
    }
    return j;
}    /* end getoutString() */

/**************************************************************
 * Name: initShowSystem(int nDelayTime);                      *
 * Description: initial system && some data                   *
 * Inputs: int nDelayTime   . the delay time when get char.   *
 * Output: none.                                              *
 * Return: none.                                              *
 * Cautions: none.                                            *
 **************************************************************/
void initShowSystem( int nDelayTime )
{
    /* ignore the interrupt signal */
    signal( SIGINT, SIG_IGN );

    initscr();
    g_nNoDelay = 0;
    if ( nDelayTime>0 )
        halfdelay( nDelayTime );
    else if ( nDelayTime==0 )
    {
        cbreak();
        g_nNoDelay = 1;
    }
    noecho();
    nonl();

    g_nHaveInCurses = 1;
    g_pCurWindow = g_pCurMainWindow = (ShowWindow *)NULL;
    if ( has_colors()==TRUE ) {
        g_nHaveInColors = 1;
        start_color();
    }
}    /* end initShowSystem() */

/**************************************************************
 * Name: exitShowSystem();                                    *
 * Description: exit curses.                                  *
 * Inputs: none.                                              *
 * Output: none.                                              *
 * Return: none.                                              *
 * Cautions: none.                                            *
 **************************************************************/
void exitShowSystem( void )
{
    if ( g_nHaveInCurses )
    {
        nl();
        echo();
        nocbreak();
        endwin();
        g_nHaveInCurses = 0;
        return ;
    }
}    /* end exitShowSystem() */

/**************************************************************
 * Name: newShowWindow(int nWindowNo, int maxrow, int maxcol);*
 * Description: create one ShowWindow.                        *
 * Inputs: int nWindowNo     . the window number for creating.*
 *         int maxrow        . the row of new window.         *
 *         int maxcol        . the col of new window.         *
 * Output: none.                                              *
 * Return: the address pointer to new window.                 *
 *         Null if no enough space.                           *
 * Cautions: none.                                            *
 **************************************************************/
ShowWindow *newShowWindow( int nWindowNo, int maxrow, int maxcol )
{
    ShowWindow *tempWindow;
    int  nLeft,nTop,nHeighth,nWidth,nMaxRow;
    int  i;

    if ( maxrow<=2 || maxcol<=2 || maxrow>24 || maxcol>80 )
        return NULL;

    nHeighth = maxrow;
    nWidth   = maxcol;
    if ( nWindowNo < 0 )
    {
        return NULL;
    }
    else if ( nWindowNo < MaxWindowNum )
    {
        nLeft = ( 80 - nWidth ) >> 1;
        nTop  = (24 - nHeighth ) >> 1;
        nMaxRow = ComMaxRow;
    }
    else if ( nWindowNo == MaxWindowNum )
    {
        nLeft = ( 80 - nWidth ) >> 1;
        nTop  = 24 - nHeighth;
        nMaxRow = ComMaxRow;
    }
    else if ( nWindowNo == MaxWindowNum+1 )
    {
        nLeft = 80 - nWidth;
        nTop  = 24 - nHeighth;
        nMaxRow = ComMaxRow;
    }
    else
    {
        return NULL;
    }

    if ( (tempWindow=(ShowWindow *) malloc(sizeof(ShowWindow)))==NULL )
        return NULL;
    tempWindow->pWin = tempWindow->sWin = (WINDOW *) NULL;

    /* create main window */
    if ( ( tempWindow->pWin =
              newwin( nHeighth, nWidth, nTop, nLeft ) ) == NULL )
    {
        free( tempWindow );
        return NULL;
    }
    else
    {
        wclear( tempWindow->pWin );
/*
        wborder( tempWindow->pWin,
                 0x100003A, 0x100003A,
                 0x100004D, 0x100004D,
                 0x1000049, 0x100009E,
                 0x1000048, 0x100009F );
*/
        wborder( tempWindow->pWin,
                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 );
        wrefresh( tempWindow->pWin );
    }

    /* create sub window in main window */
    if ( ( tempWindow->sWin =
            newwin( nHeighth-2, nWidth-4, nTop+1, nLeft+2 ) ) == NULL )
    {
        delwin( tempWindow->pWin );
        free( tempWindow );
        return NULL;
    }

    for ( i=0; i<80*nMaxRow; i++ )
        tempWindow->sWinContent[i] = ' ';
    tempWindow->sWinContent[80*nMaxRow] = '\0';
    if ( g_nNoDelay )
        nodelay( tempWindow->sWin, TRUE );
    tempWindow->nSubWinMaxRow = nHeighth-2;
    tempWindow->nSubWinMaxCol = nWidth-4;
    tempWindow->nMaxTotalRow = nMaxRow;
    tempWindow->nStartRow = tempWindow->nCurWinFirstRow = 0;
    tempWindow->nScrollSignPos = 0;
    tempWindow->nWidthPerItem = 
            tempWindow->nSubWinMaxCol / NumberPerLine - 1;
    tempWindow->nCurrentRow = -1;
    tempWindow->nCurSerno = -1;
    tempWindow->nWinType = UnknownWin;

    return tempWindow;
}    /* end newShowWindow() */

/**************************************************************
 * Name: setCurActiveWindow(int nWindowNo);                   *
 * Description: set the current active window.                *
 * Inputs: int nWindowNo     . the window number by activing. *
 * Output: none.                                              *
 * Return: none.                                              *
 * Cautions: none.                                            *
 **************************************************************/
void setCurActiveWindow( int nWindowNo )
{
    if ( nWindowNo>=0 && nWindowNo<MaxWindowNum )
    {
        if ( g_apWindow[nWindowNo] != NULL )
        {
           touchwin(g_apWindow[nWindowNo]->pWin);
           wrefresh(g_apWindow[nWindowNo]->pWin);
           touchwin(g_apWindow[nWindowNo]->sWin);
           wrefresh(g_apWindow[nWindowNo]->sWin);
           g_pCurMainWindow = g_pCurWindow = g_apWindow[nWindowNo];
        }
    }
    else if ( nWindowNo==-1 && g_pErrMsgWindow!=NULL)
    {
        touchwin(g_pErrMsgWindow->pWin);
        wrefresh(g_pErrMsgWindow->pWin);
        touchwin(g_pErrMsgWindow->sWin);
        wrefresh(g_pErrMsgWindow->sWin);
        g_pCurWindow = g_pErrMsgWindow;
    }
#ifdef EnterHand
    else if ( nWindowNo==-2 && g_pPromptWindow!=NULL )
    {
        touchwin(g_pPromptWindow->pWin);
        wrefresh(g_pPromptWindow->pWin);
        touchwin(g_pPromptWindow->sWin);
        wrefresh(g_pPromptWindow->sWin);
        g_pCurWindow = g_pPromptWindow;
    }
#endif
    displayShowWindow( g_pCurWindow );
}    /* end setCurActiveWindow() */

/**************************************************************
 * Name: createWindows(int nMainWinNumebr, int nHaveErrWin);  *
 * Description: create main windows(number: nMainWinNumber).  *
 *               If nHaveErrWin is true, create errormsg-     *
 *               window.                                      *
 * Inputs: int nMainWinNumber . the main windows number.      *
 *         int nHaveErrWin    . the sign if create errormsg-  *
 *                              window or not.                *
 * Output: none.                                              *
 * Return: 0 for success,                                     *
 *         other value for fail.                              *
 * Cautions: none.                                            *
 **************************************************************/
int createWindows( int nMainWinNumber, int nHaveErrWin )
{
    int i;

    if ( nMainWinNumber<=0 || nMainWinNumber>MaxWindowNum )
        return (-1);

    for ( i=0; i<nMainWinNumber; i++ )
    {
         if ( ( g_apWindow[i] = newShowWindow( i, 24, 80 ) ) == NULL )
             return (i+1);
    }
    for ( ; i<MaxWindowNum+2; i++ )
    {
        g_apWindow[i] = NULL;
    }
    g_pErrMsgWindow = g_pPromptWindow = NULL;
    if ( nHaveErrWin )
    {
        if ( ( g_pErrMsgWindow =
                newShowWindow( MaxWindowNum, 12, 62 ) ) == NULL )
        {
            return (MaxWindowNum+1);
        }
        else
        {
            g_apWindow[MaxWindowNum] = g_pErrMsgWindow;
        }
#ifdef EnterHand
        if ( ( g_pPromptWindow =
                newShowWindow( MaxWindowNum+1, 10, 52 ) ) == NULL )
        {
            return (MaxWindowNum+1);
        }
        else
        {
            g_apWindow[MaxWindowNum+1] = g_pPromptWindow;
        }
#endif
    }
    setCurActiveWindow( 0 );
    return 0;
}    /* end createWindows() */

/**************************************************************
 * Name: delShowWindow(ShowWindow *tempWindow);               *
 * Description: clear and free the memory of ShowWindow.      *
 * Inputs: ShowWindow *tempWindow . the window for delete.    *
 * Output: none.                                              *
 * Return: none.                                              *
 * Cautions: none.                                            *
 **************************************************************/
void delShowWindow( ShowWindow *tempWindow )
{
    if ( tempWindow==NULL )
        return ;
    if ( tempWindow->sWin ) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -