home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Utilities / Fiend-1.4.1-src / ThinkMore.m < prev    next >
Encoding:
Text File  |  1994-09-20  |  10.0 KB  |  454 lines

  1. //  ThinkMore.m
  2. //
  3. //  Contains additional methods used in the Thinker class.
  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. #import "Thinker.h"
  10. #import "SpaceView.h"
  11. #import "BackView.h"
  12. #import "SleepView.h"
  13. #import "BlackView.h"
  14. #import "Password.h"
  15. #import "Controller.h"
  16. #import "psfuncts.h"
  17.  
  18.  
  19. #import <appkit/appkit.h>
  20. #import <objc/NXBundle.h>
  21.  
  22.  
  23. #define VIEWDIRECTORY    "/LocalLibrary/BackSpaceViews"
  24.  
  25. static char *compiledViewNames[] = {
  26.     "Space",
  27.     "Boink",
  28.     "Black",
  29.     };
  30.  
  31. #define COMVIEWCOUNT    (sizeof(compiledViewNames)/sizeof(*compiledViewNames))
  32.  
  33. @implementation Thinker(thinkMore)
  34.  
  35.  
  36. //must invoke this before creating window or setting up the views
  37. - getViewType
  38. {
  39.     int i;
  40.     id theMatrix;
  41.     char buf[MAXPATHLEN];
  42.     ModuleInfo *m;
  43.  
  44.     strcpy( buf, NXHomeDirectory());
  45.     strcat( buf, "/Library/BackSpaceViews");
  46.  
  47.     moduleList = [[ModuleList alloc] init];
  48.  
  49.     [self loadViewsFrom:buf];
  50.     [[NXApp delegate] setProgressViewRatio:0.2];
  51.     [self loadViewsFrom: [self appDirectory]];
  52.     [[NXApp delegate] setProgressViewRatio:0.4];
  53.     [self loadViewsFrom: VIEWDIRECTORY];
  54.     [[NXApp delegate] setProgressViewRatio:0.6];
  55.  
  56.     for (i = 0; i < COMVIEWCOUNT; i++)
  57.     {
  58.         m = [[ModuleInfo alloc] 
  59.             initWithView:nil name:compiledViewNames[i] path:NULL];
  60.         [moduleList addObject: m];
  61.     }
  62.     [[NXApp delegate] setProgressViewRatio:0.8];
  63.  
  64.     [moduleList sort];
  65.     [viewSelectionBrowser loadColumnZero];
  66.     theMatrix = [viewSelectionBrowser matrixInColumn:0];
  67.     [theMatrix selectCellAt:realViewIndex :0];
  68.     [theMatrix scrollCellToVisible:realViewIndex :0];
  69.     [[NXApp delegate] setProgressViewRatio:0.9];
  70.  
  71.     return self;
  72. }
  73.  
  74.  
  75. - selectRealViewIndex:sender
  76. {
  77.     //sender is the NXBrowser
  78.     int index = [[viewSelectionBrowser matrixInColumn:0] selectedRow];
  79.  
  80.     if (index == realViewIndex) return self;
  81.     if (index == -1)
  82.     {
  83.         id theMatrix = [viewSelectionBrowser matrixInColumn:0];
  84.         [theMatrix selectCellAt:realViewIndex :0];
  85.         [theMatrix scrollCellToVisible:realViewIndex :0];
  86.         return self;
  87.     }
  88.     
  89.     realViewIndex = index;
  90.     [self setVirtualViewIndexAndIncrement:NO];
  91.     
  92.     return self;
  93. }
  94.  
  95.  
  96. // this method is the actual view setting mechanism,
  97. // guaranteed to get called to set the view
  98.  
  99. - setVirtualViewIndexAndIncrement:(BOOL)flag
  100. {
  101.     id myView;
  102.     id newInspector;
  103.     
  104.     if (realViewIndex)
  105.     {
  106.         virtualViewIndex = realViewIndex-1;
  107.         [self selectScreenSaverViews];
  108.     }
  109.  
  110.     else
  111.     {
  112.         if (flag)
  113.         {
  114.             virtualViewIndex = random() % [moduleList count];
  115.             [self selectScreenSaverViews];
  116.             myView = [self backView];
  117.             
  118.             while ([myView respondsTo:@selector(isBoringScreenSaver)]
  119.                 && [myView isBoringScreenSaver])
  120.             {
  121.                 if (++virtualViewIndex >= [moduleList count])
  122.                     virtualViewIndex = 0;
  123.  
  124.                 [self selectScreenSaverViews];
  125.                 myView = [self backView];
  126.             }
  127.         }
  128.         else [self selectScreenSaverViews];
  129.     }
  130.  
  131.  
  132.     //---------------------------------------------
  133.     // now plug in the inspector
  134.     //---------------------------------------------
  135.     myView = [self backView];
  136.  
  137.     if ([myView respondsTo:@selector(inspector:)])
  138.     {
  139.         newInspector = [myView inspector:self];
  140.         if (!newInspector) newInspector = [self nullInspector];
  141.     }
  142.     else newInspector = [self nullInspector];
  143.     
  144.     if (newInspector != currentInspector)
  145.     {
  146.         if ([oldInspectorOwner respondsTo:@selector(inspectorWillBeRemoved)])
  147.             [oldInspectorOwner inspectorWillBeRemoved];
  148.         // either myView will be the inspector owner, or it won't care
  149.         oldInspectorOwner = myView;
  150.         
  151.         // don't want it to resize the box.  Suboptimal technique...
  152.         [newInspector setFrame:&inspectorFrame];
  153.  
  154.         [invisibleInspectorBox setContentView: newInspector];
  155.         currentInspector = newInspector;
  156.  
  157.         if ([myView respondsTo:@selector(inspectorInstalled)])
  158.             [myView inspectorInstalled];
  159.  
  160.         [invisibleInspectorBox display];
  161.     }
  162.  
  163.     return self;
  164. }
  165.  
  166.  
  167. - selectScreenSaverViews
  168. {
  169.     id theView, bigWindow;
  170.     int myBacking;
  171.  
  172.     //need to order out big window if that's the type and buffering changed
  173.     
  174.     theView = [self backView];
  175.  
  176.     myBacking = [self backingTypeForView:theView];
  177.  
  178.     [self createBigWindowIfNecessaryForView:theView];
  179.  
  180.     if (myBacking == NX_BUFFERED) bigWindow = bigBufferedWindow;
  181.     else bigWindow = bigUnbufferedWindow;
  182.  
  183.     if (windowType == BACKWINDOW)
  184.     {
  185.         if (spaceWindow != bigWindow)
  186.         {
  187.             [spaceWindow orderOut:self];
  188.             [self useBackWindow:globalTier];
  189.         }
  190.     }
  191.  
  192.     spaceView = theView;
  193.     [self installSpaceViewIntoWindow:spaceWindow];
  194.  
  195.     if ([spaceView respondsTo:@selector(setImage:)])
  196.         [spaceView setImage: image];
  197.     if ([spaceView respondsTo:@selector(newWindow)]) [spaceView newWindow];
  198.  
  199.     [self setWindowTitle];
  200.  
  201.     NXWriteDefault("BackSpace", "viewType", (realViewIndex ? 
  202.             ([moduleList nameAt: realViewIndex-1]) : "All"));
  203.  
  204.  
  205.     if (windowType) 
  206.     {
  207.         // the unbuffered window looks better if you just display
  208.         // its contents, but for a buffered oneshot window, you must
  209.         // display the window to make sure the window server window exists.
  210.  
  211.         if (myBacking == NX_BUFFERED) 
  212.             [spaceWindow display];
  213.         else 
  214.         {
  215.             [spaceView fillBoundsWithBlack];
  216.             [spaceView display];
  217.         }
  218.     }
  219.  
  220.     if (normalWindow && (windowType == NORMALWINDOW))
  221.     {
  222.         if (myBacking == NX_BUFFERED)
  223.             [normalWindow setBackingType:NX_BUFFERED];
  224.         else[normalWindow setBackingType:NX_RETAINED];
  225.     }
  226.  
  227.     return self;
  228. }
  229.  
  230. - setWindowTitle
  231. {
  232.     if ([spaceView respondsTo:@selector(windowTitle)])
  233.     {
  234.         [normalWindow setTitle: NXLocalString([spaceView windowTitle],0,0)];
  235.     }
  236.     else [normalWindow setTitle: NXLocalString("BackSpace",0,0)];
  237.     return self;
  238. }
  239.  
  240.  
  241. - getScreenLockerSetting
  242. {
  243.     const char *ptr;
  244.     
  245.     [screenLocker setState:0];
  246.     
  247.     ptr = NXGetDefaultValue("BackSpace", "screenLocker");
  248.  
  249.     if (!ptr || !strcmp(ptr,"Off")) [self setScreenLocker:NO andRemember:NO];
  250.     else [self setScreenLocker:YES andRemember:NO];
  251.  
  252.     return self;
  253. }
  254.  
  255. - changeScreenLockerSetting:sender
  256. {
  257.     if (![password checkPassword:
  258.         NXLocalString("Enter password to change screen lock setting:",0,0)
  259.         randomPos:NO checkLock:NO withView:nil])
  260.     {
  261.         [screenLocker setState:[password isLocked]];
  262.         return self;
  263.     }
  264.  
  265.     if (![password validPassword] && ![password setPassword:self])
  266.     {
  267.         [screenLocker setState:[password isLocked]];
  268.         return self;
  269.     }
  270.     
  271.     [self setScreenLocker:([screenLocker state])andRemember:YES];
  272.     return self;
  273. }
  274.  
  275. - setScreenLocker:(BOOL)val andRemember:(BOOL)rem
  276. {
  277.     [screenLocker setState:val];
  278.     [password setLock: val];
  279.  
  280.     if (rem)
  281.     {    
  282.         if (val) NXWriteDefault("BackSpace", "screenLocker", "On");
  283.         else NXRemoveDefault("BackSpace", "screenLocker");
  284.     }
  285.     
  286.     return self;
  287. }
  288.  
  289.  
  290.  
  291.  
  292. //---------------------------------------------------
  293. //            View manager routines    
  294. //---------------------------------------------------
  295.  
  296. - backView
  297. {
  298.     NXRect aFrame = {{0,0},{200,200}};
  299.     id theView;
  300.     
  301.     if (![moduleList viewAt:virtualViewIndex])
  302.     {
  303.         char path[MAXPATHLEN];
  304.         id myClass;
  305.         const char *name;
  306.         char *filenames[] = {path, NULL};
  307.         ModuleInfo *mp;
  308.         struct mach_header *header;
  309.  
  310.         mp = [moduleList objectAt: virtualViewIndex];
  311.         name = [mp viewName];
  312.  
  313.         // before I loaded all classes at launch time; now classes are
  314.         // loaded only as needed.  This idea and some of the code here is
  315.         // from bill bumgarner, thanx!
  316.  
  317.         if ([mp path])    // we have path but no instance, must load class
  318.         {
  319.             long ret;
  320.             do
  321.             {
  322.                 sprintf(path, "%s/%sView.BackO", [mp path], name);
  323.                 ret = objc_loadModules(filenames, NULL, NULL, &header, NULL);
  324.  
  325.                 // objc_loadModules succeeds with a warning if the architecture of the
  326.                 // object file is wrong, so we better check if we really got a class
  327.  
  328.                 if (!ret)    // load succeeded or was wrong architecture
  329.                 {
  330.                     sprintf(path,"%sView", name);
  331.                     myClass = objc_getClass(path);
  332.                     if (!myClass) ret = -1;
  333.                 }
  334.  
  335.             } while (ret && [mp useNextPath]);
  336.  
  337.             [mp discardAltPaths];
  338.  
  339.             if (ret)
  340.             {
  341.                 // Ugh, failed.  Will instantiate a BlackView instead...
  342.                 NXRunAlertPanel([NXApp appName], NXLocalString(
  343.                     "Could not dynamically load class: %sView",0,0),
  344.                     NULL, NULL, NULL, name);
  345.                 name = "Black";
  346.             }
  347.             else
  348.             {
  349.                 [mp setHeader:header];
  350.             }
  351.         }
  352.  
  353.         //at this point we must have a valid name for a loaded class
  354.         
  355.         sprintf(path,"%sView", name);
  356.         myClass = objc_getClass(path);
  357.  
  358.         theView = [[myClass allocFromZone:backZone] initFrame:&aFrame];
  359.         [[moduleList objectAt:virtualViewIndex] setView:theView];
  360.     }
  361.  
  362.     theView = [moduleList viewAt:virtualViewIndex];
  363.     
  364.     return theView;
  365. }
  366.  
  367. - showSettingsPanel:sender
  368. {
  369.     [self getScreenSaverSetting];
  370.     [self getScreenLockerSetting];
  371.     [self getPrioritySetting];
  372.     [self getImageFile];
  373.     [self getHotCornerSetting];
  374.     [settingsPanel display];
  375.     [settingsPanel makeKeyAndOrderFront:self];
  376.     windowHasBeenDisplayed = YES;
  377.     return self;
  378. }
  379.  
  380. - showInfoPanel:sender
  381. {
  382.     if (!infoPanel)
  383.     {
  384.         if (![NXApp loadNibSection:"Info.nib" owner:self withNames:NO fromZone:[self zone]])
  385.             NXLogError ("Can't find Info.nib!");    
  386.     }
  387.     [infoPanel makeKeyAndOrderFront:sender];
  388.     return self;
  389. }
  390.  
  391. #define SLEEPSIZE (3.0)
  392.  
  393. - createSleepWindow
  394. {
  395.     if (!sleepWindow)
  396.     {
  397.         NXRect sleep={{0, 0},{SLEEPSIZE, SLEEPSIZE}};
  398.         id aView = [[SleepView alloc] initFrame:&sleep];
  399.  
  400.         sleepWindow = [[Window alloc]
  401.             initContent:&sleep style:NX_TOKENSTYLE
  402.             backing:NX_NONRETAINED buttonMask:0 defer:NO];
  403.  
  404.         [sleepWindow setEventMask:(NX_MOUSEENTEREDMASK | NX_MOUSEEXITEDMASK)];
  405.         PSsetwindowlevel(SLEEPTIER, [sleepWindow windowNum]);
  406.         PSWmakeWindowGray([sleepWindow windowNum]);
  407.         [[sleepWindow setContentView: aView] free];
  408.  
  409.         [sleepWindow setTrackingRect:&sleep inside:YES owner:aView
  410.             tag:3 left:NO right:NO];
  411.     }
  412.  
  413.     return self;
  414. }
  415.  
  416. - setSleepCorner:(int)val
  417. {
  418.     NXRect screen={{0, 0}};
  419.     NXCoord x = 0.0, y = 0.0;
  420.  
  421.     if (val)
  422.     {
  423.         [NXApp getScreenSize:&(screen.size)];
  424.         [self createSleepWindow];
  425.         if (val == 2 || val == 3) x = screen.size.width - SLEEPSIZE;
  426.         if (val == 3 || val == 4) y = screen.size.height - SLEEPSIZE;
  427.         [sleepWindow moveTo:x :y];
  428.         [sleepWindow orderFront:self];
  429.     }
  430.     else [sleepWindow orderOut:self];
  431.     return self;
  432. }
  433.  
  434. - getHotCornerSetting
  435. {
  436.     const char *ptr;
  437.     int tval, val=0;
  438.     
  439.     ptr = NXGetDefaultValue("BackSpace", "hotCorner");
  440.     if (ptr)
  441.     {
  442.         sscanf(ptr,"%d",&tval);
  443.         if (tval >= 0 && tval <= 4) val = tval;
  444.     }
  445.     
  446.     [cornerView setState:val];
  447.     [self setSleepCorner:val];
  448.  
  449.     return self;
  450. }
  451.  
  452.  
  453. @end
  454.