summaryrefslogtreecommitdiffstats
path: root/ksirc/mditoplevel.cpp
blob: dfad0c5c7cd66f6f46cab2379c1333eb7d2ba4ee (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

#include <tqguardedptr.h>

#include <tqtabbar.h>

#include <tdeapplication.h>
#include <kiconloader.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdepopupmenu.h>
#include <tdelocale.h>
#include <tdeaction.h>

#include "mditoplevel.h"
#include "toplevel.h"
#include "servercontroller.h"

#define ID_CLOSE 5

void KSTabWidget::mousePressEvent(TQMouseEvent *e)
{
    if(e->button() == Qt::RightButton){
        TQPoint p = tabBar()->mapFromParent(e->pos());
	TQTab *tab = tabBar()->selectTab(p);
	if(tab){
	    int id = tab->identifier();
            emit showContexMenu(page(id), tabBar()->mapToGlobal(p));
	}
    }
}

MDITopLevel::MDITopLevel(TQWidget *parent, const char *name)
    : TDEMainWindow(parent, name)
{
    m_closing = false;

    m_tab = new KSTabWidget( this );
    m_tab->setTabPosition( TQTabWidget::Bottom );

    setCentralWidget( m_tab );

    connect( m_tab, TQT_SIGNAL( currentChanged( TQWidget * ) ),
             this, TQT_SLOT( slotCurrentChanged( TQWidget * ) ) );

    connect( m_tab, TQT_SIGNAL( showContexMenu(TQWidget *, const TQPoint &) ),
             this, TQT_SLOT( slotShowContexMenu(TQWidget *, const TQPoint &) ) );

    TDEConfig *config = kapp->config();
    config->setGroup("MDI");
    TQSize s( 600, 360 );
    resize(config->readSizeEntry( "TopLevelSize", &s ));

    m_dirtyIcon = UserIcon( "star" );
    m_addressedIcon = UserIcon( "application-vnd.tde.info" );

    m_pop = new TDEPopupMenu(m_tab, "");
    m_pop->insertItem( SmallIcon("window-close"), i18n("Close"), this, TQT_SLOT( slotCloseLastWid() ));

}

MDITopLevel::~MDITopLevel()
{
  kdDebug(5008) << "~MDITopLevel in" << endl;

    TDEConfig *config = kapp->config();
    config->setGroup( "MDI" );
    config->writeEntry( "TopLevelSize", this->size() );
    config->sync();

    TQPtrListIterator<TQWidget> it( m_tabWidgets );
    for (; it.current(); ++it )
        it.current()->disconnect( this, 0 );
  kdDebug(5008) << "~MDITopLevel out" << endl;

}

void MDITopLevel::addWidget( TQWidget *widget, bool show )
{
    if ( m_tabWidgets.containsRef( widget ) )
        return;

    kdDebug(5008) << "In add widget" << endl;

    widget->reparent( m_tab, 0, TQPoint( 0, 0 ), show );

    if(show == TRUE){
        showWidget(widget);
    }

    m_tabWidgets.append( widget );

    connect( widget, TQT_SIGNAL( destroyed() ) ,
             this, TQT_SLOT( slotWidgetDestroyed() ) );

    connect( widget, TQT_SIGNAL( changeChannel( const TQString &, const TQString & ) ),
             this, TQT_SLOT( slotChangeChannelName( const TQString &, const TQString & ) ) );

    widget->installEventFilter( this );

    connect( widget, TQT_SIGNAL( changed( bool, TQString ) ),
             this, TQT_SLOT( slotMarkPageDirty( bool ) ) );
}

void MDITopLevel::removeWidget( TQWidget *widget )
{
    if (m_closing)
        return;
    m_tabWidgets.removeRef( widget );
    removeFromAddressedList( widget );
    m_tab->removePage( widget );
    widget->removeEventFilter( this );
    widget->disconnect( this, 0 );
}

void MDITopLevel::hideWidget( TQWidget *widget )
{
    m_tab->removePage( widget );
    widget->hide();
}

void MDITopLevel::showWidget( TQWidget *widget )
{
    if(m_tab->indexOf(widget) == -1){
        int space = widget->caption().find(" ");
        TQString cap = space < 1 ? widget->caption():widget->caption().left(space);
        m_tab->addTab( widget,  cap);
        m_tab->showPage( widget );
        m_tab->setCurrentPage( m_tab->indexOf(widget) );
    }
}

void MDITopLevel::next()
{
	if (m_tab->currentPageIndex() < m_tab->count() - 1)
		m_tab->setCurrentPage(m_tab->currentPageIndex() + 1);
	else
		m_tab->setCurrentPage(0);
}

void MDITopLevel::previous()
{
	if (m_tab->currentPageIndex() > 0)
		m_tab->setCurrentPage(m_tab->currentPageIndex() - 1);
	else
		m_tab->setCurrentPage(m_tab->count() - 1);
}

void MDITopLevel::closeEvent( TQCloseEvent *ev )
{
    m_closing = true;
    // Don't use iterators on a list while deleting elements
    // from it (Antonio)
    int i = 0;
    kdDebug(5008) << "Mdi got close event " << endl;
    while ( m_tabWidgets.count() && (i++ < 100000)) {
        kdDebug(5008) << "MDITopLevel trying to close: " << m_tabWidgets.first()->name() << endl;
        TQGuardedPtr<TQWidget> w = m_tabWidgets.take(0);
        w->show();
        w->close( false );
        if(w)
            delete (TQWidget *)w;
    }

    TDEMainWindow::closeEvent( ev );
    m_closing = false;
}

void MDITopLevel::slotWidgetDestroyed()
{
    const TQWidget *widget = static_cast<const TQWidget *>( sender() );

    m_tabWidgets.removeRef( widget );
    removeFromAddressedList( widget );
}

bool MDITopLevel::eventFilter( TQObject *obj, TQEvent *ev )
{
    if ( ev->type() != TQEvent::CaptionChange )
        return false;

    TQWidget *widget = dynamic_cast<TQWidget *>( obj );

    if ( !widget || !m_tabWidgets.containsRef( widget ) )
        return false;

    if ( m_tab->currentPage() == widget )
        setPlainCaption( widget->caption() );

    return false;
}

void MDITopLevel::slotCurrentChanged( TQWidget *page )
{

    m_tab->setTabIconSet( page, TQIconSet() );
    removeFromAddressedList( page );

    setPlainCaption( page->TQWidget::caption() );

    KSircTopLevel *kst = dynamic_cast<KSircTopLevel *>( page );
    if ( !kst )
        return;
    kst->lineEdit()->setFocus();
}

void MDITopLevel::slotMarkPageDirty( bool addressed )
{
    // This is called when a line appeared in this channel.
    // addressed is true if it was addressed to the user
    TDEMainWindow *window = dynamic_cast<TDEMainWindow *>( TQT_TQWIDGET(const_cast<TQT_BASE_OBJECT_NAME *>( sender() )) );

    if ( !window )
        return;

    if ( window != m_tab->currentPage() )
    {
        if ( m_addressed.containsRef( window ) )
            addressed = true;
        else if ( addressed ) {
            m_addressed.append( window );
        }
        m_tab->setTabIconSet( window, addressed ? m_addressedIcon : m_dirtyIcon );
    }
}

void MDITopLevel::slotChangeChannelName( const TQString &, const TQString &channelName )
{
    TDEMainWindow *window = dynamic_cast<TDEMainWindow *>( TQT_TQWIDGET(const_cast<TQT_BASE_OBJECT_NAME *>( sender() )) );

    if ( !window )
        return;

    TQString esc = channelName;
    esc.replace("&", "&&");
    m_tab->setTabLabel( window, esc );
    removeFromAddressedList( window );
}

void MDITopLevel::removeFromAddressedList( const TQWidget* w )
{
    // If this tab was showing a "you're being talked to" icon, remove it
    // and tell the servercontroller (so that it can update the docked icon).
    m_addressed.removeRef( w );
}

void MDITopLevel::slotShowContexMenu(TQWidget *w, const TQPoint &p)
{
    m_last_pop_wid = w;
    m_pop->popup(p);
}

void MDITopLevel::slotCloseLastWid()
{
	m_last_pop_wid->close();

}

#include "mditoplevel.moc"