summaryrefslogtreecommitdiff
path: root/sfx2/source/control/templateview.cxx
blob: bc13853cf48a18e647a6571ea069d2bc3a0d4bc2 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright 2012 LibreOffice contributors.
 *
 * 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/.
 */

#include <sfx2/templateview.hxx>

#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <basegfx/point/b2dpoint.hxx>
#include <basegfx/range/b2drange.hxx>
#include <basegfx/vector/b2dvector.hxx>
#include <drawinglayer/attribute/fillbitmapattribute.hxx>
#include <drawinglayer/primitive2d/fillbitmapprimitive2d.hxx>
#include <drawinglayer/primitive2d/textlayoutdevice.hxx>
#include <drawinglayer/primitive2d/textprimitive2d.hxx>
#include <drawinglayer/processor2d/baseprocessor2d.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfx2/templateviewitem.hxx>
#include <vcl/edit.hxx>

#include "templateview.hrc"

#define EDIT_HEIGHT 20

using namespace basegfx;
using namespace basegfx::tools;
using namespace drawinglayer::attribute;
using namespace drawinglayer::primitive2d;

TemplateView::TemplateView (Window *pParent)
    : ThumbnailView(pParent,WB_VSCROLL),
      maCloseImg(SfxResId(IMG_TEMPLATE_VIEW_CLOSE)),
      mbRenderTitle(true),
      mnId(0),
      mpEditName(new Edit(this, WB_BORDER | WB_HIDE))
{
    mnHeaderHeight = 30;
}

TemplateView::~TemplateView ()
{
    delete mpEditName;
}

void TemplateView::setName (const rtl::OUString &rName)
{
    maName = rName;
    mpEditName->SetText(rtl::OUString());
}

void TemplateView::Paint (const Rectangle &rRect)
{
    ThumbnailView::Paint(rRect);

    int nCount = 0;
    int nMaxCount = 1;

    if (mbRenderTitle)
        ++nMaxCount;

    Primitive2DSequence aSeq(nMaxCount);
    TextLayouterDevice aTextDev;

    // Draw centered region name
    Point aPos;
    Size aWinSize = GetOutputSizePixel();

    if (mbRenderTitle)
    {
        aPos.X() = (aWinSize.getWidth() - aTextDev.getTextWidth(maName,0,maName.getLength()))/2;
        aPos.Y() = aTextDev.getTextHeight() + (mnHeaderHeight - aTextDev.getTextHeight())/2;

        basegfx::B2DHomMatrix aTextMatrix( createScaleTranslateB2DHomMatrix(
                    mpItemAttrs->aFontSize.getX(), mpItemAttrs->aFontSize.getY(),
                    double( aPos.X() ), double( aPos.Y() ) ) );

        aSeq[nCount++] = Primitive2DReference(
                    new TextSimplePortionPrimitive2D(aTextMatrix,
                                                     maName,0,maName.getLength(),
                                                     std::vector< double >( ),
                                                     mpItemAttrs->aFontAttr,
                                                     com::sun::star::lang::Locale(),
                                                     Color(COL_BLACK).getBColor() ) );
    }

    // Draw close icon
    Size aImageSize = maCloseImg.GetSizePixel();

    aPos.Y() = (mnHeaderHeight - aImageSize.Height())/2;
    aPos.X() = aWinSize.Width() - aImageSize.Width() - aPos.Y();

    aSeq[nCount] = Primitive2DReference( new FillBitmapPrimitive2D(
                                        createTranslateB2DHomMatrix(aPos.X(),aPos.Y()),
                                        FillBitmapAttribute(maCloseImg.GetBitmapEx(),
                                                            B2DPoint(0,0),
                                                            B2DVector(aImageSize.Width(),aImageSize.Height()),
                                                            false)
                                        ));

    mpProcessor->process(aSeq);
}

void TemplateView::InsertItems (const std::vector<TemplateItemProperties> &rTemplates)
{
    for (size_t i = 0, n = rTemplates.size(); i < n; ++i )
    {
        TemplateViewItem *pItem = new TemplateViewItem(*this,this);
        const TemplateItemProperties *pCur = &rTemplates[i];

        pItem->mnId = pCur->nId;
        pItem->maTitle = pCur->aName;
        pItem->setPath(pCur->aPath);
        pItem->setFileType(pCur->aType);
        pItem->maPreview1 = pCur->aThumbnail;
        pItem->setSelectClickHdl(LINK(this,ThumbnailView,OnItemSelected));

        mItemList.push_back(pItem);
    }

    CalculateItemPositions();

    Invalidate();
}

void TemplateView::Resize()
{
    // Set editbox size and position
    Size aWinSize = GetOutputSize();

    Size aEditSize(aWinSize.getWidth()/2,EDIT_HEIGHT);

    Point aPos;
    aPos.X() = (aWinSize.getWidth() - aEditSize.getWidth())/2;
    aPos.Y() = (mnHeaderHeight - aEditSize.getHeight())/2;

    mpEditName->SetPosSizePixel(aPos,aEditSize);

    ThumbnailView::Resize();
}

void TemplateView::MouseButtonDown (const MouseEvent &rMEvt)
{
    if (rMEvt.IsLeft())
    {
        // Check if we are editing title
        if (mpEditName->IsVisible())
        {
            mpEditName->Show(false);
            mbRenderTitle = true;

            // Update name if its not empty
            rtl::OUString aTmp = mpEditName->GetText();

            if (!aTmp.isEmpty())
            {
                PostUserEvent(LINK(this,TemplateView,ChangeNameHdl));
            }
            else
            {
                mpEditName->SetText(rtl::OUString());
                Invalidate();
            }

            return;
        }

        Size aWinSize = GetOutputSizePixel();
        Size aImageSize = maCloseImg.GetSizePixel();

        Point aPos;
        aPos.Y() = (mnHeaderHeight - aImageSize.Height())/2;
        aPos.X() = aWinSize.Width() - aImageSize.Width() - aPos.Y();

        Rectangle aImgRect(aPos,aImageSize);

        if (aImgRect.IsInside(rMEvt.GetPosPixel()))
        {
            maCloseHdl.Call(this);
        }
        else
        {
            drawinglayer::primitive2d::TextLayouterDevice aTextDev;

            float fTextWidth = aTextDev.getTextWidth(maName,0,maName.getLength());

            aPos.X() = (aWinSize.getWidth() - fTextWidth)/2;
            aPos.Y() = (mnHeaderHeight - aTextDev.getTextHeight())/2;

            Rectangle aTitleRect(aPos,Size(fTextWidth,aTextDev.getTextHeight()));

            if (aTitleRect.IsInside(rMEvt.GetPosPixel()))
            {
                mbRenderTitle = false;

                Invalidate();
                mpEditName->Show();
            }
        }
    }

    ThumbnailView::MouseButtonDown(rMEvt);
}

void TemplateView::OnItemDblClicked(ThumbnailViewItem *pItem)
{
    maDblClickHdl.Call(pItem);
}

IMPL_LINK_NOARG(TemplateView, ChangeNameHdl)
{
    rtl::OUString aTmp = maName;
    maName = mpEditName->GetText();

    if (!maChangeNameHdl.Call(this))
        maName = aTmp;

    mpEditName->SetText(rtl::OUString());

    Invalidate();
    return 0;
}

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