Programming Gambas from Zip/Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Welcome[edit | edit source]

This book is for anyone who wants to learn to write applications and has had no experience in it before. The programming language is Gambas and it runs in the Linux operating system. Gambas can be easily downloaded and installed from software repositories and is free in the best Linux tradition. The examples and screen snapshots in this book were made with Gambas 3.13.0. It has been a project for the first half of 2019.

Having recently completed it in LibreOffice, here it is as a wikibook. The PDF version, generated by LibreOffice, can be found by clicking the image on the right.

Why Program? Why Learn Gambas?[edit | edit source]

There are many fine applications out there. LibreOffice lets you type, format and print documents, create slide shows and calculate spreadsheets. FireFox lets you browse web pages. There are applications to send and receive emails. Still, there is nothing like teaching a computer to do something you want it to do and designing how it will look yourself. There may be things your school or work might want to do that no one has thought of—very specific things, sometimes very simple things.

You can be creative. I have a notebook application. It sits as an icon in the notifications area of the taskbar, always there. Any text I have copied to the clipboard—one click on the icon and it is saved. Right-click it and a window appears where I can search for notes just by typing. Control-click it and I can save the text on the clipboard with a key word or words attached to help me easily find that note again. Text copied from web pages often comes in separated lines, so I have a menu item to "Fix broken sentences". No formatting; no links; no pictures—just text saved with a click. With programs you write yourself, when you think of something you would like to do, you can just add it, like adding extensions to a house.

Gambas is a language and programming environment to get excited about. If you need a button, drag one onto the window. To tell it what to do when you click it, double-click the button and type the instructions. If you want a new window, choose New Window from a menu. It is as easy or as sophisticated as you want it to be. Everything your computer can do can be done through Gambas (as far as I can tell!).

Scope[edit | edit source]

Link to Gambas.One page where you can download the programming examples and PDF copy of this book

The book starts with what a computer language looks like. Ordinary speech has to become computer-speak. Tell me what four times three is has to become TextBox1.text = 4*3 . TextBox1.text.font is a little like John.glasses.frame.

From examples, we discover the three things computers can do: memory, repetition and conditional execution. Memory includes remembering text and numbers, in individual memories and lists of them (arrays), and calculating with them. Repetition is doing the same thing over and over without getting bored or needing coffee breaks. Conditional execution is doing one thing or doing another depending.

Writing and reading text files comes next.

Arranging buttons and boxes and things in the window is next, so things expand and contract properly when the window size is changed.

Menus and contextual menus follow. Have you ever wanted to call a menu after yourself? No, of course not.

Gambas can save settings automatically, so the application starts up the way you left it last time.

Programs need to be ordered and compartmentalised like rooms in a house. Languages have modules and classes to do that, so all the programming is not in one amorphous mass. Modules and classes are to programs what boxes and cupboards and shelves are to a house. Some can be copied and some cannot.

There is a language within a language, and that is SQL that is used to talk to database files. SQLite is introduced. I am no expert, you understand, but know enough to get by and enough to introduce the topic with an example of a program for allocating cash spending to different categories, saving it in an SQLite database.

Lastly there is how to print. This involves putting text and pictures on a page, drawing lines and boxes.

The appendices are useful tables for reference.

What Applications Will I Write?[edit | edit source]

There is the game of HiLo ... the computer picks a number and you try to guess it in as few goes as possible, each time being told if your guess is too high or too low.

Another guessing game is Moo, also called Bulls and Cows. You must guess a four digit number, all digits different, being told after each guess how many digits were in the secret number in their correct places (bulls) and how many were in the secret number but were not in their right places (cows). It was marketed under the name of Mastermind.

The game of Animal has been around since the 1970s. The computer starts knowing only two animals. You teach it new animals and the right questions to ask to identify your animals. It teaches binary keys—useful in biology. It is a little artificial intelligence.

The game of Concentration, or Memory, is where you turn over cards, hoping to find a matching pair. Your task is to find all the matching pairs. We start with cards that have letters on them and adjust it so pictures can show. You could have pictures of family members if you like, because the pictures are read in from disk when the application starts.

To illustrate databases there is a Cash Spending program that allocates cash amounts to various categories and totals the spending in each category, showing what percentage of the cash was spent in each area.

There are two printing exercises: printing a class list with vertical and horizontal lines that form boxes, and printing a calendar page on A4 paper for the current calendar month. You supply the large picture for the top, and the squares with the dates in them are big enough to write in and the page can be blu-takked to a refrigerator door.

The Tray Item Notebook is a small icon for the system tray. Click on it and any text you have on the clipboard is saved as a text-only note in an SQLite database. To search your notes, middle-click (left and right mouse buttons together) the icon and a window appears. A few adjustments to the text can be made: fixing broken sentences, double-spacing the paragraphs and trimming and tidying.