You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
12KB

  1. /**
  2. \page basics FLTK Basics
  3. This chapter teaches you the basics of compiling programs
  4. that use FLTK.
  5. \section basics_writing Writing Your First FLTK Program
  6. All programs must include the file <tt><FL/Fl.H></tt>.
  7. In addition the program must include a header file for each
  8. FLTK class it uses. Listing 1 shows a simple "Hello,
  9. World!" program that uses FLTK to display the window.
  10. \par Listing 1 - "hello.cxx"
  11. \code
  12. #include <FL/Fl.H>
  13. #include <FL/Fl_Window.H>
  14. #include <FL/Fl_Box.H>
  15. int main(int argc, char **argv) {
  16. Fl_Window *window = new Fl_Window(340,180);
  17. Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
  18. box->box(FL_UP_BOX);
  19. box->labelfont(FL_BOLD+FL_ITALIC);
  20. box->labelsize(36);
  21. box->labeltype(FL_SHADOW_LABEL);
  22. window->end();
  23. window->show(argc, argv);
  24. return Fl::run();
  25. }
  26. \endcode
  27. <!-- NEED 2in -->
  28. After including the required header files, the program then creates a
  29. window. All following widgets will automatically be children of this window.
  30. \code
  31. Fl_Window *window = new Fl_Window(340,180);
  32. \endcode
  33. Then we create a box with the "Hello, World!" string in it. FLTK automatically
  34. adds the new box to \p window, the current grouping widget.
  35. \code
  36. Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
  37. \endcode
  38. Next, we set the type of box and the font, size, and style of the label:
  39. \code
  40. box->box(FL_UP_BOX);
  41. box->labelfont(FL_BOLD+FL_ITALIC);
  42. box->labelsize(36);
  43. box->labeltype(FL_SHADOW_LABEL);
  44. \endcode
  45. We tell FLTK that we will not add any more widgets to \p window.
  46. \code
  47. window->end();
  48. \endcode
  49. Finally, we show the window and enter the FLTK event loop:
  50. \code
  51. window->show(argc, argv);
  52. return Fl::run();
  53. \endcode
  54. The resulting program will display the window in Figure 2-1.
  55. You can quit the program by closing the window or pressing the
  56. <tt>ESC</tt>ape key.
  57. \image html hello.C.png "Figure 2-1: The Hello, World! Window"
  58. \image latex hello.C.png "The Hello, World! Window" width=8cm
  59. \subsection basics_creating Creating the Widgets
  60. The widgets are created using the C++ \p new operator. For
  61. most widgets the arguments to the constructor are:
  62. \code
  63. Fl_Widget(x, y, width, height, label)
  64. \endcode
  65. The \p x and \p y parameters determine where the
  66. widget or window is placed on the screen. In FLTK the top left
  67. corner of the window or screen is the origin
  68. (i.e. <tt>x = 0, y = 0</tt>)
  69. and the units are in pixels.
  70. The \p width and \p height parameters determine
  71. the size of the widget or window in pixels. The maximum widget
  72. size is typically governed by the underlying window system or
  73. hardware.
  74. \p label is a pointer to a character string to label
  75. the widget with or \p NULL. If not specified the label
  76. defaults to \p NULL. The label string must be in static
  77. storage such as a string constant because FLTK does not make a
  78. copy of it - it just uses the pointer.
  79. \subsection basics_hierarchies Creating Widget hierarchies
  80. Widgets are commonly ordered into functional groups, which
  81. in turn may be grouped again, creating a hierarchy of widgets.
  82. FLTK makes it easy to fill groups by automatically adding all widgets
  83. that are created between a
  84. <tt>myGroup->begin()</tt>
  85. and
  86. <tt>myGroup->end()</tt>.
  87. In this example, \p myGroup would be the \e current group.
  88. Newly created groups and their derived widgets implicitly call
  89. \p begin() in the constructor, effectively adding all
  90. subsequently created widgets to itself until \p end()
  91. is called.
  92. Setting the current group to \p NULL will stop automatic
  93. hierarchies. New widgets can now be added manually using
  94. <tt>Fl_Group::add(...)</tt>
  95. and
  96. <tt>Fl_Group::insert(...)</tt>.
  97. \subsection basics_getset Get/Set Methods
  98. <tt>box->box(FL_UP_BOX)</tt>
  99. sets the type of box the Fl_Box draws, changing it from the default of
  100. \p FL_NO_BOX, which means that no box is drawn. In our
  101. "Hello, World!" example we use \p FL_UP_BOX,
  102. which means that a raised button border will be drawn around
  103. the widget. More details are available in the
  104. \ref common_boxtypes
  105. section.
  106. You could examine the boxtype in by doing
  107. <tt>box->box()</tt>. FLTK uses method name overloading to make
  108. short names for get/set methods. A "set" method is always of
  109. the form "void name(type)", and a "get" method is always
  110. of the form "type name() const".
  111. \subsection basics_redrawing Redrawing After Changing Attributes
  112. Almost all of the set/get pairs are very fast, short inline
  113. functions and thus very efficient. However, <i>the "set" methods
  114. do not call \p redraw()</i> - you have to call it
  115. yourself. This greatly reduces code size and execution time. The
  116. only common exceptions are \p value() which calls
  117. \p redraw() and \p label() which calls
  118. \p redraw_label() if necessary.
  119. \subsection basics_labels Labels
  120. All widgets support labels. In the case of window widgets,
  121. the label is used for the label in the title bar. Our example
  122. program calls the \p labelfont(), \p labelsize(),
  123. and \p labeltype() methods.
  124. The \p labelfont() method sets the typeface and style
  125. that is used for the label, which for this example we are using
  126. \p FL_BOLD and \p FL_ITALIC. You can also specify
  127. typefaces directly.
  128. The \p labelsize() method sets the height of the font in pixels.
  129. The \p labeltype()
  130. method sets the type of label. FLTK supports normal, embossed,
  131. and shadowed labels internally, and more types can be added as
  132. desired.
  133. A complete list of all label options can be found in the section on
  134. \ref common_labels.
  135. \subsection basics_showing Showing the Window
  136. The \p show() method shows the widget or window. For windows
  137. you can also provide the command-line arguments to allow users to
  138. customize the appearance, size, and position of your windows.
  139. \subsection basics_eventloop The Main Event Loop
  140. All FLTK applications (and most GUI applications in general)
  141. are based on a simple event processing model. User actions such
  142. as mouse movement, button clicks, and keyboard activity generate
  143. events that are sent to an application. The application may then
  144. ignore the events or respond to the user, typically by redrawing
  145. a button in the "down" position, adding the text to an input
  146. field, and so forth.
  147. FLTK also supports idle, timer, and file pseudo-events that
  148. cause a function to be called when they occur. Idle functions
  149. are called when no user input is present and no timers or files
  150. need to be handled - in short, when the application is not doing
  151. anything. Idle callbacks are often used to update a 3D display
  152. or do other background processing.
  153. Timer functions are called after a specific amount of time
  154. has expired. They can be used to pop up a progress dialog after
  155. a certain amount of time or do other things that need to happen
  156. at more-or-less regular intervals. FLTK timers are not 100%
  157. accurate, so they should not be used to measure time intervals,
  158. for example.
  159. File functions are called when data is ready to read or
  160. write, or when an error condition occurs on a file. They are
  161. most often used to monitor network connections (sockets) for
  162. data-driven displays.
  163. FLTK applications must periodically check (Fl::check())
  164. or wait (Fl::wait()) for events or use the Fl::run()
  165. method to enter a standard event processing loop. Calling
  166. Fl::run() is equivalent to the following code:
  167. \code
  168. while (Fl::wait());
  169. \endcode
  170. Fl::run() does not return until all of the windows
  171. under FLTK control are closed by the user or your program.
  172. \section basics_standard_compiler Compiling Programs with Standard Compilers
  173. Under UNIX (and under Microsoft Windows when using the GNU development
  174. tools) you will probably need to tell the compiler where to find the
  175. header files. This is usually done using the \p -I option:
  176. \code
  177. CC -I/usr/local/include ...
  178. gcc -I/usr/local/include ...
  179. \endcode
  180. The \p fltk-config script included with FLTK can be
  181. used to get the options that are required by your compiler:
  182. \code
  183. CC `fltk-config --cxxflags` ...
  184. \endcode
  185. Similarly, when linking your application you will need to tell the
  186. compiler to use the FLTK library:
  187. \code
  188. CC ... -L/usr/local/lib -lfltk -lXext -lX11 -lm
  189. gcc ... -L/usr/local/lib -lfltk -lXext -lX11 -lm
  190. \endcode
  191. Aside from the "fltk" library, there is also a "fltk_forms"
  192. library for the XForms compatibility classes, "fltk_gl" for the
  193. OpenGL and GLUT classes, and "fltk_images" for the image file
  194. classes, Fl_Help_Dialog widget, and system icon support.
  195. \note
  196. The libraries are named "fltk.lib", "fltkgl.lib", "fltkforms.lib",
  197. and "fltkimages.lib", respectively under Windows.
  198. As before, the \p fltk-config script included with FLTK can be
  199. used to get the options that are required by your linker:
  200. \code
  201. CC ... `fltk-config --ldflags`
  202. \endcode
  203. <!-- NEED 2in -->
  204. The forms, GL, and images libraries are included with the "--use-foo"
  205. options, as follows:
  206. \code
  207. CC ... `fltk-config --use-forms --ldflags`
  208. CC ... `fltk-config --use-gl --ldflags`
  209. CC ... `fltk-config --use-images --ldflags`
  210. CC ... `fltk-config --use-forms --use-gl --use-images --ldflags`
  211. \endcode
  212. Finally, you can use the \p fltk-config script to
  213. compile a single source file as a FLTK program:
  214. \code
  215. fltk-config --compile filename.cpp
  216. fltk-config --use-forms --compile filename.cpp
  217. fltk-config --use-gl --compile filename.cpp
  218. fltk-config --use-images --compile filename.cpp
  219. fltk-config --use-forms --use-gl --use-images --compile filename.cpp
  220. \endcode
  221. Any of these will create an executable named \p filename.
  222. \section basics_makefile Compiling Programs with Makefiles
  223. The previous section described how to use \p fltk-config to
  224. build a program consisting of a single source file from the command
  225. line, and this is very convenient for small test programs.
  226. But \p fltk-config can also be used to set the compiler and
  227. linker options as variables within a \p Makefile that can be
  228. used to build programs out of multiple source files:
  229. \code
  230. CXX = $(shell fltk-config --cxx)
  231. DEBUG = -g
  232. CXXFLAGS = $(shell fltk-config --use-gl --use-images --cxxflags ) -I.
  233. LDFLAGS = $(shell fltk-config --use-gl --use-images --ldflags )
  234. LDSTATIC = $(shell fltk-config --use-gl --use-images --ldstaticflags )
  235. LINK = $(CXX)
  236. TARGET = cube
  237. OBJS = CubeMain.o CubeView.o CubeViewUI.o
  238. SRCS = CubeMain.cxx CubeView.cxx CubeViewUI.cxx
  239. .SUFFIXES: .o .cxx
  240. %.o: %.cxx
  241. $(CXX) $(CXXFLAGS) $(DEBUG) -c $<
  242. all: $(TARGET)
  243. $(LINK) -o $(TARGET) $(OBJS) $(LDSTATIC)
  244. $(TARGET): $(OBJS)
  245. CubeMain.o: CubeMain.cxx CubeViewUI.h
  246. CubeView.o: CubeView.cxx CubeView.h CubeViewUI.h
  247. CubeViewUI.o: CubeViewUI.cxx CubeView.h
  248. clean: $(TARGET) $(OBJS)
  249. rm -f *.o 2> /dev/null
  250. rm -f $(TARGET) 2> /dev/null
  251. \endcode
  252. \section basics_visual_cpp Compiling Programs with Microsoft Visual C++
  253. In Visual C++ you will need to tell the compiler where to
  254. find the FLTK header files. This can be done by selecting
  255. "Settings" from the "Project" menu and then changing the
  256. "Preprocessor" settings under the "C/C++" tab. You will also
  257. need to add the FLTK (<tt>FLTK.LIB</tt> or <tt>FLTKD.LIB</tt>) and the Windows
  258. Common Controls (<tt>COMCTL32.LIB</tt>) libraries to the "Link" settings.
  259. You must also define <tt>WIN32</tt>.
  260. More information can be found in <tt>README.MSWindows.txt</tt>.
  261. You can build your Microsoft Windows applications as Console or
  262. Desktop applications. If you want to use the standard C \p main()
  263. function as the entry point, FLTK includes a \p WinMain()
  264. function that will call your \p main() function for you.
  265. \section basics_naming Naming
  266. All public symbols in FLTK start with the characters 'F' and 'L':
  267. \li Functions are either \p Fl::foo() or \p fl_foo().
  268. \li Class and type names are capitalized: \p Fl_Foo.
  269. \li \ref enumerations "Constants and enumerations"
  270. are uppercase: \p FL_FOO.
  271. \li All header files start with <tt><FL/...></tt>.
  272. <!-- NEED 5in -->
  273. \section basics_headerfiles Header Files
  274. The proper way to include FLTK header files is:
  275. \code
  276. #include <FL/Fl_xyz.H>
  277. \endcode
  278. \note
  279. Case \e is significant on many operating systems,
  280. and the C standard uses the forward slash (/) to
  281. separate directories. <i>Do not use any of the following
  282. include lines:</i>
  283. \code
  284. #include <FL\Fl_xyz.H>
  285. #include <fl/fl_xyz.h>
  286. #include <Fl/fl_xyz.h>
  287. \endcode
  288. \htmlonly
  289. <hr>
  290. <table summary="navigation bar" width="100%" border="0">
  291. <tr>
  292. <td width="45%" align="LEFT">
  293. <a class="el" href="intro.html">
  294. [Prev]
  295. Introduction to FLTK
  296. </a>
  297. </td>
  298. <td width="10%" align="CENTER">
  299. <a class="el" href="index.html">[Index]</a>
  300. </td>
  301. <td width="45%" align="RIGHT">
  302. <a class="el" href="common.html">
  303. Common Widgets and Attributes
  304. [Next]
  305. </a>
  306. </td>
  307. </tr>
  308. </table>
  309. \endhtmlonly
  310. */