src/video/windows/SDL_windowsmessagebox.c
changeset 11910 9947d9f539e8
parent 11909 b2e68bf41993
child 11911 235c5929651c
     1.1 --- a/src/video/windows/SDL_windowsmessagebox.c	Wed Feb 28 01:54:22 2018 -0500
     1.2 +++ b/src/video/windows/SDL_windowsmessagebox.c	Wed Feb 28 02:14:15 2018 -0500
     1.3 @@ -341,7 +341,7 @@
     1.4      return dialog;
     1.5  }
     1.6  
     1.7 -/* This function is called if a Task Dialog is unsupported. */
     1.8 +/* This function is called if a Task Dialog is unsupported. */
     1.9  static int
    1.10  WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    1.11  {
    1.12 @@ -492,119 +492,119 @@
    1.13      return 0;
    1.14  }
    1.15  
    1.16 -/* TaskDialogIndirect procedure
    1.17 - * This is because SDL targets Windows XP (0x501), so this is not defined in the platform SDK.
    1.18 - */
    1.19 -typedef HRESULT(FAR WINAPI *TASKDIALOGINDIRECTPROC)(_In_ const TASKDIALOGCONFIG *pTaskConfig, _Out_opt_ int *pnButton, _Out_opt_ int *pnRadioButton, _Out_opt_ BOOL *pfVerificationFlagChecked);
    1.20 -
    1.21 -int
    1.22 -WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
    1.23 -{
    1.24 -    HWND ParentWindow = NULL;
    1.25 -    wchar_t *wmessage;
    1.26 -    wchar_t *wtitle;
    1.27 -    TASKDIALOGCONFIG TaskConfig;
    1.28 -    TASKDIALOG_BUTTON *pButtons;
    1.29 -    TASKDIALOG_BUTTON *pButton;
    1.30 -    HMODULE hComctl32;
    1.31 -    TASKDIALOGINDIRECTPROC pfnTaskDialogIndirect;
    1.32 -    HRESULT hr;
    1.33 -    int nButton;
    1.34 -    int nCancelButton;
    1.35 -    int i;
    1.36 -
    1.37 -    /* If we cannot load comctl32.dll use the old messagebox! */
    1.38 -    hComctl32 = LoadLibrary(TEXT("Comctl32.dll"));
    1.39 -    if (hComctl32 == NULL) {
    1.40 -        return WIN_ShowOldMessageBox(messageboxdata,buttonid);
    1.41 -    }
    1.42 -    
    1.43 -    /* If TaskDialogIndirect doesn't exist use the old messagebox!
    1.44 -       This will fail prior to Windows Vista.
    1.45 -       The manifest file in the application may require targeting version 6 of comctl32.dll, even
    1.46 -       when we use LoadLibrary here!
    1.47 -       If you don't want to bother with manifests, put this #pragma in your app's source code somewhere:
    1.48 -       pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0'  processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    1.49 -     */
    1.50 -    pfnTaskDialogIndirect = (TASKDIALOGINDIRECTPROC) GetProcAddress(hComctl32, "TaskDialogIndirect");
    1.51 -    if (pfnTaskDialogIndirect == NULL) {
    1.52 -        FreeLibrary(hComctl32);
    1.53 -        return WIN_ShowOldMessageBox(messageboxdata, buttonid);
    1.54 -    }
    1.55 -
    1.56 -    /* If we have a parent window, get the Instance and HWND for them
    1.57 -       so that our little dialog gets exclusive focus at all times. */
    1.58 -    if (messageboxdata->window) {
    1.59 -        ParentWindow = ((SDL_WindowData *) messageboxdata->window->driverdata)->hwnd;
    1.60 -    }
    1.61 -
    1.62 -    wmessage = WIN_UTF8ToString(messageboxdata->message);
    1.63 -    wtitle = WIN_UTF8ToString(messageboxdata->title);
    1.64 -
    1.65 -    SDL_zero(TaskConfig);
    1.66 -    TaskConfig.cbSize = sizeof (TASKDIALOGCONFIG);
    1.67 -    TaskConfig.hwndParent = ParentWindow;
    1.68 -    TaskConfig.dwFlags = TDF_SIZE_TO_CONTENT;
    1.69 -    TaskConfig.pszWindowTitle = wtitle;
    1.70 -    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
    1.71 -        TaskConfig.pszMainIcon = TD_ERROR_ICON;
    1.72 -    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
    1.73 -        TaskConfig.pszMainIcon = TD_WARNING_ICON;
    1.74 -    } else if (messageboxdata->flags & SDL_MESSAGEBOX_INFORMATION) {
    1.75 -        TaskConfig.pszMainIcon = TD_INFORMATION_ICON;
    1.76 -    } else {
    1.77 -        TaskConfig.pszMainIcon = NULL;
    1.78 -    }
    1.79 -
    1.80 -    TaskConfig.pszContent = wmessage;
    1.81 -    TaskConfig.cButtons = messageboxdata->numbuttons;
    1.82 -    pButtons = SDL_malloc(sizeof (TASKDIALOG_BUTTON) * messageboxdata->numbuttons);
    1.83 -    TaskConfig.nDefaultButton = 0;
    1.84 -    for (i = 0; i < messageboxdata->numbuttons; i++)
    1.85 -    {
    1.86 -        pButton = &pButtons[messageboxdata->numbuttons-1-i];
    1.87 -        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
    1.88 -            nCancelButton = messageboxdata->buttons[i].buttonid;
    1.89 -            pButton->nButtonID = 2;
    1.90 -        } else {
    1.91 -            pButton->nButtonID = messageboxdata->buttons[i].buttonid + 1;
    1.92 -            if (pButton->nButtonID >= 2) {
    1.93 -                pButton->nButtonID++;
    1.94 -            }
    1.95 -        }
    1.96 -        pButton->pszButtonText = WIN_UTF8ToString(messageboxdata->buttons[i].text);
    1.97 -        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
    1.98 -            TaskConfig.nDefaultButton = pButton->nButtonID;
    1.99 -        }
   1.100 -    }
   1.101 -    TaskConfig.pButtons = pButtons;
   1.102 -
   1.103 -    /* Show the Task Dialog */
   1.104 -    hr = pfnTaskDialogIndirect(&TaskConfig, &nButton, NULL, NULL);
   1.105 -
   1.106 -    /* Free everything */
   1.107 -    FreeLibrary(hComctl32);
   1.108 -    SDL_free(wmessage);
   1.109 -    SDL_free(wtitle);
   1.110 -    for (i = 0; i < messageboxdata->numbuttons; i++) {
   1.111 -        SDL_free((wchar_t *) pButtons[i].pszButtonText);
   1.112 -    }
   1.113 -    SDL_free(pButtons);
   1.114 -
   1.115 -    /* Check the Task Dialog was successful and give the result */
   1.116 -    if (SUCCEEDED(hr)) {
   1.117 -        if (nButton == 2) {
   1.118 -            *buttonid = nCancelButton;
   1.119 -        } else if (nButton > 2) {
   1.120 -            *buttonid = nButton-1-1;
   1.121 -        } else {
   1.122 -            *buttonid = nButton-1;
   1.123 -        }
   1.124 -        return 0;
   1.125 -    }
   1.126 -
   1.127 -    /* We failed showing the Task Dialog, use the old message box! */
   1.128 -    return WIN_ShowOldMessageBox(messageboxdata, buttonid);
   1.129 +/* TaskDialogIndirect procedure
   1.130 + * This is because SDL targets Windows XP (0x501), so this is not defined in the platform SDK.
   1.131 + */
   1.132 +typedef HRESULT(FAR WINAPI *TASKDIALOGINDIRECTPROC)(const TASKDIALOGCONFIG *pTaskConfig, int *pnButton, int *pnRadioButton, BOOL *pfVerificationFlagChecked);
   1.133 +
   1.134 +int
   1.135 +WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
   1.136 +{
   1.137 +    HWND ParentWindow = NULL;
   1.138 +    wchar_t *wmessage;
   1.139 +    wchar_t *wtitle;
   1.140 +    TASKDIALOGCONFIG TaskConfig;
   1.141 +    TASKDIALOG_BUTTON *pButtons;
   1.142 +    TASKDIALOG_BUTTON *pButton;
   1.143 +    HMODULE hComctl32;
   1.144 +    TASKDIALOGINDIRECTPROC pfnTaskDialogIndirect;
   1.145 +    HRESULT hr;
   1.146 +    int nButton;
   1.147 +    int nCancelButton;
   1.148 +    int i;
   1.149 +
   1.150 +    /* If we cannot load comctl32.dll use the old messagebox! */
   1.151 +    hComctl32 = LoadLibrary(TEXT("Comctl32.dll"));
   1.152 +    if (hComctl32 == NULL) {
   1.153 +        return WIN_ShowOldMessageBox(messageboxdata,buttonid);
   1.154 +    }
   1.155 +    
   1.156 +    /* If TaskDialogIndirect doesn't exist use the old messagebox!
   1.157 +       This will fail prior to Windows Vista.
   1.158 +       The manifest file in the application may require targeting version 6 of comctl32.dll, even
   1.159 +       when we use LoadLibrary here!
   1.160 +       If you don't want to bother with manifests, put this #pragma in your app's source code somewhere:
   1.161 +       pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0'  processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
   1.162 +     */
   1.163 +    pfnTaskDialogIndirect = (TASKDIALOGINDIRECTPROC) GetProcAddress(hComctl32, "TaskDialogIndirect");
   1.164 +    if (pfnTaskDialogIndirect == NULL) {
   1.165 +        FreeLibrary(hComctl32);
   1.166 +        return WIN_ShowOldMessageBox(messageboxdata, buttonid);
   1.167 +    }
   1.168 +
   1.169 +    /* If we have a parent window, get the Instance and HWND for them
   1.170 +       so that our little dialog gets exclusive focus at all times. */
   1.171 +    if (messageboxdata->window) {
   1.172 +        ParentWindow = ((SDL_WindowData *) messageboxdata->window->driverdata)->hwnd;
   1.173 +    }
   1.174 +
   1.175 +    wmessage = WIN_UTF8ToString(messageboxdata->message);
   1.176 +    wtitle = WIN_UTF8ToString(messageboxdata->title);
   1.177 +
   1.178 +    SDL_zero(TaskConfig);
   1.179 +    TaskConfig.cbSize = sizeof (TASKDIALOGCONFIG);
   1.180 +    TaskConfig.hwndParent = ParentWindow;
   1.181 +    TaskConfig.dwFlags = TDF_SIZE_TO_CONTENT;
   1.182 +    TaskConfig.pszWindowTitle = wtitle;
   1.183 +    if (messageboxdata->flags & SDL_MESSAGEBOX_ERROR) {
   1.184 +        TaskConfig.pszMainIcon = TD_ERROR_ICON;
   1.185 +    } else if (messageboxdata->flags & SDL_MESSAGEBOX_WARNING) {
   1.186 +        TaskConfig.pszMainIcon = TD_WARNING_ICON;
   1.187 +    } else if (messageboxdata->flags & SDL_MESSAGEBOX_INFORMATION) {
   1.188 +        TaskConfig.pszMainIcon = TD_INFORMATION_ICON;
   1.189 +    } else {
   1.190 +        TaskConfig.pszMainIcon = NULL;
   1.191 +    }
   1.192 +
   1.193 +    TaskConfig.pszContent = wmessage;
   1.194 +    TaskConfig.cButtons = messageboxdata->numbuttons;
   1.195 +    pButtons = SDL_malloc(sizeof (TASKDIALOG_BUTTON) * messageboxdata->numbuttons);
   1.196 +    TaskConfig.nDefaultButton = 0;
   1.197 +    for (i = 0; i < messageboxdata->numbuttons; i++)
   1.198 +    {
   1.199 +        pButton = &pButtons[messageboxdata->numbuttons-1-i];
   1.200 +        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT) {
   1.201 +            nCancelButton = messageboxdata->buttons[i].buttonid;
   1.202 +            pButton->nButtonID = 2;
   1.203 +        } else {
   1.204 +            pButton->nButtonID = messageboxdata->buttons[i].buttonid + 1;
   1.205 +            if (pButton->nButtonID >= 2) {
   1.206 +                pButton->nButtonID++;
   1.207 +            }
   1.208 +        }
   1.209 +        pButton->pszButtonText = WIN_UTF8ToString(messageboxdata->buttons[i].text);
   1.210 +        if (messageboxdata->buttons[i].flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT) {
   1.211 +            TaskConfig.nDefaultButton = pButton->nButtonID;
   1.212 +        }
   1.213 +    }
   1.214 +    TaskConfig.pButtons = pButtons;
   1.215 +
   1.216 +    /* Show the Task Dialog */
   1.217 +    hr = pfnTaskDialogIndirect(&TaskConfig, &nButton, NULL, NULL);
   1.218 +
   1.219 +    /* Free everything */
   1.220 +    FreeLibrary(hComctl32);
   1.221 +    SDL_free(wmessage);
   1.222 +    SDL_free(wtitle);
   1.223 +    for (i = 0; i < messageboxdata->numbuttons; i++) {
   1.224 +        SDL_free((wchar_t *) pButtons[i].pszButtonText);
   1.225 +    }
   1.226 +    SDL_free(pButtons);
   1.227 +
   1.228 +    /* Check the Task Dialog was successful and give the result */
   1.229 +    if (SUCCEEDED(hr)) {
   1.230 +        if (nButton == 2) {
   1.231 +            *buttonid = nCancelButton;
   1.232 +        } else if (nButton > 2) {
   1.233 +            *buttonid = nButton-1-1;
   1.234 +        } else {
   1.235 +            *buttonid = nButton-1;
   1.236 +        }
   1.237 +        return 0;
   1.238 +    }
   1.239 +
   1.240 +    /* We failed showing the Task Dialog, use the old message box! */
   1.241 +    return WIN_ShowOldMessageBox(messageboxdata, buttonid);
   1.242  }
   1.243  
   1.244  #endif /* SDL_VIDEO_DRIVER_WINDOWS */