summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/menu/MenuExample.java
blob: c7b72ce7ac190999739bcd2f12abc3a78795a66e (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
/***************************************************************************
* $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.*;

class MenuExample  extends TQWidget
{

private    TQMenuBar menu;
private    TQLabel   label;
private    boolean isBold;
private    boolean isUnderline;
private    int boldID, underlineID;



/* XPM */
static String  p1_xpm[] = {
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFF0000",
"                ",
"                ",
"         ....   ",
"        .XXXX.  ",
" .............. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .XXXXXXXXXXXX. ",
" .............. ",
"                "};

/* XPM */
static String  p2_xpm[] = {
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "};

/* XPM */
static String  p3_xpm[] = {
"16 16 3 1",
" 	c None",
".	c #000000000000",
"X	c #FFFFFFFFFFFF",
"                ",
"                ",
"   .........    ",
"  ...........   ",
"  ........ ..   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...........   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"  ...XXXXX...   ",
"   .........    ",
"                ",
"                "};


/*
  Auxiliary class to provide fancy menu items with different
  fonts. Used for the "bold" and "underline" menu items in the options
  menu.
 */
/**
class MyMenuItem  extends TQCustomMenuItem
{
private    String string;
private    TQFont font;

    public    MyMenuItem( String s, TQFont f )
    {
	    font = f;
    	string = s;
	}

    public void paint( TQPainter p, TQColorGroup cg, boolean act, boolean enabled, int x, int y, int w, int h )
    {
	p.setFont ( font );
	p.drawText( x, y, w, h, AlignLeft | AlignVCenter | ShowPrefix | DontClip, string );
    }

    public TQSize sizeHint()
    {
	return new TQFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip,  string );
    }
}
*/

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

MenuExample( TQWidget parent, String name )
{
    super( parent, name );
    TQPixmap p1 = new TQPixmap( p1_xpm );
    TQPixmap p2 = new TQPixmap( p2_xpm );
    TQPixmap p3 = new TQPixmap( p3_xpm );

    TQPopupMenu print = new TQPopupMenu( this );
    print.insertTearOffHandle();
    print.insertItem( "&Print to printer", this, SLOT("printer()") );
    print.insertItem( "Print to &file", this, SLOT("file()") );
    print.insertItem( "Print to fa&x", this, SLOT("fax()") );
    print.insertSeparator();
    print.insertItem( "Printer &Setup", this, SLOT("printerSetup()") );

    TQPopupMenu file = new TQPopupMenu( this );
    file.insertItem( new TQIconSet(p1), "&Open",  this, SLOT("open()"), new TQKeySequence(CTRL+Key_O) );
    file.insertItem( new TQIconSet(p2), "&New", this, SLOT("news()"), new TQKeySequence(CTRL+Key_N) );
    file.insertItem( new TQIconSet(p3), "&Save", this, SLOT("save()"), new TQKeySequence(CTRL+Key_S) );
    file.insertItem( "&Close", this, SLOT("closeDoc()"), new TQKeySequence(CTRL+Key_W) );
    file.insertSeparator();
    file.insertItem( "&Print", print, CTRL+Key_P );
    file.insertSeparator();
    file.insertItem( "E&xit",  qApp(), SLOT("quit()"), new TQKeySequence(CTRL+Key_Q) );

    TQPopupMenu edit = new TQPopupMenu( this );
    int undoID = edit.insertItem( "&Undo", this, SLOT("undo()") );
    int redoID = edit.insertItem( "&Redo", this, SLOT("redo()") );
    edit.setItemEnabled( undoID, false );
    edit.setItemEnabled( redoID, false );

    TQPopupMenu options = new TQPopupMenu( this );
    options.insertTearOffHandle();
    options.setCaption("Options");
    options.insertItem( "&Normal Font", this, SLOT("normal()") );
    options.insertSeparator();

    options.polish(); // adjust system settings
    TQFont f = options.font();
    f.setBold( true );
//    boldID = options.insertItem( new MyMenuItem( "&Bold", f ) );
    options.setAccel( new TQKeySequence(CTRL+Key_B), boldID );
    options.connectItem( boldID, this, SLOT("bold()") );
    f = font();
    f.setUnderline( true );
//    underlineID = options.insertItem( new MyMenuItem( "&Underline", f ) );
    options.setAccel( new TQKeySequence(CTRL+Key_U), underlineID );
    options.connectItem( underlineID, this, SLOT("underline()") );

    isBold = false;
    isUnderline = false;
    options.setCheckable( true );


    TQPopupMenu help = new TQPopupMenu( this );
    help.insertItem( "&About", this, SLOT("about()"), new TQKeySequence(CTRL+Key_H) );
    help.insertItem( "About &Qt", this, SLOT("aboutTQt()") );

    menu = new TQMenuBar( this );
    menu.insertItem( "&File", file );
    menu.insertItem( "&Edit", edit );
    menu.insertItem( "&Options", options );
    menu.insertSeparator();
    menu.insertItem( "&Help", help );
    menu.setSeparator( TQMenuBar.InWindowsStyle );

    label = new TQLabel( this );
    label.setGeometry( 20, rect().center().y()-20, width()-40, 40 );
    label.setFrameStyle( TQFrame.Box | TQFrame.Raised );
    label.setLineWidth( 1 );
    label.setAlignment( AlignCenter );

    connect( this,  SIGNAL("explain(String)"),
	     label, SLOT("setText(String)") );

    setMinimumSize( 100, 80 );
}


void open()
{
    emit("explain", "File/Open selected" );
}


void news()
{
    emit("explain", "File/New selected" );
}

void save()
{
    emit("explain", "File/Save selected" );
}


void closeDoc()
{
    emit("explain", "File/Close selected" );
}


void undo()
{
    emit("explain", "Edit/Undo selected" );
}


void redo()
{
    emit("explain", "Edit/Redo selected" );
}


void normal()
{
    isBold = false;
    isUnderline = false;
    menu.setItemChecked( boldID, isBold );
    menu.setItemChecked( underlineID, isUnderline );
    emit("explain", "Options/Normal selected" );
}


void bold()
{
    isBold = !isBold;
    menu.setItemChecked( boldID, isBold );
    emit("explain", "Options/Bold selected" );
}


void underline()
{
    isUnderline = !isUnderline;
    menu.setItemChecked( underlineID, isUnderline );
    emit("explain", "Options/Underline selected" );
}


void about()
{
    TQMessageBox.about( this, "Qt Menu Example",
			"This example demonstrates simple use of Qt menus.\n"
			+ "You can cut and paste lines from it to your own\n"
			+ "programs." );
}


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


void printer()
{
    emit("explain", "File/Printer/Print selected" );
}

void file()
{
    emit("explain", "File/Printer/Print To File selected" );
}

void fax()
{
    emit("explain", "File/Printer/Print To Fax selected" );
}

void printerSetup()
{
    emit("explain", "File/Printer/Printer Setup selected" );
}


protected void resizeEvent( TQResizeEvent e  )
{
    label.setGeometry( 20, rect().center().y()-20, width()-40, 40 );
}


public static void main(String[] args)
{
    TQApplication a = new TQApplication( args );
    MenuExample m = new MenuExample();
    m.setCaption("Qt Examples - Menus");
    a.setMainWidget( m );
    m.show();
    a.exec();
    return;
}

	static {
		qtjava.initialize();
	}

}