| |||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||
Print This Article REALbasic University: Column 062
FontPrinter: Part OneFirst, 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 FontPrinterFontPrinter 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 ProjectWe'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:
It's also potentially important to make sure that initially checkPreview is checked and printButton is disabled.
Adding a Font MenuThe 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:
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 MenusNext, 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:
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 WeekWe begin coding FontPrinter. LettersHere's a letter from Ben, who has got some excellent questions about the difficulty of learning to program. Marc, 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 See the REALbasic University Archives
REALbasic University contents ©2001-2004 by Marc Zeedar and REALbasic Developer. All Rights Reserved.
| |||||||||||||||||||||||||||