home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / Classes / SampleClasses / TestClass.m < prev    next >
Text File  |  1992-08-04  |  4KB  |  142 lines

  1. // -------------------------------------------------------------------------------------
  2. // TestClass.m
  3. // -------------------------------------------------------------------------------------
  4.  
  5. #import <appkit/appkit.h>
  6. #import <libc.h>
  7. #import <stdlib.h>
  8. #import <string.h>
  9. #import "ScrollText.h"
  10. #import "ReadConfig.h"
  11. #import "TestClass.h"
  12.  
  13. // -------------------------------------------------------------------------------------
  14. @implementation TestClass
  15.  
  16. // -------------------------------------------------------------------------------------
  17. // application initialization
  18. // -------------------------------------------------------------------------------------
  19.  
  20. /* new application */
  21. + new
  22. {
  23.     objectThreadPerformInit();
  24.     return [super new];
  25. }
  26.  
  27. /* application initialization */
  28. - appDidInit:sender
  29. {
  30.     
  31.     /* make an italic font from the Text objects normal font */
  32.     fontNormal = [[myScrollText docView] font];
  33.     fontItalic = [[FontManager new] convert:fontNormal toHaveTrait:NX_ITALIC];
  34.     
  35.     /* init thread count and start ReadConfig */
  36.     threadCount = 0;
  37.     [ReadConfig readAppConfig:"testConfig.cfg" target:self];
  38.     
  39.     return self;
  40. }
  41.  
  42. // -------------------------------------------------------------------------------------
  43. // outlets
  44. // -------------------------------------------------------------------------------------
  45.  
  46. - setScrollView:anObject
  47. {
  48.     myScrollText = [ScrollText newScrollText:anObject];
  49.     [myScrollText clearScrollText];
  50.     return self;
  51. }
  52.  
  53. // -------------------------------------------------------------------------------------
  54. // thread support routines
  55. // -------------------------------------------------------------------------------------
  56.  
  57. /* secondary thread routine */
  58. - testThread:(char*)string
  59. {
  60.     int i;
  61.  
  62.     [myScrollText setTextAttributeGray:NX_DKGRAY];
  63.     textPrintf(myScrollText, "\nTest thread started...\n\n");
  64.     
  65.     [myScrollText setTextAttributeGray:NX_BLACK];
  66.     textPrintf(myScrollText, "(from cfg file) => ");
  67.     [myScrollText setTextAttributeFont:fontItalic];
  68.     textPrintf(myScrollText, "%s\n", string);
  69.     [myScrollText setTextAttributeFont:fontNormal];
  70.  
  71.     textPrintf(myScrollText, "\nI can also print in ");
  72.     [myScrollText setTextAttributeGray:NX_LTGRAY];
  73.     textPrintf(myScrollText, "Light-Gray, ");
  74.     [myScrollText setTextAttributeColor:NX_COLORRED];
  75.     textPrintf(myScrollText, "or Red, ");
  76.     [myScrollText setTextAttributeColor:NX_COLORBLUE];
  77.     textPrintf(myScrollText, "or Blue, ");
  78.     [myScrollText setTextAttributeGray:NX_BLACK];
  79.     textPrintf(myScrollText, "etc.\n\n");
  80.     
  81.     for (i = 1; i <= 5; i++) textPrintf(myScrollText, "Test Record #%d\n", i);
  82.     textPrintf(myScrollText, "\n");
  83.     
  84.     [myScrollText setTextAttributeGray:NX_DKGRAY];
  85.     textPrintf(myScrollText, "Test thread terminating...\n");
  86.     
  87.     free(string);
  88.     
  89.     [self mainThreadPerform:@selector(threadIsDone) wait:YES];
  90.     
  91.     return self;
  92. }
  93.  
  94. /* main thread routine */
  95. - threadIsDone
  96. {
  97.  
  98.     /* decrement thread count */
  99.     threadCount--;
  100.     
  101.     /* this just makes sure everying has been displayed */
  102.     NXPing();
  103.     
  104.     return self;
  105. }
  106.  
  107. // -------------------------------------------------------------------------------------
  108. // config file routines
  109. // Note: The two string print methods below are for example purposes only.  It is
  110. // generally not a good idea to mix printing from two separate threads into one Text
  111. // object because may be difficult to control the order in which the lines are printed.
  112. // -------------------------------------------------------------------------------------
  113.  
  114. /* print string from main thread */
  115. - (int)printString:(char*)string
  116. {
  117.     [myScrollText setTextAttributeFont:fontItalic];
  118.     textPrintf(myScrollText, "main thread ", string);
  119.     [myScrollText setTextAttributeFont:fontNormal];
  120.     textPrintf(myScrollText, "=> %s\n", string);
  121.     return NO;
  122. }
  123.  
  124. /* print string from secondary thread */
  125. - (int)printFromThread:(char*)string
  126. {
  127.  
  128.     /* example to return an error to ReadConfig */
  129.     if (threadCount) {  // only one thread at a time
  130.         NXLogError("TestClass: A thread is already running...");
  131.         return YES;
  132.     }
  133.     
  134.     /* count and start thread */ 
  135.     threadCount++;
  136.     [self forkPerform:@selector(testThread:) with:(id)NXCopyStringBuffer(string) detach:YES];
  137.     
  138.     return NO;
  139. }
  140.  
  141. @end
  142.