ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

display.c
Go to the documentation of this file.
00001 /*
00002  *  fontview display class
00003  *
00004  *  display.c
00005  *
00006  *  Copyright (C) 2007  Timo Kreuzer <timo <dot> kreuzer <at> reactos <dot> org>
00007  *
00008  *  This program is free software; you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation; either version 2 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License along
00019  *  with this program; if not, write to the Free Software Foundation, Inc.,
00020  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00021  */
00022 
00023 #include <windows.h>
00024 #include <stdio.h>
00025 
00026 #include "display.h"
00027 
00028 #define SPACING1 8
00029 #define SPACING2 5
00030 
00031 const WCHAR g_szFontDisplayClassName[] = L"FontDisplayClass";
00032 LRESULT CALLBACK DisplayProc(HWND, UINT, WPARAM, LPARAM);
00033 
00034 /* Internal data storage type */
00035 typedef struct
00036 {
00037     int nPageHeight;
00038     WCHAR szTypeFaceName[LF_FULLFACESIZE];
00039     WCHAR szFormat[MAX_FORMAT];
00040     WCHAR szString[MAX_STRING];
00041 
00042     HFONT hCaptionFont;
00043     HFONT hCharSetFont;
00044     HFONT hSizeFont;
00045     HFONT hFonts[MAX_SIZES];
00046     int nSizes[MAX_SIZES];
00047     int nHeights[MAX_SIZES];
00048 } DISPLAYDATA;
00049 
00050 /* This is the only public function, it registers the class */
00051 BOOL
00052 Display_InitClass(HINSTANCE hInstance)
00053 {
00054     WNDCLASSEXW wincl;
00055 
00056     /* Set the fontdisplay window class structure */
00057     wincl.cbSize = sizeof(WNDCLASSEX);
00058     wincl.style = CS_DBLCLKS;
00059     wincl.lpfnWndProc = DisplayProc;
00060     wincl.cbClsExtra = 0;
00061     wincl.cbWndExtra = 0;
00062     wincl.hInstance = hInstance;
00063     wincl.hIcon = NULL;
00064     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
00065     wincl.hbrBackground = GetStockObject(WHITE_BRUSH);
00066     wincl.lpszMenuName = NULL;
00067     wincl.lpszClassName = g_szFontDisplayClassName;
00068     wincl.hIconSm = NULL;
00069 
00070     /* Register the window class, and if it fails return FALSE */
00071     if (!RegisterClassExW (&wincl))
00072     {
00073         return FALSE;
00074     }
00075     return TRUE;
00076 }
00077 
00078 static int
00079 Display_DrawText(HDC hDC, DISPLAYDATA* pData, int nYPos)
00080 {
00081     HFONT hOldFont;
00082     TEXTMETRIC tm;
00083     int i, y;
00084     WCHAR szSize[5];
00085     WCHAR szCaption[LF_FULLFACESIZE + 20];
00086 
00087     /* This is the location on the DC where we draw */
00088     y = -nYPos;
00089 
00090     hOldFont = SelectObject(hDC, pData->hCaptionFont);
00091     GetTextMetrics(hDC, &tm);
00092 
00093     swprintf(szCaption, L"%s%s", pData->szTypeFaceName, pData->szFormat);
00094     TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
00095     y += tm.tmHeight + SPACING1;
00096 
00097     /* Draw a seperation Line */
00098     SelectObject(hDC, GetStockObject(BLACK_PEN));
00099     MoveToEx(hDC, 0, y, NULL);
00100     LineTo(hDC, 10000, y);
00101     y += SPACING2;
00102 
00103     /* TODO: Output font info */
00104 
00105     /* Output Character set */
00106     SelectObject(hDC, pData->hCharSetFont);
00107     GetTextMetrics(hDC, &tm);
00108     swprintf(szCaption, L"abcdefghijklmnopqrstuvwxyz");
00109     TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
00110     y += tm.tmHeight + 1;
00111 
00112     swprintf(szCaption, L"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
00113     TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
00114     y += tm.tmHeight + 1;
00115 
00116     swprintf(szCaption, L"0123456789.:,;(\"~!@#$%^&*')");
00117     TextOutW(hDC, 0, y, szCaption, wcslen(szCaption));
00118     y += tm.tmHeight + 1;
00119 
00120     /* Draw a seperation Line */
00121     SelectObject(hDC, GetStockObject(BLACK_PEN));
00122     MoveToEx(hDC, 0, y, NULL);
00123     LineTo(hDC, 10000, y);
00124     y += SPACING2;
00125 
00126     /* Output the strings for different sizes */
00127     for (i = 0; i < MAX_SIZES; i++)
00128     {
00129         SelectObject(hDC, pData->hFonts[i]);
00130         TextOutW(hDC, 20, y, pData->szString, wcslen(pData->szString));
00131         GetTextMetrics(hDC, &tm);
00132         y += tm.tmHeight + 1;
00133         SelectObject(hDC, pData->hSizeFont);
00134         swprintf(szSize, L"%d", pData->nSizes[i]);
00135         TextOutW(hDC, 0, y - 13 - tm.tmDescent, szSize, wcslen(szSize));
00136     }
00137     SelectObject(hDC, hOldFont);
00138 
00139     return y;
00140 }
00141 
00142 static LRESULT
00143 Display_SetTypeFace(HWND hwnd, PEXTLOGFONTW pExtLogFont)
00144 {
00145     DISPLAYDATA* pData;
00146     TEXTMETRIC tm;
00147     HDC hDC;
00148     RECT rect;
00149     SCROLLINFO si;
00150     int i;
00151     LOGFONTW logfont;
00152 
00153     /* Set the new type face name */
00154     pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
00155     _snwprintf(pData->szTypeFaceName, LF_FULLFACESIZE, pExtLogFont->elfFullName);
00156 
00157     /* Create the new fonts */
00158     hDC = GetDC(hwnd);
00159     DeleteObject(pData->hCharSetFont);
00160 
00161     logfont = pExtLogFont->elfLogFont;
00162     logfont.lfHeight = -MulDiv(16, GetDeviceCaps(GetDC(NULL), LOGPIXELSY), 72);
00163     pData->hCharSetFont = CreateFontIndirectW(&logfont);
00164 
00165     /* Get font format */
00166     // FIXME: Get the real font format (OpenType?)
00167     SelectObject(hDC, pData->hCharSetFont);
00168     GetTextMetrics(hDC, &tm);
00169     if ((tm.tmPitchAndFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE)
00170     {
00171         swprintf(pData->szFormat, L" (TrueType)");
00172     }
00173 
00174     for (i = 0; i < MAX_SIZES; i++)
00175     {
00176         DeleteObject(pData->hFonts[i]);
00177         logfont.lfHeight = -MulDiv(pData->nSizes[i], GetDeviceCaps(hDC, LOGPIXELSY), 72);
00178         pData->hFonts[i] = CreateFontIndirectW(&logfont);
00179     }
00180 
00181     /* Calculate new page dimensions */
00182     pData->nPageHeight = Display_DrawText(hDC, pData, 0);
00183     ReleaseDC(hwnd, hDC);
00184 
00185     /* Set the vertical scrolling range and page size */
00186     GetClientRect(hwnd, &rect);
00187     si.cbSize = sizeof(si);
00188     si.fMask  = SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS;
00189     si.nMin   = 0;
00190     si.nMax   = pData->nPageHeight;
00191     si.nPage  = rect.bottom;
00192     si.nPos   = 0;
00193     si.nTrackPos = 0;
00194     SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
00195 
00196     return 0;
00197 }
00198 
00199 static LRESULT
00200 Display_SetString(HWND hwnd, LPARAM lParam)
00201 {
00202     DISPLAYDATA* pData;
00203 
00204     pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
00205     _snwprintf(pData->szString, MAX_STRING, (WCHAR*)lParam);
00206 
00207     InvalidateRect(hwnd, NULL, TRUE);
00208 
00209     return 0;
00210 }
00211 
00212 static LRESULT
00213 Display_OnCreate(HWND hwnd)
00214 {
00215     DISPLAYDATA* pData;
00216     const int nSizes[MAX_SIZES] = {8, 12, 18, 24, 36, 48, 60, 72};
00217     int i;
00218     EXTLOGFONTW ExtLogFont = {{50, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
00219                               ANSI_CHARSET, OUT_DEFAULT_PRECIS,
00220                               CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
00221                               DEFAULT_PITCH , L"Ms Shell Dlg"},
00222                               L"Ms Shell Dlg"};
00223 
00224     /* Create data structure */
00225     pData = malloc(sizeof(DISPLAYDATA));
00226     ZeroMemory(pData, sizeof(DISPLAYDATA));
00227 
00228     /* Set the window's GWLP_USERDATA to our data structure */
00229     SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pData);
00230 
00231     for (i = 0; i < MAX_SIZES; i++)
00232     {
00233         pData->nSizes[i] = nSizes[i];
00234     }
00235 
00236     pData->hCaptionFont = CreateFontIndirectW(&ExtLogFont.elfLogFont);
00237     ExtLogFont.elfLogFont.lfHeight = 12;
00238     pData->hSizeFont = CreateFontIndirectW(&ExtLogFont.elfLogFont);
00239 
00240     Display_SetString(hwnd, (LPARAM)L"Jackdaws love my big sphinx of quartz. 1234567890");
00241 
00242     Display_SetTypeFace(hwnd, &ExtLogFont);
00243 
00244     return 0;
00245 }
00246 
00247 static LRESULT
00248 Display_OnPaint(HWND hwnd)
00249 {
00250     DISPLAYDATA* pData;
00251     PAINTSTRUCT ps;
00252     SCROLLINFO si;
00253 
00254     pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
00255 
00256     /* Get the Scroll position */
00257     si.cbSize = sizeof(si);
00258     si.fMask = SIF_POS;
00259     GetScrollInfo(hwnd, SB_VERT, &si);
00260 
00261     BeginPaint(hwnd, &ps);
00262 
00263     /* Erase background */
00264     FillRect(ps.hdc, &ps.rcPaint, GetStockObject(WHITE_BRUSH));
00265 
00266     /* Draw the text */
00267     Display_DrawText(ps.hdc, pData, si.nPos);
00268 
00269     EndPaint(hwnd, &ps);
00270 
00271     return 0;
00272 }
00273 
00274 static LRESULT
00275 Display_OnSize(HWND hwnd)
00276 {
00277     RECT rect;
00278     SCROLLINFO si;
00279     int nOldPos;
00280 
00281     GetClientRect(hwnd, &rect);
00282 
00283     /* Get the old scroll pos */
00284     si.cbSize = sizeof(si);
00285     si.fMask  = SIF_POS;
00286     GetScrollInfo(hwnd, SB_VERT, &si);
00287     nOldPos = si.nPos;
00288 
00289     /* Set the new page size */
00290     si.fMask  = SIF_PAGE;
00291     si.nPage  = rect.bottom;
00292     SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
00293 
00294     /* Get the new scroll pos */
00295     si.fMask  = SIF_POS;
00296     GetScrollInfo(hwnd, SB_VERT, &si);
00297 
00298     /* If they don't match ... */
00299     if (nOldPos != si.nPos)
00300     {
00301         /* ... scroll the window */
00302         ScrollWindowEx(hwnd, 0, nOldPos - si.nPos, NULL, NULL, NULL, NULL, SW_INVALIDATE);
00303         UpdateWindow(hwnd);
00304     }
00305 
00306     return 0;
00307 }
00308 
00309 static LRESULT
00310 Display_OnVScroll(HWND hwnd, WPARAM wParam)
00311 {
00312     SCROLLINFO si;
00313     int nPos;
00314 
00315     si.cbSize = sizeof(si);
00316     si.fMask  = SIF_POS | SIF_RANGE | SIF_TRACKPOS;
00317     GetScrollInfo(hwnd, SB_VERT, &si);
00318 
00319     switch(LOWORD(wParam))
00320     {
00321         case SB_PAGEUP:
00322             nPos = si.nPos - 50;
00323             break;
00324         case SB_PAGEDOWN:
00325             nPos = si.nPos + 50;
00326             break;
00327         case SB_LINEUP:
00328             nPos = si.nPos - 10;
00329             break;
00330         case SB_LINEDOWN:
00331             nPos = si.nPos + 10;
00332             break;
00333         case SB_THUMBTRACK:
00334         case SB_THUMBPOSITION:
00335             nPos = si.nTrackPos;
00336             break;
00337         default:
00338             nPos = si.nPos;
00339     }
00340 
00341     nPos = max(nPos, si.nMin);
00342     nPos = min(nPos, si.nMax);
00343     if (nPos != si.nPos)
00344     {
00345         ScrollWindowEx(hwnd, 0, si.nPos - nPos, NULL, NULL, NULL, NULL, SW_INVALIDATE);
00346         si.cbSize = sizeof(si);
00347         si.nPos = nPos;
00348         si.fMask  = SIF_POS;
00349         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
00350         UpdateWindow(hwnd);
00351     }
00352 
00353     return 0;
00354 }
00355 
00356 static LRESULT
00357 Display_OnDestroy(HWND hwnd)
00358 {
00359     DISPLAYDATA* pData;
00360     int i;
00361 
00362     pData = (DISPLAYDATA*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
00363 
00364     /* Delete the fonts */
00365     DeleteObject(pData->hCaptionFont);
00366     DeleteObject(pData->hCharSetFont);
00367     DeleteObject(pData->hSizeFont);
00368 
00369     for (i = 0; i < MAX_SIZES; i++)
00370     {
00371         DeleteObject(pData->hFonts[i]);
00372     }
00373 
00374     /* Free the data structure */
00375     free(pData);
00376 
00377     return 0;
00378 }
00379 
00380 LRESULT CALLBACK
00381 DisplayProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
00382 {
00383     switch (message)
00384     {
00385         case WM_CREATE:
00386             return Display_OnCreate(hwnd);
00387 
00388         case WM_PAINT:
00389             return Display_OnPaint(hwnd);
00390 
00391         case WM_SIZE:
00392             return Display_OnSize(hwnd);
00393 
00394         case WM_VSCROLL:
00395             return Display_OnVScroll(hwnd, wParam);
00396 
00397         case FVM_SETTYPEFACE:
00398             return Display_SetTypeFace(hwnd, (PEXTLOGFONTW)lParam);
00399 
00400         case FVM_SETSTRING:
00401             return Display_SetString(hwnd, lParam);
00402 
00403         case WM_DESTROY:
00404             return Display_OnDestroy(hwnd);
00405 
00406         default:
00407             return DefWindowProcW(hwnd, message, wParam, lParam);
00408     }
00409 
00410     return 0;
00411 }
00412 

Generated on Fri May 25 2012 04:14:56 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.