summaryrefslogtreecommitdiff
path: root/sw/source/uibase/docvw/SidebarTxtControl.cxx
blob: e9ce19243225ceca2964d4d88b7c630095d4768f (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
457
458
459
460
461
462
463
/* -*- 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 "SidebarTxtControl.hxx"

#include <docsh.hxx>
#include <doc.hxx>

#include <PostItMgr.hxx>
#include <edtwin.hxx>

#include <cmdid.h>
#include <strings.hrc>

#include <unotools/securityoptions.hxx>
#include <officecfg/Office/Common.hxx>

#include <sfx2/viewfrm.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/sfxhelp.hxx>

#include <vcl/commandevent.hxx>
#include <vcl/svapp.hxx>
#include <vcl/help.hxx>
#include <vcl/weld.hxx>
#include <vcl/gradient.hxx>
#include <vcl/settings.hxx>

#include <editeng/outliner.hxx>
#include <editeng/editeng.hxx>
#include <editeng/editview.hxx>
#include <editeng/flditem.hxx>
#include <comphelper/lok.hxx>
#include <sfx2/lokhelper.hxx>

#include <uitool.hxx>
#include <view.hxx>
#include <wrtsh.hxx>
#include <AnnotationWin.hxx>
#include <redline.hxx>
#include <memory>

namespace sw::sidebarwindows {

SidebarTextControl::SidebarTextControl(sw::annotation::SwAnnotationWin& rSidebarWin,
                                       SwView& rDocView,
                                       SwPostItMgr& rPostItMgr)
    : mrSidebarWin(rSidebarWin)
    , mrDocView(rDocView)
    , mrPostItMgr(rPostItMgr)
    , mbMouseDownGainingFocus(false)
{
}

EditView* SidebarTextControl::GetEditView() const
{
    OutlinerView* pOutlinerView = mrSidebarWin.GetOutlinerView();
    if (!pOutlinerView)
        return nullptr;
    return &pOutlinerView->GetEditView();
}

EditEngine* SidebarTextControl::GetEditEngine() const
{
    OutlinerView* pOutlinerView = mrSidebarWin.GetOutlinerView();
    if (!pOutlinerView)
        return nullptr;
    return pOutlinerView->GetEditView().GetEditEngine();
}

void SidebarTextControl::SetDrawingArea(weld::DrawingArea* pDrawingArea)
{
    Size aSize(0, 0);
    pDrawingArea->set_size_request(aSize.Width(), aSize.Height());

    SetOutputSizePixel(aSize);

    weld::CustomWidgetController::SetDrawingArea(pDrawingArea);

    EnableRTL(false);

    const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
    Color aBgColor = rStyleSettings.GetWindowColor();

    OutputDevice& rDevice = pDrawingArea->get_ref_device();

    rDevice.SetMapMode(MapMode(MapUnit::MapTwip));
    rDevice.SetBackground(aBgColor);

    Size aOutputSize(rDevice.PixelToLogic(aSize));
    aSize = aOutputSize;
    aSize.setHeight(aSize.Height());

    EditView* pEditView = GetEditView();
    pEditView->setEditViewCallbacks(this);

    EditEngine* pEditEngine = GetEditEngine();
    pEditEngine->SetPaperSize(aSize);
    pEditEngine->SetRefDevice(&rDevice);

    pEditView->SetOutputArea(tools::Rectangle(Point(0, 0), aOutputSize));
    pEditView->SetBackgroundColor(aBgColor);

    pDrawingArea->set_cursor(PointerStyle::Text);

    InitAccessible();
}

void SidebarTextControl::SetCursorLogicPosition(const Point& rPosition, bool bPoint, bool bClearMark)
{
    Point aMousePos = EditViewOutputDevice().PixelToLogic(rPosition);
    m_xEditView->SetCursorLogicPosition(aMousePos, bPoint, bClearMark);
}

void SidebarTextControl::GetFocus()
{
    WeldEditView::GetFocus();
    if ( !mrSidebarWin.IsMouseOver() )
        Invalidate();
    mrSidebarWin.SetActiveSidebarWin();
}

void SidebarTextControl::LoseFocus()
{
    // write the visible text back into the SwField
    mrSidebarWin.UpdateData();

    WeldEditView::LoseFocus();
    if ( !mrSidebarWin.IsMouseOver() )
    {
        Invalidate();
    }
}

OUString SidebarTextControl::RequestHelp(tools::Rectangle& rHelpRect)
{
    if (EditView* pEditView = GetEditView())
    {
        Point aPos = rHelpRect.TopLeft();

        const OutputDevice& rOutDev = pEditView->GetOutputDevice();
        Point aLogicClick = rOutDev.PixelToLogic(aPos);
        const SvxFieldItem* pItem = pEditView->GetField(aLogicClick);
        if (pItem)
        {
            const SvxFieldData* pField = pItem->GetField();
            const SvxURLField* pURL = dynamic_cast<const SvxURLField*>( pField  );
            if (pURL)
            {
                rHelpRect = tools::Rectangle(aPos, Size(50, 10));
                return SfxHelp::GetURLHelpText(pURL->GetURL());
            }
        }
    }

    const char* pResId = nullptr;
    switch( mrSidebarWin.GetLayoutStatus() )
    {
        case SwPostItHelper::INSERTED:  pResId = STR_REDLINE_INSERT; break;
        case SwPostItHelper::DELETED:   pResId = STR_REDLINE_DELETE; break;
        default: pResId = nullptr;
    }

    SwContentAtPos aContentAtPos( IsAttrAtPos::Redline );
    if ( pResId &&
         mrDocView.GetWrtShell().GetContentAtPos( mrSidebarWin.GetAnchorPos(), aContentAtPos ) )
    {
        OUString sText = SwResId(pResId) + ": " +
                        aContentAtPos.aFnd.pRedl->GetAuthorString() + " - " +
                        GetAppLangDateTimeString( aContentAtPos.aFnd.pRedl->GetTimeStamp() );
        return sText;
    }

    return OUString();
}

void SidebarTextControl::EditViewScrollStateChange()
{
    mrSidebarWin.SetScrollbar();
}

void SidebarTextControl::DrawForPage(OutputDevice* pDev, const Point& rPt)
{
    //Take the control's height, but overwrite the scrollbar area if there was one
    OutputDevice& rDevice = GetDrawingArea()->get_ref_device();
    Size aSize(rDevice.PixelToLogic(GetOutputSizePixel()));

    if (OutlinerView* pOutlinerView = mrSidebarWin.GetOutlinerView())
    {
        pOutlinerView->GetOutliner()->Draw(pDev, tools::Rectangle(rPt, aSize));
    }

    if ( mrSidebarWin.GetLayoutStatus()!=SwPostItHelper::DELETED )
        return;

    pDev->Push(PushFlags::LINECOLOR);

    pDev->SetLineColor(mrSidebarWin.GetChangeColor());
    Point aBottomRight(rPt);
    aBottomRight.Move(aSize);
    pDev->DrawLine(rPt,  aBottomRight);

    Point aTopRight(rPt);
    aTopRight.Move(Size(aSize.Width(), 0));

    Point aBottomLeft(rPt);
    aBottomLeft.Move(Size(0, aSize.Height()));

    pDev->DrawLine(aTopRight, aBottomLeft);

    pDev->Pop();
}

void SidebarTextControl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect)
{
    Size aSize = GetOutputSizePixel();
    Point aPos;

    if (!rRenderContext.GetSettings().GetStyleSettings().GetHighContrastMode())
    {
        if (mrSidebarWin.IsMouseOverSidebarWin() || HasFocus())
        {
            rRenderContext.DrawGradient(tools::Rectangle(aPos, rRenderContext.PixelToLogic(aSize)),
                                        Gradient(GradientStyle::Linear, mrSidebarWin.ColorDark(), mrSidebarWin.ColorDark()));
        }
        else
        {
            rRenderContext.DrawGradient(tools::Rectangle(aPos, rRenderContext.PixelToLogic(aSize)),
                           Gradient(GradientStyle::Linear, mrSidebarWin.ColorLight(), mrSidebarWin.ColorDark()));
        }
    }

    DoPaint(rRenderContext, rRect);

    if (mrSidebarWin.GetLayoutStatus() == SwPostItHelper::DELETED)
    {
        const AntialiasingFlags nFormerAntialiasing( rRenderContext.GetAntialiasing() );
        const bool bIsAntiAliasing = officecfg::Office::Common::Drawinglayer::AntiAliasing::get();
        if ( bIsAntiAliasing )
            rRenderContext.SetAntialiasing(AntialiasingFlags::Enable);
        rRenderContext.SetLineColor(mrSidebarWin.GetChangeColor());
        rRenderContext.DrawLine(rRenderContext.PixelToLogic(aPos),
                                rRenderContext.PixelToLogic(aPos + Point(aSize.Width(),
                                                                         aSize.Height() * 0.95)));
        rRenderContext.DrawLine(rRenderContext.PixelToLogic(aPos + Point(aSize.Width(),
                                                                         0)),
                                rRenderContext.PixelToLogic(aPos + Point(0,
                                                                         aSize.Height() * 0.95)));
        if ( bIsAntiAliasing )
            rRenderContext.SetAntialiasing(nFormerAntialiasing);
    }
}

void SidebarTextControl::MakeVisible()
{
    //let's make sure we see our note
    mrPostItMgr.MakeVisible(&mrSidebarWin);
}

bool SidebarTextControl::KeyInput( const KeyEvent& rKeyEvt )
{
    if (getenv("SW_DEBUG") && rKeyEvt.GetKeyCode().GetCode() == KEY_F12)
    {
        if (rKeyEvt.GetKeyCode().IsShift())
        {
            mrDocView.GetDocShell()->GetDoc()->dumpAsXml();
            return true;
        }
    }

    bool bDone = false;

    const vcl::KeyCode& rKeyCode = rKeyEvt.GetKeyCode();
    sal_uInt16 nKey = rKeyCode.GetCode();
    if ( ( rKeyCode.IsMod1() && rKeyCode.IsMod2() ) &&
         ( (nKey == KEY_PAGEUP) || (nKey == KEY_PAGEDOWN) ) )
    {
        mrSidebarWin.SwitchToPostIt(nKey);
        bDone = true;
    }
    else if ( nKey == KEY_ESCAPE ||
              ( rKeyCode.IsMod1() &&
                ( nKey == KEY_PAGEUP ||
                  nKey == KEY_PAGEDOWN ) ) )
    {
        mrSidebarWin.SwitchToFieldPos();
        bDone = true;
    }
    else if ( rKeyCode.GetFullCode() == KEY_INSERT )
    {
        mrSidebarWin.ToggleInsMode();
        bDone = true;
    }
    else
    {
        MakeVisible();

        tools::Long aOldHeight = mrSidebarWin.GetPostItTextHeight();

        /// HACK: need to switch off processing of Undo/Redo in Outliner
        if ( !( (nKey == KEY_Z || nKey == KEY_Y) && rKeyCode.IsMod1()) )
        {
            bool bIsProtected = mrSidebarWin.IsProtected();
            if ( !bIsProtected || !EditEngine::DoesKeyChangeText(rKeyEvt) )
            {
                EditView* pEditView = GetEditView();
                bDone = pEditView && pEditView->PostKeyEvent(rKeyEvt);
            }
            else
            {
                std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(GetDrawingArea(), "modules/swriter/ui/inforeadonlydialog.ui"));
                std::unique_ptr<weld::MessageDialog> xQuery(xBuilder->weld_message_dialog("InfoReadonlyDialog"));
                xQuery->run();
            }
        }
        if (bDone)
            mrSidebarWin.ResizeIfNecessary( aOldHeight, mrSidebarWin.GetPostItTextHeight() );
        else
        {
            // write back data first when showing navigator
            if ( nKey==KEY_F5 )
                mrSidebarWin.UpdateData();
            bDone = mrDocView.KeyInput(rKeyEvt);
        }
    }

    mrDocView.GetViewFrame()->GetBindings().InvalidateAll(false);

    return bDone;
}

bool SidebarTextControl::MouseButtonDown(const MouseEvent& rMEvt)
{
    if (EditView* pEditView = GetEditView())
    {
        SvtSecurityOptions aSecOpts;
        bool bExecuteMod = aSecOpts.IsOptionSet( SvtSecurityOptions::EOption::CtrlClickHyperlink);

        if ( !bExecuteMod || (rMEvt.GetModifier() == KEY_MOD1))
        {
            const OutputDevice& rOutDev = pEditView->GetOutputDevice();
            Point aLogicClick = rOutDev.PixelToLogic(rMEvt.GetPosPixel());
            if (const SvxFieldItem* pItem = pEditView->GetField(aLogicClick))
            {
                const SvxFieldData* pField = pItem->GetField();
                const SvxURLField* pURL = dynamic_cast<const SvxURLField*>( pField  );
                if ( pURL )
                {
                    pEditView->MouseButtonDown( rMEvt );
                    SwWrtShell &rSh = mrDocView.GetWrtShell();
                    const OUString& sURL( pURL->GetURL() );
                    const OUString& sTarget( pURL->GetTargetFrame() );
                    ::LoadURL(rSh, sURL, LoadUrlFlags::NONE, sTarget);
                    return true;
                }
            }
        }
    }

    mbMouseDownGainingFocus = !HasFocus();
    GrabFocus();

    bool bRet = WeldEditView::MouseButtonDown(rMEvt);

    mrDocView.GetViewFrame()->GetBindings().InvalidateAll(false);

    return bRet;
}

bool SidebarTextControl::MouseButtonUp(const MouseEvent& rMEvt)
{
    bool bRet = WeldEditView::MouseButtonUp(rMEvt);

    if (mbMouseDownGainingFocus)
    {
        MakeVisible();
        mbMouseDownGainingFocus = false;
    }

    return bRet;
}

IMPL_LINK( SidebarTextControl, OnlineSpellCallback, SpellCallbackInfo&, rInfo, void )
{
    if ( rInfo.nCommand == SpellCallbackCommand::STARTSPELLDLG )
    {
        mrDocView.GetViewFrame()->GetDispatcher()->Execute( FN_SPELL_GRAMMAR_DIALOG, SfxCallMode::ASYNCHRON);
    }
}

bool SidebarTextControl::Command( const CommandEvent& rCEvt )
{
    EditView* pEditView = GetEditView();

    if ( rCEvt.GetCommand() == CommandEventId::ContextMenu )
    {
        if (IsMouseCaptured())
            ReleaseMouse();
        if ( !mrSidebarWin.IsProtected() &&
             pEditView &&
             pEditView->IsWrongSpelledWordAtPos( rCEvt.GetMousePosPixel(), true ))
        {
            Link<SpellCallbackInfo&,void> aLink = LINK(this, SidebarTextControl, OnlineSpellCallback);
            pEditView->ExecuteSpellPopup(rCEvt.GetMousePosPixel(), aLink);
        }
        else
        {
            Point aPos;
            if (rCEvt.IsMouseEvent())
                aPos = rCEvt.GetMousePosPixel();
            else
            {
                const Size aSize = GetOutputSizePixel();
                aPos = Point( aSize.getWidth()/2, aSize.getHeight()/2 );
            }
            SfxDispatcher::ExecutePopup(&mrSidebarWin, &aPos);
        }
        return true;
    }
    else if (rCEvt.GetCommand() == CommandEventId::Wheel)
    {
        // if no scrollbar, or extra keys held scroll the document and consume
        // this event, otherwise don't consume and let the event get to the
        // surrounding scrolled window
        if (!mrSidebarWin.IsScrollbarVisible())
        {
            mrDocView.HandleWheelCommands(rCEvt);
            return true;
        }
        else
        {
            const CommandWheelData* pData = rCEvt.GetWheelData();
            if (pData->IsShift() || pData->IsMod1() || pData->IsMod2())
            {
                mrDocView.HandleWheelCommands(rCEvt);
                return true;
            }
        }
    }

    return WeldEditView::Command(rCEvt);
}

} // end of namespace sw::sidebarwindows

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