summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/tinykate.cpp
blob: f177e4735524269ac1ac94d7c3c028a0f9b30333 (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
/***************************************************************************
                             tinykate.cpp
                        Tiny KATE mainwindow
                             -------------------
    begin                : November 2002
    copyright            : (C) 2002 by Joseph Wenninger <jowenn@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                         *
 *   ONLY VERSION 2 OF THE LICENSE IS APPLICABLE                           *
 *                                                                         *
 ***************************************************************************/
#include <qwidget.h>
#include <qaction.h>
#include <qlayout.h>
#include <qpe/qpetoolbar.h>
#include <qpe/qpemenubar.h>
#include <qpe/resource.h>
#include <qpe/global.h>
#include <qpe/qpeapplication.h>

#include <opie/ofiledialog.h>

#include "tinykate.h"
#include "pics/file-new.xpm"
#include "pics/file-open.xpm"
#include "pics/file-save.xpm"
#include "pics/edit-undo.xpm"
#include "pics/edit-redo.xpm"

#include <katedocument.h>
#include <katehighlight.h>

TinyKate::TinyKate( QWidget *parent, const char *name, WFlags f) :
    QMainWindow( parent, name, f )
{	
	nextUnnamed=0;
	currentView=0;
	viewCount=0;
	setCaption(tr("TinyKATE"));

	setToolBarsMovable(FALSE);

        QPEToolBar *bar = new QPEToolBar( this );
        bar->setHorizontalStretchable( TRUE );
        QPEMenuBar *mb = new QPEMenuBar( bar );
        mb->setMargin( 0 );

	tabwidget=new OTabWidget(this);
	setCentralWidget(tabwidget);
	connect(tabwidget,SIGNAL(currentChanged( QWidget *)),this,SLOT(slotCurrentChanged(QWidget *)));

//FILE ACTIONS
        QPopupMenu *popup = new QPopupMenu( this );

	// Action for creating a new document
        QAction *a = new QAction( tr( "New" ), QPixmap((const char**)file_new_xpm ), QString::null, 0, this, 0 );
        a->addTo( popup );
        connect(a, SIGNAL(activated()), this, SLOT(slotNew()));

	// Action for opening an exisiting document
        a = new QAction( tr( "Open" ), QPixmap((const char**)file_open_xpm), QString::null, 0, this, 0 );
	a->addTo(popup);
        connect(a, SIGNAL(activated()), this, SLOT(slotOpen()));


	// Action for saving  document
        a = new QAction( tr( "Save" ), QPixmap((const char**)file_save_xpm), QString::null, 0, this, 0 );
	a->addTo(popup);

	// Action for saving document to a new name
        a = new QAction( tr( "Save As" ), QPixmap((const char**)file_save_xpm), QString::null, 0, this, 0 );
	a->addTo(popup);

	// Action for closing the currently active document
        a = new QAction( tr( "Close" ), QPixmap(), QString::null, 0, this, 0 );
	a->addTo(popup);
        connect(a, SIGNAL(activated()), this, SLOT(slotClose()));
	

	mb->insertItem(tr("File"),popup);

//EDIT ACTIONS

	// Action for cutting text
        editCut = new QAction( tr( "Cut" ), Resource::loadPixmap( "cut" ), QString::null, 0, this, 0 );
        editCut->addTo( bar );

	// Action for Copying text
        editCopy = new QAction( tr( "Copy" ), Resource::loadPixmap( "copy" ), QString::null, 0, this, 0 );
        editCopy->addTo( bar );

	// Action for pasting text
        editPaste = new QAction( tr( "Paste" ), Resource::loadPixmap( "paste" ), QString::null, 0, this, 0 );
        editPaste->addTo( bar );


	// Action for finding / replacing  text
        editFindReplace = new QAction( tr( "Find/Replace" ), Resource::loadPixmap("find"), QString::null, 0, this, 0 );
        editFindReplace->addTo( bar );

	// Action for undo
        editUndo = new QAction( tr( "Undo" ), QPixmap((const char**)edit_undo_xpm), QString::null, 0, this, 0 );
        editUndo->addTo( bar );

	// Action for redo
        editRedo = new QAction( tr( "Redo" ), QPixmap((const char**)edit_redo_xpm), QString::null, 0, this, 0 );
        editRedo->addTo( bar );

//VIEW ACITONS
        popup = new QPopupMenu( this );
	
        viewIncFontSizes = new QAction( tr( "Font +" ), QString::null, 0, this, 0 );
        viewIncFontSizes->addTo( popup );

        viewDecFontSizes = new QAction( tr( "Font -" ), QString::null, 0, this, 0 );
        viewDecFontSizes->addTo( popup );

	mb->insertItem(tr("View"),popup);



        popup = new QPopupMenu( this );
	mb->insertItem(tr("Utils"),popup);

//Highlight management
	hlmenu=new QPopupMenu(this);
	HlManager *hlm=HlManager::self();
	for (int i=0;i<hlm->highlights();i++)
	{
		hlmenu->insertItem(hlm->hlName(i),i);
	}
	popup->insertItem(tr("Highlighting"),hlmenu);


        utilSettings = new QAction( tr( "Settings" ), QString::null, 0, this, 0 );
        utilSettings->addTo( popup);

  if( qApp->argc() > 1) open(qApp->argv()[1]);
  else slotNew();

}


void TinyKate::slotOpen( )
{
	QString filename=OFileDialog::getOpenFileName(OFileSelector::EXTENDED_ALL);
	if (!filename.isEmpty()) {	
		open(filename);
	}
}

void TinyKate::open(const QString & filename)
{
		KateDocument *kd= new KateDocument(false, false, this,0,this);
		KTextEditor::View *kv;
			tabwidget->addTab(kv=kd->createView(tabwidget,"bLAH"),"tinykate/tinykate","BLAH");
		qDebug(filename);
		kd->open(filename);
		viewCount++;
}

void TinyKate::slotCurrentChanged( QWidget * view)
{
	if (currentView) {
		disconnect(editCopy,SIGNAL(activated()),currentView,SLOT(copy()));
		disconnect(editCut,SIGNAL(activated()),currentView,SLOT(cut()));
		disconnect(editPaste,SIGNAL(activated()),currentView,SLOT(paste()));
		disconnect(editUndo,SIGNAL(activated()),currentView,SLOT(undo()));
		disconnect(editRedo,SIGNAL(activated()),currentView,SLOT(redo()));
    		disconnect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
    		disconnect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
		disconnect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
		disconnect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));
	}

	currentView=(KTextEditor::View*)view;
	
	connect(editCopy,SIGNAL(activated()),currentView,SLOT(copy()));
	connect(editCut,SIGNAL(activated()),currentView,SLOT(cut()));
	connect(editPaste,SIGNAL(activated()),currentView,SLOT(paste()));
	connect(editUndo,SIGNAL(activated()),currentView,SLOT(undo()));
	connect(editRedo,SIGNAL(activated()),currentView,SLOT(redo()));
    	connect(viewIncFontSizes,SIGNAL(activated()), currentView,SLOT(slotIncFontSizes()));
    	connect(viewDecFontSizes,SIGNAL(activated()), currentView,SLOT(slotDecFontSizes()));
	connect(hlmenu,SIGNAL(activated(int)), currentView,SLOT(setHl(int)));
	connect(utilSettings,SIGNAL(activated()), currentView,SLOT(configDialog()));

}

void TinyKate::slotNew( )
{
	KateDocument *kd= new KateDocument(false, false, this,0,this);
	KTextEditor::View *kv;
	tabwidget->addTab(kv=kd->createView(tabwidget,"BLAH"),"tinykate/tinykate",tr("Unnamed %1").arg(nextUnnamed++));
	viewCount++;
}

void TinyKate::slotClose( )
{
	if (currentView==0) return;
	KTextEditor::View *dv=currentView;
	currentView=0;
	tabwidget->removePage(dv);
	delete dv->document();
	viewCount--;
	if (!viewCount) slotNew();
}