First Program

From PilibWiki

Revision as of 14:18, 19 March 2007 by Mhier (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

In order to start with good basis, it is normal to begin with something very simple. We will therefore create first a simple window with the message ‘Hello World’ and then try to compile it without troubles.


To begin, launch your FORTRAN 95 text editor (I personally recommend Scite or photran for Microsoft Windows stations, both free of charge), and then copy in this code, explanations are following after. (This code is the one given in PILIB original documentation.)


Please notice that lines followed by this symbol ‘ ! ’ are essential, it means they will need to be in your future programs with PILIB).



program example

use pimod						!	(1)
call piinit					        !	(2)

call gkwindow(c('pilib example'), 25,0, ihwin,iclos)	        (3)
call gktext(c('Hello World'),ihtext)                    	(4)
call gkput(0,0,-1,-1,ihwin,ihtext)				(5)
call gkshow(ihwin)						(6)

do while(iclos.eq.0)					!       (7)
call gkproc                                             !
end do                                                  !

call gkdestroy(ihwin)                                   	(8)

end program example


Explanations:


I will try to be the clearest as possible.


First of all, we need to call the PILIB FORTRAN 95 module, called pimod here at (1).


Then libraries need to be initialized (2). This step is necessary if you want to us those libraries’ functions.


The next step is more intuitive. We first create a GTK+ window by means of gkwindow (3) and we call it ‘ihwin’. This last will display ‘Pilib example’ in the title bar. For more information on how to create windows, please have a look at the section ‘Windows’ in ‘Different Widgets’.


Notice two important things:

1. Here we use a conversion: c('pilib example') . It is due to the fact that GTK+ is C language made and it use type(string). All the time you will need to insert a ‘character’ in a widget, you will have to call this function, or made the conversion before. Please refer to section ‘Conversions’ for more details.

2. Because we haven’t declared our integers, I mean ‘ihwin’ and ‘ihtext’, we need to begin them by the term ‘ih’. In other words, if you want to call a widget ‘car’ for example, you will need to call it ‘ihcar’, or declare it just between ‘use pimod’ and ‘call piinit’ like that : ‘integer :: car’.


Next, we create a text widget, by means of gktext, called ‘ihtext’ and with the value ‘hello world’, and we add it in the window ‘ihwin’. (We will see how to use gkput in the part ‘co-ordinates’ in section ‘Organize a window’.)


Notice here that PILIB system is very simple because it uses hierarchy to classify widgets. Let me explain: you want to display a window with a button, and a text. In that case, you cannot add directly both widgets in the window, you will need to use a container (Like Panels in JAVA) because a window can only one Master widget. Then, you just have to put the button and the text in the container, and add the container in the window. Be careful, if you destroy a widget, that contains other widgets, you will destroy all of them.


Here is a picture to explain how it works…

Organization of PILIB windows


To go back to the program, we finish by displaying the window with gkshow (6) and in order to make the window waiting for an action, we create a loop (7). Don’t forget this loop or your program will freeze or just don’t display. We will see after how to use this loop in widgets section.


Last, when the user has click on close, we want the window to close. That’s why we will destroy it thanks to the gkdestroy function, here at (8).


Congratulation, your program is now ready to work with PILIB. Still alive?


We save the file to ‘example.f95’.


The next step is to compile our program and launch it. We will suppose that you are on Linux or on windows (sorry for Mac users) and that you are using the g95 compiler.


Open a console (Msys on windows) and navigate to the folder were your program is. Then enter the following command:



On Linux :

g95 fenetre.f95 -lpilib -lpilibf

On Msys :

g95 example.f95 -L/usr/lib -lpilibf -lpilib -I/usr/include


The ‘-lpilib’ ‘-lpilibf’ commands are asking to your compiler to load PILIB.


Then type


On Linux

./a.out

On Msys

a


Your windows will normally display now. If not, it seems you will have to ask on the forum why.


Example window of PILIB
Example window of PILIB


Notice that all our elements are here. You can now edit this little program in order to see what is going on if you change something. Then see you in the Different Widgets section.

Personal tools