summaryrefslogtreecommitdiff
path: root/fpicker/source/win32/filepicker/previewadapter.cxx
blob: 6cb7bcfbae80ab05cd2c95eafc8bb2be6cca2d90 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <tchar.h>
#include "previewadapter.hxx"

#include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
#include <osl/diagnose.h>
#include "dibpreview.hxx"
#include "../misc/WinImplHelper.hxx"

#include <memory>
#include <stdexcept>


using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;


// An impl class to hide implementation details
// from clients


class CPreviewAdapterImpl
{
public:
    explicit CPreviewAdapterImpl(HINSTANCE instance);

    virtual ~CPreviewAdapterImpl();

    virtual sal_Int32 SAL_CALL getTargetColorDepth();

    virtual sal_Int32 SAL_CALL getAvailableWidth();

    virtual sal_Int32 SAL_CALL getAvailableHeight();

    virtual void SAL_CALL setImage( sal_Int16 aImageFormat, const Any& aImage )
        throw (IllegalArgumentException,RuntimeException);

    virtual sal_Bool SAL_CALL setShowState(sal_Bool bShowState);

    virtual sal_Bool SAL_CALL getShowState();

    virtual void SAL_CALL setParent(HWND parent);

    virtual HWND SAL_CALL getParent();


    // parent notification handler


    virtual void SAL_CALL notifyParentShow(sal_Bool bShow);

    virtual void SAL_CALL notifyParentSizeChanged();

    virtual void SAL_CALL notifyParentWindowPosChanged();

protected:
    virtual void SAL_CALL calcRightMargin();

    virtual void SAL_CALL rearrangeLayout();

    void SAL_CALL initializeActivePreview() throw(std::runtime_error);

    HWND SAL_CALL findFileListbox() const;

// member
protected:
    HINSTANCE                   m_Instance;
    std::unique_ptr<PreviewBase> m_Preview;
    HWND                        m_FileDialog;
    int                         m_RightMargin;

//prevent copy/assignment
private:
    CPreviewAdapterImpl(const CPreviewAdapterImpl&);
    CPreviewAdapterImpl& operator=(const CPreviewAdapterImpl&);
};


CPreviewAdapterImpl::CPreviewAdapterImpl(HINSTANCE instance) :
    m_Instance(instance),
    m_Preview(new PreviewBase()), // create dummy preview (NULL-Object pattern)
    m_FileDialog(0),
    m_RightMargin(0)
{
}


CPreviewAdapterImpl::~CPreviewAdapterImpl()
{
}


sal_Int32 SAL_CALL CPreviewAdapterImpl::getTargetColorDepth()
{
    return m_Preview->getTargetColorDepth();
}


sal_Int32 SAL_CALL CPreviewAdapterImpl::getAvailableWidth()
{
    return m_Preview->getAvailableWidth();
}


sal_Int32 SAL_CALL CPreviewAdapterImpl::getAvailableHeight()
{
    return m_Preview->getAvailableHeight();
}


void SAL_CALL CPreviewAdapterImpl::setImage( sal_Int16 aImageFormat, const Any& aImage )
    throw (IllegalArgumentException,RuntimeException)
{
    m_Preview->setImage(aImageFormat,aImage);
}


sal_Bool SAL_CALL CPreviewAdapterImpl::setShowState( sal_Bool bShowState )
{
    sal_Bool bRet = m_Preview->setShowState(bShowState);
    rearrangeLayout();
    return bRet;
}


sal_Bool SAL_CALL CPreviewAdapterImpl::getShowState()
{
    return m_Preview->getShowState();
}


void SAL_CALL CPreviewAdapterImpl::setParent(HWND parent)
{
    OSL_PRECOND(IsWindow(parent),"Invalid FileDialog handle");

    m_FileDialog = parent;
    calcRightMargin();
}


HWND SAL_CALL CPreviewAdapterImpl::getParent()
{
    return m_FileDialog;
}


void SAL_CALL CPreviewAdapterImpl::calcRightMargin()
{
    // Calculate the right reference margin

    // Assumption:
    // 1. This method will be called before the dialog becomes
    //    visible
    // 2. There exist a FileListbox with the id lst1 even
    //    if it is not visible like under Win2000/XP
    // 3. Initially this FileListbox has the appropriate size
    //    to fit within the FileListbox
    // 4. The margin between the right edge of the FileListbox
    //    and the right edge of the FileDialog will be constant
    //    even if the size of the dialog changes

    HWND flb = GetDlgItem(m_FileDialog,lst1);

    OSL_ENSURE(IsWindow(flb),"Filelistbox not found");

    RECT rcFlb;
    GetWindowRect(flb,&rcFlb);

    RECT rcFileDlg;
    GetWindowRect(m_FileDialog,&rcFileDlg);

    m_RightMargin = rcFileDlg.right - rcFlb.right;
}


void SAL_CALL CPreviewAdapterImpl::notifyParentShow(sal_Bool)
{
}


void SAL_CALL CPreviewAdapterImpl::notifyParentSizeChanged()
{
    rearrangeLayout();
}


void SAL_CALL CPreviewAdapterImpl::notifyParentWindowPosChanged()
{
}


void SAL_CALL CPreviewAdapterImpl::rearrangeLayout()
{
    // try to get a handle to the filelistbox
    // if there is no new-style filelistbox like
    // in Win2000/XP there should be at least the
    // old listbox, so we take this one
    // lst1 - identifies the old-style filelistbox
    // lst2 - identifies the new-style filelistbox
    // see dlgs.h
    HWND flb_new = findFileListbox();

    // under Windows NT 4.0 the size of the old
    // filelistbox will be used as reference for
    // sizing the new filelistbox, so we have
    // to change the size of it too
    HWND flb_old = GetDlgItem(m_FileDialog,lst1);

    RECT rcFlbNew;
    GetWindowRect(flb_new,&rcFlbNew);

    RECT rcFileDlg;
    GetWindowRect(m_FileDialog,&rcFileDlg);
    rcFileDlg.right -= m_RightMargin;

    // the available area for the filelistbox should be
    // the left edge of the filelistbox and the right
    // edge of the OK button, we take this as reference
    int height = rcFlbNew.bottom - rcFlbNew.top;
    int width  = rcFileDlg.right - rcFlbNew.left;

    HWND prvwnd = m_Preview->getWindowHandle();

    // we use GetWindowLong to ask for the visibility
    // of the preview window because IsWindowVisible
    // only returns true the specified window including
    // its parent windows are visible
    // this is not the case when we are called in response
    // to the WM_SHOWWINDOW message, somehow the WS_VISIBLE
    // style bit of the FileOpen dialog must be set after that
    // message
    LONG lStyle = GetWindowLong(prvwnd,GWL_STYLE);
    bool bIsVisible = ((lStyle & WS_VISIBLE) != 0);

    int cx = 0;

    if (IsWindow(prvwnd) && bIsVisible)
    {
        cx = width/2;

        // resize the filelistbox to the half of the
        // available space
        SetWindowPos(flb_new,
            NULL, 0, 0, cx, height,
            SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

        SetWindowPos(flb_old,
            NULL, 0, 0, cx, height,
            SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

        // get the new dimensions of the filelistbox after
        // resizing and take the right,top corner as starting
        // point for the preview window
        GetWindowRect(flb_new,&rcFlbNew);
        POINT pt = { rcFlbNew.right, rcFlbNew.top };
        ScreenToClient(m_FileDialog,&pt);

        // resize the preview window to fit within
        // the available space and set the window
        // to the top of the z-order else it will
        // be invisible
        SetWindowPos(prvwnd,
            HWND_TOP, pt.x, pt.y, cx, height, SWP_NOACTIVATE);
    }
    else
    {
        // resize the filelistbox to the maximum available
        // space
        cx = rcFileDlg.right - rcFlbNew.left;

        // resize the old filelistbox
        SetWindowPos(flb_old,
            NULL, 0, 0, cx, height,
            SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

        // resize the new filelistbox
        SetWindowPos(flb_new,
            NULL, 0, 0, cx, height,
            SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE );
    }
}


void SAL_CALL CPreviewAdapterImpl::initializeActivePreview() throw(std::runtime_error)
{
    sal_Bool bShowState = m_Preview->getImaginaryShowState();

    sal_Int16 aImgFrmt;
    Any aImg;
    m_Preview->getImage(aImgFrmt,aImg);

    HWND flb = findFileListbox();

    PreviewBase* prv = new CDIBPreview(
        m_Instance, GetParent(flb), bShowState);

    m_Preview.reset(prv);

    m_Preview->setImage(aImgFrmt,aImg);
}


HWND SAL_CALL CPreviewAdapterImpl::findFileListbox() const
{
    // try to get a handle to the filelistbox
    // if there is no new-style filelistbox like
    // in Win2000/XP there should be at least the
    // old listbox, so we take this one
    // lst1 - identifies the old-style filelistbox
    // lst2 - identifies the new-style filelistbox
    // see dlgs.h
    HWND flb = GetDlgItem(m_FileDialog,lst2);
    if (!IsWindow(flb))
        flb = GetDlgItem(m_FileDialog,lst1);

    return flb;
}


// Implementation for Windows 95/NT/ME/2000/XP
// because:


class CWin95NTPreviewAdapterImpl : public CPreviewAdapterImpl
{
public:
    explicit CWin95NTPreviewAdapterImpl(HINSTANCE instance);

    virtual void SAL_CALL notifyParentShow(sal_Bool bShow);
};


CWin95NTPreviewAdapterImpl::CWin95NTPreviewAdapterImpl(HINSTANCE instance) :
    CPreviewAdapterImpl(instance)
{
}


void SAL_CALL CWin95NTPreviewAdapterImpl::notifyParentShow(sal_Bool bShow)
{
    try
    {
        if (bShow)
        {
            initializeActivePreview();
            rearrangeLayout();
        }
    }
    catch(std::runtime_error&)
    {
    }
}


// ctor


CPreviewAdapter::CPreviewAdapter(HINSTANCE instance)
{
    m_pImpl.reset(new CWin95NTPreviewAdapterImpl(instance));
}


CPreviewAdapter::~CPreviewAdapter()
{
}


Sequence<sal_Int16> SAL_CALL CPreviewAdapter::getSupportedImageFormats()
{
    css::uno::Sequence<sal_Int16> imgFormats(1);
    imgFormats[0] = css::ui::dialogs::FilePreviewImageFormats::BITMAP;
    return imgFormats;
}


sal_Int32 SAL_CALL CPreviewAdapter::getTargetColorDepth()
{
    return m_pImpl->getTargetColorDepth();
}


sal_Int32 SAL_CALL CPreviewAdapter::getAvailableWidth()
{
    return m_pImpl->getAvailableWidth();
}


sal_Int32 SAL_CALL CPreviewAdapter::getAvailableHeight()
{
    return m_pImpl->getAvailableHeight();
}


void SAL_CALL CPreviewAdapter::setImage( sal_Int16 aImageFormat, const Any& aImage )
    throw (IllegalArgumentException, RuntimeException)
{
    m_pImpl->setImage(aImageFormat,aImage);
}


sal_Bool SAL_CALL CPreviewAdapter::setShowState( sal_Bool bShowState )
{
    return m_pImpl->setShowState(bShowState);
}


sal_Bool SAL_CALL CPreviewAdapter::getShowState()
{
    return m_pImpl->getShowState();
}


void SAL_CALL CPreviewAdapter::setParent(HWND parent)
{
    m_pImpl->setParent(parent);
}


void SAL_CALL CPreviewAdapter::notifyParentShow(bool bShow)
{
    m_pImpl->notifyParentShow(bShow);
}


void SAL_CALL CPreviewAdapter::notifyParentSizeChanged()
{
    m_pImpl->notifyParentSizeChanged();
}


void SAL_CALL CPreviewAdapter::notifyParentWindowPosChanged()
{
    m_pImpl->notifyParentWindowPosChanged();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */