howto glade freebasic

15
Howto: Rapid GUI creation with FreeBasic and glade/Gtk by Klaus Siebke, www.siebke.com Preface For a longer time I was looking for a programming environment that offers both: easy to use and platform independent. Some weeks ago I found FreeBasic to fulfill these requirements perfectly . ust code your program once and run it in native mode under !S "indows and #inu$ %and if you like you can still run it under &'S(. )dditionally FreeBas ic in combination with glade*+tk offers a simple framework to create identical +,I applications for "indows and for #inu$. -he only lack so far is a tool to simplify the +,I program creation with glade*+tk. So I wrote a small utility named  glade2bas which will help you to reali/e your own +,I application with FreeBasic and glade*+tk. What you need 1. First of all yo u need the FreeBas ic comp iler for your pr eferr ed OS which c an be downloaded from: http://www.freebasic.net/ . . !hen yo u need to ins tall "tk. #f yo u are usin$ % inu& it's a lready in clud ed in your distribution (at least in )buntu/Kubuntu or openSuS*+. ust $o to your packa$e mana$er (apt or yast+ and select it. #f you are usin$ -S indows, download the latest binary from the official serer: ftp://ftp.$tk.or$/pub/$tk/  and install it. 0. !he ne&t co mpone nt you need is $lad e, the user interface builder . For %inu& user s it's the same as for "tk: install the packa$e with your packa$e mana$er that's it. )sers workin$ with -S indows should $o to http://$ladewin0.sourcefor$e.net/ , download the latest ersion of $lade and install it. 2. 3n edito r to code you r applic ation. # wou ld prefer "e any whic h suppo rts FreeBasic in a ery comfortable way . 4 ou can compile and run your application 5out of the bo&5. "eany is a ailable for -S indows and %inu& and like all the aboe software components it's free. 4ou can download "eany from http://$eany.uena.de/  . 6oweer , in some %inu& distributions "eany is already included. #n this case make sure that the distribution's ersion is 7.1 or hi$her (otherwise the FreeBasic support is not implemented+. 8. !he $lad ebas u tility to cre ate a shel l pro$ram o ut of the $lad e file. 4 ou can download the source of this utility from my pa$e www.siebke.com/freebasic/$ladebas.9ip . )n9ip the file in your FreeBasic source directory and compile it with fbc ;s $ui $ladebas.bas. A first application %et's start with a simple e&ample. %ike you probably know it from other pro$rammin$ lan$ua$es, it's a small 56ello orld5 application. esigning the interface with glade ! o desi$n the window(s+ you can either use $lade or $lade 0 whateer you prefer. Because it's the newer ersion, # use $lade0 in the followin$ e&ample.

Upload: houari-hou

Post on 02-Mar-2016

29 views

Category:

Documents


0 download

DESCRIPTION

Howto Glade Freebasic

TRANSCRIPT

Page 1: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 1/15

Howto: Rapid GUI creation with FreeBasic and glade/Gtk

by Klaus Siebke, www.siebke.com

Preface

For a longer time I was looking for a programming environment that offers both: easy touse and platform independent. Some weeks ago I found FreeBasic to fulfill theserequirements perfectly. ust code your program once and run it in native mode under !S"indows and #inu$ %and if you like you can still run it under &'S(. )dditionally FreeBasicin combination with glade*+tk offers a simple framework to create identical +,Iapplications for "indows and for #inu$.-he only lack so far is a tool to simplify the +,I program creation with glade*+tk.So I wrote a small utility named  glade2bas which will help you to reali/e your own +,Iapplication with FreeBasic and glade*+tk.

What you need

1. First of all you need the FreeBasic compiler for your preferred OS which can bedownloaded from: http://www.freebasic.net/.

. !hen you need to install "tk. #f you are usin$ %inu& it's already included in yourdistribution (at least in )buntu/Kubuntu or openSuS*+. ust $o to your packa$emana$er (apt or yast+ and select it. #f you are usin$ -S indows, download thelatest binary from the official serer: ftp://ftp.$tk.or$/pub/$tk/ and install it.

0. !he ne&t component you need is $lade, the user interface builder. For %inu& users

it's the same as for "tk: install the packa$e with your packa$e mana$er that's it.)sers workin$ with -S indows should $o to http://$ladewin0.sourcefor$e.net/,download the latest ersion of $lade and install it.

2. 3n editor to code your application. # would prefer "eany which supports FreeBasicin a ery comfortable way. 4ou can compile and run your application 5out of thebo&5. "eany is aailable for -S indows and %inu& and like all the aboe softwarecomponents it's free. 4ou can download "eany from http://$eany.uena.de/ .6oweer, in some %inu& distributions "eany is already included. #n this case makesure that the distribution's ersion is 7.1 or hi$her (otherwise the FreeBasicsupport is not implemented+.

8. !he $ladebas utility to create a shell pro$ram out of the $lade file. 4ou can

download the source of this utility from my pa$ewww.siebke.com/freebasic/$ladebas.9ip. )n9ip the file in your FreeBasic sourcedirectory and compile it with fbc ;s $ui $ladebas.bas.

A first application

%et's start with a simple e&ample. %ike you probably know it from other pro$rammin$lan$ua$es, it's a small 56ello orld5 application.

esigning the interface with glade

!o desi$n the window(s+ you can either use $lade or $lade 0 whateer you prefer.Because it's the newer ersion, # use $lade0 in the followin$ e&ample.

Page 2: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 2/15

 3fter startin$ $lade;0 you will $et this screen:

!o create a new window for your application, direct your mouse pointer to the 5<alette5 andchoose the first icon under 5!opleels5:

=lick on it and your first $lade/"tk window will be created:

>ow you hae to add a layout to the window. 4ou will find it in the 5<alette5 under5=ontainers5:

Page 3: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 3/15

 3$ain, click on the icon and then into your window. !he window appearance will chan$e asfollows:

>ow you are able to place some elements on the window. #n the 5<alette5 you will find a lotof wid$ets under 5=ontrol and ?isplay5. Select the 5OK button5 and the 5label5 wid$et byclickin$ first on the icon and then on your screen:

Page 4: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 4/15

#f you like, you can now resi9e and arran$e the elements a little bit:

!o chan$e the te&t and the action of the elements click on the wid$et to be chan$ed andlook at the 5<roperties5:

=han$e the te&t of the label from 5label5 to 56ello orld@5 and the te&t of the OK button

from 5button5 to 5OK5.

Page 5: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 5/15

4our window should now look like this one:

%et's chan$e some properties (title, startup position and si9e+ of the window itself:

Page 6: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 6/15

Finally you should define some actions (Asi$nals+ to react on in the FreeBasic pro$ram:

1. ?efine a si$nal to terminate the pro$ram when the window is deleted. "o to theproperties and click on the 5Si$nals5 tab strip. !hen $o to line 5delete;eent andclick in the 56andler column:

!he name of the handler will automatically be completed:

<ress *nter to assi$n the handler name to the eent.

=lick here and

enter an 5o5

Page 7: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 7/15

. ?efine a si$nal when the OK button is pressed  =lick on the OK button wid$et onyour screen. "o to the properties and click on the 5Si$nals5 tab strip (sameprocedure as with the si$nal aboe the handler name will be automaticallycompleted when you key in 5o5 and press *nter afterwards+:

<lease sae your work now as file 5helloworld.$lade5 by pressin$ the sae button in the$lade main window:

For this simple e&ample that's all on the $lade desi$n side. 4ou can now close theapplication and run the utility $ladebas.

Page 8: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 8/15

!on"ert glade to FreeBasic with glade#$as

 3fter startin$ $ladebas the followin$ screen appears:

=lick on the open button ri$ht to the te&t field and select the file 5helloworld.$lade5.

Page 9: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 9/15

>ow you can $enerate the FreeBasic shell pro$ram by clickin$ on the 5=onert5 button:

!he utility has created files: helloworld.decl and helloworld.bas.

Add your logic to the application

%ets hae a look at the $enerated source. Start "eany and open the file helloworld.bas:

Page 10: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 10/15

!he $enerated pro$ram is already runable. 4ou can compile und run it. 6oweer it will notreact on the defined si$nals. So it makes sense to add some additional statements.

On the top of the left side you can see the si$nals we defined in the $lade desi$n listed by"eany as 5functions5:

=lick on the first function 5onCbutton1Cclicked5 and the editor on the ri$ht will directlyposition to that function (or S)B in FreeBasic notation+:

>ow you can add a statement to react on the eent that is initiated when the OK button ispressed. ust key in:

print OK button pressed

below the comment bo&.

Page 11: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 11/15

!he other S)B (onCwindow1CdeleteCeent+ shall terminate the pro$ram. So key in:

$tkCmainCDuit(+

below that comment bo&.

>ow your code should look this way:

!o%pile the application

!o compile the pro$ram directly out of "eany please check the settin$s. "o to 5Build5 andthen to 5Set #ncludes and 3r$uments5 in the menu:

Page 12: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 12/15

-ake sure that the correct path to the FB= e&ecutable is specified for -S indows # hadto chan$e it on my machine as follows:

For %inu& you can leae the "eany default alues unchan$ed ...

... but you will hae to chan$e the !erminal strin$ in the properties:

 3fter checkin$ the aboe settin$s press the 5=ompile5 button on the tool bar and you will$et this messa$es in the status window:

Specify the pathof your source

codes here

Page 13: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 13/15

 3lternatiely you can open a command shell and compile the pro$ram manually.

Run the application

ithin "eany simply press the 5*&ecute5 button and the pro$ram starts:

!ry the OK button an see what happens in the command shell window:

>ow click on the 5&5 on the ri$ht corner of the window ...

Page 14: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 14/15

... and the application finishes.

6ere's how it looks in %inu& (kubuntu+:

&u%%ary

#n this lesson you learned how to write a simple $lade/"tk application with FreeBasic.For more information about "tk isit http://www.$tk.or$/. 6int: the $enerated shell pro$ramand the utiltiy pro$ram are fully commented (in en$lish and $erman+ and can sere as

e&amples for the $lade/"tk ")# pro$rammin$ with FreeBasic.

Page 15: Howto Glade Freebasic

7/18/2019 Howto Glade Freebasic

http://slidepdf.com/reader/full/howto-glade-freebasic-56d683e68b2be 15/15

'rou$leshooting

#f you are runnin$ into problems (especially on -S indows+ check the followin$:

?o you hae the latest "tk ersion installed on your systemE!here's a trick: the latest "tk ersion is always part of "#-<. So dowload "#-<

from http://www.$imp.or$/  and install it. 3fterwards copy (with replace+ the directory=:<ro$ram Files"#-<;.7bin into your "tk path, which should be=:<ro$ram Files=ommon Files"!K.7bin.

#s the lib$lade;.7;7.dll correctly installed (either in the application directory or in thewindowssystem0 directory+E

?isclaimer:

-icrosoft (-S+ indows and the indows lo$o are trademarks or re$istered trademarks of -icrosoft =orporation in the )nited Statesand/or other countries.

 3ll other product and company names mentioned may be trademarks or re$istered trademarks of their owners.