sumatradialogs.cc.svn-base

来自「SumatraPDF是一款小型开源的pdf阅读工具。虽然玲珑小巧(只有800多K」· SVN-BASE 代码 · 共 456 行 · 第 1/2 页

SVN-BASE
456
字号
/* Copyright Krzysztof Kowalczyk 2006-2007
   License: GPLv2 */
#include <windows.h>
#include <assert.h>

#include "SumatraPDF.h"
#include "SumatraDialogs.h"

#include "DisplayModel.h"
#include "dstring.h"
#include "Resource.h"
#include "win_util.h"
#include "dialogsizer.h"
#include "LangMenuDef.h"
#include "utf_util.h"
#include "translations.h"
#include "wstr_util.h"

typedef struct {
    const char *  in_cmdline;   /* current inverse search command line */
    char *  out_cmdline;         /* inverse search command line selected by the user */
} Dialog_InverseSearch_Data;

#ifdef _TEX_ENHANCEMENT
static BOOL CALLBACK Dialog_InverseSearch_Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND                       edit;
    Dialog_InverseSearch_Data *  data;

    if (WM_INITDIALOG == message)
    {
        data = (Dialog_InverseSearch_Data*)lParam;
        assert(data);
        assert(data->in_cmdline);
        SetWindowLongPtr(hDlg, GWL_USERDATA, (LONG_PTR)data);
        SetDlgItemText(hDlg, IDC_CMDLINE, data->in_cmdline);
        edit = GetDlgItem(hDlg, IDC_CMDLINE);
        SetFocus(edit);
        return FALSE;
    }


    switch (message)
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    data = (Dialog_InverseSearch_Data*)GetWindowLongPtr(hDlg, GWL_USERDATA);
                    assert(data);
                    edit = GetDlgItem(hDlg, IDC_CMDLINE);
                    data->out_cmdline = win_get_text(edit);
                    EndDialog(hDlg, DIALOG_OK_PRESSED);
                    return TRUE;

                case IDCANCEL:
                    EndDialog(hDlg, DIALOG_CANCEL_PRESSED);
                    return TRUE;
            }
            break;
    }
    return FALSE;
}

/* Shows a dialog that let the user configure the inverser search command line
   Returns the command line as a newly allocated string or
   NULL if user cancelled the dialog or there was an error.
   Caller needs to free() the result.
*/
char *Dialog_SetInverseSearchCmdline(WindowInfo *win, const char *cmdline)
{
    int                     dialogResult;
    Dialog_InverseSearch_Data data;
    
    assert(cmdline);
    if (!cmdline) return NULL;

    data.in_cmdline = cmdline;
    dialogResult = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DIALOG_INVERSESEARCH), win->hwndFrame, Dialog_InverseSearch_Proc, (LPARAM)&data);
    if (DIALOG_OK_PRESSED == dialogResult) {
        return data.out_cmdline;
    }
    return NULL;
}
#endif

/* For passing data to/from GetPassword dialog */
typedef struct {
    const WCHAR *  fileName;   /* name of the file for which we need the password */
    char *         pwdOut;     /* password entered by the user */
} Dialog_GetPassword_Data;

static BOOL CALLBACK Dialog_GetPassword_Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    Dialog_GetPassword_Data *  data;

    if (WM_INITDIALOG == message)
    {
        /* TODO: intelligently center the dialog within the parent window? */
        data = (Dialog_GetPassword_Data*)lParam;
        assert(data);
        assert(data->fileName);
        assert(!data->pwdOut);
        win_set_textw(hDlg, _TRW("Enter password"));
        SetWindowLongPtr(hDlg, GWL_USERDATA, (LONG_PTR)data);
        WCHAR *txt = wstr_printf(_TRW("Enter password for %s"), data->fileName);
        SetDlgItemTextW(hDlg, IDC_GET_PASSWORD_LABEL, txt);
        free(txt);
        SetDlgItemTextA(hDlg, IDC_GET_PASSWORD_EDIT, "");
        SetFocus(GetDlgItem(hDlg, IDC_GET_PASSWORD_EDIT));
        return FALSE;
    }

    switch (message)
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    data = (Dialog_GetPassword_Data*)GetWindowLongPtr(hDlg, GWL_USERDATA);
                    assert(data);
                    data->pwdOut = win_get_text(GetDlgItem(hDlg, IDC_GET_PASSWORD_EDIT));
                    EndDialog(hDlg, DIALOG_OK_PRESSED);
                    return TRUE;

                case IDCANCEL:
                    EndDialog(hDlg, DIALOG_CANCEL_PRESSED);
                    return TRUE;
            }
            break;
    }
    return FALSE;
}

/* Shows a 'get password' dialog for a given file.
   Returns a password entered by user as a newly allocated string or
   NULL if user cancelled the dialog or there was an error.
   Caller needs to free() the result.
*/
char *Dialog_GetPassword(WindowInfo *win, const WCHAR *fileName)
{
    int                     dialogResult;
    Dialog_GetPassword_Data data;
    
    assert(fileName);
    if (!fileName) return NULL;

    data.fileName = fileName;
    data.pwdOut = NULL;
    dialogResult = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DIALOG_GET_PASSWORD), win->hwndFrame, Dialog_GetPassword_Proc, (LPARAM)&data);
    if (DIALOG_OK_PRESSED == dialogResult) {
        return data.pwdOut;
    }
    free((void*)data.pwdOut);
    return NULL;
}

/* For passing data to/from GoToPage dialog */
typedef struct {
    int     currPageNo;      /* currently shown page number */
    int     pageCount;       /* total number of pages */
    int     pageEnteredOut;  /* page number entered by user */
} Dialog_GoToPage_Data;

static BOOL CALLBACK Dialog_GoToPage_Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HWND                    editPageNo;
    DString                 ds;
    TCHAR *                 newPageNoTxt;
    Dialog_GoToPage_Data *  data;

    if (WM_INITDIALOG == message)
    {
        /* TODO: intelligently center the dialog within the parent window? */
        data = (Dialog_GoToPage_Data*)lParam;
        assert(data);
        SetWindowLongPtr(hDlg, GWL_USERDATA, (LONG_PTR)data);
        assert(INVALID_PAGE_NO != data->currPageNo);
        assert(data->pageCount >= 1);
        win_set_textw(hDlg, _TRW("Go to page"));
        DStringInit(&ds);
        DStringSprintf(&ds, "%d", data->currPageNo);
        SetDlgItemTextA(hDlg, IDC_GOTO_PAGE_EDIT, ds.pString);
        DStringFree(&ds);
        DStringSprintf(&ds, "(of %d)", data->pageCount);
        SetDlgItemTextA(hDlg, IDC_GOTO_PAGE_LABEL_OF, ds.pString);
        DStringFree(&ds);
        editPageNo = GetDlgItem(hDlg, IDC_GOTO_PAGE_EDIT);
        win_edit_select_all(editPageNo);
        SetFocus(editPageNo);
        return FALSE;
    }

    switch (message)
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    data = (Dialog_GoToPage_Data*)GetWindowLongPtr(hDlg, GWL_USERDATA);
                    assert(data);
                    data->pageEnteredOut = INVALID_PAGE_NO;
                    editPageNo = GetDlgItem(hDlg, IDC_GOTO_PAGE_EDIT);
                    newPageNoTxt = win_get_text(editPageNo);
                    if (newPageNoTxt) {
                        data->pageEnteredOut = atoi(newPageNoTxt);
                        free((void*)newPageNoTxt);
                    }
                    EndDialog(hDlg, DIALOG_OK_PRESSED);
                    return TRUE;

                case IDCANCEL:
                    EndDialog(hDlg, DIALOG_CANCEL_PRESSED);
                    return TRUE;
            }
            break;
    }
    return FALSE;
}

/* Shows a 'go to page' dialog and returns a page number entered by the user
   or INVALID_PAGE_NO if user clicked "cancel" button, entered invalid
   page number or there was an error. */
int Dialog_GoToPage(WindowInfo *win)
{
    int                     dialogResult;
    Dialog_GoToPage_Data    data;
    

⌨️ 快捷键说明

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