summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/textedit/TextEdit.java
blob: 7135f0e684504c4bb1a20c1ba061870d3a697585 (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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/***************************************************************************
* $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.HashMap;

class TextEdit  extends TQMainWindow
{


private    TQAction actionTextBold,
	actionTextUnderline,
	actionTextItalic,
	actionTextColor,
	actionAlignLeft,
	actionAlignCenter,
	actionAlignRight,
	actionAlignJustify;
    TQComboBox comboStyle,
	comboFont,
	comboSize;
private    TQTabWidget tabWidget;
private    HashMap filenames = new HashMap();



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

TextEdit( TQWidget parent, String name )
{
    super( parent, name );
    setupFileActions();
    setupEditActions();
    setupTextActions();

    tabWidget = new TQTabWidget( this );
    connect( tabWidget, SIGNAL(" currentChanged( TQWidget  )"),
	     this, SLOT(" editorChanged( TQWidget  )") );
    setCentralWidget( tabWidget );

    if ( qApp().args().length == 0 ) {
	load( "example.html" );
    } else {
	for ( int i = 0; i < qApp().args().length; ++i )
	    load( qApp().args()[ i ] );
    }
}

void setupFileActions()
{
    TQToolBar tb = new TQToolBar( this );
    tb.setLabel( "File Actions" );
    TQPopupMenu menu = new TQPopupMenu( this );
    menuBar().insertItem( tr( "&File" ), menu );

    TQAction a;
    a = new TQAction( tr( "New" ), new TQIconSet(new TQPixmap( "filenew.xpm" )), tr( "&New..." ), new TQKeySequence(CTRL + Key_N), this, "fileNew" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileNew()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Open" ), new TQIconSet(new TQPixmap( "fileopen.xpm" )), tr( "&Open..." ), new TQKeySequence(CTRL + Key_O), this, "fileOpen" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileOpen()") );
    a.addTo( tb );
    a.addTo( menu );
    menu.insertSeparator();
    a = new TQAction( tr( "Save" ), new TQIconSet(new TQPixmap( "filesave.xpm" )), tr( "&Save..." ), new TQKeySequence(CTRL + Key_S), this, "fileSave" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileSave()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Save As" ), new TQIconSet(new TQPixmap()), tr( "Save &As..." ), new TQKeySequence(0), this, "fileSaveAs" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileSaveAs()") );
    a.addTo( menu );
    menu.insertSeparator();
    a = new TQAction( tr( "Print" ), new TQIconSet(new TQPixmap( "fileprint.xpm" )), tr( "&Print..." ), new TQKeySequence(CTRL + Key_P), this, "filePrint" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" filePrint()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Close" ), new TQIconSet(new TQPixmap()), tr( "&Close" ), new TQKeySequence(0), this, "fileClose" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileClose()") );
    a.addTo( menu );
    a = new TQAction( tr( "Exit" ), new TQIconSet(new TQPixmap()), tr( "E&xit" ), new TQKeySequence(0), this, "fileExit" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" fileExit()") );
    a.addTo( menu );
}

void setupEditActions()
{
    TQToolBar tb = new TQToolBar( this );
    tb.setLabel( "Edit Actions" );
    TQPopupMenu menu = new TQPopupMenu( this );
    menuBar().insertItem( tr( "&Edit" ), menu );

    TQAction a;
    a = new TQAction( tr( "Undo" ), new TQIconSet(new TQPixmap( "editundo.xpm" )), tr( "&Undo" ), new TQKeySequence(CTRL + Key_Z), this, "editUndo" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" editUndo()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Redo" ), new TQIconSet(new TQPixmap( "editredo.xpm" )), tr( "&Redo" ), new TQKeySequence(CTRL + Key_Y), this, "editRedo" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" editRedo()") );
    a.addTo( tb );
    a.addTo( menu );
    menu.insertSeparator();
    a = new TQAction( tr( "Copy" ), new TQIconSet(new TQPixmap( "editcopy.xpm" )), tr( "&Copy" ), new TQKeySequence(CTRL + Key_C), this, "editCopy" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" editCopy()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Cut" ), new TQIconSet(new TQPixmap( "editcut.xpm" )), tr( "Cu&t" ), new TQKeySequence(CTRL + Key_X), this, "editCut" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" editCut()") );
    a.addTo( tb );
    a.addTo( menu );
    a = new TQAction( tr( "Paste" ), new TQIconSet(new TQPixmap( "editpaste.xpm" )), tr( "&Paste" ), new TQKeySequence(CTRL + Key_V), this, "editPaste" );
    connect( a, SIGNAL(" activated()"), this, SLOT(" editPaste()") );
    a.addTo( tb );
    a.addTo( menu );
}

void setupTextActions()
{
    TQToolBar tb = new TQToolBar( this );
    tb.setLabel( "Format Actions" );
    TQPopupMenu menu = new TQPopupMenu( this );
    menuBar().insertItem( tr( "F&ormat" ), menu );

    comboStyle = new TQComboBox( false, tb );
    comboStyle.insertItem( "Standard" );
    comboStyle.insertItem( "Bullet List (Disc)" );
    comboStyle.insertItem( "Bullet List (Circle)" );
    comboStyle.insertItem( "Bullet List (Square)" );
    comboStyle.insertItem( "Ordered List (Decimal)" );
    comboStyle.insertItem( "Ordered List (Alpha lower)" );
    comboStyle.insertItem( "Ordered List (Alpha upper)" );
    connect( comboStyle, SIGNAL(" activated( int )"),
	     this, SLOT(" textStyle( int )") );

    comboFont = new TQComboBox( true, tb );
    TQFontDatabase db = new TQFontDatabase();
    comboFont.insertStringList( (String[]) db.families().toArray(new String[0]) );
    connect( comboFont, SIGNAL(" activated( String  )"),
	     this, SLOT(" textFamily( String  )") );
    comboFont.lineEdit().setText( TQApplication.font().family() );

    comboSize = new TQComboBox( true, tb );
    int[] sizes = db.standardSizes();
    for (int i = 0; i < sizes.length; i++ )
	comboSize.insertItem( Integer.toString(sizes[i]) );
    connect( comboSize, SIGNAL(" activated(  String  )"),
	     this, SLOT(" textSize(  String  )") );
    comboSize.lineEdit().setText( Integer.toString( TQApplication.font().pointSize() ) );

    actionTextBold = new TQAction( tr( "Bold" ), new TQIconSet(new TQPixmap( "textbold.xpm" )), tr( "&Bold" ), new TQKeySequence(CTRL + Key_B), this, "textBold" );
    connect( actionTextBold, SIGNAL(" activated()"), this, SLOT(" textBold()") );
    actionTextBold.addTo( tb );
    actionTextBold.addTo( menu );
    actionTextBold.setToggleAction( true );
    actionTextItalic = new TQAction( tr( "Italic" ), new TQIconSet(new TQPixmap( "textitalic.xpm" )), tr( "&Italic" ), new TQKeySequence(CTRL + Key_I), this, "textItalic" );
    connect( actionTextItalic, SIGNAL(" activated()"), this, SLOT(" textItalic()") );
    actionTextItalic.addTo( tb );
    actionTextItalic.addTo( menu );
    actionTextItalic.setToggleAction( true );
    actionTextUnderline = new TQAction( tr( "Underline" ), new TQIconSet(new TQPixmap( "textunder.xpm" )), tr( "&Underline" ), new TQKeySequence(CTRL + Key_U), this, "textUnderline" );
    connect( actionTextUnderline, SIGNAL(" activated()"), this, SLOT(" textUnderline()") );
    actionTextUnderline.addTo( tb );
    actionTextUnderline.addTo( menu );
    actionTextUnderline.setToggleAction( true );
    menu.insertSeparator();

    TQActionGroup grp = new TQActionGroup( this );
    grp.setExclusive( true );
    connect( grp, SIGNAL(" selected( TQAction )"), this, SLOT(" textAlign( TQAction )") );

    actionAlignLeft = new TQAction( tr( "Left" ), new TQIconSet(new TQPixmap( "textleft.xpm" )), tr( "&Left" ), new TQKeySequence(CTRL + Key_L), grp, "textLeft" );
    actionAlignLeft.addTo( tb );
    actionAlignLeft.addTo( menu );
    actionAlignLeft.setToggleAction( true );
    actionAlignCenter = new TQAction( tr( "Center" ), new TQIconSet(new TQPixmap( "textcenter.xpm" )), tr( "C&enter" ), new TQKeySequence(CTRL + Key_E), grp, "textCenter" );
    actionAlignCenter.addTo( tb );
    actionAlignCenter.addTo( menu );
    actionAlignCenter.setToggleAction( true );
    actionAlignRight = new TQAction( tr( "Right" ), new TQIconSet(new TQPixmap( "textright.xpm" )), tr( "&Right" ), new TQKeySequence(CTRL + Key_R), grp, "textRight" );
    actionAlignRight.addTo( tb );
    actionAlignRight.addTo( menu );
    actionAlignRight.setToggleAction( true );
    actionAlignJustify = new TQAction( tr( "Justify" ), new TQIconSet(new TQPixmap( "textjustify.xpm" )), tr( "&Justify" ), new TQKeySequence(CTRL + Key_J), grp, "textjustify" );
    actionAlignJustify.addTo( tb );
    actionAlignJustify.addTo( menu );
    actionAlignJustify.setToggleAction( true );

    menu.insertSeparator();

    TQPixmap pix = new TQPixmap( 16, 16 );
    pix.fill( black() );
    actionTextColor = new TQAction( tr( "Color" ), new TQIconSet(pix), tr( "&Color..." ),  new TQKeySequence(0), this, "textColor" );
    connect( actionTextColor, SIGNAL(" activated()"), this, SLOT(" textColor()") );
    actionTextColor.addTo( tb );
    actionTextColor.addTo( menu );
}

void load(  String f )
{
    if ( !TQFile.exists( f ) )
	return;
    TQTextEdit edit = new TQTextEdit( tabWidget );
    doConnections( edit );
    tabWidget.addTab( edit, new TQFileInfo( f ).fileName() );
    TQFile file = new TQFile( f );
    if ( !file.open( TQIODevice.IO_ReadOnly ) )
	return;
    TQTextStream ts = new TQTextStream( file );
    edit.setText( ts.read() );
    tabWidget.showPage( edit );
    edit.viewport().setFocus();
    filenames.put( edit, f );
}

TQTextEdit currentEditor()
{
    if ( tabWidget.currentPage() != null &&
	 tabWidget.currentPage().inherits( "TQTextEdit" ) )
	return (TQTextEdit)tabWidget.currentPage();
    return null;
}

void doConnections( TQTextEdit e )
{
    connect( e, SIGNAL(" currentFontChanged( TQFont  )"),
	     this, SLOT(" fontChanged(  TQFont  )") );
    connect( e, SIGNAL(" currentColorChanged(  TQColor  )"),
	     this, SLOT(" colorChanged(  TQColor  )") );
    connect( e, SIGNAL(" currentAlignmentChanged( int )"),
	     this, SLOT(" alignmentChanged( int )") );
}

void fileNew()
{
    TQTextEdit edit = new TQTextEdit( tabWidget );
    edit.setTextFormat( RichText );
    doConnections( edit );
    tabWidget.addTab( edit, tr( "noname" ) );
    tabWidget.showPage( edit );
    edit.viewport().setFocus();
}

void fileOpen()
{
    String fn = TQFileDialog.getOpenFileName( "", tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
    if ( !fn.equals("") )
	load( fn );
}

void fileSave()
{
    if ( currentEditor() == null )
	return;
    String fn;
    if ( !filenames.containsKey( currentEditor() ) ) {
	fileSaveAs();
    } else {
	TQFile file = new TQFile( (String) filenames.get( currentEditor() ) );
	if ( !file.open( TQIODevice.IO_WriteOnly ) )
	    return;
	TQTextStream ts = new TQTextStream( file );
	ts.writeRawBytes(currentEditor().text(), currentEditor().text().length());
    }
}

void fileSaveAs()
{
    if ( currentEditor() == null )
	return;
    String fn = TQFileDialog.getSaveFileName( "", tr( "HTML-Files (*.htm *.html);;All Files (*)" ), this );
    if ( !fn.equals("") ) {
	filenames.put( currentEditor(), fn );
	fileSave();
	tabWidget.setTabLabel( currentEditor(), new TQFileInfo( fn ).fileName() );
    }
}

void filePrint()
{
    if ( currentEditor() == null )
	return;
    TQPrinter printer = new TQPrinter();
    printer.setFullPage(true);
    if ( printer.setup( this ) ) {
	TQPainter p = new TQPainter( printer );
	// Check that there is a valid device to print to.
	if ( p.device() == null ) return;
	TQPaintDeviceMetrics metrics = new TQPaintDeviceMetrics( p.device() );
	int dpix = metrics.logicalDpiX();
	int dpiy = metrics.logicalDpiY();
	 int margin = 72; // pt
	TQRect body = new TQRect( margin * dpix / 72, margin * dpiy / 72,
		    metrics.width() - margin * dpix / 72 * 2,
		    metrics.height() - margin * dpiy / 72 * 2 );
	TQFont font = new TQFont( "times", 10 );
	TQSimpleRichText richText = new TQSimpleRichText( currentEditor().text(), font, currentEditor().context(), currentEditor().styleSheet(),
				  currentEditor().mimeSourceFactory(), body.height() );
	richText.setWidth( p, body.width() );
	TQRect view = new TQRect( body.topLeft(), body.bottomRight() );
	int page = 1;
	do {
	    richText.draw( p, body.left(), body.top(), view, colorGroup() );
	    view.moveBy( 0, body.height() );
	    p.translate( 0 , -body.height() );
	    p.setFont( font );
	    p.drawText( view.right() - p.fontMetrics().width( Integer.toString(page) ),
			view.bottom() + p.fontMetrics().ascent() + 5, Integer.toString(page) );
	    if ( view.top()  >= richText.height() )
		break;
	    printer.newPage();
	    page++;
	} while (true);
    }
}

void fileClose()
{
//    delete currentEditor();
    if ( currentEditor() != null )
	currentEditor().viewport().setFocus();				
}

void fileExit()
{
    qApp().quit();
}

void editUndo()
{
    if ( currentEditor() == null )
	return;
    currentEditor().undo();
}

void editRedo()
{
    if ( currentEditor() == null )
	return;
    currentEditor().redo();
}

void editCut()
{
    if ( currentEditor() == null )
	return;
    currentEditor().cut();
}

void editCopy()
{
    if ( currentEditor() == null )
	return;
    currentEditor().copy();
}

void editPaste()
{
    if ( currentEditor() == null )
	return;
    currentEditor().paste();
}

void textBold()
{
    if ( currentEditor() == null )
	return;
    currentEditor().setBold( actionTextBold.isOn() );
}

void textUnderline()
{
    if ( currentEditor() == null )
	return;
    currentEditor().setUnderline( actionTextUnderline.isOn() );
}

void textItalic()
{
    if ( currentEditor() == null )
	return;
    currentEditor().setItalic( actionTextItalic.isOn() );
}

void textFamily(  String f )
{
    if ( currentEditor() == null )
	return;
    currentEditor().setFamily( f );
    currentEditor().viewport().setFocus();
}

void textSize(  String p )
{
    if ( currentEditor() == null )
	return;
    currentEditor().setPointSize( Integer.parseInt(p) );
    currentEditor().viewport().setFocus();
}

void textStyle( int i )
{
    if ( currentEditor() == null )
	return;
    if ( i == 0 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayBlock, TQStyleSheetItem.ListDisc );
    else if ( i == 1 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListDisc );
    else if ( i == 2 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListCircle );
    else if ( i == 3 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListSquare );
    else if ( i == 4 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListDecimal );
    else if ( i == 5 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListLowerAlpha );
    else if ( i == 6 )
	currentEditor().setParagType( TQStyleSheetItem.DisplayListItem, TQStyleSheetItem.ListUpperAlpha );
    currentEditor().viewport().setFocus();
}

void textColor()
{
    if ( currentEditor() == null )
	return;
    TQColor col = TQColorDialog.getColor( currentEditor().color(), this );
    if ( !col.isValid() )
	return;
    currentEditor().setColor( col );
    TQPixmap pix = new TQPixmap( 16, 16 );
    pix.fill( black() );
    actionTextColor.setIconSet( new TQIconSet(pix) );
}

void textAlign( TQAction a )
{
    if ( currentEditor() == null )
	return;
    if ( a == actionAlignLeft )
	currentEditor().setAlignment( AlignLeft );
    else if ( a == actionAlignCenter )
	currentEditor().setAlignment( AlignHCenter );
    else if ( a == actionAlignRight )
	currentEditor().setAlignment( AlignRight );
    else if ( a == actionAlignJustify )
	currentEditor().setAlignment( AlignJustify );
}

void fontChanged(  TQFont f )
{
    comboFont.lineEdit().setText( f.family() );
    comboSize.lineEdit().setText( Integer.toString( f.pointSize() ) );
    actionTextBold.setOn( f.bold() );
    actionTextItalic.setOn( f.italic() );
    actionTextUnderline.setOn( f.underline() );
}

void colorChanged(  TQColor c )
{
    TQPixmap pix = new TQPixmap( 16, 16 );
    pix.fill( c );
    actionTextColor.setIconSet( new TQIconSet(pix) );
}

void alignmentChanged( int a )
{
    if ( ( a == AlignAuto ) || ( a & AlignLeft ) != 0)
	actionAlignLeft.setOn( true );
    else if ( ( a & AlignHCenter ) != 0 )
	actionAlignCenter.setOn( true );
    else if ( ( a & AlignRight ) != 0 )
	actionAlignRight.setOn( true );
    else if ( ( a & AlignJustify ) != 0 )
	actionAlignJustify.setOn( true );
}

void editorChanged( TQWidget w )
{
    if ( currentEditor() == null )
	return;
    fontChanged( currentEditor().font() );
    colorChanged( currentEditor().color() );
    alignmentChanged( currentEditor().alignment() );
}
}