Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / unx / kde4 / KDESalFrame.cxx
blobb7806f4689a87d2f4bb7b6e7d5a94f3669afa1ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <QColor>
21 #include <QStyle>
23 #include <kconfig.h>
24 #include <kglobal.h>
25 #include <kmenubar.h>
26 #include <kconfiggroup.h>
27 #include <kmainwindow.h>
28 #include <kapplication.h>
29 #include <ktoolbar.h>
30 #include <qdebug.h>
31 #include <qtooltip.h>
33 #undef Region
35 #include "KDESalFrame.hxx"
36 #include "KDEXLib.hxx"
37 #include "KDESalGraphics.hxx"
39 #include <tools/color.hxx>
41 #include <vcl/font.hxx>
42 #include <vcl/settings.hxx>
44 #include "fontmanager.hxx"
46 #include <svdata.hxx>
48 #if OSL_DEBUG_LEVEL > 1
49 #include <stdio.h>
50 #endif
52 KDESalFrame::KDESalFrame( SalFrame* pParent, sal_uLong nState ) :
53 X11SalFrame( pParent, nState )
57 void KDESalFrame::Show( bool bVisible, bool bNoActivate )
59 if ( !GetParent() && ! (GetStyle() & SAL_FRAME_STYLE_INTRO) )
61 KDEXLib* pXLib = static_cast<KDEXLib*>(GetDisplay()->GetXLib());
62 pXLib->doStartup();
65 X11SalFrame::Show( bVisible, bNoActivate );
68 /** Helper function to convert colors.
70 static Color toColor( const QColor &rColor )
72 return Color( rColor.red(), rColor.green(), rColor.blue() );
75 /** Helper function to read untranslated text entry from KConfig configuration repository.
77 static OUString readEntryUntranslated( KConfigGroup *pGroup, const char *pKey )
79 return OUString::createFromAscii( (const char *) pGroup->readEntryUntranslated( pKey ).toAscii() );
82 /** Helper function to add information to Font from QFont.
84 Mostly grabbed from the Gtk+ vclplug (salnativewidgets-gtk.cxx).
86 static vcl::Font toFont( const QFont &rQFont, const ::com::sun::star::lang::Locale& rLocale )
88 psp::FastPrintFontInfo aInfo;
89 QFontInfo qFontInfo( rQFont );
91 // set family name
92 aInfo.m_aFamilyName = OUString( (const char *) rQFont.family().toUtf8(), strlen( (const char *) rQFont.family().toUtf8() ), RTL_TEXTENCODING_UTF8 );
94 // set italic
95 aInfo.m_eItalic = ( qFontInfo.italic()? ITALIC_NORMAL: ITALIC_NONE );
97 // set weight
98 int nWeight = qFontInfo.weight();
99 if ( nWeight <= QFont::Light )
100 aInfo.m_eWeight = WEIGHT_LIGHT;
101 else if ( nWeight <= QFont::Normal )
102 aInfo.m_eWeight = WEIGHT_NORMAL;
103 else if ( nWeight <= QFont::DemiBold )
104 aInfo.m_eWeight = WEIGHT_SEMIBOLD;
105 else if ( nWeight <= QFont::Bold )
106 aInfo.m_eWeight = WEIGHT_BOLD;
107 else
108 aInfo.m_eWeight = WEIGHT_ULTRABOLD;
110 // set width
111 int nStretch = rQFont.stretch();
112 if ( nStretch <= QFont::UltraCondensed )
113 aInfo.m_eWidth = WIDTH_ULTRA_CONDENSED;
114 else if ( nStretch <= QFont::ExtraCondensed )
115 aInfo.m_eWidth = WIDTH_EXTRA_CONDENSED;
116 else if ( nStretch <= QFont::Condensed )
117 aInfo.m_eWidth = WIDTH_CONDENSED;
118 else if ( nStretch <= QFont::SemiCondensed )
119 aInfo.m_eWidth = WIDTH_SEMI_CONDENSED;
120 else if ( nStretch <= QFont::Unstretched )
121 aInfo.m_eWidth = WIDTH_NORMAL;
122 else if ( nStretch <= QFont::SemiExpanded )
123 aInfo.m_eWidth = WIDTH_SEMI_EXPANDED;
124 else if ( nStretch <= QFont::Expanded )
125 aInfo.m_eWidth = WIDTH_EXPANDED;
126 else if ( nStretch <= QFont::ExtraExpanded )
127 aInfo.m_eWidth = WIDTH_EXTRA_EXPANDED;
128 else
129 aInfo.m_eWidth = WIDTH_ULTRA_EXPANDED;
131 SAL_INFO( "vcl.kde4", "font name BEFORE system match: \"" << aInfo.m_aFamilyName << "\"" );
133 // match font to e.g. resolve "Sans"
134 psp::PrintFontManager::get().matchFont( aInfo, rLocale );
136 SAL_INFO( "vcl.kde4", "font match " <<
137 (aInfo.m_nID != 0 ? "succeeded" : "failed") <<
138 ", name AFTER: \"" << aInfo.m_aFamilyName << "\"" );
140 // font height
141 int nPointHeight = qFontInfo.pointSize();
142 if ( nPointHeight <= 0 )
143 nPointHeight = rQFont.pointSize();
145 // Create the font
146 vcl::Font aFont( aInfo.m_aFamilyName, Size( 0, nPointHeight ) );
147 if( aInfo.m_eWeight != WEIGHT_DONTKNOW )
148 aFont.SetWeight( aInfo.m_eWeight );
149 if( aInfo.m_eWidth != WIDTH_DONTKNOW )
150 aFont.SetWidthType( aInfo.m_eWidth );
151 if( aInfo.m_eItalic != ITALIC_DONTKNOW )
152 aFont.SetItalic( aInfo.m_eItalic );
153 if( aInfo.m_ePitch != PITCH_DONTKNOW )
154 aFont.SetPitch( aInfo.m_ePitch );
156 return aFont;
159 /** Implementation of KDE integration's main method.
161 void KDESalFrame::UpdateSettings( AllSettings& rSettings )
163 StyleSettings style( rSettings.GetStyleSettings() );
164 bool bSetTitleFont = false;
166 // General settings
167 QPalette pal = QApplication::palette();
169 style.SetToolbarIconSize( ToolbarIconSize::Large );
171 style.SetActiveColor(toColor(pal.color(QPalette::Active, QPalette::Window)));
172 style.SetDeactiveColor(toColor(pal.color(QPalette::Inactive, QPalette::Window)));
174 style.SetActiveTextColor(toColor(pal.color(QPalette::Active, QPalette::WindowText)));
175 style.SetDeactiveTextColor(toColor(pal.color(QPalette::Inactive, QPalette::WindowText)));
177 // WM settings
178 KConfig *pConfig = KGlobal::config().data();
179 if ( pConfig )
181 KConfigGroup aGroup = pConfig->group( "WM" );
182 const char *pKey;
184 pKey = "titleFont";
185 if ( aGroup.hasKey( pKey ) )
187 vcl::Font aFont = toFont( aGroup.readEntry( pKey, QFont() ), rSettings.GetUILanguageTag().getLocale() );
188 style.SetTitleFont( aFont );
189 bSetTitleFont = true;
192 aGroup = pConfig->group( "Icons" );
194 pKey = "Theme";
195 if ( aGroup.hasKey( pKey ) )
196 style.SetPreferredIconTheme( readEntryUntranslated( &aGroup, pKey ) );
198 //toolbar
199 pKey = "toolbarFont";
200 if ( aGroup.hasKey( pKey ) )
202 vcl::Font aFont = toFont( aGroup.readEntry( pKey, QFont() ), rSettings.GetUILanguageTag().getLocale() );
203 style.SetToolFont( aFont );
207 Color aFore = toColor( pal.color( QPalette::Active, QPalette::WindowText ) );
208 Color aBack = toColor( pal.color( QPalette::Active, QPalette::Window ) );
209 Color aText = toColor( pal.color( QPalette::Active, QPalette::Text ) );
210 Color aBase = toColor( pal.color( QPalette::Active, QPalette::Base ) );
211 Color aButn = toColor( pal.color( QPalette::Active, QPalette::ButtonText ) );
212 Color aMid = toColor( pal.color( QPalette::Active, QPalette::Mid ) );
213 Color aHigh = toColor( pal.color( QPalette::Active, QPalette::Highlight ) );
215 // Foreground
216 style.SetRadioCheckTextColor( aFore );
217 style.SetLabelTextColor( aFore );
218 style.SetInfoTextColor( aFore );
219 style.SetDialogTextColor( aFore );
220 style.SetGroupTextColor( aFore );
222 // Text
223 style.SetFieldTextColor( aText );
224 style.SetFieldRolloverTextColor( aText );
225 style.SetWindowTextColor( aText );
227 // Base
228 style.SetFieldColor( aBase );
229 style.SetWindowColor( aBase );
230 style.SetActiveTabColor( aBase );
232 // Buttons
233 style.SetButtonTextColor( aButn );
234 style.SetButtonRolloverTextColor( aButn );
236 // Tabs
237 style.SetTabTextColor( aButn );
238 style.SetTabRolloverTextColor( aButn );
239 style.SetTabHighlightTextColor( aButn );
241 // Disable color
242 style.SetDisableColor( aMid );
244 // Workspace
245 style.SetWorkspaceColor( aMid );
247 // Background
248 style.Set3DColors( aBack );
249 style.SetFaceColor( aBack );
250 style.SetInactiveTabColor( aBack );
251 style.SetDialogColor( aBack );
252 style.SetCheckedColorSpecialCase( );
254 // Selection
255 style.SetHighlightColor( aHigh );
256 style.SetHighlightTextColor( toColor(pal.color( QPalette::HighlightedText)) );
258 // Tooltip
259 style.SetHelpColor( toColor( QToolTip::palette().color( QPalette::Active, QPalette::ToolTipBase )));
260 style.SetHelpTextColor( toColor( QToolTip::palette().color( QPalette::Active, QPalette::ToolTipText )));
262 // Font
263 vcl::Font aFont = toFont( QApplication::font(), rSettings.GetUILanguageTag().getLocale() );
265 style.SetAppFont( aFont );
267 style.SetMenuFont( aFont ); // will be changed according to pMenuBar
268 //style.SetToolFont( aFont ); //already set above
269 style.SetLabelFont( aFont );
270 style.SetInfoFont( aFont );
271 style.SetRadioCheckFont( aFont );
272 style.SetPushButtonFont( aFont );
273 style.SetFieldFont( aFont );
274 style.SetIconFont( aFont );
275 style.SetTabFont( aFont );
276 style.SetGroupFont( aFont );
278 aFont.SetWeight( WEIGHT_BOLD );
279 if( !bSetTitleFont )
281 style.SetTitleFont( aFont );
283 style.SetFloatTitleFont( aFont );
285 style.SetHelpFont( toFont( QToolTip::font(), rSettings.GetUILanguageTag().getLocale()));
287 int flash_time = QApplication::cursorFlashTime();
288 style.SetCursorBlinkTime( flash_time != 0 ? flash_time/2 : STYLE_CURSOR_NOBLINKTIME );
290 // Menu
291 style.SetSkipDisabledInMenus( TRUE );
292 KMenuBar* pMenuBar = new KMenuBar();
293 if ( pMenuBar )
295 // Color
296 QPalette qMenuCG = pMenuBar->palette();
298 // Menu text and background color, theme specific
299 Color aMenuFore = toColor( qMenuCG.color( QPalette::WindowText ) );
300 Color aMenuBack = toColor( qMenuCG.color( QPalette::Window ) );
302 style.SetMenuTextColor( aMenuFore );
303 style.SetMenuBarTextColor( style.GetPersonaMenuBarTextColor().get_value_or( aMenuFore ) );
304 style.SetMenuColor( aMenuBack );
305 style.SetMenuBarColor( aMenuBack );
306 style.SetMenuHighlightColor( toColor ( qMenuCG.color( QPalette::Highlight ) ) );
307 style.SetMenuHighlightTextColor( aMenuFore );
309 // set special menubar higlight text color
310 if ( QApplication::style()->inherits( "HighContrastStyle" ) )
311 ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor = toColor( qMenuCG.color( QPalette::HighlightedText ) );
312 else
313 ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor = aMenuFore;
315 // set menubar rollover color
316 if ( pMenuBar->style()->styleHint( QStyle::SH_MenuBar_MouseTracking ) )
318 style.SetMenuBarRolloverColor( toColor ( qMenuCG.color( QPalette::Highlight ) ) );
319 style.SetMenuBarRolloverTextColor( ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor );
321 else
323 style.SetMenuBarRolloverColor( aMenuBack );
324 style.SetMenuBarRolloverTextColor( aMenuFore );
326 style.SetMenuBarHighlightTextColor(style.GetMenuHighlightTextColor());
328 // Font
329 aFont = toFont( pMenuBar->font(), rSettings.GetUILanguageTag().getLocale() );
330 style.SetMenuFont( aFont );
333 delete pMenuBar;
335 // Scroll bar size
336 style.SetScrollBarSize( QApplication::style()->pixelMetric( QStyle::PM_ScrollBarExtent ) );
337 style.SetMinThumbSize( QApplication::style()->pixelMetric( QStyle::PM_ScrollBarSliderMin ));
339 rSettings.SetStyleSettings( style );
342 void KDESalFrame::ReleaseGraphics( SalGraphics *pGraphics )
344 for( int i = 0; i < nMaxGraphics; i++ )
346 if( m_aGraphics[i].pGraphics == pGraphics )
348 m_aGraphics[i].bInUse = false;
349 break;
354 void KDESalFrame::updateGraphics( bool bClear )
356 Drawable aDrawable = bClear ? None : GetWindow();
357 for( int i = 0; i < nMaxGraphics; i++ )
359 if( m_aGraphics[i].bInUse )
360 m_aGraphics[i].pGraphics->SetDrawable( aDrawable, GetScreenNumber() );
364 KDESalFrame::~KDESalFrame()
368 KDESalFrame::GraphicsHolder::~GraphicsHolder()
370 delete pGraphics;
373 SalGraphics* KDESalFrame::AcquireGraphics()
375 if( GetWindow() )
377 for( int i = 0; i < nMaxGraphics; i++ )
379 if( ! m_aGraphics[i].bInUse )
381 m_aGraphics[i].bInUse = true;
382 if( ! m_aGraphics[i].pGraphics )
384 m_aGraphics[i].pGraphics = new KDESalGraphics();
385 m_aGraphics[i].pGraphics->Init( this, GetWindow(), GetScreenNumber() );
387 return m_aGraphics[i].pGraphics;
392 return NULL;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */