summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/qwerty/Editor.java
blob: ec1423adba90a3edf72b7e768f64bb3a12f943f2 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/***************************************************************************
* $Id$
**
* Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
* This file is part of an example program for Qt.  This example
* program may be used, distributed and modified without limitation.
**
****************************************************************************/

import org.kde.qt.*;
import java.util.ArrayList;

class Editor extends TQWidget
{
private   TQMenuBar 	   m;
private   TQMultiLineEdit e;
private   TQPrinter        printer = new TQPrinter();
private   TQPopupMenu	   save_as;
private   TQPopupMenu	   open_as;
private   boolean changed;


private   boolean no_writing = false;

static ArrayList codecList = null;

static final int Uni = 0;
static final int MBug = 1;
static final int Lat1 = 2;
static final int Local = 3;
static final int Guess = 4;
static final int Codec = 5;


Editor( )
{
	this(null, null);
}

Editor( TQWidget  parent , String  name )
{
    super( parent, name, WDestructiveClose );
    m = new TQMenuBar( this, "menu" );

    TQPopupMenu  file = new TQPopupMenu(this);
    m.insertItem( "&File", file );

    file.insertItem( "&New",   this, SLOT("newDoc()"),   new TQKeySequence(ALT+Key_N) );
    file.insertItem( "&Open...",  this, SLOT("load()"),     new TQKeySequence(ALT+Key_O) );
    file.insertItem( "&Save...",  this, SLOT("save()"),     new TQKeySequence(ALT+Key_S) );
    file.insertSeparator();
    open_as = new TQPopupMenu(file);
    file.insertItem( "Open &As",  open_as );
    save_as = new TQPopupMenu(file);
    file.insertItem( "Sa&ve As",  save_as );
    file.insertItem( "Add &Encoding", this, SLOT("addEncoding()") );    
    file.insertSeparator();
    file.insertItem( "&Print...", this, SLOT("print()"),    new TQKeySequence(ALT+Key_P) );
    file.insertSeparator();
    file.insertItem( "&Close", this, SLOT("close()"),new TQKeySequence(ALT+Key_W) );
    file.insertItem( "&Quit",  tqApp(), SLOT("closeAllWindows()"),     new TQKeySequence(ALT+Key_Q) );

    connect( save_as, SIGNAL("activated(int)"), this, SLOT("saveAsEncoding(int)") );
    connect( open_as, SIGNAL("activated(int)"), this, SLOT("openAsEncoding(int)") );
    rebuildCodecList();

    TQPopupMenu  edit = new TQPopupMenu(m);
    m.insertItem( "&Edit", edit );

    edit.insertItem( "To &Uppercase",   this, SLOT("toUpper()"),   new TQKeySequence(ALT+Key_U) );
    edit.insertItem( "To &Lowercase",   this, SLOT("toLower()"),   new TQKeySequence(ALT+Key_L) );
    edit.insertSeparator();
    edit.insertItem( "&Select Font" ,	 this, SLOT("getFont()"),     new TQKeySequence(ALT+Key_F) );
    changed = false;
    e = new TQMultiLineEdit( this, "editor" );
    connect( e, SIGNAL(" textChanged()"), this, SLOT(" textChanged()") );

    // We use Unifont - if you have it installed you'll see all
    // Unicode character glyphs.
    //
    // Unifont only comes in one pixel size, so we cannot let
    // it change pixel size as the display DPI changes.
    //
    TQFont unifont = new TQFont("unifont",16,50); unifont.setPixelSize(16);
    e.setFont( unifont );

    e.setFocus();
}


public void getFont()
{
    boolean[] ok = { true };
    TQFont f = TQFontDialog.getFont( ok, e.font() );
    if ( ok[0] ) {
        e.setFont( f );
    }
}



void rebuildCodecList()
{
    codecList = new ArrayList();
    TQTextCodec codec = null;
    int i;
    for (i = 0; (codec = TQTextCodec.codecForIndex(i)) != null; i++)
	codecList.add( codec );
    int n = codecList.size();
    for (int pm=0; pm<2; pm++) {
	TQPopupMenu menu = pm != 0 ? open_as : save_as;
	menu.clear();
	String local = "Local (";
	local += TQTextCodec.codecForLocale().name();
	local += ")";
	menu.insertItem( local, Local );
	menu.insertItem( "Unicode", Uni );
	menu.insertItem( "Latin1", Lat1 );
	menu.insertItem( "Microsoft Unicode", MBug );
	if ( pm != 0 )
	    menu.insertItem( "[guess]", Guess );
	for ( i = 0; i < n; i++ )
	    menu.insertItem( ((TQTextCodec) codecList.get(i)).name(), Codec + i );
    }
}

void newDoc()
{
    Editor ed = new Editor();
    if ( tqApp().desktop().size().width() < 450
	 || tqApp().desktop().size().height() < 450 ) {
	ed.showMaximized();
    } else {
	ed.resize( 400, 400 );
	ed.show();
    }
}


void load()
{
    String fn = TQFileDialog.getOpenFileName( "", "", this );
    if ( fn != null )
	load( fn, -1 );
}

void load( String fileName )
{
	load(fileName, -1);
}

void load( String fileName, int code )
{
    TQFile f = new TQFile( fileName );
    if ( !f.open( TQIODevice.IO_ReadOnly ) )
	return;

    e.setAutoUpdate( false );

    TQTextStream t = new TQTextStream(f);
    if ( code >= Codec )
	t.setCodec( (TQTextCodec) codecList.get(code-Codec) );
    else if ( code == Uni )
	t.setEncoding( TQTextStream.Unicode );
    else if ( code == MBug )
	t.setEncoding( TQTextStream.UnicodeReverse );
    else if ( code == Lat1 )
	t.setEncoding( TQTextStream.Latin1 );
    else if ( code == Guess ) {
	f = new TQFile(fileName);
	f.open(TQIODevice.IO_ReadOnly);
	StringBuffer buffer = new StringBuffer();
	int l = 256;
	l=(int)f.readBlock(buffer,l);
	TQTextCodec codec = TQTextCodec.codecForContent(buffer.toString(), l);
	if ( codec != null ) {
	    TQMessageBox.information(this,"Encoding","Codec: "+codec.name());
	    t.setCodec( codec );
	}
    }
    e.setText( t.read() );
    f.close();

    e.setAutoUpdate( true );
    e.repaint();
    setCaption( fileName );

    changed = false;
}

void openAsEncoding( int code )
{
    //storing filename (proper save) is left as an exercise...
    String fn = TQFileDialog.getOpenFileName( "", "", this );
    if ( !fn.equals("") )
	load( fn, code );
}

boolean save()
{
    //storing filename (proper save) is left as an exercise...
    String fn = TQFileDialog.getSaveFileName( "", "", this );
    if ( !fn.equals("") )
	return saveAs( fn );
    return false;
}

void saveAsEncoding( int code )
{
    //storing filename (proper save) is left as an exercise...
    String fn = TQFileDialog.getSaveFileName( "", "", this );
    if ( !fn.equals("") )
	saveAs( fn, code );
}

void addEncoding()
{
    String fn = TQFileDialog.getOpenFileName( "", "*.map", this );
    if ( !fn.equals("") ) {
	TQFile f = new TQFile(fn);
	if (f.open(TQIODevice.IO_ReadOnly)) {
	    if (TQTextCodec.loadCharmap(f) != null) {
		rebuildCodecList();
	    } else {
		TQMessageBox.warning(null,"Charmap error",
		    "The file did not contain a valid charmap.\n\n"
		    + "A charmap file should look like this:\n"
		       + "  <code_set_name> thename\n"
		       + "  <escape_char> /\n"
		       + "  % alias thealias\n"
		       + "  CHARMAP\n"
		       + "  <tokenname> /x12 <U3456>\n"
		       + "  <tokenname> /xAB/x12 <U0023>\n"
		       + "  ...\n"
		       + "  END CHARMAP\n"
		);
	    }
	}
    }
}


boolean saveAs( String fileName )
{
	return saveAs(fileName, -1);
}

boolean saveAs( String fileName, int code )
{
    TQFile f = new TQFile( fileName );
    if ( no_writing || !f.open( TQIODevice.IO_WriteOnly ) ) {
	TQMessageBox.warning(this,"I/O Error",
		    "The file could not be opened.\n\n"
			+fileName);
	return false;
    }
    TQTextStream t = new TQTextStream(f);
    if ( code >= Codec )
	t.setCodec( (TQTextCodec) codecList.get(code-Codec) );
    else if ( code == Uni )
	t.setEncoding( TQTextStream.Unicode );
    else if ( code == MBug )
	t.setEncoding( TQTextStream.UnicodeReverse );
    else if ( code == Lat1 )
	t.setEncoding( TQTextStream.Latin1 );
	t.writeRawBytes(e.text(), e.text().length());
    f.close();
    setCaption( fileName );
    changed = false;
    return true;
}

void print()
{
    if ( printer.setup(this) ) {		// opens printer dialog
	printer.setFullPage(true);		// we'll set our own margins
	TQPainter p = new TQPainter();
	p.begin( printer );			// paint on printer
	p.setFont( e.font() );
	TQFontMetrics fm = p.fontMetrics();
	TQPaintDeviceMetrics metrics = new TQPaintDeviceMetrics( printer ); // need width/height
						 // of printer surface
	int MARGIN = metrics.logicalDpiX() / 2; // half-inch margin
	int yPos        = MARGIN;		// y position for each line

	for( int i = 0 ; i < e.numLines() ; i++ ) {
	    if ( printer.aborted() )
		break;
	    if ( yPos + fm.lineSpacing() > metrics.height() - MARGIN ) {
		// no more room on this page
		if ( !printer.newPage() )          // start new page
		    break;                           // some error
		yPos = MARGIN;			 // back to top of page
	    }
	    p.drawText( MARGIN, yPos, metrics.width() - 2*MARGIN,
			fm.lineSpacing(), ExpandTabs, e.textLine( i ) );
	    yPos += fm.lineSpacing();
	}
	p.end();				// send job to printer
    }
}

protected void resizeEvent( TQResizeEvent event )
{
    if ( e != null && m != null )
	e.setGeometry( 0, m.height(), width(), height() - m.height() );
}

protected void closeEvent( TQCloseEvent event )
{
    event.accept();

    if ( changed ) { // the text has been changed
	switch ( TQMessageBox.warning( this, "Qwerty",
					"Save changes to Document?",
					tr("&Yes"),
					tr("&No"),
					tr("Cancel"),
					0, 2) ) {
	case 0: // yes
	    if ( save() )
		event.accept();
	    else
		event.ignore();
	    break;
	case 1: // no
	    event.accept();
	    break;
	default: // cancel
	    event.ignore();
	    break;
	}
    }
}

void toUpper()
{
    e.setText(e.text().toUpperCase());
}

void toLower()
{
    e.setText(e.text().toLowerCase());
}

void textChanged()
{
    changed = true;
}
}