// // ClockView.m // Clock // // Created by Šimon on 10/19/11. // Copyright 2011 Simon Urbanek. All rights reserved. // #import "ClockView.h" @implementation ClockView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { inverted = NO; fullScreen = NO; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateClock:) userInfo:nil repeats:YES]; } return self; } - (void) updateClock: (id) dummy { [self setNeedsDisplay:YES]; } - (void)mouseDown:(NSEvent *)theEvent { inverted = !inverted; [self updateClock: theEvent]; } - (void)otherMouseDown:(NSEvent *)theEvent { if (fullScreen) { [self exitFullScreenModeWithOptions:nil]; fullScreen = NO; } else { // find the screen that contains the center of the view NSSize size = [self bounds].size; NSPoint pt = [[self window] convertBaseToScreen:NSMakePoint(size.width / 2, size.height / 2)]; NSArray *scrs = [NSScreen screens]; NSScreen *myScreen = nil; for (NSScreen *scr in scrs) { NSRect sf = [scr frame]; // NSLog(@"%f, %f <--> [%f, %f, %f, %f]", pt.x, pt.y, sf.origin.x, sf.origin.y, sf.size.width, sf.size.height); if (pt.x >= sf.origin.x && pt.x < sf.origin.x + sf.size.width && pt.y >= sf.origin.y && pt.y < sf.origin.y + sf.size.height) { myScreen = scr; break; } } // then use it for full-scrren view if (myScreen) { [self enterFullScreenMode:myScreen withOptions:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:NO], NSFullScreenModeAllScreens, nil]]; fullScreen = YES; } } } - (void)drawRect:(NSRect)dirtyRect { NSColor *textColor = inverted ? [NSColor colorWithDeviceWhite:0.5 alpha:1.0] : [NSColor blackColor]; struct tm tm; time_t t = 0; time(&t); localtime_r(&t, &tm); if (inverted) { [[NSColor blackColor] set]; [NSBezierPath fillRect:[self bounds]]; } NSSize size = [self bounds].size; double font_size = size.height; if (font_size * 4 > size.width) font_size = size.width / 4; if (font_size > 150) font_size = 150; NSPoint pt = NSMakePoint((size.width - font_size * 4) / 2, (size.height - font_size) / 2); [[NSString stringWithFormat:@"%d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec] drawAtPoint:pt withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:font_size], NSFontAttributeName, textColor, NSForegroundColorAttributeName, nil]]; [[NSString stringWithFormat:@"%d/%02d/%d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday] drawAtPoint:pt withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:10.0], NSFontAttributeName, textColor, NSForegroundColorAttributeName, nil]]; } @end