added functions for localized dialog, string and icon resources

pull/1/head
Heiko Hund 16 years ago
parent b25d0f268a
commit 1ea8d5b8f7

@ -1,137 +1,232 @@
/* /*
* OpenVPN-GUI -- A Windows GUI for OpenVPN. * OpenVPN-GUI -- A Windows GUI for OpenVPN.
* *
* Copyright (C) 2009 Heiko Hund <heikoh@users.sf.net> * Copyright (C) 2009 Heiko Hund <heikoh@users.sf.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this * along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc., * distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include <windows.h> /* for GetUserDefaultUILanguage() */
#include <tchar.h> #include <w32api.h>
#define WINVER Windows2000
static const LANGID defaultLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);
#include <windows.h>
static HRSRC #include <tchar.h>
FindResourceLang(HINSTANCE instance, PTSTR resType, PTSTR resId, LANGID langId) #include <stdio.h>
{ #include <stdarg.h>
HRSRC res; #include "registry.h"
/* try to find the resource in requested language */ static const LANGID defaultLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);
res = FindResourceEx(instance, resType, resId, langId);
if (res) static HRSRC
return res; FindResourceLang(HINSTANCE instance, PTSTR resType, PTSTR resId, LANGID langId)
{
/* try to find the resource in the default language */ HRSRC res;
res = FindResourceEx(instance, resType, resId, defaultLangId);
if (res) /* try to find the resource in requested language */
return res; res = FindResourceEx(instance, resType, resId, langId);
if (res)
/* try to find the resource in any language */ return res;
return FindResource(instance, resId, resType);
} /* try to find the resource in the default language */
res = FindResourceEx(instance, resType, resId, defaultLangId);
if (res)
int return res;
LoadStringLang(HINSTANCE instance, UINT stringId, LANGID langId, PTSTR buffer, int bufferSize)
{ /* try to find the resource in any language */
PWCH entry; return FindResource(instance, resId, resType);
PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1); }
int resIndex = stringId & 15;
/* find resource block for string */ static LANGID
HRSRC res = FindResourceLang(instance, RT_STRING, resBlockId, langId); GetGUILanguage(void)
if (res == NULL) {
return 0; return GetUserDefaultUILanguage();
}
/* get pointer to first entry in resource block */
entry = (PWCH) LoadResource(instance, res);
if (entry == NULL) static int
return 0; LoadStringLang(HINSTANCE instance, UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args)
{
/* search for string in block */ PWCH entry;
for (int i = 0; i < 16; i++) PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1);
{ int resIndex = stringId & 15;
/* string found, copy it */
if (i == resIndex && *entry) /* find resource block for string */
{ HRSRC res = FindResourceLang(instance, RT_STRING, resBlockId, langId);
int maxLen = (*entry < bufferSize ? *entry : bufferSize); if (res == NULL)
#ifdef _UNICODE return 0;
wcsncpy(buffer, entry + 1, maxLen);
#else /* get pointer to first entry in resource block */
WideCharToMultiByte(CP_ACP, 0, entry + 1, *entry, buffer, maxLen, "?", NULL); entry = (PWCH) LoadResource(instance, res);
#endif if (entry == NULL)
buffer[bufferSize - 1] = 0; return 0;
return _tcslen(buffer);
} /* search for string in block */
for (int i = 0; i < 16; i++)
/* string does not exist */ {
if (i == resIndex) /* skip over this entry */
break; if (i != resIndex)
{
/* skip over this entry */ entry += ((*entry) + 1);
entry += ((*entry) + 1); continue;
} }
return 0; /* string does not exist */
} if (i == resIndex && *entry == 0)
break;
HICON /* string found, copy it */
LoadIconLang(HINSTANCE instance, PTSTR iconId, LANGID langId) PTSTR formatStr = (PTSTR) malloc((*entry + 1) * sizeof(TCHAR));
{ if (formatStr == NULL)
/* find group icon resource */ break;
HRSRC res = FindResourceLang(instance, RT_GROUP_ICON, iconId, langId); formatStr[*entry] = 0;
if (res == NULL)
return NULL; #ifdef _UNICODE
wcsncpy(formatStr, entry + 1, *entry);
HGLOBAL resInfo = LoadResource(instance, res); #else
if (resInfo == NULL) WideCharToMultiByte(CP_ACP, 0, entry + 1, *entry, formatStr, *entry, "?", NULL);
return NULL; #endif
int id = LookupIconIdFromDirectory(resInfo, TRUE); _vsntprintf(buffer, bufferSize, formatStr, args);
if (id == 0) buffer[bufferSize - 1] = 0;
return NULL; free(formatStr);
return _tcslen(buffer);
/* find the actual icon */ }
res = FindResourceLang(instance, RT_ICON, MAKEINTRESOURCE(id), langId);
if (res == NULL) return 0;
return NULL; }
resInfo = LoadResource(instance, res);
if (resInfo == NULL) static PTSTR
return NULL; __LoadLocalizedString(const UINT stringId, va_list args)
{
DWORD resSize = SizeofResource(instance, res); static TCHAR msg[512];
if (resSize == 0) msg[0] = 0;
return NULL; LoadStringLang(GetModuleHandle(NULL), stringId, GetGUILanguage(), msg, sizeof(msg)/sizeof(*msg), args);
return msg;
return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000); }
}
INT_PTR PTSTR
DialogBoxLang(HINSTANCE instance, PTSTR dialogId, LANGID langId, HWND parentWnd, DLGPROC dialogFunc) LoadLocalizedString(const UINT stringId, ...)
{ {
/* find dialog resource */ va_list args;
HRSRC res = FindResourceLang(instance, RT_DIALOG, dialogId, langId); va_start(args, stringId);
if (res == NULL) PTSTR str = __LoadLocalizedString(stringId, args);
return -1; va_end(args);
return str;
HGLOBAL resInfo = LoadResource(instance, res); }
if (resInfo == NULL)
return -1;
int
return DialogBoxIndirect(instance, resInfo, parentWnd, dialogFunc); LoadLocalizedStringBuf(PTSTR buffer, int bufferSize, const UINT stringId, ...)
} {
va_list args;
va_start(args, stringId);
int len = LoadStringLang(GetModuleHandle(NULL), stringId, GetGUILanguage(), buffer, bufferSize, args);
va_end(args);
return len;
}
void
ShowLocalizedMsg(const PTSTR caption, const UINT stringId, ...)
{
va_list args;
va_start(args, stringId);
MessageBox(NULL, __LoadLocalizedString(stringId, args), caption, MB_OK | MB_SETFOREGROUND);
va_end(args);
}
HICON
LoadLocalizedIcon(const UINT iconId)
{
HINSTANCE instance = GetModuleHandle(NULL);
LANGID langId = GetGUILanguage();
/* find group icon resource */
HRSRC res = FindResourceLang(instance, RT_GROUP_ICON, MAKEINTRESOURCE(iconId), langId);
if (res == NULL)
return NULL;
HGLOBAL resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return NULL;
int id = LookupIconIdFromDirectory(resInfo, TRUE);
if (id == 0)
return NULL;
/* find the actual icon */
res = FindResourceLang(instance, RT_ICON, MAKEINTRESOURCE(id), langId);
if (res == NULL)
return NULL;
resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return NULL;
DWORD resSize = SizeofResource(instance, res);
if (resSize == 0)
return NULL;
return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000);
}
INT_PTR
LocalizedDialogBoxParam(const UINT dialogId, DLGPROC dialogFunc, const LPARAM param)
{
HINSTANCE instance = GetModuleHandle(NULL);
/* find dialog resource */
HRSRC res = FindResourceLang(instance, RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
if (res == NULL)
return -1;
HGLOBAL resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return -1;
return DialogBoxIndirectParam(instance, resInfo, NULL, dialogFunc, param);
}
INT_PTR
LocalizedDialogBox(const UINT dialogId, DLGPROC dialogFunc)
{
return LocalizedDialogBoxParam(dialogId, dialogFunc, 0);
}
HWND
CreateLocalizedDialog(const UINT dialogId, DLGPROC dialogFunc)
{
HINSTANCE instance = GetModuleHandle(NULL);
/* find dialog resource */
HRSRC res = FindResourceLang(instance, RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
if (res == NULL)
return NULL;
HGLOBAL resInfo = LoadResource(instance, res);
if (resInfo == NULL)
return NULL;
return CreateDialogIndirect(instance, resInfo, NULL, dialogFunc);
}

@ -1,24 +1,33 @@
/* /*
* OpenVPN-GUI -- A Windows GUI for OpenVPN. * OpenVPN-GUI -- A Windows GUI for OpenVPN.
* *
* Copyright (C) 2009 Heiko Hund <heikoh@users.sf.net> * Copyright (C) 2009 Heiko Hund <heikoh@users.sf.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING included with this * along with this program (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc., * distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
int LoadStringLang(HINSTANCE, UINT, LANGID, PTSTR, int); #ifndef LOCALIZATION_H
HICON LoadIconLang(HINSTANCE, PTSTR, LANGID); #define LOCALIZATION_H
INT_PTR DialogBoxLang(HINSTANCE, LPCTSTR, LANGID, HWND, DLGPROC);
PTSTR LoadLocalizedString(const UINT, ...);
int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...);
void ShowLocalizedMsg(const PTSTR, const UINT, ...);
HICON LoadLocalizedIcon(const UINT);
INT_PTR LocalizedDialogBoxParam(const UINT, DLGPROC, const LPARAM);
INT_PTR LocalizedDialogBox(const UINT, DLGPROC);
HWND CreateLocalizedDialog(const UINT, DLGPROC);
#endif

Loading…
Cancel
Save