summaryrefslogtreecommitdiffstats
path: root/lib/compatibility/tdemdi/qextmdi/tdemdichildfrmcaption.cpp
blob: f07e1a56584262da9c6322a472660fd41a395da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//----------------------------------------------------------------------------
//    filename             : tdemdichildfrmcaption.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 TQt extension set of
//                                         classes and a TQt-based library
//                           2000-2003     maintained by the KDevelop project
//
//    copyright            : (C) 1999-2003 by Szymon Stefanek ([email protected])
//                                         and
//                                         Falk Brettschneider
//    email                :  [email protected] (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 "tdemdichildfrmcaption.h"
#include "tdemdichildfrmcaption.moc"

#include <tqpainter.h>
#include <tqapplication.h>
#include <tqcursor.h>
#include <tqtoolbutton.h>
#include <tqpopupmenu.h>

#include "tdemdidefines.h"
#include "tdemdichildfrm.h"
#include "tdemdichildarea.h"
#include "tdemdimainfrm.h"
#include <tdelocale.h>
#include <iostream>

#ifdef TQ_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 )
		: TQWidget( parent, "tdemdi_childfrmcaption" )
{
	m_szCaption = i18n( "Unnamed" );
	m_bActive = false;
	m_pParent = parent;
	setBackgroundMode( NoBackground );
	setFocusPolicy( TQ_NoFocus );
	m_bChildInDrag = false;
}

//============== ~KMdiChildFrmCaption =============//

KMdiChildFrmCaption::~KMdiChildFrmCaption()
{}

//============= mousePressEvent ==============//

void KMdiChildFrmCaption::mousePressEvent( TQMouseEvent *e )
{
	if ( e->button() == Qt::LeftButton )
	{
		setMouseTracking( false );
		if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
		{
			TQApplication::setOverrideCursor( TQt::sizeAllCursor, true );
		}
		m_pParent->m_bDragging = true;
		m_offset = mapToParent( e->pos() );
	}
	else if ( e->button() == Qt::RightButton )
	{
		m_pParent->systemMenu()->popup( mapToGlobal( e->pos() ) );
	}
}

//============= mouseReleaseEvent ============//

void KMdiChildFrmCaption::mouseReleaseEvent( TQMouseEvent *e )
{
	if ( e->button() == Qt::LeftButton )
	{
		if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
			TQApplication::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 )
					TQApplication::sendEvent( m_pParent->m_pClient, &ue );
				
				m_bChildInDrag = false;
			}
		}
	}
}

//============== mouseMoveEvent =============//
void KMdiChildFrmCaption::mouseMoveEvent( TQMouseEvent *e )
{
	if ( !m_pParent->m_bDragging )
		return ;

	if ( !m_bChildInDrag )
	{
		//notify child view
		KMdiChildFrmDragBeginEvent ue( e );
		if ( m_pParent->m_pClient != 0L )
			TQApplication::sendEvent( m_pParent->m_pClient, &ue );
		
		m_bChildInDrag = true;
	}

	TQPoint 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();
	}
	TQPoint 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 TQString& 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( TQPaintEvent * )
{
	TQPainter p( this );
	TQRect 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;
	TQString text = abbreviateText( m_szCaption, captionWidthForText );
	p.drawText( r, AlignVCenter | AlignLeft | SingleLine, text );

}


TQString KMdiChildFrmCaption::abbreviateText( TQString origStr, int maxWidth )
{
	TQFontMetrics 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;
	TQString 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 );
			}
		}
		TQFontMetrics fm = fontMetrics();
		w = fm.width( s );
		newLetterCount--;
	}
	return s;
}

//============= mouseDoubleClickEvent ===========//

void KMdiChildFrmCaption::mouseDoubleClickEvent( TQMouseEvent * )
{
	m_pParent->maximizePressed();
}

//============= slot_moveViaSystemMenu ===========//

void KMdiChildFrmCaption::slot_moveViaSystemMenu()
{
	setMouseTracking( true );
	grabMouse();
	
	if ( KMdiMainFrm::frameDecorOfAttachedViews() != KMdi::Win95Look )
		TQApplication::setOverrideCursor( TQt::sizeAllCursor, true );
	
	m_pParent->m_bDragging = true;
	m_offset = mapFromGlobal( TQCursor::pos() );
}