| 1 |
/* AbiWord |
| 2 |
* Copyright (C) 2002 Gilles Saint-Denis. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "ut_assert.h" |
| 21 |
#include "ut_debugmsg.h" |
| 22 |
#include "xap_Win32App.h" |
| 23 |
#include "xap_Win32LabelledSeparator.h" |
| 24 |
#include <commctrl.h> |
| 25 |
|
| 26 |
/*****************************************************************/ |
| 27 |
|
| 28 |
#define GWL(hwnd) (HFONT)GetWindowLongPtrW((hwnd), GWLP_USERDATA) |
| 29 |
#define SWL(hwnd, f) SetWindowLongPtrW((hwnd), GWLP_USERDATA,(LONG_PTR)(f)) |
| 30 |
|
| 31 |
/*! |
| 32 |
Spacing between the label text and the line separator part of the |
| 33 |
control. |
| 34 |
*/ |
| 35 |
#define TEXT_LINE_SPACING 2 |
| 36 |
/*! |
| 37 |
Line separator height in pixel |
| 38 |
*/ |
| 39 |
#define LINE_HEIGHT 2 |
| 40 |
/*! |
| 41 |
Identifier of the child line separator window. |
| 42 |
*/ |
| 43 |
#define IDC_LINE_SEPARATOR 1 |
| 44 |
|
| 45 |
/*! |
| 46 |
Windows control class name to use in the creation function or template. |
| 47 |
*/ |
| 48 |
static const wchar_t s_LabelledSeparatorWndClassName[] = L"AbiLabelledSeparator"; |
| 49 |
/*! |
| 50 |
Window procedure function of the control base class. |
| 51 |
*/ |
| 52 |
static WNDPROC s_pfnWndProc; |
| 53 |
|
| 54 |
/*****************************************************************/ |
| 55 |
|
| 56 |
/*! |
| 57 |
Function to adapt the separator line length to the remaining |
| 58 |
space available after the displayed text. |
| 59 |
\param hwnd the conrol window handle |
| 60 |
\param text the text to be shown by the control |
| 61 |
\param hFont the font handle used to represent text |
| 62 |
*/ |
| 63 |
static void AdaptSeparatorLength(HWND hwnd, const wchar_t* text, HFONT hFont) |
| 64 |
{ |
| 65 |
// Provide the right font |
| 66 |
LOGFONTW logFont; |
| 67 |
int status = GetObjectW(hFont, sizeof(LOGFONTW), &logFont); |
| 68 |
UT_return_if_fail(status); |
| 69 |
HFONT hCtrlFont = CreateFontIndirectW(&logFont); |
| 70 |
UT_return_if_fail(hCtrlFont); |
| 71 |
|
| 72 |
// Evaluate text size |
| 73 |
int length = wcslen(text); |
| 74 |
SIZE textSize; |
| 75 |
HFONT hPrevCtrlFont; |
| 76 |
HDC hdc = GetDC(hwnd); |
| 77 |
hPrevCtrlFont = (HFONT) SelectObject(hdc, hFont); |
| 78 |
GetTextExtentPoint32W(hdc, text, length, &textSize); |
| 79 |
SelectObject(hdc, hPrevCtrlFont); |
| 80 |
|
| 81 |
// Modifiy separator line length |
| 82 |
RECT controlRect; |
| 83 |
HWND hLine = GetDlgItem(hwnd, IDC_LINE_SEPARATOR); |
| 84 |
GetClientRect(hwnd, &controlRect); |
| 85 |
if(textSize.cx > 0) |
| 86 |
{ |
| 87 |
textSize.cx += TEXT_LINE_SPACING; |
| 88 |
} |
| 89 |
UT_sint32 lineWidth = controlRect.right - textSize.cx; |
| 90 |
if(lineWidth < 0) |
| 91 |
{ |
| 92 |
lineWidth = 0; |
| 93 |
} |
| 94 |
MoveWindow(hLine, |
| 95 |
textSize.cx, |
| 96 |
(controlRect.bottom - controlRect.top) / 2, |
| 97 |
lineWidth, LINE_HEIGHT, true); |
| 98 |
} |
| 99 |
|
| 100 |
/*! |
| 101 |
Window procedure function responsible for implementing the special |
| 102 |
behavior of the control. |
| 103 |
\param hwnd the control window handle |
| 104 |
\param iMsg the message identifier to process |
| 105 |
\param wParam first message parameter |
| 106 |
\param lParam second message parameter |
| 107 |
\result the result of the message processing which depend on the actual |
| 108 |
message processed |
| 109 |
*/ |
| 110 |
static LRESULT CALLBACK _LabelledSeparatorWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) |
| 111 |
{ |
| 112 |
switch (iMsg) |
| 113 |
{ |
| 114 |
case WM_CREATE: |
| 115 |
{ |
| 116 |
CREATESTRUCTW* lpCreate = (CREATESTRUCTW *) lParam; |
| 117 |
UT_DebugOnly<HWND> separator = CreateWindowW(L"STATIC", |
| 118 |
NULL, |
| 119 |
SS_ETCHEDHORZ | WS_CHILD | WS_VISIBLE, |
| 120 |
0, lpCreate->cy / 2, lpCreate->cx, LINE_HEIGHT, |
| 121 |
hwnd, (HMENU) IDC_LINE_SEPARATOR, |
| 122 |
lpCreate->hInstance, NULL); |
| 123 |
UT_ASSERT_HARMLESS(separator); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
case WM_SETFONT: |
| 128 |
{ |
| 129 |
SWL(hwnd, (HFONT) wParam); |
| 130 |
|
| 131 |
int length = GetWindowTextLengthW(hwnd); |
| 132 |
wchar_t* text = new wchar_t[length + 1]; |
| 133 |
length = GetWindowTextW(hwnd, text, length + 1); |
| 134 |
AdaptSeparatorLength(hwnd, text, (HFONT) wParam); |
| 135 |
delete [] text; |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
case WM_SETTEXT: |
| 140 |
{ |
| 141 |
AdaptSeparatorLength(hwnd, (const wchar_t*) lParam, (HFONT) GWL(hwnd)); |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
default: |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
return CallWindowProcW(s_pfnWndProc, hwnd, iMsg, wParam, lParam); |
| 150 |
} |
| 151 |
|
| 152 |
/*! |
| 153 |
Register the labelled separator control class with Windows. |
| 154 |
This function must be called before the first labelled separator control |
| 155 |
is created. |
| 156 |
|
| 157 |
A labelled separator control is like a static control with |
| 158 |
a line extending at the end of the text portion. The labelled separator |
| 159 |
control adapt the separator length to the text width. Thus, localized |
| 160 |
label can be immediately followed by the separator line. |
| 161 |
|
| 162 |
A labelled separator control can be created in a resource file by a |
| 163 |
statement like the following: |
| 164 |
\verbatim |
| 165 |
CONTROL "Actual label",IDC_OF_CONTROL,"AbiLabelledSeparator",0x0,7,17,116,8 |
| 166 |
\endverbatim |
| 167 |
The last four numbers are the usual x, y, width and height parameters |
| 168 |
in windows dialog coordinates. |
| 169 |
|
| 170 |
\param app the application object |
| 171 |
\return an indication of the class registration success |
| 172 |
*/ |
| 173 |
bool XAP_Win32LabelledSeparator_RegisterClass(XAP_Win32App * app) |
| 174 |
{ |
| 175 |
WNDCLASSEXW wndclass; |
| 176 |
HINSTANCE hinst = app->getInstance(); |
| 177 |
UT_DebugOnly<ATOM> a; |
| 178 |
|
| 179 |
wndclass.cbSize = sizeof(WNDCLASSEXW); |
| 180 |
if(GetClassInfoExW(hinst, s_LabelledSeparatorWndClassName, &wndclass)) |
| 181 |
{ |
| 182 |
// class information already registered |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
// get base class information |
| 187 |
if( ! GetClassInfoExW(NULL, L"STATIC", &wndclass)) |
| 188 |
{ |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
s_pfnWndProc = wndclass.lpfnWndProc; |
| 193 |
wndclass.cbSize = sizeof(wndclass); |
| 194 |
wndclass.lpfnWndProc = _LabelledSeparatorWndProc; |
| 195 |
wndclass.hInstance = hinst; |
| 196 |
wndclass.lpszClassName = s_LabelledSeparatorWndClassName; |
| 197 |
wndclass.lpszMenuName = NULL; |
| 198 |
|
| 199 |
a = RegisterClassExW(&wndclass); |
| 200 |
UT_ASSERT(a); |
| 201 |
|
| 202 |
return true; |
| 203 |
} |
| 204 |
|