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

📄 main.c

📁 使用mgui写的类似xwindow下的编辑器程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (MMessageDialog("", str, " Yes ", " Abort ", NULL) != 0)
        return;
    if ((buff = LoadFile(pfd->pathname, &(pfd->read_only))) == NULL)
        return;
    pfd->modified = False;
    MObjectSetText(pfd->modified_label, pfd->read_only ? "R" : " ");
    MObjectSetText(pfd->edit, buff);
    free(buff);
    FreeUndoList(pfd);
}

/*
 * This callback handles user's close request
 */
void 
CloseCB(MOBJECT p, void *od, void *ud)
{
    MOBJECT shell = (MOBJECT) ud;
    FILE_DATA *pfd = (FILE_DATA *) MObjectGetUserData(shell);
    char str[256];
    int ret, ii, kk, is_last;

    if (pfd && pfd->modified)
    {
        if (p != NULL && pfd->pathname[0] == '\0')
        {
            if (MMessageDialog("Warning", "This window content\nwill be lost !",
                               " Save ", " Don't save ", NULL) == 0)
                SaveAsCB(p, od, ud);
        }
        else if (pfd->pathname[0])
        {
            sprintf(str, "Save\n%s\nbefore exiting ?", pfd->pathname);
            ret = MMessageDialog("Warning",
                                 str, " Yes ", " No ", " Cancel ", NULL);
            if (ret == 0)
                SaveCB(p, od, ud);
            else if (ret == 2)
                return;
        }
    }
    pfd->filename[0] = '\0';
    pfd->pathname[0] = '\0';
    MObjectSetText(pfd->edit, "");
    MObjectSetText(pfd->fname_label, "");
    is_last = True;
    for (ii = kk = 0; ii < MAX_N_WINDOW; ii++)
        if (windows[ii] == shell)
        {
            kk = ii;
            windows[ii] = NULL;
        }
        else if (windows[ii] != NULL)
            is_last = False;
    if (is_last && p != NULL)
    {
/*              if (MMessageDialog("Warning",
   "This is last window!\nDo you want to quit ?",
   " Yes ", " No ", NULL)) {
   windows[kk] = shell;
   MObjectSetText(shell, MEDIT_TITLE);
   pfd->modified = False;
   MObjectSetText(pfd->modified_label, " ");
   return;
   }
 */ MGUITerm();
        exit(0);
    }
    if (pfd->search_text)
        free(pfd->search_text);
    if (pfd->replace_text)
        free(pfd->replace_text);
    free(pfd);
    MShellDestroy(shell);
}

/*
 * This function creates an empty new window
 */
static int
NewWindow(void)
{
    MOBJECT shell;
    int ii;

    for (ii = 0; ii < MAX_N_WINDOW; ii++)
        if (windows[ii] == NULL)
            break;
    if (ii == MAX_N_WINDOW)
        return -1;
    shell = MDCreatemain_shell();
    MObjectSetText(shell, MEDIT_TITLE);
    MShellSetWMCloseCallback(shell, CloseCB, shell);
    windows[ii] = shell;
    SetWindowOptions(shell);
    return ii;
}

/*
 * This callback handles user's open new file request
 */
void 
OpenNewCB(MOBJECT p, void *od, void *ad)
{
    MOBJECT shell;
    FILE_DATA *pfd;
    char fname[MAXFNAMELEN];
    char pname[MAXPATHLEN];
    char *buff;
    int ii;

    fname[0] = '\0';
    pname[0] = '\0';
    if (!MFileSelection("Open New File", EXT, fname, pname, True))
        return;
    ii = NewWindow();
    if (ii == -1)
    {
        char str[64];

        sprintf(str, "You cannot open\nmore than %d windows", MAX_N_WINDOW);
        MMessageDialog("Warning", str, " Ok ", NULL);
        return;
    }
    shell = windows[ii];
    MShellRealizeXY(shell, ii * 30, ii * 30);
    pfd = (FILE_DATA *) MObjectGetUserData(shell);
    strcpy(pfd->filename, fname);
    strcpy(pfd->pathname, pname);
    strcat(pfd->pathname, pfd->filename);
    if ((buff = LoadFile(pfd->pathname, &(pfd->read_only))) == NULL)
        return;
    MObjectSetSensitivity(pfd->edit, !pfd->read_only);
    pfd->modified = False;
    MObjectSetText(pfd->modified_label, pfd->read_only ? "R" : " ");
    MObjectSetText(pfd->fname_label, pfd->pathname);
    MObjectSetText(shell, pfd->filename);
    MObjectSetText(pfd->edit, buff);
    free(buff);
    FreeUndoList(pfd);
}

/*
 * This callback handles user's load file request
 */
void 
LoadFileCB(MOBJECT p, void *od, void *ad)
{
    MOBJECT shell = (MOBJECT) ad;
    FILE_DATA *pfd = (FILE_DATA *) MObjectGetUserData(shell);
    char str[256];
    char fname[MAXFNAMELEN];
    char pname[MAXPATHLEN];
    char *buff;
    int ret;

    if (pfd && pfd->modified)
    {
        if (p != NULL && pfd->pathname[0] == '\0')
        {
            if (MMessageDialog("Warning", "This window content\nwill be lost !",
                               " Save ", " Don't save ", NULL) == 0)
                SaveAsCB(p, od, ad);
        }
        else if (pfd->pathname[0])
        {
            sprintf(str, "Save current file:\n%s ?", pfd->pathname);
            ret = MMessageDialog("Warning",
                                 str, " Yes ", " No ", " Cancel ", NULL);
            if (ret == 0)
                SaveCB(p, od, ad);
            else if (ret == 2)
                return;
        }
    }
    fname[0] = '\0';
    pname[0] = '\0';
    if (!MFileSelection("Load File", EXT, fname, pname, True))
        return;
    strcpy(pfd->filename, fname);
    strcpy(pfd->pathname, pname);
    strcat(pfd->pathname, pfd->filename);
    if ((buff = LoadFile(pfd->pathname, &(pfd->read_only))) == NULL)
        return;
    MObjectSetSensitivity(pfd->edit, !pfd->read_only);
    pfd->modified = False;
    MObjectSetText(pfd->modified_label, pfd->read_only ? "R" : " ");
    MObjectSetText(pfd->fname_label, pfd->pathname);
    MObjectSetText(shell, pfd->filename);
    MObjectSetText(pfd->edit, buff);
    free(buff);
    FreeUndoList(pfd);
}

/*
 * This callback handles user's request to insert a file at
 * the current cursor position
 */
void 
InsertFileCB(MOBJECT p, void *od, void *ad)
{
    MOBJECT shell = (MOBJECT) ad;
    FILE_DATA *pfd = (FILE_DATA *) MObjectGetUserData(shell);
    char fname[MAXFNAMELEN];
    char pname[MAXPATHLEN];
    char *buff;
    int len, dummy;
    int curs_pos;

    fname[0] = '\0';
    pname[0] = '\0';
    if (!MFileSelection("Insert File", EXT, fname, pname, True))
        return;
    strcat(pname, fname);
    if ((buff = LoadFile(pname, &dummy)) == NULL)
        return;
    curs_pos = MEditGetCursorPos(pfd->edit);
    len = strlen(buff);
    MEditChangeText(pfd->edit, curs_pos, 0, buff, len);
    AddUndoData(pfd, NULL, curs_pos, 0, buff, len);
}

/*
 * This callback handles user's request to save
 * the selected text in to a file
 */
void 
WriteSelCB(MOBJECT p, void *od, void *ud)
{
    MOBJECT shell = (MOBJECT) ud;
    FILE_DATA *pfd = (FILE_DATA *) MObjectGetUserData(shell);
    FILE *fp;
    size_t start, end;
    char *buff;
    char str[256];
    char fname[MAXFNAMELEN];
    char pname[MAXPATHLEN];

    MEditGetSelection(pfd->edit, &start, &end);
    if (start >= end)
    {
        MMessageDialog("Warning", "No text selected!", "Ok", NULL);
        return;
    }
    if ((buff = malloc(end - start + 1)) == NULL)
    {
        MBeep();
        return;
    }
    MEditGetSubString(pfd->edit, start, end, buff);
    fname[0] = '\0';
    pname[0] = '\0';
    if (MFileSelection("Write Selection", EXT, fname, pname, True))
    {
        strcat(pname, fname);
        if (access(pname, R_OK) == 0 &&
            MMessageDialog("Warning", "File exists!\nOverwrite ?",
                           " Yes ", " No ", NULL) != 0)
        {
            free(buff);
            return;
        }
        if ((fp = fopen(pname, "w")) != NULL)
        {
            if (fwrite(buff, 1, end - start, fp) != end - start)
                MMessageDialog("Error", "Writing file", "Ok", NULL);
            fclose(fp);
        }
        else
        {
            sprintf(str, "Cannot open file:\n%s\nfor writing!", pname);
            MMessageDialog("Warning", str, " Ok ", NULL);
        }
    }
    free(buff);
}

/*
 * This callback handles user's request to print
 * the selected text or the entire text stream
 */
void 
PrintCB(MOBJECT p, void *od, void *ud)
{
    MOBJECT shell = (MOBJECT) ud;
    FILE_DATA *pfd = (FILE_DATA *) MObjectGetUserData(shell);
    FILE *fp;
    int start, end, ret;
    char *buff;
    char str[256];
    char fname[MAXFNAMELEN];

    switch (MMessageDialog("", "Print...", "File", "Selection", "Cancel", NULL))
    {
    case 2:
        return;
    case 1:
        MEditGetSelection(pfd->edit, &start, &end);
        if (start >= end)
        {
            MMessageDialog("Warning", "No text selected!", "Ok", NULL);
            return;
        }
        if ((buff = malloc(end - start + 1)) == NULL)
        {
            MBeep();
            return;
        }
        MEditGetSubString(pfd->edit, start, end, buff);
        break;
    default:
        buff = MObjectGetText(pfd->edit, NULL);
        if (buff == NULL)
        {
            MBeep();
            return;
        }
        start = 0;
        end = strlen(buff);
    }
    strcpy(fname, "meXXXXXX");
    mktemp(fname);
    if ((fp = fopen(fname, "w")) != NULL)
    {
        fwrite(buff, 1, end - start, fp);
        fclose(fp);
        free(buff);
    }
    else
    {
        sprintf(str, "Cannot open file:\n%s\nfor writing!", fname);
        MMessageDialog("Warning", str, " Ok ", NULL);
        free(buff);
        return;
    }
    for (;;)
    {
        switch (MSpoolTempFile(fname))
        {
        case MSPOOL_NOT_READY:
            ret = MMessageDialog("Error", "Printer NOT READY", "Retry", "Cancel", NULL);
            if (ret == 0)
                continue;
            break;
        default:
            MMessageDialog("Error", "Print failed!", "Ok", NULL);
            break;
        case MSPOOL_OK:
            return;
        }
        unlink(fname);
        return;
    }
}

/*
 * This callback handles user's request to open an empty new window
 */
void 
NewCB(MOBJECT p, void *od, void *ad)
{
    int ii;

    ii = NewWindow();
    if (ii == -1)
    {
        char str[64];

        sprintf(str, "You cannot open\nmore than %d windows", MAX_N_WINDOW);
        MMessageDialog("Warning", str, " Ok ", NULL);
        return;
    }
    MShellRealizeXY(windows[ii], ii * 30, ii * 30);
}

/*
 * This callback handles user's quit request
 */
void 
QuitCB(MOBJECT p, void *od, void *ud)
{
    int ii;

    for (ii = 0; ii < MAX_N_WINDOW; ii++)
        if (windows[ii] != NULL)
            CloseCB(NULL, od, windows[ii]);
    for (ii = 0; ii < MAX_N_WINDOW; ii++)
        if (windows[ii] != NULL)
            return;
    MGUITerm();
#ifdef MBCS_SUPPORT
	_setmbcp(0);
#endif
    exit(0);
}

/*
 * Convert the name supplied by the user in the command line
 * in the complete pathname and the filename
 */
void 
FilePathName(char *name, char *fname, char *pname)
{
#ifdef UNIX
    if (*name == DIR_SEP)
#else
    if (*name == DIR_SEP || *name == '.' || name[1] == ':')
#endif
        strcpy(pname, name);
    else
        sprintf(pname, "%s%s", MGetCurrentDirectory(), name);
    strcpy(fname, strrchr(pname, DIR_SEP) + 1);
}

/*
 * Access point to M-Edit program
 */
void 
MGUIMain(int argc, char **argv)
{
    char *buff;
    FILE_DATA *pfd;

#if defined MBCS_SUPPORT && defined _WINDOWS
	_setmbcp(932);
#endif

    MEnableCustomizing();
    LoadPrefs();
    NewCB(NULL, NULL, NULL);
    pfd = MObjectGetUserData(windows[0]);
    if (argc > 1)
    {
        FilePathName(argv[1], pfd->filename, pfd->pathname);
        if (access(pfd->pathname, R_OK) == 0 &&
            (buff = LoadFile(pfd->pathname, &(pfd->read_only))) != NULL)
        {
            MObjectSetSensitivity(pfd->edit, !pfd->read_only);
            MObjectSetText(pfd->modified_label,
                           pfd->read_only ? "R" : " ");
            MObjectSetText(pfd->edit, buff);
            free(buff);
        }
        pfd->modified = False;
        MObjectSetText(pfd->fname_label, pfd->pathname);
        MObjectSetText(windows[0], pfd->filename);
    }
    else
        MObjectSetText(pfd->fname_label, MEDIT_COPY);
    MMainLoop();
}

⌨️ 快捷键说明

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