summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/mdi/ApplicationWindow.java
blob: fd0079be3703cde07f14e1e6c9c3230e77afe7c1 (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
/***************************************************************************
* $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.trinitydesktop.qt.*;
import java.util.ArrayList;

class ApplicationWindow extends TQMainWindow
{

private    TQPrinter printer;
private    TQWorkspace ws;
/* Keep a copy of ws.windowList(), so that the MDIWindow
   widgets in it won't get garbage collected */
private    ArrayList windows;
private    TQToolBar fileTools;
private    TQPopupMenu windowsMenu;



private String  fileOpenText = "Click this button to open a <em>new file</em>. <br><br>"
+ "You can also select the <b>Open command</b> from the File menu.";
private String  fileSaveText = "Click this button to save the file you are "
+ "editing.  You will be prompted for a file name.\n\n"
+ "You can also select the Save command from the File menu.\n\n"
+ "Note that implementing this function is left as an exercise for the reader.";
private String  filePrintText = "Click this button to print the file you "
+ "are editing.\n\n"
+ "You can also select the Print command from the File menu.";

/* XPM */
static String filesave[] = {
"    14    14        4            1",
". c #040404",
"# c #808304",
"a c #bfc2bf",
"b c None",
"..............",
".#.aaaaaaaa.a.",
".#.aaaaaaaa...",
".#.aaaaaaaa.#.",
".#.aaaaaaaa.#.",
".#.aaaaaaaa.#.",
".#.aaaaaaaa.#.",
".##........##.",
".############.",
".##.........#.",
".##......aa.#.",
".##......aa.#.",
".##......aa.#.",
"b............."
};

/* XPM */
static String fileopen[] = {
"    16    13        5            1",
". c #040404",
"# c #808304",
"a c None",
"b c #f3f704",
"c c #f3f7f3",
"aaaaaaaaa...aaaa",
"aaaaaaaa.aaa.a.a",
"aaaaaaaaaaaaa..a",
"a...aaaaaaaa...a",
".bcb.......aaaaa",
".cbcbcbcbc.aaaaa",
".bcbcbcbcb.aaaaa",
".cbcb...........",
".bcb.#########.a",
".cb.#########.aa",
".b.#########.aaa",
"..#########.aaaa",
"...........aaaaa"
};

/* XPM */
static String fileprint[] = {
"    16    14        6            1",
". c #000000",
"# c #848284",
"a c #c6c3c6",
"b c #ffff00",
"c c #ffffff",
"d c None",
"ddddd.........dd",
"dddd.cccccccc.dd",
"dddd.c.....c.ddd",
"ddd.cccccccc.ddd",
"ddd.c.....c....d",
"dd.cccccccc.a.a.",
"d..........a.a..",
".aaaaaaaaaa.a.a.",
".............aa.",
".aaaaaa###aa.a.d",
".aaaaaabbbaa...d",
".............a.d",
"d.aaaaaaaaa.a.dd",
"dd...........ddd"
};

ApplicationWindow()
{
    super( null, "example application main window", WDestructiveClose );
    int id;

    TQPixmap openIcon, saveIcon;

    fileTools = new TQToolBar( this, "file operations" );
    addToolBar( fileTools, tr( "File Operations" ), DockTop, true );

    openIcon = new TQPixmap( fileopen );
    TQToolButton  fileOpen
	= new TQToolButton( new TQIconSet(openIcon), "Open File", "",
			   this, SLOT("load()"), fileTools, "open file" );

    saveIcon = new TQPixmap( filesave );
    TQToolButton  fileSave
	= new TQToolButton( new TQIconSet(saveIcon), "Save File", "",
			   this, SLOT("save()"), fileTools, "save file" );

    printer = new TQPrinter();
    TQPixmap printIcon;

    printIcon = new TQPixmap( fileprint );
    TQToolButton  filePrint
	= new TQToolButton( new TQIconSet(printIcon), "Print File", "",
			   this, SLOT("print()"), fileTools, "print file" );
    TQWhatsThis.add( filePrint, filePrintText );

    TQWhatsThis.whatsThisButton( fileTools );

    TQWhatsThis.add( fileOpen, fileOpenText );
    TQWhatsThis.add( fileSave, fileSaveText );

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

    file.insertItem( "&New", this, SLOT("newDoc()"), new TQKeySequence(CTRL+Key_N) );

    id = file.insertItem( new TQIconSet(openIcon), "&Open...",
			   this, SLOT("load()"), new TQKeySequence(CTRL+Key_O) );
    file.setWhatsThis( id, fileOpenText );

    id = file.insertItem( new TQIconSet(saveIcon), "&Save",
			   this, SLOT("save()"), new TQKeySequence(CTRL+Key_S) );
    file.setWhatsThis( id, fileSaveText );
    id = file.insertItem( "Save &As...", this, SLOT("saveAs()") );
    file.setWhatsThis( id, fileSaveText );
    file.insertSeparator();
    id = file.insertItem( new TQIconSet(printIcon), "&Print...",
			   this, SLOT("print()"), new TQKeySequence(CTRL+Key_P) );
    file.setWhatsThis( id, filePrintText );
    file.insertSeparator();
    file.insertItem( "&Close", this, SLOT("closeWindow()"), new TQKeySequence(CTRL+Key_W) );
    file.insertItem( "&Quit", tqApp(), SLOT(" closeAllWindows()"), new TQKeySequence(CTRL+Key_Q) );

    windowsMenu = new TQPopupMenu( this );
    windowsMenu.setCheckable( true );
    connect( windowsMenu, SIGNAL(" aboutToShow()"),
	     this, SLOT(" windowsMenuAboutToShow()") );
    menuBar().insertItem( "&Windows", windowsMenu );

    menuBar().insertSeparator();
    TQPopupMenu  help = new TQPopupMenu( this );
    menuBar().insertItem( "&Help", help );

    help.insertItem( "&About", this, SLOT("about()"), new TQKeySequence(Key_F1));
    help.insertItem( "About &Qt", this, SLOT("aboutTQt()"));
    help.insertSeparator();
    help.insertItem( "What's &This", this, SLOT("whatsThis()"), new TQKeySequence(SHIFT+Key_F1));

    TQVBox vb = new TQVBox( this );
    vb.setFrameStyle( TQFrame.StyledPanel | TQFrame.Sunken );
    ws = new TQWorkspace( vb );
    ws.setScrollBarsEnabled( true );
    setCentralWidget( vb );

    statusBar().message( "Ready", 2000 );
}


MDIWindow newDoc()
{
    MDIWindow w = new MDIWindow( ws, null, WDestructiveClose );
    connect( w, SIGNAL("message(String,int)"), statusBar(), SLOT("message(String,int)") );
    w.setCaption("unnamed document");
    w.setIcon( new TQPixmap("document.xpm") );
    // show the very first window in maximized mode
	windows = ws.windowList();
    if ( windows.size() == 0 )
	w.showMaximized();
    else
	w.show();
    return w;
}

void load()
{
    String fn = TQFileDialog.getOpenFileName( "", "", this );
    if ( !fn.equals("") ) {
	MDIWindow w = newDoc();
	w.load( fn );
    }  else {
	statusBar().message( "Loading aborted", 2000 );
    }
}

void save()
{
    MDIWindow m = (MDIWindow)ws.activeWindow();
    if ( m != null )
	m.save();
}


void saveAs()
{
    MDIWindow m = (MDIWindow)ws.activeWindow();
    if ( m != null )
	m.saveAs();
}


void print()
{
    MDIWindow m = (MDIWindow)ws.activeWindow();
    if ( m != null)
	m.print( printer );
}


void closeWindow()
{
    MDIWindow m = (MDIWindow)ws.activeWindow();
    if ( m != null )
	m.close();
}

void about()
{
    TQMessageBox.about( this, "Qt Application Example",
			"This example demonstrates simple use of\n "
			+ "Qt's Multiple Document Interface (MDI).");
}


void aboutTQt()
{
    TQMessageBox.aboutTQt( this, "Qt Application Example" );
}


void windowsMenuAboutToShow()
{
    windowsMenu.clear();
    int cascadeId = windowsMenu.insertItem("&Cascade", ws, SLOT("cascade()") );
    int tileId = windowsMenu.insertItem("&Tile", ws, SLOT("tile()") );
    windows = ws.windowList();
    if ( windows.size() == 0 ) {
	windowsMenu.setItemEnabled( cascadeId, false );
	windowsMenu.setItemEnabled( tileId, false );
    }
    windowsMenu.insertSeparator();
    for ( int i = 0; i < windows.size(); ++i ) {
	int id = windowsMenu.insertItem(((TQWidget) windows.get(i)).caption(),
					 this, SLOT(" windowsMenuActivated( int )") );
	windowsMenu.setItemParameter( id, i );
	windowsMenu.setItemChecked( id, ws.activeWindow() == windows.get(i) );
    }
}

void windowsMenuActivated( int id )
{
    windows = ws.windowList();
    TQWidget w = (TQWidget) windows.get( id );
    if ( w != null )
	w.showNormal();
    w.setFocus();
}


}