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

📄 status.c

📁 dtelent是开源的开发项目
💻 C
字号:
/* status.c
 * Copyright (c) 1997 David Cole
 *
 * Control and manage the status bar
 */
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include "platform.h"
#include "term.h"
#include "emul.h"
#include "utils.h"
#include "status.h"

static char statusWinClass[] = "DStatusWClass";	/* statusbar window class name */
static int pad;			/* padding around the text in the bar */
static int aveCharWidth;	/* average width of font in status bar */
static HWND statusWnd;		/* status window handle */
static HFONT statusFont;	/* font to use */
static char statusMsg[200];	/* current status message */

static RECT messageRect;	/* window area used to show status messages */
static RECT termRect;		/* window area used to show terminal type */
static RECT sizeRect;		/* window area used to show terminal size */

#define PANE_PAD 2

/* Return the handle of the status bar
 */
HWND statusGetWnd()
{
    return statusWnd;
}

/* Format and display a status message
 *
 * Args:
 * fmt - printf format string
 * ... - arguments to format string
 */
void statusSetMsg(char* fmt, ...)
{    
    va_list ap;

    va_start(ap, fmt);
    vsprintf(statusMsg, fmt, ap);
    va_end(ap);
    if (statusWnd)
	InvalidateRect(statusWnd, NULL, TRUE);
}

/* Tell the status bar to redisplay the terminal type
 */
void statusSetTerm()
{
    statusRedraw();
}

/* The status bar has been resized.  Recalculate all of the drawing
 * areas.
 */
void statusRedraw()
{
    HDC dc;
    HFONT oldFont;
    RECT rect;			/* status bar window rectangle */
    SIZE width;
    char widthStr[40];

    if (statusWnd == 0)
	return;

    GetClientRect(statusWnd, &rect);
    rect.top += PANE_PAD;

    /* Recalculate the positions of the panes
     */
    dc = GetDC(statusWnd);
    oldFont = (HFONT)SelectObject(dc, statusFont);

    termFormatSize(widthStr);
    GetTextExtentPoint(dc, widthStr, strlen(widthStr), &width);
    sizeRect = rect;
    sizeRect.right = rect.right;
    sizeRect.left = sizeRect.right - width.cx - (2 * (pad + 2)) - (PANE_PAD * 2);

    strcpy(widthStr, emulGetTerm()->name);
    GetTextExtentPoint(dc, widthStr, strlen(widthStr), &width);
    termRect = rect;
    termRect.right = sizeRect.left - PANE_PAD;
    termRect.left = termRect.right - width.cx - (2 * (pad + 2)) - (PANE_PAD * 2);

    SelectObject(dc, oldFont);
    ReleaseDC(statusWnd, dc);

    messageRect = rect;
    messageRect.left = 0;
    messageRect.right = termRect.left - PANE_PAD;

    InvalidateRect(statusWnd, NULL, TRUE);
}

/* Redraw a single pane
 */
static void statusDrawPane(HDC dc, LPRECT rect, char *paneText)
{
    RECT rc;
    HPEN oldPen;
    HPEN darkPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
    HPEN lightPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNHIGHLIGHT));

    SetRect(&rc, rect->left, rect->top, rect->right-1, rect->bottom-1);
    oldPen = (HPEN) SelectObject(dc, darkPen);
    MoveTo(dc, rc.left, rc.bottom-1);
    LineTo(dc, rc.left, rc.top);
    LineTo(dc, rc.right, rc.top);
    SelectObject(dc, lightPen);
    MoveTo(dc, rc.right, rc.top);
    LineTo(dc, rc.right, rc.bottom);
    LineTo(dc, rc.left-1, rc.bottom);

    SetRect(&rc, rc.left + 1 + PANE_PAD, rc.top+1, rc.right-1, rc.bottom-1);
    DrawText(dc, paneText, strlen(paneText), &rc, DT_LEFT | DT_SINGLELINE | DT_VCENTER);

    SelectObject(dc, oldPen);
    DeleteObject(lightPen);
    DeleteObject(darkPen);
}

/* Redraw the status bar
 */
static void statusPaint(void)
{
    PAINTSTRUCT ps;
    HDC dc;
    HFONT oldFont;
    char sizeStr[40];

    /* Initialise device context for repainting status bar
     */
    dc = BeginPaint(statusWnd, &ps);
    SetBkColor(dc, GetSysColor(COLOR_BTNFACE));
    SetTextColor(dc, GetSysColor(COLOR_BTNTEXT));
    oldFont = (HFONT)SelectObject(dc, statusFont);

    if (RectVisible(dc, &messageRect)) {
	statusDrawPane(dc, &messageRect, statusMsg);
    }
    if (RectVisible(dc, &termRect)) {
	statusDrawPane(dc, &termRect, emulGetTerm()->name);
    }
    if (RectVisible(dc, &sizeRect)) {
	termFormatSize(sizeStr);
	statusDrawPane(dc, &sizeRect, sizeStr);
    }

    /* Clean up the device context
     */
    SelectObject(dc, oldFont);
    EndPaint(statusWnd, &ps);
}

/* Window procedure for the status bar
 */
long CALLBACK statusWndProc(HWND wnd,
			    UINT message, WPARAM wparam, LPARAM lparam)
{                                   
    switch (message) {
    case WM_PAINT:
	statusPaint();
	return 0;

    case WM_SIZE:
	statusRedraw();
	return 0;

    case WM_DESTROY:
	DeleteObject(statusFont);
	statusWnd = NULL;
	return 0;
    }

    return DefWindowProc(wnd, message, wparam, lparam);
}

/* Register the status bar window class
 */
BOOL statusInitClass(HINSTANCE instance)
{
    WNDCLASS wc;

    wc.style = 0;
    wc.lpfnWndProc = statusWndProc;
    wc.cbClsExtra = wc.cbWndExtra = 0;
    wc.hInstance = instance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    wc.lpszMenuName =  NULL;
    wc.lpszClassName = statusWinClass;
    return RegisterClass(&wc);
}

/* Create the status bar window
 *
 * Args:
 * instance - application instance handle
 * parent -   parent window (application window)
 */
BOOL statusCreateWnd(HINSTANCE instance, HWND parent)
{
    HDC dc;			/* calculate height via font metrics */
    TEXTMETRIC tm;
    LOGFONT lf;
    HFONT oldFont;
    RECT parentClient;		/* client rectangle of parent window */
    SIZE winSize;		/* calculate size of status bar */

    /* Work out the height of the status bar by querying the font that
     * will be used to draw in it
     */
    dc = GetDC(parent);

    /* Create the font that will be used to draw in the status bar
     */
    memset(&lf, 0, sizeof(lf));
    lf.lfHeight = -MulDiv(8, GetDeviceCaps(dc, LOGPIXELSY), 72);
    lf.lfWeight = FW_NORMAL;
    lf.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
    strcpy(lf.lfFaceName, "MS Sans Serif");
    statusFont = CreateFontIndirect(&lf);

    oldFont = (HFONT)SelectObject(dc, statusFont);
    GetTextMetrics(dc, &tm);
    SelectObject(dc, oldFont);

    ReleaseDC(parent, dc);

    /* Build some padding around the various elements of the status bar
     */
    pad = tm.tmExternalLeading + 1;
    aveCharWidth = tm.tmAveCharWidth;

    /* Create the status bar at the bottom of the parent window.
     */
    GetClientRect(parent, &parentClient);
    winSize.cx = parentClient.right;
    winSize.cy = (tm.tmHeight + 2 * pad) + (PANE_PAD * 2) + pad;
    statusWnd = CreateWindow(statusWinClass, "", WS_CHILD,
	  0, parentClient.bottom-winSize.cy, winSize.cx, winSize.cy,
	  parent, NULL, instance, NULL);
    if (statusWnd == NULL)
	return FALSE;

    /* Display the status bar
     */
    ShowWindow(statusWnd, SW_SHOW);
    UpdateWindow(statusWnd);
    return TRUE;
}

⌨️ 快捷键说明

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