From 8596131f444340262860a65289dd1a7856fbc907 Mon Sep 17 00:00:00 2001 From: Quantum Date: Mon, 26 Aug 2013 16:52:47 -0400 Subject: [PATCH] Added X-SAMPA table. Also: 1. Cleaned up text handling by storing them in resource. 2. Added useful methods to make code look better. 3. Allowed PreviewWindow to be used for general text displaying. --- Makefile | 4 +- Zalgo.rc | 3 + include/MainWindow.hpp | 9 +- include/PreviewWindow.hpp | 7 +- include/Window.hpp | 9 +- include/resource.h | 9 ++ res/init.txt | Bin 0 -> 1190 bytes res/loose.txt | Bin 0 -> 576 bytes res/x-sampa.txt | Bin 0 -> 9378 bytes src/MainWindow.cpp | 139 +++++++++++++++----------- src/PreviewWindow.cpp | 200 ++++++++++++++++++++++---------------- src/Zalgo.cpp | 2 +- 12 files changed, 229 insertions(+), 153 deletions(-) create mode 100644 res/init.txt create mode 100644 res/loose.txt create mode 100644 res/x-sampa.txt diff --git a/Makefile b/Makefile index 9e30296..90753e8 100644 --- a/Makefile +++ b/Makefile @@ -52,8 +52,8 @@ $(SRCDIR)\Window.cpp: $(INCDIR)\Window.hpp $(SRCDIR)\NLSWrap.cpp: $(INCDIR)\NLSWrap.hpp $(SRCDIR)\MainLogic.cpp: $(INCDIR)\MainWindow.hpp $(INCDIR)\NLSWrap.hpp $(INCDIR)\ConversionData.inc -$(OUTDIR)\Zalgo.res: Zalgo.rc - $(RC) $(RCFLAGS) /fo$@ $** +$(OUTDIR)\Zalgo.res: Zalgo.rc res\x-sampa.txt res\init.txt + $(RC) $(RCFLAGS) /fo$@ Zalgo.rc {$(SRCDIR)}.cpp{$(OUTDIR)}.obj:: $(CXX) $(CXXFLAGS) /Fo$(OUTDIR)\ /Fd$(OUTDIR)\ $< diff --git a/Zalgo.rc b/Zalgo.rc index 55fc490..c46504d 100644 --- a/Zalgo.rc +++ b/Zalgo.rc @@ -1,3 +1,6 @@ #include RID_ICON ICON Zalgo.ico +RID_XSAMPA ZALGO_TEXT "res\\x-sampa.txt" +RID_INIT ZALGO_TEXT "res\\init.txt" +RID_LOOSE ZALGO_TEXT "res\\loose.txt" diff --git a/include/MainWindow.hpp b/include/MainWindow.hpp index 09d28ac..0592b83 100644 --- a/include/MainWindow.hpp +++ b/include/MainWindow.hpp @@ -27,10 +27,11 @@ #define ZALGO_CYRILLIC 0xA555 #define ZALGO_GREEK 0xA556 #define ZALGO_XSAMPA 0xA557 +#define ZALGO_XSAMPA_TABLE 0xA558 class MainWindow : public Window { public: - virtual LPCTSTR ClassName() { return TEXT("Zalgo_Main"); } + virtual LPCTSTR ClassName() { return L"Zalgo_Main"; } static MainWindow *Create(LPCTSTR szTitle); protected: LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); @@ -55,11 +56,11 @@ protected: HWND m_messLevel, m_messUpDown; HWND m_mess, m_unmess, m_previewShow; HWND m_nfc, m_nfd; - HWND m_latin, m_cyrillic, m_greek, m_xsampa; + HWND m_latin, m_cyrillic, m_greek, m_xsampa, m_xsampa_table; MyDropTarget m_dropTarget; - PreviewWindow *m_preview; + PreviewWindow *m_preview, *m_data_display; private: - HFONT hFont, hFontMono; + HFONT hFont; HBRUSH hBrush; UDACCEL *udaSecondAccel; }; diff --git a/include/PreviewWindow.hpp b/include/PreviewWindow.hpp index e035f89..22010f7 100644 --- a/include/PreviewWindow.hpp +++ b/include/PreviewWindow.hpp @@ -12,9 +12,13 @@ class PreviewWindow : public Window { public: - virtual LPCTSTR ClassName() { return TEXT("Zalgo_Preview"); } + virtual LPCTSTR ClassName() { return L"Zalgo_Preview"; } static PreviewWindow *Create(LPCTSTR szTitle); void Destroy() { DestroyWindow(m_hwnd); } + + void ChangeText(LPWSTR text, bool padding = true); + void SetFont(HFONT hFont); + void SetFont(const LOGFONT &lf); protected: LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); LRESULT OnCreate(); @@ -24,6 +28,7 @@ protected: virtual HICON GetIcon(); private: HFONT hFont; + bool used_logfont; HBRUSH hBrush; int xChar, yChar, xUpper, xClient, yClient, xClientMax, xPos, yPos; LPTSTR *lpLines; diff --git a/include/Window.hpp b/include/Window.hpp index 645b974..0987b82 100644 --- a/include/Window.hpp +++ b/include/Window.hpp @@ -32,8 +32,10 @@ inline void MakeRectBottomRight(RECT &rect, int x, int y, int cx, int cy) class Window { public: - HWND GetHWND() { return m_hwnd; } + HWND GetHWND() const { return m_hwnd; } + bool ShowWindow(int nCmdShow) { return ::ShowWindow(m_hwnd, nCmdShow) != 0; } static HINSTANCE GetInstance() { return (HINSTANCE) GetModuleHandle(NULL); } + operator HWND() const { return m_hwnd; } protected: virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam); virtual void PaintContent(PAINTSTRUCT *pps) {} @@ -52,9 +54,8 @@ private: void OnPaint(); void OnPrintClient(HDC hdc); - static LRESULT CALLBACK - s_WndProc(HWND hwnd, UINT uMsg, - WPARAM wParam, LPARAM lParam); + static LRESULT CALLBACK s_WndProc(HWND hwnd, UINT uMsg, + WPARAM wParam, LPARAM lParam); protected: HWND m_hwnd; }; diff --git a/include/resource.h b/include/resource.h index 4501386..5dc1b0a 100644 --- a/include/resource.h +++ b/include/resource.h @@ -1 +1,10 @@ #define RID_ICON 1 +#define RID_XSAMPA 2 +#define RID_INIT 3 +#define RID_LOOSE 4 + +inline void GetMessageFont(LOGFONT &lf) { + NONCLIENTMETRICS ncmMetrics = { sizeof(NONCLIENTMETRICS) }; + SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncmMetrics, 0); + memcpy(&lf, &ncmMetrics.lfMessageFont, sizeof lf); +} diff --git a/res/init.txt b/res/init.txt new file mode 100644 index 0000000000000000000000000000000000000000..d2b99e725e2b7e530362003b5e90438678d4341e GIT binary patch literal 1190 zcmb7^TT8=05QXPi@IS0?-oaPj6mJEIAccZ&>CG0?q$IUoe!Tjf*{mtl7b!_LJ2Pj_ zIXmh1SKlfNHn-B&tX^!w+sbDCoUy-Vbz%$87!#{`U;4daRg$aPXCfo2axfY@wO!k> z-Y;g#NVrofCszDcRyO^ken4Cm0nArcde%0kvTi%{Tb>zvpRiJ7OjS@;Ydd0 zoZ3f_8dMb<{;k-1cbyGAKApwf`_}HjGrbG@pjtl6VJs%<9qf>6-_E!naZN!Uah8GS zl6wJ`&atoqSVrdZR37DYkheqPI!`Ij!7q6mx0<9OLs8$Zoqf;GOJ@`LX&x&0)Ko4& z82PFpE|uadT;;3BG3y~*l$)mQgFNxm986tbry@;H)RL32r0;|-s*Wexd8aP9D~jA! zWau1+?wHi2sS=W0sohg)k;7fzZTI2{%Z;lRvs0mI)m&6`kmjVKWJmtyt~u%zAGxDk zsgZkedgW4@0^uds0=t@QA&tn^cc-et-Q4W;<~y(MXICA+*fj_f?>Xf3SNw$3sk-!c w)duENXQdhHk)sZp+stmzzR6O4YWg*0`u3$TehhX2ps4{;;Q0RR91 literal 0 HcmV?d00001 diff --git a/res/loose.txt b/res/loose.txt new file mode 100644 index 0000000000000000000000000000000000000000..4711f65f070c0cbc7a812288af31172c74b17885 GIT binary patch literal 576 zcma)4K@NgI44kvC*asLB_2$KkPw)e%fSVv|V2Sbb>P!nE@uDHHZJAD|1Mk-nS0oGw zJPY*5j50bzl(<-&_-u$td|RSrcEi(Zkx?YN$CCNQ5Z*s=-pP~>O2!TgW&vxg z#;R+0kgwqta58+yXOBBMTAq>BX5^8o!4x^G*ri^|Thz>IB3s^_P1H6%bMs^7nRv~u z$S)P!7Ota{Xd+NgG^z4)!wHdRwYYFJIDauBbm=F6z73DDNSH=-J_o2w%=HLMGFBdGb1Iu5>r3B4QOYl*!^z)j1C);*-m zx(_Azld{9>9)@&|5~R?k!|?n#EKB{n^WF$hIGhcf1+D+;;(5k{u_PtX)3j|xY(;OD zfl^GYh?XQy*pB5{OC0aW7q-DD#*LJ#+%HI6@Zd?58TYIFj*cGHe*yn%GF!y(&2~ z4&6gp(hUy1HUpe&ZJ34Bh_)LC7e}%()j-n}G}6^T!;wUf)3F9LQ`Y0t)_F~87dJ-WVLnomDM@Wk{p0+z=djotNNz1Zk za=$P;N<$*$Y>H*0K5fXFP6cBjJrc=9-tO1Pg zeEZm>#MDbzLmFRtd`v+;hvlRfdSFer2d48*gO4L*`zS&<8m2|-a*E}B@B12s@lHV# zj;I$%Y5T&?J>d*EIow*pomE)&j6~7v-de~Xik~zD=EfTdge%L6jlkLmEY~5sc+BpP zH-Y=oytpm$*4s_u*LNX3$>CSc#~|kq3}-LYynP*O1eA^!ZHguupeD!>Y&Y zJQA%o<_(B(-GUXY1;W*9hpeaOp>jP}inkLwz6!LsJ>uG=N8V1+=tI%S?PdA-cR_3|UpOE*Hy^wWfUB^Cf_D8hL-!Jup^BHjZ71Xy#I7a(rT~CA8r}p=x0pLI9 z;k)dZfK{8whGbNSCGH2-=iWZ*u=4Ct2f%*;y142VjYju0uV)X418x~ zJtcJBo4`2^>*Fa{uPi$f&kMsyM}YHM4lj~BU$6Qk5a0CdWcH5kqVoZua5S+BzQ^*7 zgDYe%vTrTUX8E0;Q0`+r$mG394qiQ@Mn52#MpB^;u+9$@UDk?v+uYEmAj6g zmRgE$Wjy0;osoV<*6}4I_#ILlm6$ha;udkoU~wsfeq~Q=trM1EwcQ1=Pd z9iS@0GL!gS6MC~-7Vthy6peX%=EU?iAE=L0JqcmkGA( zF?#fZg11PayUjk~UJCALPr+C)yo~QJBtpla7qzQMRW-9F3xC6WDDQl090k%`$Qgkh z*t>*R!wA=E8R$$d^`z*YzkY}or4@MRVl2h8_@AL^1HFXF#S-Xe zKw+a!<2!GrU41rP(m9bePh{CU9G}xzgC}@e-3+?8zcpl-o~I3M%pDAMFnqil*J_RM z@pOeJX6BBfpm?sQ)s2|rJ+C>{Fdftsp8Juo2{FgFaIL<>=?J?l(y#+eiyhu-YqbFA zHe%xULqouVVWudWi?^s3FMlJnKYMRqsfj%fOJ z6GcV`p6KZu(Z#d8Ul0RHO$`pu&a|3?wTvpBa*g7fZ{w}7G2aoj`W7~JRGqBM`{Pd` zm9azrnl9I6Id5k>ch&_|9?Dz}%}P=&*y*#QcR6oXVEnH@XZ`Q%&->M1p-a48CiA6$ z|L~y9tAJ&1{e7%)nO`fiibBI0ZX=hcBU<&!tjYB@B9d}>M&amcpgXUb?>4v08N8pp z1;tYmtyYURj4GQ)?2H(dwr27MRIA%XuIaN#YiD_@sMRf)A@(*I_PND_o5F z-fIccU7)t_jxEGSetFont(lf); // Subclassing wpOrigEditProc = (WNDPROC) SetWindowLongPtr(m_message, @@ -212,7 +221,9 @@ LRESULT MainWindow::OnDestroy() DestroyWindow(m_cyrillic); DestroyWindow(m_greek); DestroyWindow(m_xsampa); + DestroyWindow(m_xsampa_table); delete m_preview; + delete m_data_display; return 0; } @@ -248,25 +259,26 @@ LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) GetClientRect(m_hwnd, &client); #define REPOS(hwnd, k) hdwp = DeferWindowPos(hdwp, hwnd, 0, k, SWP_NOACTIVATE|SWP_NOZORDER) hdwp = BeginDeferWindowPos(14); - REPOS(m_message, LEFT(12, 12, client.right - 24, client.bottom - 124)); - REPOS(m_goUp, BOTTOM(12, client.bottom - 59, 140, 20)); - REPOS(m_goMiddle, BOTTOM(12, client.bottom - 34, 140, 20)); - REPOS(m_goDown, BOTTOM(12, client.bottom - 9, 140, 20)); - REPOS(m_messMini, BOTTOM(160, client.bottom - 59, 120, 20)); - REPOS(m_messNormal, BOTTOM(160, client.bottom - 34, 120, 20)); - REPOS(m_messMax, BOTTOM(160, client.bottom - 9, 120, 20)); - REPOS(m_messCustom, BOTTOM(280, client.bottom - 59, 120, 20)); - REPOS(m_messLevel, BOTTOM(280, client.bottom - 38, 84, 20)); - REPOS(m_messUpDown, BOTTOM(362, client.bottom - 38, 18, 20)); - REPOS(m_mess, BOTTOMRIGHT(client.right - 12, client.bottom - 41, 100, 25)); - REPOS(m_unmess, BOTTOMRIGHT(client.right - 12, client.bottom - 12, 100, 25)); - REPOS(m_nfc, BOTTOMRIGHT(client.right - 117, client.bottom - 41, 50, 25)); - REPOS(m_nfd, BOTTOMRIGHT(client.right - 117, client.bottom - 12, 50, 25)); - REPOS(m_previewShow,BOTTOMRIGHT(client.right - 172, client.bottom - 12, 100, 25)); - REPOS(m_latin, BOTTOM(12, client.bottom - 84, 100, 25)); - REPOS(m_cyrillic, BOTTOM(117, client.bottom - 84, 100, 25)); - REPOS(m_greek, BOTTOM(222, client.bottom - 84, 100, 25)); - REPOS(m_xsampa, BOTTOM(327, client.bottom - 84, 100, 25)); + REPOS(m_message, LEFT(12, 12, client.right - 24, client.bottom - 124)); + REPOS(m_goUp, BOTTOM(12, client.bottom - 59, 140, 20)); + REPOS(m_goMiddle, BOTTOM(12, client.bottom - 34, 140, 20)); + REPOS(m_goDown, BOTTOM(12, client.bottom - 9, 140, 20)); + REPOS(m_messMini, BOTTOM(160, client.bottom - 59, 120, 20)); + REPOS(m_messNormal, BOTTOM(160, client.bottom - 34, 120, 20)); + REPOS(m_messMax, BOTTOM(160, client.bottom - 9, 120, 20)); + REPOS(m_messCustom, BOTTOM(280, client.bottom - 59, 120, 20)); + REPOS(m_messLevel, BOTTOM(280, client.bottom - 38, 84, 20)); + REPOS(m_messUpDown, BOTTOM(362, client.bottom - 38, 18, 20)); + REPOS(m_mess, BOTTOMRIGHT(client.right - 12, client.bottom - 41, 100, 25)); + REPOS(m_unmess, BOTTOMRIGHT(client.right - 12, client.bottom - 12, 100, 25)); + REPOS(m_nfc, BOTTOMRIGHT(client.right - 117, client.bottom - 41, 50, 25)); + REPOS(m_nfd, BOTTOMRIGHT(client.right - 117, client.bottom - 12, 50, 25)); + REPOS(m_previewShow, BOTTOMRIGHT(client.right - 172, client.bottom - 12, 100, 25)); + REPOS(m_latin, BOTTOM(12, client.bottom - 84, 100, 25)); + REPOS(m_cyrillic, BOTTOM(117, client.bottom - 84, 100, 25)); + REPOS(m_greek, BOTTOM(222, client.bottom - 84, 100, 25)); + REPOS(m_xsampa, BOTTOM(327, client.bottom - 84, 100, 25)); + REPOS(m_xsampa_table, BOTTOM(432, client.bottom - 84, 100, 25)); EndDeferWindowPos(hdwp); #undef REPOS return 0; @@ -288,8 +300,9 @@ LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) text = new wchar_t[textlen]; Edit_GetText(m_message, text, textlen); - SendMessage(m_preview->GetHWND(), WM_CHANGETEXT, 0, (LPARAM) text); - ShowWindow(m_preview->GetHWND(), SW_SHOW); + m_preview->ChangeText(text); + m_preview->ShowWindow(SW_SHOW); + break; } case TEXT_TO_NFC: OnTextNFC(); @@ -314,6 +327,21 @@ LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) case ZALGO_GO_DOWN: Button_SetCheck((HWND) lParam, !IsDlgButtonChecked(m_hwnd, LOWORD(wParam))); break; + case ZALGO_XSAMPA_TABLE: { + wchar_t *text; + + SetWindowText(m_data_display->GetHWND(), L"X-SAMPA Table"); + text = GetResourceString(RID_XSAMPA); + if (!text) { + MessageBox(m_hwnd, L"X-SAMPA table not found", L"X-SAMPA Error", MB_ICONERROR); + break; + } + m_data_display->ChangeText(text, false); + m_data_display->ShowWindow(SW_SHOW); + SetForegroundWindow(*m_data_display); + delete [] text; + break; + } default: Button_SetCheck(GetDlgItem(m_hwnd, ZALGO_MINI_MESS), 0); Button_SetCheck(GetDlgItem(m_hwnd, ZALGO_NORMAL_MESS), 0); @@ -348,19 +376,16 @@ LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) if (wParam == 'K' && GetKeyState(VK_CONTROL) < 0 && GetKeyState(VK_SHIFT) < 0 && GetKeyState(VK_MENU) < 0) { static bool unlocked = false; - // A user has tried to go beyond max, let is work - if (!unlocked && MessageBox(m_hwnd, L"\ -Thou hast tried to loosen my limits, dost thou promise that:\r\n\ - 1. thou will be responsible to use the string generated\r\n\ - 2. thou will be careful to not enter a number too large that will crash me\r\n\ - 3. if thou dost crash me, blame thyself for entering a number too large\r\n\ -\r\n\ -Dost thou agree?", L"About to Unlock Secret", MB_YESNO | MB_ICONQUESTION) == IDYES) { + LPWSTR text = GetResourceString(RID_LOOSE); + // A user has tried to go beyond max, let it work + if (!unlocked && MessageBox(m_hwnd, text,L"About to Unlock Secret", + MB_YESNO | MB_ICONQUESTION) == IDYES) { PostMessage(m_messUpDown, UDM_SETRANGE32, 1, LONG_MAX); PostMessage(m_messLevel, EM_SETREADONLY, 0, 0); unlocked = true; } + delete [] text; return 0; } break; diff --git a/src/PreviewWindow.cpp b/src/PreviewWindow.cpp index 271e1c6..375dae6 100644 --- a/src/PreviewWindow.cpp +++ b/src/PreviewWindow.cpp @@ -5,29 +5,14 @@ LRESULT PreviewWindow::OnCreate() { - NONCLIENTMETRICS ncmMetrics = { sizeof(NONCLIENTMETRICS) }; + LOGFONT lf; RECT client; - - SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncmMetrics, 0); GetClientRect(m_hwnd, &client); - - hFont = CreateFontIndirect(&ncmMetrics.lfMessageFont); - // Get the handle to the client area's device context. - HDC hdc = GetDC(m_hwnd); - TEXTMETRIC tm; - // Extract font dimensions from the text metrics. - GetTextMetrics(hdc, &tm); - xChar = tm.tmAveCharWidth; - xUpper =(tm.tmPitchAndFamily & 1 ? 3 : 2) * xChar/2; - yChar = tm.tmHeight + tm.tmExternalLeading; - // Free the device context. - ReleaseDC(m_hwnd, hdc); - // Set an arbitrary maximum width for client area. - //(xClientMax is the sum of the widths of 48 average - // lowercase letters and 12 uppercase letters.) - xClientMax = 48 * xChar + 12 * xUpper; - + GetMessageFont(lf); + hFont = CreateFontIndirect(&lf); + SendMessage(m_hwnd, WM_SETFONT, (WPARAM) hFont, FALSE); + lpLines = NULL; return 0; } @@ -60,7 +45,7 @@ void PreviewWindow::OnPaint() yPos = si.nPos; SelectObject(ps.hdc, hFont); - SetBkColor(ps.hdc, RGB(0xF0, 0xF0, 0xF0)); + SetBkMode(ps.hdc, TRANSPARENT); // Find painting limits. int FirstLine = max(0, yPos + ps.rcPaint.top / yChar); int LastLine = min(lines - 1, yPos + ps.rcPaint.bottom / yChar); @@ -69,16 +54,101 @@ void PreviewWindow::OnPaint() GetScrollInfo(m_hwnd, SB_HORZ, &si); xPos = si.nPos; for (int i = FirstLine; i <= LastLine; i++) { - int x = xChar *(1 - xPos); - int y = yChar *(i - yPos); + int x = xChar * (1 - xPos); + int y = yChar * (i - yPos); rect.top = y; rect.left = x; - DrawText(ps.hdc, lpLines[i], -1, &rect, DT_NOCLIP); + DrawText(ps.hdc, lpLines[i], -1, &rect, DT_EXPANDTABS | DT_NOCLIP | DT_NOPREFIX); } done: EndPaint(m_hwnd, &ps); } +void PreviewWindow::ChangeText(LPWSTR text, bool padding) +{ + if (lpLines) { + for (int i = 0; i < lines; ++i) + delete [] lpLines[i]; + delete [] lpLines; + } + + int l = 0, longest = 0, maxlen = 0; + LPWSTR str = text; + + if (*str == L'\0') { + empty = true; + lpLines = NULL; + } else empty = false; + + while (*str++) + if (*str == L'\n') + ++l; + + if (*(str-1) != L'\n') + ++l; + + lines = l; + if (padding) + lines += 8; + lpLines = new LPTSTR[lines]; + if (padding) { + for (int i = 0; i < 4; ++i) { + lpLines[i] = new TCHAR[1]; + lpLines[i][0] = L'\0'; + } + } + + for (int i = 0; i < l; ++i) { + LPTSTR start = text, end, buf; + int len; + while (*text++ != L'\n' && *(text-1) != L'\0'); + end = text - 1; + len = end - start; + if (len > maxlen) { + longest = i + (padding ? 4 : 0); + maxlen = len; + } + buf = new TCHAR[len + 1]; + memcpy(buf, start, len*sizeof(TCHAR)); + buf[len] = L'\0'; + lpLines[i + (padding ? 4 : 0)] = buf; + } + if (padding) { + for (int i = l + 4; i < lines; ++i) { + lpLines[i] = new TCHAR[1]; + lpLines[i][0] = L'\0'; + } + } + + int upper = 0, lower = 0; + for (LPTSTR i = lpLines[longest]; *i; ++i) { + if (isupper(*i)) + ++upper; + else if (!(*i >= 0x0300 && *i < 0x0370 || *i == 0x489)) + ++lower; + } + xClientMax = lower * xChar + upper * xUpper; + + PostMessage(m_hwnd, WM_SIZE, 0, 0); +} + +void PreviewWindow::SetFont(HFONT hFont) +{ + if (used_logfont) + DeleteFont(this->hFont); + used_logfont = false; + SendMessage(m_hwnd, WM_SETFONT, (WPARAM) hFont, TRUE); +} + +void PreviewWindow::SetFont(const LOGFONT &lf) +{ + if (used_logfont) + DeleteFont(this->hFont); + used_logfont = true; + hFont = CreateFontIndirect(&lf); + SendMessage(m_hwnd, WM_SETFONT, (WPARAM) hFont, TRUE); +} + LRESULT PreviewWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { @@ -230,69 +300,31 @@ LRESULT PreviewWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) } return 0; } - case WM_CHANGETEXT: { - if (lpLines) { - for (int i = 0; i < lines; ++i) - delete [] lpLines[i]; - delete [] lpLines; - } - - LPTSTR str = (LPTSTR) lParam; - int l = 0, longest = 0, maxlen = 0; - - if (*str == L'\0') { - empty = true; - lpLines = NULL; - } else empty = false; - - while (*str++) - if (*str == L'\n') - ++l; - - if (*(str-1) != L'\n') - ++l; - - lines = l + 8; - lpLines = new LPTSTR[lines]; - for (int i = 0; i < 4; ++i) { - lpLines[i] = new TCHAR[1]; - lpLines[i][0] = L'\0'; - } - str = (LPTSTR) lParam; - for (int i = 0; i < l; ++i) { - LPTSTR start = str, end, buf; - int len; - while (*str++ != L'\n' && *(str-1) != L'\0'); - end = str - 1; - len = end - start; - if (len > maxlen) { - longest = i+4; - maxlen = len; - } - buf = new TCHAR[len + 1]; - memcpy(buf, start, len*sizeof(TCHAR)); - buf[len] = L'\0'; - lpLines[i+4] = buf; - } - for (int i = l + 4; i < lines; ++i) { - lpLines[i] = new TCHAR[1]; - lpLines[i][0] = L'\0'; - } - - int upper = 0, lower = 0; - for (LPTSTR i = lpLines[longest]; *i; ++i) { - if (isupper(*i)) - ++upper; - else if (!(*i >= 0x0300 && *i < 0x0370 || *i == 0x489)) - ++lower; - } - xClientMax = lower * xChar + upper * xUpper; - - PostMessage(m_hwnd, WM_SIZE, 0, 0); + case WM_CHANGETEXT: + ChangeText((LPTSTR) lParam, (wParam & 1) == 0); return 0; + case WM_SETFONT: { + HDC hdc = GetDC(m_hwnd); + hFont = (HFONT) wParam; + HFONT hOldFont = SelectFont(hdc, hFont); + + TEXTMETRIC tm; + // Extract font dimensions from the text metrics. + GetTextMetrics(hdc, &tm); + xChar = tm.tmAveCharWidth; + xUpper =(tm.tmPitchAndFamily & 1 ? 3 : 2) * xChar/2; + yChar = tm.tmHeight + tm.tmExternalLeading; + // Set an arbitrary maximum width for client area. + //(xClientMax is the sum of the widths of 48 average + // lowercase letters and 12 uppercase letters.) + xClientMax = 48 * xChar + 12 * xUpper; + + SelectFont(hdc, hOldFont); + ReleaseDC(m_hwnd, hdc); + break; } case WM_CLOSE: - ShowWindow(m_hwnd, SW_HIDE); + ShowWindow(SW_HIDE); return 0; } diff --git a/src/Zalgo.cpp b/src/Zalgo.cpp index 7fc62b6..b5f44ba 100644 --- a/src/Zalgo.cpp +++ b/src/Zalgo.cpp @@ -19,7 +19,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi MainWindow *win = MainWindow::Create(L"U̬̩͔̐̿ṅ̨̤̬̽i̛̘̳̹ͤ̍c̠̳̬͛o̘̳͗̿̈́d̗̅̓͗͠ḛ͍͛ ̬̠̹̉ͭ͛D̨̤̰̀̂ͦì̛͇͂a͍͇͛ͨͤc̡̟͖͗̔ͤͅr͔͇ͮ̓̍͢i̴͇͇̇͋̽t̛̟̟͋i̲̘̿̊c̺͎ͬ͗-̴̤́̔P̋̍҉͎̹ö̲̯͈̋͞l͎͂l̻̖ͩ̂͜u̵͔̳̇̋t̳̪͐̎e͈̓͢ḓ̗̭̓ ̹̫͛Ṭͫe̙̝̦̊̊̑͢x̶͉ͦ̚t̞̔̈́̀ ̡̪̪̙͒͗G̘̜̋e͍̯̻͋ͬͦn̹̩̫̑̈́ẽ͔̳r̠͙͒̀̅ͅa̭ͫ̓́t̘̺̋̏̚o̰̙̦ͪṛͦͣ́ͅ"); if (win) { - ShowWindow(win->GetHWND(), nCmdShow); + win->ShowWindow(nCmdShow); MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg);