Code

Fixed buffer overrun in FileOpenDialogImplWin32::createFilterMenu, corrected a couple...
[inkscape.git] / src / ui / dialog / filedialogimpl-win32.cpp
1 /**
2  * Implementation of the file dialog interfaces defined in filedialog.h for Win32
3  *
4  * Authors:
5  *   Joel Holdsworth
6  *   The Inkscape Organization
7  *
8  * Copyright (C) 2004-2008 The Inkscape Organization
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #ifdef WIN32
19 //General includes
20 #include <list>
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <set>
25 #include <gdk/gdkwin32.h>
26 #include <glib/gstdio.h>
27 #include <glibmm/i18n.h>
28 #include <gtkmm/window.h>
30 //Inkscape includes
31 #include "inkscape.h"
32 #include "prefs-utils.h"
33 #include <dialogs/dialog-events.h>
34 #include <extension/input.h>
35 #include <extension/output.h>
36 #include <extension/db.h>
38 #include <libnr/nr-pixops.h>
39 #include <libnr/nr-translate-scale-ops.h>
40 #include <display/nr-arena-item.h>
41 #include <display/nr-arena.h>
42 #include "sp-item.h"
43 #include "canvas-arena.h"
45 #include "filedialog.h"
46 #include "filedialogimpl-win32.h"
48 #include <zlib.h>
49 #include <cairomm/win32_surface.h>
50 #include <cairomm/context.h>
52 using namespace std;
53 using namespace Glib;
54 using namespace Cairo;
55 using namespace Gdk::Cairo;
57 namespace Inkscape
58 {
59 namespace UI
60 {
61 namespace Dialog
62 {
64 const int PreviewWidening = 150;
65 const char PreviewWindowClassName[] = "PreviewWnd";
66 const unsigned long MaxPreviewFileSize = 1344; // kB
68 #define IDC_SHOW_PREVIEW    1000
70 // Windows 2000 version of OPENFILENAMEW
71 struct OPENFILENAMEEXW : public OPENFILENAMEW {
72   void *        pvReserved;
73   DWORD         dwReserved;
74   DWORD         FlagsEx;
75 };
77 struct Filter
78 {
79     gunichar2* name;
80     glong name_length;
81     gunichar2* filter;
82     glong filter_length;
83     Inkscape::Extension::Extension* mod;
84 };
86 ustring utf16_to_ustring(const wchar_t *utf16string, int utf16length = -1)
87 {
88     gchar *utf8string = g_utf16_to_utf8((const gunichar2*)utf16string,
89         utf16length, NULL, NULL, NULL);
90     ustring result(utf8string);
91     g_free(utf8string);
93     return result;
94 }
96 /*#########################################################################
97 ### F I L E     D I A L O G    B A S E    C L A S S
98 #########################################################################*/
100 FileDialogBaseWin32::FileDialogBaseWin32(Gtk::Window &parent,
101         const Glib::ustring &dir, const gchar *title,
102         FileDialogType type, gchar const* /*preferenceBase*/) :
103         dialogType(type),
104         parent(parent),
105         _current_directory(dir)
107     _main_loop = NULL;
109         _filter_index = 1;
110         _filter_count = 0;
111         
112     _title = (wchar_t*)g_utf8_to_utf16(title, -1, NULL, NULL, NULL);
113         g_assert(_title != NULL);
115     Glib::RefPtr<const Gdk::Window> parentWindow = parent.get_window();
116     g_assert(parentWindow->gobj() != NULL);
117     _ownerHwnd = (HWND)gdk_win32_drawable_get_handle((GdkDrawable*)parentWindow->gobj());
120 FileDialogBaseWin32::~FileDialogBaseWin32()
122         if(_title != NULL)
123                 g_free(_title);
126 Inkscape::Extension::Extension *FileDialogBaseWin32::getSelectionType()
128     return _extension;
131 Glib::ustring FileDialogBaseWin32::getCurrentDirectory()
133     return _current_directory;
136 /*#########################################################################
137 ### F I L E    O P E N
138 #########################################################################*/
140 bool FileOpenDialogImplWin32::_show_preview = true;
142 /**
143  * Constructor.  Not called directly.  Use the factory.
144  */
145 FileOpenDialogImplWin32::FileOpenDialogImplWin32(Gtk::Window &parent,
146                                        const Glib::ustring &dir,
147                                        FileDialogType fileTypes,
148                                        const gchar *title) :
149     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.open")
151     // Initalize to Autodetect
152     _extension = NULL;
154     // Set our dialog type (open, import, etc...)
155     dialogType = fileTypes;
157     _show_preview_button_bitmap = NULL;
158     _preview_wnd = NULL;
159     _file_dialog_wnd = NULL;
160     _base_window_proc = NULL;
162     _preview_file_size = 0;
163     _preview_bitmap = NULL;
164     _preview_file_icon = NULL;
165     _preview_document_width = 0;
166     _preview_document_height = 0;
167     _preview_image_width = 0;
168     _preview_image_height = 0;
169         
170         _mutex = NULL;
172     createFilterMenu();
176 /**
177  * Destructor
178  */
179 FileOpenDialogImplWin32::~FileOpenDialogImplWin32()
181     if(_filter != NULL)
182         delete[] _filter;
183     if(_extension_map != NULL)
184         delete[] _extension_map;
187 void FileOpenDialogImplWin32::createFilterMenu()
189     list<Filter> filter_list;
191     // Compose the filter string
192     Inkscape::Extension::DB::InputList extension_list;
193     Inkscape::Extension::db.get_input_list(extension_list);
195     ustring all_inkscape_files_filter, all_image_files_filter;
196     Filter all_files, all_inkscape_files, all_image_files;
198     const gchar *all_files_filter_name = N_("All Files");
199     const gchar *all_inkscape_files_filter_name = N_("All Inkscape Files");
200     const gchar *all_image_files_filter_name = N_("All Image Files");
202     // Calculate the amount of memory required
203     int filter_count = 3;       // 3 - one for All Files, All Images and All Inkscape Files
204     int filter_length = 1;
206     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
207          current_item != extension_list.end(); current_item++)
208     {
209         Filter filter;
211         Inkscape::Extension::Input *imod = *current_item;
212         if (imod->deactivated()) continue;
214         // Type
215         filter.name = g_utf8_to_utf16(_(imod->get_filetypename()),
216             -1, NULL, &filter.name_length, NULL);
218         // Extension
219         const gchar *file_extension_name = imod->get_extension();
220         filter.filter = g_utf8_to_utf16(file_extension_name,
221             -1, NULL, &filter.filter_length, NULL);
223         filter.mod = imod;
224         filter_list.push_back(filter);
226         filter_length += filter.name_length +
227             filter.filter_length + 3;   // Add 3 for two \0s and a *
229         // Add to the "All Inkscape Files" Entry
230         if(all_inkscape_files_filter.length() > 0)
231             all_inkscape_files_filter += ";*";
232         all_inkscape_files_filter += file_extension_name;
233         if( strncmp("image", imod->get_mimetype(), 5) == 0)
234         {
235             // Add to the "All Image Files" Entry
236             if(all_image_files_filter.length() > 0)
237                 all_image_files_filter += ";*";
238             all_image_files_filter += file_extension_name;
239         }
241         filter_count++;
242     }
244     int extension_index = 0;
245     _extension_map = new Inkscape::Extension::Extension*[filter_count + 3];
247     // Filter Image Files
248     all_image_files.name = g_utf8_to_utf16(all_image_files_filter_name,
249         -1, NULL, &all_image_files.name_length, NULL);
250     all_image_files.filter = g_utf8_to_utf16(all_image_files_filter.data(),
251             -1, NULL, &all_image_files.filter_length, NULL);
252     filter_list.push_front(all_image_files);
253     _extension_map[extension_index++] = NULL;
255     // Filter Inkscape Files
256     all_inkscape_files.name = g_utf8_to_utf16(all_inkscape_files_filter_name,
257         -1, NULL, &all_inkscape_files.name_length, NULL);
258     all_inkscape_files.filter = g_utf8_to_utf16(all_inkscape_files_filter.data(),
259             -1, NULL, &all_inkscape_files.filter_length, NULL);
260     filter_list.push_front(all_inkscape_files);
261     _extension_map[extension_index++] = NULL;
263     // Filter All Files
264     all_files.name = g_utf8_to_utf16(all_files_filter_name,
265         -1, NULL, &all_files.name_length, NULL);
266     all_files.filter = NULL;
267     all_files.filter_length = 0;
268     filter_list.push_front(all_files);
269     _extension_map[extension_index++] = NULL;
271     filter_length += all_files.name_length + 3 +
272                     all_inkscape_files.filter_length +
273                     all_inkscape_files.name_length + 3 +
274                     all_image_files.filter_length +
275                     all_image_files.name_length + 3 + 1;
276      // Add 3 for 2*2 \0s and a *, and 1 for a trailing \0
277          
278         _filter = new wchar_t[filter_length];
279     wchar_t *filterptr = _filter;
280         
281     for(list<Filter>::iterator filter_iterator = filter_list.begin();
282         filter_iterator != filter_list.end(); filter_iterator++)
283     {
284         const Filter &filter = *filter_iterator;
286         wcsncpy(filterptr, (wchar_t*)filter.name, filter.name_length);
287         filterptr += filter.name_length;
288         g_free(filter.name);
290         *(filterptr++) = L'\0';
291         *(filterptr++) = L'*';
293         if(filter.filter != NULL)
294         {
295             wcsncpy(filterptr, (wchar_t*)filter.filter, filter.filter_length);
296             filterptr += filter.filter_length;
297             g_free(filter.filter);
298         }
300         *(filterptr++) = L'\0';
302         // Associate this input extension with the file type name
303         _extension_map[extension_index++] = filter.mod;
304     }
305     *(filterptr++) = L'\0';
306         
307         _filter_count = extension_index;
308     _filter_index = 2;  // Select the 2nd filter in the list - 2 is NOT the 3rd
311 void FileOpenDialogImplWin32::GetOpenFileName_thread()
313     OPENFILENAMEEXW ofn;
315     g_assert(this != NULL);
316         g_assert(_mutex != NULL);
317     
318     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
319         _current_directory.data(), _current_directory.length(),
320                 NULL, NULL, NULL);
322     memset(&ofn, 0, sizeof(ofn));
324     // Copy the selected file name, converting from UTF-8 to UTF-16
325     memset(_path_string, 0, sizeof(_path_string));
326     gunichar2* utf16_path_string = g_utf8_to_utf16(
327         myFilename.data(), -1, NULL, NULL, NULL);
328     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
329     g_free(utf16_path_string);
331     ofn.lStructSize = sizeof(ofn);
332     ofn.hwndOwner = _ownerHwnd;
333     ofn.lpstrFile = _path_string;
334     ofn.nMaxFile = _MAX_PATH;
335     ofn.lpstrFileTitle = NULL;
336     ofn.nMaxFileTitle = 0;
337     ofn.lpstrInitialDir = current_directory_string;
338     ofn.lpstrTitle = _title;
339     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_ENABLESIZING;
340     ofn.lpstrFilter = _filter;
341     ofn.nFilterIndex = _filter_index;
342     ofn.lpfnHook = GetOpenFileName_hookproc;
343     ofn.lCustData = (LPARAM)this;
345     _result = GetOpenFileNameW(&ofn) != 0;
347         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
348     _filter_index = ofn.nFilterIndex;
349     _extension = _extension_map[ofn.nFilterIndex - 1];
351     // Copy the selected file name, converting from UTF-16 to UTF-8
352     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
354     // Tidy up
355     g_free(current_directory_string);
357     _mutex->lock();
358     _finished = true;
359     _mutex->unlock();
362 void FileOpenDialogImplWin32::register_preview_wnd_class()
364     HINSTANCE hInstance = GetModuleHandle(NULL);
365     const WNDCLASSA PreviewWndClass =
366     {
367         CS_HREDRAW | CS_VREDRAW,
368         preview_wnd_proc,
369         0,
370         0,
371         hInstance,
372         NULL,
373         LoadCursor(hInstance, IDC_ARROW),
374         (HBRUSH)(COLOR_BTNFACE + 1),
375         NULL,
376         PreviewWindowClassName
377     };
379     RegisterClassA(&PreviewWndClass);
382 UINT_PTR CALLBACK FileOpenDialogImplWin32::GetOpenFileName_hookproc(
383     HWND hdlg, UINT uiMsg, WPARAM, LPARAM lParam)
385     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
386         GetWindowLongPtr(hdlg, GWLP_USERDATA);
388     switch(uiMsg)
389     {
390     case WM_INITDIALOG:
391         {
392             HWND hParentWnd = GetParent(hdlg);
393             HINSTANCE hInstance = GetModuleHandle(NULL);
395             // Make the window a bit wider
396             RECT rcRect;
397             GetWindowRect(hParentWnd, &rcRect);
398             MoveWindow(hParentWnd, rcRect.left, rcRect.top,
399                 rcRect.right - rcRect.left + PreviewWidening,
400                 rcRect.bottom - rcRect.top,
401                 FALSE);
403             // Set the pointer to the object
404             OPENFILENAMEW *ofn = (OPENFILENAMEW*)lParam;
405             SetWindowLongPtr(hdlg, GWLP_USERDATA, ofn->lCustData);
406             SetWindowLongPtr(hParentWnd, GWLP_USERDATA, ofn->lCustData);
407             pImpl = (FileOpenDialogImplWin32*)ofn->lCustData;
409             // Subclass the parent
410             pImpl->_base_window_proc = (WNDPROC)GetWindowLongPtr(hParentWnd, GWL_WNDPROC);
411             SetWindowLongPtr(hParentWnd, GWL_WNDPROC, (LONG_PTR)file_dialog_subclass_proc);
413             // Add a button to the toolbar
414             pImpl->_toolbar_wnd = FindWindowEx(hParentWnd, NULL, "ToolbarWindow32", NULL);
416             pImpl->_show_preview_button_bitmap = LoadBitmap(
417                 hInstance, MAKEINTRESOURCE(IDC_SHOW_PREVIEW));
418             TBADDBITMAP tbAddBitmap = {NULL, (UINT)pImpl->_show_preview_button_bitmap};
419             const int iBitmapIndex = SendMessage(pImpl->_toolbar_wnd,
420                 TB_ADDBITMAP, 1, (LPARAM)&tbAddBitmap);
422             TBBUTTON tbButton;
423             memset(&tbButton, 0, sizeof(TBBUTTON));
424             tbButton.iBitmap = iBitmapIndex;
425             tbButton.idCommand = IDC_SHOW_PREVIEW;
426             tbButton.fsState = (pImpl->_show_preview ? TBSTATE_CHECKED : 0)
427                 | TBSTATE_ENABLED;
428             tbButton.fsStyle = TBSTYLE_CHECK;
429             tbButton.iString = (INT_PTR)_("Show Preview");
430             SendMessage(pImpl->_toolbar_wnd, TB_ADDBUTTONS, 1, (LPARAM)&tbButton);
432             // Create preview pane
433             register_preview_wnd_class();
435             pImpl->_mutex->lock();
437                 pImpl->_file_dialog_wnd = hParentWnd;
439                 pImpl->_preview_wnd =
440                     CreateWindowA(PreviewWindowClassName, "",
441                         WS_CHILD | WS_VISIBLE,
442                         0, 0, 100, 100, hParentWnd, NULL, hInstance, NULL);
443                 SetWindowLongPtr(pImpl->_preview_wnd, GWLP_USERDATA, ofn->lCustData);
445             pImpl->_mutex->unlock();
447             pImpl->layout_dialog();
448         }
449         break;
451     case WM_NOTIFY:
452         {
454         OFNOTIFY *pOFNotify = reinterpret_cast<OFNOTIFY*>(lParam);
455         switch(pOFNotify->hdr.code)
456         {
457         case CDN_SELCHANGE:
458             {
459                 if(pImpl != NULL)
460                 {
461                     // Get the file name
462                     pImpl->_mutex->lock();
464                     SendMessage(pOFNotify->hdr.hwndFrom, CDM_GETFILEPATH,
465                         sizeof(pImpl->_path_string) / sizeof(wchar_t),
466                         (LPARAM)pImpl->_path_string);
468                     pImpl->_file_selected = true;
470                     pImpl->_mutex->unlock();
471                 }
472             }
473             break;
474         }
475         }
476         break;
478     case WM_CLOSE:
479         pImpl->_mutex->lock();
480         pImpl->_preview_file_size = 0;
482         pImpl->_file_dialog_wnd = NULL;
483         DestroyWindow(pImpl->_preview_wnd);
484         pImpl->_preview_wnd = NULL;
485         DeleteObject(pImpl->_show_preview_button_bitmap);
486         pImpl->_show_preview_button_bitmap = NULL;
487         pImpl->_mutex->unlock();
489         break;
490     }
492     // Use default dialog behaviour
493     return 0;
496 LRESULT CALLBACK FileOpenDialogImplWin32::file_dialog_subclass_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
498     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
499         GetWindowLongPtr(hwnd, GWLP_USERDATA);
501     LRESULT lResult = CallWindowProc(pImpl->_base_window_proc, hwnd, uMsg, wParam, lParam);
503     switch(uMsg)
504     {
505     case WM_SHOWWINDOW:
506         if(wParam != 0)
507             pImpl->layout_dialog();
508         break;
510     case WM_SIZE:
511         pImpl->layout_dialog();
512         break;
514     case WM_COMMAND:
515         if(wParam == IDC_SHOW_PREVIEW)
516         {
517             const bool enable = SendMessage(pImpl->_toolbar_wnd,
518                 TB_ISBUTTONCHECKED, IDC_SHOW_PREVIEW, 0) != 0;
519             pImpl->enable_preview(enable);
520         }
521         break;
522     }
524     return lResult;
527 LRESULT CALLBACK FileOpenDialogImplWin32::preview_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
529     const int CaptionPadding = 4;
530     const int IconSize = 32;
532     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
533         GetWindowLongPtr(hwnd, GWLP_USERDATA);
535     LRESULT lResult = 0;
537     switch(uMsg)
538     {
539     case WM_ERASEBKGND:
540         // Do nothing to erase the background
541         //  - otherwise there'll be flicker
542         lResult = 1;
543         break;
545     case WM_PAINT:
546         {
547             // Get the client rect
548             RECT rcClient;
549             GetClientRect(hwnd, &rcClient);
551             // Prepare to paint
552             PAINTSTRUCT paint_struct;
553             HDC dc = BeginPaint(hwnd, &paint_struct);
555             HFONT hCaptionFont = (HFONT)SendMessage(GetParent(hwnd),
556                     WM_GETFONT, 0, 0);
557             HFONT hOldFont = (HFONT)SelectObject(dc, hCaptionFont);
558             SetBkMode(dc, TRANSPARENT);
560             pImpl->_mutex->lock();
562             if(pImpl->_path_string[0] == 0)
563             {
564                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
565                 DrawText(dc, _("No file selected"), -1, &rcClient,
566                     DT_CENTER | DT_VCENTER | DT_NOPREFIX);
567             }
568             else if(pImpl->_preview_bitmap != NULL)
569             {
570                 BITMAP bitmap;
571                 GetObject(pImpl->_preview_bitmap, sizeof(bitmap), &bitmap);
572                 const int destX = (rcClient.right - bitmap.bmWidth) / 2;
574                 // Render the image
575                 HDC hSrcDC = CreateCompatibleDC(dc);
576                 HBITMAP hOldBitmap = (HBITMAP)SelectObject(hSrcDC, pImpl->_preview_bitmap);
578                 BitBlt(dc, destX, 0, bitmap.bmWidth, bitmap.bmHeight,
579                     hSrcDC, 0, 0, SRCCOPY);
581                 SelectObject(hSrcDC, hOldBitmap);
582                 DeleteDC(hSrcDC);
584                 // Fill in the background area
585                 HRGN hEraseRgn = CreateRectRgn(rcClient.left, rcClient.top,
586                     rcClient.right, rcClient.bottom);
587                 HRGN hImageRgn = CreateRectRgn(destX, 0,
588                     destX + bitmap.bmWidth, bitmap.bmHeight);
589                 CombineRgn(hEraseRgn, hEraseRgn, hImageRgn, RGN_DIFF);
591                 FillRgn(dc, hEraseRgn, GetSysColorBrush(COLOR_3DFACE));
593                 DeleteObject(hImageRgn);
594                 DeleteObject(hEraseRgn);
596                 // Draw the caption on
597                 RECT rcCaptionRect = {rcClient.left,
598                     rcClient.top + bitmap.bmHeight + CaptionPadding,
599                     rcClient.right, rcClient.bottom};
601                 WCHAR szCaption[_MAX_FNAME + 32];
602                 const int iLength = pImpl->format_caption(
603                     szCaption, sizeof(szCaption) / sizeof(WCHAR));
605                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
606                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
607             }
608             else if(pImpl->_preview_file_icon != NULL)
609             {
610                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
612                 // Draw the files icon
613                 const int destX = (rcClient.right - IconSize) / 2;
614                 DrawIconEx(dc, destX, 0, pImpl->_preview_file_icon,
615                     IconSize, IconSize, 0, NULL,
616                     DI_NORMAL | DI_COMPAT);
618                 // Draw the caption on
619                 RECT rcCaptionRect = {rcClient.left,
620                     rcClient.top + IconSize + CaptionPadding,
621                     rcClient.right, rcClient.bottom};
623                 WCHAR szFileName[_MAX_FNAME], szCaption[_MAX_FNAME + 32];
624                 _wsplitpath(pImpl->_path_string, NULL, NULL, szFileName, NULL);
626                 const int iLength = snwprintf(szCaption,
627                     sizeof(szCaption), L"%s\n%d kB",
628                     szFileName, pImpl->_preview_file_size);
630                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
631                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
632             }
633             else
634             {
635                 // Can't show anything!
636                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
637             }
639             pImpl->_mutex->unlock();
641             // Finish painting
642             SelectObject(dc, hOldFont);
643             EndPaint(hwnd, &paint_struct);
644         }
646         break;
648     case WM_DESTROY:
649         pImpl->free_preview();
650         break;
652     default:
653         lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
654         break;
655     }
657     return lResult;
660 void FileOpenDialogImplWin32::enable_preview(bool enable)
662     _show_preview = enable;
664     // Relayout the dialog
665     ShowWindow(_preview_wnd, enable ? SW_SHOW : SW_HIDE);
666     layout_dialog();
668     // Load or unload the preview
669     if(enable)
670     {
671         _mutex->lock();
672         _file_selected = true;
673         _mutex->unlock();
674     }
675     else free_preview();
678 void FileOpenDialogImplWin32::layout_dialog()
680     union RECTPOINTS
681     {
682         RECT r;
683         POINT p[2];
684     };
686     const float MaxExtentScale = 2.0f / 3.0f;
688     RECT rcClient;
689     GetClientRect(_file_dialog_wnd, &rcClient);
691     // Re-layout the dialog
692     HWND hFileListWnd = GetDlgItem(_file_dialog_wnd, lst2);
693     HWND hFolderComboWnd = GetDlgItem(_file_dialog_wnd, cmb2);
696     RECT rcFolderComboRect;
697     RECTPOINTS rcFileList;
698     GetWindowRect(hFileListWnd, &rcFileList.r);
699     GetWindowRect(hFolderComboWnd, &rcFolderComboRect);
700     const int iPadding = rcFileList.r.top - rcFolderComboRect.bottom;
701     MapWindowPoints(NULL, _file_dialog_wnd, rcFileList.p, 2);
703     RECT rcPreview;
704     RECT rcBody = {rcFileList.r.left, rcFileList.r.top,
705         rcClient.right - iPadding, rcFileList.r.bottom};
706     rcFileList.r.right = rcBody.right;
708     if(_show_preview)
709     {
710         rcPreview.top = rcBody.top;
711         rcPreview.left = rcClient.right - (rcBody.bottom - rcBody.top);
712         const int iMaxExtent = (int)(MaxExtentScale * (float)(rcBody.left + rcBody.right)) + iPadding / 2;
713         if(rcPreview.left < iMaxExtent) rcPreview.left = iMaxExtent;
714         rcPreview.bottom = rcBody.bottom;
715         rcPreview.right = rcBody.right;
717         // Re-layout the preview box
718         _mutex->lock();
720             _preview_width = rcPreview.right - rcPreview.left;
721             _preview_height = rcPreview.bottom - rcPreview.top;
723         _mutex->unlock();
725         render_preview();
727         MoveWindow(_preview_wnd, rcPreview.left, rcPreview.top,
728             _preview_width, _preview_height, TRUE);
730         rcFileList.r.right = rcPreview.left - iPadding;
731     }
733     // Re-layout the file list box
734     MoveWindow(hFileListWnd, rcFileList.r.left, rcFileList.r.top,
735         rcFileList.r.right - rcFileList.r.left,
736         rcFileList.r.bottom - rcFileList.r.top, TRUE);
738     // Re-layout the toolbar
739     RECTPOINTS rcToolBar;
740     GetWindowRect(_toolbar_wnd, &rcToolBar.r);
741     MapWindowPoints(NULL, _file_dialog_wnd, rcToolBar.p, 2);
742     MoveWindow(_toolbar_wnd, rcToolBar.r.left, rcToolBar.r.top,
743         rcToolBar.r.right - rcToolBar.r.left, rcToolBar.r.bottom - rcToolBar.r.top, TRUE);
746 void FileOpenDialogImplWin32::file_selected()
748     // Destroy any previous previews
749     free_preview();
752     // Determine if the file exists
753     DWORD attributes = GetFileAttributesW(_path_string);
754     if(attributes == 0xFFFFFFFF ||
755         attributes == FILE_ATTRIBUTE_DIRECTORY)
756     {
757         InvalidateRect(_preview_wnd, NULL, FALSE);
758         return;
759     }
761     // Check the file exists and get the file size
762     HANDLE file_handle = CreateFileW(_path_string, GENERIC_READ,
763         FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
764     if(file_handle == INVALID_HANDLE_VALUE) return;
765     const DWORD file_size = GetFileSize(file_handle, NULL);
766     if (file_size == INVALID_FILE_SIZE) return;
767     _preview_file_size = file_size / 1024;
768     CloseHandle(file_handle);
770     if(_show_preview) load_preview();
773 void FileOpenDialogImplWin32::load_preview()
775     // Destroy any previous previews
776     free_preview();
778     // Try to get the file icon
779     SHFILEINFOW fileInfo;
780     if(SUCCEEDED(SHGetFileInfoW(_path_string, 0, &fileInfo,
781         sizeof(fileInfo), SHGFI_ICON | SHGFI_LARGEICON)))
782         _preview_file_icon = fileInfo.hIcon;
784     // Will this file be too big?
785     if(_preview_file_size > MaxPreviewFileSize)
786     {
787         InvalidateRect(_preview_wnd, NULL, FALSE);
788         return;
789     }
791     // Prepare to render a preview
792     const Glib::ustring svg = ".svg";
793     const Glib::ustring svgz = ".svgz";
794     const Glib::ustring path = utf16_to_ustring(_path_string);
796     bool success = false;
798     _preview_document_width = _preview_document_height = 0;
800     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
801             (hasSuffix(path, svg) || hasSuffix(path, svgz)))
802         success = set_svg_preview();
803     else if (isValidImageFile(path))
804         success = set_image_preview();
805     else {
806         // Show no preview
807     }
809     if(success) render_preview();
811     InvalidateRect(_preview_wnd, NULL, FALSE);
814 void FileOpenDialogImplWin32::free_preview()
816     _mutex->lock();
817     if(_preview_bitmap != NULL)
818         DeleteObject(_preview_bitmap);
819     _preview_bitmap = NULL;
821     if(_preview_file_icon != NULL)
822         DestroyIcon(_preview_file_icon);
823     _preview_file_icon = NULL;
825     _preview_bitmap_image.clear();
826     _mutex->unlock();
829 bool FileOpenDialogImplWin32::set_svg_preview()
831     const int PreviewSize = 512;
833     gchar *utf8string = g_utf16_to_utf8((const gunichar2*)_path_string,
834         _MAX_PATH, NULL, NULL, NULL);
835     SPDocument *svgDoc = sp_document_new (utf8string, true);
836     g_free(utf8string);
838     // Check the document loaded properly
839     if(svgDoc == NULL) return false;
840     if(svgDoc->root == NULL)
841     {
842         sp_document_unref(svgDoc);
843         return false;
844     }
846     // Get the size of the document
847     const double svgWidth = sp_document_width(svgDoc);
848     const double svgHeight = sp_document_height(svgDoc);
850     // Find the minimum scale to fit the image inside the preview area
851     const double scaleFactorX =    PreviewSize / svgWidth;
852     const double scaleFactorY =    PreviewSize / svgHeight;
853     const double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
855     // Now get the resized values
856     const double scaledSvgWidth  = scaleFactor * svgWidth;
857     const double scaledSvgHeight = scaleFactor * svgHeight;
859     NR::Rect area(NR::Point(0, 0), NR::Point(scaledSvgWidth, scaledSvgHeight));
860     NRRectL areaL = {0, 0, scaledSvgWidth, scaledSvgHeight};
861     NRRectL bbox = {0, 0, scaledSvgWidth, scaledSvgHeight};
863     // write object bbox to area
864     NR::Maybe<NR::Rect> maybeArea(area);
865     sp_document_ensure_up_to_date (svgDoc);
866     sp_item_invoke_bbox((SPItem *) svgDoc->root, &maybeArea,
867         sp_item_i2r_affine((SPItem *)(svgDoc->root)), TRUE);
869     NRArena *const arena = NRArena::create();
871     unsigned const key = sp_item_display_key_new(1);
873     NRArenaItem *root = sp_item_invoke_show((SPItem*)(svgDoc->root),
874         arena, key, SP_ITEM_SHOW_DISPLAY);
876     NRGC gc(NULL);
877     nr_matrix_set_scale(&gc.transform, scaleFactor, scaleFactor);
879     nr_arena_item_invoke_update (root, NULL, &gc,
880         NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
882     // Prepare a GDI compatible NRPixBlock
883     NRPixBlock pixBlock;
884     pixBlock.size = NR_PIXBLOCK_SIZE_BIG;
885     pixBlock.mode = NR_PIXBLOCK_MODE_R8G8B8;
886     pixBlock.empty = 1;
887     pixBlock.visible_area.x0 = pixBlock.area.x0 = 0;
888     pixBlock.visible_area.y0 = pixBlock.area.y0 = 0;
889     pixBlock.visible_area.x1 = pixBlock.area.x1 = scaledSvgWidth;
890     pixBlock.visible_area.y1 = pixBlock.area.y1 = scaledSvgHeight;
891     pixBlock.rs = 4 * ((3 * (int)scaledSvgWidth + 3) / 4);
892     pixBlock.data.px = g_try_new (unsigned char, pixBlock.rs * scaledSvgHeight);
894     // Fail if the pixblock failed to allocate
895     if(pixBlock.data.px == NULL)
896     {
897         sp_document_unref(svgDoc);
898         return false;
899     }
901     memset(pixBlock.data.px, 0xFF, pixBlock.rs * scaledSvgHeight);
903     memcpy(&root->bbox, &areaL, sizeof(areaL));
905     // Render the image
906     nr_arena_item_invoke_render(NULL, root, &bbox, &pixBlock, /*0*/NR_ARENA_ITEM_RENDER_NO_CACHE);
908     // Tidy up
909     sp_document_unref(svgDoc);
910     sp_item_invoke_hide((SPItem*)(svgDoc->root), key);
911     nr_arena_item_unref(root);
912     nr_object_unref((NRObject *) arena);
914     // Create the GDK pixbuf
915     _mutex->lock();
917     _preview_bitmap_image = Gdk::Pixbuf::create_from_data(
918         pixBlock.data.px, Gdk::COLORSPACE_RGB, false, 8,
919         (int)scaledSvgWidth, (int)scaledSvgHeight, pixBlock.rs,
920         sigc::ptr_fun(destroy_svg_rendering));
922     _preview_document_width = scaledSvgWidth;
923     _preview_document_height = scaledSvgHeight;
924     _preview_image_width = svgWidth;
925     _preview_image_height = svgHeight;
927     _mutex->unlock();
929     return true;
932 void FileOpenDialogImplWin32::destroy_svg_rendering(const guint8 *buffer)
934     g_assert(buffer != NULL);
935     g_free((void*)buffer);
938 bool FileOpenDialogImplWin32::set_image_preview()
940     const Glib::ustring path = utf16_to_ustring(_path_string, _MAX_PATH);
942     _mutex->lock();
943     _preview_bitmap_image = Gdk::Pixbuf::create_from_file(path);
944     if(!_preview_bitmap_image) return false;
946     _preview_image_width = _preview_bitmap_image->get_width();
947     _preview_document_width = _preview_image_width;
948     _preview_image_height = _preview_bitmap_image->get_height();
949     _preview_document_height = _preview_image_height;
951     _mutex->unlock();
953     return true;
956 void FileOpenDialogImplWin32::render_preview()
958     double x, y;
959     const double blurRadius = 8;
960     const double halfBlurRadius = blurRadius / 2;
961     const int shaddowOffsetX = 0;
962     const int shaddowOffsetY = 2;
963     const int pagePadding = 5;
964     const double shaddowAlpha = 0.75;
966     // Is the preview showing?
967     if(!_show_preview)
968         return;
970     // Do we have anything to render?
971     _mutex->lock();
973     if(!_preview_bitmap_image)
974     {
975         _mutex->unlock();
976         return;
977     }
979     // Tidy up any previous bitmap renderings
980     if(_preview_bitmap != NULL)
981         DeleteObject(_preview_bitmap);
982     _preview_bitmap = NULL;
984     // Calculate the size of the caption
985     int captionHeight = 0;
987     if(_preview_wnd != NULL)
988     {
989         RECT rcCaptionRect;
990         WCHAR szCaption[_MAX_FNAME + 32];
991         const int iLength = format_caption(szCaption,
992             sizeof(szCaption) / sizeof(WCHAR));
994         HDC dc = GetDC(_preview_wnd);
995         DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
996             DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_CALCRECT);
997         ReleaseDC(_preview_wnd, dc);
999         captionHeight = rcCaptionRect.bottom - rcCaptionRect.top;
1000     }
1002     // Find the minimum scale to fit the image inside the preview area
1003     const double scaleFactorX =
1004         ((double)_preview_width - pagePadding * 2 - blurRadius)  / _preview_document_width;
1005     const double scaleFactorY =
1006         ((double)_preview_height - pagePadding * 2
1007         - shaddowOffsetY - halfBlurRadius - captionHeight) / _preview_document_height;
1008     double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
1009     scaleFactor = (scaleFactor > 1.0) ? 1.0 : scaleFactor;
1011     // Now get the resized values
1012     const double scaledSvgWidth  = scaleFactor * _preview_document_width;
1013     const double scaledSvgHeight = scaleFactor * _preview_document_height;
1015     const int svgX = pagePadding + halfBlurRadius;
1016     const int svgY = pagePadding;
1018     const int frameX = svgX - pagePadding;
1019     const int frameY = svgY - pagePadding;
1020     const int frameWidth = scaledSvgWidth + pagePadding * 2;
1021     const int frameHeight = scaledSvgHeight + pagePadding * 2;
1023     const int totalWidth = (int)ceil(frameWidth + blurRadius);
1024     const int totalHeight = (int)ceil(frameHeight + blurRadius);
1026     // Prepare the drawing surface
1027     HDC hDC = GetDC(_preview_wnd);
1028     HDC hMemDC = CreateCompatibleDC(hDC);
1029     _preview_bitmap = CreateCompatibleBitmap(hDC, totalWidth, totalHeight);
1030     HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, _preview_bitmap);
1031     Cairo::RefPtr<Win32Surface> surface = Win32Surface::create(hMemDC);
1032     Cairo::RefPtr<Context> context = Context::create(surface);
1034     // Paint the background to match the dialog colour
1035     const COLORREF background = GetSysColor(COLOR_3DFACE);
1036     context->set_source_rgb(
1037         GetRValue(background) / 255.0,
1038         GetGValue(background) / 255.0,
1039         GetBValue(background) / 255.0);
1040     context->paint();
1042     //----- Draw the drop shaddow -----//
1044     // Left Edge
1045     x = frameX + shaddowOffsetX - halfBlurRadius;
1046     Cairo::RefPtr<LinearGradient> leftEdgeFade = LinearGradient::create(
1047         x, 0.0, x + blurRadius, 0.0);
1048     leftEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1049     leftEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1050     context->set_source(leftEdgeFade);
1051     context->rectangle (x, frameY + shaddowOffsetY + halfBlurRadius,
1052         blurRadius, frameHeight - blurRadius);
1053     context->fill();
1055     // Right Edge
1056     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1057     Cairo::RefPtr<LinearGradient> rightEdgeFade = LinearGradient::create(
1058         x, 0.0,    x + blurRadius, 0.0);
1059     rightEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1060     rightEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1061     context->set_source(rightEdgeFade);
1062     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1063         frameY + shaddowOffsetY + halfBlurRadius,
1064         blurRadius, frameHeight - blurRadius);
1065     context->fill();
1067     // Top Edge
1068     y = frameY + shaddowOffsetY - halfBlurRadius;
1069     Cairo::RefPtr<LinearGradient> topEdgeFade = LinearGradient::create(
1070         0.0, y, 0.0, y + blurRadius);
1071     topEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1072     topEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1073     context->set_source(topEdgeFade);
1074     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1075         frameWidth - blurRadius, blurRadius);
1076     context->fill();
1078     // Bottom Edge
1079     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1080     Cairo::RefPtr<LinearGradient> bottomEdgeFade = LinearGradient::create(
1081         0.0, y,    0.0, y + blurRadius);
1082     bottomEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1083     bottomEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1084     context->set_source(bottomEdgeFade);
1085     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1086         frameWidth - blurRadius, blurRadius);
1087     context->fill();
1089     // Top Left Corner
1090     x = frameX + shaddowOffsetX - halfBlurRadius;
1091     y = frameY + shaddowOffsetY - halfBlurRadius;
1092     Cairo::RefPtr<RadialGradient> topLeftCornerFade = RadialGradient::create(
1093         x + blurRadius, y + blurRadius, 0, x + blurRadius, y + blurRadius, blurRadius);
1094     topLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1095     topLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1096     context->set_source(topLeftCornerFade);
1097     context->rectangle (x, y, blurRadius, blurRadius);
1098     context->fill();
1100     // Top Right Corner
1101     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1102     y = frameY + shaddowOffsetY - halfBlurRadius;
1103     Cairo::RefPtr<RadialGradient> topRightCornerFade = RadialGradient::create(
1104         x, y + blurRadius, 0, x, y + blurRadius, blurRadius);
1105     topRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1106     topRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1107     context->set_source(topRightCornerFade);
1108     context->rectangle (x, y, blurRadius, blurRadius);
1109     context->fill();
1111     // Bottom Left Corner
1112     x = frameX + shaddowOffsetX - halfBlurRadius;
1113     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1114     Cairo::RefPtr<RadialGradient> bottomLeftCornerFade = RadialGradient::create(
1115         x + blurRadius, y, 0, x + blurRadius, y, blurRadius);
1116     bottomLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1117     bottomLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1118     context->set_source(bottomLeftCornerFade);
1119     context->rectangle (x, y, blurRadius, blurRadius);
1120     context->fill();
1122     // Bottom Right Corner
1123     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1124     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1125     Cairo::RefPtr<RadialGradient> bottomRightCornerFade = RadialGradient::create(
1126         x, y, 0, x, y, blurRadius);
1127     bottomRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1128     bottomRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1129     context->set_source(bottomRightCornerFade);
1130     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1131         frameY + frameHeight + shaddowOffsetY - halfBlurRadius,
1132         blurRadius, blurRadius);
1133     context->fill();
1135     // Draw the frame
1136     context->set_line_width(1);
1137     context->rectangle (frameX, frameY,    frameWidth, frameHeight);
1139     context->set_source_rgb(1.0, 1.0, 1.0);
1140     context->fill_preserve();
1141     context->set_source_rgb(0.25, 0.25, 0.25);
1142     context->stroke_preserve();
1144     // Draw the image
1145     if(_preview_bitmap_image)    // Is the image a pixbuf?
1146     {
1147         // Set the transformation
1148         const Matrix matrix = {
1149             scaleFactor, 0,
1150             0, scaleFactor,
1151             svgX, svgY };
1152         context->set_matrix (matrix);
1154         // Render the image
1155         set_source_pixbuf (context, _preview_bitmap_image, 0, 0);
1156         context->paint();
1158         // Reset the transformation
1159         context->set_identity_matrix();
1160     }
1162     // Draw the inner frame
1163     context->set_source_rgb(0.75, 0.75, 0.75);
1164     context->rectangle (svgX, svgY,    scaledSvgWidth, scaledSvgHeight);
1165     context->stroke();
1167     _mutex->unlock();
1169     // Finish drawing
1170     surface->finish();
1171     SelectObject(hMemDC, hOldBitmap) ;
1172     DeleteDC(hMemDC);
1174     // Refresh the preview pane
1175     InvalidateRect(_preview_wnd, NULL, FALSE);
1178 int FileOpenDialogImplWin32::format_caption(wchar_t *caption, int caption_size)
1180     wchar_t szFileName[_MAX_FNAME];
1181     _wsplitpath(_path_string, NULL, NULL, szFileName, NULL);
1183     return snwprintf(caption, caption_size,
1184         L"%s\n%d kB\n%d \xD7 %d", szFileName, _preview_file_size,
1185         (int)_preview_document_width, (int)_preview_document_height);
1188 /**
1189  * Show this dialog modally.  Return true if user hits [OK]
1190  */
1191 bool
1192 FileOpenDialogImplWin32::show()
1194     // We can only run one worker thread at a time
1195     if(_mutex != NULL) return false;
1197     if(!Glib::thread_supported())
1198         Glib::thread_init();
1200     _result = false;
1201     _finished = false;
1202     _file_selected = false;
1203     _mutex = new Glib::Mutex();
1204     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1206     if(Glib::Thread::create(sigc::mem_fun(*this, &FileOpenDialogImplWin32::GetOpenFileName_thread), true))
1207     {
1208         while(1)
1209         {
1210             g_main_context_iteration(g_main_context_default(), FALSE);
1212             if(_mutex->trylock())
1213             {
1214                 // Read mutexed data
1215                 const bool finished = _finished;
1216                 const bool is_file_selected = _file_selected;
1217                 _file_selected = false;
1218                 _mutex->unlock();
1220                 if(finished) break;
1221                 if(is_file_selected) file_selected();
1222             }
1224             Sleep(10);
1225         }
1226     }
1228     // Tidy up
1229     delete _mutex;
1230     _mutex = NULL;
1232     return _result;
1235 /**
1236  * To Get Multiple filenames selected at-once.
1237  */
1238 std::vector<Glib::ustring>FileOpenDialogImplWin32::getFilenames()
1240     std::vector<Glib::ustring> result;
1241     result.push_back(getFilename());
1242     return result;
1246 /*#########################################################################
1247 ### F I L E    S A V E
1248 #########################################################################*/
1250 /**
1251  * Constructor
1252  */
1253 FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent,
1254             const Glib::ustring &dir,
1255             FileDialogType fileTypes,
1256             const char *title,
1257             const Glib::ustring &/*default_key*/) :
1258     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as")
1260     createFilterMenu();
1263 FileSaveDialogImplWin32::~FileSaveDialogImplWin32()
1267 void FileSaveDialogImplWin32::createFilterMenu()
1269     list<Filter> filter_list;
1271     knownExtensions.clear();
1273     // Compose the filter string
1274     Glib::ustring all_inkscape_files_filter, all_image_files_filter;
1275     Inkscape::Extension::DB::OutputList extension_list;
1276     Inkscape::Extension::db.get_output_list(extension_list);
1278     int filter_count = 0;
1279     int filter_length = 0;
1281     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1282          current_item != extension_list.end(); current_item++)
1283     {
1284         Inkscape::Extension::Output *omod = *current_item;
1285         if (omod->deactivated()) continue;
1287         filter_count++;
1289         Filter filter;
1291         // Extension
1292         const gchar *filter_extension = omod->get_extension();
1293         filter.filter = g_utf8_to_utf16(
1294             filter_extension, -1, NULL, &filter.filter_length, NULL);
1295         knownExtensions.insert( Glib::ustring(filter_extension).casefold() );
1297         // Type
1298         filter.name = g_utf8_to_utf16(
1299             _(omod->get_filetypename()), -1, NULL, &filter.name_length, NULL);
1301         filter.mod = omod;
1303         filter_length += filter.name_length +
1304             filter.filter_length + 3;   // Add 3 for two \0s and a *
1306         filter_list.push_back(filter);
1307     }
1309     int extension_index = 0;
1310     _extension_map = new Inkscape::Extension::Extension*[filter_count];
1312     _filter = new wchar_t[filter_length];
1313     wchar_t *filterptr = _filter;
1315     for(list<Filter>::iterator filter_iterator = filter_list.begin();
1316         filter_iterator != filter_list.end(); filter_iterator++)
1317     {
1318         const Filter &filter = *filter_iterator;
1320         wcsncpy(filterptr, (wchar_t*)filter.name, filter.name_length);
1321         filterptr += filter.name_length;
1322         g_free(filter.name);
1324         *(filterptr++) = L'\0';
1325         *(filterptr++) = L'*';
1327         wcsncpy(filterptr, (wchar_t*)filter.filter, filter.filter_length);
1328         filterptr += filter.filter_length;
1329         g_free(filter.filter);
1331         *(filterptr++) = L'\0';
1333         // Associate this input extension with the file type name
1334         _extension_map[extension_index++] = filter.mod;
1335     }
1336     *(filterptr++) = 0;
1338         _filter_count = extension_index;
1339     _filter_index = 1;  // A value of 1 selects the 1st filter - NOT the 2nd
1342 void FileSaveDialogImplWin32::GetSaveFileName_thread()
1344     OPENFILENAMEEXW ofn;
1346     g_assert(this != NULL);
1347     g_assert(_main_loop != NULL);
1349     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
1350         _current_directory.data(), _current_directory.length(),
1351                 NULL, NULL, NULL);
1353     // Copy the selected file name, converting from UTF-8 to UTF-16
1354     memset(_path_string, 0, sizeof(_path_string));
1355     gunichar2* utf16_path_string = g_utf8_to_utf16(
1356         myFilename.data(), -1, NULL, NULL, NULL);
1357     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
1358     g_free(utf16_path_string);
1360     ZeroMemory(&ofn, sizeof(ofn));
1361     ofn.lStructSize = sizeof(ofn);
1362     ofn.hwndOwner = _ownerHwnd;
1363     ofn.lpstrFile = _path_string;
1364     ofn.nMaxFile = _MAX_PATH;
1365     ofn.nFilterIndex = _filter_index;
1366     ofn.lpstrFileTitle = NULL;
1367     ofn.nMaxFileTitle = 0;
1368     ofn.lpstrInitialDir = current_directory_string;
1369     ofn.lpstrTitle = _title;
1370     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
1371     ofn.lpstrFilter = _filter;
1372     ofn.nFilterIndex = _filter_index;
1374     _result = GetSaveFileNameW(&ofn) != 0;
1376         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
1377     _filter_index = ofn.nFilterIndex;
1378     _extension = _extension_map[ofn.nFilterIndex - 1];
1380     // Copy the selected file name, converting from UTF-16 to UTF-8
1381     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
1383     // Tidy up
1384     g_free(current_directory_string);
1386     g_main_loop_quit(_main_loop);
1389 /**
1390  * Show this dialog modally.  Return true if user hits [OK]
1391  */
1392 bool
1393 FileSaveDialogImplWin32::show()
1395     if(!Glib::thread_supported())
1396         Glib::thread_init();
1398     _result = false;
1399     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1400         
1401         if(_main_loop != NULL)
1402         {
1403             if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true))
1404                 g_main_loop_run(_main_loop);
1406             if(_result)
1407                 appendExtension(myFilename, (Inkscape::Extension::Output*)_extension);
1408         }
1409         
1410     return _result;
1413 void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension * /*key*/ )
1415     // If no pointer to extension is passed in, look up based on filename extension.
1423 #endif
1425 /*
1426   Local Variables:
1427   mode:c++
1428   c-file-style:"stroustrup"
1429   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1430   indent-tabs-mode:nil
1431   fill-column:99
1432   End:
1433 */
1434 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :