kmdichildfrmcaption.cpp

00001 //----------------------------------------------------------------------------
00002 //    filename             : kmdichildfrmcaption.cpp
00003 //----------------------------------------------------------------------------
00004 //    Project              : KDE MDI extension
00005 //
00006 //    begin                : 07/1999       by Szymon Stefanek as part of kvirc
00007 //                                         (an IRC application)
00008 //    changes              : 09/1999       by Falk Brettschneider to create an
00009 //                           - 06/2000     stand-alone Qt extension set of
00010 //                                         classes and a Qt-based library
00011 //                           2000-2003     maintained by the KDevelop project
00012 //
00013 //    copyright            : (C) 1999-2003 by Szymon Stefanek (stefanek@tin.it)
00014 //                                         and
00015 //                                         Falk Brettschneider
00016 //    email                :  falkbr@kdevelop.org (Falk Brettschneider)
00017 //----------------------------------------------------------------------------
00018 //
00019 //----------------------------------------------------------------------------
00020 //
00021 //    This program is free software; you can redistribute it and/or modify
00022 //    it under the terms of the GNU Library General Public License as
00023 //    published by the Free Software Foundation; either version 2 of the
00024 //    License, or (at your option) any later version.
00025 //
00026 //----------------------------------------------------------------------------
00027 
00028 #include "kmdichildfrmcaption.h"
00029 #include "kmdichildfrmcaption.moc"
00030 
00031 #include <qpainter.h>
00032 #include <qapplication.h>
00033 #include <qcursor.h>
00034 #include <qtoolbutton.h>
00035 #include <qpopupmenu.h>
00036 
00037 #include "kmdidefines.h"
00038 #include "kmdichildfrm.h"
00039 #include "kmdichildarea.h"
00040 #include "kmdimainfrm.h"
00041 #include <klocale.h>
00042 #include <iostream>
00043 
00044 #ifdef Q_WS_WIN 
00045 //TODO: one day gradient can be added for win98/winnt5+
00046 // ask system properties on windows
00047 #ifndef SPI_GETGRADIENTCAPTIONS
00048 # define SPI_GETGRADIENTCAPTIONS 0x1008
00049 #endif
00050 #ifndef COLOR_GRADIENTACTIVECAPTION
00051 # define COLOR_GRADIENTACTIVECAPTION 27
00052 #endif
00053 #ifndef COLOR_GRADIENTINACTIVECAPTION
00054 # define COLOR_GRADIENTINACTIVECAPTION 28
00055 #endif
00056 #endif 
00057 //#endif
00058 
00060 // Class   : KMdiChildFrmCaption
00061 // Purpose : An MDI label that draws the title
00062 //
00063 //
00065 
00066 //============== KMdiChildFrmCaption =============//
00067 
00068 KMdiChildFrmCaption::KMdiChildFrmCaption( KMdiChildFrm *parent )
00069         : QWidget( parent, "kmdi_childfrmcaption" )
00070 {
00071     m_szCaption = i18n( "Unnamed" );
00072     m_bActive = false;
00073     m_pParent = parent;
00074     setBackgroundMode( NoBackground );
00075     setFocusPolicy( NoFocus );
00076     m_bChildInDrag = false;
00077 }
00078 
00079 //============== ~KMdiChildFrmCaption =============//
00080 
00081 KMdiChildFrmCaption::~KMdiChildFrmCaption()
00082 {}
00083 
00084 //============= mousePressEvent ==============//
00085 
00086 void KMdiChildFrmCaption::mousePressEvent( QMouseEvent *e )
00087 {
00088     if ( e->button() == LeftButton )
00089     {
00090         setMouseTracking( false );
00091         if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
00092         {
00093             QApplication::setOverrideCursor( Qt::sizeAllCursor, true );
00094         }
00095         m_pParent->m_bDragging = true;
00096         m_offset = mapToParent( e->pos() );
00097     }
00098     else if ( e->button() == RightButton )
00099     {
00100         m_pParent->systemMenu()->popup( mapToGlobal( e->pos() ) );
00101     }
00102 }
00103 
00104 //============= mouseReleaseEvent ============//
00105 
00106 void KMdiChildFrmCaption::mouseReleaseEvent( QMouseEvent *e )
00107 {
00108     if ( e->button() == LeftButton )
00109     {
00110         if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
00111             QApplication::restoreOverrideCursor();
00112         
00113         releaseMouse();
00114         if ( m_pParent->m_bDragging )
00115         {
00116             m_pParent->m_bDragging = false;
00117             if ( m_bChildInDrag )
00118             {
00119                 //notify child view
00120                 KMdiChildFrmDragEndEvent ue( e );
00121                 if ( m_pParent->m_pClient != 0L )
00122                     QApplication::sendEvent( m_pParent->m_pClient, &ue );
00123                 
00124                 m_bChildInDrag = false;
00125             }
00126         }
00127     }
00128 }
00129 
00130 //============== mouseMoveEvent =============//
00131 void KMdiChildFrmCaption::mouseMoveEvent( QMouseEvent *e )
00132 {
00133     if ( !m_pParent->m_bDragging )
00134         return ;
00135 
00136     if ( !m_bChildInDrag )
00137     {
00138         //notify child view
00139         KMdiChildFrmDragBeginEvent ue( e );
00140         if ( m_pParent->m_pClient != 0L )
00141             QApplication::sendEvent( m_pParent->m_pClient, &ue );
00142         
00143         m_bChildInDrag = true;
00144     }
00145 
00146     QPoint relMousePosInChildArea = m_pParent->m_pManager->mapFromGlobal( e->globalPos() );
00147 
00148     // mouse out of child area? stop child frame dragging
00149     if ( !m_pParent->m_pManager->rect().contains( relMousePosInChildArea ) )
00150     {
00151         if ( relMousePosInChildArea.x() < 0 )
00152             relMousePosInChildArea.rx() = 0;
00153         
00154         if ( relMousePosInChildArea.y() < 0 )
00155             relMousePosInChildArea.ry() = 0;
00156         
00157         if ( relMousePosInChildArea.x() > m_pParent->m_pManager->width() )
00158             relMousePosInChildArea.rx() = m_pParent->m_pManager->width();
00159         
00160         if ( relMousePosInChildArea.y() > m_pParent->m_pManager->height() )
00161             relMousePosInChildArea.ry() = m_pParent->m_pManager->height();
00162     }
00163     QPoint mousePosInChildArea = relMousePosInChildArea - m_offset;
00164 
00165     // set new child frame position
00166     parentWidget() ->move( mousePosInChildArea );
00167 }
00168 
00169 //=============== setActive ===============//
00170 
00171 void KMdiChildFrmCaption::setActive( bool bActive )
00172 {
00173     if ( m_bActive == bActive )
00174         return ;
00175 
00176     //    Ensure the icon's pixmap has the correct bg color
00177     m_pParent->m_pWinIcon->setBackgroundColor( bActive ?
00178                                                m_pParent->m_pManager->m_captionActiveBackColor :
00179                                                m_pParent->m_pManager->m_captionInactiveBackColor );
00180     m_pParent->m_pUnixIcon->setBackgroundColor( bActive ?
00181                                                 m_pParent->m_pManager->m_captionActiveBackColor :
00182                                                 m_pParent->m_pManager->m_captionInactiveBackColor );
00183 
00184     m_bActive = bActive;
00185     repaint( false );
00186 }
00187 
00188 //=============== setCaption ===============//
00189 
00190 void KMdiChildFrmCaption::setCaption( const QString& text )
00191 {
00192     m_szCaption = text;
00193     repaint( false );
00194 }
00195 
00196 //============== heightHint ===============//
00197 
00198 int KMdiChildFrmCaption::heightHint()
00199 {
00200     int hint = m_pParent->m_pManager->m_captionFontLineSpacing + 3;
00201     if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look )
00202     {
00203         if ( hint < 18 )
00204             hint = 18;
00205     }
00206     else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look )
00207     {
00208         if ( hint < 20 )
00209             hint = 20;
00210     }
00211     else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook )
00212     {
00213         if ( hint < 16 )
00214             hint = 16;
00215     }
00216     else
00217     {   // kde2laptop look
00218         hint -= 4;
00219         if ( hint < 14 )
00220             hint = 14;
00221     }
00222     return hint;
00223 }
00224 
00225 //=============== paintEvent ==============//
00226 
00227 void KMdiChildFrmCaption::paintEvent( QPaintEvent * )
00228 {
00229     QPainter p( this );
00230     QRect r = rect();
00231     p.setFont( m_pParent->m_pManager->m_captionFont );
00232     
00233     if ( m_bActive )
00234     {
00235         p.fillRect( r, m_pParent->m_pManager->m_captionActiveBackColor );
00236         p.setPen( m_pParent->m_pManager->m_captionActiveForeColor );
00237     }
00238     else
00239     {
00240         p.fillRect( r, m_pParent->m_pManager->m_captionInactiveBackColor );
00241         p.setPen( m_pParent->m_pManager->m_captionInactiveForeColor );
00242     }
00243     
00244     //Shift the text after the icon
00245     if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look )
00246         r.setLeft( r.left() + m_pParent->icon() ->width() + 3 );
00247     else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look )
00248         r.setLeft( r.left() + 22 );
00249     else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook )
00250         r.setLeft( r.left() + m_pParent->icon() ->width() + 3 );
00251     else  // kde2laptop look
00252         r.setLeft( r.left() + 30 );
00253 
00254     int captionWidthForText = width() - 4 * m_pParent->m_pClose->width() - m_pParent->icon() ->width() - 5;
00255     QString text = abbreviateText( m_szCaption, captionWidthForText );
00256     p.drawText( r, AlignVCenter | AlignLeft | SingleLine, text );
00257 
00258 }
00259 
00260 
00261 QString KMdiChildFrmCaption::abbreviateText( QString origStr, int maxWidth )
00262 {
00263     QFontMetrics fm = fontMetrics();
00264     int actualWidth = fm.width( origStr );
00265 
00266     int realLetterCount = origStr.length();
00267     int newLetterCount;
00268     
00269     if ( actualWidth != 0 )
00270         newLetterCount = ( maxWidth * realLetterCount ) / actualWidth;
00271     else
00272         newLetterCount = realLetterCount; // should be 0 anyway
00273 
00274     int w = maxWidth + 1;
00275     QString s = origStr;
00276     
00277     if ( newLetterCount <= 0 )
00278         s = "";
00279     
00280     while ( ( w > maxWidth ) && ( newLetterCount >= 1 ) )
00281     {
00282         if ( newLetterCount < realLetterCount )
00283         {
00284             if ( newLetterCount > 3 )
00285                 s = origStr.left( newLetterCount / 2 ) + "..." + origStr.right( newLetterCount / 2 );
00286             else
00287             {
00288                 if ( newLetterCount > 1 )
00289                     s = origStr.left( newLetterCount ) + "..";
00290                 else
00291                     s = origStr.left( 1 );
00292             }
00293         }
00294         QFontMetrics fm = fontMetrics();
00295         w = fm.width( s );
00296         newLetterCount--;
00297     }
00298     return s;
00299 }
00300 
00301 //============= mouseDoubleClickEvent ===========//
00302 
00303 void KMdiChildFrmCaption::mouseDoubleClickEvent( QMouseEvent * )
00304 {
00305     m_pParent->maximizePressed();
00306 }
00307 
00308 //============= slot_moveViaSystemMenu ===========//
00309 
00310 void KMdiChildFrmCaption::slot_moveViaSystemMenu()
00311 {
00312     setMouseTracking( true );
00313     grabMouse();
00314     
00315     if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
00316         QApplication::setOverrideCursor( Qt::sizeAllCursor, true );
00317     
00318     m_pParent->m_bDragging = true;
00319     m_offset = mapFromGlobal( QCursor::pos() );
00320 }
00321 
00322 // kate: space-indent off; replace-tabs off; indent-mode csands; tab-width 4;
KDE Home | KDE Accessibility Home | Description of Access Keys