filesel.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 793 行 · 第 1/2 页

C
793
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "posix.h"
#include "vi.h"
#include "keys.h"
#include "win.h"
#include "colors.h"
#include "menu.h"
#include "source.h"

extern int      CurrentMenuNumber;

/*
 * SelectFileOpen - select file from specified directory
 */
int SelectFileOpen( char *dir, char **result_ptr, char *mask, bool want_all_dirs )
{
    char                dd[FILENAME_MAX],cdir[FILENAME_MAX];
    int                 i,j,rc = ERR_NO_ERR;
    file                *cfile;
    fcb                 *cfcb;
    line                *cline;
    selflinedata        sfd;
    bool                need_entire_path;
    char                *result = *result_ptr;

    /*
     * get current directory
     */
    strcpy( dd, dir );
    strcpy( cdir, dir );
    SetCWD( dir );
    need_entire_path = FALSE;

    /*
     * work through all files
     */
    while( TRUE ) {

        if( dd[ strlen(dd) -1 ] != FILE_SEP ) {
            strcat( dd,FILE_SEP_STR );
        }
        strcat( dd,mask );
        i = GetSortDir( dd, want_all_dirs );
        if( i ) {
            return( i );
        }

        /*
         * allocate temporary file structure
         */
        cfile = FileAlloc( NULL );

        FormatDirToFile( cfile, TRUE );

        /*
         * go get selected line
         */
        memset( &sfd, 0, sizeof( sfd ) );
        sfd.f = cfile;
        sfd.wi = &dirw_info;
        sfd.title = CurrentDirectory;
        sfd.show_lineno = TRUE;
        sfd.cln = 1;
        sfd.eiw = -1;
        rc = SelectLineInFile( &sfd );
        if( rc ) {
            break;
        }
        if( sfd.sl == -1 ) {
            result[0] = 0;
            break;
        }
        j = (int) sfd.sl - 1;
        if( j >= DirFileCount || DirFiles[j]->attr & _A_SUBDIR ) {
            if( j >= DirFileCount ) {
                GimmeLinePtr( j+1, cfile, &cfcb, &cline );
                dd[0] = cline->data[3];
                dd[1] = ':';
                dd[2] = 0;
            } else {
                strcpy( dd, cdir );
                if( dd[strlen(dd)-1] != FILE_SEP ) {
                    strcat( dd, FILE_SEP_STR );
                }
                strcat( dd, DirFiles[j]->name );
            }
            FreeEntireFile( cfile );
            i = SetCWD( dd );
            if( i ) {
                return( i );
            }
            need_entire_path = TRUE;
            strcpy( cdir, CurrentDirectory );
            strcpy( dd, CurrentDirectory );
            continue;
        }
        if( need_entire_path ) {
            strcpy( result, CurrentDirectory );
            if( result[ strlen(result) -1 ] != FILE_SEP ) {
                strcat( result, FILE_SEP_STR );
            }
        } else {
            result[0] = 0;
        }
        strcat( result, DirFiles[j]->name );
        break;

    }

    /*
     * done, free memory
     */
    FreeEntireFile( cfile );
    DCDisplayAllLines();
    return( rc );

} /* SelectFileOpen */

static window_id        cWin;
static bool             isMenu;

/*
 * displayGenericLines - display all lines in a window
 */
static int displayGenericLines( file *f,linenum pagetop, int leftcol,
                         linenum hilite, type_style *style, char **hichars,
                         char **vals, int valoff )
{
    int         i,j,k,text_lines;
    linenum     cl=pagetop;
    fcb         *cfcb,*tfcb;
    line        *cline;
    char        *ptr;
    type_style  *text, *hot_key;
    window_info *info;
    type_style  base;
    char        tmp[MAX_STR];
    bool        disabled;

    /*
     * get pointer to first line on page, and window info
     */
    i = GimmeLinePtr( pagetop, f, &cfcb, &cline );
    if( i ) {
        return( i );
    }
    base.foreground = WindowAuxInfo( cWin, WIND_INFO_TEXT_COLOR );
    base.background = WindowAuxInfo( cWin, WIND_INFO_BACKGROUND_COLOR );
    base.font = WindowAuxInfo( cWin, WIND_INFO_TEXT_FONT );
    text_lines = WindowAuxInfo( cWin, WIND_INFO_TEXT_LINES );

    /*
     * mark all fcb's as being not in display
     */
    tfcb = f->fcb_head;
    while( tfcb != NULL ) {
        tfcb->on_display = FALSE;
        tfcb=tfcb->next;
    }
    cfcb->on_display = TRUE;

    /*
     * run through each line in the window
     */
    ptr = (char *) (hichars);
    if( ptr != NULL ) {
        ptr += 2*(pagetop-1);
    }
    for( j = 1; j <= text_lines; j++ ) {
        if( cline != NULL ) {
            if( isMenu ) {
                if( InvokeMenuHook( CurrentMenuNumber, cl ) == -1 ) {
                    disabled = TRUE;
                    if( cl == hilite ) {
                        info = &activegreyedmenu_info;
                    } else {
                        info = &greyedmenu_info;
                    }
                } else {
                    disabled = FALSE;
                    if( cl == hilite ) {
                        info = &activemenu_info;
                    } else {
                        info = &menuw_info;
                    }
                }
                text = &info->text;
                hot_key = &info->hilight;
            } else {
                text = &base;
                if( cl == hilite ) {
                    text = style;
                }
                hot_key = text;
            }

            /*
             * now, display what we can of the line on the window
             */
            if( cline->len == 0 ) {
                DisplayCrossLineInWindow( cWin,j );
                goto evil_goto;
            } else if( cline->len > leftcol ) {
                if( vals != NULL ) {
                    i = cline->len-leftcol;
                    strncpy( tmp, &(cline->data[leftcol] ), WindMaxWidth+5);
                    for( k = i; k < valoff; k++ ) tmp[ k ] = ' ';
                    tmp[k] = 0;
                    strcat( tmp, vals[j+pagetop-2] );
                    DisplayLineInWindowWithColor( cWin, j, tmp, text, 0 );
                } else {
                    DisplayLineInWindowWithColor( cWin, j, cline->data,
                        text, leftcol );
                }
            } else {
                DisplayLineInWindowWithColor( cWin, j, SingleBlank, text, 0 );
            }
            if( ptr != NULL ) {
                SetCharInWindowWithColor( cWin, j, 1+(int)ptr[1], ptr[0], hot_key );
            }
evil_goto:  if( ptr != NULL ) {
                ptr += 2;
            }
            i = GimmeNextLinePtr( f, &cfcb, &cline );
            if( i ) {
                if( i == ERR_NO_MORE_LINES ) {
                    continue;
                }
                return( i );
            }
            cl++;
            cfcb->on_display = TRUE;
        } else {
            DisplayLineInWindow( cWin, j, "~" );
        }

    }
    return( ERR_NO_ERR );

} /* displayGenericLines */

typedef enum {
    MS_NONE,
    MS_PAGEDOWN,
    MS_PAGEUP,
    MS_DOWN,
    MS_UP,
    MS_EXPOSEDOWN,
    MS_EXPOSEUP
} ms_type;

static window_id        oWin,mouseWin;
static int              mouseLine=-1;
static ms_type          mouseScroll;
static bool             rlMenu;
static int              rlMenuNum;

/*
 * SelectLineMouseHandler - handle mouse events for line selector
 */
bool SelectLineMouseHandler( window_id id, int win_x, int win_y )
{
    int x,y,i;

    if( LastMouseEvent != MOUSE_DRAG && LastMouseEvent != MOUSE_PRESS &&
            LastMouseEvent != MOUSE_DCLICK && LastMouseEvent != MOUSE_RELEASE &&
            LastMouseEvent != MOUSE_REPEAT && LastMouseEvent != MOUSE_PRESS_R ) {
        return( FALSE );
    }
    mouseWin = id;
    mouseScroll = MS_NONE;

    if( !isMenu && (id == cWin) && (LastMouseEvent == MOUSE_REPEAT
                        || LastMouseEvent == MOUSE_PRESS
                        || LastMouseEvent == MOUSE_DCLICK ) ) {
        x = WindowAuxInfo( cWin, WIND_INFO_WIDTH );
        y = WindowAuxInfo( cWin, WIND_INFO_HEIGHT );
        if( win_x == x-1 ) {
            if( win_y == 1 ) {
                mouseScroll = MS_EXPOSEUP;
                return( TRUE );
            } else if( win_y == y-2 ) {
                mouseScroll = MS_EXPOSEDOWN;
                return( TRUE );
            } else if( win_y > 1 && win_y < y/2 ) {
                mouseScroll = MS_PAGEUP;
                return( TRUE );
            } else if( win_y >= y/2 && win_y < y-1 ) {
                mouseScroll = MS_PAGEDOWN;
                return( TRUE );
            }
        }
    }
    if( LastMouseEvent == MOUSE_REPEAT ) {
        if( id != cWin && !isMenu ) {
            y = WindowAuxInfo( cWin, WIND_INFO_Y1 );
            if( MouseRow < y ) {
                mouseScroll = MS_UP;
                return( TRUE );
            }
            y = WindowAuxInfo( cWin, WIND_INFO_Y2 );
            if( MouseRow > y ) {
                mouseScroll = MS_DOWN;
                return( TRUE );
            }
        }
        return( FALSE );
    }
    if( isMenu && EditFlags.Menus && id == MenuWindow &&
                LastMouseEvent != MOUSE_PRESS_R ) {
        i = GetMenuIdFromCoord( win_x );
        if( i >=0 ) {
            rlMenuNum = i - GetCurrentMenuId();
            if( rlMenuNum != 0 ) {
                rlMenu = TRUE;
            }
        }
        return( TRUE );
    }
    if( id != cWin && id != oWin ) {
        return( TRUE );
    }

    if( !InsideWindow( id, win_x, win_y ) ) {
        return( FALSE );
    }
    mouseLine = win_y-1;
    return( TRUE );

} /* SelectLineMouseHandler */

/*
 * adjustCLN - adjust current line number and pagetop
 */
static bool adjustCLN( linenum *cln, linenum *pagetop, int amt,
                                linenum endline, int text_lines )
{
    bool        drawbord=FALSE;

    if( !isMenu ) {
        if( amt < 0 ) {
            if( *cln + amt > 1 ) {
                *cln += amt;
                if( *cln < *pagetop ) {
                    *pagetop += amt;
                    drawbord = TRUE;
                }
            } else {
                *cln = 1;
                *pagetop = 1;
            }
        } else {
            if( *cln + amt < endline ) {
                *cln += amt;
                if( *cln >= *pagetop+text_lines ) {
                    *pagetop += amt;
                    drawbord = TRUE;
                }
            } else {
                *cln = endline;
                *pagetop = endline-text_lines+1;
            }

⌨️ 快捷键说明

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