Skip to content

Commit

Permalink
- added API to retrieve localized dialog template
Browse files Browse the repository at this point in the history
- using instance from global options struct
  • Loading branch information
d12fk committed Jan 26, 2009
1 parent 33b255e commit 2f3ca9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
61 changes: 33 additions & 28 deletions localization.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,30 @@
#include <tchar.h>
#include <stdio.h>
#include <stdarg.h>
#include "options.h"
#include "registry.h"

extern struct options o;

static const LANGID defaultLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);

static HRSRC
FindResourceLang(HINSTANCE instance, PTSTR resType, PTSTR resId, LANGID langId)
FindResourceLang(PTSTR resType, PTSTR resId, LANGID langId)
{
HRSRC res;

/* try to find the resource in requested language */
res = FindResourceEx(instance, resType, resId, langId);
res = FindResourceEx(o.hInstance, resType, resId, langId);
if (res)
return res;

/* try to find the resource in the default language */
res = FindResourceEx(instance, resType, resId, defaultLangId);
res = FindResourceEx(o.hInstance, resType, resId, defaultLangId);
if (res)
return res;

/* try to find the resource in any language */
return FindResource(instance, resId, resType);
return FindResource(o.hInstance, resId, resType);
}


Expand All @@ -59,19 +62,19 @@ GetGUILanguage(void)


static int
LoadStringLang(HINSTANCE instance, UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args)
LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args)
{
PWCH entry;
PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1);
int resIndex = stringId & 15;

/* find resource block for string */
HRSRC res = FindResourceLang(instance, RT_STRING, resBlockId, langId);
HRSRC res = FindResourceLang(RT_STRING, resBlockId, langId);
if (res == NULL)
return 0;

/* get pointer to first entry in resource block */
entry = (PWCH) LoadResource(instance, res);
entry = (PWCH) LoadResource(o.hInstance, res);
if (entry == NULL)
return 0;

Expand Down Expand Up @@ -116,7 +119,7 @@ __LoadLocalizedString(const UINT stringId, va_list args)
{
static TCHAR msg[512];
msg[0] = 0;
LoadStringLang(GetModuleHandle(NULL), stringId, GetGUILanguage(), msg, sizeof(msg)/sizeof(*msg), args);
LoadStringLang(stringId, GetGUILanguage(), msg, sizeof(msg)/sizeof(*msg), args);
return msg;
}

Expand All @@ -137,7 +140,7 @@ 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);
int len = LoadStringLang(stringId, GetGUILanguage(), buffer, bufferSize, args);
va_end(args);
return len;
}
Expand All @@ -156,15 +159,14 @@ ShowLocalizedMsg(const PTSTR caption, const UINT stringId, ...)
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);
HRSRC res = FindResourceLang(RT_GROUP_ICON, MAKEINTRESOURCE(iconId), langId);
if (res == NULL)
return NULL;

HGLOBAL resInfo = LoadResource(instance, res);
HGLOBAL resInfo = LoadResource(o.hInstance, res);
if (resInfo == NULL)
return NULL;

Expand All @@ -173,37 +175,42 @@ LoadLocalizedIcon(const UINT iconId)
return NULL;

/* find the actual icon */
res = FindResourceLang(instance, RT_ICON, MAKEINTRESOURCE(id), langId);
res = FindResourceLang(RT_ICON, MAKEINTRESOURCE(id), langId);
if (res == NULL)
return NULL;

resInfo = LoadResource(instance, res);
resInfo = LoadResource(o.hInstance, res);
if (resInfo == NULL)
return NULL;

DWORD resSize = SizeofResource(instance, res);
DWORD resSize = SizeofResource(o.hInstance, res);
if (resSize == 0)
return NULL;

return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000);
}


INT_PTR
LocalizedDialogBoxParam(const UINT dialogId, DLGPROC dialogFunc, const LPARAM param)
LPCDLGTEMPLATE
LocalizedDialogResource(const UINT dialogId)
{
HINSTANCE instance = GetModuleHandle(NULL);

/* find dialog resource */
HRSRC res = FindResourceLang(instance, RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
HRSRC res = FindResourceLang(RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
if (res == NULL)
return -1;
return NULL;

HGLOBAL resInfo = LoadResource(instance, res);
return LoadResource(o.hInstance, res);
}


INT_PTR
LocalizedDialogBoxParam(const UINT dialogId, DLGPROC dialogFunc, const LPARAM param)
{
LPCDLGTEMPLATE resInfo = LocalizedDialogResource(dialogId);
if (resInfo == NULL)
return -1;

return DialogBoxIndirectParam(instance, resInfo, NULL, dialogFunc, param);
return DialogBoxIndirectParam(o.hInstance, resInfo, NULL, dialogFunc, param);
}


Expand All @@ -217,16 +224,14 @@ LocalizedDialogBox(const UINT dialogId, DLGPROC dialogFunc)
HWND
CreateLocalizedDialog(const UINT dialogId, DLGPROC dialogFunc)
{
HINSTANCE instance = GetModuleHandle(NULL);

/* find dialog resource */
HRSRC res = FindResourceLang(instance, RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
HRSRC res = FindResourceLang(RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
if (res == NULL)
return NULL;

HGLOBAL resInfo = LoadResource(instance, res);
HGLOBAL resInfo = LoadResource(o.hInstance, res);
if (resInfo == NULL)
return NULL;

return CreateDialogIndirect(instance, resInfo, NULL, dialogFunc);
return CreateDialogIndirect(o.hInstance, resInfo, NULL, dialogFunc);
}
1 change: 1 addition & 0 deletions localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PTSTR LoadLocalizedString(const UINT, ...);
int LoadLocalizedStringBuf(PTSTR, const int, const UINT, ...);
void ShowLocalizedMsg(const PTSTR, const UINT, ...);
HICON LoadLocalizedIcon(const UINT);
LPCDLGTEMPLATE LocalizedDialogResource(const UINT);
INT_PTR LocalizedDialogBoxParam(const UINT, DLGPROC, const LPARAM);
INT_PTR LocalizedDialogBox(const UINT, DLGPROC);
HWND CreateLocalizedDialog(const UINT, DLGPROC);
Expand Down

0 comments on commit 2f3ca9d

Please sign in to comment.