shell_dialogs.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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 CHROME_BROWSER_UI_SHELL_DIALOGS_H_
6#define CHROME_BROWSER_UI_SHELL_DIALOGS_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/file_path.h"
13#include "base/ref_counted.h"
14#include "base/string16.h"
15#include "ui/gfx/native_widget_types.h"
16
17namespace gfx {
18class Font;
19}
20
21// This function is declared extern such that it is accessible for unit tests
22// in /chrome/browser/ui/views/shell_dialogs_win_unittest.cc
23extern std::wstring AppendExtensionIfNeeded(const std::wstring& filename,
24                                            const std::wstring& filter_selected,
25                                            const std::wstring& suggested_ext);
26
27// A base class for shell dialogs.
28class BaseShellDialog {
29 public:
30  // Returns true if a shell dialog box is currently being shown modally
31  // to the specified owner.
32  virtual bool IsRunning(gfx::NativeWindow owning_window) const = 0;
33
34  // Notifies the dialog box that the listener has been destroyed and it should
35  // no longer be sent notifications.
36  virtual void ListenerDestroyed() = 0;
37
38 protected:
39  virtual ~BaseShellDialog() {}
40};
41
42// Shows a dialog box for selecting a file or a folder.
43class SelectFileDialog
44    : public base::RefCountedThreadSafe<SelectFileDialog>,
45      public BaseShellDialog {
46 public:
47  enum Type {
48    SELECT_NONE,
49    SELECT_FOLDER,
50    SELECT_SAVEAS_FILE,
51    SELECT_OPEN_FILE,
52    SELECT_OPEN_MULTI_FILE
53  };
54
55  // An interface implemented by a Listener object wishing to know about the
56  // the result of the Select File/Folder action. These callbacks must be
57  // re-entrant.
58  class Listener {
59   public:
60    // Notifies the Listener that a file/folder selection has been made. The
61    // file/folder path is in |path|. |params| is contextual passed to
62    // SelectFile. |index| specifies the index of the filter passed to the
63    // the initial call to SelectFile.
64    virtual void FileSelected(const FilePath& path,
65                              int index, void* params) = 0;
66
67    // Notifies the Listener that many files have been selected. The
68    // files are in |files|. |params| is contextual passed to SelectFile.
69    virtual void MultiFilesSelected(
70      const std::vector<FilePath>& files, void* params) {}
71
72    // Notifies the Listener that the file/folder selection was aborted (via
73    // the  user canceling or closing the selection dialog box, for example).
74    // |params| is contextual passed to SelectFile.
75    virtual void FileSelectionCanceled(void* params) {}
76
77   protected:
78    virtual ~Listener() {}
79  };
80
81  // Creates a dialog box helper. This object is ref-counted, but the returned
82  // object will have no reference (refcount is 0).
83  static SelectFileDialog* Create(Listener* listener);
84
85  // Holds information about allowed extensions on a file save dialog.
86  // |extensions| is a list of allowed extensions. For example, it might be
87  //   { { "htm", "html" }, { "txt" } }. Only pass more than one extension
88  //   in the inner vector if the extensions are equivalent. Do NOT include
89  //   leading periods.
90  // |extension_description_overrides| overrides the system descriptions of the
91  //   specified extensions. Entries correspond to |extensions|; if left blank
92  //   the system descriptions will be used.
93  // |include_all_files| specifies whether there will be a filter added for all
94  //   files (i.e. *.*).
95  struct FileTypeInfo {
96    FileTypeInfo();
97    ~FileTypeInfo();
98
99    std::vector<std::vector<FilePath::StringType> > extensions;
100    std::vector<string16> extension_description_overrides;
101    bool include_all_files;
102  };
103
104  // Selects a file. This will start displaying the dialog box. This will also
105  // block the calling window until the dialog box is complete. The listener
106  // associated with this object will be notified when the selection is
107  // complete.
108  // |type| is the type of file dialog to be shown, see Type enumeration above.
109  // |title| is the title to be displayed in the dialog. If this string is
110  //   empty, the default title is used.
111  // |default_path| is the default path and suggested file name to be shown in
112  //   the dialog. This only works for SELECT_SAVEAS_FILE and SELECT_OPEN_FILE.
113  //   Can be an empty string to indicate the platform default.
114  // |file_types| holds the infomation about the file types allowed. Pass NULL
115  //   to get no special behavior
116  // |file_type_index| is the 1-based index into the file type list in
117  //   |file_types|. Specify 0 if you don't need to specify extension behavior.
118  // |default_extension| is the default extension to add to the file if the
119  //   user doesn't type one. This should NOT include the '.'. On Windows, if
120  //   you specify this you must also specify |file_types|.
121  // |owning_window| is the window the dialog is modal to, or NULL for a
122  //   modeless dialog.
123  // |params| is data from the calling context which will be passed through to
124  //   the listener. Can be NULL.
125  // NOTE: only one instance of any shell dialog can be shown per owning_window
126  //       at a time (for obvious reasons).
127  virtual void SelectFile(Type type,
128                          const string16& title,
129                          const FilePath& default_path,
130                          const FileTypeInfo* file_types,
131                          int file_type_index,
132                          const FilePath::StringType& default_extension,
133                          gfx::NativeWindow owning_window,
134                          void* params) = 0;
135
136  // browser_mode is true when running inside the browser.
137  virtual void set_browser_mode(bool value) {}
138
139 protected:
140  friend class base::RefCountedThreadSafe<SelectFileDialog>;
141  SelectFileDialog();
142  virtual ~SelectFileDialog();
143};
144
145// Shows a dialog box for selecting a font.
146class SelectFontDialog
147    : public base::RefCountedThreadSafe<SelectFontDialog>,
148      public BaseShellDialog {
149 public:
150
151  // An interface implemented by a Listener object wishing to know about the
152  // the result of the Select Font action. These callbacks must be
153  // re-entrant.
154  class Listener {
155   public:
156    // Notifies the Listener that a font selection has been made. The font
157    // details are supplied in |font|. |params| is contextual passed to
158    // SelectFont.
159    virtual void FontSelected(const gfx::Font& font, void* params) = 0;
160
161    // Notifies the Listener that the font selection was aborted (via the user
162    // canceling or closing the selection dialog box, for example). |params| is
163    // contextual passed to SelectFont.
164    virtual void FontSelectionCanceled(void* params) {}
165
166   protected:
167    virtual ~Listener() {}
168  };
169
170  // Creates a dialog box helper. This object is ref-counted, but the returned
171  // object will have no reference (refcount is 0).
172  static SelectFontDialog* Create(Listener* listener);
173
174  // Selects a font. This will start displaying the dialog box. This will also
175  // block the calling window until the dialog box is complete. The listener
176  // associated with this object will be notified when the selection is
177  // complete.
178  // |owning_window| is the window the dialog is modal to, or NULL for a
179  // modeless dialog.
180  // |params| is data from the calling context which will be passed through to
181  // the listener. Can be NULL.
182  // NOTE: only one instance of any shell dialog can be shown per owning_window
183  //       at a time (for obvious reasons).
184  // TODO(beng): support specifying the default font selected in the list when
185  //             the dialog appears.
186  virtual void SelectFont(gfx::NativeWindow owning_window,
187                          void* params) = 0;
188
189  // Same as above - also support specifying the default font selected in the
190  // list when the dialog appears.
191  virtual void SelectFont(gfx::NativeWindow owning_window,
192                          void* params,
193                          const std::wstring& font_name,
194                          int font_size) = 0;
195
196 protected:
197  friend class base::RefCountedThreadSafe<SelectFontDialog>;
198  SelectFontDialog();
199  virtual ~SelectFontDialog();
200};
201
202#endif  // CHROME_BROWSER_UI_SHELL_DIALOGS_H_
203