Академический Документы
Профессиональный Документы
Культура Документы
1 Fltk
FLTK (pronounced "fulltick") is a LGPL'd C++ graphical user interface
toolkit for X (UNIX), OpenGL, and WIN32 (Microsoft Windows NT 4.0,
95, or 98). It is currently maintained by a small group of developers across
the world. As most GUI toolkits, it is based on event-driven programming
paradigm.
#include <
#include <
#include <
int
int
>
FL/ F l . H
>
FL/ Fl Window . H
>
FL/ F l B o x . H
main (
Fl Window
Fl Box
bo x
bo x
bo x
>
>
>
>
>
argc ,
char
new
new
window =
box =
argv )
Fl Window ( 3 0 0 , 1 8 0 ) ;
F l B o x ( FL UP BOX , 2 0 , 4 0 , 2 6 0 , 1 0 0 , " H e l l o ,
World ! " ) ;
l a b e l f o n t ( FL BOLD+FL ITALIC ) ;
labelsize (36);
window
end ( ) ;
return
show ( a r g c ,
window
argv ) ;
Fl : : run ( ) ;
file:///D|/ta/557/tutorial/Fl_Box.html
class Fl_Box
Class Hierarchy
Fl_Widget
|
+----Fl_Box
Include Files
#include <FL/Fl_Box.H>
Description
This widget simply draws its box, and possibly it's label. Putting it before some other widgets and making
it big enough to surround them will let you draw a frame around them.
Methods
Fl_Box
~Fl_Box
Fl_Box::Fl_Box(int x, int y, int w, int h, const char * = 0)
Fl_Box::Fl_Box(Fl_Boxtype b, int x, int y, int w, int h, const char *)
The first constructor sets box() to FL_NO_BOX, which means it is invisible. However such widgets are
useful as placeholders or Fl_Group::resizable() values. To change the box to something visible, use
box(n).
The second form of the constructor sets the box to the specified box type.
Fl_Box::~Fl_Box(void)
The destructor removes the box.
Contents Previous Next
1 of 1
1/17/00 11:51 AM
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
#include <
#include <
#include <
#include <
#include <
#include <
#include <
#include <
#include <
>
>
stdio .h
FL/ F l . H
>
FL/ Fl Window . H
>
FL/ F l M e n u B a r . H
FL/ F l
Value
>
S l i d e r .H
>
FL/ F l B u t t o n . H
>
FL/ F l B o x . H
Fl Window
Fl Window
FL/ f l
file
FL/ f l
ask .h
Slider
Fl Button
void
file
fl
cb
dlg
fl
void
void
cb
s e l e c t ( Fl Widget
n e w f i l e= f l
cb
open
file
cb
o ,
"
. cp p " ,
NULL ) ;
newfile );
void f
o ,
v)
void f
v)
)v);
void f
o ,
v)
f
is
about . " ) ;
s l i d e s ( Fl Widget
menubar
cb
file " ,
hide ( ) ;
about ( )
>
>
int
o ,
>
r e s i z e ( window
10
resize (0,
r e s e t ( Fl Widget
slider
>
m e s s a g e ( " This
cb
c h o o s e %s " ,
hide ( ) ;
window
cb
char
q u i t ( Fl Widget
>
void
v)
" You
p a s s ( Fl Widget
fl
void f
o ,
show ( ) ;
window
void
c h o o s e r ( " Choose
d i a l o g ( Fl Widget
>
void
boxes
m e s s a g e ( msg ) ;
message ((
dlg
chooser
button ;
s p r i n t f ( msg ,
void
message
menubar ;
msg [ 2 5 6 ] ;
file
/ / FLTK
slider ;
( n e w f i l e !=NULL )
/ / FLTK
dlg ;
char
char
if
cb
>
window ;
Fl Menu Bar
Fl
>
c h o o s e r .H
>
value ( 2 0 ) ;
slides ( slider ,
void f
v)
x (),
)((( Fl
0,
10
o ,
window
int
Slider
)((( Fl
void f
void
>
) o)
v)
)20);
y (),
>
value ( ) ) ,
Slider
) o)
>
window
>
h());
value ( ) ) ,
25);
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
char
Fl Menu Item
f
g
menuitems [ ]
"& F i l e " ,
f
f
f
f
f
f
0,
0,
0,
FL SUBMENU
FL ALT
's ',
( Fl
Callback
FL ALT
'd ' ,
( Fl
Callback
"& F l t k " ,
FL ALT
' f ',
( Fl
Callback
"&OpenGl " ,
FL ALT
'o ',
( Fl
Callback
"& Q u i t " ,
FL ALT
'q ' ,
( Fl
Callback
0,
) cb
file
) cb
open
) cb
pass ,
) cb
pass ,
f
f
g
void
void
g
0,
"&About " ,
0
quit
0,
FL SUBMENU
0,
FL ALT +
'a ' ,
( Fl
Callback
) cb
about
int
main (
//
new
menubar =
window
200,
200,
Fl Menu Bar ( 0 ,
0,
200,
25);
menu ( m e n u i t e m s ) ;
quit ) ;
end ( ) ;
Fl Window ( 3 0 0 ,
new
install
slider
slider
slider
slider
slider
slider
slider
slider
slider
slider
>
>
>
>
>
>
>
>
>
slider
Fl
530,
200,
70,
size
Value
Slider ( 0 ,
10,
150,
20,
minimum ( 1 ) ;
maximum( 4 0 ) ;
step ( 1 ) ;
value ( 2 0 ) ;
>
//
set
its
value
a l i g n ( FL ALIGN RIGHT ) ;
callback ( c b
Fl Button
button
dlg
new
300,
bar
callback ( cb
>
new
//
>
>
>
menu
when ( FL HIDE ) ;
window
dlg
argv )
Fl Window ( 3 0 0 ,
install
menubar
window
char
argc ,
window =
button
callback ( cb
slides );
new
Fl Button ( 6 0 ,
40,
80,
20,
reset );
>
end ( ) ;
window
return
>
show ( a r g c ,
argv ) ;
Fl : : run ( ) ;
In line 3-7, we include all the header les for the widgets used in the example. In line 10 and 11, we include the other two include les,
le chooser.h
5
dialog ,
FL MENU DIVIDER
) cb
select
FL MENU DIVIDER
"&H e l p " ,
int
char
) fltk
) opengl ,
and
ask.h. These two les dene some global functions. The rst one is
for le lter and selector and the second one is for the popup message box.
The design of an event-driven program is usually divided into two steps:
to design the user interface and then to add in the callback functions. The
user interface of the sample program looks like:
In the dialog, we have a slider and a button. The setting for them should
be quite obvious. When the position of the slider changes, the callback
function, cb slide, will be called. In this function, we will resize the window
and the menubar such that the width is 10 times of the value in the slider.
The member function, x(), y(), w() and h(), will return the current position
and dimension for that widget. resize() will change the size and position of
the widget.
For the reset button, we reset the value of the slider back to 20 and call
cb slide to change the size of the window consistently. Note that overloaded
function, value(), is used both to set or read the value of the slider.
2 OpenGL
OpenGL is a 2D/3D Graphics Library developed at Silicon Graphics Inc. It
has been widely used in computer graphics industry. It is a must to work in
this eld. The best book for learning OpenGL is the OpenGL Programming
Guide, Second Edition. We have several Copies of this book available in
Sieg 228. You can also look at http://www.opengl.org for more information.
If you are working on NT, MSDN also includes the reference to OpenGL
functions .
OpenGL is a state machine. It will keep various states, such as the
drawing color, point size, line width, buer to write and so on. Those states
will keep the same until you change them explicitly. Many state variables are
switched on or o by glEnable() or glDisable() commands. For example, you
can turn o depth test by issuing glDisable(GL DEPTH TEST). Besides,
you will need to turn on the blend function by calling glEnable(GL BLEND)
to have alpha blending eect.
1
1 the
material in this section is adapted from the previous tutorials for CSE457
signed bytes.
red ,
green ,
GL LINE STRIP
blue ,
alpha ) ;
);
glVertex2i (
Ax ,
Ay
);
glVertex2i (
Bx ,
By
);
glVertex2i (
Cx ,
Cy
);
glVertex2i (
Ax ,
Ay
);
glEnd ( ) ;
glFlush ()
of the vertices. Finally, glFlush() tells OpenGL to draw the primitives now.
image transformation. Without going into too much detail, let's just
say that you'll want to save your current transformation matrix ("push"
it onto a matrix stack) before doing your brush-specic translation/rotation,
and restore the original matrix ("pop" it o the matrix stack) when
you're done with that brush stroke. If you are not sure what already
happened in the matrix stack, you can call glLoadIdentity() to clear
the currently modiable matrix for future transformation.
Here are the calls you need to use.
glPushMatrix ( ) ;
<
<
&&l t D o
the
&&l t D r a w
translation ,
the
brush
rotation
>>
>>
stroke
glPopMatrix ( ) ;
12
3 Fltk+OpenGL
In this section, we will introduce how to use OpenGL in
tk. The most
convenient way is to subclass Fl Gl Window. The only restriction is that
you can only invoke OpenGL drawing functions in draw(). Whenever you
want to update the display, call redraw() and
tk will call draw() later. Since
all the drawing functions must be called within draw(), you need to have some
mechanism to inform draw() what it should do. In the skeleton code, we set
some variables, isAnEvent and eventToDo, for this purpose. The better way
may be to maintain a message queue for it?
A class denition
A draw() method
A handle() method
void
int
public
MyGLWindow
handle (
:
MyGLWindow(
int
int
Fl Gl Window
draw ( ) ;
);
int
X,
F l G l W i n d o w (X ,
Y,
Y , W,
int
int
W,
H,
fg
L)
H,
const char
L)
void
if
g
//
MyGLWindow : : draw ( )
( ! valid ())
//
whenever
//
and
turned
//
yo u
need
//
get
the
put
2 the
your
the
to
window
on
set
up
dimension
drawing
size
after
the
is
changed ,
first
call
projection ,
of
the
operations
to
valid
viewpoint
window
from
will
be
turned
off
draw ( )
. . .
w( ) ,
h()
here
13
case
event )
( event )
FL PUSH :
coord . x =
Fl : : e v e n t
x ();
if
else
Fl : : e v e n t
y ();
coord . y =
( Fl : : e v e n t
button ()
>
1)
i s A n E v e n t =1;
return
case
redraw ( ) ;
1;
FL DRAG :
coord . x =
Fl : : e v e n t
x ();
if
else
Fl : : e v e n t
y ();
coord . y =
( Fl : : e v e n t
button ()
>
1)
i s A n E v e n t =1;
return
case
redraw ( ) ;
1;
FL RELEASE :
coord . x =
Fl : : e v e n t
x ();
if
else
Fl : : e v e n t
y ();
coord . y =
( Fl : : e v e n t
return
default
return
button ()
>
1)
i s A n E v e n t =1;
1;
//
tell
FLTK
that
don ' t
understand
other
events
0;
3.2 An Example
In this section, we will present a extremely simplied impressionist program.
This program has a canvas of the size 300x300. When user click the left
mouse button, it will draw a 20x20 square at the clicked position. Clicking
right mouse button will pick up the drawing color among red, green and
blue sequentially. If the left button is clicked outside the canvas, the square
14
When window is resized, that is, the frame buers are destroyed and
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
Listing 3 - "main.cpp"
#include
"MyGLWindow . h "
MyGLWindow
int
main (
window ;
int
window
return
new
char
argc ,
window =
window
>
>
argv )
MyGLWindow ( 2 0 0 ,
200,
300,
300,
"GL demo" ) ;
r e s i z a b l e ( window ) ;
show ( ) ;
Fl : : run ( ) ;
#ifndef
#define
#include <
#include <
#include <
class
void
int
public
Listing 4 - "MyGLWindow.h"
MY GL WINDOW H
MY GL WINDOW H
>
>
FL/ F l . H
>
FL/ F l G l W i n d o w . H
FL/ g l . h
MyGLWindow
handle (
MyGLWindow (
void
int
int
draw ( ) ;
public
Fl Gl Window
);
X,
int
Y,
int
W,
int
SaveCurrentContent ( ) ;
16
H,
const char
L);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void
int
int
int
int
int
int
unsigned char
RestoreContent ( ) ;
windowWidth ,
drawWidth ,
mx ,
windowHeight ;
drawHeight ;
my ;
isAnEvent ;
eventTo D o ;
curColor ;
buf ,
bitstart ;
#endif
Listing 5 - "MyGLWindow.cpp"
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
#include
#define
#define
#ifndef
#define
#endif
"MyGLWindow . h "
WIN32
min ( a ,
b)
( ( a)
MyGLWindow : : MyGLWindow(
:
<
int
F l G l W i n d o w (X ,
(b ) )
X,
buf
buf ,
curColor
static
void
if
GLubyte
int
W,
int
H,
const char
L)
= H;
[W H
L)
( b)
= W;
drawHeight
0, W H
Y,
H,
memset (
( a )
int
Y , W,
windowWidth = drawWidth
windowHeight
3];
);
0;
drawColor [ 3 ] [ 3 ] =
glClearColor (0.7 f ,
glDisable (
255,0,0
g f
,
0,255,0
g f
,
0,0,255
gg
MyGLWindow : : draw ( )
( ! valid ( ))
ff
0.7 f ,
GL DEPTH TEST
0.7 f ,
1.0);
);
ortho ( ) ;
glClear (
);
//
clear
the
window
windowWidth = w ( ) ;
windowHeight
= h();
int
if
startrow
(
startrow
drawHeight
<
startrow
min (
=
0;
17
drawHeight ,
windowHeight
);
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
bitstart
buf
( ( drawWidth
startrow ) ) ;
RestoreContent ( ) ;
if
! isAnEvent )
else f
RestoreContent ( ) ;
isAnEvent =0;
switch
case
//
( eventTo D o )
clear
it
after
processing
g l C o l o r 3 u b v ( drawColor [ c u r C o l o r ] ) ;
glBegin (
GL QUADS
);
g l V e r t e x 2 d ( mx ,
( windowHeight
g l V e r t e x 2 d ( mx ,
( windowHeight
my ) ) ;
my)
20);
g l V e r t e x 2 d ( mx+ 2 0 ,
( windowHeight
my)
20);
g l V e r t e x 2 d ( mx+ 2 0 ,
( windowHeight
my ) ) ;
glEnd ( ) ;
SaveCurrentContent ( ) ;
case
g
int
switch
case
break
break
c u r C o l o r =( c u r C o l o r +1)%3;
;
MyGLWindow : : h a n d l e (
( event )
int
event )
FL PUSH :
mx =
Fl : : e v e n t
if
else
Fl : : e v e n t
my =
( Fl : : e v e n t
x ();
y ();
button ()
>
1)
i s A n E v e n t =1;
return
default
return
redraw ( ) ;
//
g
g
void
f
return
1;
:
tell
FLTK
that
don ' t
understand
0;
0;
MyGLWindow : : S a v e C u r r e n t C o n t e n t ( )
g l R e a d B u f f e r (GL BACK ) ;
18
other
events
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
glPixelStorei (
GL PACK ALIGNMENT ,
glPixelStorei (
glReadPixels (
0,
windowHeight
min ( drawWidth ,
GL RGB ,
void
f
) ;
drawWidth
);
min ( d r a w H e i g h t ,
windowWidth ) ,
GL UNSIGNED BYTE ,
windowHeight )
min ( d r a w H e i g h t ,
bitstart
windowHeight ) ,
);
MyGLWindow : : R e s t o r e C o n t e n t ( )
g l D r a w B u f f e r (GL BACK ) ;
glRasterPos2i (
glPixelStorei (
0,
windowHeight
min ( d r a w H e i g h t ,
GL UNPACK ALIGNMENT ,
min ( drawWidth ,
GL RGB ,
windowHeight )
);
);
drawWidth
windowWidth ) ,
GL UNSIGNED BYTE ,
);
min ( d r a w H e i g h t ,
bitstart
windowHeight ) ,
);
4 Impressionist
4.1 The Structure of Impressionist
The ancestor of
tk impressionist is MFC impressionist. Inherited from it,
this version also adapts the Document-View architecture. Figure 7 illustrates
the relationship among all these classes.
19
1 2 1
0 0 0
-1 -2 -1
or simply,
-1 1
-1
1
20