PDF in Gambas — I review the Search Engine Terms that show up on people’s searches that bring people to this blog. One of the terms that shows up frequently is “Gambas .PDF”, “Gambas open .pdf”, “Gambas save .pdf”, or something similar. For anyone who is interested in interfacing Gambas with .PDF files:
- You can find information about the gb.pdf component at “http://gambasdoc.org/help/comp/gb.pdf?v3”
- A “PDF Printer” is an included installed printer on every GUI distribution of Linux that I’m aware of. If it is not included with your distro, it can easily be installed from the repositories. An easy way to generate a .pdf file from inside your program is to write a printing routine, and the user can select “Print to File”, and select the .PDF file format. To make it easier, you can link a button control directly to your print routine and make the PDF printer the default choice. You can see an example of this in Download #4 in the Flash Widget — the “Screenshooter” program includes a pre-selected “Save as PDF” print-to-file option.
Filling list controls from arrays — Just in case anyone has not noticed this, you can assign an array list to a list-based control in Gambas:
- “ListBox1.List = strSomeKindOfArray”
That’s really fast and convenient. However, you can’t assign the ListBox1 contents to an array: “strSomeKindOfArray = ListBox1.List” will not work — and it will not give you an error message! You must use some kind of loop like “FOR…NEXT” to transfer the control list to an array.
Some notes about Key functions: The Web-based documentation for Gambas is really good (much improved from the past). But some things need more explanation. We didn’t cover everything in detail in our book “A Beginner’s Guide to Gambas”, because … you know, “…Beginner’s…” Here is some clarification about the Key component that might be useful.
- Using Key.Code — You can only use Key.Code inside a Sub for a _KeyPress or _KeyRelease event:
Public Sub TextBox1_KeyRelease() ‘When TextBox1 has the focus, if you hit Return or Enter, it will be the same as clicking Button1.
__If Val(TextBox1.Text) Then ‘If only numerical values have been entered.
‘Note that the “Return” key is the “enter” key near the center of a standard desktop PC keyboard, with the “return” symbol.
‘The “Enter” key is the “enter” key at the right side of a standard desktop PC keyboard, with the numeric keypad.
‘The keys are equivalent in function, but each one returns a different key code.
____If Key.Code = Key.Return Or Key.Code = Key.Enter Then
______Button1_Click() ‘Here, we call the Sub Button1_Click as if we actually had a Button_Click event. It is not necessary for the user to trigger the actual event.
- “Key.Code” returns an integer code for each key, and String.Chr or CHR$ converts that integer into a character. Therefore, to capture the key that was pressed on a keyboard (again, it must be inside a Sub for a _KeyPress or _KeyRelease event), you can use this syntax:
strChar = String.Chr(Key.Code)
Have radiobuttons with no radiobutton selected: Unlike CheckBox controls, RadioButton controls automatically de-select the previously selected button, when you click on another RadioButton in the same container, but one of them will always be selected. Have you ever wanted to have no RadioButton selected? You can do that by creating one more RadioButton than you need, and make that RadioButton invisible — “RadioButton7.Visible = False”. Then, you can open a form with RadioButtons, but you can set the focus to the invisible RadioButton — “RadioButton7.SetFocus”. In the course of the program, you can use that statement to transfer the selection back to that invisible RadioButton, if you want to.
Shell to multi-line Linux commands, and receive output from interactive Linux commands: When you want to use a Linux program and capture its output (like “ps aux”), or use the sudo command (which requires that you issue the “sudo…” command, then follow up with a password after you are prompted), the Gambas “Shell” function will work fine.
- The Shell statement below allows you to run the Linux commandline program to find running processes, and the TO keyword allows you to capture the output in a string. You can parse the string with the Split function, etc., display the data in one of the View controls (like ColumnView or GrindView), and analyze the processes — maybe manage the processes, like killing a process that is hogging too much memory or processor time: see http://gambasdoc.org/help/comp/gb/process?v3
Shell “ps aux” TO strShellResponseString
- In the Shell statement below, the -S (stdin) option after “sudo” causes sudo to read the password from the standard input instead of the terminal device. In this case, the standard input reads from your program. The password must be followed by a newline character.
In total, the construction below is a shell script construction meaning: run the command sequence “sudo -S chmod 0777 ~/myfilename” and redirect (<< EOF) the string “mypassword” into its standard input after that sequence, until you hit a line with “EOF” on it, which signifies the end of input to the interactive shell.
The shell will recognize “\n” as the newline code — there must be NO extra spaces before or after “\n”, because empty spaces will be interpreted as part of the script or part of the password. The TO keyword will assign the output of this command to your string variable “strShellResponseString”, in case you want to display that in your program — for example, if you input the wrong password, sudo will give you an error message.
Shell “sudo -S chmod 0777 ~/myfilename << EOF\nmypassword\nEOF” TO strShellResponseString
Just a few interesting Gambas usages that were not obvious from the online documentation.