TweetFollow Us on Twitter

Rhapsody SimpleText

Volume Number: 13 (1997)
Issue Number: 11
Column Tag: Rhapsody

A Simple Word Processor...on Rhapsody

by Andrew Stone, Chief Executive Haquer, Stone Design Corp

Here is a Simple Rich Text and Graphics Word Processor you can build yourself in 20 minutes on Rhapsody

Leveraging the power of Rhapsody is your key to building cool apps quickly. If I asked you "How many lines of code would you have to write to implement a word procesor that reads/writes rich text files (including full support for graphics of EPS, TIFF, JPEG, PICT, GIF, etc type), full font support, full rulers with tabs and hanging indents, full support for color, printing, faxing and saving as PS with embedded fonts".

But wait, before you answer, that's not all (can you hear the Ginzu knife salesman yet?)... "What if it also had ligatures, kerning, superscripting, justification, underlining, ability to drag out graphics, copy and paste of contents and copy and paste of font styles? Don't answer yet, because it will also run on Windows95 and Windows NT, and has a built-in spell checker."

If I said 13 lines of code and that you'll be done in 20 minutes, would you believe it? This is why I quit developing on the Mac in 1989 when I saw my first NeXT demo, and why I'm happy as heck to be developing on a Mac in 1997!

First look at Rhapsody

The week before Macworld Boston 97, the Rhapsody Group at Apple invited several key OpenStep developers out to Cupertino to port their wares to Rhapsody running on the PowerPC. It was a great privilege to be included in this 3 day Kitchen - a MacHack with the Apple engineers dropping in and helping us at all hours. And, as promised, create, compile, and it "just worked".

This article will take you step-by-step through the process of creating a new application, building the user interface, generating the skeleton class code, filling in the 13 lines required, compiling and testing your word processor. If you encounter terms you don't recognize, please refer to the online documentation for developers, especially /NextLibrary/Documentation/NextDev/TasksAndConcepts/DevEnvGuide/DevGuide.rtfd.

Note that this article was written before the Developer release, so be aware that the screenshots may be a bit out-of-date! Note also that the purpose is to show how easy it is to get going, not good application architecture!

How to Build It

Step 0: Install the Rhapsody Developer release on your Power Mac, if you haven't done so already. Refer to Apple's instructions on how to do this.

1. Launch ProjectBuilder.app (aka "PB").

It's in /NextDeveloper/Apps, just double-click it.

2. Click Project - New...

An OpenPanel will come up - select "Application" for project type in the pop-up menu, type in "sWord" and "OK".

This will create a new directory named "sWord" with various project files:

PB.project: the file which maintains your makefiles.
Makefile, Makefile.preamble, Makefile.postamble: the makefiles.
sWord_main.m: the source file which defines main().
English.lproj: the directory with localized interface files (NIBs)
sWord.iconheader: the file which keeps track of App and Doc icons.

All of these files are created and maintained by ProjectBuilder - you don't have to write a single line of code to get your app skeleton.

3. In the sWord ProjectBuilder window, click "Interfaces", and then double-click "sWord.nib" to automatically launch InterfaceBuilder - where you will design your interface, create new classes, and test your application.

Note also the sWord-windows.nib file, which is for deploying your application on WindowNT and Window95, with no changes to your code! Since Windows organizes its menus differently, this NIB file will be different, according to Windows human interface (or lack thereof) design.

4. In InterfaceBuilder's Palette window, click the "Text" icon to load "DataViews", the palette containing Text in a ScrollView. Drag the ScrollView onto your main window, "My Window".

5. Move the ScrollView to the upper left hand corner of your window, and drag its bottom right knob to resize the ScrollView to fill your window.

6. We will now add the ability for the Text to be able to accept drag and drop graphics, as well as display and save them. Bring up InterfaceBuilder's Inspector Panel with Tools - Inspector. Be sure that the ScrollView is selected. All you have to do is click the "Graphics Allowed" switch!

7. InterfaceBuilder allows you to specify how objects behave when the window they are in is resized. For our ScrollView with our Text object to automatically fill the window, we must set its "autosizing". IB allows to visually set these constraints. This eliminates us having to write code to deal with window size changes.

Choose "Size" from the pop-up menu at the top of the inspector. The Size inspector allows you to specify how objects behave when a user resizes the window. Lines mean "stays fixed", springs means "size to fit". click the middle vertical and horizontal lines to allow the ScrollView to expand and contract with the window:

8. It's time to add power to our app, which we will get for free by adding various menus to our app. By default, IB gives your app some lightweight menus without the depth of functionality that is possible. For example, the stock "Edit" menu just has copy,cut,delete and paste. However, if you drag off an "Edit" menu from the IB palette, it will contain the full range of menu items and associated functionality, including the SpellChecker and a Find Menu.

First, delete some of the stock ones provided by clicking the menu item, and choosing Edit - Cut. (This may change for Rhapsody Developer Release.) Your menu should look something like this now:

9. In IB's Palette window, click the Menu icon to load the Menus. Drag over the following menus from the Palette to the sWord Menu window:

Apple
	Document (rename this to "File")
	Edit (replace the other one - this one has a Spell Checker in it!)
	Font
	Text

Click the File menu to drop it down. From the IB Palette window, click and drag from the "Item" button to the sWord File menu. Rename the new menu item to "Page Setup...". Drag over another menu item, and rename this one to "Print...". These items may be there automatically in the Rhapsody Developer Release.

10. Now we are going to design a simple object, add its outlets (technically speaking, "instance variables") and actions (Objective C methods), and have InterfaceBuilder automatically create the source files for this new class. All you will have to do is add the few lines of code reproduced below to make these custom actions do something useful.

Click the "Classes" tab in the window with the "Instances" of objects in your interface, and an outline of the class hierarchy will be displayed.

Click "NSObject". Select "Classes - Subclass" from the menu bar.

Rename "MyNSObject" to "WordDelegate".

11. We will now add an instance of this new class, WordDelegate to our app. Choose "Classes- Instantiate".

Click the "Instances" tab to reveal what we've added:

12. Now, return to the "Classes" tab to add the actions (the objC method called when you click a menu item or button) and outlets (the instances of objects that your WordDelegate knows about) to the WordDelegate Class.

13. Click the "outlet plug" icon to the right of the WordDelegate, and click "Outlets" when it appears. Hit the RETURN key to create a new outlet, "myOutlet". Double-click to select the text and rename this outlet to "theText". This outlet will become an instance variable in our new WordDelegate class, so we can refer to the NSTextView object programmatically and send messages to it, but we'll "hook it up" in InterfaceBuilder. "Hooking Up" is the visual programming equivalent of assigning both an action and a target for menu items, buttons, controls, etc.

14. Connect the WordDelegate's outlet "theText" to the NSTextView which is inside of the ScrollView by control-dragging from the WordDelegate instance. Double-click "theText" in the Inspector's Outlets Browser.

15. Now, we'll add the functionality to our WordDelegate by adding the actions to which it can respond.

a. Click the small cross icon on the right to drop down the "Actions".

b.Select Actions, and type RETURN to add a new action.

c. Rename it "newText:".

d. Again, type RETURN and rename the new action to "openText:".

e. Again, for "saveText:".

16. Now, we'll connect our menu items to the object which performs the action and select the action to be performed.

Click the File menu to drop it down. Hold down the Control-Key, and click-drag from the Open... menu to the instance of WordDelegate, and release the mouse. Black lines will connect them up. Select "openText:" in the Actions browser, and click "Connect" in the NSMenuItem Inspector (double-click openText: to avoid this second step).

Repeat this for "Save" and "New", but double-click the actions "saveText:" and "newText:" respectively.

17. In the same control-drag manner, connect the Page Setup... to "First Responder" object in the Instance Browser. This is a very cool "placeholder" object which will send the method to the most appropriate object for the current context of the application. In Rhapsody, there is this notion of a responder chain which begins with the active user interface object (such as the Text if your cursor is blinking there), the window's delegate, the window, then the Application's delegate, and finally, the Application itself. The action is sent to the first object in the chain that responds to it (ie First Responder), and if none do, the menu item is automatically dimmed and disabled. For a full description of the Responder chain, you can access the online documentation via ProjectBuilder: click "Frameworks" - "AppKit.framework" - "Documentation" - "Reference" - "Classes" - "NSResponder.rtf". You will quickly learn how useful these docs are!

For our WordDelegate object to get these First Responder method calls, we'll must insert our WordDelegate into the First Responder chain.

Control-Drag from the "My window" icon in the Instance Browser to the "WordDelegate" instance, and select "delegate" outlet in the Outlets browser of the Window Inspector.

18. Control drag from "Page Setup..." menu item in the dropped down File menu to the First Responder icon in the Instances browser. Double-click "runPageLayout:" in the Inspector's Actions browser.

Likewise, Control-drag from "Print..." menu item to the Text portion of the ScrollView. Double-click "print:" in the Actions browser.

19. Now, let's test drive our app within InterfaceBuilder by choosing "Document- Test Interface". This then "runs" our application in an interpreted environment, so you can try out typing text, changing fonts, bringing up the ruler, dragging in graphics and so on. You won't be able to save or open yet, because we need to write that code, compile it, and run the compiled version to see additional functionality over what is already part of the runtime system.

20. We've designed an object, hooked it up, now let's ask IB to make the skeletal source files. Click the "Classes" tab and select the WordDelegate Class. Choose "Classes- Create Files..." from the menu bar.

After verifying that creating classes is what you want to do, InterfaceBuilder will then ask you if you want insert these new files into your sWord project.

Click "OK", and then ProjectBuilder will come up showing you the new source files.

21. Click "WordDelegate.m" under the Classes category in ProjectBuilder. The skeletal source file will be displayed, now it's time to write those 13 lines of code, and you'll see the beauty and elegance of Rhapsody!

22. Type in this code, I added the comments for your edification, so they don't count in the number of lines of code!

 ***** WordDelegte.m *****

#import "WordDelegate.h"
@implementation WordDelegate
- (void)newText:(id)sender
{
// empty out the text with the empty NSString:
    [theText setString:@""];

// Set the window's title to be untitled:
    [[theText window]setTitle:@"Untitled"];
// bring the window up in case the user has closed it:
    [[theText window] makeKeyAndOrderFront:self];
}

- (void)openText:(id)sender
{
// Get a new Open Panel
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];

// Have it run modal and look for files of "rtf" or "rtfd" type:
    if ([openPanel runModalForTypes:[NSArray arrayWithObjects:@"rtf",@"rtfd",NULL]]) {
// we have a valid file, ask theText to read it in
        [theText readRTFDFromFile:[openPanel filename]];
// Update the window's name with the filename, but in a readable way:
        [[theText window]setTitleWithRepresentedFilename:[openPanel filename]];
// bring the window up in case the user has closed it:
        [[theText window] makeKeyAndOrderFront:self];
    }
}

- (void)saveText:(id)sender
{
// Get a new Save Panel:
    NSSavePanel *savePanel = [NSSavePanel savePanel];

// Set it to save "rtfd" files:
    [savePanel setRequiredFileType:@"rtfd"];

// Run modal, which returns YES if a valid path is chosen:
    if ([savePanel runModal]) {

// Ask the text to write itself to the chosen filename
// But don't make it back up before
// Set atomically:YES if you want "save backups", slower but more secure
        [theText writeRTFDToFile:[savePanel filename] atomically:NO];

// Update the title bar of the window
        [[theText window]setTitleWithRepresentedFilename:[savePanel filename]];
   }
}


@end
*************************

23. Click PB's Build icon, which brings up the "sWord - Project Build" panel. Click the Hammer icon again, and your app will get compiled. You can run it from within PB, or simply double-click the sWord.app in your sWord directory.

24. To build an installed version, click the "Options" panel button, and choose "Install" for the make target, then select the architectures you wish your app to run on. Build again. This will install the sWord.app into your ~/Apps directory, after "stripping" it to its smallest possible size.

25. Launch sWord.app and try it out!

Epilogue

That was easy, eh? Here are some things you can do to enhance your word processor:

  1. Rename "My Window" to "Untitled" so that the title starts in the right state. This is trivial to do in IB's Inspector, "Attributes" when the window is selected in the Instance browser.
  2. Add multiple documents to your app by creating a separate nib file which is owned by the WordDelegate class. See /NextDeveloper/Examples/AppKit/TextEdit for a very powerful, yet simple TextEditor which allows multiple docs (Document.h & Document.m).
  3. Add an Application Icon by creating a 48*48 icon (/NextDeveloper/Apps/IconBuilder.app), saving it, and then dragging it from the FileViewer to the "Project" icon well in ProjectBuilder's inspector, and recompiling.
  4. Add Find - TextFinder.h, TextFinder.m and FindPanel.nib and FindPanel.strings from TextEdit contain the functionality you need. This is much vaunted "code reuse" of object programming!
  5. Create methods for SaveAs... Again, look at the document architecture in the /NextDeveloper/Examples/Appkit folder.
  6. Add an "About..." panel. Drag in a panel from the IB palette and connect the "About..." menu item to this panel, with an action of "orderFront:".
  7. Add Tool Tips. Simply create an rtf file for each object that you want give popup help to, and attach to the user interface object in IB, Inspector- Help.
  8. Make the OpenPanel and SavePanel remember their last opened directory by making those variables static, and "retaining" them.
 - (void)saveText:(id)sender
{
// Create a static variable lives between invocations:
    static NSSavePanel *savePanel = nil;
// If it's the first time through, get a new Save Panel:
    if (savePanel == nil) {
   	 NSSavePanel *savePanel = [[NSSavePanel savePanel] retain];
    }
// Now, it will 'remember' it's last chosen directory...

Anyway, I hope this gives you a taste for the elegance and comfort of finely integrated Rhapsody development tools.


Andrew Stone, an early HyperTalk developer and coauthor of "Tricks of the HyperTalk Masters" emigrated to the NeXT community in 1989, going on to write such NeXT classics as TextArt, Create, DataPhile and 3Dreality.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »
Live, Playdate, Live! – The TouchArcade...
In this week’s episode of The TouchArcade Show we kick things off by talking about all the games I splurged on during the recent Playdate Catalog one-year anniversary sale, including the new Lucas Pope jam Mars After Midnight. We haven’t played any... | Read more »

Price Scanner via MacPrices.net

Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more
New promo at Visible: Buy a new iPhone, get $...
Switch to Visible, and buy a new iPhone, and Visible will take $10 off their monthly Visible+ service for 24 months. Visible+ is normally $45 per month. With this promotion, the cost of Visible+ is... Read more
B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for $100 off Apple’s new MSRP, only $899. Free 1-2 day delivery is available to most US addresses. Their... Read more
Take advantage of Apple’s steep discounts on...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.