This tutorial should give you a introduction in creating a simple window in FOX.
No matter if you're working on Linux or Windows, every FOX application needs to have a main function (ie. no WinMain function). So since this is a must, let's start looking at the main function, in "main.cpp", for our application.
#include <fx.h> #include "mywindow.h"
int main(int argc,char ** argv) {
FXApp application("Tutorial #1","FOX Tutorials");
application.init(argc,argv);
new MyWindow(&application);
application.create();
return application.run(); }
The first two lines simply includes the necessary include files. For
FOX, we only need to include fx.h. mywindow.h contains our own class
definitions.
Each FOX application needs one application object. In the
constructor we may specify the application and vendor name. These are
used in conjuction with the FOX Registry. After constructing the
application object (constructing it on the stack will make sure it will
be automatically deleted after we close our program), we need to
initialize the application. Passing argc and argv allows FOX to scan
for certain commandline arguments (for example -display or
-tracelevel).
Now we are ready to create our main window. First we construct our own
mainwindow (MyWindow) which is derived from FXMainWindow later on in
this tutorial.Now comes the essential part. We need to call
application.create(). Before we called create, all objects existed only
on our side of the application. By calling create, all the server side
resources are also created. Note that FXApp::create will automatically
call create() on any toplevel window it knows about.
Finally, FXApp::run() will start the main event loop.
Now that we have our main function, the next thing we should do is to
declare our "MyWindow" class. We will do this in a file called
"mywindow.h".
class MyWindow : public FXMainWindow {
Every class that is derived from FXObject should use the macro
FXDECLARE(..). This ensures our object can receive messages and can be
used in serialization. The FXDECLARE macro declares a static const
member variable called metaClass, which is a table describing this
class. It provides some form of runtime type information. It also
declares a virtual function getMetaClass() which can be used to obtain
a pointer to an objects metaclass variable; this way, one can
interrogate the type of an object. In addition, it declares a static
member function called manufacture() which will construct an object of
this class using the default constructor.
FXDECLARE(MyWindow)
As stated above, the FXDECLARE macro forces us to define a default constructor...
private: MyWindow() {} public:
MyWindow(FXApp *);
virtual ~MyWindow();
And finally we override the create() function from FXMainWindow.
virtual void create(); };
The only thing left to do, is to implement our "MyWindow" class. Lets
get back to "mywindow.cpp" and start implementing. We start by defining
our message map. For now we will define an empty messagemap since we
won't handle any messages ourselves (It's not necessary to defined it
now, just more of a convenience thing since we know we're going to add
stuff to our message map).
#include "fx.h" #include "mywindow.h" ... FXDEFMAP(MyWindow) MyWindowMap[]={0 };
The FXIMPLEMENT macro is used to define and fill-in the table
declared using FXDECLARE. It defines the static member function
manufacture(), the virtual member function getMetaClass(), and fills in
the static member variable metaClass. If the object handles messages,
it also fills in a pointer to the message table. The first two
parameters specify our class name and the name of the class we derived
from. Make sure those are both correct. Especially if you fill in the
wrong base class name, strange things may occur. The third and fourth
parameter are related to message map. The third parameter specifies the
defined message map and the fourth the size of this map. Note that FOX
provides a convenient macro to get the size of the message map! If you
didn't define a message map you can just fill NULL and 0 for the last two parameters. Since we already defined it we just do this:
FXIMPLEMENT(MyWindow,FXMainWindow,MyWindowMap,ARRAYNUMBER(MyWindowMap))
Next we will implement the rest of our class, starting with the
constructor. The constructor of our baseclass FXMainWindow takes 9
parameters. The first one is a pointer to FXApp. The second parameter
specifies our window title. The third and fourth parameters are used
for specifying our window icons (big and small). The sixth parameter
specifies the decoration to be used for our window which are drawn by
the window manager. Using decoration hints, we can influence the
behaviour and/or "look and feel" of our window. There are several hints
available.
- DECOR_ALL - All hints are passed to the windowmanager
- DECOR_NONE - Borderless window
- DECOR_TITLE - Window title
- DECOR_MINIMIZE - Minimize button
- DECOR_MAXIMIZE - Maximize button
- DECOR_CLOSE - Close button
- DECOR_BORDER - Border
- DECOR_STRETCHABLE - Window is stretchable.
- DECOR_SHRINKABLE - Window is shrinkable.
- DECOR_RESIZE - Combination of DECOR_STRETCHABLE and DECOR_SHRINKABLE
- DECOR_MENU - Window has a menu.
Multiple hints may be passed by using the | operator (ie.
DECOR_BORDER|DECOR_CLOSE). Note that these are hints and depending on
the window manager being used may not always look or behave the same.
The next four parameters control the position and the initial size of
the window.
MyWindow::MyWindow(FXApp * a) :FXMainWindow(a,"The Window Title",NULL,NULL,DECOR_ALL,0,0,800,600){ }
MyWindow::~MyWindow(){ }
The last function we have to implement is MyWindow::create(). We
first call the FXMainWindows::create(). After that we will make the
window visible by calling show. There are several ways to place the
window on the screen:
- PLACEMENT_DEFAULT - Place it at the default size and location
- PLACEMENT_VISIBLE - Place window to be fully visible
- PLACEMENT_CURSOR - Place it under the cursor position
- PLACEMENT_OWNER - Place it centered on its owner
- PLACEMENT_SCREEN - Place it centered on the screen
- PLACEMENT_MAXIMIZED - Place the window maximized to the screen size
void MyWindow::create(){ FXMainWindow::create(); show(PLACEMENT_SCREEN); }
Well... that's all folks.. you just created your first FOX application.
Compiling FOX Applications
Unix Platforms
Libraries
The libraries are named using the following convention:
libFOX-$FOX_MAJOR.$FOX_MINOR.[a,so] (for example: libFOX-1.6.a,
libFOX-1.6.so etc). Note that the FOX 1.0.x didn't have a version
number in the library name (just libFOX.[so,a]). If installed by the
configure script, the libraries will be installed in $prefix/lib. By
default $prefix is "/usr/local".
Headers
On most Unix platforms the FOX header files are located in
$prefix/include/fox-$FOX_MAJOR.$FOX_MINOR. The configure script for FOX
will use /usr/local as the default prefix, though that can be changed
using the "--prefix=..." command line option. Note that the FOX 1.0.x
had its header files in $prefix/include/fox.
fox-config
If FOX was installed by configure you will find a "fox-config" script
in $prefix/bin. This script can help you setup your compiler flags and
check the version number of the installed library.
To query the version number of the library:
fox-config --version
To query the libraries that FOX depends on:
fox-config --libs
How to Compile with g++
Replace $FOX_PREFIX with the directory you gave as
--prefix
to fox'es ./configure script (usually /usr/local):
for FOX 1.0.x:
g++ -I$FOX_PREFIX/include/fox -o mywindow mywindow.cpp main.cpp -L$FOX_PREFIX/lib -L/usr/X11R6/lib -lFOX -lX11 -lXext -lm
for FOX 1.2.x:
g++ -I$FOX_PREFIX/include/fox-1.2 -o mywindow mywindow.cpp main.cpp -L$FOX_PREFIX/lib -L/usr/X11R6/lib -lFOX-1.2 -lX11 -lXext -lm
for FOX 1.3.x:
g++ -I$FOX_PREFIX/include/fox-1.3 -o mywindow mywindow.cpp main.cpp -L$FOX_PREFIX/lib -L/usr/X11R6/lib -lFOX-1.3 -lX11 -lXext -lm
The windows version of the g++ command is
g++ -I$FOX_PREFIX/include/fox -o mywindow mywindow.cpp main.cpp -L$FOX_PREFIX/lib -lFOX -mwindows
for FOX 1.6.x:
g++ -DWIN32 -I$FOX_PREFIX/include/fox-1.6 -o mywindow mywindow.cpp main.cpp -L$FOX_PREFIX/lib -lFOX1.6 -mwindows
|