1 // 2 // config.c 3 // 4 // Screensaver Configuration dialog 5 // 6 #include <windows.h> 7 #include <commctrl.h> 8 #include <tchar.h> 9 #include "resource.h" 10 #include "globals.h" 11 #include "message.h" 12 #include "matrix.h" 13 14 // needed for slider-controls 15 //#pragma comment(lib, "comctl32.lib") 16 17 // 18 // Called once for every font - add to list 19 // 20 int CALLBACK EnumFontProc(ENUMLOGFONT *lpelfe, NEWTEXTMETRIC *lpntme, int FontType, LPARAM lParam) 21 { 22 SendMessage((HWND)lParam, CB_ADDSTRING, 0, (LPARAM)lpelfe->elfLogFont.lfFaceName); 23 return 1; 24 } 25 26 // 27 // Add every fontname into specified combobox 28 // 29 void AddFonts(HWND hwndCombo) 30 { 31 HDC hdc; 32 LOGFONT lf; 33 34 lf.lfCharSet = ANSI_CHARSET; 35 lf.lfPitchAndFamily = 0; 36 lf.lfFaceName[0] = _T('\0'); 37 38 hdc = GetDC(0); 39 EnumFontFamiliesEx(hdc, &lf, (FONTENUMPROC)EnumFontProc, (LONG_PTR)hwndCombo, 0); 40 ReleaseDC(0, hdc); 41 } 42 43 // 44 // Redraw preview control with current font/fontsize 45 // 46 void UpdatePreview(HWND hwnd) 47 { 48 g_nFontSize = SendDlgItemMessage(hwnd, IDC_SLIDER4, TBM_GETPOS,0, 0); 49 SetMessageFont(hwnd, g_szFontName, g_nFontSize, g_fFontBold); 50 InvalidateRect(GetDlgItem(hwnd, IDC_PREVIEW), 0, 0); 51 } 52 53 // 54 // Dialogbox procedure for Configuration window 55 // 56 INT_PTR CALLBACK ConfigDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 57 { 58 static TCHAR buf[256]; 59 HWND hwndCombo, hwndCtrl; 60 int index, items, val; 61 RECT rect; 62 static int prevwidth, prevheight; 63 64 switch(uMsg) 65 { 66 case WM_INITDIALOG: 67 68 prevwidth = GetSystemMetrics(SM_CXSCREEN) / GLYPH_WIDTH; 69 prevheight = GetSystemMetrics(SM_CYSCREEN) / GLYPH_HEIGHT + 1; 70 71 //Add any saved messages to the combo box 72 for(index = 0; index < g_nNumMessages; index++) 73 { 74 if(lstrlen(g_szMessages[index]) > 0) 75 SendDlgItemMessage(hwnd, IDC_COMBO1, CB_ADDSTRING, 0, (LPARAM)g_szMessages[index]); 76 } 77 78 //select the first message, and preview it 79 SendDlgItemMessage(hwnd, IDC_COMBO1, CB_SETCURSEL, 0, 0); 80 81 SendDlgItemMessage(hwnd, IDC_SLIDER1, TBM_SETRANGE, 0, MAKELONG(SPEED_MIN, SPEED_MAX)); 82 SendDlgItemMessage(hwnd, IDC_SLIDER2, TBM_SETRANGE, 0, MAKELONG(DENSITY_MIN, DENSITY_MAX)); 83 SendDlgItemMessage(hwnd, IDC_SLIDER3, TBM_SETRANGE, 0, MAKELONG(MSGSPEED_MIN, MSGSPEED_MAX)); 84 SendDlgItemMessage(hwnd, IDC_SLIDER4, TBM_SETRANGE, 0, MAKELONG(FONT_MIN, FONT_MAX)); 85 86 //SendDlgItemMessage(hwnd, IDC_SLIDER1, TBM_SETTICFREQ, 5, 0); 87 SendDlgItemMessage(hwnd, IDC_SLIDER2, TBM_SETTICFREQ, 5, 0); 88 SendDlgItemMessage(hwnd, IDC_SLIDER3, TBM_SETTICFREQ, 50, 0); 89 SendDlgItemMessage(hwnd, IDC_SLIDER4, TBM_SETTICFREQ, 2, 0); 90 91 SendDlgItemMessage(hwnd, IDC_SLIDER1, TBM_SETPOS, TRUE, g_nMatrixSpeed); 92 SendDlgItemMessage(hwnd, IDC_SLIDER2, TBM_SETPOS, TRUE, g_nDensity); 93 SendDlgItemMessage(hwnd, IDC_SLIDER3, TBM_SETPOS, TRUE, g_nMessageSpeed); 94 SendDlgItemMessage(hwnd, IDC_SLIDER4, TBM_SETPOS, TRUE, g_nFontSize); 95 96 CheckDlgButton(hwnd, IDC_RANDOM, g_fRandomizeMessages); 97 CheckDlgButton(hwnd, IDC_BOLD, g_fFontBold); 98 99 AddFonts(GetDlgItem(hwnd, IDC_COMBO2)); 100 index = SendDlgItemMessage(hwnd, IDC_COMBO2, CB_FINDSTRING, 0, (LPARAM)g_szFontName); 101 SendDlgItemMessage(hwnd, IDC_COMBO2, CB_SETCURSEL, index, 0); 102 UpdatePreview(hwnd); 103 return 0; 104 105 case WM_DESTROY: 106 //DeInitMessage(); 107 return 0; 108 109 case WM_CTLCOLORSTATIC: 110 111 if(GetDlgCtrlID((HWND)lParam) == IDC_ABOUT) 112 { 113 SetTextColor((HDC)wParam, RGB(0,80,0)); 114 SetBkColor((HDC)wParam, GetSysColor(COLOR_3DFACE)); 115 return (INT_PTR)GetSysColorBrush(COLOR_3DFACE); 116 } 117 else if(GetDlgCtrlID((HWND)lParam) == IDC_PREVIEW) 118 { 119 HDC hdc = (HDC)wParam; 120 RECT clip; 121 122 GetDlgItemText(hwnd, IDC_COMBO1, buf, 256); 123 124 GetClientRect((HWND)lParam, &rect); 125 126 if(prevwidth < rect.right) 127 { 128 rect.left = (rect.right-prevwidth) / 2; 129 rect.right = rect.left + prevwidth; 130 } 131 else 132 { 133 rect.left = 0; 134 rect.right = prevwidth; 135 } 136 137 if(prevheight < rect.bottom) 138 { 139 rect.top = (rect.bottom-prevheight) / 2; 140 rect.bottom = rect.top + prevheight; 141 } 142 else 143 { 144 rect.top = 0; 145 rect.bottom = prevheight; 146 } 147 148 SetTextColor(hdc, RGB(128,255,128)); 149 SetBkColor(hdc, 0); 150 151 //SetRect(&rect, 0, 0, PrevMessage->width, MAXMSG_HEIGHT); 152 CopyRect(&clip, &rect); 153 FillRect(hdc, &rect, GetStockObject(BLACK_BRUSH)); 154 155 SelectObject(hdc, g_hFont); 156 157 InflateRect(&clip, 2, 2); 158 159 FrameRect(hdc, &clip, GetSysColorBrush(COLOR_3DSHADOW)); 160 IntersectClipRect(hdc, rect.left, rect.top, rect.right, rect.bottom); 161 162 // figure out where the bounding rectangle should be 163 DrawText(hdc, buf, -1, &rect, DT_CENTER|DT_VCENTER|DT_WORDBREAK|DT_CALCRECT); 164 OffsetRect(&rect, (prevwidth-(rect.right-rect.left))/2, (prevheight-(rect.bottom-rect.top))/2); 165 166 // now draw it! 167 DrawText(hdc, buf, -1, &rect, DT_CENTER|DT_VCENTER|DT_WORDBREAK); 168 169 170 return (INT_PTR)GetStockObject(NULL_BRUSH); 171 } 172 else 173 { 174 break; 175 } 176 177 case WM_HSCROLL: 178 179 if((HWND)lParam == GetDlgItem(hwnd, IDC_SLIDER4)) 180 { 181 // one of the sliders changed..update 182 UpdatePreview(hwnd); 183 } 184 185 return 0; 186 187 case WM_COMMAND: 188 189 switch(HIWORD(wParam)) 190 { 191 case CBN_EDITCHANGE: 192 case CBN_SELCHANGE: 193 194 //fall through to Preview: 195 index = SendDlgItemMessage(hwnd, IDC_COMBO2, CB_GETCURSEL, 0, 0); 196 SendDlgItemMessage(hwnd, IDC_COMBO2, CB_GETLBTEXT, index, (LPARAM)g_szFontName); 197 //SetMessageFont(hwnd, g_szFontName, g_nFontSize, TRUE); 198 199 UpdatePreview(hwnd); 200 return 0; 201 } 202 203 switch(LOWORD(wParam)) 204 { 205 case IDC_RANDOM: 206 g_fRandomizeMessages = IsDlgButtonChecked(hwnd, IDC_RANDOM); 207 break; 208 209 case IDC_BOLD: 210 g_fFontBold = IsDlgButtonChecked(hwnd, IDC_BOLD); 211 UpdatePreview(hwnd); 212 break; 213 214 case IDOK: 215 216 hwndCtrl = GetDlgItem(hwnd, IDC_COMBO1); 217 218 items = min(MAX_MESSAGES, SendMessage(hwndCtrl, CB_GETCOUNT, 0, 0)); 219 220 for(index = 0; index < items; index++) 221 { 222 SendMessage(hwndCtrl, CB_GETLBTEXT, index, (LPARAM)g_szMessages[index]); 223 } 224 225 g_nNumMessages = items; 226 227 //matrix speed 228 val = SendDlgItemMessage(hwnd, IDC_SLIDER1, TBM_GETPOS, 0, 0); 229 if(val >= SPEED_MIN && val <= SPEED_MAX) 230 g_nMatrixSpeed = val; 231 232 //density 233 val = SendDlgItemMessage(hwnd, IDC_SLIDER2, TBM_GETPOS, 0, 0); 234 if(val >= DENSITY_MIN && val <= DENSITY_MAX) 235 g_nDensity = val; 236 237 //message speed 238 val = SendDlgItemMessage(hwnd, IDC_SLIDER3, TBM_GETPOS, 0, 0); 239 if(val >= MSGSPEED_MIN && val <= MSGSPEED_MAX) 240 g_nMessageSpeed = val; 241 242 //font size 243 val = SendDlgItemMessage(hwnd, IDC_SLIDER4, TBM_GETPOS, 0, 0); 244 if(val >= FONT_MIN && val <= FONT_MAX) 245 g_nFontSize = val; 246 247 SaveSettings(); 248 EndDialog(hwnd, 0); 249 return 0; 250 251 case IDCANCEL: 252 EndDialog(hwnd, 0); 253 return TRUE; 254 255 case IDC_ADD: 256 257 hwndCombo = GetDlgItem(hwnd, IDC_COMBO1); 258 259 if(GetWindowText(hwndCombo, buf, 256)) 260 { 261 SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)buf); 262 } 263 264 UpdatePreview(hwnd); 265 266 return 0; 267 268 case IDC_REMOVE: 269 hwndCombo = GetDlgItem(hwnd, IDC_COMBO1); 270 GetWindowText(hwndCombo, buf, 256); 271 272 index = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0); 273 SendMessage(hwndCombo, CB_DELETESTRING, index, 0); 274 275 SendMessage(hwndCombo, CB_SETCURSEL, 0, 0); 276 UpdatePreview(hwnd); 277 return 0; 278 } 279 return 0; 280 281 case WM_CLOSE: 282 EndDialog(hwnd, 0); 283 return 0; 284 } 285 return 0; 286 } 287 288 // 289 // Display the configuration dialog 290 // 291 int Configure(HWND hwndParent) 292 { 293 INITCOMMONCONTROLSEX icc; 294 295 icc.dwICC = ICC_UPDOWN_CLASS | ICC_BAR_CLASSES; 296 icc.dwSize = sizeof(icc); 297 298 InitCommonControlsEx(&icc); 299 300 #ifdef _DEBUG 301 if(hwndParent == NULL) 302 hwndParent = 0; 303 #else 304 if(hwndParent == NULL) 305 hwndParent = GetForegroundWindow(); 306 #endif 307 308 DialogBox(GetModuleHandle(0), MAKEINTRESOURCE(IDD_CONFIG), hwndParent, ConfigDlgProc); 309 310 return 0; 311 } 312 313