summaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/filedialogimpl-win32.h
blob: 3b12f7cbf4e2e1cfc6e2cf60947fcca0fff01ffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * @brief Implementation of native file dialogs for Win32
 */
/* Authors:
 *   Joel Holdsworth
 *   Inkscape Authors
 *
 * Copyright (C) 2004-2008 Inkscape Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glibmm.h>

#ifdef _WIN32

#include "filedialogimpl-gtkmm.h"
 
#include "inkgc/gc-core.h"

#include <memory>
#include <mutex>
#include <windows.h>


namespace Inkscape
{
namespace UI
{
namespace Dialog
{

/*#########################################################################
### F I L E     D I A L O G    B A S E    C L A S S
#########################################################################*/

/// This class is the base implementation of a MS Windows
/// file dialog.
class FileDialogBaseWin32
{
protected:
    /// Abstract Constructor
    /// @param parent The parent window for the dialog
    /// @param dir The directory to begin browsing from
    /// @param title The title caption for the dialog in UTF-8
    /// @param type The dialog type
    /// @param preferenceBase The preferences key
    FileDialogBaseWin32(Gtk::Window &parent, const Glib::ustring &dir,
        const char *title, FileDialogType type,
        gchar const *preferenceBase);

    /// Destructor
    ~FileDialogBaseWin32();

public:

    /// Get the path of the current directory
    Glib::ustring getCurrentDirectory();

protected:
    /// The dialog type
    FileDialogType dialogType;

	/// A pointer to the GTK main-loop context object. This
    /// is used to keep the rest of the inkscape UI running
    /// while the file dialog is displayed
    GMainLoop *_main_loop;

    /// The result of the call to GetOpenFileName. If true
    /// the user clicked OK, if false the user clicked cancel
    bool _result;

    /// The parent window
    Gtk::Window &parent;

    /// The windows handle of the parent window
    HWND _ownerHwnd;

    /// The path of the directory that is currently being
    /// browsed
    Glib::ustring _current_directory;

    /// The title of the dialog in UTF-16
    wchar_t *_title;

    /// The path of the currently selected file in UTF-16
    wchar_t _path_string[_MAX_PATH];

    /// The filter string for GetOpenFileName in UTF-16
    wchar_t *_filter;

    /// The index of the currently selected filter.
	/// This value must be greater than or equal to 1,
	/// and less than or equal to _filter_count.
    unsigned int _filter_index;

	/// The number of filters registered
	unsigned int _filter_count;

    /// An array of the extensions associated with the
    /// file types of each filter. So the Nth entry of
    /// this array corresponds to the extension of the Nth
    /// filter in the list. NULL if no specific extension is
    /// specified/
    Inkscape::Extension::Extension **_extension_map;
};


/*#########################################################################
### F I L E    O P E N
#########################################################################*/

/// An Inkscape compatible wrapper around MS Windows GetOpenFileName API
class FileOpenDialogImplWin32 : public FileOpenDialog, public FileDialogBaseWin32
{
public:
    /// Constructor
    /// @param parent The parent window for the dialog
    /// @param dir The directory to begin browsing from
    /// @param title The title caption for the dialog in UTF-8
    /// @param type The dialog type
    FileOpenDialogImplWin32(Gtk::Window &parent,
                            const Glib::ustring &dir,
                            FileDialogType fileTypes,
                            const char *title);

    /// Destructor
    virtual ~FileOpenDialogImplWin32();

    /// Shows the file dialog, and blocks until a file
    /// has been selected.
    /// @return Returns true if the user selected a
    /// file, or false if the user pressed cancel.
    bool show();

    /// Gets a list of the selected file names
    /// @return Returns an STL vector filled with the
    /// GTK names of the selected files
    std::vector<Glib::ustring> getFilenames();

    /// Get the path of the current directory
    virtual Glib::ustring getCurrentDirectory()
        { return FileDialogBaseWin32::getCurrentDirectory(); }

    /// Add a custom file filter menu item
    /// @param name - Name of the filter (such as "Javscript")
    /// @param pattern - File filtering pattern (such as "*.js")
    /// Use the FileDialogType::CUSTOM_TYPE in constructor to not include other file types
    void addFilterMenu(const Glib::ustring &name, Glib::ustring pattern = "", Inkscape::Extension::Extension *mod = nullptr) override;

private:

    /// Create filter menu for this type of dialog
    void createFilterMenu();

    /// The handle of the preview pane window
    HWND _preview_wnd;

    /// The handle of the file dialog window
    HWND _file_dialog_wnd;

    /// A pointer to the standard window proc of the
    /// unhooked file dialog
    WNDPROC _base_window_proc;

    /// The handle of the bitmap of the "show preview"
    /// toggle button
    HBITMAP _show_preview_button_bitmap;

    /// The handle of the toolbar's window
    HWND _toolbar_wnd;

    /// This flag is set true when the preview should be
    /// shown, or false when it should be hidden
    static bool _show_preview;


    /// The current width of the preview pane in pixels
    int _preview_width;

    /// The current height of the preview pane in pixels
    int _preview_height;

    /// The handle of the windows to display within the
    /// preview pane, or NULL if no image should be displayed
    HBITMAP _preview_bitmap;

    /// The windows shell icon for the selected file
    HICON _preview_file_icon;

    /// The size of the preview file in kilobytes
    unsigned long _preview_file_size;


    /// The width of the document to be shown in the preview panel
    double _preview_document_width;

    /// The width of the document to be shown in the preview panel
    double _preview_document_height;

    /// The width of the rendered preview image in pixels
    int _preview_image_width;

    /// The height of the rendered preview image in pixels
    int _preview_image_height;

    /// A GDK Pixbuf of the rendered preview to be displayed
    Glib::RefPtr<Gdk::Pixbuf> _preview_bitmap_image;

    /// This flag is set true if a file has been selected
    bool _file_selected;

	/// This flag is set true when the GetOpenFileName call
    /// has returned
    bool _finished;

    /// This mutex is used to ensure that the worker thread
    /// that calls GetOpenFileName cannot collide with the
    /// main Inkscape thread
    std::unique_ptr<std::mutex> _mutex;

    /// The controller function for the thread which calls
    /// GetOpenFileName
    void GetOpenFileName_thread();

    /// Registers the Windows Class of the preview panel window
    static void register_preview_wnd_class();

    /// A message proc which is called by the standard dialog
    /// proc
    static UINT_PTR CALLBACK GetOpenFileName_hookproc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam);

    /// A message proc which wraps the standard dialog proc,
    /// but intercepts some calls
    static LRESULT CALLBACK file_dialog_subclass_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    /// The message proc for the preview panel window
    static LRESULT CALLBACK preview_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    /// Lays out the controls in the file dialog given it's
    /// current size
    /// GetOpenFileName thread only.
    void layout_dialog();

    /// Enables or disables the file preview.
    /// GetOpenFileName thread only.
    void enable_preview(bool enable);

    /// This function is called in the App thread when a file had
    /// been selected
    void file_selected();

    /// Loads and renders the unshrunk preview image.
    /// Main app thread only.
    void load_preview();

    /// Frees all the allocated objects associated with the file
    /// currently being previewed
    /// Main app thread only.
    void free_preview();

    /// Loads preview for an SVG or SVGZ file.
    /// Main app thread only.
    /// @return Returns true if the SVG loaded successfully
    bool set_svg_preview();

    /// A callback to allow this class to dispose of the
    /// memory block of the rendered SVG bitmap
    /// @buffer buffer The buffer to free
    static void destroy_svg_rendering(const guint8 *buffer);

    /// Loads the preview for a raster image
    /// Main app thread only.
    /// @return Returns true if the image loaded successfully
    bool set_image_preview();

    /// Loads the preview for a meta file
    /// Main app thread only.
    /// @return Returns true if the image loaded successfully
    bool set_emf_preview();

    /// This flag is set true when a meta file is previewed
    bool _preview_emf_image;

    /// Renders the unshrunk preview image to a windows HTBITMAP
    /// which can be painted in the preview pain.
    /// Main app thread only.
    void render_preview();

    /// Formats the caption in UTF-16 for the preview image
    /// @param caption The buffer to format the caption string into
    /// @param caption_size The number of wchar_ts in the caption buffer
    /// @return Returns the number of characters in caption string
    int format_caption(wchar_t *caption, int caption_size);
};


/*#########################################################################
### F I L E    S A V E
#########################################################################*/

/// An Inkscape compatible wrapper around MS Windows GetSaveFileName API
class FileSaveDialogImplWin32 : public FileSaveDialog, public FileDialogBaseWin32
{

public:
    FileSaveDialogImplWin32(Gtk::Window &parent,
                            const Glib::ustring &dir,
                            FileDialogType fileTypes,
                            const char *title,
                            const Glib::ustring &default_key,
                            const char *docTitle,
                            const Inkscape::Extension::FileSaveMethod save_method);

    /// Destructor
    virtual ~FileSaveDialogImplWin32();

    /// Shows the file dialog, and blocks until a file
    /// has been selected.
    /// @return Returns true if the user selected a
    /// file, or false if the user pressed cancel.
    bool show();

    /// Get the path of the current directory
    virtual Glib::ustring getCurrentDirectory()
        { return FileDialogBaseWin32::getCurrentDirectory(); }

    void addFileType(Glib::ustring name, Glib::ustring pattern);
    void addFilterMenu(const Glib::ustring &name, Glib::ustring pattern = "", Inkscape::Extension::Extension *mod = nullptr) override
    {
    }

private:
	/// A handle to the title label and edit box
    HWND _title_label;
    HWND _title_edit;

    /// Create a filter menu for this type of dialog
    void createFilterMenu();

    // SaveAs or SaveAsCopy
    Inkscape::Extension::FileSaveMethod save_method;

    /// The controller function for the thread which calls
    /// GetSaveFileName
    void GetSaveFileName_thread();

    /// A message proc which is called by the standard dialog
    /// proc
    static UINT_PTR CALLBACK GetSaveFileName_hookproc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam);

};


}
}
}

#endif

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :