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

📄 helpbox.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
                *cp++ =
            (wnd->WindowColors [HILITE_COLOR] [FG] & 255) | 0x80;
                *cp++ =
            (wnd->WindowColors [HILITE_COLOR] [BG] & 255) | 0x80;
                cp1 = cp;
                if ((cp = strchr(cp, ']')) != NULL)    {
                    if (thisword != NULL)
                        thisword->off3 =
                            thisword->off2 + (int) (cp - cp1);
                    *cp++ = RESETCOLOR;
                }
                if ((cp = strchr(cp, '<')) != NULL)    {
                    char *cp1 = strchr(cp, '>');
                    if (cp1 != NULL)    {
                        int len = (int) ((int)cp1 - (int)cp);
                        thisword->hname = DFcalloc(1, len);
                        strncpy(thisword->hname, cp+1, len-1);
                        memmove(cp, cp1+1, strlen(cp1));
                    }
                }
            }
        }
        PutItemText(wnd, ID_HELPTEXT, hline);
        /* -- display help text as soon as window is full -- */
        if (++linectr == ClientHeight(cwnd))
            DfSendMessage(cwnd, PAINT, 0, 0);
        if (linectr > ClientHeight(cwnd) &&
                !TestAttribute(cwnd, VSCROLLBAR))    {
            AddAttribute(cwnd, VSCROLLBAR);
            DfSendMessage(cwnd, BORDER, 0, 0);
        }
    }
}

/* ---- compute the displayed length of a help text line --- */
static int HelpLength(char *s)
{
    int len = strlen(s);
    char *cp = strchr(s, '[');
    while (cp != NULL)    {
        len -= 4;
        cp = strchr(cp+1, '[');
    }
    cp = strchr(s, '<');
    while (cp != NULL)    {
        char *cp1 = strchr(cp, '>');
        if (cp1 != NULL)
            len -= (int) (cp1-cp)+1;
        cp = strchr(cp1, '<');
    }
    return len;
}

/* ----------- load the help text file ------------ */
void LoadHelpFile()
{
    char *cp;

    if (Helping)
        return;
    UnLoadHelpFile();
    if ((helpfp = OpenHelpFile()) == NULL)
        return;
    *hline = '\0';
    while (*hline != '<')    {
        if (GetHelpLine(hline) == NULL)    {
            fclose(helpfp);
            return;
        }
    }
    while (*hline == '<')   {
        if (strncmp(hline, "<end>", 5) == 0)
            break;

        /* -------- parse the help window's text name ----- */
        if ((cp = strchr(hline, '>')) != NULL)    {
            ThisHelp = DFcalloc(1, sizeof(struct helps));
            if (FirstHelp == NULL)
            FirstHelp = ThisHelp;
            *cp = '\0';
            ThisHelp->hname=DFmalloc(strlen(hline+1)+1);
            strcpy(ThisHelp->hname, hline+1);

            HelpFilePosition(&ThisHelp->hptr, &ThisHelp->bit);

            if (GetHelpLine(hline) == NULL)
                break;

            /* ------- build the help linked list entry --- */
            while (*hline == '[')    {
                HelpFilePosition(&ThisHelp->hptr,
                                            &ThisHelp->bit);
                /* ---- parse the <<prev button pointer ---- */
                if (strncmp(hline, "[<<]", 4) == 0)    {
                    char *cp = strchr(hline+4, '<');
                    if (cp != NULL)    {
                        char *cp1 = strchr(cp, '>');
                        if (cp1 != NULL)    {
                            int len = (int) (cp1-cp);
                            ThisHelp->PrevName=DFcalloc(1,len);
                            strncpy(ThisHelp->PrevName,
                                cp+1,len-1);
                        }
                    }
                    if (GetHelpLine(hline) == NULL)
                        break;
                    continue;
                }
                /* ---- parse the next>> button pointer ---- */
                else if (strncmp(hline, "[>>]", 4) == 0)    {
                    char *cp = strchr(hline+4, '<');
                    if (cp != NULL)    {
                        char *cp1 = strchr(cp, '>');
                        if (cp1 != NULL)    {
                            int len = (int) (cp1-cp);
                            ThisHelp->NextName=DFcalloc(1,len);
                            strncpy(ThisHelp->NextName,
                                            cp+1,len-1);
                        }
                    }
                    if (GetHelpLine(hline) == NULL)
                        break;
                    continue;
                }
                else
                    break;
            }
            ThisHelp->hheight = 0;
            ThisHelp->hwidth = 0;
            ThisHelp->NextHelp = NULL;

            /* ------ append entry to the linked list ------ */
            if (LastHelp != NULL)
                LastHelp->NextHelp = ThisHelp;
            LastHelp = ThisHelp;
        }
        /* -------- move to the next <helpname> token ------ */
        if (GetHelpLine(hline) == NULL)
            strcpy(hline, "<end>");
        while (*hline != '<')    {
            ThisHelp->hwidth =
                max(ThisHelp->hwidth, HelpLength(hline));
            ThisHelp->hheight++;
            if (GetHelpLine(hline) == NULL)
                strcpy(hline, "<end>");
        }
    }
    fclose(helpfp);
}

/* ------ free the memory used by the help file table ------ */
void UnLoadHelpFile(void)
{
    while (FirstHelp != NULL)    {
        ThisHelp = FirstHelp;
        if (ThisHelp->hname != NULL)
            free(ThisHelp->hname);
        if (ThisHelp->PrevName != NULL)
            free(ThisHelp->PrevName);
        if (ThisHelp->NextName != NULL)
            free(ThisHelp->NextName);
        FirstHelp = ThisHelp->NextHelp;
        free(ThisHelp);
    }
    ThisHelp = LastHelp = NULL;
    free(HelpTree);
	HelpTree = NULL;
}

/* ---------- display a specified help text ----------- */
BOOL DisplayHelp(DFWINDOW wnd, char *Help)
{
	BOOL rtn = FALSE;
    if (Helping)
        return TRUE;
	wnd->isHelping++;
    FindHelp(Help);
    if (ThisHelp != NULL)    {
        if (LastStack == NULL ||
                stricmp(Help, LastStack->hname))    {
            /* ---- add the window to the history stack ---- */
            ThisStack = DFcalloc(1,sizeof(struct HelpStack));
            ThisStack->hname = DFmalloc(strlen(Help)+1);
            if (ThisStack->hname != NULL)
                strcpy(ThisStack->hname, Help);
            ThisStack->PrevStack = LastStack;
            LastStack = ThisStack;
        }
        if ((helpfp = OpenHelpFile()) != NULL)    {
            DBOX *db;
            int offset, i;

            db = DFcalloc(1,sizeof HelpBox);
            memcpy(db, &HelpBox, sizeof HelpBox);
            /* -- seek to the first line of the help text -- */
            SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
            /* ----- read the title ----- */
            GetHelpLine(hline);
            hline[strlen(hline)-1] = '\0';
            db->dwnd.title = DFmalloc(strlen(hline)+1);
            strcpy(db->dwnd.title, hline);
            /* ----- set the height and width ----- */
            db->dwnd.h = min(ThisHelp->hheight, MAXHEIGHT)+7;
            db->dwnd.w = max(45, ThisHelp->hwidth+6);
            /* ------ position the help window ----- */
            BestFit(wnd, &db->dwnd);
            /* ------- position the command buttons ------ */
            db->ctl[0].dwnd.w = max(40, ThisHelp->hwidth+2);
            db->ctl[0].dwnd.h =
                        min(ThisHelp->hheight, MAXHEIGHT)+2;
            offset = (db->dwnd.w-40) / 2;
            for (i = 1; i < 5; i++)    {
                db->ctl[i].dwnd.y =
                        min(ThisHelp->hheight, MAXHEIGHT)+3;
                db->ctl[i].dwnd.x += offset;
            }
            /* ---- disable ineffective buttons ---- */
            if (ThisStack != NULL)
                if (ThisStack->PrevStack == NULL)
                    DisableButton(db, ID_BACK);
            if (ThisHelp->NextName == NULL)
                DisableButton(db, ID_NEXT);
            if (ThisHelp->PrevName == NULL)
                DisableButton(db, ID_PREV);
            /* ------- display the help window ----- */
            DfDialogBox(NULL, db, TRUE, HelpBoxProc);
            free(db);
            fclose(helpfp);
            rtn = TRUE;
        }
    }
	--wnd->isHelping;
    return rtn;
}

/* ------- display a definition window --------- */
static void DisplayDefinition(DFWINDOW wnd, char *def)
{
    DFWINDOW dwnd;
    DFWINDOW hwnd = wnd;
    int y;

    if (GetClass(wnd) == POPDOWNMENU)
        hwnd = GetParent(wnd);
    y = GetClass(hwnd) == MENUBAR ? 2 : 1;
    FindHelp(def);
    if (ThisHelp != NULL)    {
        if ((helpfp = OpenHelpFile()) != NULL)    {
            dwnd = DfCreateWindow(
                        TEXTBOX,
                        NULL,
                        GetClientLeft(hwnd),
                        GetClientTop(hwnd)+y,
                        min(ThisHelp->hheight, MAXHEIGHT)+3,
                        ThisHelp->hwidth+2,
                        NULL,
                        wnd,
                        NULL,
                        HASBORDER | NOCLIP | SAVESELF);
            if (dwnd != NULL)    {
                /* ----- read the help text ------- */
                SeekHelpLine(ThisHelp->hptr, ThisHelp->bit);
                while (TRUE)    {
                    if (GetHelpLine(hline) == NULL)
                        break;
                    if (*hline == '<')
                        break;
                    hline[strlen(hline)-1] = '\0';
                    DfSendMessage(dwnd,ADDTEXT,(PARAM)hline,0);
                }
                DfSendMessage(dwnd, SHOW_WINDOW, 0, 0);
                DfSendMessage(NULL, WAITKEYBOARD, 0, 0);
                DfSendMessage(NULL, WAITMOUSE, 0, 0);
                DfSendMessage(dwnd, CLOSE_WINDOW, 0, 0);
            }
            fclose(helpfp);
        }
    }
}

/* ------ compare help names with wild cards ----- */
static BOOL wildcmp(char *s1, char *s2)
{
    while (*s1 || *s2)    {
        if (tolower(*s1) != tolower(*s2))
            if (*s1 != '?' && *s2 != '?')
                return TRUE;
        s1++, s2++;
    }
    return FALSE;
}

/* --- ThisHelp = the help window matching specified name --- */
static void FindHelp(char *Help)
{
    ThisHelp = FirstHelp;
    while (ThisHelp != NULL)    {
        if (wildcmp(Help, ThisHelp->hname) == FALSE)
            break;
        ThisHelp = ThisHelp->NextHelp;
    }
}

/* --- ThisHelp = the help window matching specified wnd --- */
static void FindHelpWindow(DFWINDOW wnd)
{
    ThisHelp = FirstHelp;
    while (ThisHelp != NULL)    {
        if (wnd == ThisHelp->hwnd)
            break;
        ThisHelp = ThisHelp->NextHelp;
    }
}

static int OverLap(int a, int b)
{
    int ov = a - b;
    if (ov < 0)
        ov = 0;
    return ov;
}

/* ----- compute the best location for a help dialogbox ----- */
static void BestFit(DFWINDOW wnd, DIALOGWINDOW *dwnd)
{
    int above, below, right, left;
    if (GetClass(wnd) == MENUBAR ||
                GetClass(wnd) == APPLICATION)    {
        dwnd->x = dwnd->y = -1;
        return;
    }
    /* --- compute above overlap ---- */
    above = OverLap(dwnd->h, GetTop(wnd));
    /* --- compute below overlap ---- */
    below = OverLap(GetBottom(wnd), DfGetScreenHeight()-dwnd->h);
    /* --- compute right overlap ---- */
    right = OverLap(GetRight(wnd), DfGetScreenWidth()-dwnd->w);
    /* --- compute left  overlap ---- */
    left = OverLap(dwnd->w, GetLeft(wnd));

    if (above < below)
        dwnd->y = max(0, GetTop(wnd)-dwnd->h-2);
    else
        dwnd->y = min(DfGetScreenHeight()-dwnd->h, GetBottom(wnd)+2);
    if (right < left)
        dwnd->x = min(GetRight(wnd)+2, DfGetScreenWidth()-dwnd->w);
    else
        dwnd->x = max(0, GetLeft(wnd)-dwnd->w-2);

    if (dwnd->x == GetRight(wnd)+2 ||
            dwnd->x == GetLeft(wnd)-dwnd->w-2)
        dwnd->y = -1;
    if (dwnd->y ==GetTop(wnd)-dwnd->h-2 ||
            dwnd->y == GetBottom(wnd)+2)
        dwnd->x = -1;
}

/* EOF */

⌨️ 快捷键说明

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