Fixed issue #2507: Support keyboard shortcuts in yes/no prompts
[TortoiseGit.git] / src / Utils / MiscUI / HTMLFormatter.h
blobd2e7a55dc6238f9fd0bba2129b7a511a0b497951
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006 - Stefan Kueng
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
21 /**
22 * \ingroup Utils
23 * Class for drawing formatted text on a device context.
25 class CHTMLFormatter
27 public:
28 CHTMLFormatter(void);
29 ~CHTMLFormatter(void);
30 /**
31 * Draws formatted text to the given device context with the given font. The following formatting
32 * parameters are available:\n
33 * - \c \<b> \e text \c \</b> \t draws the text in bold
34 * - \c \<u> \e text \c \</u> \t draws the text underlined
35 * - \c \<i> \e text \c \</u> \t draws the text in italic
36 * - \c \<s> \e text \c \</u> \t draws the text with strikeout
38 * - \c \<ct=0x123456> \e text \c \</ct> \t draws the text in the given color. The value is in RGB format.
39 * - \c \<cb=0x123456> \e text \c \</cb> \t draws the background of the text in the given color. The value is in RGB format.
41 * - \c \<al> \e text \c \</al> \t aligns the text to the left
42 * - \c \<ac> \e text \c \</ac> \t aligns the text to the center
43 * - \c \<ar> \e text \c \</ar> \t aligns the text to the right
45 * - \c \<hr=50%> \t draws a horizontal line with 50% of the whole width
46 * - \c \<hr=100> \t draws a horizontal line with 100 pixels length
48 * - \c \<a=http://something.com> \e text \c \</a> \t the text is marked as a link in the internal link list.
50 * also the common control codes \\n, \\t, \\r are recognized.
52 * An example of usage:
53 * \code
54 * CHTMLFormatter formatter;
56 * CString strInfo = _T("<ct=0x0000FF><<b></ct>text<ct=0x0000FF><</b></ct><t=8> - <b>Bold text</b>\n");
57 * strInfo += _T("<ct=0x0000FF><<i></ct>text<ct=0x0000FF><</i></ct><t=8> - <i>Italic text</i>\n");
58 * strInfo += _T("<ct=0x0000FF><<u></ct>text<ct=0x0000FF><</u></ct><t=8> - <u>Underline text</u>\n");
59 * strInfo += _T("<ct=0x0000FF><<s></ct>text<ct=0x0000FF><</s></ct><t=8> - <s>Strikeout text</s>\n");
60 * strInfo += _T("<ct=0x0000FF><<ct=0x0000FF></ct>text<ct=0x0000FF><</ct></ct><t=5> - <ct=0x0000FF>Red text</ct>\n");
61 * strInfo += _T("<ct=0x0000FF><<cb=0xFFFF00></ct>text<ct=0x0000FF><</cb></ct><t=5> - <cb=0xFFFF00>Cyan background</cb>\n");
62 * strInfo += _T("<ct=0x0000FF><<t></ct><t=10> - Tabulation\n");
63 * strInfo += _T("<ct=0x0000FF><<hr=80%></ct><t=9> - Horizontal line\n");
64 * strInfo += _T("<hr=80%>\n");
65 * strInfo += _T("<ct=0x0000FF><<al></ct><t=10> - Left align\n");
66 * strInfo += _T("<ct=0x0000FF><<ac></ct><t=10> - Center align\n");
67 * strInfo += _T("<ct=0x0000FF><<ar></ct><t=10> - Right align\n");
68 * strInfo += _T("<ct=0x0000FF><<a=http://somelink.com><<u></ct>link<ct=0x0000FF><</u><</a></ct><t> - Link\n");
70 * formatter.DrawHTML(pDC, rect, strInfo, font);
71 * \endcode
72 * this example produces the following picture:
73 * \image html "htmlformatter.png"
75 * \remarks please be aware that this is a lightweight class and not a real HTML printer. Only very basic
76 * tags are available and folding tags is also very limited.\n
77 * The link tag also has restrictions:
78 * - a link must not span more than one line of text
79 * - inner tags are allowed, but only for the whole link text. I.e. "<a=http://something.com><b>this</b> is my link</a>" is not allowed!
81 * \param pDC the device context to draw the text on
82 * \param rect the rectangle to draw the text within
83 * \param str the string to draw
84 * \param font the font to draw the text with
85 * \param bCalculate if set to TRUE, then no drawing is done but only the size of the required rectangle to fit
86 * the text is calculated.
87 * \return The required size of the rectangle to fit the text in.
89 CSize DrawHTML(CDC * pDC, CRect rect, CString str, LOGFONT font, BOOL bCalculate = FALSE);
91 /**
92 * Checks if a given point is over a hyperlink text
94 BOOL IsPointOverALink(CPoint pt);
96 /**
97 * Returns the URL of the link or an empty string if the point is not over a hyperlink text.
99 CString GetLinkForPoint(CPoint pt);
101 protected:
102 typedef enum{ NONE = 0,
103 BOLD,
104 ITALIC,
105 STRIKE,
106 UNDERLINE,
107 COLOR_TEXT,
108 COLOR_BK,
109 NEW_LINE,
110 TABULATION,
111 HORZ_LINE,
112 HORZ_LINE_PERCENT,
113 LINK
114 } COMMAND;
116 typedef enum{ BEGIN_TAG = 0,
117 END_TAG,
118 TEXT_TAG,
119 BEGIN_NUMBER,
120 TEXT_NUMBER,
121 BEGIN_TEXT,
122 TEXT,
123 PERCENT,
124 BEGIN_URL,
125 TEXT_URL
126 } STATEMACHINE;
128 enum{ ALIGN_LEFT = 0,
129 ALIGN_CENTER,
130 ALIGN_RIGHT
133 * Draws a horizontal line to the device context
134 * \param pDC the device context to draw to
135 * \param xStart the starting x coordinate of the line
136 * \param xEnd the ending x coordinate of the line
137 * \param y the y coordinate of the line
139 static void DrawHorzLine(CDC * pDC, int xStart, int xEnd, int y);
142 CArray<CRect, CRect&> m_arLinkRects;
143 CStringArray m_arLinkURLs;