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

📄 support.c

📁 清华魏永名miniGUI dos下的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
// Support.c
// This file include functions that handle the display of 
// mono bitmap, caret, and lower layer functions.
//
// Copyright (c) 1994.8 ~ 1998.4, Mr. Wei Yongming.
//
// Last modified date: 1998.04.15.

#include <stdio.h>
#include <conio.h> 
#include <io.h>
#include <dos.h>
#include <bios.h>
#include <process.h>
#include <time.h>
#include <stddef.h>
#include <stdlib.h>
#include <malloc.h>
#include <graph.h>
#include <memory.h>
#include <Vmemory.h>
#include <string.h>
#include <direct.h>
#include <ctype.h>
#include <float.h>
#include <math.h>

#include "Common.h"
#include "Support.h"
#include "MainGUI.h"
#include "Dialog.h"

struct _videoconfig vc;
RECT rcClipRgn;
void (__interrupt __far *oldClock)();
CARETINFO  CaretInfo = {0, 0, 0, FALSE, FALSE, 0, 0};
BOOL fNewKb = FALSE;

static CFONTINFO CFontInfo;
static char* pAFont;
static _vmhnd_t hdlImage = _VM_NULL;       // Image memory handle
static MOUSEINFO mi =
{
    1, 0, 0,
    0, 0,
    0, 0,
    0, 0,
    0, 0
};

static BOOL LoadFont( void );
static void UnloadFont( void );

static int MouseInit( void );

/*
 * Function: BOOL GUIAPI InitVirtualMemory( void );
 * Parameters:
 *      None.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
 
BOOL GUIAPI InitVirtualMemory( unsigned int iMinKByte, unsigned int iMaxKByte )
{
    // Allocate virtual memory and lock it.
    if ( !_vheapinit( 64*iMinKByte, 64*iMaxKByte, _VM_XMS ) )
    {
        printf( "Could not initialize virtual memory manager.\n\n" );
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("Please check the free conventional memory and extended memory manager of your computer!");
        return FALSE;
    }
    
    return TRUE;
}

/*
 * Function: BOOL LockFont( void );
 *      This function load the Chinese font(16*16) library into the virtual memory.
 * Parameters:
 *      None.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
BOOL FAR LockFont( void )
{
    return TRUE;
}

/*
 * Function: BOOL UnlockFont( void );
 *      This function load the Chinese font(16*16) library into the virtual memory.
 * Parameters:
 *      None.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
void FAR UnlockFont( void )
{
}

/*
 * Function: BOOL LoadFont( void );
 *      This function load the Chinese font(16*16) library into the virtual memory.
 * Parameters:
 *      None.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
static BOOL LoadFont( void )
{
    int i, j;
    FILE* fp;
    char pBuffer[1328];
    LPSTR lpStr;
    
    // Load Chinese font information to the virtual memory.
    printf( "Allocate virtual memory for the font library..." );
    for( i=0; i<5; i++)
    {
        if ( (CFontInfo.Handle[i] = _vmalloc( 60L*1024 )) == _VM_NULL )
        {
            printf( "failure.\n\n");
            _settextposition(24, 1);
            _settextcolor( 14 );
            _outtext("Your computer should have less than 2Mb memory!");
            return FALSE;
        }
    }
    printf( "done.\n" );

    // Open font file and read information to the virtual memory.
    printf( "Read and load font information from file..." );
    if(!(fp = fopen( ".\\bin\\hzk16", "rb")))
    {
        printf("failure.\n\n");
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("The font file ""hzk16"" can not be found!");
        return FALSE;
    }
        
    for(i=0; i<4; i++)
    {
        lpStr = (LPSTR)_vload( CFontInfo.Handle[i], _VM_DIRTY );
        fseek(fp, 0L + i*61440, SEEK_SET);
        for(j=0; j<60; j++)
        {
            fread(pBuffer, sizeof(char), 1024, fp);
            _fmemcpy(lpStr + j*1024L, pBuffer, 1024);
        }
    }
    
    lpStr = (LPSTR)_vload( CFontInfo.Handle[4], _VM_DIRTY );
    fseek(fp, 4L*61440, SEEK_SET);
    for(j=0; j<15; j++)
    {
        fread(pBuffer, sizeof(char), 1024, fp);
        _fmemcpy(lpStr + j*1024L, pBuffer, 1024);
    }
    fclose(fp);
    
    // Load ASSIC font information to the near memory.
    printf( "Load ASSIC font..." );
    pAFont = malloc(4096);
    if(!(fp = fopen( ".\\bin\\asc16", "rb")))
    {
        printf("failure.\n\n");
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("The font file ""asc16"" can not be found!");
        return FALSE;
    }
    fseek(fp, 0L, SEEK_SET);
    fread(pAFont, sizeof(char), 4096, fp);
    fclose(fp);
    printf( "done.\n" );
    
    // Alloc memory and load icon image
    printf( "Allocate virtual memory for icon image..." );
    if ( (hdlImage = _vmalloc( 3L*1328 )) == _VM_NULL )
    {
        printf( "failure.\n\n");
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("Your computer should have less than 1Mb extended memory!");
        return FALSE;
    }
    if ( (lpStr = (LPSTR)_vload( hdlImage, _VM_DIRTY )) == NULL )
    {
        printf( "failure.\n\n");
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("Insufficient memory!");
        return FALSE;
    }
    printf( "done.\n" );

    // Load icon image from file.
    printf( "Read icon image from file..." );
    if(!(fp = fopen( ".\\bin\\icon.img", "rb")))
    {
        printf("failure.\n\n");
        _settextposition(24, 1);
        _settextcolor( 14 );
        _outtext("The icon image file ""icon.img"" can not be found!");
        return FALSE;
    }
        
    fseek(fp, 0L, SEEK_SET);
    for(j=0; j<3; j++)
    {
        fread(pBuffer, sizeof(char), 1328, fp);
        _fmemcpy(lpStr + j*1328L, pBuffer, 1328);
    }
    fclose(fp);
    printf( "done.\n" );

    return TRUE;
}

/*
 * Function: void UnloadFont( void );
 *      This function unload the Chinese font(16*16) library 
 *      and free the vitual memory then terminate the vitual memory manager.
 * Parameters:
 *      None.
 * Return:
 *      None.
 */
static void UnloadFont( void )
{
    int i;
    
    for(i=0; i<5; i++)
    {
        if(CFontInfo.Handle[i] != _VM_NULL)
            _vfree(CFontInfo.Handle[i]);
    }
    
    free(pAFont);
    
    if(hdlImage != _VM_NULL)
        _vfree(hdlImage);
}

/*
 * Function: BOOL InitGUI( void );
 *      This Function Initializate tha graphics element, such as
 *    videomode, pallete etc.
 * Parameters:
 *      None.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
BOOL GUIAPI InitGUI()
{   
    // Color pallete.
    long pal[16]=
       {RGB( 0, 0, 0 ),         //black         --0
        RGB( 0, 0, 63 ),        //blue          --1
        RGB( 0, 63, 63 ),       //cyan          --2
        RGB( 0, 63, 0 ),        //green         --3
        RGB( 63, 63, 0 ),       //yellow        --4
        RGB( 63, 0, 0 ),        //red           --5
        RGB( 63, 0, 63 ),       //magenta       --6
        RGB( 48, 48, 48 ),      //light gray    --7
        RGB( 0, 0, 32 ),        //dark blue     --8
        RGB( 0, 32, 32 ),       //dark cyan     --9
        RGB( 0, 32, 0 ),        //dark green    --10
        RGB( 32, 32, 0 ),       //dark yellow   --11
        RGB( 32, 0, 0 ),        //dark red      --12
        RGB( 32, 0, 32 ),       //dark magenta  --13
        RGB( 32, 32, 32 ),      //dark gray     --14
        RGB( 63, 63, 63 )       //light white   --15
       };
        
    // Set new interrupt vector.
    _disable();
    oldClock = _dos_getvect(0x1c);
    _dos_setvect(0x1c, Clock);
    _enable();
    
    // Set new critical error handler.
    _harderr(&CriticalErrorHandler);
    
    // Load the Chinese font library and message box icon image to the vitual memory.
    if(!LoadFont())
        return FALSE;

    // Initialize the graphics adapter.
    if( !_setvideomode( _VRES16COLOR ) )
    {
        _setvideomode( _DEFAULTMODE );
        printf( "Invalid graphics adapter!\n" );
        return FALSE;
    }
    
    _getvideoconfig( &vc );
    rcClipRgn.left = rcClipRgn.top = 0;
    rcClipRgn.right = vc.numxpixels - 1;
    rcClipRgn.bottom = vc.numypixels - 1;

  if( !_remapallpalette( pal ) )
  {
      _setvideomode( _DEFAULTMODE );
      printf( "Palettes not available with this adapter!\n" );
      return FALSE;
  }
    
    // To determine weather the keyboad is ehanced.
    if( (*(BYTE FAR *)( 0x00400096 )) & 0x10 )
        fNewKb = TRUE;
        
    MouseInit();
    return TRUE;
}

/*
 * Function: void TerminateGUI( void );
 *      This Function Terminate this procedure.
 * Parameters:
 *      None.
 * Return:
 *      None.
 */

void GUIAPI TerminateGUI( void )
{ 
    _disable();
    _dos_setvect(0x1c, oldClock);
    _enable();
    
    if(vc.mode != 3)
        _setvideomode( _DEFAULTMODE );
    
    UnloadFont();
}

/*
 * Function: void DosShell(PGUIINFO pGUIInfo);
 *      This Function Terminate this procedure.
 * Parameters:
 *      None.
 * Return:
 *      None.
 */
void GUIAPI DosShell()
{ 
    short oldfgd;
    
    _disable();
    _dos_setvect(0x1c, oldClock);
    _enable();
    
    _setvideomode( _DEFAULTMODE );
    UnloadFont();
    _vheapterm();

    _flushall();
    
    oldfgd = _gettextcolor();
    _settextcolor( 14 );         // Set to yellow color.
    _settextposition(1, 1);
    _outtext("鄄卑鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍氨槽");
    _settextposition(2, 1);
    _outtext("鄄卑                                                                        氨槽");
    _settextposition(3, 1);
    _outtext("鄄卑    Type EXIT and press ENTER to quit this MS_DOS prompt and return.    氨槽");
    _settextposition(4, 1);
    _outtext("鄄卑                                                                        氨槽");
    _settextposition(5, 1);
    _outtext("鄄卑               (C) Copyright Mr. Wei Yongming 1996 ~ 1998.              氨槽");
    _settextposition(6, 1);
    _outtext("鄄卑                                                                        氨槽");
    _settextposition(7, 1);
    _outtext("鄄卑鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍鞍氨槽");
    _settextcolor(oldfgd);
    
    _spawnvp(_P_WAIT, "c:\\command.com", NULL);
    
    
}

/*
 * Function: void ExecuteExternProcFromGUI( PSTR cmdname )
 *      This Function Terminate this procedure.
 * Parameters:
 *      None.
 * Return:
 *      None.
 */
void GUIAPI ExecuteExternProcFromGUI( PSTR cmdname )
{ 
    _disable();
    _dos_setvect(0x1c, oldClock);
    _enable();
    
    _setvideomode( _DEFAULTMODE );
    UnloadFont();
    _vheapterm();

    _flushall();

    _spawnvp(_P_WAIT, cmdname, NULL);
}

/*
 * Function: BOOL Bitmap( LPSTR lpFileName, int x, int y);
 *      This Function display a color bitmap with information in the file
 * Parameters:
 *      lpFileNme: File name;
 *      x,y: the coordination of display vector.
 * Return:  success-----TRUE.
 *          failure-----FALSE.
 */
BOOL GUIAPI Bitmap( LPCSTR lpFileName, short color, short lx, short by)
{
    char FileName[13];
    BYTE *bitmapBuff, *bitmap;
    int byte;
    FILE *fp;
    int i,j;
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    long reset;
    
    _fstrcpy((char __far *)FileName, lpFileName);

    if(!(fp = fopen( FileName, "rb")))return FALSE;
   
    fseek(fp, 0L, SEEK_SET);
    fread(&bmfh, sizeof(char), sizeof(BITMAPFILEHEADER), fp);
    fread(&bmih, sizeof(char), sizeof(BITMAPINFOHEADER), fp);
    reset = (long)(bmih.biSizeImage/bmih.biHeight - bmih.biWidth/8);
    byte = (int)bmih.biWidth/8;
    
    set_color(color);
    
    if(!(bitmap = malloc((size_t)bmih.biSizeImage)))
    {
        fclose(fp);
        return FALSE;
    }
    bitmapBuff = bitmap;
    fseek(fp, (long)bmfh.bfOffBits, SEEK_SET);
    fread(bitmap, 1, (size_t)bmih.biSizeImage, fp);
    fclose(fp);

    for(i=0; i<(int)bmih.biHeight; i++)
    {
        for(j=0; j<byte; j++)
        {
            disp_byte(lx + j*8, by - i, (BYTE)(~(*bitmap)));
            bitmap++;
        }
        bitmap += reset;
    }
    
    free(bitmapBuff);
    return TRUE;
}

static BYTE cGrayTextMask[]={0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
void GUIAPI SetGrayTextMask()
{
    SetDisplayMask(cGrayTextMask);
}

static BYTE cDisplayMask[]={0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void GUIAPI SetDisplayMask(BYTE* cMask)
{
    memcpy(cDisplayMask, cMask, sizeof(BYTE)*8);
}

void GUIAPI RemoveDisplayMask()
{
    int i;
    for(i=0; i<8; i++)
        cDisplayMask[i] = 0xFF;
}

/*
 * Function: cc_wt16( LPSTR lpCStr, int x, int y )
 *       This Function display Chinese word with information in file.
 * Parameters:
 *       x,y: the coordination of display point.
 * Return:  None.
 */

void GUIAPI cc_wt16( LPCSTR lpCStr, short x, short y )
{
    BYTE KBuffer[32];
    BYTE bQM;
    BYTE bWM;
    BYTE byte;
    long lOffset;
    int i, j;
    int iCurIndex = 0;
    int iIndex = 0;
    LPSTR lpStr = NULL;
   
    i = j = 0;
    do
    {
        bQM = (BYTE)(*(lpCStr + i));
        bWM = (BYTE)(*(lpCStr + i + 1));
        
        if(bQM == '\0' || bQM == '\t' || bQM == '\n')
            break;
        
        if((bQM & 0x80) && (bWM & 0x80))    // This is a Chinese word
        {
            lOffset = ((bQM - 161)*94 + bWM - 161)*32L;
            
            iIndex = (int)(lOffset/61440L);
            if(lpStr == NULL || iCurIndex != iIndex)
            {
                iCurIndex = iIndex;
                lpStr = (LPSTR)_vload(CFontInfo.Handle[iIndex], _VM_CLEAN);
            }
            
            lOffset = lOffset%61440;
            _fmemcpy(KBuffer, lpStr + lOffset, 32);
            for(j=0; j<16; j++)

⌨️ 快捷键说明

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