![]() |
WGX library Architecture - Reference Manual - Guides |
|
00001 /* 00002 * WGX - Windows GUI Extended Library. 00003 * Copyright (c) 2007-2010 by Dmitri Arkhangelski (dmitriar@gmail.com). 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00027 #define WIN32_NO_STATUS 00028 #include <windows.h> 00029 #include <string.h> 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 00033 #include "wgx.h" 00034 00035 void ExtractToken(short *dest, short *src, int max_chars) 00036 { 00037 signed int i,cnt; 00038 short ch; 00039 00040 cnt = 0; 00041 for(i = 0; i < max_chars; i++){ 00042 ch = src[i]; 00043 /* skip spaces and tabs in the beginning */ 00044 if((ch != 0x20 && ch != '\t') || cnt){ 00045 dest[cnt] = ch; 00046 cnt++; 00047 } 00048 } 00049 dest[cnt] = 0; 00050 /* remove spaces, tabs and \r\n from the end */ 00051 if(cnt == 0) return; 00052 for(i = (cnt - 1); i >= 0; i--){ 00053 ch = dest[i]; 00054 if(ch != 0x20 && ch != '\t' && ch != '\r' && ch != '\n') break; 00055 dest[i] = 0; 00056 } 00057 } 00058 00059 void AddResourceEntry(PWGX_I18N_RESOURCE_ENTRY table,short *line_buffer) 00060 { 00061 short first_char = line_buffer[0]; 00062 short *eq_pos; 00063 int param_len, value_len; 00064 int i; 00065 short *param_buffer; 00066 short *value_buffer; 00067 00068 /* skip comments and empty lines */ 00069 if(first_char == ';' || first_char == '#') 00070 return; 00071 eq_pos = wcschr(line_buffer,'='); 00072 if(!eq_pos) return; 00073 00074 param_buffer = malloc(8192 * sizeof(short)); 00075 if(!param_buffer) return; 00076 value_buffer = malloc(8192 * sizeof(short)); 00077 if(!value_buffer){ free(param_buffer); return; } 00078 00079 /* extract a parameter-value pair */ 00080 param_buffer[0] = value_buffer[0] = 0; 00081 param_len = (int)/*(LONG_PTR)*/(eq_pos - line_buffer); 00082 value_len = (int)/*(LONG_PTR)*/(line_buffer + wcslen(line_buffer) - eq_pos - 1); 00083 ExtractToken(param_buffer,line_buffer,param_len); 00084 ExtractToken(value_buffer,eq_pos + 1,value_len); 00085 (void)_wcsupr(param_buffer); 00086 00087 /* search for table entry */ 00088 for(i = 0;; i++){ 00089 if(table[i].Key == NULL) break; 00090 if(!wcscmp(table[i].Key,param_buffer)){ 00091 table[i].LoadedString = malloc((wcslen(value_buffer) + 1) * sizeof(short)); 00092 if(table[i].LoadedString) (void)wcscpy(table[i].LoadedString, value_buffer); 00093 /* break; // the same text may be used for few GUI controls */ 00094 } 00095 } 00096 free(param_buffer); free(value_buffer); 00097 } 00098 00107 BOOL __stdcall WgxBuildResourceTable(PWGX_I18N_RESOURCE_ENTRY table,short *lng_file_path) 00108 { 00109 FILE *f; 00110 short *line_buffer; 00111 00112 /* parameters validation */ 00113 if(!table || !lng_file_path) return FALSE; 00114 00115 line_buffer = malloc(8192 * sizeof(short)); 00116 if(!line_buffer) return FALSE; 00117 00118 /* open lng file */ 00119 f = _wfopen(lng_file_path,L"rb"); /* binary mode required! */ 00120 if(!f){ free(line_buffer); return FALSE; } 00121 00122 /* read lines and applies them to specified table */ 00123 while(fgetws(line_buffer,8192,f)){ 00124 line_buffer[8192 - 1] = 0; 00125 AddResourceEntry(table,line_buffer); 00126 } 00127 fclose(f); free(line_buffer); 00128 return TRUE; 00129 } 00130 00136 void __stdcall WgxApplyResourceTable(PWGX_I18N_RESOURCE_ENTRY table,HWND hWindow) 00137 { 00138 int i; 00139 HWND hChild; 00140 00141 if(!table || !hWindow) return; 00142 00143 for(i = 0;; i++){ 00144 if(table[i].Key == NULL) break; 00145 hChild = GetDlgItem(hWindow,table[i].ControlID); 00146 if(table[i].LoadedString) (void)SetWindowTextW(hChild,table[i].LoadedString); 00147 else (void)SetWindowTextW(hChild,table[i].DefaultString); 00148 } 00149 } 00150 00158 short * __stdcall WgxGetResourceString(PWGX_I18N_RESOURCE_ENTRY table,short *key) 00159 { 00160 int i; 00161 00162 if(!table || !key) return NULL; 00163 00164 for(i = 0;; i++){ 00165 if(table[i].Key == NULL) break; 00166 if(!wcscmp(table[i].Key,key)){ 00167 if(table[i].LoadedString) return table[i].LoadedString; 00168 return table[i].DefaultString; 00169 } 00170 } 00171 return NULL; 00172 } 00173 00178 void __stdcall WgxDestroyResourceTable(PWGX_I18N_RESOURCE_ENTRY table) 00179 { 00180 int i; 00181 00182 if(!table) return; 00183 00184 for(i = 0;; i++){ 00185 if(table[i].Key == NULL) break; 00186 if(table[i].LoadedString) free(table[i].LoadedString); 00187 } 00188 } 00189