Code

patch from 226459, approved by joel
[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 "display/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];
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         all_image_files.mod = NULL;
252     filter_list.push_front(all_image_files);
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         all_inkscape_files.mod = NULL;
260     filter_list.push_front(all_inkscape_files);
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         all_files.mod = NULL;
268     filter_list.push_front(all_files);
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         from_2geom(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     gc.transform = NR::Matrix(NR::scale(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     bool successful = false;
943     _mutex->lock();
945     try {
946         _preview_bitmap_image = Gdk::Pixbuf::create_from_file(path);
947         if (_preview_bitmap_image) {
948             _preview_image_width = _preview_bitmap_image->get_width();
949             _preview_document_width = _preview_image_width;
950             _preview_image_height = _preview_bitmap_image->get_height();
951             _preview_document_height = _preview_image_height;
952             successful = true;
953         }
954     }
955     catch (const Gdk::PixbufError&) {}
956     catch (const Glib::FileError&) {}
958     _mutex->unlock();
960     return successful;
963 void FileOpenDialogImplWin32::render_preview()
965     double x, y;
966     const double blurRadius = 8;
967     const double halfBlurRadius = blurRadius / 2;
968     const int shaddowOffsetX = 0;
969     const int shaddowOffsetY = 2;
970     const int pagePadding = 5;
971     const double shaddowAlpha = 0.75;
973     // Is the preview showing?
974     if(!_show_preview)
975         return;
977     // Do we have anything to render?
978     _mutex->lock();
980     if(!_preview_bitmap_image)
981     {
982         _mutex->unlock();
983         return;
984     }
986     // Tidy up any previous bitmap renderings
987     if(_preview_bitmap != NULL)
988         DeleteObject(_preview_bitmap);
989     _preview_bitmap = NULL;
991     // Calculate the size of the caption
992     int captionHeight = 0;
994     if(_preview_wnd != NULL)
995     {
996         RECT rcCaptionRect;
997         WCHAR szCaption[_MAX_FNAME + 32];
998         const int iLength = format_caption(szCaption,
999             sizeof(szCaption) / sizeof(WCHAR));
1001         HDC dc = GetDC(_preview_wnd);
1002         DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
1003             DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_CALCRECT);
1004         ReleaseDC(_preview_wnd, dc);
1006         captionHeight = rcCaptionRect.bottom - rcCaptionRect.top;
1007     }
1009     // Find the minimum scale to fit the image inside the preview area
1010     const double scaleFactorX =
1011         ((double)_preview_width - pagePadding * 2 - blurRadius)  / _preview_document_width;
1012     const double scaleFactorY =
1013         ((double)_preview_height - pagePadding * 2
1014         - shaddowOffsetY - halfBlurRadius - captionHeight) / _preview_document_height;
1015     double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
1016     scaleFactor = (scaleFactor > 1.0) ? 1.0 : scaleFactor;
1018     // Now get the resized values
1019     const double scaledSvgWidth  = scaleFactor * _preview_document_width;
1020     const double scaledSvgHeight = scaleFactor * _preview_document_height;
1022     const int svgX = pagePadding + halfBlurRadius;
1023     const int svgY = pagePadding;
1025     const int frameX = svgX - pagePadding;
1026     const int frameY = svgY - pagePadding;
1027     const int frameWidth = scaledSvgWidth + pagePadding * 2;
1028     const int frameHeight = scaledSvgHeight + pagePadding * 2;
1030     const int totalWidth = (int)ceil(frameWidth + blurRadius);
1031     const int totalHeight = (int)ceil(frameHeight + blurRadius);
1033     // Prepare the drawing surface
1034     HDC hDC = GetDC(_preview_wnd);
1035     HDC hMemDC = CreateCompatibleDC(hDC);
1036     _preview_bitmap = CreateCompatibleBitmap(hDC, totalWidth, totalHeight);
1037     HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, _preview_bitmap);
1038     Cairo::RefPtr<Win32Surface> surface = Win32Surface::create(hMemDC);
1039     Cairo::RefPtr<Context> context = Context::create(surface);
1041     // Paint the background to match the dialog colour
1042     const COLORREF background = GetSysColor(COLOR_3DFACE);
1043     context->set_source_rgb(
1044         GetRValue(background) / 255.0,
1045         GetGValue(background) / 255.0,
1046         GetBValue(background) / 255.0);
1047     context->paint();
1049     //----- Draw the drop shaddow -----//
1051     // Left Edge
1052     x = frameX + shaddowOffsetX - halfBlurRadius;
1053     Cairo::RefPtr<LinearGradient> leftEdgeFade = LinearGradient::create(
1054         x, 0.0, x + blurRadius, 0.0);
1055     leftEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1056     leftEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1057     context->set_source(leftEdgeFade);
1058     context->rectangle (x, frameY + shaddowOffsetY + halfBlurRadius,
1059         blurRadius, frameHeight - blurRadius);
1060     context->fill();
1062     // Right Edge
1063     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1064     Cairo::RefPtr<LinearGradient> rightEdgeFade = LinearGradient::create(
1065         x, 0.0,    x + blurRadius, 0.0);
1066     rightEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1067     rightEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1068     context->set_source(rightEdgeFade);
1069     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1070         frameY + shaddowOffsetY + halfBlurRadius,
1071         blurRadius, frameHeight - blurRadius);
1072     context->fill();
1074     // Top Edge
1075     y = frameY + shaddowOffsetY - halfBlurRadius;
1076     Cairo::RefPtr<LinearGradient> topEdgeFade = LinearGradient::create(
1077         0.0, y, 0.0, y + blurRadius);
1078     topEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1079     topEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1080     context->set_source(topEdgeFade);
1081     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1082         frameWidth - blurRadius, blurRadius);
1083     context->fill();
1085     // Bottom Edge
1086     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1087     Cairo::RefPtr<LinearGradient> bottomEdgeFade = LinearGradient::create(
1088         0.0, y,    0.0, y + blurRadius);
1089     bottomEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1090     bottomEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1091     context->set_source(bottomEdgeFade);
1092     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1093         frameWidth - blurRadius, blurRadius);
1094     context->fill();
1096     // Top Left Corner
1097     x = frameX + shaddowOffsetX - halfBlurRadius;
1098     y = frameY + shaddowOffsetY - halfBlurRadius;
1099     Cairo::RefPtr<RadialGradient> topLeftCornerFade = RadialGradient::create(
1100         x + blurRadius, y + blurRadius, 0, x + blurRadius, y + blurRadius, blurRadius);
1101     topLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1102     topLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1103     context->set_source(topLeftCornerFade);
1104     context->rectangle (x, y, blurRadius, blurRadius);
1105     context->fill();
1107     // Top Right Corner
1108     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1109     y = frameY + shaddowOffsetY - halfBlurRadius;
1110     Cairo::RefPtr<RadialGradient> topRightCornerFade = RadialGradient::create(
1111         x, y + blurRadius, 0, x, y + blurRadius, blurRadius);
1112     topRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1113     topRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1114     context->set_source(topRightCornerFade);
1115     context->rectangle (x, y, blurRadius, blurRadius);
1116     context->fill();
1118     // Bottom Left Corner
1119     x = frameX + shaddowOffsetX - halfBlurRadius;
1120     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1121     Cairo::RefPtr<RadialGradient> bottomLeftCornerFade = RadialGradient::create(
1122         x + blurRadius, y, 0, x + blurRadius, y, blurRadius);
1123     bottomLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1124     bottomLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1125     context->set_source(bottomLeftCornerFade);
1126     context->rectangle (x, y, blurRadius, blurRadius);
1127     context->fill();
1129     // Bottom Right Corner
1130     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1131     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1132     Cairo::RefPtr<RadialGradient> bottomRightCornerFade = RadialGradient::create(
1133         x, y, 0, x, y, blurRadius);
1134     bottomRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1135     bottomRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1136     context->set_source(bottomRightCornerFade);
1137     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1138         frameY + frameHeight + shaddowOffsetY - halfBlurRadius,
1139         blurRadius, blurRadius);
1140     context->fill();
1142     // Draw the frame
1143     context->set_line_width(1);
1144     context->rectangle (frameX, frameY,    frameWidth, frameHeight);
1146     context->set_source_rgb(1.0, 1.0, 1.0);
1147     context->fill_preserve();
1148     context->set_source_rgb(0.25, 0.25, 0.25);
1149     context->stroke_preserve();
1151     // Draw the image
1152     if(_preview_bitmap_image)    // Is the image a pixbuf?
1153     {
1154         // Set the transformation
1155         const Matrix matrix = {
1156             scaleFactor, 0,
1157             0, scaleFactor,
1158             svgX, svgY };
1159         context->set_matrix (matrix);
1161         // Render the image
1162         set_source_pixbuf (context, _preview_bitmap_image, 0, 0);
1163         context->paint();
1165         // Reset the transformation
1166         context->set_identity_matrix();
1167     }
1169     // Draw the inner frame
1170     context->set_source_rgb(0.75, 0.75, 0.75);
1171     context->rectangle (svgX, svgY,    scaledSvgWidth, scaledSvgHeight);
1172     context->stroke();
1174     _mutex->unlock();
1176     // Finish drawing
1177     surface->finish();
1178     SelectObject(hMemDC, hOldBitmap) ;
1179     DeleteDC(hMemDC);
1181     // Refresh the preview pane
1182     InvalidateRect(_preview_wnd, NULL, FALSE);
1185 int FileOpenDialogImplWin32::format_caption(wchar_t *caption, int caption_size)
1187     wchar_t szFileName[_MAX_FNAME];
1188     _wsplitpath(_path_string, NULL, NULL, szFileName, NULL);
1190     return snwprintf(caption, caption_size,
1191         L"%s\n%d kB\n%d \xD7 %d", szFileName, _preview_file_size,
1192         (int)_preview_document_width, (int)_preview_document_height);
1195 /**
1196  * Show this dialog modally.  Return true if user hits [OK]
1197  */
1198 bool
1199 FileOpenDialogImplWin32::show()
1201     // We can only run one worker thread at a time
1202     if(_mutex != NULL) return false;
1204     if(!Glib::thread_supported())
1205         Glib::thread_init();
1207     _result = false;
1208     _finished = false;
1209     _file_selected = false;
1210     _mutex = new Glib::Mutex();
1211     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1213     if(Glib::Thread::create(sigc::mem_fun(*this, &FileOpenDialogImplWin32::GetOpenFileName_thread), true))
1214     {
1215         while(1)
1216         {
1217             g_main_context_iteration(g_main_context_default(), FALSE);
1219             if(_mutex->trylock())
1220             {
1221                 // Read mutexed data
1222                 const bool finished = _finished;
1223                 const bool is_file_selected = _file_selected;
1224                 _file_selected = false;
1225                 _mutex->unlock();
1227                 if(finished) break;
1228                 if(is_file_selected) file_selected();
1229             }
1231             Sleep(10);
1232         }
1233     }
1235     // Tidy up
1236     delete _mutex;
1237     _mutex = NULL;
1239     return _result;
1242 /**
1243  * To Get Multiple filenames selected at-once.
1244  */
1245 std::vector<Glib::ustring>FileOpenDialogImplWin32::getFilenames()
1247     std::vector<Glib::ustring> result;
1248     result.push_back(getFilename());
1249     return result;
1253 /*#########################################################################
1254 ### F I L E    S A V E
1255 #########################################################################*/
1257 /**
1258  * Constructor
1259  */
1260 FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent,
1261             const Glib::ustring &dir,
1262             FileDialogType fileTypes,
1263             const char *title,
1264             const Glib::ustring &/*default_key*/) :
1265     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as")
1267     createFilterMenu();
1270 FileSaveDialogImplWin32::~FileSaveDialogImplWin32()
1274 void FileSaveDialogImplWin32::createFilterMenu()
1276     list<Filter> filter_list;
1278     knownExtensions.clear();
1280     // Compose the filter string
1281     Glib::ustring all_inkscape_files_filter, all_image_files_filter;
1282     Inkscape::Extension::DB::OutputList extension_list;
1283     Inkscape::Extension::db.get_output_list(extension_list);
1285     int filter_count = 0;
1286     int filter_length = 1;
1288     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1289          current_item != extension_list.end(); current_item++)
1290     {
1291         Inkscape::Extension::Output *omod = *current_item;
1292         if (omod->deactivated()) continue;
1294         filter_count++;
1296         Filter filter;
1298         // Extension
1299         const gchar *filter_extension = omod->get_extension();
1300         filter.filter = g_utf8_to_utf16(
1301             filter_extension, -1, NULL, &filter.filter_length, NULL);
1302         knownExtensions.insert( Glib::ustring(filter_extension).casefold() );
1304         // Type
1305         filter.name = g_utf8_to_utf16(
1306             _(omod->get_filetypename()), -1, NULL, &filter.name_length, NULL);
1308         filter.mod = omod;
1310         filter_length += filter.name_length +
1311             filter.filter_length + 3;   // Add 3 for two \0s and a *
1313         filter_list.push_back(filter);
1314     }
1316     int extension_index = 0;
1317     _extension_map = new Inkscape::Extension::Extension*[filter_count];
1319     _filter = new wchar_t[filter_length];
1320     wchar_t *filterptr = _filter;
1322     for(list<Filter>::iterator filter_iterator = filter_list.begin();
1323         filter_iterator != filter_list.end(); filter_iterator++)
1324     {
1325         const Filter &filter = *filter_iterator;
1327         wcsncpy(filterptr, (wchar_t*)filter.name, filter.name_length);
1328         filterptr += filter.name_length;
1329         g_free(filter.name);
1331         *(filterptr++) = L'\0';
1332         *(filterptr++) = L'*';
1334         wcsncpy(filterptr, (wchar_t*)filter.filter, filter.filter_length);
1335         filterptr += filter.filter_length;
1336         g_free(filter.filter);
1338         *(filterptr++) = L'\0';
1340         // Associate this input extension with the file type name
1341         _extension_map[extension_index++] = filter.mod;
1342     }
1343     *(filterptr++) = 0;
1345         _filter_count = extension_index;
1346     _filter_index = 1;  // A value of 1 selects the 1st filter - NOT the 2nd
1349 void FileSaveDialogImplWin32::GetSaveFileName_thread()
1351     OPENFILENAMEEXW ofn;
1353     g_assert(this != NULL);
1354     g_assert(_main_loop != NULL);
1356     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
1357         _current_directory.data(), _current_directory.length(),
1358                 NULL, NULL, NULL);
1360     // Copy the selected file name, converting from UTF-8 to UTF-16
1361     memset(_path_string, 0, sizeof(_path_string));
1362     gunichar2* utf16_path_string = g_utf8_to_utf16(
1363         myFilename.data(), -1, NULL, NULL, NULL);
1364     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
1365     g_free(utf16_path_string);
1367     ZeroMemory(&ofn, sizeof(ofn));
1368     ofn.lStructSize = sizeof(ofn);
1369     ofn.hwndOwner = _ownerHwnd;
1370     ofn.lpstrFile = _path_string;
1371     ofn.nMaxFile = _MAX_PATH;
1372     ofn.nFilterIndex = _filter_index;
1373     ofn.lpstrFileTitle = NULL;
1374     ofn.nMaxFileTitle = 0;
1375     ofn.lpstrInitialDir = current_directory_string;
1376     ofn.lpstrTitle = _title;
1377     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
1378     ofn.lpstrFilter = _filter;
1379     ofn.nFilterIndex = _filter_index;
1381     _result = GetSaveFileNameW(&ofn) != 0;
1383         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
1384     _filter_index = ofn.nFilterIndex;
1385     _extension = _extension_map[ofn.nFilterIndex - 1];
1387     // Copy the selected file name, converting from UTF-16 to UTF-8
1388     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
1390     // Tidy up
1391     g_free(current_directory_string);
1393     g_main_loop_quit(_main_loop);
1396 /**
1397  * Show this dialog modally.  Return true if user hits [OK]
1398  */
1399 bool
1400 FileSaveDialogImplWin32::show()
1402     if(!Glib::thread_supported())
1403         Glib::thread_init();
1405     _result = false;
1406     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1407         
1408         if(_main_loop != NULL)
1409         {
1410             if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true))
1411                 g_main_loop_run(_main_loop);
1413             if(_result)
1414                 appendExtension(myFilename, (Inkscape::Extension::Output*)_extension);
1415         }
1416         
1417     return _result;
1420 void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension * /*key*/ )
1422     // If no pointer to extension is passed in, look up based on filename extension.
1430 #endif
1432 /*
1433   Local Variables:
1434   mode:c++
1435   c-file-style:"stroustrup"
1436   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1437   indent-tabs-mode:nil
1438   fill-column:99
1439   End:
1440 */
1441 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :