home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / MiniExamples / ConvertXYtoChar / Controller.m < prev    next >
Encoding:
Text File  |  1991-10-04  |  3.7 KB  |  123 lines

  1. /* Controller.m
  2.  * Purpose:  A subclass of Object and the main controlling class of
  3.  *    the application.  Controller is the application delegate.
  4.  *
  5.  * You may freely copy, distribute, and reuse the code in this example.
  6.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  7.  * fitness for any particular use.
  8.  *
  9.  * Written by: Sharon Zakhour
  10.  * Created: 22/July/91
  11.  *
  12.  */
  13.  
  14. #import <stdio.h>
  15. #import <appkit/Text.h>
  16. #import <appkit/TextField.h>
  17. #import <appkit/Matrix.h>
  18. #import <appkit/ScrollView.h>
  19. #import "Controller.h"
  20. #import "TurboText.h"
  21.  
  22. @implementation Controller
  23.  
  24. /* Since we have subclassed the Text object but we have used */
  25. /* The ScrollView from IB we need to replace the Text object in  */
  26. /* the IB-generated ScrollView with our own subclass.  At the */
  27. /* same time we want to preserve the text that was inserted into */
  28. /* the view.  The replaceText method performs this swap.  This */
  29. /* is also covered in the NeXTanswer  appkit.555. */
  30. - appDidInit:sender
  31. {
  32.     /* Replace the Text object in the ScrollView with my */
  33.     /* own TurboText object */
  34.     [self replaceText];
  35.     
  36.     /* Now that we're ready, bring the window up */
  37.     /* In IB, we've instructed the window NOT to become */
  38.     /* visible at launch time so that we can make this swap */
  39.     /* invisibly. */
  40.     [[myScrollView window] makeKeyAndOrderFront: nil];
  41.     return self;
  42. }
  43.  
  44. /* This method is called when the user selects the ConvertTo */
  45. /* button in the window.  The x,y coordinates are extracted from */
  46. /* two form fields and the result is placed into yet-another */
  47. /* form field.  At the same time, the character position is echoed */
  48. /* in the ScrollView with an insertion cursor at the new position. */
  49. - convert:sender
  50. {
  51.     NXPoint    myPoint;
  52.     int    charPos;
  53.     NXSelPt    start, end;
  54.     
  55.     myPoint.x = [[charCoordinate cellAt: 0:0] floatValue];
  56.     myPoint.y = [[charCoordinate cellAt: 1:0] floatValue];
  57.     
  58.     charPos = [[myScrollView docView] convertPoint: &myPoint];
  59.     [[charPosition cellAt: 0: 0] setFloatValue: (float)charPos];
  60.     
  61.     /* Set the selection at the character position so that */
  62.     /* we can see it.  This will also make the ScrollView the */
  63.     /* first responder which is necessary to see the blinking */
  64.     /* insertion point. */
  65.     [[myScrollView docView] setSel: charPos :charPos];
  66.     
  67.     /* Now let's get the selection point back.  The actual x,y */
  68.     /* coordinates have been adjusted by the Text object */
  69.     /* and we want to update our textfields */
  70.     [[myScrollView docView] getSel: &start :&end];
  71.     [[charPosition cellAt: 1:0] setFloatValue: start.x];
  72.     [[charPosition cellAt: 2:0] setFloatValue: start.y];
  73.     return self;
  74. }
  75.  
  76. /* Swaps a standard Text object from IB with my own.  See */
  77. /* NeXTanswer appkit.555 for more information. */
  78. - replaceText
  79. {
  80.     NXStream *s = (NXStream *)nil;
  81.     id        stdDoc, newDoc;
  82.     NXRect    r;
  83.         
  84.     /* Measure the old doc view, then trash it. */
  85.     stdDoc = [myScrollView docView];
  86.     [stdDoc getFrame:&r];
  87.  
  88.     /* Get the entire text, using the rich RTF format */
  89.     s = NXOpenMemory(NULL, 0, NX_READWRITE);
  90.     if (s)   [stdDoc writeRichText: s];
  91.  
  92.     [myScrollView setVertScrollerRequired:YES];
  93.     [myScrollView setHorizScrollerRequired:NO];
  94.     [myScrollView setDynamicScrolling:YES]; 
  95.     
  96.     newDoc = [[TurboText alloc] initFrame:&r];
  97.     [newDoc moveTo:0.0:0.0];
  98.     [newDoc notifyAncestorWhenFrameChanged: YES];
  99.     [newDoc setVertResizable:YES];
  100.     [newDoc setSelectable:YES];
  101.     [newDoc setEditable: YES];
  102.     [newDoc setAutosizing:NX_WIDTHSIZABLE];
  103.     [newDoc setMinSize:&r.size];
  104.     [newDoc setMonoFont:NO];
  105.     r.size.height = 1.0e30;
  106.     [newDoc setMaxSize:&r.size];
  107.     [myScrollView setDocView:newDoc];
  108.     [stdDoc free];
  109.     
  110.     /* Stick the text from the original doc into the new doc */
  111.     if (s)
  112.     {
  113.         /* Rewind to the beginning of the stream */
  114.         NXSeek(s, 0L, NX_FROMSTART);
  115.         [newDoc readRichText:s];
  116.         NXCloseMemory(s, NX_FREEBUFFER);
  117.     }
  118.  
  119.     return self;
  120. }
  121.  
  122. @end
  123.