summaryrefslogtreecommitdiffstats
path: root/src/multibuffer.cpp
blob: fe988c02134cf8d721117ba388cc91866d261091 (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
/*
 * KDevelop Multiple Buffer Support
 *
 * Copyright (c) 2005 Adam Treat <treat@kde.org>
 *
 * This program 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 program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

// Ewww... need this to access KParts::Part::setWidget(), so that tdevelop
// doesn't need to be rearchitected for multiple views before the lazy view
// creation can go in
#define protected public
#include <tdeparts/part.h>
#undef protected

#include "multibuffer.h"

#include "api.h"
#include "toplevel.h"
#include "editorproxy.h"
#include "partcontroller.h"
#include "kdevlanguagesupport.h"

#include <kdebug.h>
#include <kmimetype.h>
#include <tdemainwindow.h>
#include <tdeapplication.h>

#include <tdeparts/factory.h>

#include <tdetexteditor/view.h>
#include <tdetexteditor/document.h>
#include <tdetexteditor/viewcursorinterface.h>

MultiBuffer::MultiBuffer( TQWidget *parent )
        : TQSplitter( parent, "MultiBuffer" ),
        m_editorFactory( 0 ),
        m_delayActivate( false ),
        m_activated( false ),
        m_activeBuffer( 0 )
{
    EditorProxy::getInstance() ->registerEditor( this );
    if ( KDevLanguageSupport *lang =
         API::getInstance() ->languageSupport() )
    {
        setOrientation( lang->splitOrientation() );
        connect( lang, TQ_SIGNAL( splitOrientationChanged( TQt::Orientation ) ),
                 this, TQ_SLOT( setOrientation( TQt::Orientation ) ) );
    }
    else
    {
        setOrientation( TQt::Vertical );
    }
}

MultiBuffer::~MultiBuffer()
{
    EditorProxy::getInstance() ->deregisterEditor( this );
}

KParts::Part *MultiBuffer::activeBuffer( ) const
{
    if ( m_activeBuffer )
        return m_activeBuffer;
    // The active buffer might just been deleted...
    else if ( m_buffers.begin().data() )
        return ( m_buffers.begin().data() );
    else
        return 0;
}

bool MultiBuffer::hasURL( const KURL &url ) const
{
    return m_buffers.contains( url );
}

int MultiBuffer::numberOfBuffers() const
{
    return m_buffers.count();
}

bool MultiBuffer::isActivated() const
{
    if ( m_delayActivate )
        return m_activated;
    else
        return true;
}

void MultiBuffer::setDelayedActivation( bool delayed )
{
    m_delayActivate = delayed;
    if ( delayed )
        m_activated = false;
}

KParts::Part* MultiBuffer::openURL( const KURL &url )
{
    KParts::ReadOnlyPart * part =
        dynamic_cast<KParts::ReadOnlyPart*>( createPart( url ) );

    if ( !part )
        return 0;

    if ( !part->openURL( url ) )
        return 0;

    m_buffers.insert( url, part );
    return part;
}

bool MultiBuffer::closeURL( const KURL &url )
{
    if ( !m_buffers.contains( url ) )
        return false;

    bool result = false;
    KParts::ReadOnlyPart * part =
        dynamic_cast<KParts::ReadOnlyPart *>( m_buffers[ url ] );
    if ( part )
        if (part->closeURL())
        {
            m_buffers.remove( url );
            return true;
        }
    return false;
}

void MultiBuffer::registerURL( const KURL &url, KParts::Part *part )
{
    m_buffers.insert( url, part );
}

void MultiBuffer::registerDelayedActivation( KParts::Part *part,
        int line, int col )
{
    m_delayedActivation[ part ] = qMakePair( line, col );
}

KParts::Part* MultiBuffer::createPart( const TQString &mimeType,
                                       const TQString &partType,
                                       const TQString &className,
                                       const TQString &preferredName )
{
    m_editorFactory = PartController::getInstance() ->findPartFactory(
                          mimeType, partType, preferredName );

    if ( !className.isEmpty() && m_editorFactory )
    {
        return m_editorFactory->createPart(
                   this, 0, 0, 0, className.latin1() );
    }

    return 0;
}

KParts::Part* MultiBuffer::createPart( const KURL &url )
{
    if ( !url.isValid() )
        return 0;

    KMimeType::Ptr mimeType = KMimeType::findByURL( url );

    TQString className;
    TQString services[] = { "KParts/ReadWritePart", "KParts/ReadOnlyPart" };
    TQString classnames[] = { "KParts::ReadWritePart", "KParts::ReadOnlyPart" };
    for ( uint i = 0; i < 2; ++i )
    {
        m_editorFactory = PartController::getInstance() ->findPartFactory(
                              mimeType->name(), services[ i ] );
        if ( m_editorFactory )
        {
            className = classnames[ i ];
            break;
        }
    }

    if ( !className.isEmpty() && m_editorFactory )
    {
        return m_editorFactory->createPart(
                   this, 0, 0, 0, className.latin1() );
    }

    return 0;
}

void MultiBuffer::show()
{
    if ( !m_delayedActivation.count() || m_activated )
    {
        TQSplitter::show();
        return ;
    }

    ActivationMap::Iterator it = m_delayedActivation.begin();
    for ( ; it != m_delayedActivation.end(); ++it )
    {
        KTextEditor::Document *document =
            dynamic_cast<KTextEditor::Document*>( it.key() );

        if ( !document )
            continue;

        int line = it.data().first;
        int column = it.data().second;
        KTextEditor::View *view = document->createView( this );
        document->setWidget( view );

        // We're managing the view deletion by being its parent,
        // don't let the part self-destruct
        disconnect( view, TQ_SIGNAL( destroyed() ),
                    document, TQ_SLOT( slotWidgetDestroyed() ) );

        document->insertChildClient( view );
        PartController::getInstance() ->integrateTextEditorPart( document );

        KTextEditor::ViewCursorInterface *iface =
            dynamic_cast<KTextEditor::ViewCursorInterface*>(
                static_cast<KTextEditor::View*>( view ) );
        if ( iface )
        {
            iface->setCursorPositionReal( line,
                                          column == -1 ? 0 : column );
        }
        else
        {
            // Shouldn't get here
            Q_ASSERT( false );
        }
        view->show();
        kdDebug( 9000 ) << "Delayed activation of "
        << document->url().fileName() << " is now complete." << endl;
    }

    m_activated = true;
    TQSplitter::show();
}

void MultiBuffer::setOrientation( TQt::Orientation orientation )
{
    TQSplitter::setOrientation( orientation );
}

void MultiBuffer::activePartChanged( const KURL &url )
{
    if ( !m_buffers.contains( url ) )
        return ;

    m_activeBuffer = m_buffers[ url ];
    TopLevel::getInstance() ->setCurrentDocumentCaption( url.fileName() );
}

void MultiBuffer::focusInEvent( TQFocusEvent *ev )
{
    KParts::Part *active = activeBuffer();
    if (active && active->widget())
	active->widget()->setFocus();
    TQSplitter::focusInEvent(ev);
}

bool MultiBuffer::hasPart( KParts::Part *part )
{
    for (BufferMap::iterator it = m_buffers.begin(); it != m_buffers.end(); ++it)
    {
        if (it.data() == part)
            return true;
    }
    return false;
}

void MultiBuffer::updateUrlForPart(KParts::Part *part, KURL url)
{
    if (!url.isValid())
        return;
    KURL formerURL;
    for (BufferMap::iterator it = m_buffers.begin(); it != m_buffers.end(); ++it)
    {
        if (it.data() == part)
        {
            formerURL = it.key();
            break;
        }
    }
    m_buffers.remove(formerURL);
    m_buffers.insert(url, part);
}

#include "multibuffer.moc"