25 #include "tabwidget.h"
28 #include <tqapplication.h>
29 #include <tqiconset.h>
30 #include <tqclipboard.h>
32 #include <tqptrdict.h>
34 #include <tqtoolbutton.h>
35 #include <tqtooltip.h>
37 #include <tdeapplication.h>
39 #include <ktabwidget.h>
41 #include <tdepopupmenu.h>
43 #include <tdelocale.h>
44 #include <tdehtmlview.h>
45 #include <tdehtml_part.h>
46 #include <kiconloader.h>
49 #include <kmimetype.h>
51 #include "actionmanager.h"
53 #include "akregatorconfig.h"
57 class TabWidget::TabWidgetPrivate
60 TQPtrDict<Frame> frames;
61 uint CurrentMaxLength;
62 TQWidget* currentItem;
63 TQToolButton* tabsClose;
66 TabWidget::TabWidget(TQWidget * parent,
const char *name)
67 :KTabWidget(parent, name), d(new TabWidgetPrivate)
69 d->CurrentMaxLength = 30;
71 setMinimumSize(250,150);
72 setTabReorderingEnabled(
false);
73 connect(
this, TQ_SIGNAL( currentChanged(TQWidget *) ),
this,
74 TQ_SLOT( slotTabChanged(TQWidget *) ) );
75 connect(
this, TQ_SIGNAL(closeRequest(TQWidget*)),
this, TQ_SLOT(slotCloseRequest(TQWidget*)));
76 setHoverCloseButton(Settings::closeButtonOnTabs());
78 d->tabsClose =
new TQToolButton(
this);
79 d->tabsClose->setAccel(TQKeySequence(
"Ctrl+W"));
80 connect( d->tabsClose, TQ_SIGNAL( clicked() ),
this,
81 TQ_SLOT( slotRemoveCurrentFrame() ) );
83 d->tabsClose->setIconSet( SmallIconSet(
"tab_remove" ) );
84 d->tabsClose->adjustSize();
85 TQToolTip::add(d->tabsClose, i18n(
"Close the current tab"));
86 setCornerWidget( d->tabsClose, TopRight );
89 TabWidget::~TabWidget()
95 void TabWidget::slotSettingsChanged()
97 if (hoverCloseButton() != Settings::closeButtonOnTabs())
98 setHoverCloseButton(Settings::closeButtonOnTabs());
101 void TabWidget::slotNextTab()
103 setCurrentPage((currentPageIndex()+1) % count());
106 void TabWidget::slotPreviousTab()
108 if (currentPageIndex() == 0)
109 setCurrentPage(count()-1);
111 setCurrentPage(currentPageIndex()-1);
114 void TabWidget::addFrame(Frame *f)
116 if (!f || !f->widget())
118 d->frames.insert(f->widget(), f);
119 addTab(f->widget(), f->title());
120 connect(f, TQ_SIGNAL(titleChanged(Frame*,
const TQString& )),
this, TQ_SLOT(slotSetTitle(Frame*,
const TQString& )));
121 slotSetTitle(f, f->title());
124 Frame *TabWidget::currentFrame()
126 TQWidget* w = currentPage();
128 return w ? d->frames[w] : 0;
131 TQPtrList<Frame> TabWidget::frames()
const
133 TQPtrList<Frame> result;
134 TQPtrDictIterator<Frame> it(d->frames);
137 result.append(it.current());
144 void TabWidget::slotTabChanged(TQWidget *w)
147 d->tabsClose->setDisabled(currentPageIndex() == 0);
148 emit currentFrameChanged(d->frames[w]);
151 void TabWidget::slotRemoveCurrentFrame()
153 removeFrame(currentFrame());
156 void TabWidget::removeFrame(Frame *f)
159 d->frames.remove(f->widget());
160 removePage(f->widget());
162 setTitle( currentFrame()->title(), currentPage() );
166 uint TabWidget::tabBarWidthForMaxChars( uint maxLength )
169 hframe = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabHSpace,
this );
170 overlap = tabBar()->style().pixelMetric( TQStyle::PM_TabBarTabOverlap,
this );
172 TQFontMetrics fm = tabBar()->fontMetrics();
174 for(
int i=0; i < count(); ++i ) {
175 Frame *f=d->frames[page(i)];
176 TQString newTitle=f->title();
177 if ( newTitle.length() > maxLength )
178 newTitle = newTitle.left( maxLength-3 ) +
"...";
180 TQTab* tab = tabBar()->tabAt( i );
181 int lw = fm.width( newTitle );
183 if ( tab->iconSet() )
184 iw = tab->iconSet()->pixmap( TQIconSet::Small, TQIconSet::Normal ).width() + 4;
186 x += ( tabBar()->style().sizeFromContents( TQStyle::CT_TabBarTab,
this, TQSize( TQMAX( lw + hframe + iw, TQApplication::globalStrut().width() ), 0 ), TQStyleOption( tab ) ) ).width();
191 void TabWidget::slotSetTitle(Frame* frame,
const TQString& title)
193 setTitle(title, frame->widget());
196 void TabWidget::setTitle(
const TQString &title , TQWidget* sender)
198 removeTabToolTip( sender );
201 int tabBarHeight = tabBar()->sizeHint().height();
202 if ( cornerWidget( TopLeft ) && cornerWidget( TopLeft )->isVisible() )
203 lcw = TQMAX( cornerWidget( TopLeft )->width(), tabBarHeight );
204 if ( cornerWidget( TopRight ) && cornerWidget( TopRight )->isVisible() )
205 rcw = TQMAX( cornerWidget( TopRight )->width(), tabBarHeight );
206 uint maxTabBarWidth = width() - lcw - rcw;
208 uint newMaxLength=30;
209 for ( ; newMaxLength > 3; newMaxLength-- )
211 if ( tabBarWidthForMaxChars( newMaxLength ) < maxTabBarWidth )
214 TQString newTitle = title;
215 if ( newTitle.length() > newMaxLength )
217 setTabToolTip( sender, newTitle );
218 newTitle = newTitle.left( newMaxLength-3 ) +
"...";
221 newTitle.replace(
'&',
"&&" );
222 if ( tabLabel( sender ) != newTitle )
223 changeTab( sender, newTitle );
225 if( newMaxLength != d->CurrentMaxLength )
227 for(
int i = 0; i < count(); ++i)
229 Frame *f=d->frames[page(i)];
231 removeTabToolTip( page( i ) );
232 if ( newTitle.length() > newMaxLength )
234 setTabToolTip( page( i ), newTitle );
235 newTitle = newTitle.left( newMaxLength-3 ) +
"...";
238 newTitle.replace(
'&',
"&&" );
239 if ( newTitle != tabLabel( page( i ) ) )
240 changeTab( page( i ), newTitle );
242 d->CurrentMaxLength = newMaxLength;
246 void TabWidget::contextMenu(
int i,
const TQPoint &p)
248 TQWidget* w = ActionManager::getInstance()->container(
"tab_popup");
249 d->currentItem = page(i);
251 if (w && indexOf(d->currentItem) != 0)
252 static_cast<TQPopupMenu *
>(w)->exec(p);
256 void TabWidget::slotDetachTab()
258 if (!d->currentItem || indexOf(d->currentItem) == -1)
259 d->currentItem = currentPage();
261 if (indexOf(d->currentItem) == 0)
265 TDEHTMLView* view =
dynamic_cast<TDEHTMLView*
>(d->currentItem);
270 url = view->part()->url();
272 kapp->invokeBrowser(url.url(),
"0");
276 void TabWidget::slotCopyLinkAddress()
278 if(!d->currentItem || indexOf(d->currentItem) == -1)
279 d->currentItem = currentPage();
280 if(indexOf(d->currentItem) == 0)
284 TDEHTMLView* view =
dynamic_cast<TDEHTMLView*
>(d->currentItem);
289 url = view->part()->url();
291 kapp->clipboard()->setText(url.prettyURL(), TQClipboard::Selection);
292 kapp->clipboard()->setText(url.prettyURL(), TQClipboard::Clipboard);
295 void TabWidget::slotCloseTab()
297 if (!d->currentItem || indexOf(d->currentItem) == -1)
298 d->currentItem = currentPage();
299 if (indexOf(d->currentItem) == 0)
301 if (d->frames.find(d->currentItem) != NULL)
302 removeFrame(d->frames.find(d->currentItem));
303 delete d->currentItem;
307 void TabWidget::initiateDrag(
int tab)
312 Frame* frame = d->frames[page(tab)];
317 lst.append( frame->part()->url() );
318 KURLDrag* drag =
new KURLDrag( lst,
this );
319 drag->setPixmap( KMimeType::pixmapForURL( lst.first(), 0, TDEIcon::Small ) );
324 void TabWidget::slotCloseRequest(TQWidget* widget)
326 if (d->frames.find(widget) != NULL)
327 removeFrame(d->frames.find(widget));
331 #include "tabwidget.moc"