Changeset 8479
- Timestamp:
- Feb 13, 2008, 10:22:52 AM (17 years ago)
- Location:
- trunk/source/examples/cocoa/nib-loading
- Files:
-
- 2 edited
-
HOWTO.html (modified) (9 diffs)
-
nib-loading.lisp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/examples/cocoa/nib-loading/HOWTO.html
r8450 r8479 38 38 elements.</p> 39 39 40 <p>InterfaceBuilder is an appli action that ships with Apple's40 <p>InterfaceBuilder is an application that ships with Apple's 41 41 Developer Tools. The Developer Tools are an optional install 42 42 that comes with Mac OS X. Before you can use this HOWTO, you'll … … 114 114 115 115 <p>The pathname is just a reference to the nibfile we want to 116 load. The dictionary holds references to objects —the117 object that owns the nibfile (in this case, the running118 NSApplication object), and an array used to hold any toplevel119 objects in the nibfile. The zone is areference to the area of120 memory wherethe nibfile objects will be allocated.</p>116 load. The dictionary holds references to objects. In this 117 first simple example, we'll use it only to identify the 118 nibfile's owner, which in this case is the application 119 itself. The zone is a reference to the area of memory where 120 the nibfile objects will be allocated.</p> 121 121 122 122 <p>Don't worry if none of this makes sense to you; the code to … … 128 128 </div> 129 129 130 <p>First, we'll get the zone from the running application. We'll131 t ell Cocoa to allocate the nibfile objects in the same zone that132 the application uses, so getting a zone is a simple matter of133 a sking the application for the one it's using.</p>130 <p>First, we'll get a memory zone. We'll tell Cocoa to allocate 131 the nibfile objects in the same zone that the application 132 uses, so getting a zone is a simple matter of asking the 133 application for the one it's using.</p> 134 134 135 135 <p>Before we can ask the application anything, we need a … … 148 148 149 149 <pre> 150 ? (setf *my-app-class* (#_NSClassFromString (%make-nsstring "NSApplication"))) 151 #<OBJC:OBJC-CLASS NS:NS-APPLICATION (#x7FFF704C5C00)> 150 ? (setf *my-app* 151 (let* ((class-name (%make-nsstring "NSApplication")) 152 (appclass (#_NSClassFromString class-name))) 153 (#/release class-name) 154 (#/sharedApplication appclass))) 155 #<LISP-APPLICATION <LispApplication: 0x1b8de0> (#x1B8DE0)> 152 156 </pre> 153 157 154 <p>Notice, by the way, that this form allocates an NSString 155 object. We do it this way for the sake of simplicity, but it's 156 not an example of good programming practice. NSStrings are 157 foreign objects, allocated by the Objective-C runtime. They 158 are not garbage-collected by Lisp, and so when you create them 159 they hang around in memory until you manually deallocate them, 160 or until you quit from Clozure CL.</p> 161 162 <p>This simple example goes on to create several foreign objects 163 by evaluating forms in the Listener, storing some of them in 164 global variables. In this example, these objects are never 165 deallocated. It's not a problem in such a small example; we 166 just create a handful of objects at the Listener, and they are 167 disposed of when we quit Clozure CL. But when writing real 168 applications using the Objective-C bridge, you will need to 169 learn to use Cocoa's memory-management discipline so that you 170 can ensure that foreign objects are allocated and deallocated 171 properly.</p> 172 173 <p>Now that we have the application class, we can ask it for a 174 reference to the running application:</p> 175 176 <pre> 177 ? (setf *my-app* (#/sharedApplication *my-app-class*)) 178 #<LISP-APPLICATION <LispApplication: 0x1b8e20> (#x1B8E20)> 179 </pre> 158 <p>Let's review this form step-by-step.</p> 159 160 <p>First of all, it's going to store the returned application 161 object in the variable <code>*my-app*</code>, so that we have it 162 convenient for later use.</p> 163 164 <p>We need an <code>NSString</code> object that contains the 165 name of the application class, so the code allocates one by 166 calling <code>%make-nsstring</code>. The <code>NSString</code> 167 object is a dynamically-allocated foreign object, not managed by 168 Lisp's garbage-collector, so we'll have to be sure to release it 169 later.</p> 170 171 <p>The code next uses the class-name to get the 172 actual <code>NSApplication</code> class object, by 173 calling <code>#_NSClassFromString</code>.</p> 174 175 <p>Finally, after first releasing the <code>NSString</code> 176 object, it calls <code>#/sharedApplication</code> to get the 177 running application object, which turns out to be an instance 178 of <code>LispApplication</code>.</p> 180 179 181 180 <p>Voilà! We have a reference to the running Clozure CL … … 188 187 </pre> 189 188 190 <p>Now we have a reference to the application's zone. We can 191 pass it 192 to <code>loadNibFile:externalNameTable:withZone:</code> to 193 tell it to allocate the nibfile's objects in the 194 application's zone.</p> 189 <p>Now we have a reference to the application's zone, which is 190 one of the parameters we need to pass 191 to <code>loadNibFile:externalNameTable:withZone:</code>.</p> 195 192 196 193 <div class="section-head"> … … 199 196 200 197 <p>The dictionary argument 201 to <code>loadNibFile:externalNameTable:withZone:</code> is used 202 for two purposes. First, we use it to pass an owner object to 203 the method. Some Cocoa objects need to have references to owner 204 objects. For example, a window might need to check with an owner 205 object to determine whether its fields and buttons should be 206 enabled. You supply an owner object in the dictionary, under the 198 to <code>loadNibFile:externalNameTable:withZone:</code> is 199 used for two purposes: to identify the nibfile's owner, and 200 to collect toplevel objects.</p> 201 202 <p>The nibfile's owner becomes the owner of all the toplevel 203 objects created when the nibfile is loaded, objects such as 204 windows, buttons, and so on. A nibfile's owner manages the 205 objects created when the nibfile is loaded, and provides a 206 way for your code to get references to those objects. You 207 supply an owner object in the dictionary, under the 207 208 key <code>"NSNibOwner"</code>.</p> 208 209 209 <p>The second purpose of the dictionary object is to collect 210 references to any toplevel objects (such as buttons, text 211 fields, and so on) that the runtime creates when loading the 212 nibfile. To collect these, you pass an NSMutableArray object 213 under the key <code>"NSNibTopLevelObjects"</code>.</p> 210 <p>The toplevel objects are objects, such as windows, that are 211 created when the nibfile is loaded. To collect these, you 212 can pass an <code>NSMutableArray</code> object under the 213 key <code>"NSNibTopLevelObjects"</code>.</p> 214 214 215 215 <p>For this first example, we'll pass an owner object (the … … 219 219 220 220 <pre> 221 ? (setf *my-dict* (#/dictionaryWithObject:forKey: (@class ns-mutable-dictionary) *my-app* #@"NSNibOwner")) 221 ? (setf *my-dict* 222 (#/dictionaryWithObject:forKey: (@class ns-mutable-dictionary) 223 *my-app* 224 #@"NSNibOwner")) 222 225 #<NS-MUTABLE-DICTIONARY { 223 226 NSNibOwner = <LispApplication: 0x1b8e10>; … … 235 238 236 239 <pre> 237 ? (setf *nib-path* (%make-nsstring (namestring "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib"))) 238 #<NS-MUTABLE-STRING "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib" (#x13902C10)> 240 ? (setf *nib-path* 241 (%make-nsstring 242 (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib"))) 243 #<NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)> 239 244 </pre> 240 245 … … 243 248 244 249 <pre> 245 ? (setf *nib-path* (%make-nsstring (namestring "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib"))) 246 #<NS-MUTABLE-STRING "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib" (#x13902C10)> 247 ? (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle) *nib-path* *my-dict* *my-zone*) 250 ? (#/loadNibFile:externalNameTable:withZone: 251 (@class ns-bundle) 252 *nib-path* 253 *my-dict* 254 *my-zone*) 248 255 T 249 256 </pre> -
trunk/source/examples/cocoa/nib-loading/nib-loading.lisp
r8450 r8479 25 25 (values load-succeeded-p context))) 26 26 27 (setf *my-app* 28 (let* ((class-name (%make-nsstring "NSApplication")) 29 (appclass (#_NSClassFromString class-name))) 30 (#/release class-name) 31 (#/sharedApplication appclass))) 32 27 33 28 34 #|
Note:
See TracChangeset
for help on using the changeset viewer.
