summaryrefslogtreecommitdiffstats
path: root/kword/KWAnchor.cpp
blob: 11136e3d87bdbd84e9fe355abfb55b9e46162bd1 (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
/* This file is part of the KDE project
   Copyright (C) 2001 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KWAnchor.h"
#include "KWTextFrameSet.h"
#include "KWDocument.h"
#include "KWViewMode.h"
#include "KWView.h"
#include <KoXmlWriter.h>
#include <kdebug.h>

//#define DEBUG_DRAWING

KWAnchor::KWAnchor( KoTextDocument *textDocument, KWFrameSet * frameset, int frameNum )
    : KoTextCustomItem( textDocument),
      m_frameset( frameset ),
      m_frameNum( frameNum )
{
}

KWAnchor::~KWAnchor()
{
    kdDebug(32001) << "KWAnchor::~KWAnchor" << endl;
}

void KWAnchor::setFormat( KoTextFormat* format )
{
    m_frameset->setAnchorFormat( format, m_frameNum );
}

void KWAnchor::finalize()
{
    if ( m_deleted )
        return;

    int paragx = paragraph()->rect().x();
    int paragy = paragraph()->rect().y();
    kdDebug(32001) << this << " KWAnchor::finalize " << x() << "," << y() << " paragx=" << paragx << " paragy=" << paragy << endl;

    KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
    KoPoint dPoint;
    if ( fs->internalToDocument( TQPoint( x()+paragx, y()+paragy ), dPoint ) )
    {
        //kdDebug(32001) << "KWAnchor::finalize moving frame to " << dPoint.x() << "," << dPoint.y() << endl;
        // Move the frame to position dPoint.
        m_frameset->moveFloatingFrame( m_frameNum, dPoint );
    } else
    {
        // This can happen if the page hasn't been created yet
        kdDebug(32001) << "KWAnchor::move internalToDocument returned 0L for " << x()+paragx << ", " << y()+paragy << endl;
    }
}

void KWAnchor::draw( TQPainter* p, int x, int y, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected )
{
    // (x,y) is the position of the inline item (in Layout Units)
    // (cx,cy,cw,ch) is the rectangle to be painted, in tqlayout units too

    if ( m_deleted )
        return;

    Q_ASSERT( x == xpos );
    Q_ASSERT( y == ypos );
    if ( x != xpos || y != ypos )
        kdDebug() << "Warning: x=" << x << " y=" << y << " xpos=" << xpos << " ypos=" << ypos << endl;

    // The containing text-frameset.
    KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
    KoTextZoomHandler* zh = fs->textDocument()->paintingZoomHandler();

    int paragx = paragraph()->rect().x();
    int paragy = paragraph()->rect().y();
    TQRect inlineFrameLU( paragx+xpos, paragy+ypos, width, height );
#ifdef DEBUG_DRAWING
    kdDebug(32001) << "KWAnchor::draw x:" << x << ", y:" << y << " paragx=" << paragx << " paragy=" << paragy << endl;
    kdDebug(32001) << "               inline frame in LU coordinates: " << inlineFrameLU << endl;
#endif

    TQRect crectLU = TQRect( (cx > 0 ? cx : 0)+paragx, cy+paragy, cw, ch );
#ifdef DEBUG_DRAWING
    kdDebug(32001) << "               crect in LU coordinates: " << DEBUGRECT( crectLU ) << endl;
#endif

    crectLU = crectLU.intersect ( inlineFrameLU ); // KoTextParag::paintDefault could even do this


#ifdef DEBUG_DRAWING
    kdDebug(32001) << "               crect&frame in LU coordinates: " << DEBUGRECT( crectLU ) << endl;
#endif

    // Convert crect to document coordinates, first topleft then bottomright
    TQPoint topLeftLU = crectLU.topLeft();
    TQPoint bottomRightLU = crectLU.bottomRight();
    KWFrame* containingFrame = fs->currentDrawnFrame(); // always set, except in the textviewmode
    if(containingFrame)
        containingFrame = KWFrameSet::settingsFrame(containingFrame);
    else { // if its not set (in textviewmode) try to get it from the FS
        TQPoint paragPos = inlineFrameLU.topLeft();
        KoPoint dummy(0, 0);
        containingFrame = fs->internalToDocument(paragPos, dummy);
    }
    KoPoint topLeftPt = fs->internalToDocumentKnowingFrame( topLeftLU, containingFrame );

    // Now we can convert the bottomright
    KoPoint bottomRightPt = fs->internalToDocumentKnowingFrame( bottomRightLU, containingFrame );
    KoRect crectPt( topLeftPt, bottomRightPt );

    // Convert crect to view coords
    TQRect crect = fs->currentViewMode()->normalToView( zh->zoomRect( crectPt ) );
    // and add 1 to right and bottom, to avoid rounding errors (and due to qrect semantics)
    crect.rBottom() += 2; // HACK: 1 doesn't do it, it leaves a white line along window borders
    crect.rRight() += 1;
#ifdef DEBUG_DRAWING
    kdDebug() << "               crect in view coordinates (pixel) : " << DEBUGRECT( crect ) << endl;
#endif

    // Ok, we finally have our crect in view coordinates!
    // Now ensure the containing frame is the one actually containing our text
    // (for copies, e.g. headers and footers, we need to go back until finding a real frame)

    if ( containingFrame && containingFrame->isCopy() )
    {
        // Find last real frame, in case we are in a copied frame
        TQPtrListIterator<KWFrame> frameIt( fs->frameIterator() );
        frameIt.toLast(); // from the end to avoid a 2*N in the worst case
        while ( !frameIt.atFirst() && frameIt.current() != containingFrame ) // look for 'containingFrame'
            --frameIt;
        if ( frameIt.atFirst() && frameIt.current() != containingFrame )
            kdWarning() << "KWAnchor::draw: containingFrame not found " << containingFrame << endl;
        while ( !frameIt.atFirst() && frameIt.current()->isCopy() ) // go back to last non-copy
            --frameIt;
        containingFrame = frameIt.current();
        //kdDebug() << "KWAnchor::draw frame=" << containingFrame << endl;
    }

    // Same calculation as in internalToDocument, but we know the frame already
    KoPoint topLeftParagPt( 0, 0 );
    if ( containingFrame ) // 0 in the textviewmode
        topLeftParagPt = containingFrame->innerRect().topLeft();

    topLeftParagPt.rx() += zh->layoutUnitPtToPt( zh->pixelYToPt( paragx ) );
    topLeftParagPt.ry() += zh->layoutUnitPtToPt( zh->pixelYToPt( paragy ) );
    if ( containingFrame ) // 0 in the textviewmode
        topLeftParagPt.ry() -= containingFrame->internalY();

    TQPoint topLeftParag = fs->currentViewMode()->normalToView( zh->zoomPoint( topLeftParagPt ) );

    // Finally, make the painter go back to view coord system
    // (this is exactly the opposite of the code in KWFrameSet::drawContents)
    // (It does translate(view - internal), so we do translate(internal - view) - e.g. with (0,0) for internal)
    p->save();
    p->translate( -topLeftParag.x(), -topLeftParag.y() );
#ifdef DEBUG_DRAWING
    kdDebug() << "               translating by " << -topLeftParag.x() << "," << -topLeftParag.y() << endl;
#endif

    TQColorGroup cg2( cg );

    KWFrameViewManager *fvm = 0;
    if(m_frameset->kWordDocument()) {
        TQValueList<KWView *> views = m_frameset->kWordDocument()->getAllViews();
        // Note that "views" is empty when the KWDocument is an (inactive) embedded document
        if ( !views.isEmpty() )
            fvm = views.first()->frameViewManager();
    }
    m_frameset->drawContents( p, crect, cg2, false, true, 0L, fs->currentViewMode(), fvm);

    if( selected && placement() == PlaceInline && p->device()->devType() != TQInternal::Printer )
    {
        // The above rects are about the containing frame.
        // To draw the inline frame as selected, we need to look at the inline frame's own size.
        TQRect frameRect = crect;
#ifdef DEBUG_DRAWING
        kdDebug() << "KWAnchor::draw selected frame. frameRect=" << frameRect << endl;
#endif
        p->fillRect( frameRect, TQBrush( cg.highlight(), TQBrush::Dense4Pattern) );
    }
    p->restore();

#ifdef DEBUG_DRAWING
    kdDebug() << "KWAnchor::draw done" << endl;
#endif
}

TQSize KWAnchor::size() const
{
    KoSize kosz = m_frameset->floatingFrameSize( m_frameNum );
    //kdDebug() << "KWAnchor::size in pt: " << kosz.width() << "x" << kosz.height() << endl;
    KoTextZoomHandler * zh = textDocument()->formattingZoomHandler();
    TQSize sz( zh->ptToLayoutUnitPixX( kosz.width() ), zh->ptToLayoutUnitPixY( kosz.height() ) );
    //kdDebug() << "KWAnchor::size in LU: " << sz.width() << "x" << sz.height() << endl;
    //kdDebug() << "          size in pixels: " << zh->layoutUnitToPixelX( sz.width() ) << "x"
    //          << zh->layoutUnitToPixelY( sz.height() ) << endl;
    if ( sz.isNull() ) // for some reason, we don't know the size yet
        sz = TQSize( width, height ); // LU
    return sz;
}

int KWAnchor::ascent() const
{
    int baseline = m_frameset->floatingFrameBaseline( m_frameNum );
    int ret = ( baseline == -1 ) ? height : baseline;
    //kdDebug() << "KWAnchor::ascent " << ret << endl;
    return ret;
}

void KWAnchor::resize()
{
    if ( m_deleted )
        return;
    TQSize s = size();
    if ( width != s.width() || height != s.height() )
    {
        width = s.width();
        height = s.height();
        kdDebug(32001) << "KWAnchor::resize " << width << "x" << height << endl;
        KoTextParag * parag = paragraph();
        if ( parag )
        {
            kdDebug(32001) << "KWAnchor::resize invalidating parag " << parag->paragId() << endl;
            parag->tqinvalidate( 0 );
        }
    }
}

KCommand * KWAnchor::createCommand()
{
    kdDebug(32001) << "KWAnchor::addCreateCommand" << endl;
    return m_frameset->anchoredObjectCreateCommand( m_frameNum );
}

KCommand * KWAnchor::deleteCommand()
{
    kdDebug(32001) << "KWAnchor::addDeleteCommand" << endl;
    return m_frameset->anchoredObjectDeleteCommand( m_frameNum );
}

void KWAnchor::setDeleted( bool b )
{
    // Do this first, because setAnchored->updateAllFrames->isDeleted, so it must have the right value already
    KoTextCustomItem::setDeleted( b );

    kdDebug() << "KWAnchor::setDeleted " << b << endl;
    if ( b )
        m_frameset->setAnchored( 0L );
    else
        m_frameset->setAnchored( static_cast<KWTextDocument *>(textDocument())->textFrameSet() );
}

void KWAnchor::save( TQDomElement &parentElem )
{
    TQDomElement anchorElem = parentElem.ownerDocument().createElement( "ANCHOR" );
    parentElem.appendChild( anchorElem );
    anchorElem.setAttribute( "type", "frameset" ); // the only possible value currently
    //KWDocument * doc = textDocument()->textFrameSet()->kWordDocument();
    // ## TODO save the frame number as well ? Only the first frame ? to be determined
    // ## or maybe use len=<number of frames>. Difficult :}
    anchorElem.setAttribute( "instance", m_frameset->name() );
}

void KWAnchor::saveOasis( KoXmlWriter& writer, KoSavingContext& context ) const
{
    if ( m_frameset->canBeSavedAsInlineCharacter() )
        m_frameset->saveOasis( writer, context, true );
    else // special case for inline tables [which are not alone in their paragraph, see KWTextParag]
    {
        writer.startElement( "draw:frame" );
        writer.addAttribute( "draw:name", m_frameset->name() + "-Wrapper" );
        // Mark as wrapper frame. KWTextDocument::loadSpanTag will try to get rid of it upon loading.
        writer.addAttribute( "koffice:is-wrapper-frame", "true" );
        //writer.addAttribute( "draw:style-name", saveOasisFrameStyle( mainStyles ) );
        KoSize kosz = m_frameset->floatingFrameSize( m_frameNum );
        writer.addAttributePt( "svg:width", kosz.width() );
        writer.addAttributePt( "svg:height", kosz.height() );
        writer.startElement( "draw:text-box" );
        m_frameset->saveOasis( writer, context, true );
        writer.endElement();
        writer.endElement();
    }
}

bool KWAnchor::ownLine() const
{
    if ( m_deleted )
        return FALSE;

    if ( m_frameset)
        return m_frameset->ownLine();
    return FALSE;
}