home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Productivity / MenuClock-1.02 / MenuClock.m < prev    next >
Encoding:
Text File  |  1997-12-29  |  4.2 KB  |  154 lines

  1. #import "MenuClock.h"
  2.  
  3. #import <AppKit/psopsNeXT.h>
  4.  
  5. #define    TimeFormat        @"TimeFormat"
  6. #define    defaultTimeFormat    @"%I:%M:%S %p %m/%d/%y"
  7.  
  8. static    int    activeCount    = 0;
  9.  
  10. @interface MCWindow:NSWindow
  11. - (void)setAvoidsActivation:(BOOL)value;
  12. - (void)mouseDown:(NSEvent *)theEvent;
  13. @end
  14.  
  15. @implementation MCWindow
  16. - (void)setAvoidsActivation:(BOOL)value
  17. {
  18.     _wFlags.avoidsActivation = value;
  19. }
  20. - (void)mouseDown:(NSEvent *)theEvent
  21. {
  22.     [[[NSApplication sharedApplication] delegate] setClockMouseDown];
  23.     [super mouseDown:theEvent];
  24. }
  25. @end
  26.  
  27. @implementation MenuClock
  28.  
  29. + (void)initialize{
  30.  
  31.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  32.     NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:defaultTimeFormat forKey:TimeFormat];
  33.  
  34.     [defaults registerDefaults:appDefaults];
  35. }
  36.  
  37. - init
  38. {
  39.     NSRect        screenRect, windowRect;
  40.     NSFont        *font;
  41.     float        f = 14;
  42.  
  43.     screenRect = [[NSScreen mainScreen] frame];
  44.  
  45.     windowRect.origin.x = screenRect.size.width - 250;
  46.     windowRect.origin.y = screenRect.size.height - f;
  47.     windowRect.size.width = 250;
  48.     windowRect.size.height = (f ? f : 14);
  49.  
  50.     window = [[[MCWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO] retain];
  51.  
  52.     // stolen from MagnifyingGlass example
  53.     // we don't want the window to be filled so it stays transparent
  54.     PSsetautofill(NO, [window windowNumber]);
  55.  
  56.     [window setLevel:NSModalPanelWindowLevel];
  57.     [window orderWindow:NSWindowAbove relativeTo:0];
  58.     [window orderFrontRegardless];
  59.     //[window setAvoidsActivation:YES];
  60.  
  61.     font = [NSFont menuFontOfSize:12];
  62.  
  63.     textField = [[NSTextField alloc] initWithFrame:[window frame]];
  64.     [textField setFont:font];
  65.     [textField setBordered:NO];
  66.     [textField setDrawsBackground:NO];
  67.     [textField setEditable:NO];
  68.     [[textField cell] setAlignment:NSRightTextAlignment];
  69.  
  70.     //[textField setStringValue:[[NSCalendarDate calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] stringForKey:TimeFormat]]];
  71.  
  72.     [window setContentView:textField];
  73.     [window display];
  74.     
  75.     timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES] retain];
  76.  
  77.     makeKey=0;
  78.     
  79.     return self;
  80. }
  81.  
  82. - (void)updateTimer:(id)sender
  83. {
  84.     [textField setStringValue:[[NSCalendarDate calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] stringForKey:TimeFormat]]];
  85.     if (([aboutWindow isVisible] == NO) && (makeKey > 0))
  86.     {
  87.         makeKey --;
  88.         if ( makeKey == 0 )
  89.             [[NSApplication sharedApplication] hide:self];
  90.     }
  91. }
  92.  
  93. - (void)dealloc
  94. {
  95.     [timer invalidate];
  96.     [timer release];
  97.     [textField release];
  98.     [window release];
  99.     return;
  100. }
  101.  
  102. - (void)about:(id)sender
  103. {
  104.     [aboutWindow center];
  105.     [menuFormat setStringValue:[[NSUserDefaults standardUserDefaults] stringForKey:TimeFormat]];
  106.     [menuSample setStringValue:[[NSCalendarDate calendarDate] descriptionWithCalendarFormat:[[NSUserDefaults standardUserDefaults] stringForKey:TimeFormat]]];
  107.     [aboutWindow makeKeyAndOrderFront:self];
  108. }
  109.  
  110. - (void)setClockMouseDown
  111. {
  112.     makeKey = 5;
  113. }
  114.  
  115. - (void)restoreDefault:(id)sender
  116. {
  117.     [[NSUserDefaults standardUserDefaults] setObject:defaultTimeFormat forKey:TimeFormat];
  118.     [menuFormat setStringValue:defaultTimeFormat];
  119.     [menuSample setStringValue:[[NSCalendarDate calendarDate] descriptionWithCalendarFormat:defaultTimeFormat]];
  120. }
  121. - (void)changeFormat:(id)sender
  122. {
  123.     NS_DURING
  124.         [menuSample setStringValue:[[NSCalendarDate calendarDate] descriptionWithCalendarFormat:[menuFormat stringValue]]];
  125.         [[NSUserDefaults standardUserDefaults] setObject:[menuFormat stringValue] forKey:TimeFormat];
  126.     NS_HANDLER
  127.         [menuSample setStringValue:@"invalid date format"];
  128.     NS_ENDHANDLER
  129. }
  130.  
  131. - (void)applicationDidHide:(NSNotification *)aNotification
  132. {
  133.     [window orderFront:self];
  134. }
  135. - (void)applicationWillBecomeActive:(NSNotification *)notification;
  136. {
  137.     if ( makeKey <= 0 )
  138.         [[notification object] hide:self];
  139. }
  140. @end
  141.  
  142.  
  143. @implementation ClockApp
  144.  
  145. - (void)finishLaunching
  146. {
  147.     NSPoint aPt = {-10000,-10000};
  148.  
  149.     [super finishLaunching]; 
  150.     [_appIcon setFrameOrigin:aPt];
  151.         return;
  152. }
  153.  
  154. @end