Debian Codegraph @

src/af/xap/win/xap_Win32Dlg_FontChooser.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998,1999 AbiSource, Inc.
3
4 *
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.
9 *
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.
14 *
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 */
20
21 #include <windows.h>
22 #include <stdio.h>
23 #include "ut_assert.h"
24 #include "ut_debugmsg.h"
25 #include "ut_string.h"
26 #include "ut_misc.h"
27 #include "ut_Win32LocaleString.h"
28
29 #include "xap_App.h"
30 #include "xap_EncodingManager.h"
31 #include "xap_Win32App.h"
32 #include "xap_Win32FrameImpl.h"
33 #include "xap_Win32DialogBase.h"
34
35 #include "xap_Strings.h"
36 #include "xap_Dialog_Id.h"
37 #include "xap_Win32Dlg_FontChooser.h"
38 #include "xap_Win32PreviewWidget.h"
39 #include "gr_Graphics.h"
40 #include "gr_Win32Graphics.h"
41
42 #include "xap_Win32Resources.rc2"
43
44 /*****************************************************************/
45 XAP_Dialog * XAP_Win32Dialog_FontChooser::static_constructor(XAP_DialogFactory * pFactory,
46 XAP_Dialog_Id id)
47 {
48 XAP_Win32Dialog_FontChooser * p = new XAP_Win32Dialog_FontChooser(pFactory,id);
49 return p;
50 }
51
52 XAP_Win32Dialog_FontChooser::XAP_Win32Dialog_FontChooser(XAP_DialogFactory * pDlgFactory,
53 XAP_Dialog_Id id)
54 : XAP_Dialog_FontChooser(pDlgFactory,id),
55 m_pPreviewWidget(NULL),
56 m_bWin32Overline(false),
57 m_iColorIndx(0),
58 m_iColorCount(0)
59 {
60 }
61
62 XAP_Win32Dialog_FontChooser::~XAP_Win32Dialog_FontChooser(void)
63 {
64 DELETEP(m_pPreviewWidget);
65 }
66
67 /*****************************************************************/
68
69 void XAP_Win32Dialog_FontChooser::runModal(XAP_Frame * pFrame)
70 {
71 UT_return_if_fail(pFrame);
72
73 XAP_Win32App * pApp = static_cast<XAP_Win32App *>(XAP_App::getApp());
74 UT_return_if_fail(pApp);
75 const XAP_EncodingManager *pEncMan = pApp->getEncodingManager();
76 UT_return_if_fail(pEncMan);
77 UT_Win32LocaleString family;
78
79 UT_DEBUGMSG(("FontChooserStart: Family[%s] Size[%s] Weight[%s] Style[%s] Color[%s] Underline[%d] StrikeOut[%d]\n",
80 m_sFontFamily.c_str(),
81 m_sFontSize.c_str(),
82 m_sFontWeight.c_str(),
83 m_sFontStyle.c_str(),
84 m_sColor.c_str(),
85 m_bUnderline,
86 m_bStrikeout));
87
88 m_bWin32Overline = m_bOverline;
89 m_bWin32Hidden = m_bHidden;
90 m_bWin32SuperScript = m_bSuperScript;
91 m_bWin32SubScript = m_bSubScript;
92
93
94 /*
95 WARNING: any changes to this function should be closely coordinated
96 with the equivalent logic in Win32Graphics::FindFont()
97 */
98 LOGFONTW lf;
99 memset(&lf, 0, sizeof(lf));
100
101 CHOOSEFONTW cf;
102 memset(&cf, 0, sizeof(cf));
103 cf.lStructSize = sizeof(cf);
104 cf.hwndOwner = static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow();
105 cf.lpLogFont = &lf;
106 cf.Flags = CF_SCREENFONTS |
107 CF_EFFECTS |
108 CF_ENABLEHOOK |
109 CF_ENABLETEMPLATE |
110 CF_INITTOLOGFONTSTRUCT;
111 cf.lpTemplateName = MAKEINTRESOURCEW(XAP_RID_DIALOG_FONT);
112 cf.lpfnHook = (LPCFHOOKPROC) s_hookProc;
113 cf.lCustData = (LPARAM) this;
114 cf.hInstance = pApp->getInstance();
115
116 if (!m_sFontFamily.empty())
117 {
118 family.fromUTF8 (m_sFontFamily.c_str());
119 lstrcpynW(lf.lfFaceName,family.c_str(),LF_FACESIZE);
120 }
121 else
122 cf.Flags |= CF_NOFACESEL;
123
124 if (!m_sFontSize.empty())
125 {
126 UT_ASSERT(sizeof(char) == sizeof(gchar));
127 lf.lfHeight = (long) -(UT_convertToPoints(m_sFontSize.c_str()))*4/3;
128 }
129 else
130 cf.Flags |= CF_NOSIZESEL;
131
132 if (!m_sFontWeight.empty())
133 {
134 if (g_ascii_strcasecmp(m_sFontWeight.c_str(),"bold") == 0)
135 lf.lfWeight = 700;
136 // TODO do we need any others here...
137 }
138 else
139 cf.Flags |= CF_NOSTYLESEL;
140
141 if (!m_sFontStyle.empty())
142 {
143 if (g_ascii_strcasecmp(m_sFontStyle.c_str(),"italic") == 0)
144 lf.lfItalic = TRUE;
145 }
146 else
147 cf.Flags |= CF_NOSTYLESEL;
148
149 if (!m_sColor.empty())
150 {
151 UT_RGBColor c;
152 UT_parseColor(m_sColor.c_str(),c);
153 cf.rgbColors = RGB(c.m_red,c.m_grn,c.m_blu);
154 }
155
156 if (m_bUnderline)
157 lf.lfUnderline = TRUE;
158 if (m_bStrikeout)
159 lf.lfStrikeOut = TRUE;
160
161 // run the actual dialog...
162 m_answer = (ChooseFontW(&cf) ? a_OK : a_CANCEL);
163 // Convert the font name returned by the Windows Font Chooser
164 // to UTF-8.
165 family.fromLocale (lf.lfFaceName);
166 UT_UTF8String family_utf = family.utf8_str();
167 const char *szFontFamily = family_utf.utf8_str();
168
169 if (m_answer == a_OK)
170 {
171 if(!m_sFontFamily.empty())
172 {
173 if((g_ascii_strcasecmp(szFontFamily, m_sFontFamily.c_str()) != 0))
174 {
175 m_bChangedFontFamily = true;
176 m_sFontFamily = szFontFamily;
177 }
178 }
179 else
180 {
181 if(szFontFamily[0])
182 {
183 m_bChangedFontFamily = true;
184 m_sFontFamily = szFontFamily;
185 }
186 }
187
188 bool bIsSizeValid = ((cf.Flags & CF_NOSIZESEL) == 0);
189 // bool bWasSizeValid = !m_sFontSize.empty();
190 char bufSize[10];
191 if (bIsSizeValid)
192 sprintf(bufSize,"%dpt",(cf.iPointSize/10));
193 else
194 bufSize[0] = 0;
195
196 // why? let's see
197 if (bIsSizeValid && (g_ascii_strcasecmp(bufSize,m_sFontSize.c_str()) != 0))
198 {
199 m_bChangedFontSize = true;
200 m_sFontSize = bufSize;
201 }
202 else
203 {
204 /* nothing changed */
205 }
206
207 bool bIsBold = ((cf.nFontType & BOLD_FONTTYPE) != 0);
208 bool bWasBold = (g_ascii_strcasecmp(m_sFontWeight.c_str(),"bold") == 0);
209 bool bIsNormal = ((cf.nFontType & REGULAR_FONTTYPE) != 0);
210 bool bWasNormal = (m_sFontWeight.empty() || (g_ascii_strcasecmp(m_sFontWeight.c_str(),"normal") != 0));
211 if ((bIsBold != bWasBold) || (bIsNormal != bWasNormal))
212 {
213 m_bChangedFontWeight = true;
214 if( bIsBold )
215 m_sFontWeight = "bold";
216 else
217 m_sFontWeight = "normal";
218 }
219
220 bool bIsItalic = ((cf.nFontType & ITALIC_FONTTYPE) != 0);
221 bool bWasItalic = (g_ascii_strcasecmp(m_sFontStyle.c_str(),"italic") == 0);
222 if (bIsItalic != bWasItalic)
223 {
224 m_bChangedFontStyle = true;
225 if( bIsItalic )
226 m_sFontStyle = "italic";
227 else
228 m_sFontStyle = "normal";
229 }
230
231 char bufColor[10];
232 sprintf(bufColor,"%02x%02x%02x",GetRValue(cf.rgbColors),
233 GetGValue(cf.rgbColors),GetBValue(cf.rgbColors));
234 bool bWasColorValid = !m_sColor.empty();
235
236 if ( m_bChangedColor && ((bWasColorValid && (g_ascii_strcasecmp(bufColor,m_sColor.c_str()) != 0))
237 || (!bWasColorValid && (g_ascii_strcasecmp(bufColor,"000000") != 0))))
238 {
239 m_sColor = bufColor;
240 }
241
242 m_bChangedUnderline = ((lf.lfUnderline == TRUE) != m_bUnderline);
243 m_bChangedStrikeOut = ((lf.lfStrikeOut == TRUE) != m_bStrikeout);
244 m_bChangedOverline = (m_bWin32Overline != m_bOverline);
245 if (m_bChangedUnderline ||
246 m_bChangedStrikeOut ||
247 m_bChangedOverline)
248 setFontDecoration( (lf.lfUnderline == TRUE),
249 m_bWin32Overline,
250 (lf.lfStrikeOut == TRUE), NULL, NULL);
251
252 m_bChangedHidden = (m_bWin32Hidden != m_bHidden);
253 m_bChangedSuperScript = (m_bWin32SuperScript != m_bSuperScript);
254 m_bChangedSubScript = (m_bWin32SubScript != m_bSubScript);
255
256 if(m_bChangedHidden)
257 setHidden(m_bWin32Hidden);
258
259 if(m_bChangedSuperScript)
260 setSuperScript(m_bWin32SuperScript);
261
262 if(m_bChangedSubScript)
263 setSubScript(m_bWin32SubScript);
264 }
265
266 UT_DEBUGMSG(("FontChooserEnd: Family[%s%s] Size[%s%s] Weight[%s%s] Style[%s%s] Color[%s%s] Underline[%d%s] StrikeOut[%d%s]\n",
267 m_sFontFamily.c_str(), ((m_bChangedFontFamily) ? "(chg)" : ""),
268 m_sFontSize.c_str(), ((m_bChangedFontSize) ? "(chg)" : ""),
269 m_sFontWeight.c_str(), ((m_bChangedFontWeight) ? "(chg)" : ""),
270 m_sFontStyle.c_str(), ((m_bChangedFontStyle) ? "(chg)" : ""),
271 m_sColor.c_str(), ((m_bChangedColor) ? "(chg)" : ""),
272 m_bUnderline, ((m_bChangedUnderline) ? "(chg)" : ""),
273 m_bStrikeout, ((m_bChangedStrikeOut) ? "(chg)" : "")));
274
275 // the caller can get the answer from getAnswer().
276
277 m_pWin32Frame = NULL;
278 }
279
280 UINT CALLBACK XAP_Win32Dialog_FontChooser::s_hookProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
281 {
282 // static routine
283 XAP_Win32Dialog_FontChooser* pThis;
284
285 switch(msg)
286 {
287 case WM_INITDIALOG:
288 pThis = (XAP_Win32Dialog_FontChooser *) ((CHOOSEFONTW *)lParam)->lCustData;
289 SetWindowLongPtrW(hDlg,DWLP_USER,(LPARAM) pThis);
290 return pThis->_onInitDialog(hDlg,wParam,lParam);
291
292 case WM_COMMAND:
293 pThis = (XAP_Win32Dialog_FontChooser *)GetWindowLongPtrW(hDlg,DWLP_USER);
294 if (pThis)
295 return pThis->_onCommand(hDlg,wParam,lParam);
296 else
297 return 0;
298
299 case WM_HELP:
300 pThis = (XAP_Win32Dialog_FontChooser *)GetWindowLongPtrW(hDlg,DWLP_USER);
301 if (pThis)
302 return pThis->_callHelp();
303 else
304 return 0;
305
306 default:
307 return 0;
308
309 }
310 // Default Dialog handles all other issues
311 return 0;
312 }
313
314 #define _DS(c,s) setDlgItemText(XAP_RID_DIALOG_##c,pSS->getValue(XAP_STRING_ID_##s))
315
316 BOOL XAP_Win32Dialog_FontChooser::_onInitDialog(HWND hWnd, WPARAM /*wParam*/, LPARAM /*lParam*/)
317 {
318 XAP_App* pApp = XAP_App::getApp();
319 const XAP_StringSet* pSS = pApp->getStringSet();
320
321 m_hDlg = hWnd;
322
323 setDialogTitle(pSS->getValue(XAP_STRING_ID_DLG_UFS_FontTitle));
324
325 // localize controls
326 _DS(FONT_TEXT_FONT, DLG_UFS_FontLabel);
327 _DS(FONT_TEXT_FONT_STYLE, DLG_UFS_StyleLabel);
328 _DS(FONT_TEXT_SIZE, DLG_UFS_SizeLabel);
329 _DS(FONT_TEXT_EFFECTS, DLG_UFS_EffectsFrameLabel);
330 _DS(FONT_BTN_STRIKEOUT, DLG_UFS_StrikeoutCheck);
331 _DS(FONT_BTN_UNDERLINE, DLG_UFS_UnderlineCheck);
332 _DS(FONT_CHK_OVERLINE, DLG_UFS_OverlineCheck);
333 _DS(FONT_TEXT_COLOR, DLG_UFS_ColorLabel);
334 _DS(FONT_TEXT_SCRIPT, DLG_UFS_ScriptLabel);
335 _DS(FONT_TEXT_SAMPLE, DLG_UFS_SampleFrameLabel);
336 _DS(FONT_BTN_OK, DLG_OK);
337 _DS(FONT_BTN_CANCEL, DLG_Cancel);
338 _DS(FONT_CHK_HIDDEN, DLG_UFS_HiddenCheck);
339 _DS(FONT_CHK_SUPERSCRIPT, DLG_UFS_SuperScript);
340 _DS(FONT_CHK_SUBSCRIPT, DLG_UFS_SubScript);
341 _DS(FONT_CHK_ALLCAPS, DLG_UFS_Effects_UpperCase);
342 _DS(FONT_CHK_SMALLCAPS, DLG_UFS_Effects_SmallCaps);
343
344 // set initial state
345 if( m_bWin32Overline )
346 CheckDlgButton( hWnd, XAP_RID_DIALOG_FONT_CHK_OVERLINE, BST_CHECKED );
347
348 if( m_bWin32Hidden )
349 CheckDlgButton( hWnd, XAP_RID_DIALOG_FONT_CHK_HIDDEN, BST_CHECKED );
350
351 if( m_bWin32SuperScript)
352 CheckDlgButton(hWnd, XAP_RID_DIALOG_FONT_CHK_SUPERSCRIPT, BST_CHECKED );
353
354 if( m_bWin32SubScript)
355 CheckDlgButton(hWnd, XAP_RID_DIALOG_FONT_CHK_SUBSCRIPT, BST_CHECKED );
356
357 // use the owner-draw-control dialog-item (aka window) specified in the
358 // dialog resource file as a parent to the window/widget that we create
359 // here and thus have complete control of.
360 // m_pPreviewWidget = new XAP_Win32PreviewWidget(static_cast<XAP_Win32App *>(m_pApp),
361 // GetDlgItem(hWnd, XAP_RID_DIALOG_FONT_PREVIEW),
362 // 0);
363
364 // instantiate the XP preview object using the win32 preview widget (window)
365 // we just created. we seem to have a mish-mash of terms here, sorry.
366
367 // UT_uint32 w,h;
368 // m_pPreviewWidget->getWindowSize(&w,&h);
369 // _createPreviewFromGC(m_pPreviewWidget->getGraphics(),w,h);
370 // m_pPreviewWidget->setPreview(); // we need this to call draw() on WM_PAINTs
371 // _updatePreviewZoomPercent(getZoomPercent());
372
373 // get the initial offset in the text color dlg so that we can tell if the user
374 // changed color
375 m_iColorIndx = SendDlgItemMessageW(hWnd, 1139, CB_GETCURSEL, 0, 0);
376 m_iColorCount = SendDlgItemMessageW(hWnd, 1139, CB_GETCOUNT, 0, 0);
377
378 if(m_iColorIndx == 0 && !m_sColor.empty() && strcmp(m_sColor.c_str(), "000000") != 0)
379 {
380 // the first item of the list was selected either becase becase the color we
381 // passed to the dlg is not one of those it supports -- we simply add it to the end
382 // of the list
383
384 // we have no name for this color, so left it empty
385 SendDlgItemMessageW(hWnd, 1139, CB_ADDSTRING, 0, (LPARAM)(LPCSTR)"");
386
387 // set the color for the entry
388 UT_RGBColor c;
389 UT_parseColor(m_sColor.c_str(),c);
390 DWORD dColor = RGB(c.m_red,c.m_grn,c.m_blu);
391 SendDlgItemMessageW(hWnd, 1139, CB_SETITEMDATA, m_iColorCount, (LPARAM)(DWORD)dColor);
392
393 // make sure this worked and select the color
394 int iOldCount = m_iColorCount;
395 m_iColorCount = SendDlgItemMessageW(hWnd, 1139, CB_GETCOUNT, 0, 0);
396 if(iOldCount + 1 == m_iColorCount)
397 {
398 SendDlgItemMessageW(hWnd, 1139, CB_SETCURSEL, m_iColorCount-1, 0);
399 m_iColorIndx = m_iColorCount-1;
400 }
401 }
402
403 return 1; // 1 == we did not call SetFocus()
404 }
405
406 BOOL XAP_Win32Dialog_FontChooser::_onCommand(HWND hWnd, WPARAM wParam, LPARAM /*lParam*/)
407 {
408 WORD wNotifyCode = HIWORD(wParam);
409 WORD wId = LOWORD(wParam);
410
411 switch (wId)
412 {
413 case XAP_RID_DIALOG_FONT_CHK_OVERLINE:
414 m_bWin32Overline = (IsDlgButtonChecked(hWnd,XAP_RID_DIALOG_FONT_CHK_OVERLINE)==BST_CHECKED);
415 return 1;
416
417 case XAP_RID_DIALOG_FONT_CHK_HIDDEN:
418 m_bWin32Hidden = (IsDlgButtonChecked(hWnd,XAP_RID_DIALOG_FONT_CHK_HIDDEN)==BST_CHECKED);
419 return 1;
420
421 case XAP_RID_DIALOG_FONT_CHK_SUPERSCRIPT:
422 m_bWin32SuperScript = (IsDlgButtonChecked(hWnd,XAP_RID_DIALOG_FONT_CHK_SUPERSCRIPT)==BST_CHECKED);
423 /* It makes no sense to have subscript and superscript at the same time, Word behaves this way also*/
424 if (m_bWin32SuperScript) CheckDlgButton(hWnd, XAP_RID_DIALOG_FONT_CHK_SUBSCRIPT, BST_UNCHECKED);
425 return 1;
426
427 case XAP_RID_DIALOG_FONT_CHK_SUBSCRIPT:
428 m_bWin32SubScript = (IsDlgButtonChecked(hWnd,XAP_RID_DIALOG_FONT_CHK_SUBSCRIPT)==BST_CHECKED);
429 /* It makes no sense to have subscript and superscript at the same time, Word behaves this way also*/
430 if (m_bWin32SubScript) CheckDlgButton(hWnd, XAP_RID_DIALOG_FONT_CHK_SUPERSCRIPT, BST_UNCHECKED);
431 return 1;
432
433 case 1139: // this is hardcoded in the dlg
434 if(wNotifyCode == CBN_SELCHANGE)
435 {
436 int iIndx = SendDlgItemMessageW(hWnd, 1139, CB_GETCURSEL, 0, 0);
437 if(iIndx != m_iColorIndx)
438 m_bChangedColor = true;
439
440 // return 0 in order for windows to do its thing, i.e., update the preview
441 return 0;
442 }
443 return 0;
444
445 default: // we did not handle this notification
446 UT_DEBUGMSG(("WM_Command for id %ld\n",wId));
447 return 0; // return zero to let windows take care of it.
448 }
449 }
450
451 extern bool helpLocalizeAndOpenURL(const char* pathBeforeLang, const char* pathAfterLang, const char *remoteURLbase);
452
453 BOOL XAP_Win32Dialog_FontChooser::_callHelp()
454 {
455 if ( getHelpUrl().size () > 0 )
456 {
457 helpLocalizeAndOpenURL ("help", getHelpUrl().c_str(), "http://www.abisource.com/help/" );
458 }
459 else
460 {
461 // TODO: warn no help on this topic
462 UT_DEBUGMSG(("NO HELP FOR THIS TOPIC!!\n"));
463 }
464
465 return TRUE;
466 }
467