OSDN Git Service

Change Dir Structure to be same as TortoiseSVN'
[tortoisegit/TortoiseGitJp.git] / src / Utils / MiscUI / ExtTextOutFL.h
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #pragma once\r
20 #include <mlang.h>\r
21 \r
22 \r
23 // these functions are taken from an article of the 'old new thing' blog:\r
24 // http://blogs.msdn.com/oldnewthing/archive/2004/07/16/185261.aspx\r
25 \r
26 \r
27 HRESULT TextOutFL(HDC hdc, int x, int y, LPCWSTR psz, int cch)\r
28 {\r
29         HRESULT hr;\r
30         IMLangFontLink2 *pfl;\r
31         if (SUCCEEDED(hr = CoCreateInstance(CLSID_CMultiLanguage, NULL,\r
32                 CLSCTX_ALL, IID_IMLangFontLink2, (void**)&pfl))) \r
33         {\r
34                         HFONT hfOrig = (HFONT)GetCurrentObject(hdc, OBJ_FONT);\r
35                         POINT ptOrig;\r
36                         DWORD dwAlignOrig = GetTextAlign(hdc);\r
37                         if (!(dwAlignOrig & TA_UPDATECP)) \r
38                         {\r
39                                 SetTextAlign(hdc, dwAlignOrig | TA_UPDATECP);\r
40                         }\r
41                         MoveToEx(hdc, x, y, &ptOrig);\r
42                         DWORD dwFontCodePages = 0;\r
43                         hr = pfl->GetFontCodePages(hdc, hfOrig, &dwFontCodePages);\r
44                         if (SUCCEEDED(hr)) \r
45                         {\r
46                                 while (cch > 0) \r
47                                 {\r
48                                         DWORD dwActualCodePages;\r
49                                         long cchActual;\r
50                                         hr = pfl->GetStrCodePages(psz, cch, dwFontCodePages, &dwActualCodePages, &cchActual);\r
51                                         if (FAILED(hr)) \r
52                                         {\r
53                                                 break;\r
54                                         }\r
55 \r
56                                         if (dwActualCodePages & dwFontCodePages) \r
57                                         {\r
58                                                 TextOut(hdc, 0, 0, psz, cchActual);\r
59                                         } \r
60                                         else \r
61                                         {\r
62                                                 HFONT hfLinked;\r
63                                                 if (FAILED(hr = pfl->MapFont(hdc, dwActualCodePages, 0, &hfLinked))) \r
64                                                 {\r
65                                                         break;\r
66                                                 }\r
67                                                 SelectObject(hdc, (HGDIOBJ)(HFONT)hfLinked);\r
68                                                 TextOut(hdc, 0, 0, psz, cchActual);\r
69                                                 SelectObject(hdc, (HGDIOBJ)(HFONT)hfOrig);\r
70                                                 pfl->ReleaseFont(hfLinked);\r
71                                         }\r
72                                         psz += cchActual;\r
73                                         cch -= cchActual;\r
74                                 }\r
75                                 if (FAILED(hr)) \r
76                                 {\r
77                                         //  We started outputting characters so we have to finish.\r
78                                         //  Do the rest without font linking since we have no choice.\r
79                                         TextOut(hdc, 0, 0, psz, cch);\r
80                                         hr = S_FALSE;\r
81                                 }\r
82                         }\r
83 \r
84                         pfl->Release();\r
85 \r
86                         if (!(dwAlignOrig & TA_UPDATECP)) \r
87                         {\r
88                                 SetTextAlign(hdc, dwAlignOrig);\r
89                                 MoveToEx(hdc, ptOrig.x, ptOrig.y, NULL);\r
90                         }\r
91         }\r
92 \r
93         return hr;\r
94 }\r
95 \r
96 void TextOutTryFL(HDC hdc, int x, int y, LPCWSTR psz, int cch)\r
97 {\r
98         if (FAILED(TextOutFL(hdc, x, y, psz, cch))) \r
99         {\r
100                 TextOut(hdc, x, y, psz, cch);\r
101         }\r
102 }\r
103 \r