summaryrefslogtreecommitdiffstats
path: root/tdemdi/tdemdi/tabwidget.cpp
blob: 01eb6d889496b796f6eeb6a9bc457b429b0c71cb (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
/* This file is part of the KDE libraries
   Copyright (C) 2004 Christoph Cullmann <cullmann@kde.org>
   Copyright (C) 2002,2003 Joseph Wenninger <jowenn@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.

   Based on:

   //----------------------------------------------------------------------------
   //    Project              : KDE MDI extension
   //
   //    begin                : 07/1999       by Szymon Stefanek as part of kvirc
   //                                         (an IRC application)
   //    changes              : 09/1999       by Falk Brettschneider to create an
   //                           - 06/2000     stand-alone Qt extension set of
   //                                         classes and a Qt-based library
   //                         : 02/2000       by Massimo Morin (mmorin@schedsys.com)
   //                           2000-2003     maintained by the KDevelop project
   //    patches              : -/2000        by Lars Beikirch (Lars.Beikirch@gmx.net)
   //                         : 01/2003       by Jens Zurheide (jens.zurheide@gmx.de)
   //
   //    copyright            : (C) 1999-2003 by Falk Brettschneider
   //                                         and
   //                                         Szymon Stefanek (stefanek@tin.it)
   //    email                :  falkbr@kdevelop.org (Falk Brettschneider)
   //----------------------------------------------------------------------------
*/

#include <ktabbar.h>
#include <tdepopupmenu.h>
#include <kdebug.h>

#include <tqobjectlist.h>

#include "tabwidget.h"
#include "tabwidget.moc"

namespace KMDIPrivate
{
  class TabWidgetPrivate
  {

  };
}

namespace KMDI
{

TabWidget::TabWidget(TQWidget* parent, const char* name)
 : KTabWidget(parent,name)
 , m_visibility (KMDI::ShowWhenMoreThanOneTab)
 , d (new KMDIPrivate::TabWidgetPrivate())
{
  installEventFilter (this);

  tabBar()->hide();

  setHoverCloseButton(true);

  connect(this, TQT_SIGNAL(closeRequest(TQWidget*)), this, TQT_SLOT(closeTab(TQWidget*)));
}

TabWidget::~TabWidget()
{
  delete d;
  d = 0;
}

bool TabWidget::eventFilter(TQObject *obj, TQEvent *e )
{
  if(e->type() == TQEvent::FocusIn)
  {
    emit focusInEvent ();
  }
  else if (e->type() == TQEvent::ChildRemoved)
  {
    // if we lost a child we uninstall ourself as event filter for the lost
    // child and its children
    TQObject* pLostChild = TQT_TQOBJECT(((TQChildEvent*)e)->child());
    if ((pLostChild != 0L) && (pLostChild->isWidgetType())) {
       TQObjectList *list = pLostChild->queryList( TQWIDGET_OBJECT_NAME_STRING );
       list->insert(0, pLostChild);        // add the lost child to the list too, just to save code
       TQObjectListIt it( *list );          // iterate over all lost child widgets
       TQObject * o;
       while ( (o=it.current()) != 0 ) { // for each found object...
          TQWidget* widg = (TQWidget*)o;
          ++it;
          widg->removeEventFilter(this);
       }
       delete list;                        // delete the list, not the objects
    }
  }
  else if (e->type() == TQEvent::ChildInserted)
  {
    // if we got a new child and we are attached to the MDI system we
    // install ourself as event filter for the new child and its children
    // (as we did when we were added to the MDI system).
    TQObject* pNewChild = TQT_TQOBJECT(((TQChildEvent*)e)->child());
    if ((pNewChild != 0L) && (pNewChild->isWidgetType()))
    {
       TQWidget* pNewWidget = (TQWidget*)pNewChild;
       if (pNewWidget->testWFlags((WFlags)(WType_Dialog | WShowModal)))
           return false;
       TQObjectList *list = pNewWidget->queryList( TQWIDGET_OBJECT_NAME_STRING );
       list->insert(0, pNewChild);         // add the new child to the list too, just to save code
       TQObjectListIt it( *list );          // iterate over all new child widgets
       TQObject * o;
       while ( (o=it.current()) != 0 ) { // for each found object...
          TQWidget* widg = (TQWidget*)o;
          ++it;
          widg->installEventFilter(this);
          connect(widg, TQT_SIGNAL(destroyed()), this, TQT_SLOT(childDestroyed()));
       }
       delete list;                        // delete the list, not the objects
    }
  }

  return KTabWidget::eventFilter (obj, e);
}

void TabWidget::childDestroyed()
{
  // if we lost a child we uninstall ourself as event filter for the lost
  // child and its children
  const TQObject* pLostChild = TQT_TQOBJECT_CONST(sender());
  if ((pLostChild != 0L) && (pLostChild->isWidgetType()))
  {
     TQObjectList *list = ((TQObject*)(pLostChild))->queryList(TQWIDGET_OBJECT_NAME_STRING);
     list->insert(0, pLostChild);        // add the lost child to the list too, just to save code
     TQObjectListIt it( *list );          // iterate over all lost child widgets
     TQObject * obj;
     while ( (obj=it.current()) != 0 ) { // for each found object...
       TQWidget* widg = (TQWidget*)obj;
       ++it;
       widg->removeEventFilter(this);
     }
     delete list;                        // delete the list, not the objects
  }
}

void TabWidget::closeTab(TQWidget* w)
{
  w->close();
}

void TabWidget::addTab ( TQWidget * child, const TQString & label )
{
  KTabWidget::addTab(child,label);
  showPage(child);
  maybeShow();
}

void TabWidget::addTab ( TQWidget * child, const TQIconSet & iconset, const TQString & label )
{
  KTabWidget::addTab(child,iconset,label);
  showPage(child);
  maybeShow();
}

void TabWidget::addTab ( TQWidget * child, TQTab * tab )
{
  KTabWidget::addTab(child,tab);
  showPage(child);
  maybeShow();
}

void TabWidget::insertTab ( TQWidget * child, const TQString & label, int index)
{
  KTabWidget::insertTab(child,label,index);
  showPage(child);
  maybeShow();
  tabBar()->repaint();
}

void TabWidget::insertTab ( TQWidget * child, const TQIconSet & iconset, const TQString & label, int index )
{
  KTabWidget::insertTab(child,iconset,label,index);
  showPage(child);
  maybeShow();
  tabBar()->repaint();
}

void TabWidget::insertTab ( TQWidget * child, TQTab * tab, int index)
{
  KTabWidget::insertTab(child,tab,index);
  showPage(child);
  maybeShow();
  tabBar()->repaint();
}

void TabWidget::removePage ( TQWidget * w )
{
  KTabWidget::removePage(w);
  maybeShow();
}

void TabWidget::updateIconInView( TQWidget *w, TQPixmap icon )
{
  changeTab(w,icon,tabLabel(w));
}

void TabWidget::updateCaptionInView( TQWidget *w, const TQString &caption )
{
  changeTab(w, caption);
}

void TabWidget::maybeShow()
{
  switch (m_visibility)
  {
    case KMDI::AlwaysShowTabs:
      tabBar()->show();

      // show/hide corner widgets
      if (count() == 0)
        setCornerWidgetVisibility(false);
      else
        setCornerWidgetVisibility(true);

      break;

    case KMDI::ShowWhenMoreThanOneTab:
      if (count()<2) tabBar()->hide();
      else tabBar()->show();

      // show/hide corner widgets
      if (count() < 2)
        setCornerWidgetVisibility(false);
      else
        setCornerWidgetVisibility(true);

      break;

    case KMDI::NeverShowTabs:
      tabBar()->hide();
      break;
  }
}

void TabWidget::setCornerWidgetVisibility(bool visible) {
  // there are two corner widgets: on TopLeft and on TopTight!

  if (cornerWidget(TQt::TopLeft) ) {
    if (visible)
      cornerWidget(TQt::TopLeft)->show();
    else
      cornerWidget(TQt::TopLeft)->hide();
  }

  if (cornerWidget(TQt::TopRight) ) {
    if (visible)
      cornerWidget(TQt::TopRight)->show();
    else
      cornerWidget(TQt::TopRight)->hide();
  }
}

void TabWidget::setTabWidgetVisibility( KMDI::TabWidgetVisibility visibility )
{
  m_visibility = visibility;
  maybeShow();
}

KMDI::TabWidgetVisibility TabWidget::tabWidgetVisibility( ) const
{
  return m_visibility;
}

}