Debian Codegraph @

src/af/xap/win/xap_Win32Dlg_FileOpenSaveAs.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 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 <stdio.h>
21 #include <windows.h>
22
23 #include "ut_png.h"
24 #include "ut_svg.h"
25 #include "ut_misc.h"
26 #include "ut_string.h"
27 #include "ut_assert.h"
28 #include "ut_bytebuf.h"
29 #include "ut_debugmsg.h"
30 #include "ut_string_class.h"
31 #include "ut_Win32LocaleString.h"
32
33 #include "xap_App.h"
34 #include "xap_Strings.h"
35 #include "xap_Win32App.h"
36 #include "xap_Win32FrameImpl.h"
37
38 #include "xap_Dialog_Id.h"
39 #include "xap_Dlg_FileOpenSaveAs.h"
40 #include "xap_Win32Dlg_FileOpenSaveAs.h"
41
42 #include "gr_Win32Image.h"
43 #include "gr_Win32Graphics.h"
44 #include "ap_Win32App.h"
45
46 #include "ie_types.h"
47 #include "ie_imp.h"
48 #include "ie_impGraphic.h"
49
50 #include "fg_Graphic.h"
51
52 #include "xap_Win32Resources.rc2"
53
54
55 #define MAX_DLG_INS_PICT_STRING 1030
56 /*****************************************************************/
57 XAP_Dialog * XAP_Win32Dialog_FileOpenSaveAs::static_constructor(XAP_DialogFactory * pFactory,
58 XAP_Dialog_Id id)
59 {
60 XAP_Win32Dialog_FileOpenSaveAs * p = new XAP_Win32Dialog_FileOpenSaveAs(pFactory,id);
61 return p;
62 }
63
64 XAP_Win32Dialog_FileOpenSaveAs::XAP_Win32Dialog_FileOpenSaveAs(XAP_DialogFactory * pDlgFactory,
65 XAP_Dialog_Id id)
66 : XAP_Dialog_FileOpenSaveAs(pDlgFactory,id)
67 {
68 m_szDefaultExtension[0] = 0;
69 }
70
71 XAP_Win32Dialog_FileOpenSaveAs::~XAP_Win32Dialog_FileOpenSaveAs(void)
72 {
73 }
74
75 /*****************************************************************/
76
77 /*!
78 Check for the presence of a file suffix in a list of suffixes
79
80 \param haystack List of suffixes in the form "*a; *b; *c"
81 \param needle Suffix to check for
82 */
83 static bool SuffixInList(const char *haystack, const char *needle)
84 {
85 int l = strlen(needle);
86 const char *s = strstr(haystack, needle);
87 if (s)
88 if (s[l] == 0 || s[l] == ';')
89 return true;
90 return false;
91 }
92
93 /*!
94 Retrieve the default extension to use if user does not provide one
95
96 \param indx -- index into the filter list
97 */
98 const wchar_t * XAP_Win32Dialog_FileOpenSaveAs::_getDefaultExtension(UT_uint32 indx)
99 {
100 static wchar_t abw_sfx[] = L"abw";
101 char defaultExtension[DEFAULT_EXT_SIZE + 1];
102 UT_Win32LocaleString def_ext;
103
104 UT_uint32 end = g_strv_length((gchar **) m_szDescriptions);
105 if(indx >= end)
106 {
107 if(m_id == XAP_DIALOG_ID_FILE_SAVE_IMAGE)
108 return L"png";
109
110 return abw_sfx;
111 }
112
113 // copy at most DEFAULT_EXT_SIZE characters from the suffix;
114 strncpy(defaultExtension, m_szSuffixes[indx] + 2, DEFAULT_EXT_SIZE);
115
116 defaultExtension[DEFAULT_EXT_SIZE] = 0;
117
118 // make sure that we get rid off the semicolon if it got copied
119 char * semicolon = strchr(defaultExtension, ';');
120 if(semicolon)
121 *semicolon = 0;
122 UT_DEBUGMSG(("Default sfx [%s], (from [%s]\n", m_szDefaultExtension,m_szSuffixes[indx]));
123 def_ext.fromASCII (defaultExtension);
124 wcscpy (m_szDefaultExtension, def_ext.c_str());
125 return m_szDefaultExtension;
126 }
127
128 /*!
129 Build a Windows filter list from Abi's filetypes
130
131 \param sFilter Build the list in this string
132 */
133 void XAP_Win32Dialog_FileOpenSaveAs::_buildFilterList(UT_String& sFilter)
134 {
135 UT_String sAllSuffixes;
136
137 const XAP_StringSet* pSS = XAP_App::getApp()->getStringSet();
138
139 UT_ASSERT(g_strv_length((gchar **) m_szSuffixes) == g_strv_length((gchar **) m_szDescriptions));
140
141 // measure one list, they should all be the same length
142 UT_uint32 end = g_strv_length((gchar **) m_szDescriptions);
143
144 for (UT_uint32 i = 0; i < end; i++)
145 {
146 // HACK for inconsistent UTF-8 api
147 sFilter += m_szDescriptions[i];
148 sFilter += '\004'; // include the trailing 0
149 sFilter += m_szSuffixes[i];
150 sFilter += '\004'; // include the trailing 0
151
152 // extract one suffix at a time
153 const char *s1 = m_szSuffixes[i];
154 while (1)
155 {
156 const char *s2 = s1;
157 while (*s2 && *s2 != ';')
158 ++s2;
159 UT_String sSuffix(s1, s2-s1);
160 // build a complete list with no repeats
161 if (!SuffixInList(sAllSuffixes.c_str(), sSuffix.c_str()))
162 {
163 if (!sAllSuffixes.empty())
164 sAllSuffixes += "; ";
165 sAllSuffixes += sSuffix;
166 }
167 if (*s2 == 0)
168 break;
169 UT_ASSERT(s2[0] == ';' && s2[1] == ' ');
170 s1 = s2 + 2;
171 }
172 }
173
174 // all supported files filter
175 sFilter += m_id == XAP_DIALOG_ID_INSERT_PICTURE ? pSS->getValue(XAP_STRING_ID_DLG_FOSA_ALLIMAGES) : pSS->getValue(XAP_STRING_ID_DLG_FOSA_ALLDOCS);
176 sFilter += '\004'; // include the trailing 0
177 sFilter += sAllSuffixes;
178 sFilter += '\004'; // include the trailing 0
179
180 // all files filter
181 sFilter += pSS->getValue(XAP_STRING_ID_DLG_FOSA_ALL);
182 sFilter += '\004'; // include the trailing 0
183 sFilter += "*.*";
184 sFilter += '\004'; // include the trailing 0
185
186 sFilter += '\004'; // double 0 at the end
187 }
188
189 /*****************************************************************/
190
191 /*!
192 Gets the Windows File/Save common dialog box. To be able to get
193 the places bar containing icons for commonly-used folders when we
194 use hooking we need to use the new OPENFILENAME structure as defined
195 in WINNT5 or better. To keep backward compability, first try
196 with the new size of the structure, if it does not work it means the
197 the common dialog box DLL installed in the system does not support
198 the new features, and then we use the old one. In this situation, we
199 user does not have a places bar capable common dialog box DLL
200 anyway.
201 */
202 BOOL XAP_Win32Dialog_FileOpenSaveAs::GetSaveFileName_Hooked(OPENFILENAME_WIN50* lpofn, BOOL bSave)
203 {
204 BOOL bRslt;
205
206 lpofn->lStructSize = sizeof(OPENFILENAME_WIN50); // Size of the new structure
207 lpofn->pvReserved = NULL;
208 lpofn->dwReserved = 0;
209 lpofn->FlagsEx = 0;
210
211 if (bSave)
212 bRslt = GetSaveFileNameW((OPENFILENAMEW *)lpofn);
213 else
214 bRslt = GetOpenFileNameW((OPENFILENAMEW *)lpofn);
215
216 if (!bRslt) // Error
217 {
218 DWORD dwError = CommDlgExtendedError();
219
220 if (dwError==CDERR_STRUCTSIZE) // This system does not support the place bar
221 {
222 // Try with the old one
223 lpofn->lStructSize = sizeof(OPENFILENAMEW);
224 if (bSave)
225 bRslt = GetSaveFileNameW((OPENFILENAMEW *)lpofn);
226 else
227 bRslt = GetOpenFileNameW((OPENFILENAMEW *)lpofn);
228
229 }
230 }
231
232 return bRslt;
233 }
234
235
236 /*****************************************************************/
237
238 void XAP_Win32Dialog_FileOpenSaveAs::runModal(XAP_Frame * pFrame)
239 {
240 UT_return_if_fail(pFrame);
241 UT_return_if_fail(pFrame->getFrameImpl());
242
243 XAP_Win32App* pWin32App = static_cast<XAP_Win32App*>(XAP_App::getApp());
244
245 HWND hFrame = static_cast<XAP_Win32FrameImpl*>(pFrame->getFrameImpl())->getTopLevelWindow();
246
247 wchar_t szFile[MAX_DLG_INS_PICT_STRING]; // buffer for filename
248 wchar_t szDir[MAX_DLG_INS_PICT_STRING]; // buffer for directory
249 UT_String sFilter;
250 OPENFILENAME_WIN50 ofn; // common dialog box structure
251 UT_Win32LocaleString filter;
252
253 ZeroMemory(szFile,sizeof(szFile));
254 ZeroMemory(szDir,sizeof(szDir));
255 ZeroMemory(&ofn, sizeof(OPENFILENAME_WIN50));
256
257 _buildFilterList(sFilter);
258 filter.fromUTF8(sFilter.c_str()/*, sFilter.size()*/);
259 LPWSTR f_cur=(LPWSTR)filter.c_str();
260 // \004 is a placeholder for \000 inside string
261 while (*f_cur) {
262 if (*f_cur == 4) *f_cur=0;
263 f_cur++;
264 }
265
266 ofn.lStructSize = sizeof(OPENFILENAMEW); // Old size
267 ofn.hwndOwner = hFrame;
268 ofn.lpstrFile = szFile;
269 ofn.nMaxFile = sizeof(szFile);
270 ofn.lpstrFilter = filter.c_str();
271 ofn.nFilterIndex = 1;
272 ofn.lpstrFileTitle = NULL;
273 ofn.nMaxFileTitle = 0;
274 ofn.lpstrInitialDir = NULL;
275 ofn.Flags = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY;
276 ofn.lpstrDefExt = _getDefaultExtension(0);
277 ofn.lCustData = (DWORD)(size_t)this;
278 // use the persistence info and/or the suggested filename
279 // to properly seed the dialog.
280
281 if (!m_szInitialPathname || !*m_szInitialPathname)
282 {
283 // the caller did not supply initial pathname
284 // (or supplied an empty one). see if we have
285 // some persistent info.
286
287 UT_ASSERT(!m_bSuggestName);
288 if (m_szPersistPathname)
289 {
290 // we have a pathname from a previous use,
291 // extract the directory portion and start
292 // the dialog there (but without a filename).
293
294 // use directory(m_szPersistPathname)
295
296 UT_Win32LocaleString path;
297 path.fromUTF8 (m_szPersistPathname);
298
299 wcscpy(szDir,path.c_str());
300 wchar_t * pLastSlash = wcsrchr(szDir, L'/');
301 if (pLastSlash)
302 pLastSlash[1] = 0;
303 ofn.lpstrInitialDir = szDir;
304 }
305 else
306 {
307 // no initial pathname given and we don't have
308 // a pathname from a previous use, so just let
309 // it come up in the current working directory.
310 // since we set OFN_NOCHANGEDIR we don't have to
311 // to worry about where this is.
312 }
313 }
314 else
315 {
316 // we have an initial pathname (the name of the document
317 // in the frame that we were invoked on). if the caller
318 // wanted us to suggest a name, use the initial
319 // pathname as is. if not, use the directory portion of
320 // it. either way, we need to cut the pathname into two
321 // parts -- directory and file -- for the common dlg.
322
323 UT_Win32LocaleString uri;
324 const char * szURI = g_filename_from_uri(m_szInitialPathname, NULL, NULL);
325 if(!szURI)
326 szURI = g_strdup(m_szInitialPathname);
327
328 uri.fromUTF8 (szURI);
329
330 wcscpy(szDir,uri.c_str());
331 wchar_t * pLastSlash = wcsrchr(szDir, L'\\');
332 if (!pLastSlash)
333 pLastSlash = wcsrchr(szDir, L'/');
334 if (pLastSlash)
335 pLastSlash[1] = 0;
336 ofn.lpstrInitialDir = szDir;
337
338 if (m_bSuggestName)
339 {
340 if (pLastSlash)
341 wcscpy(szFile, uri.c_str() + (pLastSlash-szDir+1));
342 else
343 wcscpy(szFile, uri.c_str());
344
345 // if the file name has an extension, remove it
346 // (if we don't, and the document is of a different
347 // type than the one initially selected in the dialogue
348 // and the user just clicks OK, we get type - extension
349 // mismatch)
350 wchar_t * dot = wcsrchr(szFile, L'.');
351 if(dot)
352 *dot = 0;
353 }
354
355 FREEP(szURI);
356 }
357
358 // display the appropriate dialog box.
359
360 BOOL bDialogResult;
361
362 if( m_nDefaultFileType != XAP_DIALOG_FILEOPENSAVEAS_FILE_TYPE_AUTO )
363 {
364 // Find the index of the type we were given
365 UT_uint32 iCounter, iNumTypes = g_strv_length((gchar **) m_nTypeList);
366
367 for( iCounter = 0; iCounter < iNumTypes; iCounter++ )
368 {
369 if( m_nTypeList[ iCounter ] == m_nDefaultFileType )
370 {
371 ofn.nFilterIndex = iCounter + 1;
372 break;
373 }
374 }
375 }
376
377 const XAP_StringSet* pSS = XAP_App::getApp()->getStringSet();
378 UT_Win32LocaleString title;
379
380 switch (m_id)
381 {
382 case XAP_DIALOG_ID_FILE_OPEN:
383 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_OpenTitle));
384 ofn.lpstrTitle = title.c_str();
385 ofn.Flags |= OFN_FILEMUSTEXIST;
386 ofn.nFilterIndex = g_strv_length((gchar **) m_szDescriptions) + 1;
387 bDialogResult = GetOpenFileNameW((OPENFILENAMEW *)&ofn);
388 break;
389
390 case XAP_DIALOG_ID_PRINTTOFILE:
391 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_PrintToFileTitle));
392 ofn.lpstrTitle = title.c_str();
393 ofn.Flags |= OFN_OVERWRITEPROMPT;
394 bDialogResult = GetSaveFileNameW((OPENFILENAMEW *)&ofn);
395 break;
396
397 case XAP_DIALOG_ID_FILE_SAVEAS:
398 case XAP_DIALOG_ID_FILE_SAVE_IMAGE:
399 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_SaveAsTitle));
400 ofn.lpstrTitle = title.c_str();
401 ofn.lpfnHook = (LPOFNHOOKPROC) s_hookSaveAsProc;
402 ofn.Flags |= OFN_OVERWRITEPROMPT;
403 ofn.Flags |= OFN_EXPLORER;
404 ofn.Flags |= OFN_ENABLEHOOK;
405
406 bDialogResult = GetSaveFileName_Hooked(&ofn, TRUE);
407 break;
408
409 case XAP_DIALOG_ID_INSERT_PICTURE:
410 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_IP_Title));
411 ofn.lpstrTitle = title.c_str();
412 ofn.hInstance = pWin32App->getInstance();
413 ofn.lpTemplateName = MAKEINTRESOURCEW(XAP_RID_DIALOG_INSERT_PICTURE);
414 ofn.lpfnHook = (LPOFNHOOKPROC) s_hookInsertPicProc;
415 ofn.nFilterIndex = g_strv_length((gchar **) m_szDescriptions) + 1;
416 ofn.Flags |= OFN_EXPLORER;
417 ofn.Flags |= OFN_ENABLETEMPLATE;
418 ofn.Flags |= OFN_ENABLEHOOK;
419 bDialogResult = GetSaveFileName_Hooked(&ofn, FALSE);
420 break;
421
422 case XAP_DIALOG_ID_FILE_IMPORT:
423 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_ImportTitle));
424 ofn.lpstrTitle = title.c_str();
425 ofn.nFilterIndex = g_strv_length((gchar **) m_szDescriptions) + 1;
426 ofn.Flags |= OFN_FILEMUSTEXIST;
427 bDialogResult = GetOpenFileNameW((OPENFILENAMEW *)&ofn);
428 break;
429
430 case XAP_DIALOG_ID_INSERTMATHML:
431 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_InsertMath));
432 ofn.lpstrTitle = title.c_str();
433 ofn.nFilterIndex = g_strv_length((gchar **) m_szDescriptions) + 1;
434 ofn.Flags |= OFN_FILEMUSTEXIST;
435 bDialogResult = GetOpenFileNameW((OPENFILENAMEW *)&ofn);
436 break;
437
438 case XAP_DIALOG_ID_FILE_EXPORT:
439 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_ExportTitle));
440 ofn.lpstrTitle = title.c_str();
441 ofn.lpfnHook = (LPOFNHOOKPROC) s_hookSaveAsProc;
442 ofn.Flags |= OFN_OVERWRITEPROMPT;
443 ofn.Flags |= OFN_EXPLORER;
444 ofn.Flags |= OFN_ENABLEHOOK;
445 bDialogResult = GetSaveFileName_Hooked(&ofn, TRUE);
446 break;
447
448 case XAP_DIALOG_ID_INSERT_FILE:
449 title.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_FOSA_InsertTitle));
450 ofn.lpstrTitle = title.c_str();
451 ofn.Flags |= OFN_FILEMUSTEXIST;
452 ofn.nFilterIndex = g_strv_length((gchar **) m_szDescriptions) + 1;
453 bDialogResult = GetOpenFileNameW((OPENFILENAMEW *)&ofn);
454 break;
455
456 default:
457 bDialogResult = FALSE;
458 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
459 break;
460 }
461
462 // TODO how do cancels get reported...
463 // TODO verify that current-working-directory is not changed...
464
465 if (bDialogResult != FALSE)
466 {
467 UT_uint32 end = g_strv_length((gchar **) m_szSuffixes);
468 UT_Win32LocaleString sfile;
469 sfile.fromLocale (szFile);
470
471 if ((m_id == XAP_DIALOG_ID_FILE_SAVEAS) &&
472 (UT_pathSuffix(sfile.utf8_str().utf8_str()).empty()))
473 {
474 // add suffix based on selected file type
475 // if selected file is "all documents" or "all"
476 // default to .abw, since that is how it will get saved
477 UT_ASSERT(ofn.nFilterIndex > 0);
478
479 std::string suffix;
480 char abw_sfx[] = ".abw";
481
482 if(ofn.nFilterIndex <= end)
483 suffix = UT_pathSuffix(m_szSuffixes[ofn.nFilterIndex - 1]);
484 else
485 suffix = abw_sfx;
486
487 UT_ASSERT(!suffix.empty());
488
489 UT_uint32 length = strlen(sfile.utf8_str().utf8_str()) + suffix.size() + 1;
490 m_szFinalPathname = (char *)UT_calloc(length,sizeof(char));
491 if (m_szFinalPathname)
492 {
493 char * p = m_szFinalPathname;
494
495 strcpy(p,sfile.utf8_str().utf8_str());
496 strcat(p,suffix.c_str());
497 }
498 }
499 else
500 {
501 char *uri = UT_go_filename_to_uri(sfile.utf8_str().utf8_str());
502 if(uri)
503 {
504 if(!(m_szFinalPathname = g_strdup(uri)))
505 {
506 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
507 m_szFinalPathname = NULL;
508 }
509 FREEP(uri);
510 }
511 else
512 {
513 UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN);
514 m_szFinalPathname = NULL;
515 }
516 }
517
518 m_answer = a_OK;
519
520 // Set filetype to auto if a generic filter was set
521 if (ofn.nFilterIndex > g_strv_length((gchar **) m_szSuffixes))
522 m_nFileType = XAP_DIALOG_FILEOPENSAVEAS_FILE_TYPE_AUTO;
523 else
524 m_nFileType = ofn.nFilterIndex;
525 }
526 else
527 {
528 m_answer = a_CANCEL;
529 UT_DEBUGMSG(("Didn't get a file: reason=0x%x\n", CommDlgExtendedError()));
530 }
531 }
532
533 /*!
534 Used for SaveAs case of FileOpenSaveAs dialog
535 \param hDlg
536 \param msg
537 \param wParam
538 \param lParam
539
540 Static routine
541 */
542 UINT CALLBACK XAP_Win32Dialog_FileOpenSaveAs::s_hookSaveAsProc(HWND hDlg, UINT msg, WPARAM /*wParam*/, LPARAM lParam)
543 {
544 XAP_Win32Dialog_FileOpenSaveAs* pThis;
545 static wchar_t buff[MAX_DLG_INS_PICT_STRING];
546 switch(msg)
547 {
548 case WM_NOTIFY:
549 {
550 const OFNOTIFYW * pNotify = reinterpret_cast<OFNOTIFYW *>(lParam);
551 switch (pNotify->hdr.code)
552 {
553 case CDN_TYPECHANGE:
554 {
555 UT_DEBUGMSG(("SaveAs filetype changed to %d\n", pNotify->lpOFN->nFilterIndex));
556 pThis = (XAP_Win32Dialog_FileOpenSaveAs*)pNotify->lpOFN->lCustData;
557 const wchar_t * ext = pThis->_getDefaultExtension(pNotify->lpOFN->nFilterIndex - 1);
558 // for some reason the lpstrFile member of the struct will not be set properly
559 // so we have to retrieve the text directly from the control (I could swear that
560 // this used to work, and have no idea what changed)
561 GetDlgItemTextW(GetParent(hDlg), edt1, buff, MAX_DLG_INS_PICT_STRING);
562 wcscpy(buff,pNotify->lpOFN->lpstrFile); //was once commented
563 wchar_t * dot = wcschr(buff, L'.');
564 if(dot)
565 {
566 *(dot+1) = 0;
567 }
568 else if(*buff)
569 {
570 UT_ASSERT(wcslen(buff) < MAX_DLG_INS_PICT_STRING);
571 dot = buff + wcslen(buff);
572 *dot = L'.';
573 *(dot+1) = 0;
574 }
575
576 if(dot)
577 {
578 UT_ASSERT(wcslen(buff) + wcslen(pNotify->lpOFN->lpstrDefExt) < MAX_DLG_INS_PICT_STRING);
579
580 if(ext)
581 wcscat(buff,ext);
582 else
583 *dot = 0;
584
585 CommDlg_OpenSave_SetControlText(GetParent(hDlg), edt1, buff);
586 }
587 CommDlg_OpenSave_SetDefExt(GetParent(hDlg), ext);
588 }
589 break;
590 }
591 }
592 break;
593
594 default:
595 return false;
596 }
597 return false;
598 }
599
600 /*!
601 Used for Insert Picture case of FileOpenSaveAs dialog
602 \param hDlg
603 \param msg
604 \param wParam
605 \param lParam
606
607 Static routine
608 */
609 UINT CALLBACK XAP_Win32Dialog_FileOpenSaveAs::s_hookInsertPicProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
610 {
611 XAP_Win32Dialog_FileOpenSaveAs* pThis = (XAP_Win32Dialog_FileOpenSaveAs *) GetWindowLongPtrW(hDlg,GWLP_USERDATA);
612 bool bPreviewImage = ( IsDlgButtonChecked( hDlg, XAP_RID_DIALOG_INSERT_PICTURE_CHECK_ACTIVATE_PREVIEW )
613 == BST_CHECKED );
614
615 switch(msg)
616 {
617
618 case WM_PAINT:
619 if (bPreviewImage)
620 pThis->_previewPicture(hDlg);
621 return false;
622
623 case WM_NOTIFY:
624 // Only bother if Preview Image is selected
625 if (!bPreviewImage)
626 return false;
627
628 switch ( ((OFNOTIFYW*) lParam)->hdr.code )
629 {
630 case CDN_FOLDERCHANGE:
631 UT_DEBUGMSG(("Folder Changed File Name Cleared\n"));
632 SetDlgItemTextW( GetParent(hDlg), edt1, NULL );
633 SetDlgItemTextW( hDlg, XAP_RID_DIALOG_INSERT_PICTURE_TEXT_HEIGHT, NULL );
634 SetDlgItemTextW( hDlg, XAP_RID_DIALOG_INSERT_PICTURE_TEXT_WIDTH, NULL );
635 return true;
636 case CDN_SELCHANGE:
637 UT_return_val_if_fail(pThis,false);
638 return pThis->_previewPicture(hDlg);
639 }
640 return false;
641
642 case WM_COMMAND:
643 // Check box to Activate Image Preview
644 UT_DEBUGMSG(("WM_COMMAND Received: wParam = %ld, lParam = %ld\n",wParam,lParam));
645 if ( HIWORD(wParam) == BN_CLICKED )
646 {
647 switch ( LOWORD(wParam) )
648 {
649 case XAP_RID_DIALOG_INSERT_PICTURE_CHECK_ACTIVATE_PREVIEW:
650 UT_return_val_if_fail(pThis,false);
651 if (bPreviewImage)
652 {
653 ShowWindow( GetDlgItem(hDlg,XAP_RID_DIALOG_INSERT_PICTURE_TEXT_HEIGHT),
654 SW_SHOW );
655 ShowWindow( GetDlgItem(hDlg,XAP_RID_DIALOG_INSERT_PICTURE_TEXT_WIDTH),
656 SW_SHOW );
657 pThis->_previewPicture(hDlg);
658 return true;
659 }
660 else
661 {
662 ShowWindow( GetDlgItem(hDlg,XAP_RID_DIALOG_INSERT_PICTURE_TEXT_HEIGHT),
663 SW_HIDE );
664 ShowWindow( GetDlgItem(hDlg,XAP_RID_DIALOG_INSERT_PICTURE_TEXT_WIDTH),
665 SW_HIDE );
666 return pThis->_initPreviewDlg(hDlg);
667 }
668 }
669 }
670 return false;
671
672 case WM_INITDIALOG:
673 pThis = (XAP_Win32Dialog_FileOpenSaveAs *)reinterpret_cast<OPENFILENAMEW*>(lParam)->lCustData;
674 SetWindowLongPtrW(hDlg, GWLP_USERDATA, (LONG_PTR) pThis);
675 return pThis->_initPreviewDlg(hDlg);
676
677 default:
678 return false;
679
680 }
681 // Default Dialog handles all other issues
682 return false;
683 }
684
685 UINT XAP_Win32Dialog_FileOpenSaveAs::_previewPicture(HWND hDlg)
686 {
687 HWND hFOSADlg = GetParent(hDlg);
688 HWND hThumbnail = GetDlgItem(hDlg,XAP_RID_DIALOG_INSERT_PICTURE_IMAGE_PREVIEW);
689 UT_Win32LocaleString str;
690
691 const XAP_StringSet* pSS = XAP_App::getApp()->getStringSet();
692 UT_return_val_if_fail(pSS, false);
693
694 // Check if File Name is for a file
695 wchar_t buf[MAX_DLG_INS_PICT_STRING];
696 SendMessageW( hFOSADlg, CDM_GETFILEPATH, sizeof(buf), (LPARAM) buf );
697 // If a Directory stop
698 if ( GetFileAttributesW( buf ) == FILE_ATTRIBUTE_DIRECTORY )
699 {
700 return false;
701 }
702
703 str.fromLocale (buf);
704 UT_DEBUGMSG(("File Selected is %s\n", str.utf8_str().utf8_str()));
705
706 // Build an Import Graphic based on file type
707 UT_Error errorCode = UT_ERROR;
708 FG_Graphic *pfg = NULL;
709 char *uri = UT_go_filename_to_uri(str.utf8_str().utf8_str());
710 if(uri)
711 errorCode = IE_ImpGraphic::loadGraphic(uri, IEGFT_Unknown, &pfg);
712
713 if (errorCode)
714 {
715 return false;
716 }
717
718 double scale_factor = 0.0;
719 UT_sint32 scaled_width,scaled_height;
720 UT_sint32 iImageWidth,iImageHeight;
721 RECT r;
722
723 GetClientRect (hThumbnail, &r);
724 InvalidateRect(hThumbnail, &r, true);
725
726 iImageWidth = pfg->getWidth();
727 iImageHeight = pfg->getHeight();
728
729 // Update Height and Width Strings
730 str.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_IP_Height_Label));
731 swprintf( buf,
732 L"%s %d",
733 str.c_str(),
734 iImageHeight );
735 SetDlgItemTextW( hDlg,
736 XAP_RID_DIALOG_INSERT_PICTURE_TEXT_HEIGHT,
737 buf );
738
739 str.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_IP_Width_Label));
740 swprintf( buf,
741 L"%s %d",
742 str.c_str(),
743 iImageWidth );
744 SetDlgItemTextW( hDlg,
745 XAP_RID_DIALOG_INSERT_PICTURE_TEXT_WIDTH,
746 buf );
747
748 // Reset String for Height and Width
749 if (r.right >= iImageWidth && r.bottom >= iImageHeight)
750 scale_factor = 1.0;
751 else
752 scale_factor = UT_MIN( (double) r.right/iImageWidth,
753 (double) r.bottom/iImageHeight );
754
755 scaled_width = (int) (scale_factor * iImageWidth);
756 scaled_height = (int) (scale_factor * iImageHeight);
757
758 GR_Win32Image* pImage = new GR_Win32Image(NULL);
759 pImage->convertFromBuffer(pfg->getBuffer(), pfg->getMimeType(), scaled_width, scaled_height);
760
761 PAINTSTRUCT ps;
762 HDC hdc = BeginPaint(hThumbnail, &ps);
763 FillRect(hdc, &r, GetSysColorBrush(COLOR_BTNFACE));
764
765 GR_Win32AllocInfo ai(hdc,hThumbnail);
766 GR_Win32Graphics *pGr = (GR_Win32Graphics *)XAP_App::getApp()->newGraphics(ai);
767 UT_return_val_if_fail(pGr, false);
768
769 pGr->drawImage(pImage,
770 pGr->tlu((r.right - scaled_width ) / 2),
771 pGr->tlu((r.bottom - scaled_height) / 2));
772 EndPaint(hThumbnail,&ps);
773
774 DELETEP(pfg);
775 DELETEP(pImage);
776 DELETEP(pGr);
777
778 return true;
779 }
780
781 UINT XAP_Win32Dialog_FileOpenSaveAs::_initPreviewDlg(HWND hDlg)
782 {
783 HWND hFOSADlg = GetParent(hDlg);
784 UT_Win32LocaleString str;
785 setHandle (hDlg);
786
787 const XAP_StringSet* pSS = XAP_App::getApp()->getStringSet();
788 UT_return_val_if_fail(pSS, false);
789
790 setDlgItemText( XAP_RID_DIALOG_INSERT_PICTURE_IMAGE_PREVIEW,
791 pSS->getValue(XAP_STRING_ID_DLG_IP_No_Picture_Label));
792
793 setDlgItemText( XAP_RID_DIALOG_INSERT_PICTURE_CHECK_ACTIVATE_PREVIEW,
794 pSS->getValue(XAP_STRING_ID_DLG_IP_Activate_Label));
795
796 setDlgItemText( XAP_RID_DIALOG_INSERT_PICTURE_TEXT_HEIGHT,
797 pSS->getValue(XAP_STRING_ID_DLG_IP_Height_Label));
798
799 setDlgItemText( XAP_RID_DIALOG_INSERT_PICTURE_TEXT_WIDTH,
800 pSS->getValue(XAP_STRING_ID_DLG_IP_Width_Label));
801
802 str.fromUTF8 (pSS->getValue(XAP_STRING_ID_DLG_IP_Button_Label));
803 SetDlgItemTextW( hFOSADlg,
804 IDOK,
805 str.c_str());
806
807 return true;
808
809 }