1 /* AbiWord
2 * Copyright (C) 2000 AbiSource, Inc.
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 <windows.h>
21
22 #include "ut_string.h"
23 #include "ut_assert.h"
24 #include "ut_debugmsg.h"
25
26 #include "xap_App.h"
27 #include "xap_Win32App.h"
28 #include "xap_Win32FrameImpl.h"
29
30 #include "xap_Strings.h"
31 #include "xap_Dialog_Id.h"
32 #include "xap_Dlg_Insert_Symbol.h"
33 #include "xap_Win32Dlg_Insert_Symbol.h"
34 #include "xap_Win32PreviewWidget.h"
35
36 #include "gr_Win32Graphics.h"
37 #include "xap_Win32DialogHelper.h"
38 #include "xap_Win32Resources.rc2"
39 #include "ap_Win32App.h"
40
41 /*****************************************************************/
42
43 char Symbol_font_selected[32] = "Symbol";
44
static_constructor(XAP_DialogFactory * pFactory,XAP_Dialog_Id id)45 XAP_Dialog * XAP_Win32Dialog_Insert_Symbol::static_constructor(XAP_DialogFactory * pFactory,
46 XAP_Dialog_Id id)
47 {
48 XAP_Win32Dialog_Insert_Symbol * p = new XAP_Win32Dialog_Insert_Symbol(pFactory,id);
49 return p;
50 }
51
XAP_Win32Dialog_Insert_Symbol(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id id)52 XAP_Win32Dialog_Insert_Symbol::XAP_Win32Dialog_Insert_Symbol(XAP_DialogFactory * pDlgFactory,
53 XAP_Dialog_Id id)
54 : XAP_Dialog_Insert_Symbol(pDlgFactory,id)
55 {
56 m_pSymbolPreviewWidget = NULL;
57 m_pSamplePreviewWidget = NULL;
58 m_DrawSymbolSample = NULL;
59
60 }
61
~XAP_Win32Dialog_Insert_Symbol(void)62 XAP_Win32Dialog_Insert_Symbol::~XAP_Win32Dialog_Insert_Symbol(void)
63 {
64 DELETEP(m_pSymbolPreviewWidget);
65 DELETEP(m_pSamplePreviewWidget);
66 DELETEP(m_DrawSymbolSample);
67 }
68
69
runModal(XAP_Frame * pFrame)70 void XAP_Win32Dialog_Insert_Symbol::runModal(XAP_Frame * pFrame)
71 {
72 UT_ASSERT(pFrame);
73 UT_ASSERT(m_id == XAP_DIALOG_ID_INSERT_SYMBOL);
74
75 setDialog(this);
76 createModal(pFrame, MAKEINTRESOURCEW(XAP_RID_DIALOG_INSERT_SYMBOL));
77 }
78
runModeless(XAP_Frame * pFrame)79 void XAP_Win32Dialog_Insert_Symbol::runModeless(XAP_Frame * pFrame)
80 {
81 UT_ASSERT(pFrame);
82 UT_ASSERT(m_id == XAP_DIALOG_ID_INSERT_SYMBOL);
83
84 setDialog(this);
85 HWND hWndDialog = createModeless( pFrame, MAKEINTRESOURCEW(XAP_RID_DIALOG_INSERT_SYMBOL) );
86
87 UT_ASSERT((hWndDialog != NULL));
88 ShowWindow(hWndDialog, SW_SHOW);
89
90 m_pApp->rememberModelessId(m_id, this);
91 }
92
destroy(void)93 void XAP_Win32Dialog_Insert_Symbol::destroy(void)
94 {
95 modeless_cleanup();
96 DestroyWindow(m_hDlg);
97 }
98
activate(void)99 void XAP_Win32Dialog_Insert_Symbol::activate(void)
100 {
101 UT_DebugOnly<int> iResult;
102
103 // Update the caption
104 ConstructWindowName();
105 setDialogTitle(m_WindowName);
106
107 iResult = ShowWindow( m_hDlg, SW_SHOW );
108
109 iResult = BringWindowToTop( m_hDlg );
110
111 UT_ASSERT((iResult != 0));
112 }
113
114
notifyActiveFrame(XAP_Frame * pFrame)115 void XAP_Win32Dialog_Insert_Symbol::notifyActiveFrame(XAP_Frame *pFrame)
116 {
117 UT_return_if_fail(pFrame);
118
119 HWND frameHWND = static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow();
120 if((HWND)GetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT) != frameHWND)
121 {
122 // Update the caption
123 ConstructWindowName();
124 setDialogTitle(m_WindowName);
125
126 SetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT, (LONG_PTR)frameHWND);
127 SetWindowPos(m_hDlg, NULL, 0, 0, 0, 0,
128 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
129 }
130 }
131
notifyCloseFrame(XAP_Frame * pFrame)132 void XAP_Win32Dialog_Insert_Symbol::notifyCloseFrame(XAP_Frame *pFrame)
133 {
134 UT_return_if_fail(pFrame);
135 if((HWND)GetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT) == static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow())
136 {
137 SetWindowLongPtrW(m_hDlg, GWLP_HWNDPARENT, 0);
138 SetWindowPos(m_hDlg, NULL, 0, 0, 0, 0,
139 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
140 }
141 }
142
_onInitDialog(HWND hWnd,WPARAM,LPARAM)143 BOOL XAP_Win32Dialog_Insert_Symbol::_onInitDialog(HWND hWnd, WPARAM /*wParam*/, LPARAM /*lParam*/)
144 {
145 m_hDlg = hWnd;
146
147 // localize controls
148 localizeControlText(XAP_RID_DIALOG_INSERTSYMBOL_INSERT_BUTTON,XAP_STRING_ID_DLG_Insert);
149 localizeControlText(XAP_RID_DIALOG_INSERTSYMBOL_CLOSE_BUTTON,XAP_STRING_ID_DLG_Close);
150
151
152 // *** this is how we add the gc for symbol table ***
153 // attach a new graphics context to the drawing area
154 UT_DebugOnly<XAP_Win32App *> app = static_cast<XAP_Win32App *> (m_pApp);
155 UT_ASSERT(app);
156
157 HWND hwndChild = GetDlgItem(hWnd, XAP_RID_DIALOG_INSERTSYMBOL_SYMBOLS);
158
159 m_pSymbolPreviewWidget = new XAP_Win32PreviewWidget(static_cast<XAP_Win32App *>(m_pApp),
160 hwndChild,
161 0);
162 UT_uint32 w,h;
163 m_pSymbolPreviewWidget->getWindowSize(&w,&h);
164 _createSymbolFromGC(m_pSymbolPreviewWidget->getGraphics(), w, h);
165 m_pSymbolPreviewWidget->setPreview(m_DrawSymbol);
166 m_pSymbolPreviewWidget->setInsertSymbolParent(this);
167
168 hwndChild = GetDlgItem(hWnd, XAP_RID_DIALOG_INSERTSYMBOL_SYMBOL_PREVIEW);
169
170 m_pSamplePreviewWidget = new XAP_Win32PreviewWidget(static_cast<XAP_Win32App *>(m_pApp),
171 hwndChild,
172 0);
173
174 m_pSamplePreviewWidget->getWindowSize(&w,&h);
175 _createSymbolareaFromGC(m_pSamplePreviewWidget->getGraphics(), w, h);
176
177 m_DrawSymbolSample = new XAP_Draw_Symbol_sample(m_DrawSymbol, m_pSamplePreviewWidget->getGraphics());
178
179 // TODO: Colour
180 GR_Win32Graphics* gr = (GR_Win32Graphics*) m_DrawSymbolSample->m_pSymbolDraw->getGraphics();
181
182
183 gr->setBrush((HBRUSH)GetSysColorBrush(COLOR_3DFACE));
184
185 m_pSamplePreviewWidget->setPreview(m_DrawSymbolSample);
186
187 UT_DebugOnly<XAP_Draw_Symbol *> iDrawSymbol = _getCurrentSymbolMap();
188 UT_ASSERT(iDrawSymbol);
189
190 // Fill the list box with symbol fonts.
191
192 HDC hDCScreen = CreateDCW(L"DISPLAY", NULL, NULL, NULL);
193
194 #if 1
195 EnumFontFamiliesW(hDCScreen, (const wchar_t *)NULL, (FONTENUMPROCW)fontEnumProcedure, (LPARAM)this);
196 #else
197 LOGFONTW LogFont;
198 // LogFont.lfCharSet = SYMBOL_CHARSET; - all fonts enum is more inline with XP nature
199 LogFont.lfCharSet = DEFAULT_CHARSET;
200 LogFont.lfFaceName[0] = '\0';
201 EnumFontFamiliesExW(hDCScreen, &LogFont, (FONTENUMPROCW)fontEnumProcedure, (LPARAM)this, 0);
202 #endif
203
204 DeleteDC(hDCScreen);
205
206 // Select the current font.
207
208 UT_sint32 Index = SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_FINDSTRING, -1, (LPARAM)Symbol_font_selected);
209
210 if(Index != -1)
211 {
212 _setFontFromCombo(Index);
213 }
214 else
215 {
216 _setFontFromCombo(0);
217 }
218
219 // Update the caption
220 ConstructWindowName();
221 setDialogTitle(m_WindowName);
222 centerDialog();
223
224 return 1; // 1 == we did not call SetFocus()
225 }
226
_onCommand(HWND,WPARAM wParam,LPARAM)227 BOOL XAP_Win32Dialog_Insert_Symbol::_onCommand(HWND /*hWnd*/, WPARAM wParam, LPARAM /*lParam*/)
228 {
229 WORD wNotifyCode = HIWORD(wParam);
230 WORD wId = LOWORD(wParam);
231
232 switch (wId)
233 {
234 case XAP_RID_DIALOG_INSERTSYMBOL_CLOSE_BUTTON:
235 m_answer = XAP_Dialog_Insert_Symbol::a_CANCEL;
236 destroy();
237 return 1;
238
239 case XAP_RID_DIALOG_INSERTSYMBOL_INSERT_BUTTON:
240 doInsertSymbol();
241 return 1;
242
243 case XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST:
244 switch(wNotifyCode)
245 {
246 case CBN_SELCHANGE:
247 _setFontFromCombo(SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_GETCURSEL, 0, 0));
248 return 1;
249 }
250 return 0;
251
252 default: // we did not handle this notification
253 UT_DEBUGMSG(("WM_Command for id %ld\n",wId));
254 return 0; // return zero to let windows take care of it.
255 }
256 }
257
_onDeltaPos(NM_UPDOWN *)258 BOOL XAP_Win32Dialog_Insert_Symbol::_onDeltaPos(NM_UPDOWN * /*pnmud*/)
259 {
260 return FALSE;
261 }
262
fontEnumProcedure(const LOGFONTW * pLogFont,const TEXTMETRICW * pTextMetric,DWORD Font_type,LPARAM lParam)263 int CALLBACK XAP_Win32Dialog_Insert_Symbol::fontEnumProcedure(const LOGFONTW *pLogFont, const TEXTMETRICW *pTextMetric, DWORD Font_type, LPARAM lParam)
264 {
265
266 XAP_Win32Dialog_Insert_Symbol *pThis = (XAP_Win32Dialog_Insert_Symbol *)lParam;
267
268 return pThis->_enumFont(pLogFont, pTextMetric, Font_type);
269 }
270
_enumFont(const LOGFONTW * pLogFont,const TEXTMETRICW *,DWORD Font_type)271 int XAP_Win32Dialog_Insert_Symbol::_enumFont(const LOGFONTW *pLogFont, const TEXTMETRICW * /*pTextMetric*/, DWORD Font_type)
272 {
273 if( ((int)Font_type) & TRUETYPE_FONTTYPE ) // Only except true type fonts.
274 {
275 SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_ADDSTRING, 0, (LPARAM)pLogFont->lfFaceName);
276 }
277 // if(Font_type & TRUETYPE_FONTTYPE) // Only accept true type fonts.
278 // {
279 // addItemToList(XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, pLogFont->lfFaceName);
280 // }
281 return TRUE;
282 }
283
_setFontFromCombo(UT_sint32 Index)284 void XAP_Win32Dialog_Insert_Symbol::_setFontFromCombo(UT_sint32 Index)
285 {
286 if(Index >= 0)
287 {
288 SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_SETCURSEL, Index, 0);
289
290 int Length = SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_GETLBTEXTLEN, Index, 0);
291
292 if(Length != CB_ERR)
293 {
294 char *p_buffer = new char[Length + 1];
295
296 SendDlgItemMessageW(m_hDlg, XAP_RID_DIALOG_INSERTSYMBOL_FONT_LIST, CB_GETLBTEXT, Index, (LPARAM)p_buffer);
297
298 strcpy(Symbol_font_selected, p_buffer);
299
300 UT_UCSChar *p_UC_buffer = new UT_UCSChar[Length + 1];
301
302 UT_UCS4_strcpy_char(p_UC_buffer, p_buffer);
303
304 m_DrawSymbol->setSelectedFont(p_buffer);
305 m_DrawSymbol->draw();
306 m_DrawSymbol->drawarea(m_CurrentSymbol, m_PreviousSymbol);
307
308 delete [] p_UC_buffer;
309 delete [] p_buffer;
310
311 }
312 else
313 {
314 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
315 }
316 }
317 }
318
doInsertSymbol(void)319 void XAP_Win32Dialog_Insert_Symbol::doInsertSymbol( void )
320 {
321 m_Inserted_Symbol = m_DrawSymbol->getCurrent();
322 _onInsertButton();
323 }
324