xref: /aoo41x/main/vcl/unx/kde4/KDESalFrame.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define Region QtXRegion
29 
30 #include <QColor>
31 #include <QStyle>
32 
33 #include <kconfig.h>
34 #include <kglobal.h>
35 #include <kmenubar.h>
36 #include <kconfiggroup.h>
37 #include <kmainwindow.h>
38 #include <kapplication.h>
39 #include <ktoolbar.h>
40 
41 #undef Region
42 
43 #include "KDESalFrame.hxx"
44 #include "KDEXLib.hxx"
45 #include "KDESalGraphics.hxx"
46 
47 #include <tools/color.hxx>
48 
49 #include <vcl/settings.hxx>
50 #include <vcl/font.hxx>
51 
52 #include <svdata.hxx>
53 
54 #include <unx/pspgraphics.h>
55 
56 #if OSL_DEBUG_LEVEL > 1
57 #include <stdio.h>
58 #endif
59 
60 KDESalFrame::KDESalFrame( SalFrame* pParent, sal_uLong nState ) :
61     X11SalFrame( pParent, nState )
62 {
63 }
64 
65 void KDESalFrame::Show( sal_Bool bVisible, sal_Bool bNoActivate )
66 {
67     if ( !GetParent() && ! (GetStyle() & SAL_FRAME_STYLE_INTRO) )
68     {
69         KDEXLib* pXLib = static_cast<KDEXLib*>(GetDisplay()->GetXLib());
70         pXLib->doStartup();
71     }
72 
73     X11SalFrame::Show( bVisible, bNoActivate );
74 }
75 
76 /** Helper function to convert colors.
77 */
78 static Color toColor( const QColor &rColor )
79 {
80     return Color( rColor.red(), rColor.green(), rColor.blue() );
81 }
82 
83 /** Helper function to read untranslated text entry from KConfig configuration repository.
84 */
85 static OUString readEntryUntranslated( KConfigGroup *pGroup, const char *pKey )
86 {
87     return OUString::createFromAscii( (const char *) pGroup->readEntryUntranslated( pKey ).toAscii() );
88 }
89 
90 #if 0
91 /** Helper function to read color from KConfig configuration repository.
92 */
93 static Color readColor( KConfigGroup *pGroup, const char *pKey )
94 {
95     return toColor( pGroup->readEntry( pKey, QColor(Qt::white) ) );
96 }
97 #endif
98 
99 /** Helper function to add information to Font from QFont.
100 
101     Mostly grabbed from the Gtk+ vclplug (salnativewidgets-gtk.cxx).
102 */
103 static Font toFont( const QFont &rQFont, const ::com::sun::star::lang::Locale& rLocale )
104 {
105     psp::FastPrintFontInfo aInfo;
106     QFontInfo qFontInfo( rQFont );
107 
108     // set family name
109     aInfo.m_aFamilyName = String( (const char *) rQFont.family().toUtf8(), RTL_TEXTENCODING_UTF8 );
110 
111     // set italic
112     aInfo.m_eItalic = ( qFontInfo.italic()? psp::italic::Italic: psp::italic::Upright );
113 
114     // set weight
115     int nWeight = qFontInfo.weight();
116     if ( nWeight <= QFont::Light )
117         aInfo.m_eWeight = psp::weight::Light;
118     else if ( nWeight <= QFont::Normal )
119         aInfo.m_eWeight = psp::weight::Normal;
120     else if ( nWeight <= QFont::DemiBold )
121         aInfo.m_eWeight = psp::weight::SemiBold;
122     else if ( nWeight <= QFont::Bold )
123         aInfo.m_eWeight = psp::weight::Bold;
124     else
125         aInfo.m_eWeight = psp::weight::UltraBold;
126 
127     // set width
128     int nStretch = rQFont.stretch();
129     if ( nStretch <= QFont::UltraCondensed )
130         aInfo.m_eWidth = psp::width::UltraCondensed;
131     else if ( nStretch <= QFont::ExtraCondensed )
132         aInfo.m_eWidth = psp::width::ExtraCondensed;
133     else if ( nStretch <= QFont::Condensed )
134         aInfo.m_eWidth = psp::width::Condensed;
135     else if ( nStretch <= QFont::SemiCondensed )
136         aInfo.m_eWidth = psp::width::SemiCondensed;
137     else if ( nStretch <= QFont::Unstretched )
138         aInfo.m_eWidth = psp::width::Normal;
139     else if ( nStretch <= QFont::SemiExpanded )
140         aInfo.m_eWidth = psp::width::SemiExpanded;
141     else if ( nStretch <= QFont::Expanded )
142         aInfo.m_eWidth = psp::width::Expanded;
143     else if ( nStretch <= QFont::ExtraExpanded )
144         aInfo.m_eWidth = psp::width::ExtraExpanded;
145     else
146         aInfo.m_eWidth = psp::width::UltraExpanded;
147 
148 #if OSL_DEBUG_LEVEL > 1
149     fprintf( stderr, "font name BEFORE system match: \"%s\"\n", OUStringToOString( aInfo.m_aFamilyName, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
150 #endif
151 
152     // match font to e.g. resolve "Sans"
153     psp::PrintFontManager::get().matchFont( aInfo, rLocale );
154 
155 #if OSL_DEBUG_LEVEL > 1
156     fprintf( stderr, "font match %s, name AFTER: \"%s\"\n",
157              aInfo.m_nID != 0 ? "succeeded" : "failed",
158              OUStringToOString( aInfo.m_aFamilyName, RTL_TEXTENCODING_ISO_8859_1 ).getStr() );
159 #endif
160 
161     // font height
162     int nPointHeight = qFontInfo.pointSize();
163     if ( nPointHeight <= 0 )
164         nPointHeight = rQFont.pointSize();
165 
166     // Create the font
167     Font aFont( aInfo.m_aFamilyName, Size( 0, nPointHeight ) );
168     if( aInfo.m_eWeight != psp::weight::Unknown )
169         aFont.SetWeight( PspGraphics::ToFontWeight( aInfo.m_eWeight ) );
170     if( aInfo.m_eWidth != psp::width::Unknown )
171         aFont.SetWidthType( PspGraphics::ToFontWidth( aInfo.m_eWidth ) );
172     if( aInfo.m_eItalic != psp::italic::Unknown )
173         aFont.SetItalic( PspGraphics::ToFontItalic( aInfo.m_eItalic ) );
174     if( aInfo.m_ePitch != psp::pitch::Unknown )
175         aFont.SetPitch( PspGraphics::ToFontPitch( aInfo.m_ePitch ) );
176 
177     return aFont;
178 }
179 
180 /** Implementation of KDE integration's main method.
181 */
182 void KDESalFrame::UpdateSettings( AllSettings& rSettings )
183 {
184     StyleSettings style( rSettings.GetStyleSettings() );
185 	bool bSetTitleFont = false;
186 
187 	// General settings
188     QPalette pal = kapp->palette();
189 
190 	style.SetActiveColor(toColor(pal.color(QPalette::Active, QPalette::Window)));
191 	style.SetDeactiveColor(toColor(pal.color(QPalette::Inactive, QPalette::Window)));
192 
193 	style.SetActiveColor2(toColor(pal.color(QPalette::Active, QPalette::Window)));
194 	style.SetDeactiveColor2(toColor(pal.color(QPalette::Inactive, QPalette::Window)));
195 
196 	style.SetActiveTextColor(toColor(pal.color(QPalette::Active, QPalette::WindowText)));
197 	style.SetDeactiveTextColor(toColor(pal.color(QPalette::Inactive, QPalette::WindowText)));
198 
199     // WM settings
200     KConfig *pConfig = KGlobal::config().data();
201     if ( pConfig )
202     {
203         KConfigGroup aGroup = pConfig->group( "WM" );
204         const char *pKey;
205 
206         pKey = "titleFont";
207         if ( aGroup.hasKey( pKey ) )
208         {
209             Font aFont = toFont( aGroup.readEntry( pKey, QFont() ), rSettings.GetUILocale() );
210             style.SetTitleFont( aFont );
211 			bSetTitleFont = true;
212         }
213 
214         aGroup = pConfig->group( "Icons" );
215 
216         pKey = "Theme";
217         if ( aGroup.hasKey( pKey ) )
218             style.SetPreferredSymbolsStyleName( readEntryUntranslated( &aGroup, pKey ) );
219 
220 		//toolbar
221 		pKey = "toolbarFont";
222 		if ( aGroup.hasKey( pKey ) )
223 		{
224 			Font aFont = toFont( aGroup.readEntry( pKey, QFont() ), rSettings.GetUILocale() );
225 			style.SetToolFont( aFont );
226 		}
227     }
228 
229     Color aFore = toColor( pal.color( QPalette::Active, QPalette::WindowText ) );
230     Color aBack = toColor( pal.color( QPalette::Active, QPalette::Window ) );
231     Color aText = toColor( pal.color( QPalette::Active, QPalette::Text ) );
232     Color aBase = toColor( pal.color( QPalette::Active, QPalette::Base ) );
233     Color aButn = toColor( pal.color( QPalette::Active, QPalette::ButtonText ) );
234     Color aMid = toColor( pal.color( QPalette::Active, QPalette::Mid ) );
235     Color aHigh = toColor( pal.color( QPalette::Active, QPalette::Highlight ) );
236 
237     // Foreground
238     style.SetRadioCheckTextColor( aFore );
239     style.SetLabelTextColor( aFore );
240     style.SetInfoTextColor( aFore );
241     style.SetDialogTextColor( aFore );
242     style.SetGroupTextColor( aFore );
243 
244     // Text
245     style.SetFieldTextColor( aText );
246     style.SetFieldRolloverTextColor( aText );
247     style.SetWindowTextColor( aText );
248     style.SetHelpTextColor( aText );
249 
250     // Base
251     style.SetFieldColor( aBase );
252     style.SetHelpColor( aBase );
253     style.SetWindowColor( aBase );
254     style.SetActiveTabColor( aBase );
255 
256     // Buttons
257     style.SetButtonTextColor( aButn );
258     style.SetButtonRolloverTextColor( aButn );
259 
260     // Disable color
261     style.SetDisableColor( aMid );
262 
263     // Workspace
264     style.SetWorkspaceColor( aMid );
265 
266     // Background
267     style.Set3DColors( aBack );
268     style.SetFaceColor( aBack );
269     style.SetInactiveTabColor( aBack );
270     style.SetDialogColor( aBack );
271 
272     if( aBack == COL_LIGHTGRAY )
273         style.SetCheckedColor( Color( 0xCC, 0xCC, 0xCC ) );
274     else
275     {
276         Color aColor2 = style.GetLightColor();
277         style.
278             SetCheckedColor( Color( (sal_uInt8)(((sal_uInt16)aBack.GetRed()+(sal_uInt16)aColor2.GetRed())/2),
279                         (sal_uInt8)(((sal_uInt16)aBack.GetGreen()+(sal_uInt16)aColor2.GetGreen())/2),
280                         (sal_uInt8)(((sal_uInt16)aBack.GetBlue()+(sal_uInt16)aColor2.GetBlue())/2)
281                         ) );
282     }
283 
284     // Selection
285     style.SetHighlightColor( aHigh );
286     style.SetHighlightTextColor( toColor(pal.color( QPalette::HighlightedText))  );
287 
288     // Font
289     Font aFont = toFont( kapp->font(), rSettings.GetUILocale() );
290 
291     style.SetAppFont( aFont );
292     style.SetHelpFont( aFont );
293 
294 	if( !bSetTitleFont )
295 	{
296 		style.SetTitleFont( aFont );
297 	}
298 
299 	style.SetFloatTitleFont( aFont );
300     style.SetMenuFont( aFont ); // will be changed according to pMenuBar
301     //style.SetToolFont( aFont ); //already set above
302     style.SetLabelFont( aFont );
303     style.SetInfoFont( aFont );
304     style.SetRadioCheckFont( aFont );
305     style.SetPushButtonFont( aFont );
306     style.SetFieldFont( aFont );
307     style.SetIconFont( aFont );
308     style.SetGroupFont( aFont );
309 
310     int flash_time = QApplication::cursorFlashTime();
311     style.SetCursorBlinkTime( flash_time != 0 ? flash_time/2 : STYLE_CURSOR_NOBLINKTIME );
312 
313     // Menu
314     style.SetSkipDisabledInMenus( TRUE );
315     KMenuBar* pMenuBar = new KMenuBar();
316     if ( pMenuBar )
317     {
318         // Color
319         QPalette qMenuCG = pMenuBar->palette();
320 
321         // Menu text and background color, theme specific
322         Color aMenuFore = toColor( qMenuCG.color( QPalette::WindowText ) );
323         Color aMenuBack = toColor( qMenuCG.color( QPalette::Window ) );
324 
325         aMenuFore = toColor( qMenuCG.color( QPalette::ButtonText ) );
326         aMenuBack = toColor( qMenuCG.color( QPalette::Button ) );
327 
328         style.SetMenuTextColor( aMenuFore );
329         style.SetMenuBarTextColor( aMenuFore );
330         style.SetMenuColor( aMenuBack );
331         style.SetMenuBarColor( aMenuBack );
332 
333         style.SetMenuHighlightColor( toColor ( qMenuCG.color( QPalette::Highlight ) ) );
334 
335         style.SetMenuHighlightTextColor( aMenuFore );
336 
337         // set special menubar higlight text color
338         if ( kapp->style()->inherits( "HighContrastStyle" ) )
339             ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor = toColor( qMenuCG.color( QPalette::HighlightedText ) );
340         else
341             ImplGetSVData()->maNWFData.maMenuBarHighlightTextColor = aMenuFore;
342 
343         // Font
344         aFont = toFont( pMenuBar->font(), rSettings.GetUILocale() );
345         style.SetMenuFont( aFont );
346     }
347 
348 	delete pMenuBar;
349 
350     // Scroll bar size
351     style.SetScrollBarSize( kapp->style()->pixelMetric( QStyle::PM_ScrollBarExtent ) );
352 
353     rSettings.SetStyleSettings( style );
354 }
355 
356 
357 void KDESalFrame::ReleaseGraphics( SalGraphics *pGraphics )
358 {
359     for( int i = 0; i < nMaxGraphics; i++ )
360     {
361         if( m_aGraphics[i].pGraphics == pGraphics )
362         {
363             m_aGraphics[i].bInUse = false;
364             break;
365         }
366     }
367 }
368 
369 void KDESalFrame::updateGraphics( bool bClear )
370 {
371     Drawable aDrawable = bClear ? None : GetWindow();
372     for( int i = 0; i < nMaxGraphics; i++ )
373     {
374         if( m_aGraphics[i].bInUse )
375             m_aGraphics[i].pGraphics->SetDrawable( aDrawable, GetScreenNumber() );
376     }
377 }
378 
379 KDESalFrame::~KDESalFrame()
380 {
381 }
382 
383 KDESalFrame::GraphicsHolder::~GraphicsHolder()
384 {
385 	delete pGraphics;
386 }
387 
388 SalGraphics* KDESalFrame::GetGraphics()
389 {
390     if( GetWindow() )
391     {
392         for( int i = 0; i < nMaxGraphics; i++ )
393         {
394             if( ! m_aGraphics[i].bInUse )
395             {
396                 m_aGraphics[i].bInUse = true;
397                 if( ! m_aGraphics[i].pGraphics )
398                 {
399                     m_aGraphics[i].pGraphics = new KDESalGraphics();
400                     m_aGraphics[i].pGraphics->Init( this, GetWindow(), GetScreenNumber() );
401                 }
402                 return m_aGraphics[i].pGraphics;
403             }
404         }
405     }
406 
407     return NULL;
408 }
409