diff --git a/lib/kcommondecoration.cpp b/lib/kcommondecoration.cpp index 6e7d682d8..31beec1a9 100644 --- a/lib/kcommondecoration.cpp +++ b/lib/kcommondecoration.cpp @@ -1,968 +1,978 @@ /* This file is part of the KDE project. Copyright (C) 2005 Sandro Giessl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include "kdecorationfactory.h" #include #include #include "kcommondecoration.h" #include "kcommondecoration.moc" KCommonDecoration::KCommonDecoration(KDecorationBridge* bridge, KDecorationFactory* factory) : KDecoration (bridge, factory), m_previewWidget(0), btnHideMinWidth(200), btnHideLastWidth(0), closing(false) { // sizeof(...) is calculated at compile time memset(m_button, 0, sizeof(KCommonDecorationButton *) * NumButtons); } KCommonDecoration::~KCommonDecoration() { for (int n=0; nsetAttribute(Qt::WA_NoSystemBackground); widget()->installEventFilter( this ); resetLayout(); connect(this, SIGNAL(keepAboveChanged(bool) ), SLOT(keepAboveChange(bool) ) ); connect(this, SIGNAL(keepBelowChanged(bool) ), SLOT(keepBelowChange(bool) ) ); updateCaption(); } void KCommonDecoration::reset( unsigned long changed ) { if (changed & SettingButtons) { resetLayout(); widget()->update(); } } QRegion KCommonDecoration::cornerShape(WindowCorner) { return QRegion(); } void KCommonDecoration::updateCaption() { // This should be reimplemented in decorations for better efficiency widget()->update(); } void KCommonDecoration::borders( int& left, int& right, int& top, int& bottom ) const { left = layoutMetric(LM_BorderLeft); right = layoutMetric(LM_BorderRight); bottom = layoutMetric(LM_BorderBottom); top = layoutMetric(LM_TitleHeight) + layoutMetric(LM_TitleEdgeTop) + layoutMetric(LM_TitleEdgeBottom); updateLayout(); // TODO!! don't call every time we are in ::borders } void KCommonDecoration::updateLayout() const { QRect r = widget()->rect(); int r_x, r_y, r_x2, r_y2; r.getCoords(&r_x, &r_y, &r_x2, &r_y2); // layout preview widget if (m_previewWidget) { const int borderLeft = layoutMetric(LM_BorderLeft); const int borderRight = layoutMetric(LM_BorderRight); const int borderBottom = layoutMetric(LM_BorderBottom); const int titleHeight = layoutMetric(LM_TitleHeight); const int titleEdgeTop = layoutMetric(LM_TitleEdgeTop); const int titleEdgeBottom = layoutMetric(LM_TitleEdgeBottom); int left = r_x+borderLeft; int top = r_y+titleEdgeTop+titleHeight+titleEdgeBottom; int width = r_x2-borderRight-left+1; int height = r_y2-borderBottom-top+1; m_previewWidget->setGeometry(left, top, width, height); moveWidget(left,top, m_previewWidget); resizeWidget(width, height, m_previewWidget); } // resize buttons... for (int n=0; nsize() ) m_button[n]->setSize(newSize); } } // layout buttons int y = r_y + layoutMetric(LM_TitleEdgeTop) + layoutMetric(LM_ButtonMarginTop); if (m_buttonsLeft.count() > 0) { const int buttonSpacing = layoutMetric(LM_ButtonSpacing); int x = r_x + layoutMetric(LM_TitleEdgeLeft); for (ButtonContainer::const_iterator it = m_buttonsLeft.begin(); it != m_buttonsLeft.end(); ++it) { bool elementLayouted = false; if (*it) { if (!(*it)->isHidden() ) { moveWidget(x,y, *it); x += layoutMetric(LM_ButtonWidth, true, qobject_cast(*it) ); elementLayouted = true; } } else { x+= layoutMetric(LM_ExplicitButtonSpacer); elementLayouted = true; } if (elementLayouted && it != m_buttonsLeft.end() ) x += buttonSpacing; } } if (m_buttonsRight.count() > 0) { const int titleEdgeRightLeft = r_x2-layoutMetric(LM_TitleEdgeRight)+1; const int buttonSpacing = layoutMetric(LM_ButtonSpacing); int x = titleEdgeRightLeft - buttonContainerWidth(m_buttonsRight); for (ButtonContainer::const_iterator it = m_buttonsRight.begin(); it != m_buttonsRight.end(); ++it) { bool elementLayouted = false; if (*it) { if (!(*it)->isHidden() ) { moveWidget(x,y, *it); x += layoutMetric(LM_ButtonWidth, true, qobject_cast(*it) );; elementLayouted = true; } } else { x += layoutMetric(LM_ExplicitButtonSpacer); elementLayouted = true; } if (elementLayouted && it != m_buttonsRight.end() ) x += buttonSpacing; } } } void KCommonDecoration::updateButtons() const { for (int n=0; nupdate(); } void KCommonDecoration::resetButtons() const { for (int n=0; nreset(KCommonDecorationButton::ManualReset); } void KCommonDecoration::resetLayout() { for (int n=0; n%1 preview", visibleName() ), widget()); m_previewWidget->setAutoFillBackground(true); m_previewWidget->show(); } addButtons(m_buttonsLeft, options()->customButtonPositions() ? options()->titleButtonsLeft() : defaultButtonsLeft(), true); addButtons(m_buttonsRight, options()->customButtonPositions() ? options()->titleButtonsRight() : defaultButtonsRight(), false); updateLayout(); const int minTitleBarWidth = 35; btnHideMinWidth = buttonContainerWidth(m_buttonsLeft,true) + buttonContainerWidth(m_buttonsRight,true) + layoutMetric(LM_TitleEdgeLeft,false) + layoutMetric(LM_TitleEdgeRight,false) + layoutMetric(LM_TitleBorderLeft,false) + layoutMetric(LM_TitleBorderRight,false) + minTitleBarWidth; btnHideLastWidth = 0; } int KCommonDecoration::buttonsLeftWidth() const { return buttonContainerWidth(m_buttonsLeft); } int KCommonDecoration::buttonsRightWidth() const { return buttonContainerWidth(m_buttonsRight); } int KCommonDecoration::buttonContainerWidth(const ButtonContainer &btnContainer, bool countHidden) const { int explicitSpacer = layoutMetric(LM_ExplicitButtonSpacer); int shownElementsCount = 0; int w = 0; for (ButtonContainer::const_iterator it = btnContainer.begin(); it != btnContainer.end(); ++it) { if (*it) { if (countHidden || !(*it)->isHidden() ) { w += (*it)->width(); ++shownElementsCount; } } else { w += explicitSpacer; ++shownElementsCount; } } w += layoutMetric(LM_ButtonSpacing)*(shownElementsCount-1); return w; } void KCommonDecoration::addButtons(ButtonContainer &btnContainer, const QString& s, bool isLeft) { if (s.length() > 0) { for (int n=0; n < s.length(); n++) { KCommonDecorationButton *btn = 0; switch (s[n].toAscii()) { case 'M': // Menu button if (!m_button[MenuButton]){ btn = createButton(MenuButton); if (!btn) break; btn->setTipText(i18n("Menu") ); btn->setRealizeButtons(Qt::LeftButton|Qt::RightButton); connect(btn, SIGNAL(pressed()), SLOT(menuButtonPressed())); connect(btn, SIGNAL(released()), this, SLOT(menuButtonReleased())); m_button[MenuButton] = btn; } break; case 'S': // OnAllDesktops button if (!m_button[OnAllDesktopsButton]){ btn = createButton(OnAllDesktopsButton); if (!btn) break; const bool oad = isOnAllDesktops(); btn->setTipText(oad?i18n("Not on all desktops"):i18n("On all desktops") ); btn->setToggleButton(true); btn->setOn( oad ); connect(btn, SIGNAL(clicked()), SLOT(toggleOnAllDesktops())); m_button[OnAllDesktopsButton] = btn; } break; case 'H': // Help button if ((!m_button[HelpButton]) && providesContextHelp()){ btn = createButton(HelpButton); if (!btn) break; btn->setTipText(i18n("Help") ); connect(btn, SIGNAL(clicked()), SLOT(showContextHelp())); m_button[HelpButton] = btn; } break; case 'I': // Minimize button if ((!m_button[MinButton]) && isMinimizable()){ btn = createButton(MinButton); if (!btn) break; btn->setTipText(i18n("Minimize") ); connect(btn, SIGNAL(clicked()), SLOT(minimize())); m_button[MinButton] = btn; } break; case 'A': // Maximize button if ((!m_button[MaxButton]) && isMaximizable()){ btn = createButton(MaxButton); if (!btn) break; btn->setRealizeButtons(Qt::LeftButton|Qt::MidButton|Qt::RightButton); const bool max = maximizeMode()==MaximizeFull; btn->setTipText(max?i18n("Restore"):i18n("Maximize") ); btn->setToggleButton(true); btn->setOn( max ); connect(btn, SIGNAL(clicked()), SLOT(slotMaximize())); m_button[MaxButton] = btn; } break; case 'X': // Close button if ((!m_button[CloseButton]) && isCloseable()){ btn = createButton(CloseButton); if (!btn) break; btn->setTipText(i18n("Close") ); connect(btn, SIGNAL(clicked()), SLOT(closeWindow())); m_button[CloseButton] = btn; } break; case 'F': // AboveButton button if (!m_button[AboveButton]){ btn = createButton(AboveButton); if (!btn) break; bool above = keepAbove(); btn->setTipText(above?i18n("Do not keep above others"):i18n("Keep above others") ); btn->setToggleButton(true); btn->setOn( above ); connect(btn, SIGNAL(clicked()), SLOT(slotKeepAbove())); m_button[AboveButton] = btn; } break; case 'B': // BelowButton button if (!m_button[BelowButton]){ btn = createButton(BelowButton); if (!btn) break; bool below = keepBelow(); btn->setTipText(below?i18n("Do not keep below others"):i18n("Keep below others") ); btn->setToggleButton(true); btn->setOn( below ); connect(btn, SIGNAL(clicked()), SLOT(slotKeepBelow())); m_button[BelowButton] = btn; } break; case 'L': // Shade button if ((!m_button[ShadeButton]) && isShadeable()){ btn = createButton(ShadeButton); if (!btn) break; bool shaded = isSetShade(); btn->setTipText(shaded?i18n("Unshade"):i18n("Shade") ); btn->setToggleButton(true); btn->setOn( shaded ); connect(btn, SIGNAL(clicked()), SLOT(slotShade())); m_button[ShadeButton] = btn; } break; case '_': // Spacer item btnContainer.append(0); } if (btn) { btn->setLeft(isLeft); btn->setSize(QSize(layoutMetric(LM_ButtonWidth, true, btn),layoutMetric(LM_ButtonHeight, true, btn)) ); btn->show(); btnContainer.append(btn); } } } } void KCommonDecoration::calcHiddenButtons() { if (width() == btnHideLastWidth) return; btnHideLastWidth = width(); //Hide buttons in the following order: KCommonDecorationButton* btnArray[] = { m_button[HelpButton], m_button[ShadeButton], m_button[BelowButton], m_button[AboveButton], m_button[OnAllDesktopsButton], m_button[MaxButton], m_button[MinButton], m_button[MenuButton], m_button[CloseButton] }; const int buttonsCount = sizeof( btnArray ) / sizeof( btnArray[ 0 ] ); int current_width = width(); int count = 0; // Hide buttons while (current_width < btnHideMinWidth && count < buttonsCount) { if (btnArray[count] ) { current_width += btnArray[count]->width(); if (btnArray[count]->isVisible() ) btnArray[count]->hide(); } count++; } // Show the rest of the buttons... for(int i = count; i < buttonsCount; i++) { if (btnArray[i] ) { if (! btnArray[i]->isHidden() ) break; // all buttons shown... btnArray[i]->show(); } } } void KCommonDecoration::show() { if (decorationBehaviour(DB_ButtonHide) ) calcHiddenButtons(); widget()->show(); } void KCommonDecoration::resize( const QSize& s ) { widget()->resize( s ); } QSize KCommonDecoration::minimumSize() const { const int minWidth = qMax(layoutMetric(LM_TitleEdgeLeft), layoutMetric(LM_BorderLeft)) +qMax(layoutMetric(LM_TitleEdgeRight), layoutMetric(LM_BorderRight)) +layoutMetric(LM_TitleBorderLeft)+layoutMetric(LM_TitleBorderRight); return QSize(minWidth, layoutMetric(LM_TitleEdgeTop)+layoutMetric(LM_TitleHeight) +layoutMetric(LM_TitleEdgeBottom) +layoutMetric(LM_BorderBottom) ); } void KCommonDecoration::maximizeChange() { if( m_button[MaxButton] ) { m_button[MaxButton]->setOn( maximizeMode()==MaximizeFull); m_button[MaxButton]->setTipText( (maximizeMode()!=MaximizeFull) ? i18n("Maximize") : i18n("Restore")); m_button[MaxButton]->reset(KCommonDecorationButton::StateChange); } updateWindowShape(); widget()->update(); } void KCommonDecoration::desktopChange() { if ( m_button[OnAllDesktopsButton] ) { m_button[OnAllDesktopsButton]->setOn( isOnAllDesktops() ); m_button[OnAllDesktopsButton]->setTipText( isOnAllDesktops() ? i18n("Not on all desktops") : i18n("On all desktops")); m_button[OnAllDesktopsButton]->reset(KCommonDecorationButton::StateChange); } } void KCommonDecoration::shadeChange() { if ( m_button[ShadeButton] ) { bool shaded = isSetShade(); m_button[ShadeButton]->setOn( shaded ); m_button[ShadeButton]->setTipText( shaded ? i18n("Unshade") : i18n("Shade")); m_button[ShadeButton]->reset(KCommonDecorationButton::StateChange); } } void KCommonDecoration::iconChange() { if (m_button[MenuButton]) { m_button[MenuButton]->update(); m_button[MenuButton]->reset(KCommonDecorationButton::IconChange); } } void KCommonDecoration::activeChange() { updateButtons(); widget()->update(); // do something similar to updateCaption here } void KCommonDecoration::captionChange() { updateCaption(); } void KCommonDecoration::keepAboveChange(bool above) { if (m_button[AboveButton]) { m_button[AboveButton]->setOn(above); m_button[AboveButton]->setTipText( above?i18n("Do not keep above others"):i18n("Keep above others") ); m_button[AboveButton]->reset(KCommonDecorationButton::StateChange); } if (m_button[BelowButton] && m_button[BelowButton]->isChecked()) { m_button[BelowButton]->setOn(false); m_button[BelowButton]->setTipText( i18n("Keep below others") ); m_button[BelowButton]->reset(KCommonDecorationButton::StateChange); } } void KCommonDecoration::keepBelowChange(bool below) { if (m_button[BelowButton]) { m_button[BelowButton]->setOn(below); m_button[BelowButton]->setTipText( below?i18n("Do not keep below others"):i18n("Keep below others") ); m_button[BelowButton]->reset(KCommonDecorationButton::StateChange); } if (m_button[AboveButton] && m_button[AboveButton]->isChecked()) { m_button[AboveButton]->setOn(false); m_button[AboveButton]->setTipText( i18n("Keep above others") ); m_button[AboveButton]->reset(KCommonDecorationButton::StateChange); } } void KCommonDecoration::slotMaximize() { if (m_button[MaxButton]) { maximize(m_button[MaxButton]->lastMousePress() ); } } void KCommonDecoration::slotShade() { setShade( !isSetShade() ); } void KCommonDecoration::slotKeepAbove() { setKeepAbove(!keepAbove() ); } void KCommonDecoration::slotKeepBelow() { setKeepBelow(!keepBelow() ); } void KCommonDecoration::menuButtonPressed() { static QTime* t = NULL; static KCommonDecoration* lastClient = NULL; if (t == NULL) t = new QTime; bool dbl = (lastClient==this && t->elapsed() <= QApplication::doubleClickInterval()); lastClient = this; t->start(); if (!dbl || !decorationBehaviour(DB_MenuClose) ) { QRect menuRect = m_button[MenuButton]->rect(); QPoint menutop = m_button[MenuButton]->mapToGlobal(menuRect.topLeft()); QPoint menubottom = m_button[MenuButton]->mapToGlobal(menuRect.bottomRight())+QPoint(0,2); KDecorationFactory* f = factory(); showWindowMenu(QRect(menutop, menubottom)); if( !f->exists( this )) // 'this' was deleted return; m_button[MenuButton]->setDown(false); } else closing = true; } void KCommonDecoration::menuButtonReleased() { if(closing) closeWindow(); } void KCommonDecoration::resizeEvent(QResizeEvent */*e*/) { if (decorationBehaviour(DB_ButtonHide) ) calcHiddenButtons(); updateLayout(); updateWindowShape(); // FIXME: don't update() here! this would result in two paintEvent()s // because there is already "something" else triggering the repaint... // widget()->update(); } void KCommonDecoration::moveWidget(int x, int y, QWidget *widget) const { QPoint p = widget->pos(); int oldX = p.y(); int oldY = p.x(); if (x!=oldX || y!=oldY) widget->move(x,y); } void KCommonDecoration::resizeWidget(int w, int h, QWidget *widget) const { QSize s = widget->size(); int oldW = s.width(); int oldH = s.height(); if (w!=oldW || h!=oldH) widget->resize(w,h); } void KCommonDecoration::mouseDoubleClickEvent(QMouseEvent *e) { if( e->button() != Qt::LeftButton ) return; int tb = layoutMetric(LM_TitleEdgeTop)+layoutMetric(LM_TitleHeight)+layoutMetric(LM_TitleEdgeBottom); // when shaded, react on double clicks everywhere to make it easier to unshade. otherwise // react only on double clicks in the title bar region... if (isSetShade() || e->pos().y() <= tb ) titlebarDblClickOperation(); } void KCommonDecoration::wheelEvent(QWheelEvent *e) { int tb = layoutMetric(LM_TitleEdgeTop)+layoutMetric(LM_TitleHeight)+layoutMetric(LM_TitleEdgeBottom); if (isSetShade() || e->pos().y() <= tb ) titlebarMouseWheelOperation( e->delta()); } KCommonDecoration::Position KCommonDecoration::mousePosition(const QPoint &point) const { const int corner = 18+3*layoutMetric(LM_BorderBottom, false)/2; Position pos = PositionCenter; QRect r = widget()->rect(); int r_x, r_y, r_x2, r_y2; r.getCoords(&r_x, &r_y, &r_x2, &r_y2); int p_x = point.x(); int p_y = point.y(); const int borderLeft = layoutMetric(LM_BorderLeft); // const int borderRight = layoutMetric(LM_BorderRight); const int borderBottom = layoutMetric(LM_BorderBottom); const int titleHeight = layoutMetric(LM_TitleHeight); const int titleEdgeTop = layoutMetric(LM_TitleEdgeTop); const int titleEdgeBottom = layoutMetric(LM_TitleEdgeBottom); const int titleEdgeLeft = layoutMetric(LM_TitleEdgeLeft); const int titleEdgeRight = layoutMetric(LM_TitleEdgeRight); const int borderBottomTop = r_y2-borderBottom+1; const int borderLeftRight = r_x+borderLeft-1; // const int borderRightLeft = r_x2-borderRight+1; const int titleEdgeLeftRight = r_x+titleEdgeLeft-1; const int titleEdgeRightLeft = r_x2-titleEdgeRight+1; const int titleEdgeBottomBottom = r_y+titleEdgeTop+titleHeight+titleEdgeBottom-1; const int titleEdgeTopBottom = r_y+titleEdgeTop-1; if (p_y <= titleEdgeTopBottom) { if (p_x <= r_x+corner) pos = PositionTopLeft; else if (p_x >= r_x2-corner) pos = PositionTopRight; else pos = PositionTop; } else if (p_y <= titleEdgeBottomBottom) { if (p_x <= titleEdgeLeftRight) pos = PositionTopLeft; else if (p_x >= titleEdgeRightLeft) pos = PositionTopRight; else pos = PositionCenter; // title bar } else if (p_y < borderBottomTop) { if (p_y < r_y2-corner) { if (p_x <= borderLeftRight) pos = PositionLeft; else pos = PositionRight; } else { if (p_x <= borderLeftRight) pos = PositionBottomLeft; else pos = PositionBottomRight; } } else if(p_y >= borderBottomTop) { if (p_x <= r_x+corner) pos = PositionBottomLeft; else if (p_x >= r_x2-corner) pos = PositionBottomRight; else pos = PositionBottom; } return pos; } void KCommonDecoration::updateWindowShape() { // don't mask the widget... if (!decorationBehaviour(DB_WindowMask) ) return; int w = widget()->width(); int h = widget()->height(); bool tl=true,tr=true,bl=true,br=true; // is there a transparent rounded corner in top-left? etc QDesktopWidget *desktop=KApplication::desktop(); // no transparent rounded corners if this window corner lines up with a screen corner for(int screen=0; screen < desktop->numScreens(); ++screen) { QRect fullscreen(desktop->screenGeometry(screen)); QRect window = geometry(); if(window.topLeft() == fullscreen.topLeft() ) tl = false; if(window.topRight() == fullscreen.topRight() ) tr = false; if(window.bottomLeft() == fullscreen.bottomLeft() ) bl = false; if(window.bottomRight()== fullscreen.bottomRight() ) br = false; } QRegion mask(0, 0, w, h); // Remove top-left corner. if(tl) { mask -= cornerShape(WC_TopLeft); } // Remove top-right corner. if(tr) { mask -= cornerShape(WC_TopRight); } // Remove top-left corner. if(bl) { mask -= cornerShape(WC_BottomLeft); } // Remove top-right corner. if(br) { mask -= cornerShape(WC_BottomRight); } setMask( mask ); } bool KCommonDecoration::eventFilter( QObject* o, QEvent* e ) { if( o != widget()) return false; switch( e->type()) { case QEvent::Resize: resizeEvent(static_cast(e) ); return true; case QEvent::Paint: paintEvent( static_cast< QPaintEvent* >( e )); return true; case QEvent::MouseButtonDblClick: mouseDoubleClickEvent( static_cast< QMouseEvent* >( e )); return true; case QEvent::MouseButtonPress: processMousePressEvent( static_cast< QMouseEvent* >( e )); return true; case QEvent::Wheel: wheelEvent( static_cast< QWheelEvent* >( e )); return true; default: return false; } } const int SUPPORTED_WINDOW_TYPES_MASK = NET::NormalMask | NET::DesktopMask | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask /*| NET::OverrideMask*/ | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask; bool KCommonDecoration::isToolWindow() const { NET::WindowType type = windowType( SUPPORTED_WINDOW_TYPES_MASK ); return ((type==NET::Toolbar)||(type==NET::Utility)||(type==NET::Menu)); } QRect KCommonDecoration::titleRect() const { int r_x, r_y, r_x2, r_y2; widget()->rect().getCoords(&r_x, &r_y, &r_x2, &r_y2); const int titleEdgeLeft = layoutMetric(LM_TitleEdgeLeft); const int titleEdgeTop = layoutMetric(LM_TitleEdgeTop); const int titleEdgeRight = layoutMetric(LM_TitleEdgeRight); const int titleEdgeBottom = layoutMetric(LM_TitleEdgeBottom); const int titleBorderLeft = layoutMetric(LM_TitleBorderLeft); const int titleBorderRight = layoutMetric(LM_TitleBorderRight); const int ttlHeight = layoutMetric(LM_TitleHeight); const int titleEdgeBottomBottom = r_y+titleEdgeTop+ttlHeight+titleEdgeBottom-1; return QRect(r_x+titleEdgeLeft+buttonsLeftWidth()+titleBorderLeft, r_y+titleEdgeTop, r_x2-titleEdgeRight-buttonsRightWidth()-titleBorderRight-(r_x+titleEdgeLeft+buttonsLeftWidth()+titleBorderLeft), titleEdgeBottomBottom-(r_y+titleEdgeTop) ); } KCommonDecorationButton::KCommonDecorationButton(ButtonType type, KCommonDecoration *parent) : QAbstractButton(parent->widget() ), m_decoration(parent), m_type(type), m_realizeButtons(Qt::LeftButton), m_lastMouse(Qt::NoButton), m_isLeft(true) { setCursor(Qt::ArrowCursor); } KCommonDecorationButton::~KCommonDecorationButton() { } KCommonDecoration *KCommonDecorationButton::decoration() const { return m_decoration; } ButtonType KCommonDecorationButton::type() const { return m_type; } bool KCommonDecorationButton::isLeft() const { return m_isLeft; } void KCommonDecorationButton::setLeft(bool left) { m_isLeft = left; } void KCommonDecorationButton::setRealizeButtons(int btns) { m_realizeButtons = btns; } void KCommonDecorationButton::setSize(const QSize &s) { if (!m_size.isValid() || s != size() ) { m_size = s; setFixedSize(m_size); reset(SizeChange); } } QSize KCommonDecorationButton::sizeHint() const { return m_size; } void KCommonDecorationButton::setTipText(const QString &tip) { this->setToolTip( tip ); } void KCommonDecorationButton::setToggleButton(bool toggle) { QAbstractButton::setCheckable(toggle); reset(ToggleChange); } void KCommonDecorationButton::setOn(bool on) { if (on != isChecked() ) { QAbstractButton::setChecked(on); reset(StateChange); } } void KCommonDecorationButton::mousePressEvent(QMouseEvent* e) { m_lastMouse = e->button(); // pass on event after changing button to LeftButton QMouseEvent me(e->type(), e->pos(), (e->button()&m_realizeButtons)?Qt::LeftButton : Qt::NoButton, e->buttons(), e->modifiers() ); QAbstractButton::mousePressEvent(&me); } void KCommonDecorationButton::mouseReleaseEvent(QMouseEvent* e) { m_lastMouse = e->button(); // pass on event after changing button to LeftButton QMouseEvent me(e->type(), e->pos(), (e->button()&m_realizeButtons)?Qt::LeftButton : Qt::NoButton, e->buttons(), e->modifiers() ); QAbstractButton::mouseReleaseEvent(&me); } // kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle; diff --git a/lib/kcommondecoration.h b/lib/kcommondecoration.h index 3a8e6ad14..43964f9c5 100644 --- a/lib/kcommondecoration.h +++ b/lib/kcommondecoration.h @@ -1,369 +1,369 @@ /* This file is part of the KDE project. Copyright (C) 2005 Sandro Giessl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef KCOMMONDECORATION_H #define KCOMMONDECORATION_H #include #include "kdecoration.h" class KDecorationBridge; class KDecorationFactory; enum ButtonType { HelpButton=0, MaxButton, MinButton, CloseButton, MenuButton, OnAllDesktopsButton, AboveButton, BelowButton, ShadeButton, NumButtons }; class KCommonDecorationButton; class KCommonDecorationButtonPrivate; class KCommonDecorationPrivate; /** * This class eases development of decorations by implementing parts of KDecoration * which are error prone and common for most decorations. * It takes care of the window layout, button/action handling, and window mask creation. */ class KWIN_EXPORT KCommonDecoration : public KDecoration { Q_OBJECT public: KCommonDecoration(KDecorationBridge* bridge, KDecorationFactory* factory); virtual ~KCommonDecoration(); /** * Used to calculate the decoration layout. The basic layout looks like this: * * Window: * _______________________________________________________________ * | LM_TitleEdgeTop | * |_______________________________________________________________| * | LM_TitleEdgeLeft | [title] | LM_TitleEdgeRight | * |__________________|________________________|___________________| * | LM_TitleEdgeBottom | * |_______________________________________________________________| * | | | | * | | | | * | | | | * |LM_BorderLeft LM_BorderRight| * |_|___________________________________________________________|_| * | LM_BorderBottom | * |_______________________________________________________________| * * Title: * ___________________________________________________________________________________ * | LM_ButtonMarginTop | | LM_ButtonMarginTop | * |________________________________| |_________________________________| * | [Buttons] | LM_TitleBorderLeft | LM_TitleHeight | LM_TitleBorderRight | [Buttons] | * |___________|____________________|________________|_____________________|___________| * * Buttons: * _____________________________________________________________________________________________ * | button | spacing | button | spacing | explicit spacer | spacing | ... | spacing | button | * |________|_________|________|_________|_________________|_________|________|_________|________| * * @see layoutMetric() */ enum LayoutMetric { LM_BorderLeft, LM_BorderRight, LM_BorderBottom, LM_TitleHeight, LM_TitleBorderLeft, LM_TitleBorderRight, LM_TitleEdgeLeft, LM_TitleEdgeRight, LM_TitleEdgeTop, LM_TitleEdgeBottom, LM_ButtonWidth, LM_ButtonHeight, LM_ButtonSpacing, LM_ExplicitButtonSpacer, LM_ButtonMarginTop }; enum DecorationBehaviour { DB_MenuClose, ///< Close window on double clicking the menu DB_WindowMask, ///< Set a mask on the window DB_ButtonHide ///< Hide buttons when there is not enough space in the titlebar }; enum WindowCorner { WC_TopLeft, WC_TopRight, WC_BottomLeft, WC_BottomRight }; /** * The name of the decoration used in the decoration preview. */ virtual QString visibleName() const = 0; /** * The default title button order on the left. * @see KDecoration::titleButtonsLeft() * @see KDecoration::titleButtonsRight() */ - virtual QString defaultButtonsLeft() const = 0; + virtual QString defaultButtonsLeft(); /** * The default title button order on the left. * @see KDecoration::titleButtonsLeft() * @see KDecoration::titleButtonsRight() */ - virtual QString defaultButtonsRight() const = 0; + virtual QString defaultButtonsRight(); /** * This controls whether some specific behaviour should be enabled or not. * @see DecorationBehaviour */ virtual bool decorationBehaviour(DecorationBehaviour behaviour) const; /** * This controls the layout of the decoration in various ways. It is * possible to have a different layout for different window states. * @param lm The layout element. * @param respectWindowState Whether window states should be taken into account or a "default" state should be assumed. * @param button For LM_ButtonWidth and LM_ButtonHeight, the button. */ virtual int layoutMetric(LayoutMetric lm, bool respectWindowState = true, const KCommonDecorationButton *button = 0) const; /** * Create a new title bar button. KCommonDecoration takes care of memory management. * @return a pointer to the button, or 0 if the button should not be created. */ virtual KCommonDecorationButton *createButton(ButtonType type) = 0; /** * @return the mask for the specific window corner. */ virtual QRegion cornerShape(WindowCorner corner); /** * This updates the window mask using the information provided by * cornerShape(). Edges which are aligned to screen corners are not * shaped for better usability (remember to paint these areas in paintEvent(), too). * You normally don't want/need to reimplement updateWindowShape(). * @see cornerShape() */ virtual void updateWindowShape(); /** * Draw the window decoration. */ virtual void paintEvent(QPaintEvent *e) = 0; /** * This is used to update the painting of the title bar after the caption has been changed. * Reimplement for a more efficient implementation (default calls update() on the whole decoration). */ virtual void updateCaption(); int buttonsLeftWidth() const; int buttonsRightWidth() const; /** * TODO: remove? */ void updateLayout() const; /** * Makes sure all buttons are repainted. */ void updateButtons() const; /** * Manually call reset() on each button. */ void resetButtons() const; /** * Convenience method. * @returns true if the window type is NET::Toolbar, NET::Utility, or NET::Menu */ bool isToolWindow() const; /** * Convenience method. * @returns the title rect. */ QRect titleRect() const; public: /** * Handles widget and layout creation, call the base implementation when subclassing this member. */ virtual void init(); /** * Handles SettingButtons, call the base implementation when subclassing this member. */ virtual void reset( unsigned long changed ); virtual void borders( int& left, int& right, int& top, int& bottom ) const; virtual void show(); virtual void resize(const QSize& s); virtual QSize minimumSize() const; virtual void maximizeChange(); virtual void desktopChange(); virtual void shadeChange(); virtual void iconChange(); virtual void activeChange(); virtual void captionChange(); public Q_SLOTS: void keepAboveChange(bool above); void keepBelowChange(bool below); void slotMaximize(); void slotShade(); void slotKeepAbove(); void slotKeepBelow(); void menuButtonPressed(); void menuButtonReleased(); public: virtual Position mousePosition(const QPoint &point) const; virtual bool eventFilter( QObject* o, QEvent* e ); virtual void resizeEvent(QResizeEvent *e); virtual void mouseDoubleClickEvent(QMouseEvent *e); virtual void wheelEvent(QWheelEvent *e); private: void resetLayout(); void moveWidget(int x, int y, QWidget *widget) const; void resizeWidget(int w, int h, QWidget *widget) const; typedef QVector ButtonContainer; ///< If the entry is 0, it's a spacer. int buttonContainerWidth(const ButtonContainer &btnContainer, bool countHidden = false) const; void addButtons(ButtonContainer &btnContainer, const QString& buttons, bool isLeft); KCommonDecorationButton *m_button[NumButtons]; ButtonContainer m_buttonsLeft; ButtonContainer m_buttonsRight; QWidget *m_previewWidget; // button hiding for small windows void calcHiddenButtons(); int btnHideMinWidth; int btnHideLastWidth; bool closing; // for menu doubleclick closing... KCommonDecorationPrivate *d; }; /** * Title bar buttons of KCommonDecoration need to inherit this class. */ class KWIN_EXPORT KCommonDecorationButton : public QAbstractButton { Q_OBJECT friend class KCommonDecoration; public: KCommonDecorationButton(ButtonType type, KCommonDecoration *parent); virtual ~KCommonDecorationButton(); /** * These flags specify what has changed, e.g. the reason for a reset(). */ enum { ManualReset = 1 << 0, ///< The button might want to do a full reset for some reason... SizeChange = 1 << 1, ///< The button size changed @see setSize() ToggleChange = 1 << 2, ///< The button toggle state has changed @see setToggleButton() StateChange = 1 << 3, ///< The button has been set pressed or not... @see setOn() IconChange = 1 << 4, ///< The window icon has been changed DecorationReset = 1 << 5 ///< E.g. when decoration colors have changed }; /** * Initialize the button after size change etc. */ virtual void reset(unsigned long changed) = 0; /** * @returns the KCommonDecoration the button belongs to. */ KCommonDecoration *decoration() const; /** * @returns the button type. * @see ButtonType */ ButtonType type() const; /** * Whether the button is left of the titlebar or not. */ bool isLeft() const; /** * Set which mouse buttons the button should honor. Used e.g. to prevent accidental right mouse clicks. */ void setRealizeButtons(int btns); /** * Set the button size. */ void setSize(const QSize &s); /** * Set/update the button's tool tip */ void setTipText(const QString &tip); /** * The mouse button that has been clicked last time. */ Qt::MouseButtons lastMousePress() const { return m_lastMouse; } QSize sizeHint() const; protected: void setToggleButton(bool toggle); void setOn(bool on); void setLeft(bool left); void mousePressEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); private: KCommonDecoration *m_decoration; ButtonType m_type; int m_realizeButtons; QSize m_size; Qt::MouseButtons m_lastMouse; bool m_isLeft; KCommonDecorationButtonPrivate *d; }; #endif // KCOMMONDECORATION_H // kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle; diff --git a/lib/kdecoration_p.cpp b/lib/kdecoration_p.cpp index 4c982e94e..e319a995f 100644 --- a/lib/kdecoration_p.cpp +++ b/lib/kdecoration_p.cpp @@ -1,236 +1,236 @@ /***************************************************************** This file is part of the KDE project. Copyright (C) 2003 Lubos Lunak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************/ #include "kdecoration_p.h" #include #include #include #include #include #include KDecorationOptionsPrivate::KDecorationOptionsPrivate() { for(int i=0; i < NUM_COLORS*2; ++i) pal[i] = NULL; } KDecorationOptionsPrivate::~KDecorationOptionsPrivate() { int i; for(i=0; i < NUM_COLORS*2; ++i) { if(pal[i]) { delete pal[i]; pal[i] = NULL; } } } void KDecorationOptionsPrivate::defaultKWinSettings() { - title_buttons_left = "MS"; - title_buttons_right = "HIAX"; + title_buttons_left = KDecorationOptions::defaultTitleButtonsLeft(); + title_buttons_right = KDecorationOptions::defaultTitleButtonsRight(); custom_button_positions = false; show_tooltips = true; border_size = BorderNormal; cached_border_size = BordersCount; // invalid move_resize_maximized_windows = true; OpMaxButtonRightClick = MaximizeOp; OpMaxButtonMiddleClick = VMaximizeOp; OpMaxButtonLeftClick = HMaximizeOp; } unsigned long KDecorationOptionsPrivate::updateKWinSettings( KConfig* config ) { unsigned long changed = 0; KConfigGroup wmConfig(config, "WM"); // SettingColors QColor old_colors[NUM_COLORS*2]; for( int i = 0; i < NUM_COLORS*2; ++i ) old_colors[ i ] = colors[ i ]; QPalette appPal = QApplication::palette(); // normal colors colors[ColorFrame] = appPal.color( QPalette::Active, QPalette::Background ); colors[ColorFrame] = wmConfig.readEntry("frame", colors[ColorFrame]); colors[ColorHandle] = colors[ColorFrame]; colors[ColorHandle] = wmConfig.readEntry("handle", colors[ColorHandle]); // full button configuration (background, blend, and foreground if(QPixmap::defaultDepth() > 8) colors[ColorButtonBg] = colors[ColorFrame].light(130); else colors[ColorButtonBg] = colors[ColorFrame]; colors[ColorButtonBg] = wmConfig.readEntry("activeTitleBtnBg", colors[ColorFrame]); colors[ColorTitleBar] = appPal.color( QPalette::Active, QPalette::Highlight ); colors[ColorTitleBar] = wmConfig.readEntry("activeBackground", colors[ColorTitleBar]); if(QPixmap::defaultDepth() > 8) colors[ColorTitleBlend] = colors[ ColorTitleBar ].dark(110); else colors[ColorTitleBlend] = colors[ ColorTitleBar ]; colors[ColorTitleBlend] = wmConfig.readEntry("activeBlend", colors[ColorTitleBlend]); colors[ColorFont] = appPal.color( QPalette::Active, QPalette::HighlightedText ); colors[ColorFont] = wmConfig.readEntry("activeForeground", colors[ColorFont]); // inactive colors[ColorFrame+NUM_COLORS] = wmConfig.readEntry("inactiveFrame", colors[ColorFrame]); colors[ColorTitleBar+NUM_COLORS] = colors[ColorFrame]; colors[ColorTitleBar+NUM_COLORS] = wmConfig. readEntry("inactiveBackground", colors[ColorTitleBar+NUM_COLORS]); if(QPixmap::defaultDepth() > 8) colors[ColorTitleBlend+NUM_COLORS] = colors[ ColorTitleBar+NUM_COLORS ].dark(110); else colors[ColorTitleBlend+NUM_COLORS] = colors[ ColorTitleBar+NUM_COLORS ]; colors[ColorTitleBlend+NUM_COLORS] = wmConfig.readEntry("inactiveBlend", colors[ColorTitleBlend+NUM_COLORS]); // full button configuration if(QPixmap::defaultDepth() > 8) colors[ColorButtonBg+NUM_COLORS] = colors[ColorFrame+NUM_COLORS].light(130); else colors[ColorButtonBg+NUM_COLORS] = colors[ColorFrame+NUM_COLORS]; colors[ColorButtonBg+NUM_COLORS] = wmConfig.readEntry("inactiveTitleBtnBg", colors[ColorButtonBg]); colors[ColorHandle+NUM_COLORS] = wmConfig.readEntry("inactiveHandle", colors[ColorHandle]); colors[ColorFont+NUM_COLORS] = colors[ColorFrame].dark(); colors[ColorFont+NUM_COLORS] = wmConfig.readEntry("inactiveForeground", colors[ColorFont+NUM_COLORS]); for( int i = 0; i < NUM_COLORS*2; ++i ) if( old_colors[ i ] != colors[ i ] ) changed |= SettingColors; // SettingFont QFont old_activeFont = activeFont; QFont old_inactiveFont = inactiveFont; QFont old_activeFontSmall = activeFontSmall; QFont old_inactiveFontSmall = inactiveFontSmall; QFont activeFontGuess = KGlobalSettings::windowTitleFont(); activeFont = wmConfig.readEntry("activeFont", activeFontGuess); inactiveFont = wmConfig.readEntry("inactiveFont", activeFont); activeFontSmall = activeFont; #ifdef __GNUC__ #warning KDE4 : is it useful ? ( temporary hack ) #endif // activeFontSmall.setPointSize(activeFont.pointSize() - 2 > 0 ? activeFont.pointSize() - 2 : activeFont.pointSize()+1 ); activeFontSmall = wmConfig.readEntry("activeFontSmall", activeFontSmall); inactiveFontSmall = wmConfig.readEntry("inactiveFontSmall", activeFontSmall); if( old_activeFont != activeFont || old_inactiveFont != inactiveFont || old_activeFontSmall != activeFontSmall || old_inactiveFontSmall != inactiveFontSmall ) changed |= SettingFont; KConfigGroup styleConfig(config, "Style"); // SettingsButtons QString old_title_buttons_left = title_buttons_left; QString old_title_buttons_right = title_buttons_right; bool old_custom_button_positions = custom_button_positions; custom_button_positions = styleConfig.readEntry("CustomButtonPositions", false); if (custom_button_positions) { - title_buttons_left = styleConfig.readEntry("ButtonsOnLeft", "MS"); - title_buttons_right = styleConfig.readEntry("ButtonsOnRight", "HIAX"); + title_buttons_left = styleConfig.readEntry("ButtonsOnLeft", KDecorationOptions::defaultTitleButtonsLeft()); + title_buttons_right = styleConfig.readEntry("ButtonsOnRight", KDecorationOptions::defaultTitleButtonsRight()); } else { - title_buttons_left = "MS"; - title_buttons_right = "HIAX"; + title_buttons_left = KDecorationOptions::defaultTitleButtonsLeft(); + title_buttons_right = KDecorationOptions::defaultTitleButtonsRight(); } if( old_custom_button_positions != custom_button_positions || ( custom_button_positions && ( old_title_buttons_left != title_buttons_left || old_title_buttons_right != title_buttons_right ))) changed |= SettingButtons; // SettingTooltips bool old_show_tooltips = show_tooltips; show_tooltips = styleConfig.readEntry("ShowToolTips", true); if( old_show_tooltips != show_tooltips ) changed |= SettingTooltips; // SettingBorder BorderSize old_border_size = border_size; int border_size_num = styleConfig.readEntry( "BorderSize", (int)BorderNormal ); if( border_size_num >= 0 && border_size_num < BordersCount ) border_size = static_cast< BorderSize >( border_size_num ); else border_size = BorderNormal; if( old_border_size != border_size ) changed |= SettingBorder; cached_border_size = BordersCount; // invalid KConfigGroup windowsConfig(config, "Windows"); bool old_move_resize_maximized_windows = move_resize_maximized_windows; move_resize_maximized_windows = windowsConfig.readEntry( "MoveResizeMaximizedWindows", false); if( old_move_resize_maximized_windows != move_resize_maximized_windows ) changed |= SettingBorder; // destroy cached values int i; for(i=0; i < NUM_COLORS*2; ++i) { if(pal[i]) { delete pal[i]; pal[i] = NULL; } } return changed; } KDecorationDefines::BorderSize KDecorationOptionsPrivate::findPreferredBorderSize( BorderSize size, QList< BorderSize > sizes ) const { for( QList< BorderSize >::ConstIterator it = sizes.begin(); it != sizes.end(); ++it ) if( size <= *it ) // size is either a supported size, or *it is the closest larger supported return *it; return sizes.last(); // size is larger than all supported ones, return largest }