kmdichildfrmcaption.cpp
//----------------------------------------------------------------------------
// filename : kmdichildfrmcaption.cpp
//----------------------------------------------------------------------------
// 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
// 2000-2003 maintained by the KDevelop project
//
// copyright : (C) 1999-2003 by Szymon Stefanek (stefanek@tin.it)
// and
// Falk Brettschneider
// email : falkbr@kdevelop.org (Falk Brettschneider)
//----------------------------------------------------------------------------
//
//----------------------------------------------------------------------------
//
// This program 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.
//
//----------------------------------------------------------------------------
#include "kmdichildfrmcaption.h"
#include "kmdichildfrmcaption.moc"
#include <qpainter.h>
#include <qapplication.h>
#include <qcursor.h>
#include <qtoolbutton.h>
#include <qpopupmenu.h>
#include "kmdidefines.h"
#include "kmdichildfrm.h"
#include "kmdichildarea.h"
#include "kmdimainfrm.h"
#include <klocale.h>
#include <iostream>
#ifdef Q_WS_WIN
//TODO: one day gradient can be added for win98/winnt5+
// ask system properties on windows
#ifndef SPI_GETGRADIENTCAPTIONS
# define SPI_GETGRADIENTCAPTIONS 0x1008
#endif
#ifndef COLOR_GRADIENTACTIVECAPTION
# define COLOR_GRADIENTACTIVECAPTION 27
#endif
#ifndef COLOR_GRADIENTINACTIVECAPTION
# define COLOR_GRADIENTINACTIVECAPTION 28
#endif
#endif
//#endif
// Class : KMdiChildFrmCaption
// Purpose : An MDI label that draws the title
//
//
//============== KMdiChildFrmCaption =============//
KMdiChildFrmCaption::KMdiChildFrmCaption( KMdiChildFrm *parent )
: QWidget( parent, "kmdi_childfrmcaption" )
{
m_szCaption = i18n( "Unnamed" );
m_bActive = false;
m_pParent = parent;
setBackgroundMode( NoBackground );
setFocusPolicy( NoFocus );
m_bChildInDrag = false;
}
//============== ~KMdiChildFrmCaption =============//
KMdiChildFrmCaption::~KMdiChildFrmCaption()
{}
//============= mousePressEvent ==============//
void KMdiChildFrmCaption::mousePressEvent( QMouseEvent *e )
{
if ( e->button() == LeftButton )
{
setMouseTracking( false );
if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
{
QApplication::setOverrideCursor( Qt::sizeAllCursor, true );
}
m_pParent->m_bDragging = true;
m_offset = mapToParent( e->pos() );
}
else if ( e->button() == RightButton )
{
m_pParent->systemMenu()->popup( mapToGlobal( e->pos() ) );
}
}
//============= mouseReleaseEvent ============//
void KMdiChildFrmCaption::mouseReleaseEvent( QMouseEvent *e )
{
if ( e->button() == LeftButton )
{
if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
QApplication::restoreOverrideCursor();
releaseMouse();
if ( m_pParent->m_bDragging )
{
m_pParent->m_bDragging = false;
if ( m_bChildInDrag )
{
//notify child view
KMdiChildFrmDragEndEvent ue( e );
if ( m_pParent->m_pClient != 0L )
QApplication::sendEvent( m_pParent->m_pClient, &ue );
m_bChildInDrag = false;
}
}
}
}
//============== mouseMoveEvent =============//
void KMdiChildFrmCaption::mouseMoveEvent( QMouseEvent *e )
{
if ( !m_pParent->m_bDragging )
return ;
if ( !m_bChildInDrag )
{
//notify child view
KMdiChildFrmDragBeginEvent ue( e );
if ( m_pParent->m_pClient != 0L )
QApplication::sendEvent( m_pParent->m_pClient, &ue );
m_bChildInDrag = true;
}
QPoint relMousePosInChildArea = m_pParent->m_pManager->mapFromGlobal( e->globalPos() );
// mouse out of child area? stop child frame dragging
if ( !m_pParent->m_pManager->rect().contains( relMousePosInChildArea ) )
{
if ( relMousePosInChildArea.x() < 0 )
relMousePosInChildArea.rx() = 0;
if ( relMousePosInChildArea.y() < 0 )
relMousePosInChildArea.ry() = 0;
if ( relMousePosInChildArea.x() > m_pParent->m_pManager->width() )
relMousePosInChildArea.rx() = m_pParent->m_pManager->width();
if ( relMousePosInChildArea.y() > m_pParent->m_pManager->height() )
relMousePosInChildArea.ry() = m_pParent->m_pManager->height();
}
QPoint mousePosInChildArea = relMousePosInChildArea - m_offset;
// set new child frame position
parentWidget() ->move( mousePosInChildArea );
}
//=============== setActive ===============//
void KMdiChildFrmCaption::setActive( bool bActive )
{
if ( m_bActive == bActive )
return ;
// Ensure the icon's pixmap has the correct bg color
m_pParent->m_pWinIcon->setBackgroundColor( bActive ?
m_pParent->m_pManager->m_captionActiveBackColor :
m_pParent->m_pManager->m_captionInactiveBackColor );
m_pParent->m_pUnixIcon->setBackgroundColor( bActive ?
m_pParent->m_pManager->m_captionActiveBackColor :
m_pParent->m_pManager->m_captionInactiveBackColor );
m_bActive = bActive;
repaint( false );
}
//=============== setCaption ===============//
void KMdiChildFrmCaption::setCaption( const QString& text )
{
m_szCaption = text;
repaint( false );
}
//============== heightHint ===============//
int KMdiChildFrmCaption::heightHint()
{
int hint = m_pParent->m_pManager->m_captionFontLineSpacing + 3;
if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look )
{
if ( hint < 18 )
hint = 18;
}
else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look )
{
if ( hint < 20 )
hint = 20;
}
else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook )
{
if ( hint < 16 )
hint = 16;
}
else
{ // kde2laptop look
hint -= 4;
if ( hint < 14 )
hint = 14;
}
return hint;
}
//=============== paintEvent ==============//
void KMdiChildFrmCaption::paintEvent( QPaintEvent * )
{
QPainter p( this );
QRect r = rect();
p.setFont( m_pParent->m_pManager->m_captionFont );
if ( m_bActive )
{
p.fillRect( r, m_pParent->m_pManager->m_captionActiveBackColor );
p.setPen( m_pParent->m_pManager->m_captionActiveForeColor );
}
else
{
p.fillRect( r, m_pParent->m_pManager->m_captionInactiveBackColor );
p.setPen( m_pParent->m_pManager->m_captionInactiveForeColor );
}
//Shift the text after the icon
if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::Win95Look )
r.setLeft( r.left() + m_pParent->icon() ->width() + 3 );
else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDE1Look )
r.setLeft( r.left() + 22 );
else if ( KMdiMainFrm::frameDecorOfAttachedViews() == KMdi::KDELook )
r.setLeft( r.left() + m_pParent->icon() ->width() + 3 );
else // kde2laptop look
r.setLeft( r.left() + 30 );
int captionWidthForText = width() - 4 * m_pParent->m_pClose->width() - m_pParent->icon() ->width() - 5;
QString text = abbreviateText( m_szCaption, captionWidthForText );
p.drawText( r, AlignVCenter | AlignLeft | SingleLine, text );
}
QString KMdiChildFrmCaption::abbreviateText( QString origStr, int maxWidth )
{
QFontMetrics fm = fontMetrics();
int actualWidth = fm.width( origStr );
int realLetterCount = origStr.length();
int newLetterCount;
if ( actualWidth != 0 )
newLetterCount = ( maxWidth * realLetterCount ) / actualWidth;
else
newLetterCount = realLetterCount; // should be 0 anyway
int w = maxWidth + 1;
QString s = origStr;
if ( newLetterCount <= 0 )
s = "";
while ( ( w > maxWidth ) && ( newLetterCount >= 1 ) )
{
if ( newLetterCount < realLetterCount )
{
if ( newLetterCount > 3 )
s = origStr.left( newLetterCount / 2 ) + "..." + origStr.right( newLetterCount / 2 );
else
{
if ( newLetterCount > 1 )
s = origStr.left( newLetterCount ) + "..";
else
s = origStr.left( 1 );
}
}
QFontMetrics fm = fontMetrics();
w = fm.width( s );
newLetterCount--;
}
return s;
}
//============= mouseDoubleClickEvent ===========//
void KMdiChildFrmCaption::mouseDoubleClickEvent( QMouseEvent * )
{
m_pParent->maximizePressed();
}
//============= slot_moveViaSystemMenu ===========//
void KMdiChildFrmCaption::slot_moveViaSystemMenu()
{
setMouseTracking( true );
grabMouse();
if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
QApplication::setOverrideCursor( Qt::sizeAllCursor, true );
m_pParent->m_bDragging = true;
m_offset = mapFromGlobal( QCursor::pos() );
}
// kate: space-indent off; replace-tabs off; indent-mode csands; tab-width 4;
KMdiChildFrmCaption::KMdiChildFrmCaption
KMdiChildFrmCaption(KMdiChildFrm *parent)
Constructor.
Definition: kmdichildfrmcaption.cpp:68
KMdiChildFrmCaption::m_pParent
KMdiChildFrm * m_pParent
parent widget
Definition: kmdichildfrmcaption.h:122
KMdiChildFrmCaption::setCaption
void setCaption(const QString &text)
Repaint with a new caption bar title.
Definition: kmdichildfrmcaption.cpp:190
KMdiChildFrmCaption::~KMdiChildFrmCaption
~KMdiChildFrmCaption()
Destructor.
Definition: kmdichildfrmcaption.cpp:81
KMdiChildFrmCaption::m_offset
QPoint m_offset
the position offset related to its parent widget (internally used for translating mouse move position...
Definition: kmdichildfrmcaption.h:132
KMdiChildArea::m_captionActiveForeColor
QColor m_captionActiveForeColor
The background color of the active MDI childframe window captions.
Definition: kmdichildarea.h:84
KMdiChildFrmCaption::setActive
void setActive(bool bActive)
Repaint the caption bar in active background colors.
Definition: kmdichildfrmcaption.cpp:171
KMdiChildFrmCaption::m_bActive
bool m_bActive
state variable indicating whether activated or not activated
Definition: kmdichildfrmcaption.h:127
KMdiChildFrmCaption::mousePressEvent
virtual void mousePressEvent(QMouseEvent *)
The same as KMdiChildFrmCaption::slot_moveViaSystemMenu.
Definition: kmdichildfrmcaption.cpp:86
KMdiChildFrmCaption::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Checks if out of move range of the KMdiChildArea and calls KMdiChildFrm::move.
Definition: kmdichildfrmcaption.cpp:131
KMdiChildArea::m_captionFont
QFont m_captionFont
The MDI childframe window caption font.
Definition: kmdichildarea.h:74
KMdiChildFrm::icon
QPixmap * icon() const
Returns the child frame icon.
Definition: kmdichildfrm.cpp:720
KMdiChildFrm
Internal class.
Definition: kmdichildfrm.h:130
KMdiChildArea::m_captionInactiveForeColor
QColor m_captionInactiveForeColor
The background color of inactive MDI childframe window captions.
Definition: kmdichildarea.h:94
KMdiChildFrmCaption::paintEvent
virtual void paintEvent(QPaintEvent *e)
Draws the caption bar and its title using the settings.
Definition: kmdichildfrmcaption.cpp:227
KMdiChildFrmCaption::heightHint
int heightHint()
Returns the caption bar height depending on the used font.
Definition: kmdichildfrmcaption.cpp:198
KMdiChildFrmCaption::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *)
Restore the normal mouse cursor, set the state variable back to 'not moving'.
Definition: kmdichildfrmcaption.cpp:106
KMdiChildFrmCaption::mouseDoubleClickEvent
virtual void mouseDoubleClickEvent(QMouseEvent *)
Calls maximizePressed of the parent widget ( KMdiChildFrm )
Definition: kmdichildfrmcaption.cpp:303
KMdiChildFrmCaption::m_bChildInDrag
bool m_bChildInDrag
True if the child knows that it is currently being dragged.
Definition: kmdichildfrmcaption.h:137
KMdiChildFrm::maximizePressed
void maximizePressed()
Handles a click on the Maximize button.
Definition: kmdichildfrm.cpp:442
KMdiMainFrm::frameDecorOfAttachedViews
static int frameDecorOfAttachedViews()
Definition: kmdimainfrm.h:461
KMdiChildFrmCaption::slot_moveViaSystemMenu
void slot_moveViaSystemMenu()
Grabs the mouse, a move cursor, sets a move indicator variable to true and keeps the global mouse pos...
Definition: kmdichildfrmcaption.cpp:310
KMdiChildFrmCaption::abbreviateText
QString abbreviateText(QString origStr, int maxWidth)
Computes a new abbreviated string from a given string depending on a given maximum width...
Definition: kmdichildfrmcaption.cpp:261
KMdiChildFrmDragBeginEvent
a QCustomEvent for begin of dragging This special event will be useful, to inform view about child fr...
Definition: kmdichildfrm.h:83
KMdiChildFrmCaption::m_szCaption
QString m_szCaption
the title string shown in the caption bar
Definition: kmdichildfrmcaption.h:116
KMdiChildFrm::systemMenu
QPopupMenu * systemMenu() const
Returns the system menu.
Definition: kmdichildfrm.cpp:1228
KMdiChildFrmDragEndEvent
a QCustomEvent for end of dragging This special event will be useful, to inform view about child fram...
Definition: kmdichildfrm.h:94
KMdiChildArea::m_captionActiveBackColor
QColor m_captionActiveBackColor
The foreground color of the active MDI childframe window caption.
Definition: kmdichildarea.h:79
KMdiChildArea::m_captionInactiveBackColor
QColor m_captionInactiveBackColor
The foreground color of inactive MDI childframe window captions.
Definition: kmdichildarea.h:89

KDE Home | KDE Accessibility Home | Description of Access Keys