From 746451bafa0eba6a2a8d94c3fe4dc79cd9de20d6 Mon Sep 17 00:00:00 2001 From: Heiko Hund Date: Fri, 16 Jan 2009 16:58:03 +0000 Subject: [PATCH] added functions to load localized resources --- Makefile.in | 10 ++-- localization.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++ localization.h | 23 ++++++++++ 3 files changed, 150 insertions(+), 5 deletions(-) create mode 100755 localization.c create mode 100755 localization.h diff --git a/Makefile.in b/Makefile.in index 51e4f8e..b40d62c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -14,13 +14,13 @@ CFLAGS += -W -Wall -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wbad-function -Wcast-align -Wwrite-strings -Wconversion -Wsign-compare \ -Waggregate-return -Wmissing-noreturn -Wmissing-format-attribute \ -Wredundant-decls -Winline -Wdisabled-optimization \ - -Wno-unused-function -Wno-unused-variable + -Wno-unused-function -Wno-unused-variable OBJS = main.o tray.o openvpn.o openvpn_monitor_process.o viewlog.o \ service.o options.o passphrase.o proxy.o ieproxy.o registry.o \ - openvpn_config.o scripts.o openvpn-gui-res.o + openvpn_config.o scripts.o localization.o openvpn-gui-res.o -SOURCES = main.c main.h openvpn.c openvpn.h \ +SOURCES = main.c main.h openvpn.c openvpn.h localization.c localization.h \ openvpn_monitor_process.c openvpn_monitor_process.h \ tray.c tray.h viewlog.c viewlog.h service.c service.h \ options.c options.h passphrase.c passphrase.h proxy.c proxy.h \ @@ -31,9 +31,9 @@ SOURCES = main.c main.h openvpn.c openvpn.h \ EXTRA = connected.ico connecting.ico disconnected.ico reconnecting.ico openvpn-gui.ico \ acinclude.m4 Makefile.in aclocal.m4 config.h.in configure configure.ac \ OpenVPN\ GUI\ ReadMe.txt changes.txt COPYING COPYRIGHT.GPL - + all : ${OBJS} - ${CC} $(CFLAGS) $(LDFLAGS) -o ${EXE} ${OBJS} ${LIBS} + ${CC} $(CFLAGS) $(LDFLAGS) -o ${EXE} ${OBJS} ${LIBS} %.o : %.c %.h $(CC) $(CFLAGS) -c $< diff --git a/localization.c b/localization.c new file mode 100755 index 0000000..4da7804 --- /dev/null +++ b/localization.c @@ -0,0 +1,122 @@ +/* + * OpenVPN-GUI -- A Windows GUI for OpenVPN. + * + * Copyright (C) 2009 Heiko Hund + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include + +static const LANGID defaultLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL); + +static HRSRC +FindLangResource(HINSTANCE instance, PTSTR resType, PTSTR resId, LANGID langId) +{ + HRSRC res; + + /* try to find the resource in requested language */ + res = FindResourceEx(instance, resType, resId, langId); + if (res) + return res; + + /* try to find the resource in the default language */ + res = FindResourceEx(instance, resType, resId, defaultLangId); + if (res) + return res; + + /* try to find the resource in any language */ + return FindResource(instance, resId, resType); +} + + +int +LoadLangString(HINSTANCE instance, UINT stringId, LANGID langId, PTSTR buffer, int bufferSize) +{ + PWCH entry; + PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1); + int resIndex = stringId & 15; + + /* find resource block for string */ + HRSRC res = FindLangResource(instance, RT_STRING, resBlockId, langId); + if (res == NULL) + return 0; + + /* get pointer to first entry in resource block */ + entry = (PWCH) LoadResource(instance, res); + if (entry == NULL) + return 0; + + /* search for string in block */ + for (int i = 0; i < 16; i++) + { + /* string found, copy it */ + if (i == resIndex && *entry) + { + int maxLen = (*entry < bufferSize ? *entry : bufferSize); +#ifdef _UNICODE + wcsncpy(buffer, entry + 1, maxLen); +#else + WideCharToMultiByte(CP_ACP, 0, entry + 1, *entry, buffer, maxLen, "?", NULL); +#endif + buffer[bufferSize - 1] = 0; + return _tcslen(buffer); + } + + /* string does not exist */ + if (i == resIndex) + break; + + /* skip over this entry */ + entry += ((*entry) + 1); + } + + return 0; +} + + +HICON +LoadLangIcon(HINSTANCE instance, PTSTR iconId, LANGID langId) +{ + /* find group icon resource */ + HRSRC res = FindLangResource(instance, RT_GROUP_ICON, 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 = FindLangResource(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); +} diff --git a/localization.h b/localization.h new file mode 100755 index 0000000..39fc7bc --- /dev/null +++ b/localization.h @@ -0,0 +1,23 @@ +/* + * OpenVPN-GUI -- A Windows GUI for OpenVPN. + * + * Copyright (C) 2009 Heiko Hund + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (see the file COPYING included with this + * distribution); if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +int LoadLangString(HINSTANCE, UINT, LANGID, PTSTR, int); +HICON LoadLangIcon(HINSTANCE, PTSTR, LANGID);