Code

Win32 post-GSoC fixups.
[inkscape.git] / src / ui / dialog / filedialogimpl-win32.cpp
index 4f9c33ff5946c642a33517184a65eaca0c26c36d..ca3a995616d6cdba73480d79e0f16899569fc76b 100644 (file)
@@ -1,21 +1,21 @@
-/**
- * Implementation of the file dialog interfaces defined in filedialog.h for Win32
- *
- * Authors:
+/** @file
+ * @brief Implementation of native file dialogs for Win32
+ */
+/* Authors:
  *   Joel Holdsworth
  *   The Inkscape Organization
+ *   Abhishek Sharma
  *
  * Copyright (C) 2004-2008 The Inkscape Organization
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
+#ifdef WIN32
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
 
-#ifdef WIN32
-
 //General includes
 #include <list>
 #include <unistd.h>
@@ -29,7 +29,6 @@
 
 //Inkscape includes
 #include "inkscape.h"
-#include "prefs-utils.h"
 #include <dialogs/dialog-events.h>
 #include <extension/input.h>
 #include <extension/output.h>
@@ -61,9 +60,13 @@ namespace UI
 namespace Dialog
 {
 
-const int PreviewWidening = 150;
+const int PREVIEW_WIDENING = 150;
+const int WINDOW_WIDTH_MINIMUM = 32;
+const int WINDOW_WIDTH_FALLBACK = 450;
+const int WINDOW_HEIGHT_MINIMUM = 32;
+const int WINDOW_HEIGHT_FALLBACK = 360;
 const char PreviewWindowClassName[] = "PreviewWnd";
-const unsigned long MaxPreviewFileSize = 1344; // kB
+const unsigned long MaxPreviewFileSize = 10240; // kB
 
 #define IDC_SHOW_PREVIEW    1000
 
@@ -93,6 +96,21 @@ ustring utf16_to_ustring(const wchar_t *utf16string, int utf16length = -1)
     return result;
 }
 
+namespace {
+
+int sanitizeWindowSizeParam( int size, int delta, int minimum, int fallback )
+{
+    int result = size;
+    if ( size < minimum ) {
+        g_warning( "Window size %d is less than cutoff.", size );
+        result = fallback - delta;
+    }
+    result += delta;
+    return result;
+}
+
+} // namespace
+
 /*#########################################################################
 ### F I L E     D I A L O G    B A S E    C L A S S
 #########################################################################*/
@@ -108,7 +126,7 @@ FileDialogBaseWin32::FileDialogBaseWin32(Gtk::Window &parent,
 
        _filter_index = 1;
        _filter_count = 0;
-       
+
     _title = (wchar_t*)g_utf8_to_utf16(title, -1, NULL, NULL, NULL);
        g_assert(_title != NULL);
 
@@ -166,7 +184,7 @@ FileOpenDialogImplWin32::FileOpenDialogImplWin32(Gtk::Window &parent,
     _preview_image_width = 0;
     _preview_image_height = 0;
     _preview_emf_image = false;
-       
+
        _mutex = NULL;
 
     createFilterMenu();
@@ -192,15 +210,17 @@ void FileOpenDialogImplWin32::createFilterMenu()
     Inkscape::Extension::DB::InputList extension_list;
     Inkscape::Extension::db.get_input_list(extension_list);
 
-    ustring all_inkscape_files_filter, all_image_files_filter;
-    Filter all_files, all_inkscape_files, all_image_files;
+    ustring all_inkscape_files_filter, all_image_files_filter, all_vectors_filter, all_bitmaps_filter;
+    Filter all_files, all_inkscape_files, all_image_files, all_vectors, all_bitmaps;
 
-    const gchar *all_files_filter_name = N_("All Files");
-    const gchar *all_inkscape_files_filter_name = N_("All Inkscape Files");
-    const gchar *all_image_files_filter_name = N_("All Image Files");
+    const gchar *all_files_filter_name = _("All Files");
+    const gchar *all_inkscape_files_filter_name = _("All Inkscape Files");
+    const gchar *all_image_files_filter_name = _("All Images");
+    const gchar *all_vectors_filter_name = _("All Vectors");
+    const gchar *all_bitmaps_filter_name = _("All Bitmaps");
 
     // Calculate the amount of memory required
-    int filter_count = 3;       // 3 - one for All Files, All Images and All Inkscape Files
+    int filter_count = 5;       // 5 - one for each filter type
     int filter_length = 1;
 
     for (Inkscape::Extension::DB::InputList::iterator current_item = extension_list.begin();
@@ -238,12 +258,54 @@ void FileOpenDialogImplWin32::createFilterMenu()
             all_image_files_filter += file_extension_name;
         }
 
+        // I don't know of any other way to define "bitmap" formats other than by listing them
+        // if you change it here, do the same change in filedialogimpl-gtkmm
+        if ( 
+            strncmp("image/png", imod->get_mimetype(), 9)==0 ||
+            strncmp("image/jpeg", imod->get_mimetype(), 10)==0 ||
+            strncmp("image/gif", imod->get_mimetype(), 9)==0 ||
+            strncmp("image/x-icon", imod->get_mimetype(), 12)==0 ||
+            strncmp("image/x-navi-animation", imod->get_mimetype(), 22)==0 ||
+            strncmp("image/x-cmu-raster", imod->get_mimetype(), 18)==0 ||
+            strncmp("image/x-xpixmap", imod->get_mimetype(), 15)==0 ||
+            strncmp("image/bmp", imod->get_mimetype(), 9)==0 ||
+            strncmp("image/vnd.wap.wbmp", imod->get_mimetype(), 18)==0 ||
+            strncmp("image/tiff", imod->get_mimetype(), 10)==0 ||
+            strncmp("image/x-xbitmap", imod->get_mimetype(), 15)==0 ||
+            strncmp("image/x-tga", imod->get_mimetype(), 11)==0 ||
+            strncmp("image/x-pcx", imod->get_mimetype(), 11)==0 
+            ) {
+            if(all_bitmaps_filter.length() > 0)
+                all_bitmaps_filter += ";*";
+            all_bitmaps_filter += file_extension_name;
+        } else {
+            if(all_vectors_filter.length() > 0)
+                all_vectors_filter += ";*";
+            all_vectors_filter += file_extension_name;
+        }
+
         filter_count++;
     }
 
     int extension_index = 0;
     _extension_map = new Inkscape::Extension::Extension*[filter_count];
 
+    // Filter bitmap files
+    all_bitmaps.name = g_utf8_to_utf16(all_bitmaps_filter_name,
+        -1, NULL, &all_bitmaps.name_length, NULL);
+    all_bitmaps.filter = g_utf8_to_utf16(all_bitmaps_filter.data(),
+            -1, NULL, &all_bitmaps.filter_length, NULL);
+       all_bitmaps.mod = NULL;
+    filter_list.push_front(all_bitmaps);
+
+    // Filter vector files
+    all_vectors.name = g_utf8_to_utf16(all_vectors_filter_name,
+        -1, NULL, &all_vectors.name_length, NULL);
+    all_vectors.filter = g_utf8_to_utf16(all_vectors_filter.data(),
+            -1, NULL, &all_vectors.filter_length, NULL);
+       all_vectors.mod = NULL;
+    filter_list.push_front(all_vectors);
+
     // Filter Image Files
     all_image_files.name = g_utf8_to_utf16(all_image_files_filter_name,
         -1, NULL, &all_image_files.name_length, NULL);
@@ -272,12 +334,17 @@ void FileOpenDialogImplWin32::createFilterMenu()
                     all_inkscape_files.filter_length +
                     all_inkscape_files.name_length + 3 +
                     all_image_files.filter_length +
-                    all_image_files.name_length + 3 + 1;
+                    all_image_files.name_length + 3 +
+                    all_vectors.filter_length +
+                    all_vectors.name_length + 3 +
+                    all_bitmaps.filter_length +
+                    all_bitmaps.name_length + 3 +
+                                                  1;
      // Add 3 for 2*2 \0s and a *, and 1 for a trailing \0
-        
+
        _filter = new wchar_t[filter_length];
     wchar_t *filterptr = _filter;
-       
+
     for(list<Filter>::iterator filter_iterator = filter_list.begin();
         filter_iterator != filter_list.end(); filter_iterator++)
     {
@@ -303,7 +370,7 @@ void FileOpenDialogImplWin32::createFilterMenu()
         _extension_map[extension_index++] = filter.mod;
     }
     *(filterptr++) = L'\0';
-       
+
        _filter_count = extension_index;
     _filter_index = 2; // Select the 2nd filter in the list - 2 is NOT the 3rd
 }
@@ -314,7 +381,7 @@ void FileOpenDialogImplWin32::GetOpenFileName_thread()
 
     g_assert(this != NULL);
        g_assert(_mutex != NULL);
-    
+
     WCHAR* current_directory_string = (WCHAR*)g_utf8_to_utf16(
         _current_directory.data(), _current_directory.length(),
                NULL, NULL, NULL);
@@ -396,9 +463,9 @@ UINT_PTR CALLBACK FileOpenDialogImplWin32::GetOpenFileName_hookproc(
             RECT rcRect;
             GetWindowRect(hParentWnd, &rcRect);
             MoveWindow(hParentWnd, rcRect.left, rcRect.top,
-                rcRect.right - rcRect.left + PreviewWidening,
-                rcRect.bottom - rcRect.top,
-                FALSE);
+                       rcRect.right - rcRect.left + PREVIEW_WIDENING,
+                       rcRect.bottom - rcRect.top,
+                       FALSE);
 
             // Set the pointer to the object
             OPENFILENAMEW *ofn = (OPENFILENAMEW*)lParam;
@@ -407,15 +474,15 @@ UINT_PTR CALLBACK FileOpenDialogImplWin32::GetOpenFileName_hookproc(
             pImpl = (FileOpenDialogImplWin32*)ofn->lCustData;
 
             // Subclass the parent
-            pImpl->_base_window_proc = (WNDPROC)GetWindowLongPtr(hParentWnd, GWL_WNDPROC);
-            SetWindowLongPtr(hParentWnd, GWL_WNDPROC, (LONG_PTR)file_dialog_subclass_proc);
+            pImpl->_base_window_proc = (WNDPROC)GetWindowLongPtr(hParentWnd, GWLP_WNDPROC);
+            SetWindowLongPtr(hParentWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(file_dialog_subclass_proc));
 
             // Add a button to the toolbar
             pImpl->_toolbar_wnd = FindWindowEx(hParentWnd, NULL, "ToolbarWindow32", NULL);
 
             pImpl->_show_preview_button_bitmap = LoadBitmap(
                 hInstance, MAKEINTRESOURCE(IDC_SHOW_PREVIEW));
-            TBADDBITMAP tbAddBitmap = {NULL, (UINT)pImpl->_show_preview_button_bitmap};
+            TBADDBITMAP tbAddBitmap = {NULL, reinterpret_cast<UINT_PTR>(pImpl->_show_preview_button_bitmap)};
             const int iBitmapIndex = SendMessage(pImpl->_toolbar_wnd,
                 TB_ADDBITMAP, 1, (LPARAM)&tbAddBitmap);
 
@@ -561,9 +628,12 @@ LRESULT CALLBACK FileOpenDialogImplWin32::preview_wnd_proc(HWND hwnd, UINT uMsg,
 
             if(pImpl->_path_string[0] == 0)
             {
+                WCHAR* noFileText=(WCHAR*)g_utf8_to_utf16(_("No file selected"),
+                    -1, NULL, NULL, NULL);
                 FillRect(dc, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
-                DrawText(dc, _("No file selected"), -1, &rcClient,
+                DrawTextW(dc,  noFileText, -1, &rcClient, 
                     DT_CENTER | DT_VCENTER | DT_NOPREFIX);
+                g_free(noFileText);
             }
             else if(pImpl->_preview_bitmap != NULL)
             {
@@ -837,20 +907,20 @@ bool FileOpenDialogImplWin32::set_svg_preview()
 
     gchar *utf8string = g_utf16_to_utf8((const gunichar2*)_path_string,
         _MAX_PATH, NULL, NULL, NULL);
-    SPDocument *svgDoc = sp_document_new (utf8string, true);
+    SPDocument *svgDoc = SPDocument::createNewDoc (utf8string, true);
     g_free(utf8string);
 
     // Check the document loaded properly
     if(svgDoc == NULL) return false;
     if(svgDoc->root == NULL)
     {
-        sp_document_unref(svgDoc);
+        svgDoc->doUnref();
         return false;
     }
 
     // Get the size of the document
-    const double svgWidth = sp_document_width(svgDoc);
-    const double svgHeight = sp_document_height(svgDoc);
+    const double svgWidth = svgDoc->getWidth();
+    const double svgHeight = svgDoc->getHeight();
 
     // Find the minimum scale to fit the image inside the preview area
     const double scaleFactorX =    PreviewSize / svgWidth;
@@ -866,16 +936,16 @@ bool FileOpenDialogImplWin32::set_svg_preview()
     NRRectL bbox = {0, 0, scaledSvgWidth, scaledSvgHeight};
 
     // write object bbox to area
-    boost::optional<NR::Rect> maybeArea(from_2geom(area));
-    sp_document_ensure_up_to_date (svgDoc);
-    sp_item_invoke_bbox((SPItem *) svgDoc->root, &maybeArea,
-        sp_item_i2r_affine((SPItem *)(svgDoc->root)), TRUE);
+    Geom::OptRect maybeArea(area);
+    svgDoc->ensureUpToDate();
+    static_cast<SPItem *>(svgDoc->root)->invoke_bbox( maybeArea,
+        static_cast<SPItem *>(svgDoc->root)->i2d_affine(), TRUE);
 
     NRArena *const arena = NRArena::create();
 
-    unsigned const key = sp_item_display_key_new(1);
+    unsigned const key = SPItem::display_key_new(1);
 
-    NRArenaItem *root = sp_item_invoke_show((SPItem*)(svgDoc->root),
+    NRArenaItem *root = static_cast<SPItem*>(svgDoc->root)->invoke_show(
         arena, key, SP_ITEM_SHOW_DISPLAY);
 
     NRGC gc(NULL);
@@ -899,7 +969,7 @@ bool FileOpenDialogImplWin32::set_svg_preview()
     // Fail if the pixblock failed to allocate
     if(pixBlock.data.px == NULL)
     {
-        sp_document_unref(svgDoc);
+        svgDoc->doUnref();
         return false;
     }
 
@@ -911,9 +981,8 @@ bool FileOpenDialogImplWin32::set_svg_preview()
     nr_arena_item_invoke_render(NULL, root, &bbox, &pixBlock, /*0*/NR_ARENA_ITEM_RENDER_NO_CACHE);
 
     // Tidy up
-    sp_document_unref(svgDoc);
-    sp_item_invoke_hide((SPItem*)(svgDoc->root), key);
-    nr_arena_item_unref(root);
+    svgDoc->doUnref();
+    static_cast<SPItem*>(svgDoc->root)->invoke_hide(key);
     nr_object_unref((NRObject *) arena);
 
     // Create the GDK pixbuf
@@ -1010,7 +1079,7 @@ MyGetEnhMetaFileW( const WCHAR *filename )
         if (hmf) {
             // Convert Windows Metafile to Enhanced Metafile
             DWORD nSize = GetMetaFileBitsEx( hmf, 0, NULL );
-            
+
             if (nSize) {
                 BYTE *lpvData = new BYTE[nSize];
                 if (lpvData) {
@@ -1324,10 +1393,10 @@ void FileOpenDialogImplWin32::render_preview()
     if(_preview_bitmap_image)    // Is the image a pixbuf?
     {
         // Set the transformation
-        const Matrix matrix = {
+        const Cairo::Matrix matrix(
             scaleFactor, 0,
             0, scaleFactor,
-            svgX, svgY };
+            svgX, svgY);
         context->set_matrix (matrix);
 
         // Render the image
@@ -1447,9 +1516,15 @@ FileSaveDialogImplWin32::FileSaveDialogImplWin32(Gtk::Window &parent,
             const Glib::ustring &dir,
             FileDialogType fileTypes,
             const char *title,
-            const Glib::ustring &/*default_key*/) :
-    FileDialogBaseWin32(parent, dir, title, fileTypes, "dialogs.save_as")
+            const Glib::ustring &/*default_key*/,
+            const char *docTitle,
+            const Inkscape::Extension::FileSaveMethod save_method) :
+    FileDialogBaseWin32(parent, dir, title, fileTypes,
+                        (save_method == Inkscape::Extension::FILE_SAVE_METHOD_SAVE_COPY) ? "dialogs.save_copy" :  "dialogs.save_as"),
+        _title_label(NULL),
+        _title_edit(NULL)
 {
+    FileSaveDialog::myDocTitle = docTitle;
     createFilterMenu();
 }
 
@@ -1560,9 +1635,11 @@ void FileSaveDialogImplWin32::GetSaveFileName_thread()
     ofn.nMaxFileTitle = 0;
     ofn.lpstrInitialDir = current_directory_string;
     ofn.lpstrTitle = _title;
-    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
+    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK;
     ofn.lpstrFilter = _filter;
     ofn.nFilterIndex = _filter_index;
+    ofn.lpfnHook = GetSaveFileName_hookproc;
+    ofn.lCustData = (LPARAM)this;
 
     _result = GetSaveFileNameW(&ofn) != 0;
 
@@ -1590,7 +1667,7 @@ FileSaveDialogImplWin32::show()
 
     _result = false;
     _main_loop = g_main_loop_new(g_main_context_default(), FALSE);
-       
+
        if(_main_loop != NULL)
        {
            if(Glib::Thread::create(sigc::mem_fun(*this, &FileSaveDialogImplWin32::GetSaveFileName_thread), true))
@@ -1599,7 +1676,7 @@ FileSaveDialogImplWin32::show()
            if(_result)
                appendExtension(myFilename, (Inkscape::Extension::Output*)_extension);
        }
-       
+
     return _result;
 }
 
@@ -1609,11 +1686,97 @@ void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension *
 
 }
 
-}
-}
+UINT_PTR CALLBACK FileSaveDialogImplWin32::GetSaveFileName_hookproc(
+    HWND hdlg, UINT uiMsg, WPARAM, LPARAM lParam)
+{
+    FileSaveDialogImplWin32 *pImpl = (FileSaveDialogImplWin32*)
+        GetWindowLongPtr(hdlg, GWLP_USERDATA);
+
+    switch(uiMsg)
+    {
+    case WM_INITDIALOG:
+        {
+            HWND hParentWnd = GetParent(hdlg);
+            HINSTANCE hInstance = GetModuleHandle(NULL);
+
+            // get size/pos of typical combo box
+            RECT rEDT1, rCB1, rROOT, rST;
+            GetWindowRect(GetDlgItem(hParentWnd, cmb1), &rCB1);
+            GetWindowRect(GetDlgItem(hParentWnd, cmb13), &rEDT1);
+            GetWindowRect(GetDlgItem(hParentWnd, stc2), &rST);
+            GetWindowRect(hdlg, &rROOT);
+            int ydelta = rCB1.top - rEDT1.top;
+            if ( ydelta < 0 ) {
+                g_warning("Negative dialog ydelta");
+                ydelta = 0;
+            }
+
+            // Make the window a bit longer
+            // Note: we have a width delta of 1 because there is a suspicion that MoveWindow() to the same size causes zero-width results.
+            RECT rcRect;
+            GetWindowRect(hParentWnd, &rcRect);
+            MoveWindow(hParentWnd, rcRect.left, rcRect.top,
+                       sanitizeWindowSizeParam( rcRect.right - rcRect.left, 1, WINDOW_WIDTH_MINIMUM, WINDOW_WIDTH_FALLBACK ),
+                       sanitizeWindowSizeParam( rcRect.bottom - rcRect.top, ydelta, WINDOW_HEIGHT_MINIMUM, WINDOW_HEIGHT_FALLBACK ),
+                       FALSE);
+
+            // It is not necessary to delete stock objects by calling DeleteObject
+            HGDIOBJ dlgFont = GetStockObject(DEFAULT_GUI_FONT);
+
+            // Set the pointer to the object
+            OPENFILENAMEW *ofn = (OPENFILENAMEW*)lParam;
+            SetWindowLongPtr(hdlg, GWLP_USERDATA, ofn->lCustData);
+            SetWindowLongPtr(hParentWnd, GWLP_USERDATA, ofn->lCustData);
+            pImpl = (FileSaveDialogImplWin32*)ofn->lCustData;
+
+            // Create the Title label and edit control
+            pImpl->_title_label = CreateWindowEx(NULL, "STATIC", "Title:",
+                                        WS_VISIBLE|WS_CHILD,
+                                        CW_USEDEFAULT, CW_USEDEFAULT, rCB1.left-rST.left, rST.bottom-rST.top,
+                                        hParentWnd, NULL, hInstance, NULL);
+            if(pImpl->_title_label) {
+              if(dlgFont) SendMessage(pImpl->_title_label, WM_SETFONT, (WPARAM)dlgFont, MAKELPARAM(FALSE, 0));
+              SetWindowPos(pImpl->_title_label, NULL, rST.left-rROOT.left, rST.top+ydelta-rROOT.top,
+                           rCB1.left-rST.left, rST.bottom-rST.top, SWP_SHOWWINDOW|SWP_NOZORDER);
+            }
+
+            pImpl->_title_edit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
+                                        WS_VISIBLE|WS_CHILD|WS_TABSTOP|ES_AUTOHSCROLL,
+                                        CW_USEDEFAULT, CW_USEDEFAULT, rCB1.right-rCB1.left, rCB1.bottom-rCB1.top,
+                                        hParentWnd, NULL, hInstance, NULL);
+            if(pImpl->_title_edit) {
+              if(dlgFont) SendMessage(pImpl->_title_edit, WM_SETFONT, (WPARAM)dlgFont, MAKELPARAM(FALSE, 0));
+              SetWindowPos(pImpl->_title_edit, NULL, rCB1.left-rROOT.left, rCB1.top+ydelta-rROOT.top,
+                           rCB1.right-rCB1.left, rCB1.bottom-rCB1.top, SWP_SHOWWINDOW|SWP_NOZORDER);
+              // TODO: make sure this works for Unicode
+              SetWindowText(pImpl->_title_edit, pImpl->myDocTitle.c_str());
+            }
+        }
+        break;
+    case WM_DESTROY:
+      {
+        if(pImpl->_title_edit) {
+          int length = GetWindowTextLength(pImpl->_title_edit)+1;
+          char* temp_title = new char[length];
+          GetWindowText(pImpl->_title_edit, temp_title, length);
+          pImpl->myDocTitle = temp_title;
+          delete[] temp_title;
+          DestroyWindow(pImpl->_title_label);
+          pImpl->_title_label = NULL;
+          DestroyWindow(pImpl->_title_edit);
+          pImpl->_title_edit = NULL;
+        }
+      }
+      break;
+    }
+
+    // Use default dialog behaviour
+    return 0;
 }
 
-#endif
+} } } // namespace Dialog, UI, Inkscape
+
+#endif // ifdef WIN32
 
 /*
   Local Variables:
@@ -1624,4 +1787,4 @@ void FileSaveDialogImplWin32::setSelectionType( Inkscape::Extension::Extension *
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :