summaryrefslogtreecommitdiffstats
path: root/src/tabwidget.cpp
blob: 92bd8aa020cd5e38b0ee6523fa1d63e215dffebb (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
/***************************************************************************
 *     Copyright (C) 2004 by Sashmit Bhaduri                               *
 *     smt@vfemail.net                                                     *
 *                                                                         *
 *     Licensed under GPL.                                                 *
 ***************************************************************************/

#include "tabwidget.h"

#include <tqstyle.h>
#include <tqclipboard.h>

#include <tdeapplication.h>
#include <ktabbar.h>
#include <tdepopupmenu.h>
#include <tdelocale.h>
#include <kiconloader.h>


using namespace KlamAV;

TabWidget::TabWidget(TQWidget * parent, const char *name)
        :KTabWidget(parent, name), m_CurrentMaxLength(30)
{
    setTabReorderingEnabled(false);
    connect( this, SIGNAL( currentChanged(TQWidget *) ), this,
            SLOT( slotTabChanged(TQWidget *) ) );
    connect(this, SIGNAL(closeRequest(TQWidget*)), this, SLOT(slotCloseRequest(TQWidget*)));
    //setHoverCloseButton(Settings::closeButtonOnTabs());
}

TabWidget::~TabWidget()
{
}

void TabWidget::slotSettingsChanged()
{
    //if (hoverCloseButton() != Settings::closeButtonOnTabs())
    //    setHoverCloseButton(Settings::closeButtonOnTabs());
}

void TabWidget::addFrame(Frame *f)
{
    if (!f || !f->widget()) return;
    m_frames.insert(f->widget(), f);
    addTab(f->widget(), f->title());
}

Frame *TabWidget::currentFrame()
{
    TQWidget *w=currentPage();
    if (!w) return 0;
    return m_frames[w];
}

void TabWidget::slotTabChanged(TQWidget *w)
{
    emit currentFrameChanged(m_frames[w]);
}

void TabWidget::removeFrame(Frame *f)
{
    f->setCompleted();
    m_frames.remove(f->widget());
    removePage(f->widget());
    setTitle( currentFrame()->title(), currentPage() );
}

// copied wholesale from KonqFrameTabs
unsigned int TabWidget::tabBarWidthForMaxChars( uint maxLength )
{
    int hframe, overlap;
    hframe    = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace, this );
    overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap, this );

    TQFontMetrics fm = tabBar()->fontMetrics();
    int x = 0;
    for( int i=0; i < count(); ++i ) {
        Frame *f=m_frames[page(i)];
        TQString newTitle=f->title();
        if ( newTitle.length() > maxLength )
            newTitle = newTitle.left( maxLength-3 ) + "...";

        TQTab* tab = tabBar()->tabAt( i );
        int lw = fm.width( newTitle );
        int iw = 0;
        if ( tab->iconSet() )
            iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;

        x += ( tabBar()->style().sizeFromContents( TQStyle::CT_TabBarTab, this,                             TQSize( TQMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ), TQStyleOption( tab ) ) ).width();
    }
    return x;
}

void TabWidget::setTitle( const TQString &title , TQWidget* sender)
{
    removeTabToolTip( sender );

    Frame *f=m_frames[sender];
    if (f)
        f->setTitle(title);

    uint lcw=0, rcw=0;
    int tabBarHeight = tabBar()->sizeHint().height();
    if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
        lcw = TQMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
    if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
        rcw = TQMAX( cornerWidget( TopRight )->width(), tabBarHeight );
    uint maxTabBarWidth = width() - lcw - rcw;

    uint newMaxLength=30;
    for ( ; newMaxLength > 3; newMaxLength-- ) {
        if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
            break;
    }
    TQString newTitle = title;
    if ( newTitle.length() > newMaxLength )
    {
        setTabToolTip( sender, newTitle );
        newTitle = newTitle.left( newMaxLength-3 ) + "...";
    }

    newTitle.replace( '&', "&&" );
    if ( tabLabel( sender ) != newTitle )
        changeTab( sender, newTitle );

    if( newMaxLength != m_CurrentMaxLength )
    {
        for( int i = 0; i < count(); ++i)
        {
            Frame *f=m_frames[page(i)];
            newTitle=f->title();
            removeTabToolTip( page( i ) );
            if ( newTitle.length() > newMaxLength )
            {
                setTabToolTip( page( i ), newTitle );
                newTitle = newTitle.left( newMaxLength-3 ) + "...";
            }

            newTitle.replace( '&', "&&" );
            if ( newTitle != tabLabel( page( i ) ) )
                    changeTab( page( i ), newTitle );
        }
        m_CurrentMaxLength = newMaxLength;
    }
}

void TabWidget::contextMenu(int i, const TQPoint &p)
{
   currentItem = page(i);
   TDEPopupMenu popup;

   int closeTab = popup.insertItem( SmallIcon("tab_remove"), i18n("Close Tab"), this, SLOT( slotCloseTab() ) );
   if(indexOf(currentItem) == 0) { // you can't detach or close articles tab..
      popup.setItemEnabled(closeTab, false);
   }
   popup.exec(p);
}

void TabWidget::slotCloseTab()
{
   if(!currentItem) return;
   removePage(currentItem);
   delete currentItem;
   currentItem = 0;
}

void TabWidget::slotCloseRequest(TQWidget* widget)
{
    if (m_frames.find(widget) != NULL)
        removeFrame(m_frames.find(widget));
}


// vim: set et ts=4 sts=4 sw=4:
#include "tabwidget.moc"