widget/windows/nsFilePicker.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  *
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef nsFilePicker_h__
     8 #define nsFilePicker_h__
    10 #include <windows.h>
    12 // For Vista IFileDialog interfaces which aren't exposed
    13 // unless _WIN32_WINNT >= _WIN32_WINNT_LONGHORN.
    14 #if _WIN32_WINNT < _WIN32_WINNT_LONGHORN
    15 #define _WIN32_WINNT_bak _WIN32_WINNT
    16 #undef _WIN32_WINNT
    17 #define _WIN32_WINNT _WIN32_WINNT_LONGHORN
    18 #define _WIN32_IE_bak _WIN32_IE
    19 #undef _WIN32_IE
    20 #define _WIN32_IE _WIN32_IE_IE70
    21 #endif
    23 #include "nsIFile.h"
    24 #include "nsITimer.h"
    25 #include "nsISimpleEnumerator.h"
    26 #include "nsCOMArray.h"
    27 #include "nsAutoPtr.h"
    28 #include "nsBaseFilePicker.h"
    29 #include "nsString.h"
    30 #include "nsdefs.h"
    31 #include <commdlg.h>
    32 #include <shobjidl.h>
    33 #undef LogSeverity // SetupAPI.h #defines this as DWORD
    35 class nsILoadContext;
    37 class nsBaseWinFilePicker :
    38   public nsBaseFilePicker
    39 {
    40 public:
    41   NS_IMETHOD GetDefaultString(nsAString& aDefaultString);
    42   NS_IMETHOD SetDefaultString(const nsAString& aDefaultString);
    43   NS_IMETHOD GetDefaultExtension(nsAString& aDefaultExtension);
    44   NS_IMETHOD SetDefaultExtension(const nsAString& aDefaultExtension);
    46 protected:
    47   nsString mDefaultFilePath;
    48   nsString mDefaultFilename;
    49   nsString mDefaultExtension;
    50 };
    52 /**
    53  * Native Windows FileSelector wrapper
    54  */
    56 class nsFilePicker :
    57   public IFileDialogEvents,
    58   public nsBaseWinFilePicker
    59 {
    60 public:
    61   nsFilePicker(); 
    62   virtual ~nsFilePicker();
    64   NS_IMETHOD Init(nsIDOMWindow *aParent, const nsAString& aTitle, int16_t aMode);
    66   NS_DECL_ISUPPORTS
    68   // IUnknown's QueryInterface
    69   STDMETHODIMP QueryInterface(REFIID refiid, void** ppvResult);
    71   // nsIFilePicker (less what's in nsBaseFilePicker and nsBaseWinFilePicker)
    72   NS_IMETHOD GetFilterIndex(int32_t *aFilterIndex);
    73   NS_IMETHOD SetFilterIndex(int32_t aFilterIndex);
    74   NS_IMETHOD GetFile(nsIFile * *aFile);
    75   NS_IMETHOD GetFileURL(nsIURI * *aFileURL);
    76   NS_IMETHOD GetFiles(nsISimpleEnumerator **aFiles);
    77   NS_IMETHOD Show(int16_t *aReturnVal); 
    78   NS_IMETHOD ShowW(int16_t *aReturnVal); 
    79   NS_IMETHOD AppendFilter(const nsAString& aTitle, const nsAString& aFilter);
    81   // IFileDialogEvents
    82   HRESULT STDMETHODCALLTYPE OnFileOk(IFileDialog *pfd);
    83   HRESULT STDMETHODCALLTYPE OnFolderChanging(IFileDialog *pfd, IShellItem *psiFolder);
    84   HRESULT STDMETHODCALLTYPE OnFolderChange(IFileDialog *pfd);
    85   HRESULT STDMETHODCALLTYPE OnSelectionChange(IFileDialog *pfd);
    86   HRESULT STDMETHODCALLTYPE OnShareViolation(IFileDialog *pfd, IShellItem *psi, FDE_SHAREVIOLATION_RESPONSE *pResponse);
    87   HRESULT STDMETHODCALLTYPE OnTypeChange(IFileDialog *pfd);
    88   HRESULT STDMETHODCALLTYPE OnOverwrite(IFileDialog *pfd, IShellItem *psi, FDE_OVERWRITE_RESPONSE *pResponse);
    90 protected:
    91   enum PickerType {
    92     PICKER_TYPE_OPEN,
    93     PICKER_TYPE_SAVE,
    94   };
    96   /* method from nsBaseFilePicker */
    97   virtual void InitNative(nsIWidget *aParent,
    98                           const nsAString& aTitle);
    99   static void GetQualifiedPath(const wchar_t *aInPath, nsString &aOutPath);
   100   void GetFilterListArray(nsString& aFilterList);
   101   bool FilePickerWrapper(OPENFILENAMEW* ofn, PickerType aType);
   102   bool ShowXPFolderPicker(const nsString& aInitialDir);
   103   bool ShowXPFilePicker(const nsString& aInitialDir);
   104   bool ShowFolderPicker(const nsString& aInitialDir, bool &aWasInitError);
   105   bool ShowFilePicker(const nsString& aInitialDir, bool &aWasInitError);
   106   void AppendXPFilter(const nsAString& aTitle, const nsAString& aFilter);
   107   void RememberLastUsedDirectory();
   108   bool IsPrivacyModeEnabled();
   109   bool IsDefaultPathLink();
   110   bool IsDefaultPathHtml();
   111   void SetDialogHandle(HWND aWnd);
   112   bool ClosePickerIfNeeded(bool aIsXPDialog);
   113   static void PickerCallbackTimerFunc(nsITimer *aTimer, void *aPicker);
   114   static UINT_PTR CALLBACK MultiFilePickerHook(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
   115   static UINT_PTR CALLBACK FilePickerHook(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
   117   nsCOMPtr<nsILoadContext> mLoadContext;
   118   nsCOMPtr<nsIWidget>    mParentWidget;
   119   nsString               mTitle;
   120   nsCString              mFile;
   121   nsString               mFilterList;
   122   int16_t                mSelectedType;
   123   nsCOMArray<nsIFile>    mFiles;
   124   static char            mLastUsedDirectory[];
   125   nsString               mUnicodeFile;
   126   static char16_t      *mLastUsedUnicodeDirectory;
   127   HWND                   mDlgWnd;
   129   class ComDlgFilterSpec
   130   {
   131   public:
   132     ComDlgFilterSpec() {}
   133     ~ComDlgFilterSpec() {}
   135     const uint32_t Length() {
   136       return mSpecList.Length();
   137     }
   139     const bool IsEmpty() {
   140       return (mSpecList.Length() == 0);
   141     }
   143     const COMDLG_FILTERSPEC* get() {
   144       return mSpecList.Elements();
   145     }
   147     void Append(const nsAString& aTitle, const nsAString& aFilter);
   148   private:
   149     nsAutoTArray<COMDLG_FILTERSPEC, 1> mSpecList;
   150     nsAutoTArray<nsString, 2> mStrings;
   151   };
   153   ComDlgFilterSpec       mComFilterList;
   154   DWORD                  mFDECookie;
   155 };
   157 #if defined(_WIN32_WINNT_bak)
   158 #undef _WIN32_WINNT
   159 #define _WIN32_WINNT _WIN32_WINNT_bak
   160 #undef _WIN32_IE
   161 #define _WIN32_IE _WIN32_IE_bak
   162 #endif
   164 #endif // nsFilePicker_h__

mercurial