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