REALbasic University Resources:

RBU: Glossary Defines common REALbasic programming terms
  Archives Previously published columns
Translations: Dutch Courtesy of Floris van Sandwijk
  Japanese Courtesy of Kazuo Ishizuka
  Chinese Courtesy of Dong Li
  RBU Translation Guide Information on Translating RBU into other languages
Books: Matt's Book (2nd Edition!) Ideal for experienced programmers
  Erick's Book Best for beginning programmers
Websites: Mother Ship The publisher of REALbasic
  RB Webring Links to hundreds of REALbasic websites
  RESExcellence Another REALbasic programming column
  REALbasic Developer Magazine The premiere source for REALbasic instruction.

REALbasic University is Sponsored by

Make your Mac do what YOU want it to. Create games, utilities, cool Mac OS X tricks. Download REALbasic now and create your own software.


Print This Article

REALbasic University: Column 062

FontPrinter: Part One

First, I'd like to apologize for the irregular publishing schedule of RBU lately. Between vacaction, traveling, a brief bout with a cold, a trip to New York City for the Macworld Expo, and the publication of the premiere issue of REALbasic Developer magazine, I've just been going six directions at once lately.

Fortunately, I'm back at home now and working full-time on RBD and my freelance activities, so things should settle down into a more routine schedule. For the next couple of weeks I plan to do two postings a week to catch up a bit (make up for missed weeks), so get ready to code!

I'd originally intended to do a quick review of the new REALbasic 4.5, but I just haven't had the time to explore it properly myself! So instead I'm going to do a quick series exploring something many people have been interested in: printing in REALbasic. I'll do my RB 4.5 review in a couple weeks.

About FontPrinter

FontPrinter is an application I wrote for myself a few years ago. Working in graphic design, I found myself frequently needing to know the keyboard combinations that produce various special characters, especially in symbolic fonts (like Zapf Dingbats).

There used to be various utilities to do this: KeyCaps was Apple's limp version of a program that displays an onscreen keyboard and allows you to see the what key combinations produce in various fonts. However, it was pathetic compared to Fifth Generation System's BigCaps, which let you specify the size of the keyboard. With KeyCaps the symbols were often so small I couldn't even tell what they were! Sadly, BigCaps died a few OS revs ago, and there's been nothing to replace it.

There are third-party programs that produce type samples from fonts, but those are often overkill and cost money. I tested many but never found one that worked they way I wanted. So one day I wrote FontPrinter -- I've never looked back since.

The principal behind FontPrinter is simplicity. There are few options: you simply pick a font and hit print. The chart it prints out fits on one 8.5" x 11" page and gives you all the symbols accessible via the keyboard. It's primitive in many ways and probably doesn't support international keyboards well, but since it's written in REALbasic, it's easily modified to handle any situation you'd like.

So you understand where we are going with FontPrinter, here's a sample of the output it produces (the Webdings font was used):

There are four columns. Each corresponds to characters typed with different modifier keys: Normal (no modifier), Shift, Option, and Shift-Option together.

Within each column you find, in parenthesis, the normal letter produced by that key combination (printed in Helvetica), followed by the same letter in the selected font (printed much larger). The larger letters are staggered in order to save vertical space. This lets you output symbol or dingbat fonts and still be able to see what letters are what. Note that the letters are in the same order as the QWERTY keyboard layout, and the light gray bars behind every other line help you see which character you need to type to produce what symbol.

So, for example, if you wanted to produce the pushpin dingbat, we can see from the chart it is on line d, fourth column, so typing Shift-Option-d would produce one.

Note that the above screen shot is from FontPrinter's print preview feature: it's a flexible system you can use in your own programs, and as you'll learn, it's a great way to test printing without wasting acres of paper.

Starting the Project

We'll begin by creating a new folder for our project. Name it FontPrinter and inside it put a folder called Linked Resources (which will contain graphics and other things our program will need).

Launch REALbasic and start a new project (File menu, "New") and save the new project as FontPrinter.rb like this:

Now select Window1 and give it the following settings:

We're going add some simple controls to the window next. Make it look something like this:

Obviously, there's nothing too complicated going on here, and the exact positioning or settings for the controls isn't critical. However, the names of the controls aren't obvious from the picture, so name them like this:

popupMenu = fontPopup
checkBox = checkPreview
pushButton = printButton

It's also potentially important to make sure that initially checkPreview is checked and printButton is disabled.

Adding a Font Menu

The main interface item of FontPrinter is the font menu. This is ridiculously easy to generate in REALbasic. (The code is actually available within the online help.) Just add this code within the Open event of fontPopup:

  
dim i, n as integer

n = fontCount - 1
for i = 0 to n
me.addRow font(i)
next

me.listIndex = 0

FontCount is a built-in function of REALbasic, which returns an integer telling you how many fonts are installed on the current system. We subtract one from it so we can start our loop at zero (0), since the font() function starts at zero. If we didn't start the loop at zero, our font menu would be missing the first installed font! Also, if we didn't subtract one from fontCount, our loop would go one too many: font(fontCount) is an "out of bounds" error since font() only has as many items as fonts installed, but starts counting at zero.

The final step in the code ensures the first font in the list is selected.

That's it! Run the program and you'll see the popup menu contains a list of all the fonts installed on your system and you can pick and choose among them. Pretty cool, eh?

Extra Credit: you could, if you wanted, create your own popupMenu subclass that contains this code and call it FontMenuClass. Then you could export that class (drag it to the Finder) for use in other programs. Instant font menus!

Adding Menus

Next, let's take care of some busy work and add some standard menus to our program. First, add an About menu command. Double-click on the Menu item within your project. In the blank line under the Apple Menu, type in "About FontPrinter..." and press return. Rename the menu item to "AppleAbout" (it's shorter and simpler).

Then add two menus to the File menu, like this:

Notice that I've dragged them into the proper order, and I've added a separator (type a hyphen, "-", as the item's text and press return) and placed it above the Quit item. I also gave the Print commmand a "P" as a keyboard shortcut (Page Setup doesn't usually have one). We'll use the default names for all these items.

When you're finished with that, let's add some menu handlers for them. (Menu handlers are like methods, except they execute when their corresponding menu item is selected by the user.)

Go to the Edit menu and choose "New Menu Handler" and one-by-one, select appleAbout, filePageSetup, and filePrint to add each handler. For now, we'll leave these handlers blank, but that won't hurt anything -- the program will work, but nothing will happen when a menu item is chosen.

Of course if you run the program now, none of the new menus will be available: we never told REALbasic to activate them (the default is to keep them disabled).

Go to the EnableMenuItems event and this code:

  
appleAbout.enabled = true
filePageSetup.enabled = true
if fontPopup.text <> "" then
filePrint.enabled = true
printButton.enabled = true
else
printButton.enabled = false
end if

Notice what we're doing here: first, we enable appleAbout and filePageSetup, which should always be available (never disabled). Since printing is only available once a user has selected a font (there'd be no point in printing without a font chosen), we make sure the print menu and the print button are disabled if fontPopup is not set to a font. (Note that we only have to disable printButton manually since filePrint is disabled by default.)

That pretty much wraps up the bare bones interface for FontPrinter: in our next lesson, we'll start creating the code to make this program do stuff!

If you would like the complete REALbasic project file for this week's tutorial (including resources), you may download it here.

Next Week

We begin coding FontPrinter.

Letters

Here's a letter from Ben, who has got some excellent questions about the difficulty of learning to program.

Marc,

I appreciate all the effort that goes into the RB Univ. and the attempt to help neophytes learn to program. The trouble is, 95% of all the stuff I see in these sites is completely useless to me.

Let me give my demographics. I am a scientist, I own a low tech manufacturing busness (mfg solar energy equipment), do R&D for hire and self, invent things (microscopes, cystic fibrosis therapy devices, etc.), and tinker.

In the early '80s I wrote 5 programs on my AII to run the solar mfg business. They included an Order Entry/Purchase Order pgm, a Ledger pgm (pre Quicken, but better in some ways), an engineering pgm to calculate the performance of a solar system for a particular application, a financial analysis pgm to determine the internal rate of return of a solar installation, and finally, a computational pgm to calculate the shape of a frame to hold collectors due south based on the roof pitch and orientation.

We used these pgms extensively in our business and even sold the pgms to engineers and dealers, so they could do the calcs real time without calling me. Our bookeeper used the Ledger pgm to keep our books.

When the AIII came out, I converted all the pgms to Apple Business Basic. The solar co is pretty much moth balled now, but I still have need for a lot of the software I wrote and would like to get it going again in a modern language.

Enter RealBasic. Since the structure is completely different from old sphagetti code, it is a daunting task to learn. I have not seen a single tutorial that describes what the structure is all about and how it works. Oh, there are plenty of books that go down to 10,000 feet in about three pages and claim to be for beginners. Hogwash!

The only way I can learn a new pgm architecture is by examples. Most of the examples I see are about games, sticking pretty pictures some where and scrolling them - absolutely nothing of use to someone who wants to write non-game/non-play software. I am not trying to sound harsh here, I just see nothing out there to help a guy like me who wants to get some simple business programs running, and hopefully commercially ready.

I have been at it for over 1 1/2 years now, working on my first pgm in RB. It is the order entry/purchase order pgm, which has universal utility for me. I am now about 85% complete. I got here by crashing along, learning by accident, and study (I have Newberg's ? book). Obviously, this is a part time effort - there is no staff of programmers around here.

Some of the simplest things are not covered. Up front - what are the meanings and uses of the all the event areas under an editfield? How does the code flow? How to capture key strokes? How to build a simple table of values - a spreadsheet - that you can motor around in and enter and erase entries - very hard. How to do column math with a qty, description, price, and total column. How to open a file to read a name. How to specify the path to a particular folder, so I don't have to navigate to it each time? How to print? What does a canvas mean?

The looping of event driven pgmg drove me nuts. I had no way of knowing where the pgm was going. How do I know when this key is evaluated? Breakpoints were of very minor help.

All the quirks of RB are another problem. Number formatting is poor and tedious and has bugs. Error messages are non descriptive. Editfields don't work right - I have lingering cursors in editfields that won't go away. etc, etc,...The whole business of inserting a number in a editfield and have it formatted as a number and justified correctly.

As I mentioned earlier, I am about 85% of the way to finishing this program, so I have answers to many of these questions. But I still don't have any confidence that I know what i am doing. I feel like the blind man who has stumbled from the house to the outhouse, but has little idea of how he got there.

OK, let me sum this up. Simple examples are the most important way I have gotten where I am. I have made up a folder I call 'Utilities" where I stuff pieces that demonstrate something I need. When I check the references, the theory is usually out of context and way over my head. I still have no idea how to implement the long list of parameters under editfield, for example in the reference. I don't know how or where the commands are used. I read all the stuff and then go straight to the examples, if there are any. I will see if I can get it to work. Five examples with different twists are even better. I have never gotten a Page Setup command to work for printing. After 1 1/2 years, I understand much more that when I started, but it is still a scramble of pieces.

My sincere hope is that someone will open a website where one can learn the basics, the fundamentals, the small pieces. Event driven pgmg is very complicated - we should quit calling it easy, it is hard. If you don't have the architecture firmly in your head, you are completely lost. It took me months to get started. I didn't know what elements it took to get a pgm to run. All the built-in event listings were daunting. I had no idea what they were for.

Well, I am repeating myself, thanks for your attention.

Ben Gravely

Thanks for your letter, Ben. I've been thinking a great deal on this subject recently, and while I don't plan any radical changes immediately, I am considering some changes in teaching techniques.

I am now in charge of two teaching platforms: REALbasic University and REALbasic Developer magazine. RBU is a weekly web-based tutorial written sometimes just hours before publication, while the magazine is written months in advance. It is important to me that I taylor the content and teaching style of each medium to match the audience. (Any thoughts or ideas you have in regards to this are appreciated.)

I firmly believe both publications are important and needed, and each reach a slightly different market (with some overlap, or course). The magazine, of course, since it's written by dozens of people, will be more diverse, covering topics that I know little about.

My original idea for RBU was for it to be a place to build, step-by-step, real programs that people could use. I still like that idea and think it's a good way to learn programming since most programming concepts are transferable from one type of program to another. For instance, you may not be interested in a font printer like we're making now, but this tutorial will teach you how that Page Setup command works, which sounds like something you need.

However, I am leaning away from that slightly as the more complete programs take a long time to cover (we spent six months on RBU Pyramid). I am now looking for real (i.e. useful) programs that I can cover in three or four lessons.

You are correct that programming, even object-oriented programming, is not easy. It's easier than traditional programming in many ways (especially if your mind isn't "corrupted" by traditional techniques ;-), but it's still work. I'm very much in favor of non-programmers learning to program -- I think it's one of the best thinking skills a person can have -- but at the same time, despite the ease-of-use of a product like REALbasic, I caution people that it isn't as easy as following a step-by-step recipe and baking a cake.

Looking at the specific things you mention, you might note that you're asking for two different things. On the one hand, you're wanting help for simple items (like what is a canvas), but then you're also wanting a spreadsheet control (which is challenging).

I'll be honest with you: as someone who learned to program in a non-graphic user interface environment (i.e. a PC running DOS), there are many times a non-graphic environment is better and simpler to program!

There are always two parts to a program: the way the program looks to a user (the interface) and the way the data looks to the program (the data structure). Often these two are complete opposites. That's simply because humans don't think like computers and vice versa. Take something simple like a time and date: a computer is perfectly happy using a 15-digit number to represent the current time and date in seconds. For a human, that's practically impossible to understand: we need the number of seconds translated into a calendar date and o'clock time.

Much of what makes a program complicated is the translation of computer data to human format and vice versa: when you add an easy-to-use user interface on top of that, it makes the program even more complex. Throw in event-driven programming when you're used to traditional sequential programming and you've really got a pickle.

For example, you mention something like a spreadsheet control: a way to edit and sum columns of numbers.

Sure, REALbasic can do something like that, but there's no built-in support for it. You must use a flexible listBox control and program it to work like a spreadsheet. It's doable, but not easy, and there's a lot of text-to-number conversion involved (since listBoxes only work with text). That actually sounds like a good project for RBU -- I'm going to consider that for a future lesson.

In fact, I'd like to solicit ideas for future columns. What types of projects would you like to see me cover? Please be as specific as possible. Like most people, I learn best from practical examples, and if there are simple projects that I can cover in a few lessons that still let us create useful applications, I want to do them. Send your suggestions to rbu@stonetablesoftware.com.


About the Column
REALbasic University is a weekly instructional column on programming with REALbasic and is brought to you by REALbasic Developer, the magazine for REALbasic programmers.

Each week we answer select reader questions, and we're always open to ideas for future columns. Send your questions to . (Keep your questions simple and specific. General queries like "How do I write my own web browser?" will be neglected.) Your question won't be answered immediately, but will be answered in a future column. (If you don't want your correspondence published, just be sure to indicate that when you write. Otherwise it's fair game.)

About the Author
is an author, philosopher, graphic designer, photographer, film director, soccer fanatic, and programmer (among other things). He writes for MacOpinion, runs his own software company, Stone Table Software, which sells the revolutionary Z-Write word processor, and is Publisher and Editor of REALbasic Developer. He lives in Northern California with his cats, Mischief and Mayhem, and is rapidly running out of free time.

See the REALbasic University Archives


REALbasic University contents ©2001-2004 by Marc Zeedar and REALbasic Developer. All Rights Reserved.

Email This Article - Comment On This Article

.

Reader Specials

Server Racks Online:
Apple Xserve CompatibleServer Racks and Universal Network Racks
42U KVM Switch Solutions:
High-End Mac and Multi-Platform KVM Matrix switching solutions!
Digital Camera Online:
Great prices on Digital Cameras and accessories!
KVM Switches Online:
Great prices on Mac KVM Switches from the leading manufacturers!
LCD Monitors Online:
Great prices on LCD Monitors from the leading manufacturers!
LCD Projectors Online:
Shop online for LCD Projectors from the leading manufacturers!
USB 2.0 Online:
Great prices on USB 2.0 products from the leading manufacturers

Serious Business Software:
Accounting, Sales, Inventory, CRM, Shipping, Payroll & more!

KVM Switch solutions for MACs:
DAXTEN is a KVM switch, KVM extender and monitor splitter specialist for PC, SUN and MAC applications from name brand manufacturers - offices worldwide.

The "Think Different Store: The iPod Accessories Store - iPod cases, iPod mini, iPod photo, speakers, itrip, inMotion, Soundstage and all other iPod accessories

Earn Cash with the ThinkDifferent Store Affiliates Program

Need A Web Site?
Applelinks Web Hosting Starting at 19.95 a Month

iTunes_RGB_9mm

.

iTunes_RGB_9mm

Cool Mac Gear


iPod 1G-2G
iPod 3G
iPod 4G
iPod Mini
PowerBook-iBook
Keyboard Skins
Garageband