Code

noop: reverted one line of commit #17642 (it's safe to call g_free with NULL)
[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     g_free(_title);
125 Inkscape::Extension::Extension *FileDialogBaseWin32::getSelectionType()
127     return _extension;
130 Glib::ustring FileDialogBaseWin32::getCurrentDirectory()
132     return _current_directory;
135 /*#########################################################################
136 ### F I L E    O P E N
137 #########################################################################*/
139 bool FileOpenDialogImplWin32::_show_preview = true;
141 /**
142  * Constructor.  Not called directly.  Use the factory.
143  */
144 FileOpenDialogImplWin32::FileOpenDialogImplWin32(Gtk::Window &parent,
145                                        const Glib::ustring &dir,
146                                        FileDialogType fileTypes,
147                                        const gchar *title) :
148     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.open")
150     // Initalize to Autodetect
151     _extension = NULL;
153     // Set our dialog type (open, import, etc...)
154     dialogType = fileTypes;
156     _show_preview_button_bitmap = NULL;
157     _preview_wnd = NULL;
158     _file_dialog_wnd = NULL;
159     _base_window_proc = NULL;
161     _preview_file_size = 0;
162     _preview_bitmap = NULL;
163     _preview_file_icon = NULL;
164     _preview_document_width = 0;
165     _preview_document_height = 0;
166     _preview_image_width = 0;
167     _preview_image_height = 0;
168         
169         _mutex = NULL;
171     createFilterMenu();
175 /**
176  * Destructor
177  */
178 FileOpenDialogImplWin32::~FileOpenDialogImplWin32()
180     if(_filter != NULL)
181         delete[] _filter;
182     if(_extension_map != NULL)
183         delete[] _extension_map;
186 void FileOpenDialogImplWin32::createFilterMenu()
188     list<Filter> filter_list;
190     // Compose the filter string
191     Inkscape::Extension::DB::InputList extension_list;
192     Inkscape::Extension::db.get_input_list(extension_list);
194     ustring all_inkscape_files_filter, all_image_files_filter;
195     Filter all_files, all_inkscape_files, all_image_files;
197     const gchar *all_files_filter_name = N_("All Files");
198     const gchar *all_inkscape_files_filter_name = N_("All Inkscape Files");
199     const gchar *all_image_files_filter_name = N_("All Image Files");
201     // Calculate the amount of memory required
202     int filter_count = 3;       // 3 - one for All Files, All Images and All Inkscape Files
203     int filter_length = 1;
205     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
206          current_item != extension_list.end(); current_item++)
207     {
208         Filter filter;
210         Inkscape::Extension::Input *imod = *current_item;
211         if (imod->deactivated()) continue;
213         // Type
214         filter.name = g_utf8_to_utf16(_(imod->get_filetypename()),
215             -1, NULL, &filter.name_length, NULL);
217         // Extension
218         const gchar *file_extension_name = imod->get_extension();
219         filter.filter = g_utf8_to_utf16(file_extension_name,
220             -1, NULL, &filter.filter_length, NULL);
222         filter.mod = imod;
223         filter_list.push_back(filter);
225         filter_length += filter.name_length +
226             filter.filter_length + 3;   // Add 3 for two \0s and a *
228         // Add to the "All Inkscape Files" Entry
229         if(all_inkscape_files_filter.length() > 0)
230             all_inkscape_files_filter += ";*";
231         all_inkscape_files_filter += file_extension_name;
232         if( strncmp("image", imod->get_mimetype(), 5) == 0)
233         {
234             // Add to the "All Image Files" Entry
235             if(all_image_files_filter.length() > 0)
236                 all_image_files_filter += ";*";
237             all_image_files_filter += file_extension_name;
238         }
240         filter_count++;
241     }
243     int extension_index = 0;
244     _extension_map = new Inkscape::Extension::Extension*[filter_count + 3];
246     // Filter Image Files
247     all_image_files.name = g_utf8_to_utf16(all_image_files_filter_name,
248         -1, NULL, &all_image_files.name_length, NULL);
249     all_image_files.filter = g_utf8_to_utf16(all_image_files_filter.data(),
250             -1, NULL, &all_image_files.filter_length, NULL);
251     filter_list.push_front(all_image_files);
252     _extension_map[extension_index++] = NULL;
254     // Filter Inkscape Files
255     all_inkscape_files.name = g_utf8_to_utf16(all_inkscape_files_filter_name,
256         -1, NULL, &all_inkscape_files.name_length, NULL);
257     all_inkscape_files.filter = g_utf8_to_utf16(all_inkscape_files_filter.data(),
258             -1, NULL, &all_inkscape_files.filter_length, NULL);
259     filter_list.push_front(all_inkscape_files);
260     _extension_map[extension_index++] = NULL;
262     // Filter All Files
263     all_files.name = g_utf8_to_utf16(all_files_filter_name,
264         -1, NULL, &all_files.name_length, NULL);
265     all_files.filter = NULL;
266     all_files.filter_length = 0;
267     filter_list.push_front(all_files);
268     _extension_map[extension_index++] = NULL;
270     filter_length += all_files.name_length + 3 +
271                     all_inkscape_files.filter_length +
272                     all_inkscape_files.name_length + 3 +
273                     all_image_files.filter_length +
274                     all_image_files.name_length + 3 + 1;
275      // Add 3 for 2*2 \0s and a *, and 1 for a trailing \0
276          
277         _filter = new wchar_t[filter_length];
278     wchar_t *filterptr = _filter;
279         
280     for(list<Filter>::iterator filter_iterator = filter_list.begin();
281         filter_iterator != filter_list.end(); filter_iterator++)
282     {
283         const Filter &filter = *filter_iterator;
285         wcsncpy(filterptr, (wchar_t*)filter.name, filter.name_length);
286         filterptr += filter.name_length;
287         g_free(filter.name);
289         *(filterptr++) = L'\0';
290         *(filterptr++) = L'*';
292         if(filter.filter != NULL)
293         {
294             wcsncpy(filterptr, (wchar_t*)filter.filter, filter.filter_length);
295             filterptr += filter.filter_length;
296             g_free(filter.filter);
297         }
299         *(filterptr++) = L'\0';
301         // Associate this input extension with the file type name
302         _extension_map[extension_index++] = filter.mod;
303     }
304     *(filterptr++) = L'\0';
305         
306         _filter_count = extension_index;
307     _filter_index = 2;  // Select the 2nd filter in the list - 2 is NOT the 3rd
310 void FileOpenDialogImplWin32::GetOpenFileName_thread()
312     OPENFILENAMEEXW ofn;
314     g_assert(this != NULL);
315         g_assert(_mutex != NULL);
316     
317     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
318         _current_directory.data(), _current_directory.length(),
319                 NULL, NULL, NULL);
321     memset(&ofn, 0, sizeof(ofn));
323     // Copy the selected file name, converting from UTF-8 to UTF-16
324     memset(_path_string, 0, sizeof(_path_string));
325     gunichar2* utf16_path_string = g_utf8_to_utf16(
326         myFilename.data(), -1, NULL, NULL, NULL);
327     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
328     g_free(utf16_path_string);
330     ofn.lStructSize = sizeof(ofn);
331     ofn.hwndOwner = _ownerHwnd;
332     ofn.lpstrFile = _path_string;
333     ofn.nMaxFile = _MAX_PATH;
334     ofn.lpstrFileTitle = NULL;
335     ofn.nMaxFileTitle = 0;
336     ofn.lpstrInitialDir = current_directory_string;
337     ofn.lpstrTitle = _title;
338     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_ENABLESIZING;
339     ofn.lpstrFilter = _filter;
340     ofn.nFilterIndex = _filter_index;
341     ofn.lpfnHook = GetOpenFileName_hookproc;
342     ofn.lCustData = (LPARAM)this;
344     _result = GetOpenFileNameW(&ofn) != 0;
346         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
347     _filter_index = ofn.nFilterIndex;
348     _extension = _extension_map[ofn.nFilterIndex - 1];
350     // Copy the selected file name, converting from UTF-16 to UTF-8
351     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
353     // Tidy up
354     g_free(current_directory_string);
356     _mutex->lock();
357     _finished = true;
358     _mutex->unlock();
361 void FileOpenDialogImplWin32::register_preview_wnd_class()
363     HINSTANCE hInstance = GetModuleHandle(NULL);
364     const WNDCLASSA PreviewWndClass =
365     {
366         CS_HREDRAW | CS_VREDRAW,
367         preview_wnd_proc,
368         0,
369         0,
370         hInstance,
371         NULL,
372         LoadCursor(hInstance, IDC_ARROW),
373         (HBRUSH)(COLOR_BTNFACE + 1),
374         NULL,
375         PreviewWindowClassName
376     };
378     RegisterClassA(&PreviewWndClass);
381 UINT_PTR CALLBACK FileOpenDialogImplWin32::GetOpenFileName_hookproc(
382     HWND hdlg, UINT uiMsg, WPARAM, LPARAM lParam)
384     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
385         GetWindowLongPtr(hdlg, GWLP_USERDATA);
387     switch(uiMsg)
388     {
389     case WM_INITDIALOG:
390         {
391             HWND hParentWnd = GetParent(hdlg);
392             HINSTANCE hInstance = GetModuleHandle(NULL);
394             // Make the window a bit wider
395             RECT rcRect;
396             GetWindowRect(hParentWnd, &rcRect);
397             MoveWindow(hParentWnd, rcRect.left, rcRect.top,
398                 rcRect.right - rcRect.left + PreviewWidening,
399                 rcRect.bottom - rcRect.top,
400                 FALSE);
402             // Set the pointer to the object
403             OPENFILENAMEW *ofn = (OPENFILENAMEW*)lParam;
404             SetWindowLongPtr(hdlg, GWLP_USERDATA, ofn->lCustData);
405             SetWindowLongPtr(hParentWnd, GWLP_USERDATA, ofn->lCustData);
406             pImpl = (FileOpenDialogImplWin32*)ofn->lCustData;
408             // Subclass the parent
409             pImpl->_base_window_proc = (WNDPROC)GetWindowLongPtr(hParentWnd, GWL_WNDPROC);
410             SetWindowLongPtr(hParentWnd, GWL_WNDPROC, (LONG_PTR)file_dialog_subclass_proc);
412             // Add a button to the toolbar
413             pImpl->_toolbar_wnd = FindWindowEx(hParentWnd, NULL, "ToolbarWindow32", NULL);
415             pImpl->_show_preview_button_bitmap = LoadBitmap(
416                 hInstance, MAKEINTRESOURCE(IDC_SHOW_PREVIEW));
417             TBADDBITMAP tbAddBitmap = {NULL, (UINT)pImpl->_show_preview_button_bitmap};
418             const int iBitmapIndex = SendMessage(pImpl->_toolbar_wnd,
419                 TB_ADDBITMAP, 1, (LPARAM)&tbAddBitmap);
421             TBBUTTON tbButton;
422             memset(&tbButton, 0, sizeof(TBBUTTON));
423             tbButton.iBitmap = iBitmapIndex;
424             tbButton.idCommand = IDC_SHOW_PREVIEW;
425             tbButton.fsState = (pImpl->_show_preview ? TBSTATE_CHECKED : 0)
426                 | TBSTATE_ENABLED;
427             tbButton.fsStyle = TBSTYLE_CHECK;
428             tbButton.iString = (INT_PTR)_("Show Preview");
429             SendMessage(pImpl->_toolbar_wnd, TB_ADDBUTTONS, 1, (LPARAM)&tbButton);
431             // Create preview pane
432             register_preview_wnd_class();
434             pImpl->_mutex->lock();
436                 pImpl->_file_dialog_wnd = hParentWnd;
438                 pImpl->_preview_wnd =
439                     CreateWindowA(PreviewWindowClassName, "",
440                         WS_CHILD | WS_VISIBLE,
441                         0, 0, 100, 100, hParentWnd, NULL, hInstance, NULL);
442                 SetWindowLongPtr(pImpl->_preview_wnd, GWLP_USERDATA, ofn->lCustData);
444             pImpl->_mutex->unlock();
446             pImpl->layout_dialog();
447         }
448         break;
450     case WM_NOTIFY:
451         {
453         OFNOTIFY *pOFNotify = reinterpret_cast<OFNOTIFY*>(lParam);
454         switch(pOFNotify->hdr.code)
455         {
456         case CDN_SELCHANGE:
457             {
458                 if(pImpl != NULL)
459                 {
460                     // Get the file name
461                     pImpl->_mutex->lock();
463                     SendMessage(pOFNotify->hdr.hwndFrom, CDM_GETFILEPATH,
464                         sizeof(pImpl->_path_string) / sizeof(wchar_t),
465                         (LPARAM)pImpl->_path_string);
467                     pImpl->_file_selected = true;
469                     pImpl->_mutex->unlock();
470                 }
471             }
472             break;
473         }
474         }
475         break;
477     case WM_CLOSE:
478         pImpl->_mutex->lock();
479         pImpl->_preview_file_size = 0;
481         pImpl->_file_dialog_wnd = NULL;
482         DestroyWindow(pImpl->_preview_wnd);
483         pImpl->_preview_wnd = NULL;
484         DeleteObject(pImpl->_show_preview_button_bitmap);
485         pImpl->_show_preview_button_bitmap = NULL;
486         pImpl->_mutex->unlock();
488         break;
489     }
491     // Use default dialog behaviour
492     return 0;
495 LRESULT CALLBACK FileOpenDialogImplWin32::file_dialog_subclass_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
497     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
498         GetWindowLongPtr(hwnd, GWLP_USERDATA);
500     LRESULT lResult = CallWindowProc(pImpl->_base_window_proc, hwnd, uMsg, wParam, lParam);
502     switch(uMsg)
503     {
504     case WM_SHOWWINDOW:
505         if(wParam != 0)
506             pImpl->layout_dialog();
507         break;
509     case WM_SIZE:
510         pImpl->layout_dialog();
511         break;
513     case WM_COMMAND:
514         if(wParam == IDC_SHOW_PREVIEW)
515         {
516             const bool enable = SendMessage(pImpl->_toolbar_wnd,
517                 TB_ISBUTTONCHECKED, IDC_SHOW_PREVIEW, 0) != 0;
518             pImpl->enable_preview(enable);
519         }
520         break;
521     }
523     return lResult;
526 LRESULT CALLBACK FileOpenDialogImplWin32::preview_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
528     const int CaptionPadding = 4;
529     const int IconSize = 32;
531     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
532         GetWindowLongPtr(hwnd, GWLP_USERDATA);
534     LRESULT lResult = 0;
536     switch(uMsg)
537     {
538     case WM_ERASEBKGND:
539         // Do nothing to erase the background
540         //  - otherwise there'll be flicker
541         lResult = 1;
542         break;
544     case WM_PAINT:
545         {
546             // Get the client rect
547             RECT rcClient;
548             GetClientRect(hwnd, &rcClient);
550             // Prepare to paint
551             PAINTSTRUCT paint_struct;
552             HDC dc = BeginPaint(hwnd, &paint_struct);
554             HFONT hCaptionFont = (HFONT)SendMessage(GetParent(hwnd),
555                     WM_GETFONT, 0, 0);
556             HFONT hOldFont = (HFONT)SelectObject(dc, hCaptionFont);
557             SetBkMode(dc, TRANSPARENT);
559             pImpl->_mutex->lock();
561             if(pImpl->_path_string[0] == 0)
562             {
563                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
564                 DrawText(dc, _("No file selected"), -1, &rcClient,
565                     DT_CENTER | DT_VCENTER | DT_NOPREFIX);
566             }
567             else if(pImpl->_preview_bitmap != NULL)
568             {
569                 BITMAP bitmap;
570                 GetObject(pImpl->_preview_bitmap, sizeof(bitmap), &bitmap);
571                 const int destX = (rcClient.right - bitmap.bmWidth) / 2;
573                 // Render the image
574                 HDC hSrcDC = CreateCompatibleDC(dc);
575                 HBITMAP hOldBitmap = (HBITMAP)SelectObject(hSrcDC, pImpl->_preview_bitmap);
577                 BitBlt(dc, destX, 0, bitmap.bmWidth, bitmap.bmHeight,
578                     hSrcDC, 0, 0, SRCCOPY);
580                 SelectObject(hSrcDC, hOldBitmap);
581                 DeleteDC(hSrcDC);
583                 // Fill in the background area
584                 HRGN hEraseRgn = CreateRectRgn(rcClient.left, rcClient.top,
585                     rcClient.right, rcClient.bottom);
586                 HRGN hImageRgn = CreateRectRgn(destX, 0,
587                     destX + bitmap.bmWidth, bitmap.bmHeight);
588                 CombineRgn(hEraseRgn, hEraseRgn, hImageRgn, RGN_DIFF);
590                 FillRgn(dc, hEraseRgn, GetSysColorBrush(COLOR_3DFACE));
592                 DeleteObject(hImageRgn);
593                 DeleteObject(hEraseRgn);
595                 // Draw the caption on
596                 RECT rcCaptionRect = {rcClient.left,
597                     rcClient.top + bitmap.bmHeight + CaptionPadding,
598                     rcClient.right, rcClient.bottom};
600                 WCHAR szCaption[_MAX_FNAME + 32];
601                 const int iLength = pImpl->format_caption(
602                     szCaption, sizeof(szCaption) / sizeof(WCHAR));
604                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
605                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
606             }
607             else if(pImpl->_preview_file_icon != NULL)
608             {
609                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
611                 // Draw the files icon
612                 const int destX = (rcClient.right - IconSize) / 2;
613                 DrawIconEx(dc, destX, 0, pImpl->_preview_file_icon,
614                     IconSize, IconSize, 0, NULL,
615                     DI_NORMAL | DI_COMPAT);
617                 // Draw the caption on
618                 RECT rcCaptionRect = {rcClient.left,
619                     rcClient.top + IconSize + CaptionPadding,
620                     rcClient.right, rcClient.bottom};
622                 WCHAR szFileName[_MAX_FNAME], szCaption[_MAX_FNAME + 32];
623                 _wsplitpath(pImpl->_path_string, NULL, NULL, szFileName, NULL);
625                 const int iLength = snwprintf(szCaption,
626                     sizeof(szCaption), L"%s\n%d kB",
627                     szFileName, pImpl->_preview_file_size);
629                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
630                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
631             }
632             else
633             {
634                 // Can't show anything!
635                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
636             }
638             pImpl->_mutex->unlock();
640             // Finish painting
641             SelectObject(dc, hOldFont);
642             EndPaint(hwnd, &paint_struct);
643         }
645         break;
647     case WM_DESTROY:
648         pImpl->free_preview();
649         break;
651     default:
652         lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
653         break;
654     }
656     return lResult;
659 void FileOpenDialogImplWin32::enable_preview(bool enable)
661     _show_preview = enable;
663     // Relayout the dialog
664     ShowWindow(_preview_wnd, enable ? SW_SHOW : SW_HIDE);
665     layout_dialog();
667     // Load or unload the preview
668     if(enable)
669     {
670         _mutex->lock();
671         _file_selected = true;
672         _mutex->unlock();
673     }
674     else free_preview();
677 void FileOpenDialogImplWin32::layout_dialog()
679     union RECTPOINTS
680     {
681         RECT r;
682         POINT p[2];
683     };
685     const float MaxExtentScale = 2.0f / 3.0f;
687     RECT rcClient;
688     GetClientRect(_file_dialog_wnd, &rcClient);
690     // Re-layout the dialog
691     HWND hFileListWnd = GetDlgItem(_file_dialog_wnd, lst2);
692     HWND hFolderComboWnd = GetDlgItem(_file_dialog_wnd, cmb2);
695     RECT rcFolderComboRect;
696     RECTPOINTS rcFileList;
697     GetWindowRect(hFileListWnd, &rcFileList.r);
698     GetWindowRect(hFolderComboWnd, &rcFolderComboRect);
699     const int iPadding = rcFileList.r.top - rcFolderComboRect.bottom;
700     MapWindowPoints(NULL, _file_dialog_wnd, rcFileList.p, 2);
702     RECT rcPreview;
703     RECT rcBody = {rcFileList.r.left, rcFileList.r.top,
704         rcClient.right - iPadding, rcFileList.r.bottom};
705     rcFileList.r.right = rcBody.right;
707     if(_show_preview)
708     {
709         rcPreview.top = rcBody.top;
710         rcPreview.left = rcClient.right - (rcBody.bottom - rcBody.top);
711         const int iMaxExtent = (int)(MaxExtentScale * (float)(rcBody.left + rcBody.right)) + iPadding / 2;
712         if(rcPreview.left < iMaxExtent) rcPreview.left = iMaxExtent;
713         rcPreview.bottom = rcBody.bottom;
714         rcPreview.right = rcBody.right;
716         // Re-layout the preview box
717         _mutex->lock();
719             _preview_width = rcPreview.right - rcPreview.left;
720             _preview_height = rcPreview.bottom - rcPreview.top;
722         _mutex->unlock();
724         render_preview();
726         MoveWindow(_preview_wnd, rcPreview.left, rcPreview.top,
727             _preview_width, _preview_height, TRUE);
729         rcFileList.r.right = rcPreview.left - iPadding;
730     }
732     // Re-layout the file list box
733     MoveWindow(hFileListWnd, rcFileList.r.left, rcFileList.r.top,
734         rcFileList.r.right - rcFileList.r.left,
735         rcFileList.r.bottom - rcFileList.r.top, TRUE);
737     // Re-layout the toolbar
738     RECTPOINTS rcToolBar;
739     GetWindowRect(_toolbar_wnd, &rcToolBar.r);
740     MapWindowPoints(NULL, _file_dialog_wnd, rcToolBar.p, 2);
741     MoveWindow(_toolbar_wnd, rcToolBar.r.left, rcToolBar.r.top,
742         rcToolBar.r.right - rcToolBar.r.left, rcToolBar.r.bottom - rcToolBar.r.top, TRUE);
745 void FileOpenDialogImplWin32::file_selected()
747     // Destroy any previous previews
748     free_preview();
751     // Determine if the file exists
752     DWORD attributes = GetFileAttributesW(_path_string);
753     if(attributes == 0xFFFFFFFF ||
754         attributes == FILE_ATTRIBUTE_DIRECTORY)
755     {
756         InvalidateRect(_preview_wnd, NULL, FALSE);
757         return;
758     }
760     // Check the file exists and get the file size
761     HANDLE file_handle = CreateFileW(_path_string, GENERIC_READ,
762         FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
763     if(file_handle == INVALID_HANDLE_VALUE) return;
764     const DWORD file_size = GetFileSize(file_handle, NULL);
765     if (file_size == INVALID_FILE_SIZE) return;
766     _preview_file_size = file_size / 1024;
767     CloseHandle(file_handle);
769     if(_show_preview) load_preview();
772 void FileOpenDialogImplWin32::load_preview()
774     // Destroy any previous previews
775     free_preview();
777     // Try to get the file icon
778     SHFILEINFOW fileInfo;
779     if(SUCCEEDED(SHGetFileInfoW(_path_string, 0, &fileInfo,
780         sizeof(fileInfo), SHGFI_ICON | SHGFI_LARGEICON)))
781         _preview_file_icon = fileInfo.hIcon;
783     // Will this file be too big?
784     if(_preview_file_size > MaxPreviewFileSize)
785     {
786         InvalidateRect(_preview_wnd, NULL, FALSE);
787         return;
788     }
790     // Prepare to render a preview
791     const Glib::ustring svg = ".svg";
792     const Glib::ustring svgz = ".svgz";
793     const Glib::ustring path = utf16_to_ustring(_path_string);
795     bool success = false;
797     _preview_document_width = _preview_document_height = 0;
799     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
800             (hasSuffix(path, svg) || hasSuffix(path, svgz)))
801         success = set_svg_preview();
802     else if (isValidImageFile(path))
803         success = set_image_preview();
804     else {
805         // Show no preview
806     }
808     if(success) render_preview();
810     InvalidateRect(_preview_wnd, NULL, FALSE);
813 void FileOpenDialogImplWin32::free_preview()
815     _mutex->lock();
816     if(_preview_bitmap != NULL)
817         DeleteObject(_preview_bitmap);
818     _preview_bitmap = NULL;
820     if(_preview_file_icon != NULL)
821         DestroyIcon(_preview_file_icon);
822     _preview_file_icon = NULL;
824     _preview_bitmap_image.clear();
825     _mutex->unlock();
828 bool FileOpenDialogImplWin32::set_svg_preview()
830     const int PreviewSize = 512;
832     gchar *utf8string = g_utf16_to_utf8((const gunichar2*)_path_string,
833         _MAX_PATH, NULL, NULL, NULL);
834     SPDocument *svgDoc = sp_document_new (utf8string, true);
835     g_free(utf8string);
837     // Check the document loaded properly
838     if(svgDoc == NULL) return false;
839     if(svgDoc->root == NULL)
840     {
841         sp_document_unref(svgDoc);
842         return false;
843     }
845     // Get the size of the document
846     const double svgWidth = sp_document_width(svgDoc);
847     const double svgHeight = sp_document_height(svgDoc);
849     // Find the minimum scale to fit the image inside the preview area
850     const double scaleFactorX =    PreviewSize / svgWidth;
851     const double scaleFactorY =    PreviewSize / svgHeight;
852     const double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
854     // Now get the resized values
855     const double scaledSvgWidth  = scaleFactor * svgWidth;
856     const double scaledSvgHeight = scaleFactor * svgHeight;
858     NR::Rect area(NR::Point(0, 0), NR::Point(scaledSvgWidth, scaledSvgHeight));
859     NRRectL areaL = {0, 0, scaledSvgWidth, scaledSvgHeight};
860     NRRectL bbox = {0, 0, scaledSvgWidth, scaledSvgHeight};
862     // write object bbox to area
863     NR::Maybe<NR::Rect> maybeArea(area);
864     sp_document_ensure_up_to_date (svgDoc);
865     sp_item_invoke_bbox((SPItem *) svgDoc->root, &maybeArea,
866         sp_item_i2r_affine((SPItem *)(svgDoc->root)), TRUE);
868     NRArena *const arena = NRArena::create();
870     unsigned const key = sp_item_display_key_new(1);
872     NRArenaItem *root = sp_item_invoke_show((SPItem*)(svgDoc->root),
873         arena, key, SP_ITEM_SHOW_DISPLAY);
875     NRGC gc(NULL);
876     nr_matrix_set_scale(&gc.transform, scaleFactor, scaleFactor);
878     nr_arena_item_invoke_update (root, NULL, &gc,
879         NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
881     // Prepare a GDI compatible NRPixBlock
882     NRPixBlock pixBlock;
883     pixBlock.size = NR_PIXBLOCK_SIZE_BIG;
884     pixBlock.mode = NR_PIXBLOCK_MODE_R8G8B8;
885     pixBlock.empty = 1;
886     pixBlock.visible_area.x0 = pixBlock.area.x0 = 0;
887     pixBlock.visible_area.y0 = pixBlock.area.y0 = 0;
888     pixBlock.visible_area.x1 = pixBlock.area.x1 = scaledSvgWidth;
889     pixBlock.visible_area.y1 = pixBlock.area.y1 = scaledSvgHeight;
890     pixBlock.rs = 4 * ((3 * (int)scaledSvgWidth + 3) / 4);
891     pixBlock.data.px = g_try_new (unsigned char, pixBlock.rs * scaledSvgHeight);
893     // Fail if the pixblock failed to allocate
894     if(pixBlock.data.px == NULL)
895     {
896         sp_document_unref(svgDoc);
897         return false;
898     }
900     memset(pixBlock.data.px, 0xFF, pixBlock.rs * scaledSvgHeight);
902     memcpy(&root->bbox, &areaL, sizeof(areaL));
904     // Render the image
905     nr_arena_item_invoke_render(NULL, root, &bbox, &pixBlock, /*0*/NR_ARENA_ITEM_RENDER_NO_CACHE);
907     // Tidy up
908     sp_document_unref(svgDoc);
909     sp_item_invoke_hide((SPItem*)(svgDoc->root), key);
910     nr_arena_item_unref(root);
911     nr_object_unref((NRObject *) arena);
913     // Create the GDK pixbuf
914     _mutex->lock();
916     _preview_bitmap_image = Gdk::Pixbuf::create_from_data(
917         pixBlock.data.px, Gdk::COLORSPACE_RGB, false, 8,
918         (int)scaledSvgWidth, (int)scaledSvgHeight, pixBlock.rs,
919         sigc::ptr_fun(destroy_svg_rendering));
921     _preview_document_width = scaledSvgWidth;
922     _preview_document_height = scaledSvgHeight;
923     _preview_image_width = svgWidth;
924     _preview_image_height = svgHeight;
926     _mutex->unlock();
928     return true;
931 void FileOpenDialogImplWin32::destroy_svg_rendering(const guint8 *buffer)
933     g_assert(buffer != NULL);
934     g_free((void*)buffer);
937 bool FileOpenDialogImplWin32::set_image_preview()
939     const Glib::ustring path = utf16_to_ustring(_path_string, _MAX_PATH);
941     _mutex->lock();
942     _preview_bitmap_image = Gdk::Pixbuf::create_from_file(path);
943     if(!_preview_bitmap_image) return false;
945     _preview_image_width = _preview_bitmap_image->get_width();
946     _preview_document_width = _preview_image_width;
947     _preview_image_height = _preview_bitmap_image->get_height();
948     _preview_document_height = _preview_image_height;
950     _mutex->unlock();
952     return true;
955 void FileOpenDialogImplWin32::render_preview()
957     double x, y;
958     const double blurRadius = 8;
959     const double halfBlurRadius = blurRadius / 2;
960     const int shaddowOffsetX = 0;
961     const int shaddowOffsetY = 2;
962     const int pagePadding = 5;
963     const double shaddowAlpha = 0.75;
965     // Is the preview showing?
966     if(!_show_preview)
967         return;
969     // Do we have anything to render?
970     _mutex->lock();
972     if(!_preview_bitmap_image)
973     {
974         _mutex->unlock();
975         return;
976     }
978     // Tidy up any previous bitmap renderings
979     if(_preview_bitmap != NULL)
980         DeleteObject(_preview_bitmap);
981     _preview_bitmap = NULL;
983     // Calculate the size of the caption
984     int captionHeight = 0;
986     if(_preview_wnd != NULL)
987     {
988         RECT rcCaptionRect;
989         WCHAR szCaption[_MAX_FNAME + 32];
990         const int iLength = format_caption(szCaption,
991             sizeof(szCaption) / sizeof(WCHAR));
993         HDC dc = GetDC(_preview_wnd);
994         DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
995             DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_CALCRECT);
996         ReleaseDC(_preview_wnd, dc);
998         captionHeight = rcCaptionRect.bottom - rcCaptionRect.top;
999     }
1001     // Find the minimum scale to fit the image inside the preview area
1002     const double scaleFactorX =
1003         ((double)_preview_width - pagePadding * 2 - blurRadius)  / _preview_document_width;
1004     const double scaleFactorY =
1005         ((double)_preview_height - pagePadding * 2
1006         - shaddowOffsetY - halfBlurRadius - captionHeight) / _preview_document_height;
1007     double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
1008     scaleFactor = (scaleFactor > 1.0) ? 1.0 : scaleFactor;
1010     // Now get the resized values
1011     const double scaledSvgWidth  = scaleFactor * _preview_document_width;
1012     const double scaledSvgHeight = scaleFactor * _preview_document_height;
1014     const int svgX = pagePadding + halfBlurRadius;
1015     const int svgY = pagePadding;
1017     const int frameX = svgX - pagePadding;
1018     const int frameY = svgY - pagePadding;
1019     const int frameWidth = scaledSvgWidth + pagePadding * 2;
1020     const int frameHeight = scaledSvgHeight + pagePadding * 2;
1022     const int totalWidth = (int)ceil(frameWidth + blurRadius);
1023     const int totalHeight = (int)ceil(frameHeight + blurRadius);
1025     // Prepare the drawing surface
1026     HDC hDC = GetDC(_preview_wnd);
1027     HDC hMemDC = CreateCompatibleDC(hDC);
1028     _preview_bitmap = CreateCompatibleBitmap(hDC, totalWidth, totalHeight);
1029     HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, _preview_bitmap);
1030     Cairo::RefPtr<Win32Surface> surface = Win32Surface::create(hMemDC);
1031     Cairo::RefPtr<Context> context = Context::create(surface);
1033     // Paint the background to match the dialog colour
1034     const COLORREF background = GetSysColor(COLOR_3DFACE);
1035     context->set_source_rgb(
1036         GetRValue(background) / 255.0,
1037         GetGValue(background) / 255.0,
1038         GetBValue(background) / 255.0);
1039     context->paint();
1041     //----- Draw the drop shaddow -----//
1043     // Left Edge
1044     x = frameX + shaddowOffsetX - halfBlurRadius;
1045     Cairo::RefPtr<LinearGradient> leftEdgeFade = LinearGradient::create(
1046         x, 0.0, x + blurRadius, 0.0);
1047     leftEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1048     leftEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1049     context->set_source(leftEdgeFade);
1050     context->rectangle (x, frameY + shaddowOffsetY + halfBlurRadius,
1051         blurRadius, frameHeight - blurRadius);
1052     context->fill();
1054     // Right Edge
1055     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1056     Cairo::RefPtr<LinearGradient> rightEdgeFade = LinearGradient::create(
1057         x, 0.0,    x + blurRadius, 0.0);
1058     rightEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1059     rightEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1060     context->set_source(rightEdgeFade);
1061     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1062         frameY + shaddowOffsetY + halfBlurRadius,
1063         blurRadius, frameHeight - blurRadius);
1064     context->fill();
1066     // Top Edge
1067     y = frameY + shaddowOffsetY - halfBlurRadius;
1068     Cairo::RefPtr<LinearGradient> topEdgeFade = LinearGradient::create(
1069         0.0, y, 0.0, y + blurRadius);
1070     topEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1071     topEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1072     context->set_source(topEdgeFade);
1073     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1074         frameWidth - blurRadius, blurRadius);
1075     context->fill();
1077     // Bottom Edge
1078     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1079     Cairo::RefPtr<LinearGradient> bottomEdgeFade = LinearGradient::create(
1080         0.0, y,    0.0, y + blurRadius);
1081     bottomEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1082     bottomEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1083     context->set_source(bottomEdgeFade);
1084     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1085         frameWidth - blurRadius, blurRadius);
1086     context->fill();
1088     // Top Left Corner
1089     x = frameX + shaddowOffsetX - halfBlurRadius;
1090     y = frameY + shaddowOffsetY - halfBlurRadius;
1091     Cairo::RefPtr<RadialGradient> topLeftCornerFade = RadialGradient::create(
1092         x + blurRadius, y + blurRadius, 0, x + blurRadius, y + blurRadius, blurRadius);
1093     topLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1094     topLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1095     context->set_source(topLeftCornerFade);
1096     context->rectangle (x, y, blurRadius, blurRadius);
1097     context->fill();
1099     // Top Right Corner
1100     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1101     y = frameY + shaddowOffsetY - halfBlurRadius;
1102     Cairo::RefPtr<RadialGradient> topRightCornerFade = RadialGradient::create(
1103         x, y + blurRadius, 0, x, y + blurRadius, blurRadius);
1104     topRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1105     topRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1106     context->set_source(topRightCornerFade);
1107     context->rectangle (x, y, blurRadius, blurRadius);
1108     context->fill();
1110     // Bottom Left Corner
1111     x = frameX + shaddowOffsetX - halfBlurRadius;
1112     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1113     Cairo::RefPtr<RadialGradient> bottomLeftCornerFade = RadialGradient::create(
1114         x + blurRadius, y, 0, x + blurRadius, y, blurRadius);
1115     bottomLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1116     bottomLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1117     context->set_source(bottomLeftCornerFade);
1118     context->rectangle (x, y, blurRadius, blurRadius);
1119     context->fill();
1121     // Bottom Right Corner
1122     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1123     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1124     Cairo::RefPtr<RadialGradient> bottomRightCornerFade = RadialGradient::create(
1125         x, y, 0, x, y, blurRadius);
1126     bottomRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1127     bottomRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1128     context->set_source(bottomRightCornerFade);
1129     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1130         frameY + frameHeight + shaddowOffsetY - halfBlurRadius,
1131         blurRadius, blurRadius);
1132     context->fill();
1134     // Draw the frame
1135     context->set_line_width(1);
1136     context->rectangle (frameX, frameY,    frameWidth, frameHeight);
1138     context->set_source_rgb(1.0, 1.0, 1.0);
1139     context->fill_preserve();
1140     context->set_source_rgb(0.25, 0.25, 0.25);
1141     context->stroke_preserve();
1143     // Draw the image
1144     if(_preview_bitmap_image)    // Is the image a pixbuf?
1145     {
1146         // Set the transformation
1147         const Matrix matrix = {
1148             scaleFactor, 0,
1149             0, scaleFactor,
1150             svgX, svgY };
1151         context->set_matrix (matrix);
1153         // Render the image
1154         set_source_pixbuf (context, _preview_bitmap_image, 0, 0);
1155         context->paint();
1157         // Reset the transformation
1158         context->set_identity_matrix();
1159     }
1161     // Draw the inner frame
1162     context->set_source_rgb(0.75, 0.75, 0.75);
1163     context->rectangle (svgX, svgY,    scaledSvgWidth, scaledSvgHeight);
1164     context->stroke();
1166     _mutex->unlock();
1168     // Finish drawing
1169     surface->finish();
1170     SelectObject(hMemDC, hOldBitmap) ;
1171     DeleteDC(hMemDC);
1173     // Refresh the preview pane
1174     InvalidateRect(_preview_wnd, NULL, FALSE);
1177 int FileOpenDialogImplWin32::format_caption(wchar_t *caption, int caption_size)
1179     wchar_t szFileName[_MAX_FNAME];
1180     _wsplitpath(_path_string, NULL, NULL, szFileName, NULL);
1182     return snwprintf(caption, caption_size,
1183         L"%s\n%d kB\n%d \xD7 %d", szFileName, _preview_file_size,
1184         (int)_preview_document_width, (int)_preview_document_height);
1187 /**
1188  * Show this dialog modally.  Return true if user hits [OK]
1189  */
1190 bool
1191 FileOpenDialogImplWin32::show()
1193     // We can only run one worker thread at a time
1194     if(_mutex != NULL) return false;
1196     if(!Glib::thread_supported())
1197         Glib::thread_init();
1199     _result = false;
1200     _finished = false;
1201     _file_selected = false;
1202     _mutex = new Glib::Mutex();
1203     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1205     if(Glib::Thread::create(sigc::mem_fun(*this, &FileOpenDialogImplWin32::GetOpenFileName_thread), true))
1206     {
1207         while(1)
1208         {
1209             g_main_context_iteration(g_main_context_default(), FALSE);
1211             if(_mutex->trylock())
1212             {
1213                 // Read mutexed data
1214                 const bool finished = _finished;
1215                 const bool is_file_selected = _file_selected;
1216                 _file_selected = false;
1217                 _mutex->unlock();
1219                 if(finished) break;
1220                 if(is_file_selected) file_selected();
1221             }
1223             Sleep(10);
1224         }
1225     }
1227     // Tidy up
1228     delete _mutex;
1229     _mutex = NULL;
1231     return _result;
1234 /**
1235  * To Get Multiple filenames selected at-once.
1236  */
1237 std::vector<Glib::ustring>FileOpenDialogImplWin32::getFilenames()
1239     std::vector<Glib::ustring> result;
1240     result.push_back(getFilename());
1241     return result;
1245 /*#########################################################################
1246 ### F I L E    S A V E
1247 #########################################################################*/
1249 /**
1250  * Constructor
1251  */
1252 FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent,
1253             const Glib::ustring &dir,
1254             FileDialogType fileTypes,
1255             const char *title,
1256             const Glib::ustring &/*default_key*/) :
1257     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as")
1259     createFilterMenu();
1262 FileSaveDialogImplWin32::~FileSaveDialogImplWin32()
1266 void FileSaveDialogImplWin32::createFilterMenu()
1268     list<Filter> filter_list;
1270     knownExtensions.clear();
1272     // Compose the filter string
1273     Glib::ustring all_inkscape_files_filter, all_image_files_filter;
1274     Inkscape::Extension::DB::OutputList extension_list;
1275     Inkscape::Extension::db.get_output_list(extension_list);
1277     int filter_count = 0;
1278     int filter_length = 0;
1280     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1281          current_item != extension_list.end(); current_item++)
1282     {
1283         Inkscape::Extension::Output *omod = *current_item;
1284         if (omod->deactivated()) continue;
1286         filter_count++;
1288         Filter filter;
1290         // Extension
1291         const gchar *filter_extension = omod->get_extension();
1292         filter.filter = g_utf8_to_utf16(
1293             filter_extension, -1, NULL, &filter.filter_length, NULL);
1294         knownExtensions.insert( Glib::ustring(filter_extension).casefold() );
1296         // Type
1297         filter.name = g_utf8_to_utf16(
1298             _(omod->get_filetypename()), -1, NULL, &filter.name_length, NULL);
1300         filter.mod = omod;
1302         filter_length += filter.name_length +
1303             filter.filter_length + 3;   // Add 3 for two \0s and a *
1305         filter_list.push_back(filter);
1306     }
1308     int extension_index = 0;
1309     _extension_map = new Inkscape::Extension::Extension*[filter_count];
1311     _filter = new wchar_t[filter_length];
1312     wchar_t *filterptr = _filter;
1314     for(list<Filter>::iterator filter_iterator = filter_list.begin();
1315         filter_iterator != filter_list.end(); filter_iterator++)
1316     {
1317         const Filter &filter = *filter_iterator;
1319         wcsncpy(filterptr, (wchar_t*)filter.name, filter.name_length);
1320         filterptr += filter.name_length;
1321         g_free(filter.name);
1323         *(filterptr++) = L'\0';
1324         *(filterptr++) = L'*';
1326         wcsncpy(filterptr, (wchar_t*)filter.filter, filter.filter_length);
1327         filterptr += filter.filter_length;
1328         g_free(filter.filter);
1330         *(filterptr++) = L'\0';
1332         // Associate this input extension with the file type name
1333         _extension_map[extension_index++] = filter.mod;
1334     }
1335     *(filterptr++) = 0;
1337         _filter_count = extension_index;
1338     _filter_index = 1;  // A value of 1 selects the 1st filter - NOT the 2nd
1341 void FileSaveDialogImplWin32::GetSaveFileName_thread()
1343     OPENFILENAMEEXW ofn;
1345     g_assert(this != NULL);
1346     g_assert(_main_loop != NULL);
1348     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
1349         _current_directory.data(), _current_directory.length(),
1350                 NULL, NULL, NULL);
1352     // Copy the selected file name, converting from UTF-8 to UTF-16
1353     memset(_path_string, 0, sizeof(_path_string));
1354     gunichar2* utf16_path_string = g_utf8_to_utf16(
1355         myFilename.data(), -1, NULL, NULL, NULL);
1356     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
1357     g_free(utf16_path_string);
1359     ZeroMemory(&ofn, sizeof(ofn));
1360     ofn.lStructSize = sizeof(ofn);
1361     ofn.hwndOwner = _ownerHwnd;
1362     ofn.lpstrFile = _path_string;
1363     ofn.nMaxFile = _MAX_PATH;
1364     ofn.nFilterIndex = _filter_index;
1365     ofn.lpstrFileTitle = NULL;
1366     ofn.nMaxFileTitle = 0;
1367     ofn.lpstrInitialDir = current_directory_string;
1368     ofn.lpstrTitle = _title;
1369     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
1370     ofn.lpstrFilter = _filter;
1371     ofn.nFilterIndex = _filter_index;
1373     _result = GetSaveFileNameW(&ofn) != 0;
1375         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
1376     _filter_index = ofn.nFilterIndex;
1377     _extension = _extension_map[ofn.nFilterIndex - 1];
1379     // Copy the selected file name, converting from UTF-16 to UTF-8
1380     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
1382     // Tidy up
1383     g_free(current_directory_string);
1385     g_main_loop_quit(_main_loop);
1388 /**
1389  * Show this dialog modally.  Return true if user hits [OK]
1390  */
1391 bool
1392 FileSaveDialogImplWin32::show()
1394     if(!Glib::thread_supported())
1395         Glib::thread_init();
1397     _result = false;
1398     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1399         
1400         if(_main_loop != NULL)
1401         {
1402             if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true))
1403                 g_main_loop_run(_main_loop);
1405             if(_result)
1406                 appendExtension(myFilename, (Inkscape::Extension::Output*)_extension);
1407         }
1408         
1409     return _result;
1412 void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension * /*key*/ )
1414     // If no pointer to extension is passed in, look up based on filename extension.
1422 #endif
1424 /*
1425   Local Variables:
1426   mode:c++
1427   c-file-style:"stroustrup"
1428   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1429   indent-tabs-mode:nil
1430   fill-column:99
1431   End:
1432 */
1433 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :