• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_BASE_WIN_OPEN_FILE_NAME_WIN_H_
6 #define UI_BASE_WIN_OPEN_FILE_NAME_WIN_H_
7 
8 #include <Windows.h>
9 #include <Commdlg.h>
10 
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "base/strings/string16.h"
15 #include "base/tuple.h"
16 #include "ui/base/ui_base_export.h"
17 
18 namespace base {
19 class FilePath;
20 }  // namespace base
21 
22 namespace ui {
23 namespace win {
24 
25 // Encapsulates an OPENFILENAME struct and related buffers. Also provides static
26 // methods for interpreting the properties of an OPENFILENAME.
27 class UI_BASE_EXPORT OpenFileName {
28  public:
29   // Initializes the OPENFILENAME, which may be accessed using Get(). All fields
30   // will be NULL except for |lStructSize|, |lpstrFile|, and |nMaxFile|. The
31   // file buffer will initially contain a null-terminated empty string.
32   OpenFileName(HWND parent_window, DWORD flags);
33   ~OpenFileName();
34 
35   // Initializes |lpstrFilter| from the label/pattern pairs in |filters|.
36   void SetFilters(
37       const std::vector<Tuple2<base::string16, base::string16> >& filters);
38 
39   // Sets |lpstrInitialDir| and |lpstrFile|.
40   void SetInitialSelection(const base::FilePath& initial_directory,
41                            const base::FilePath& initial_filename);
42 
43   // The save as dialog on Windows XP remembers its last position, and if the
44   // screen resolution has changed it may be off screen. This method will check
45   // if we are running on XP and if so install a hook to reposition the dialog
46   // if necessary.
47   void MaybeInstallWindowPositionHookForSaveAsOnXP();
48 
49   // Returns the single selected file, or an empty path if there are more or
50   // less than one results.
51   base::FilePath GetSingleResult();
52 
53   // Returns the selected file or files.
54   void GetResult(base::FilePath* directory,
55                  std::vector<base::FilePath>* filenames);
56 
57   // Returns the OPENFILENAME structure.
GetOPENFILENAME()58   OPENFILENAME* GetOPENFILENAME() { return &openfilename_; }
59 
60   // Returns the OPENFILENAME structure.
GetOPENFILENAME()61   const OPENFILENAME* GetOPENFILENAME() const { return &openfilename_; }
62 
63   // Stores directory and filenames in the buffer pointed to by
64   // |openfilename->lpstrFile| and sized |openfilename->nMaxFile|.
65   static void SetResult(const base::FilePath& directory,
66                         const std::vector<base::FilePath>& filenames,
67                         OPENFILENAME* openfilename);
68 
69   // Returns a vector of label/pattern pairs built from
70   // |openfilename->lpstrFilter|.
71   static std::vector<Tuple2<base::string16, base::string16> > GetFilters(
72       const OPENFILENAME* openfilename);
73 
74  private:
75   OPENFILENAME openfilename_;
76   base::string16 initial_directory_buffer_;
77   wchar_t filename_buffer_[UNICODE_STRING_MAX_CHARS];
78   base::string16 filter_buffer_;
79 
80   DISALLOW_COPY_AND_ASSIGN(OpenFileName);
81 };
82 
83 }  // namespace win
84 }  // namespace ui
85 
86 #endif  // UI_BASE_WIN_OPEN_FILE_NAME_WIN_H_
87