Code

fixed another localization issue - file extension names are now localized properly.
[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);
114     Glib::RefPtr<const Gdk::Window> parentWindow = parent.get_window();
115     g_assert(parentWindow->gobj() != NULL);
116     _ownerHwnd = (HWND)gdk_win32_drawable_get_handle((GdkDrawable*)parentWindow->gobj());
119 FileDialogBaseWin32::~FileDialogBaseWin32()
121     g_free(_title);
124 Inkscape::Extension::Extension *FileDialogBaseWin32::getSelectionType()
126     return _extension;
129 Glib::ustring FileDialogBaseWin32::getCurrentDirectory()
131     return _current_directory;
134 /*#########################################################################
135 ### F I L E    O P E N
136 #########################################################################*/
138 bool FileOpenDialogImplWin32::_show_preview = true;
140 /**
141  * Constructor.  Not called directly.  Use the factory.
142  */
143 FileOpenDialogImplWin32::FileOpenDialogImplWin32(Gtk::Window &parent,
144                                        const Glib::ustring &dir,
145                                        FileDialogType fileTypes,
146                                        const gchar *title) :
147     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.open")
149     // Initalize to Autodetect
150     _extension = NULL;
152     // Set our dialog type (open, import, etc...)
153     dialogType = fileTypes;
155     _show_preview_button_bitmap = NULL;
156     _preview_wnd = NULL;
157     _file_dialog_wnd = NULL;
158     _base_window_proc = NULL;
160     _preview_file_size = 0;
161     _preview_bitmap = NULL;
162     _preview_file_icon = NULL;
163     _preview_document_width = 0;
164     _preview_document_height = 0;
165     _preview_image_width = 0;
166     _preview_image_height = 0;
168     createFilterMenu();
172 /**
173  * Destructor
174  */
175 FileOpenDialogImplWin32::~FileOpenDialogImplWin32()
177     if(_filter != NULL)
178         delete[] _filter;
179     if(_extension_map != NULL)
180         delete[] _extension_map;
183 void FileOpenDialogImplWin32::createFilterMenu()
185     list<Filter> filter_list;
187     // Compose the filter string
188     Inkscape::Extension::DB::InputList extension_list;
189     Inkscape::Extension::db.get_input_list(extension_list);
191     ustring all_inkscape_files_filter, all_image_files_filter;
192     Filter all_files, all_inkscape_files, all_image_files;
194     const gchar *all_files_filter_name = N_("All Files");
195     const gchar *all_inkscape_files_filter_name = N_("All Inkscape Files");
196     const gchar *all_image_files_filter_name = N_("All Image Files");
198     // Calculate the amount of memory required
199     int filter_count = 3;       // 3 - one for All Files, All Images and All Inkscape Files
200     int filter_length = 1;
202     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
203          current_item != extension_list.end(); current_item++)
204     {
205         Filter filter;
207         Inkscape::Extension::Input *imod = *current_item;
208         if (imod->deactivated()) continue;
210         // Type
211         filter.name = g_utf8_to_utf16(_(imod->get_filetypename()),
212             -1, NULL, &filter.name_length, NULL);
214         // Extension
215         const gchar *file_extension_name = imod->get_extension();
216         filter.filter = g_utf8_to_utf16(file_extension_name,
217             -1, NULL, &filter.filter_length, NULL);
219         filter.mod = imod;
220         filter_list.push_back(filter);
222         filter_length += filter.name_length +
223             filter.filter_length + 3;   // Add 3 for two \0s and a *
225         // Add to the "All Inkscape Files" Entry
226         if(all_inkscape_files_filter.length() > 0)
227             all_inkscape_files_filter += ";*";
228         all_inkscape_files_filter += file_extension_name;
229         if( strncmp("image", imod->get_mimetype(), 5) == 0)
230         {
231             // Add to the "All Image Files" Entry
232             if(all_image_files_filter.length() > 0)
233                 all_image_files_filter += ";*";
234             all_image_files_filter += file_extension_name;
235         }
237         filter_count++;
238     }
240     int extension_index = 0;
241     _extension_map = new Inkscape::Extension::Extension*[filter_count];
243     // Filter Image Files
244     all_image_files.name = g_utf8_to_utf16(_(all_image_files_filter_name),
245         -1, NULL, &all_image_files.name_length, NULL);
246     all_image_files.filter = g_utf8_to_utf16(all_image_files_filter.data(),
247             -1, NULL, &all_image_files.filter_length, NULL);
248     filter_list.push_front(all_image_files);
249     _extension_map[extension_index++] = NULL;
251     // Filter Inkscape Files
252     all_inkscape_files.name = g_utf8_to_utf16(_(all_inkscape_files_filter_name),
253         -1, NULL, &all_inkscape_files.name_length, NULL);
254     all_inkscape_files.filter = g_utf8_to_utf16(all_inkscape_files_filter.data(),
255             -1, NULL, &all_inkscape_files.filter_length, NULL);
256     filter_list.push_front(all_inkscape_files);
257     _extension_map[extension_index++] = NULL;
259     // Filter All Files
260     all_files.name = g_utf8_to_utf16(_(all_files_filter_name),
261         -1, NULL, &all_files.name_length, NULL);
262     all_files.filter = NULL;
263     all_files.filter_length = 0;
264     filter_list.push_front(all_files);
265     _extension_map[extension_index++] = NULL;
267     filter_length += all_files.name_length + 3 +
268                     all_inkscape_files.filter_length +
269                     all_inkscape_files.name_length + 3 +
270                     all_image_files.filter_length +
271                     all_image_files.name_length + 3 + 1;
272      // Add 3 for 2*2 \0s and a *, and 1 for a trailing \0
275     _filter = new wchar_t[filter_length];
276     wchar_t *filterptr = _filter;
278     for(list<Filter>::iterator filter_iterator = filter_list.begin();
279         filter_iterator != filter_list.end(); filter_iterator++)
280     {
281         const Filter &filter = *filter_iterator;
283         memcpy(filterptr, filter.name, filter.name_length * 2);
284         filterptr += filter.name_length;
285         g_free(filter.name);
287         *(filterptr++) = L'\0';
288         *(filterptr++) = L'*';
290         if(filter.filter != NULL)
291         {
292             memcpy(filterptr, filter.filter, filter.filter_length * 2);
293             filterptr += filter.filter_length;
294             g_free(filter.filter);
295         }
297         *(filterptr++) = L'\0';
299         // Associate this input extension with the file type name
300         _extension_map[extension_index++] = filter.mod;
301     }
302     *(filterptr++) = L'\0';
304         _filter_count = extension_index;
305     _filter_index = 2;  // Select the 2nd filter in the list - NOT the 3rd
308 void FileOpenDialogImplWin32::GetOpenFileName_thread()
310     OPENFILENAMEEXW ofn;
312     g_assert(this != NULL);
313     
314     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
315         _current_directory.data(), -1, NULL, NULL, NULL);
317     memset(&ofn, 0, sizeof(ofn));
319     // Copy the selected file name, converting from UTF-8 to UTF-16
320     memset(_path_string, 0, sizeof(_path_string));
321     gunichar2* utf16_path_string = g_utf8_to_utf16(
322         myFilename.data(), -1, NULL, NULL, NULL);
323     wcsncpy(_path_string, (wchar_t*)utf16_path_string, _MAX_PATH);
324     g_free(utf16_path_string);
326     ofn.lStructSize = sizeof(ofn);
327     ofn.hwndOwner = _ownerHwnd;
328     ofn.lpstrFile = _path_string;
329     ofn.nMaxFile = _MAX_PATH;
330     ofn.lpstrFileTitle = NULL;
331     ofn.nMaxFileTitle = 0;
332     ofn.lpstrInitialDir = current_directory_string;
333     ofn.lpstrTitle = _title;
334     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_ENABLESIZING;
335     ofn.lpstrFilter = _filter;
336     ofn.nFilterIndex = _filter_index;
337     ofn.lpfnHook = GetOpenFileName_hookproc;
338     ofn.lCustData = (LPARAM)this;
340     _result = GetOpenFileNameW(&ofn) != 0;
342         g_assert(ofn.nFilterIndex >= 1 && ofn.nFilterIndex <= _filter_count);
343     _filter_index = ofn.nFilterIndex;
344     _extension = _extension_map[ofn.nFilterIndex - 1];
346     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
348     // Copy the selected file name, converting from UTF-16 to UTF-8
349     myFilename = utf16_to_ustring(_path_string, _MAX_PATH);
351     // Tidy up
352     g_free(current_directory_string);
354     _mutex->lock();
355     _finished = true;
356     _mutex->unlock();
359 void FileOpenDialogImplWin32::register_preview_wnd_class()
361     HINSTANCE hInstance = GetModuleHandle(NULL);
362     const WNDCLASSA PreviewWndClass =
363     {
364         CS_HREDRAW | CS_VREDRAW,
365         preview_wnd_proc,
366         0,
367         0,
368         hInstance,
369         NULL,
370         LoadCursor(hInstance, IDC_ARROW),
371         (HBRUSH)(COLOR_BTNFACE + 1),
372         NULL,
373         PreviewWindowClassName
374     };
376     RegisterClassA(&PreviewWndClass);
379 UINT_PTR CALLBACK FileOpenDialogImplWin32::GetOpenFileName_hookproc(
380     HWND hdlg, UINT uiMsg, WPARAM, LPARAM lParam)
382     FileOpenDialogImplWin32 *pImpl = (FileOpenDialogImplWin32*)
383         GetWindowLongPtr(hdlg, GWLP_USERDATA);
385     switch(uiMsg)
386     {
387     case WM_INITDIALOG:
388         {
389             HWND hParentWnd = GetParent(hdlg);
390             HINSTANCE hInstance = GetModuleHandle(NULL);
392             // Make the window a bit wider
393             RECT rcRect;
394             GetWindowRect(hParentWnd, &rcRect);
395             MoveWindow(hParentWnd, rcRect.left, rcRect.top,
396                 rcRect.right - rcRect.left + PreviewWidening,
397                 rcRect.bottom - rcRect.top,
398                 FALSE);
400             // Set the pointer to the object
401             OPENFILENAMEW *ofn = (OPENFILENAMEW*)lParam;
402             SetWindowLongPtr(hdlg, GWLP_USERDATA, ofn->lCustData);
403             SetWindowLongPtr(hParentWnd, GWLP_USERDATA, ofn->lCustData);
404             pImpl = (FileOpenDialogImplWin32*)ofn->lCustData;
406             // Subclass the parent
407             pImpl->_base_window_proc = (WNDPROC)GetWindowLongPtr(hParentWnd, GWL_WNDPROC);
408             SetWindowLongPtr(hParentWnd, GWL_WNDPROC, (LONG_PTR)file_dialog_subclass_proc);
410             // Add a button to the toolbar
411             pImpl->_toolbar_wnd = FindWindowEx(hParentWnd, NULL, "ToolbarWindow32", NULL);
413             pImpl->_show_preview_button_bitmap = LoadBitmap(
414                 hInstance, MAKEINTRESOURCE(IDC_SHOW_PREVIEW));
415             TBADDBITMAP tbAddBitmap = {NULL, (UINT)pImpl->_show_preview_button_bitmap};
416             const int iBitmapIndex = SendMessage(pImpl->_toolbar_wnd,
417                 TB_ADDBITMAP, 1, (LPARAM)&tbAddBitmap);
419             TBBUTTON tbButton;
420             memset(&tbButton, 0, sizeof(TBBUTTON));
421             tbButton.iBitmap = iBitmapIndex;
422             tbButton.idCommand = IDC_SHOW_PREVIEW;
423             tbButton.fsState = (pImpl->_show_preview ? TBSTATE_CHECKED : 0)
424                 | TBSTATE_ENABLED;
425             tbButton.fsStyle = TBSTYLE_CHECK;
426             tbButton.iString = (INT_PTR)_("Show Preview");
427             SendMessage(pImpl->_toolbar_wnd, TB_ADDBUTTONS, 1, (LPARAM)&tbButton);
429             // Create preview pane
430             register_preview_wnd_class();
432             pImpl->_mutex->lock();
434                 pImpl->_file_dialog_wnd = hParentWnd;
436                 pImpl->_preview_wnd =
437                     CreateWindow(PreviewWindowClassName, "",
438                         WS_CHILD | WS_VISIBLE,
439                         0, 0, 100, 100, hParentWnd, NULL, hInstance, NULL);
440                 SetWindowLongPtr(pImpl->_preview_wnd, GWLP_USERDATA, ofn->lCustData);
442             pImpl->_mutex->unlock();
444             pImpl->layout_dialog();
445         }
446         break;
448     case WM_NOTIFY:
449         {
451         OFNOTIFY *pOFNotify = reinterpret_cast<OFNOTIFY*>(lParam);
452         switch(pOFNotify->hdr.code)
453         {
454         case CDN_SELCHANGE:
455             {
456                 if(pImpl != NULL)
457                 {
458                     // Get the file name
459                     pImpl->_mutex->lock();
461                     SendMessage(pOFNotify->hdr.hwndFrom, CDM_GETFILEPATH,
462                         sizeof(pImpl->_path_string) / sizeof(wchar_t),
463                         (LPARAM)pImpl->_path_string);
465                     pImpl->_file_selected = true;
467                     pImpl->_mutex->unlock();
469                     //pImpl->file_selected();
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             //FillRect(dc, &client_rect, (HBRUSH)(COLOR_HOTLIGHT+1));
562             if(pImpl->_path_string[0] == 0)
563             {
564                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
565                 DrawText(dc, _("No file selected"), -1, &rcClient,
566                     DT_CENTER | DT_VCENTER | DT_NOPREFIX);
567             }
568             else if(pImpl->_preview_bitmap != NULL)
569             {
570                 BITMAP bitmap;
571                 GetObject(pImpl->_preview_bitmap, sizeof(bitmap), &bitmap);
572                 const int destX = (rcClient.right - bitmap.bmWidth) / 2;
574                 // Render the image
575                 HDC hSrcDC = CreateCompatibleDC(dc);
576                 HBITMAP hOldBitmap = (HBITMAP)SelectObject(hSrcDC, pImpl->_preview_bitmap);
578                 BitBlt(dc, destX, 0, bitmap.bmWidth, bitmap.bmHeight,
579                     hSrcDC, 0, 0, SRCCOPY);
581                 SelectObject(hSrcDC, hOldBitmap);
582                 DeleteDC(hSrcDC);
584                 // Fill in the background area
585                 HRGN hEraseRgn = CreateRectRgn(rcClient.left, rcClient.top,
586                     rcClient.right, rcClient.bottom);
587                 HRGN hImageRgn = CreateRectRgn(destX, 0,
588                     destX + bitmap.bmWidth, bitmap.bmHeight);
589                 CombineRgn(hEraseRgn, hEraseRgn, hImageRgn, RGN_DIFF);
591                 FillRgn(dc, hEraseRgn, GetSysColorBrush(COLOR_3DFACE));
593                 DeleteObject(hImageRgn);
594                 DeleteObject(hEraseRgn);
596                 // Draw the caption on
597                 RECT rcCaptionRect = {rcClient.left,
598                     rcClient.top + bitmap.bmHeight + CaptionPadding,
599                     rcClient.right, rcClient.bottom};
601                 WCHAR szCaption[_MAX_FNAME + 32];
602                 const int iLength = pImpl->format_caption(
603                     szCaption, sizeof(szCaption) / sizeof(WCHAR));
605                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
606                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
607             }
608             else if(pImpl->_preview_file_icon != NULL)
609             {
610                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
612                 // Draw the files icon
613                 const int destX = (rcClient.right - IconSize) / 2;
614                 DrawIconEx(dc, destX, 0, pImpl->_preview_file_icon,
615                     IconSize, IconSize, 0, NULL,
616                     DI_NORMAL | DI_COMPAT);
618                 // Draw the caption on
619                 RECT rcCaptionRect = {rcClient.left,
620                     rcClient.top + IconSize + CaptionPadding,
621                     rcClient.right, rcClient.bottom};
623                 WCHAR szFileName[_MAX_FNAME], szCaption[_MAX_FNAME + 32];
624                 _wsplitpath(pImpl->_path_string, NULL, NULL, szFileName, NULL);
626                 const int iLength = snwprintf(szCaption,
627                     sizeof(szCaption), L"%s\n%d kB",
628                     szFileName, pImpl->_preview_file_size);
630                 DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
631                     DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS);
632             }
633             else
634             {
635                 // Can't show anything!
636                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
637             }
639             pImpl->_mutex->unlock();
641             // Finish painting
642             SelectObject(dc, hOldFont);
643             EndPaint(hwnd, &paint_struct);
644         }
646         break;
648     case WM_DESTROY:
649         pImpl->free_preview();
650         break;
652     default:
653         lResult = DefWindowProc(hwnd, uMsg, wParam, lParam);
654         break;
655     }
657     return lResult;
660 void FileOpenDialogImplWin32::enable_preview(bool enable)
662     _show_preview = enable;
664     // Relayout the dialog
665     ShowWindow(_preview_wnd, enable ? SW_SHOW : SW_HIDE);
666     layout_dialog();
668     // Load or unload the preview
669     if(enable)
670     {
671         _mutex->lock();
672         _file_selected = true;
673         _mutex->unlock();
674     }
675     else free_preview();
678 void FileOpenDialogImplWin32::layout_dialog()
680     union RECTPOINTS
681     {
682         RECT r;
683         POINT p[2];
684     };
686     const float MaxExtentScale = 2.0f / 3.0f;
688     RECT rcClient;
689     GetClientRect(_file_dialog_wnd, &rcClient);
691     // Re-layout the dialog
692     HWND hFileListWnd = GetDlgItem(_file_dialog_wnd, lst2);
693     HWND hFolderComboWnd = GetDlgItem(_file_dialog_wnd, cmb2);
696     RECT rcFolderComboRect;
697     RECTPOINTS rcFileList;
698     GetWindowRect(hFileListWnd, &rcFileList.r);
699     GetWindowRect(hFolderComboWnd, &rcFolderComboRect);
700     const int iPadding = rcFileList.r.top - rcFolderComboRect.bottom;
701     MapWindowPoints(NULL, _file_dialog_wnd, rcFileList.p, 2);
703     RECT rcPreview;
704     RECT rcBody = {rcFileList.r.left, rcFileList.r.top,
705         rcClient.right - iPadding, rcFileList.r.bottom};
706     rcFileList.r.right = rcBody.right;
708     if(_show_preview)
709     {
710         rcPreview.top = rcBody.top;
711         rcPreview.left = rcClient.right - (rcBody.bottom - rcBody.top);
712         const int iMaxExtent = (int)(MaxExtentScale * (float)(rcBody.left + rcBody.right)) + iPadding / 2;
713         if(rcPreview.left < iMaxExtent) rcPreview.left = iMaxExtent;
714         rcPreview.bottom = rcBody.bottom;
715         rcPreview.right = rcBody.right;
717         // Re-layout the preview box
718         _mutex->lock();
720             _preview_width = rcPreview.right - rcPreview.left;
721             _preview_height = rcPreview.bottom - rcPreview.top;
723         _mutex->unlock();
725         render_preview();
727         MoveWindow(_preview_wnd, rcPreview.left, rcPreview.top,
728             _preview_width, _preview_height, TRUE);
730         rcFileList.r.right = rcPreview.left - iPadding;
731     }
733     // Re-layout the file list box
734     MoveWindow(hFileListWnd, rcFileList.r.left, rcFileList.r.top,
735         rcFileList.r.right - rcFileList.r.left,
736         rcFileList.r.bottom - rcFileList.r.top, TRUE);
738     // Re-layout the toolbar
739     RECTPOINTS rcToolBar;
740     GetWindowRect(_toolbar_wnd, &rcToolBar.r);
741     MapWindowPoints(NULL, _file_dialog_wnd, rcToolBar.p, 2);
742     MoveWindow(_toolbar_wnd, rcToolBar.r.left, rcToolBar.r.top,
743         rcToolBar.r.right - rcToolBar.r.left, rcToolBar.r.bottom - rcToolBar.r.top, TRUE);
746 void FileOpenDialogImplWin32::file_selected()
748     // Destroy any previous previews
749     free_preview();
752     // Determine if the file exists
753     DWORD attributes = GetFileAttributesW(_path_string);
754     if(attributes == 0xFFFFFFFF ||
755         attributes == FILE_ATTRIBUTE_DIRECTORY)
756     {
757         InvalidateRect(_preview_wnd, NULL, FALSE);
758         return;
759     }
761     // Check the file exists and get the file size
762     HANDLE file_handle = CreateFileW(_path_string, GENERIC_READ,
763         FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
764     if(file_handle == INVALID_HANDLE_VALUE) return;
765     const DWORD file_size = GetFileSize(file_handle, NULL);
766     if (file_size == INVALID_FILE_SIZE) return;
767     _preview_file_size = file_size / 1024;
768     CloseHandle(file_handle);
770     if(_show_preview) load_preview();
773 void FileOpenDialogImplWin32::load_preview()
775     // Destroy any previous previews
776     free_preview();
778     // Try to get the file icon
779     SHFILEINFOW fileInfo;
780     if(SUCCEEDED(SHGetFileInfoW(_path_string, 0, &fileInfo,
781         sizeof(fileInfo), SHGFI_ICON | SHGFI_LARGEICON)))
782         _preview_file_icon = fileInfo.hIcon;
784     // Will this file be too big?
785     if(_preview_file_size > MaxPreviewFileSize)
786     {
787         InvalidateRect(_preview_wnd, NULL, FALSE);
788         return;
789     }
791     // Prepare to render a preview
792     const Glib::ustring svg = ".svg";
793     const Glib::ustring svgz = ".svgz";
794     const Glib::ustring path = utf16_to_ustring(_path_string);
796     bool success = false;
798     _preview_document_width = _preview_document_height = 0;
800     if ((dialogType == SVG_TYPES || dialogType == IMPORT_TYPES) &&
801             (hasSuffix(path, svg) || hasSuffix(path, svgz)))
802         success = set_svg_preview();
803     else if (isValidImageFile(path))
804         success = set_image_preview();
805     else {
806         // Show no preview
807     }
809     if(success) render_preview();
811     InvalidateRect(_preview_wnd, NULL, FALSE);
814 void FileOpenDialogImplWin32::free_preview()
816     _mutex->lock();
817     if(_preview_bitmap != NULL)
818         DeleteObject(_preview_bitmap);
819     _preview_bitmap = NULL;
821     if(_preview_file_icon != NULL)
822         DestroyIcon(_preview_file_icon);
823     _preview_file_icon = NULL;
825     _preview_bitmap_image.clear();
826     _mutex->unlock();
829 bool FileOpenDialogImplWin32::set_svg_preview()
831     const int PreviewSize = 512;
833     gchar *utf8string = g_utf16_to_utf8((const gunichar2*)_path_string,
834         _MAX_PATH, NULL, NULL, NULL);
835     SPDocument *svgDoc = sp_document_new (utf8string, true);
836     g_free(utf8string);
838     // Check the document loaded properly
839     if(svgDoc == NULL) return false;
840     if(svgDoc->root == NULL)
841     {
842         sp_document_unref(svgDoc);
843         return false;
844     }
846     // Get the size of the document
847     const double svgWidth = sp_document_width(svgDoc);
848     const double svgHeight = sp_document_height(svgDoc);
850     // Find the minimum scale to fit the image inside the preview area
851     const double scaleFactorX =    PreviewSize / svgWidth;
852     const double scaleFactorY =    PreviewSize / svgHeight;
853     const double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
855     // Now get the resized values
856     const double scaledSvgWidth  = scaleFactor * svgWidth;
857     const double scaledSvgHeight = scaleFactor * svgHeight;
859     NR::Rect area(NR::Point(0, 0), NR::Point(scaledSvgWidth, scaledSvgHeight));
860     NRRectL areaL = {0, 0, scaledSvgWidth, scaledSvgHeight};
861     NRRectL bbox = {0, 0, scaledSvgWidth, scaledSvgHeight};
863     // write object bbox to area
864     NR::Maybe<NR::Rect> maybeArea(area);
865     sp_document_ensure_up_to_date (svgDoc);
866     sp_item_invoke_bbox((SPItem *) svgDoc->root, &maybeArea,
867         sp_item_i2r_affine((SPItem *)(svgDoc->root)), TRUE);
869     NRArena *const arena = NRArena::create();
871     unsigned const key = sp_item_display_key_new(1);
873     NRArenaItem *root = sp_item_invoke_show((SPItem*)(svgDoc->root),
874         arena, key, SP_ITEM_SHOW_DISPLAY);
876     NRGC gc(NULL);
877     nr_matrix_set_scale(&gc.transform, scaleFactor, scaleFactor);
879     nr_arena_item_invoke_update (root, NULL, &gc,
880         NR_ARENA_ITEM_STATE_ALL, NR_ARENA_ITEM_STATE_NONE);
882     // Prepare a GDI compatible NRPixBlock
883     NRPixBlock pixBlock;
884     pixBlock.size = NR_PIXBLOCK_SIZE_BIG;
885     pixBlock.mode = NR_PIXBLOCK_MODE_R8G8B8;
886     pixBlock.empty = 1;
887     pixBlock.visible_area.x0 = pixBlock.area.x0 = 0;
888     pixBlock.visible_area.y0 = pixBlock.area.y0 = 0;
889     pixBlock.visible_area.x1 = pixBlock.area.x1 = scaledSvgWidth;
890     pixBlock.visible_area.y1 = pixBlock.area.y1 = scaledSvgHeight;
891     pixBlock.rs = 4 * ((3 * (int)scaledSvgWidth + 3) / 4);
892     pixBlock.data.px = g_try_new (unsigned char, pixBlock.rs * scaledSvgHeight);
894     // Fail if the pixblock failed to allocate
895     if(pixBlock.data.px == NULL)
896     {
897         sp_document_unref(svgDoc);
898         return false;
899     }
901     memset(pixBlock.data.px, 0xFF, pixBlock.rs * scaledSvgHeight);
903     memcpy(&root->bbox, &areaL, sizeof(areaL));
905     // Render the image
906     nr_arena_item_invoke_render(NULL, root, &bbox, &pixBlock, /*0*/NR_ARENA_ITEM_RENDER_NO_CACHE);
908     // Tidy up
909     sp_document_unref(svgDoc);
910     sp_item_invoke_hide((SPItem*)(svgDoc->root), key);
911     nr_arena_item_unref(root);
912     nr_object_unref((NRObject *) arena);
914     // Create the GDK pixbuf
915     _mutex->lock();
917     _preview_bitmap_image = Gdk::Pixbuf::create_from_data(
918         pixBlock.data.px, Gdk::COLORSPACE_RGB, false, 8,
919         (int)scaledSvgWidth, (int)scaledSvgHeight, pixBlock.rs,
920         sigc::ptr_fun(destroy_svg_rendering));
922     _preview_document_width = scaledSvgWidth;
923     _preview_document_height = scaledSvgHeight;
924     _preview_image_width = svgWidth;
925     _preview_image_height = svgHeight;
927     _mutex->unlock();
929     return true;
932 void FileOpenDialogImplWin32::destroy_svg_rendering(const guint8 *buffer)
934     g_assert(buffer != NULL);
935     g_free((void*)buffer);
938 bool FileOpenDialogImplWin32::set_image_preview()
940     const Glib::ustring path = utf16_to_ustring(_path_string, _MAX_PATH);
942     _mutex->lock();
943     _preview_bitmap_image = Gdk::Pixbuf::create_from_file(path);
944     if(!_preview_bitmap_image) return false;
946     _preview_image_width = _preview_bitmap_image->get_width();
947     _preview_document_width = _preview_image_width;
948     _preview_image_height = _preview_bitmap_image->get_height();
949     _preview_document_height = _preview_image_height;
951     _mutex->unlock();
953     return true;
956 void FileOpenDialogImplWin32::render_preview()
958     double x, y;
959     const double blurRadius = 8;
960     const double halfBlurRadius = blurRadius / 2;
961     const int shaddowOffsetX = 0;
962     const int shaddowOffsetY = 2;
963     const int pagePadding = 5;
964     const double shaddowAlpha = 0.75;
966     // Is the preview showing?
967     if(!_show_preview)
968         return;
970     // Do we have anything to render?
971     _mutex->lock();
973     if(!_preview_bitmap_image)
974     {
975         _mutex->unlock();
976         return;
977     }
979     // Tidy up any previous bitmap renderings
980     if(_preview_bitmap != NULL)
981         DeleteObject(_preview_bitmap);
982     _preview_bitmap = NULL;
984     // Calculate the size of the caption
985     int captionHeight = 0;
987     if(_preview_wnd != NULL)
988     {
989         RECT rcCaptionRect;
990         WCHAR szCaption[_MAX_FNAME + 32];
991         const int iLength = format_caption(szCaption,
992             sizeof(szCaption) / sizeof(WCHAR));
994         HDC dc = GetDC(_preview_wnd);
995         DrawTextW(dc, szCaption, iLength, &rcCaptionRect,
996             DT_CENTER | DT_TOP | DT_NOPREFIX | DT_PATH_ELLIPSIS | DT_CALCRECT);
997         ReleaseDC(_preview_wnd, dc);
999         captionHeight = rcCaptionRect.bottom - rcCaptionRect.top;
1000     }
1002     // Find the minimum scale to fit the image inside the preview area
1003     const double scaleFactorX =
1004         ((double)_preview_width - pagePadding * 2 - blurRadius)  / _preview_document_width;
1005     const double scaleFactorY =
1006         ((double)_preview_height - pagePadding * 2
1007         - shaddowOffsetY - halfBlurRadius - captionHeight) / _preview_document_height;
1008     double scaleFactor = (scaleFactorX > scaleFactorY) ? scaleFactorY : scaleFactorX;
1009     scaleFactor = (scaleFactor > 1.0) ? 1.0 : scaleFactor;
1011     // Now get the resized values
1012     const double scaledSvgWidth  = scaleFactor * _preview_document_width;
1013     const double scaledSvgHeight = scaleFactor * _preview_document_height;
1015     const int svgX = pagePadding + halfBlurRadius;
1016     const int svgY = pagePadding;
1018     const int frameX = svgX - pagePadding;
1019     const int frameY = svgY - pagePadding;
1020     const int frameWidth = scaledSvgWidth + pagePadding * 2;
1021     const int frameHeight = scaledSvgHeight + pagePadding * 2;
1023     const int totalWidth = (int)ceil(frameWidth + blurRadius);
1024     const int totalHeight = (int)ceil(frameHeight + blurRadius);
1026     // Prepare the drawing surface
1027     HDC hDC = GetDC(_preview_wnd);
1028     HDC hMemDC = CreateCompatibleDC(hDC);
1029     _preview_bitmap = CreateCompatibleBitmap(hDC, totalWidth, totalHeight);
1030     HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, _preview_bitmap);
1031     Cairo::RefPtr<Win32Surface> surface = Win32Surface::create(hMemDC);
1032     Cairo::RefPtr<Context> context = Context::create(surface);
1034     // Paint the background to match the dialog colour
1035     const COLORREF background = GetSysColor(COLOR_3DFACE);
1036     context->set_source_rgb(
1037         GetRValue(background) / 255.0,
1038         GetGValue(background) / 255.0,
1039         GetBValue(background) / 255.0);
1040     context->paint();
1042     //----- Draw the drop shaddow -----//
1044     // Left Edge
1045     x = frameX + shaddowOffsetX - halfBlurRadius;
1046     Cairo::RefPtr<LinearGradient> leftEdgeFade = LinearGradient::create(
1047         x, 0.0, x + blurRadius, 0.0);
1048     leftEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1049     leftEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1050     context->set_source(leftEdgeFade);
1051     context->rectangle (x, frameY + shaddowOffsetY + halfBlurRadius,
1052         blurRadius, frameHeight - blurRadius);
1053     context->fill();
1055     // Right Edge
1056     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1057     Cairo::RefPtr<LinearGradient> rightEdgeFade = LinearGradient::create(
1058         x, 0.0,    x + blurRadius, 0.0);
1059     rightEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1060     rightEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1061     context->set_source(rightEdgeFade);
1062     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1063         frameY + shaddowOffsetY + halfBlurRadius,
1064         blurRadius, frameHeight - blurRadius);
1065     context->fill();
1067     // Top Edge
1068     y = frameY + shaddowOffsetY - halfBlurRadius;
1069     Cairo::RefPtr<LinearGradient> topEdgeFade = LinearGradient::create(
1070         0.0, y, 0.0, y + blurRadius);
1071     topEdgeFade->add_color_stop_rgba (0, 0, 0, 0, 0);
1072     topEdgeFade->add_color_stop_rgba (1, 0, 0, 0, shaddowAlpha);
1073     context->set_source(topEdgeFade);
1074     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1075         frameWidth - blurRadius, blurRadius);
1076     context->fill();
1078     // Bottom Edge
1079     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1080     Cairo::RefPtr<LinearGradient> bottomEdgeFade = LinearGradient::create(
1081         0.0, y,    0.0, y + blurRadius);
1082     bottomEdgeFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1083     bottomEdgeFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1084     context->set_source(bottomEdgeFade);
1085     context->rectangle (frameX + shaddowOffsetX + halfBlurRadius, y,
1086         frameWidth - blurRadius, blurRadius);
1087     context->fill();
1089     // Top Left Corner
1090     x = frameX + shaddowOffsetX - halfBlurRadius;
1091     y = frameY + shaddowOffsetY - halfBlurRadius;
1092     Cairo::RefPtr<RadialGradient> topLeftCornerFade = RadialGradient::create(
1093         x + blurRadius, y + blurRadius, 0, x + blurRadius, y + blurRadius, blurRadius);
1094     topLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1095     topLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1096     context->set_source(topLeftCornerFade);
1097     context->rectangle (x, y, blurRadius, blurRadius);
1098     context->fill();
1100     // Top Right Corner
1101     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1102     y = frameY + shaddowOffsetY - halfBlurRadius;
1103     Cairo::RefPtr<RadialGradient> topRightCornerFade = RadialGradient::create(
1104         x, y + blurRadius, 0, x, y + blurRadius, blurRadius);
1105     topRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1106     topRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1107     context->set_source(topRightCornerFade);
1108     context->rectangle (x, y, blurRadius, blurRadius);
1109     context->fill();
1111     // Bottom Left Corner
1112     x = frameX + shaddowOffsetX - halfBlurRadius;
1113     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1114     Cairo::RefPtr<RadialGradient> bottomLeftCornerFade = RadialGradient::create(
1115         x + blurRadius, y, 0, x + blurRadius, y, blurRadius);
1116     bottomLeftCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1117     bottomLeftCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1118     context->set_source(bottomLeftCornerFade);
1119     context->rectangle (x, y, blurRadius, blurRadius);
1120     context->fill();
1122     // Bottom Right Corner
1123     x = frameX + frameWidth + shaddowOffsetX - halfBlurRadius;
1124     y = frameY + frameHeight + shaddowOffsetY - halfBlurRadius;
1125     Cairo::RefPtr<RadialGradient> bottomRightCornerFade = RadialGradient::create(
1126         x, y, 0, x, y, blurRadius);
1127     bottomRightCornerFade->add_color_stop_rgba (0, 0, 0, 0, shaddowAlpha);
1128     bottomRightCornerFade->add_color_stop_rgba (1, 0, 0, 0, 0);
1129     context->set_source(bottomRightCornerFade);
1130     context->rectangle (frameX + frameWidth + shaddowOffsetX - halfBlurRadius,
1131         frameY + frameHeight + shaddowOffsetY - halfBlurRadius,
1132         blurRadius, blurRadius);
1133     context->fill();
1135     // Draw the frame
1136     context->set_line_width(1);
1137     context->rectangle (frameX, frameY,    frameWidth, frameHeight);
1139     context->set_source_rgb(1.0, 1.0, 1.0);
1140     context->fill_preserve();
1141     context->set_source_rgb(0.25, 0.25, 0.25);
1142     context->stroke_preserve();
1144     // Draw the image
1145     if(_preview_bitmap_image)    // Is the image a pixbuf?
1146     {
1147         // Set the transformation
1148         const Matrix matrix = {
1149             scaleFactor, 0,
1150             0, scaleFactor,
1151             svgX, svgY };
1152         context->set_matrix (matrix);
1154         // Render the image
1155         set_source_pixbuf (context, _preview_bitmap_image, 0, 0);
1156         context->paint();
1158         // Reset the transformation
1159         context->set_identity_matrix();
1160     }
1162     // Draw the inner frame
1163     context->set_source_rgb(0.75, 0.75, 0.75);
1164     context->rectangle (svgX, svgY,    scaledSvgWidth, scaledSvgHeight);
1165     context->stroke();
1167     _mutex->unlock();
1169     // Finish drawing
1170     surface->finish();
1171     SelectObject(hMemDC, hOldBitmap) ;
1172     DeleteDC(hMemDC);
1174     // Refresh the preview pane
1175     InvalidateRect(_preview_wnd, NULL, FALSE);
1178 int FileOpenDialogImplWin32::format_caption(wchar_t *caption, int caption_size)
1180     wchar_t szFileName[_MAX_FNAME];
1181     _wsplitpath(_path_string, NULL, NULL, szFileName, NULL);
1183     return snwprintf(caption, caption_size,
1184         L"%s\n%d kB\n%d \xD7 %d", szFileName, _preview_file_size,
1185         (int)_preview_document_width, (int)_preview_document_height);
1188 /**
1189  * Show this dialog modally.  Return true if user hits [OK]
1190  */
1191 bool
1192 FileOpenDialogImplWin32::show()
1194     // We can only run one worker thread at a time
1195     if(_mutex != NULL) return false;
1197     if(!Glib::thread_supported())
1198         Glib::thread_init();
1200     _result = false;
1201     _finished = false;
1202     _file_selected = false;
1203     _mutex = new Glib::Mutex();
1204     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
1206     if(Glib::Thread::create(sigc::mem_fun(*this, &FileOpenDialogImplWin32::GetOpenFileName_thread), true))
1207     {
1208         while(1)
1209         {
1210             g_main_context_iteration(g_main_context_default(), FALSE);
1212             if(_mutex->trylock())
1213             {
1214                 // Read mutexed data
1215                 const bool finished = _finished;
1216                 const bool is_file_selected = _file_selected;
1217                 _file_selected = false;
1218                 _mutex->unlock();
1220                 if(finished) break;
1221                 if(is_file_selected) file_selected();
1222             }
1224             Sleep(10);
1225         }
1226     }
1228     // Tidy up
1229     delete _mutex;
1230     _mutex = NULL;
1232     return _result;
1235 /**
1236  * To Get Multiple filenames selected at-once.
1237  */
1238 std::vector<Glib::ustring>FileOpenDialogImplWin32::getFilenames()
1240     std::vector<Glib::ustring> result;
1241     result.push_back(getFilename());
1242     return result;
1246 /*#########################################################################
1247 ### F I L E    S A V E
1248 #########################################################################*/
1250 /**
1251  * Constructor
1252  */
1253 FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent,
1254             const Glib::ustring &dir,
1255             FileDialogType fileTypes,
1256             const char *title,
1257             const Glib::ustring &/*default_key*/) :
1258     FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as")
1260     createFilterMenu();
1263 FileSaveDialogImplWin32::~FileSaveDialogImplWin32()
1267 void FileSaveDialogImplWin32::createFilterMenu()
1269     list<Filter> filter_list;
1271     knownExtensions.clear();
1273     // Compose the filter string
1274     Glib::ustring all_inkscape_files_filter, all_image_files_filter;
1275     Inkscape::Extension::DB::OutputList extension_list;
1276     Inkscape::Extension::db.get_output_list(extension_list);
1278     int filter_count = 0;
1279     int filter_length = 0;
1281     for (Inkscape::Extension::DB::OutputList::iterator current_item = extension_list.begin();
1282          current_item != extension_list.end(); current_item++)
1283     {
1284         Inkscape::Extension::Output *omod = *current_item;
1285         if (omod->deactivated()) continue;
1287         filter_count++;
1289         Filter filter;
1291         // Extension
1292         const gchar *filter_extension = omod->get_extension();
1293         filter.filter = g_utf8_to_utf16(
1294             filter_extension, -1, NULL, &filter.filter_length, NULL);
1295         knownExtensions.insert( Glib::ustring(filter_extension).casefold() );
1297         // Type
1298         filter.name = g_utf8_to_utf16(
1299             _(omod->get_filetypename()), -1, NULL, &filter.name_length, NULL);
1301         filter.mod = omod;
1303         filter_length += filter.name_length +
1304             filter.filter_length + 3;   // Add 3 for two \0s and a *
1306         filter_list.push_back(filter);
1307     }
1309     int extension_index = 0;
1310     _extension_map = new Inkscape::Extension::Extension*[filter_count];
1312     _filter = new wchar_t[filter_length];
1313     wchar_t *filterptr = _filter;
1315     for(list<Filter>::iterator filter_iterator = filter_list.begin();
1316         filter_iterator != filter_list.end(); filter_iterator++)
1317     {
1318         const Filter &filter = *filter_iterator;
1320         memcpy(filterptr, filter.name, filter.name_length * 2);
1321         filterptr += filter.name_length;
1322         g_free(filter.name);
1324         *(filterptr++) = L'\0';
1325         *(filterptr++) = L'*';
1327         memcpy(filterptr, filter.filter, filter.filter_length * 2);
1328         filterptr += filter.filter_length;
1329         g_free(filter.filter);
1331         *(filterptr++) = L'\0';
1333         // Associate this input extension with the file type name
1334         _extension_map[extension_index++] = filter.mod;
1335     }
1336     *(filterptr++) = 0;
1338         _filter_count = extension_index;
1339     _filter_index = 1;  // A value of 1 selects the 1st filter - NOT the 2nd
1342 void FileSaveDialogImplWin32::GetSaveFileName_thread()
1344     OPENFILENAMEEXW ofn;
1346     g_assert(this != NULL);
1347     g_assert(_main_loop != NULL);
1349     gunichar2* current_directory_string = g_utf8_to_utf16(
1350         _current_directory.data(), -1, 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 = (wchar_t*)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);
1400     if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true))
1401         g_main_loop_run(_main_loop);
1403     if(_result)
1404         appendExtension(myFilename, (Inkscape::Extension::Output*)_extension);
1406     return _result;
1409 void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension * /*key*/ )
1411     // If no pointer to extension is passed in, look up based on filename extension.
1419 #endif
1421 /*
1422   Local Variables:
1423   mode:c++
1424   c-file-style:"stroustrup"
1425   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1426   indent-tabs-mode:nil
1427   fill-column:99
1428   End:
1429 */
1430 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :