home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / LangDLL / LangDLL.c next >
C/C++ Source or Header  |  2003-11-12  |  5KB  |  163 lines

  1. #include <windows.h>
  2. #include "resource.h"
  3.  
  4. // JF> updated usage
  5. // call like this:
  6. // LangDLL:LangDialog "Window Title" "Window subtext" <number of languages>[F] language_text language_id ... [font_size font_face]
  7. // ex:
  8. //  LangDLL:LangDialog "Language Selection" "Choose a language" 2 French 1036 English 1033
  9. // or (the F after the 2 means we're supplying font information)
  10. //  LangDLL:LangDialog "Language Selection" "Choose a language" 2F French 1036 English 1033 12 Garamond
  11.  
  12.  
  13. #include "../exdll/exdll.h"
  14.  
  15. int myatoi(char *s);
  16.  
  17. HINSTANCE g_hInstance;
  18. HWND g_hwndParent;
  19.  
  20. char temp[1024];
  21. char g_wndtitle[1024], g_wndtext[1024];
  22. int dofont;
  23.  
  24. int langs_num;
  25.  
  26. struct lang {
  27.   char *name;
  28.   char *id;
  29. } *langs;
  30.  
  31. BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  32. {
  33.   int i, size;
  34.   static HFONT font;
  35.   switch (uMsg) {
  36.       case WM_INITDIALOG:
  37.       for (i = langs_num - 1; i >= 0; i--) {
  38.         SendDlgItemMessage(hwndDlg, IDC_LANGUAGE, CB_ADDSTRING, 0, (LPARAM)langs[i].name);
  39.       }
  40.       SetDlgItemText(hwndDlg, IDC_TEXT, g_wndtext);
  41.       SetWindowText(hwndDlg, g_wndtitle);
  42.       SendDlgItemMessage(hwndDlg, IDC_APPICON, STM_SETICON, (LPARAM)LoadIcon(GetModuleHandle(0),MAKEINTRESOURCE(103)), 0);
  43.       for (i = 0; i < langs_num; i++) {
  44.         if (!lstrcmp(langs[i].id, getuservariable(INST_LANG))) {
  45.           SendDlgItemMessage(hwndDlg, IDC_LANGUAGE, CB_SETCURSEL, langs_num-i-1, 0);
  46.           break;
  47.         }
  48.       }
  49.       if (dofont && !popstring(temp))
  50.       {
  51.         size = myatoi(temp);
  52.         if (!popstring(temp)) {
  53.           LOGFONT f = {0,};
  54.           if (lstrcmp(temp, "MS Shell Dlg")) {
  55.             f.lfHeight = -MulDiv(size, GetDeviceCaps(GetDC(hwndDlg), LOGPIXELSY), 72);
  56.             lstrcpy(f.lfFaceName, temp);
  57.             font = CreateFontIndirect(&f);
  58.             SendMessage(hwndDlg, WM_SETFONT, (WPARAM)font, 1);
  59.             SendDlgItemMessage(hwndDlg, IDOK, WM_SETFONT, (WPARAM)font, 1);
  60.             SendDlgItemMessage(hwndDlg, IDCANCEL, WM_SETFONT, (WPARAM)font, 1);
  61.             SendDlgItemMessage(hwndDlg, IDC_LANGUAGE, WM_SETFONT, (WPARAM)font, 1);
  62.             SendDlgItemMessage(hwndDlg, IDC_TEXT, WM_SETFONT, (WPARAM)font, 1);
  63.           }
  64.         }
  65.       }
  66.       ShowWindow(hwndDlg, SW_SHOW);
  67.       break;
  68.     case WM_COMMAND:
  69.       switch (LOWORD(wParam)) {
  70.           case IDOK:
  71.           pushstring(langs[langs_num-SendDlgItemMessage(hwndDlg, IDC_LANGUAGE, CB_GETCURSEL, 0, 0)-1].id);
  72.           EndDialog(hwndDlg, 0);
  73.           break;
  74.         case IDCANCEL:
  75.           pushstring("cancel");
  76.           EndDialog(hwndDlg, 1);
  77.           break;
  78.       }
  79.       break;
  80.     case WM_CLOSE:
  81.       if (font) DeleteObject(font);
  82.       pushstring("cancel");
  83.       EndDialog(hwndDlg, 1);
  84.       break;
  85.     default:
  86.       return 0;
  87.   }
  88.   return 1;
  89. }
  90.  
  91. void __declspec(dllexport) LangDialog(HWND hwndParent, int string_size, 
  92.                                       char *variables, stack_t **stacktop)
  93. {
  94.   g_hwndParent=hwndParent;
  95.   EXDLL_INIT();
  96.  
  97.   {
  98.     int i;
  99.     BOOL bPopOneMore = FALSE;
  100.  
  101.     if (popstring(g_wndtitle)) return;
  102.     if (popstring(g_wndtext)) return;
  103.  
  104.     if (popstring(temp)) return;
  105.     if (*temp == 'A')
  106.     {
  107.       stack_t *th;
  108.       langs_num=0;
  109.       th=(*g_stacktop);
  110.       while (th && th->text[0]) {
  111.         langs_num++;
  112.         th = th->next;
  113.       }
  114.       if (!th) return;
  115.       langs_num /= 2;
  116.       bPopOneMore = TRUE;
  117.     }
  118.     else
  119.       langs_num = myatoi(temp);
  120.     {
  121.       char *p=temp;
  122.       while (*p) if (*p++ == 'F') dofont=1;
  123.     }
  124.  
  125.     if (!langs_num) return;
  126.  
  127.     langs = (struct lang *)GlobalAlloc(GPTR, langs_num*sizeof(struct lang));
  128.  
  129.     for (i = 0; i < langs_num; i++) {
  130.       if (popstring(temp)) return;
  131.       langs[i].name = GlobalAlloc(GPTR, lstrlen(temp)+1);
  132.       lstrcpy(langs[i].name, temp);
  133.  
  134.       if (popstring(temp)) return;
  135.       langs[i].id = GlobalAlloc(GPTR, lstrlen(temp)+1);
  136.       lstrcpy(langs[i].id, temp);
  137.     }
  138.     if (bPopOneMore) {
  139.       if (popstring(temp)) return;
  140.     }
  141.  
  142.     DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG), 0, DialogProc);
  143.   }
  144. }
  145.  
  146. BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
  147. {
  148.   g_hInstance=hInst;
  149.     return TRUE;
  150. }
  151.  
  152. int myatoi(char *s)
  153. {
  154.   unsigned int v=0;
  155.   for (;;)
  156.   {
  157.     int c=*s++ - '0';
  158.     if (c < 0 || c > 9) break;
  159.     v*=10;
  160.     v+=c;
  161.   }
  162.   return (int)v;
  163. }