Changeset 8479


Ignore:
Timestamp:
Feb 13, 2008, 6:22:52 PM (12 years ago)
Author:
mikel
Message:

edits to the nob-loading HOWTO

Location:
trunk/source/examples/cocoa/nib-loading
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/examples/cocoa/nib-loading/HOWTO.html

    r8450 r8479  
    3838        elements.</p>
    3939     
    40       <p>InterfaceBuilder is an appliaction that ships with Apple's
     40      <p>InterfaceBuilder is an application that ships with Apple's
    4141        Developer Tools. The Developer Tools are an optional install
    4242        that comes with Mac OS X. Before you can use this HOWTO, you'll
     
    114114
    115115      <p>The pathname is just a reference to the nibfile we want to
    116         load. The dictionary holds references to objects&mdash;the
    117         object that owns the nibfile (in this case, the running
    118         NSApplication object), and an array used to hold any toplevel
    119         objects in the nibfile. The zone is areference to the area of
    120         memory where the 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>
    121121
    122122      <p>Don't worry if none of this makes sense to you; the code to
     
    128128      </div>
    129129
    130       <p>First, we'll get the zone from the running application. We'll
    131         tell Cocoa to allocate the nibfile objects in the same zone that
    132         the application uses, so getting a zone is a simple matter of
    133         asking 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>
    134134
    135135      <p>Before we can ask the application anything, we need a
     
    148148
    149149      <pre>
    150         ? (setf *my-app-class* (#_NSClassFromString (%make-nsstring "NSApplication")))
    151         #&lt;OBJC:OBJC-CLASS NS:NS-APPLICATION (#x7FFF704C5C00)&gt;
     150        ? (setf  *my-app*
     151                 (let* ((class-name (%make-nsstring "NSApplication"))
     152                        (appclass (#_NSClassFromString class-name)))
     153                   (#/release class-name)
     154                   (#/sharedApplication appclass)))
     155        #&lt;LISP-APPLICATION &lt;LispApplication: 0x1b8de0&gt; (#x1B8DE0)&gt;
    152156      </pre>
    153157
    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         #&lt;LISP-APPLICATION <LispApplication: 0x1b8e20> (#x1B8E20)&gt;
    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>
    180179
    181180        <p>Voilà! We have a reference to the running Clozure CL
     
    188187        </pre>
    189188
    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>
    195192
    196193        <div class="section-head">
     
    199196
    200197        <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
    207208          key <code>"NSNibOwner"</code>.</p>
    208209
    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>
    214214
    215215        <p>For this first example, we'll pass an owner object (the
     
    219219
    220220        <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"))
    222225          #&lt;NS-MUTABLE-DICTIONARY {
    223226                                  NSNibOwner = &lt;LispApplication: 0x1b8e10&gt;;
     
    235238
    236239        <pre>
    237           ? (setf *nib-path* (%make-nsstring (namestring "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib")))
    238           #&lt;NS-MUTABLE-STRING "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib" (#x13902C10)&gt;
     240          ? (setf *nib-path*
     241                  (%make-nsstring
     242                     (namestring "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib")))
     243          #&lt;NS-MUTABLE-STRING "/usr/local/openmcl/ccl/examples/cocoa/nib-loading/hello.nib" (#x13902C10)&gt;
    239244        </pre>
    240245
     
    243248
    244249        <pre>
    245           ? (setf *nib-path* (%make-nsstring (namestring "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib")))
    246           #&lt;NS-MUTABLE-STRING "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib" (#x13902C10)&gt;
    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*)
    248255          T
    249256        </pre>
  • trunk/source/examples/cocoa/nib-loading/nib-loading.lisp

    r8450 r8479  
    2525    (values load-succeeded-p context)))
    2626
     27(setf  *my-app*
     28       (let* ((class-name (%make-nsstring "NSApplication"))
     29              (appclass (#_NSClassFromString class-name)))
     30         (#/release class-name)
     31         (#/sharedApplication appclass)))
     32
    2733
    2834#|
Note: See TracChangeset for help on using the changeset viewer.