Changeset 8503 for trunk/source/examples


Ignore:
Timestamp:
Feb 17, 2008, 3:25:49 AM (12 years ago)
Author:
mikel
Message:

created nib-loading fnction

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

Legend:

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

    r8500 r8503  
    211211        created when the nibfile is loaded. To collect these, you
    212212        can pass an <code>NSMutableArray</code> object under the
    213         key <code>"NSNibTopLevelObjects"</code>.</p>
     213        key <code>NSNibTopLevelObjects</code>.</p>
    214214
    215215      <p>For this first example, we'll pass an owner object (the
    216216        application object), but we don't need to collect toplevel
    217217        objects, so we'll omit
    218         the <code>"NSNibTopLevelObjects"</code> key.</p>
     218        the <code>NSNibTopLevelObjects</code> key.</p>
    219219
    220220      <pre>
     
    282282        needed.</p>
    283283
    284       <p>The nib-loading function can simply parameterize the file to
    285       be loaded, and then perform the sequence of steps covered in the
     284      <p>The nib-loading function can take the file to be loaded as a
     285      parameter, and then perform the sequence of steps covered in the
    286286      previous section. If we just literally do that, the result will
    287287      look something like this:</p>
     
    336336      can get access to them from our code? We'll need another version
    337337      of our function.</p>
     338
     339      <p>In order to collect toplevel objects, we'll want to pass an
     340      NSMutableArray object in the dictionary, stored under the key
     341      <code>NSNibTopLevelObjects</code>. So we first need to create such an
     342      array object in the <code>let</code> form:</p>
     343
     344      <pre>
     345(let* (...
     346       (objects-array (#/arrayWithCapacity: (@class ns-mutable-array) 16))
     347       ...)
     348  ...)
     349      </pre>
     350
     351      <p>Now that we have the array in which to store the nibfile's
     352      toplevel objects, we need to change the code that creates the
     353      dictionary, so that it contains not only the owner object, but
     354      also the array we just created:</p>
     355
     356      <pre>
     357  (let* (...
     358         (dict (#/dictionaryWithObjectsAndKeys: (@class ns-mutable-dictionary)
     359                    app #@"NSNibOwner"
     360                    objects-array #&amp;NSToplevelObjects))
     361         ...)
     362    ...)
     363 
     364      </pre>
     365
     366      <p>We'll want to release the <code>NSMutableArray</code>
     367      object before returning, but first we need to collect the
     368      objects in it. We'll do that by making a local variable to
     369      store them, then iterating over the array object to get them all.</p>
     370
     371      <pre>
     372  (let* (...
     373         (toplevel-objects (list))
     374         ...)
     375    (dotimes (i (#/count objects-array))
     376      (setf toplevel-objects
     377            (cons (#/objectAtIndex: objects-array i)
     378                  toplevel-objects)))
     379    ...)
     380      </pre>
     381
     382      <p>After collecting the objects, we can release the array, then
     383      return the list of objects. It's still possible we might want
     384      to know whether the load succeeded, so we
     385      use <code>values</code> to return both the toplevel objects and
     386      the success or failure of the load.</p>
     387
     388      <p>The final version of the nib-loading code looks like
     389      this:</p>
     390
     391      <pre>
     392(defun load-nibfile (nib-path)
     393  (let* ((app-class-name (%make-nsstring "NSApplication"))
     394         (app-class (#_NSClassFromString app-class-name))
     395         (app (#/sharedApplication app-class))
     396         (app-zone (#/zone app))
     397         (nib-name (%make-nsstring (namestring nib-path)))
     398         (objects-array (#/arrayWithCapacity: (@class ns-mutable-array) 16))
     399         (dict (#/dictionaryWithObjectsAndKeys: (@class ns-mutable-dictionary)
     400                    app #@"NSNibOwner"
     401                    objects-array #&amp;NSNibToplevelObjects))
     402         (toplevel-objects (list))
     403         (result (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
     404                                                            nib-name
     405                                                            dict
     406                                                            app-zone)))
     407    (dotimes (i (#/count objects-array))
     408      (setf toplevel-objects
     409            (cons (#/objectAtIndex: objects-array i)
     410                  toplevel-objects)))
     411    (#/release app-class-name)
     412    (#/release nib-name)
     413    (#/release dict)
     414    (#/release objects-array)
     415    (values toplevel-objects result)))
     416      </pre>
     417
     418      <p>Now we can call this function with some suitable nibfile,
     419      such as simple "hello.nib" that comes with this HOWTO:</p>
     420
     421      <pre>
     422? (ccl::load-nibfile "hello.nib")
     423(#&lt;LISP-APPLICATION &lt;LispApplication: 0x1b8da0&gt; (#x1B8DA0)&gt;
     424 #&lt;NS-WINDOW &lt;NSWindow: 0x171344d0&gt; (#x171344D0)&gt;)
     425T
     426
     427      </pre>
     428
     429      <p>The "Hello!" window appears on the screen, and two values are
     430      returned. The first value is the list of toplevel objects that
     431      were loaded. The second value, <code>T</code> indicates that the
     432      nibfile was loaded successfully.</p>
    338433
    339434      <div class="section-head">
  • trunk/source/examples/cocoa/nib-loading/nib-loading.lisp

    r8500 r8503  
    1414(defun load-nibfile (nib-path)
    1515  (let* ((app-class-name (%make-nsstring "NSApplication"))
    16          (app-class (#_NSClassFromString class-name))
    17          (app (#/sharedApplication appclass))
     16         (app-class (#_NSClassFromString app-class-name))
     17         (app (#/sharedApplication app-class))
    1818         (app-zone (#/zone app))
    1919         (nib-name (%make-nsstring (namestring nib-path)))
    20          (dict (#/dictionaryWithObject:forKey:
    21                 (@class ns-mutable-dictionary) app #@"NSNibOwner"))
     20         (objects-array (#/arrayWithCapacity: (@class ns-mutable-array) 16))
     21         (dict (#/dictionaryWithObjectsAndKeys: (@class ns-mutable-dictionary)
     22                    app #@"NSNibOwner"
     23                    objects-array #&NSNibTopLevelObjects))
     24         (toplevel-objects (list))
    2225         (result (#/loadNibFile:externalNameTable:withZone: (@class ns-bundle)
    2326                                                            nib-name
    2427                                                            dict
    2528                                                            app-zone)))
     29    (dotimes (i (#/count objects-array))
     30      (setf toplevel-objects
     31            (cons (#/objectAtIndex: objects-array i)
     32                  toplevel-objects)))
    2633    (#/release app-class-name)
    2734    (#/release nib-name)
    2835    (#/release dict)
    29     result))
     36    (#/release objects-array)
     37    (values toplevel-objects result)))
     38
    3039#|
    3140(ccl::load-nibfile "/usr/local/openmcl/trunk/source/examples/cocoa/nib-loading/hello.nib")
    3241|#
     42
Note: See TracChangeset for help on using the changeset viewer.