summaryrefslogtreecommitdiffstats
path: root/src/modules/term/termwidget.cpp
blob: 04d6156713dc1cdf760337e77b010acdc9c5be6c (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
//
//   File : termwidget.cpp
//   Creation date : Thu Aug 10 2000 17:42:12 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2005 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//

#include "termwidget.h"
#include "termwindow.h"

#include "kvi_module.h"
#include "kvi_frame.h"
#include "kvi_iconmanager.h"
#include "kvi_locale.h"

#include <tqlabel.h>
#include "kvi_pointerlist.h"
#include <tqtooltip.h>
#include <tqtimer.h>

#ifdef COMPILE_KDE_SUPPORT

#include "klibloader.h"
#include "kparts/part.h"
#include "kparts/factory.h"

extern KviModule            * g_pTermModule;
extern KviPointerList<KviTermWidget> * g_pTermWidgetList;
extern KviPointerList<KviTermWindow> * g_pTermWindowList;
extern KviStr                 g_szKonsoleLibraryName;

KviTermWidget::KviTermWidget(TQWidget * par,KviFrame * lpFrm,bool bIsStandalone)
: TQFrame(par,"term_widget")
{
	if(bIsStandalone)g_pTermWidgetList->append(this);
	m_bIsStandalone = bIsStandalone;

	m_pKonsolePart = 0;
	m_pKonsoleWidget = 0;

	if(bIsStandalone)
	{
		m_pHBox = new KviTalHBox(this);
		m_pTitleLabel = new TQLabel(__tr2qs("Terminal emulator"),m_pHBox);
		m_pTitleLabel->setFrameStyle(TQFrame::Raised | TQFrame::WinPanel);
		m_pCloseButton = new TQPushButton("",m_pHBox);
		m_pCloseButton->setPixmap(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_CLOSE)));
		TQToolTip::add(m_pCloseButton,__tr2qs("Close this window"));
		m_pHBox->setStretchFactor(m_pTitleLabel,2);
		connect(m_pCloseButton,TQT_SIGNAL(clicked()),this,TQT_SLOT(closeClicked()));
	} else {
		m_pHBox = 0;
		m_pTitleLabel = 0;
		m_pCloseButton = 0;
	}

	setFrameStyle(TQFrame::Sunken | TQFrame::Panel);

	KParts::Factory * pKonsoleFactory = static_cast<KParts::Factory *>(
			KLibLoader::self()->factory(g_szKonsoleLibraryName.ptr()));

	if(pKonsoleFactory)
	{
//		debug("FACTORY %d",pKonsoleFactory);
		m_pKonsolePart = static_cast<KParts::Part *>(pKonsoleFactory->createPart(
				this,"terminal emulator",this,"the konsole part"));

		if(m_pKonsolePart)
		{
//			debug("PART %d",m_pKonsolePart);
			m_pKonsoleWidget = m_pKonsolePart->widget();
			connect(m_pKonsoleWidget,TQT_SIGNAL(destroyed()),this,TQT_SLOT(konsoleDestroyed()));
//			debug("Widget %d",m_pKonsoleWidget);
		} else {
			m_pKonsoleWidget = new TQLabel(this,
				__tr2qs("Can't create the terminal emulation part"));
		}

	} else {
		m_pKonsoleWidget = new TQLabel(this,
			__tr2qs("Can't retrieve the terminal emulation factory"));
	}
}

KviTermWidget::~KviTermWidget()
{
	if(m_pKonsoleWidget)
		disconnect(m_pKonsoleWidget,TQT_SIGNAL(destroyed()),this,TQT_SLOT(konsoleDestroyed()));

	if(m_bIsStandalone)g_pTermWidgetList->removeRef(this);
	if(g_pTermWindowList->isEmpty() && g_pTermWidgetList->isEmpty())g_pTermModule->unlock();

//	debug("DELETING KONSOLE WIDGET");
//	if(m_pKonsoleWidget)delete m_pKonsoleWidget; <--// TQt will delete it
//	debug("DELETING KONSOLE PART");
//	if(m_pKonsolePart)delete m_pKonsolePart; <--// the part will delete self when the widget will die
//	debug("KONSOLE PART DELETED");
}

void KviTermWidget::resizeEvent(TQResizeEvent *e)
{
	int hght = 0;
	if(m_bIsStandalone)
	{
		hght = m_pCloseButton->tqsizeHint().height();
		m_pHBox->setGeometry(1,1,width() - 2,hght + 1);

	}
	if(m_pKonsoleWidget)m_pKonsoleWidget->setGeometry(1,hght + 1,width() - 2,height() - (hght + 2));
}

void KviTermWidget::closeClicked()
{
	// this is called only in standalone  mode
	delete this;
}

void KviTermWidget::konsoleDestroyed()
{
	m_pKonsoleWidget = 0;
	m_pKonsolePart = 0;
	hide();
	TQTimer::singleShot(0,this,TQT_SLOT(autoClose()));
}

void KviTermWidget::autoClose()
{
	if(m_bIsStandalone)delete this;
	else ((KviWindow *)tqparent())->close();
}

void KviTermWidget::changeTitle(int i,const TQString& str)
{
	if(m_bIsStandalone)m_pTitleLabel->setText(str);
}

void KviTermWidget::notifySize(int,int)
{
}

void KviTermWidget::changeColumns(int)
{
}

TQSize KviTermWidget::tqsizeHint() const
{
	int hght = 0;
	int wdth = 0;
	if(m_pKonsoleWidget)
	{
		hght += m_pKonsoleWidget->tqsizeHint().height();
		wdth = m_pKonsoleWidget->tqsizeHint().width();
	}
	if(m_pCloseButton)
	{
		hght += m_pCloseButton->tqsizeHint().height();
	}
	
	return TQSize(wdth + 2,hght + 2);
}

#include "termwidget.moc"

#endif //COMPILE_KDE_SUPPORT