[102] | 1 | //
|
---|
| 2 | // PSCalendarController.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Fri Feb 14 2003.
|
---|
| 6 | // Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "PSCalendarController.h"
|
---|
| 10 | #import "OACalendarView.h"
|
---|
| 11 | #import "NSCalendarDate-OFExtensions.h"
|
---|
| 12 | #import "NSCalendarDate-NJRExtensions.h"
|
---|
| 13 |
|
---|
| 14 | @implementation PSCalendarController
|
---|
| 15 |
|
---|
| 16 | + (PSCalendarController *)controllerWithDate:(NSCalendarDate *)aDate delegate:(id)aDelegate;
|
---|
| 17 | {
|
---|
| 18 | return [[self alloc] initWithDate:(NSCalendarDate *)aDate delegate:(id)aDelegate];
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | - (id)initWithDate:(NSCalendarDate *)aDate delegate:(id)aDelegate;
|
---|
| 22 | {
|
---|
| 23 | if ([self initWithWindowNibName: @"Calendar"]) {
|
---|
| 24 | NSWindow *window = [self window]; // connect outlets
|
---|
| 25 | [calendarView setTarget: self]; // delegate
|
---|
| 26 | [calendarView setSelectionType: OACalendarViewSelectByDay];
|
---|
| 27 | [calendarView setShowsDaysForOtherMonths: YES];
|
---|
| 28 | if (aDate == nil) aDate = [NSCalendarDate calendarDate];
|
---|
| 29 | [calendarView setSelectedDay: aDate];
|
---|
| 30 | [calendarView setVisibleMonth: aDate];
|
---|
| 31 | delegate = [aDelegate retain];
|
---|
| 32 |
|
---|
| 33 | NSView *view = [aDelegate calendarControllerLaunchingView: self];
|
---|
| 34 | if (view != nil) {
|
---|
| 35 | NSRect rect = [view convertRect: [view bounds] toView: nil];
|
---|
| 36 | NSWindow *parentWindow = [view window];
|
---|
| 37 | rect.origin = [parentWindow convertBaseToScreen: rect.origin];
|
---|
| 38 | rect.origin.x -= [window frame].size.width - rect.size.width;
|
---|
| 39 | [window setFrameTopLeftPoint: rect.origin];
|
---|
| 40 | NSRect visibleFrame = [[parentWindow screen] visibleFrame];
|
---|
| 41 | if (!NSContainsRect(visibleFrame, [window frame])) {
|
---|
| 42 | NSPoint textFieldTopLeft = { rect.origin.x, rect.origin.y + rect.size.height };
|
---|
| 43 | [window setFrameOrigin: textFieldTopLeft];
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | [window setOpaque: NO];
|
---|
| 47 | [window setBackgroundColor: [NSColor colorWithCalibratedWhite: 0.81f alpha: 0.9f]];
|
---|
| 48 | [window setHasShadow: NO];
|
---|
| 49 | [window setLevel: NSModalPanelWindowLevel];
|
---|
| 50 | [NSApp runModalForWindow: window];
|
---|
| 51 | }
|
---|
| 52 | return self;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | - (void)dealloc;
|
---|
| 56 | {
|
---|
| 57 | [delegate release];
|
---|
| 58 | [super dealloc];
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | - (IBAction)close:(NSButton *)sender;
|
---|
| 62 | {
|
---|
| 63 | [NSApp stopModal];
|
---|
| 64 | [delegate calendarController: self didSetDate: [calendarView selectedDay]];
|
---|
| 65 | [self close];
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | - (IBAction)cancel:(NSButton *)sender;
|
---|
| 69 | {
|
---|
| 70 | [NSApp stopModal];
|
---|
| 71 | [self close];
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | - (IBAction)today:(NSButton *)sender;
|
---|
| 75 | {
|
---|
| 76 | NSCalendarDate *today = [NSCalendarDate calendarDate];
|
---|
| 77 | [calendarView setSelectedDay: today];
|
---|
| 78 | [calendarView setVisibleMonth: today];
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @end
|
---|
| 82 |
|
---|
| 83 | @implementation PSCalendarController (OACalendarViewDelegate)
|
---|
| 84 |
|
---|
| 85 | - (BOOL)calendarView:(OACalendarView *)aCalendarView shouldSelectDate:(NSCalendarDate *)aDate;
|
---|
| 86 | {
|
---|
| 87 | return ([[NSCalendarDate dateForDay: aDate] compare: [NSCalendarDate dateForDay: [NSCalendarDate calendarDate]]] != NSOrderedAscending);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | - (int)calendarView:(OACalendarView *)aCalendarView highlightMaskForVisibleMonth:(NSCalendarDate *)visibleMonth;
|
---|
| 91 | {
|
---|
| 92 | NSCalendarDate *today = [NSCalendarDate calendarDate];
|
---|
| 93 | if ([visibleMonth yearOfCommonEra] == [today yearOfCommonEra] && [visibleMonth monthOfYear] == [today monthOfYear]) {
|
---|
| 94 | return 1 << ([today dayOfMonth] - 1 + [[today firstDayOfMonth] dayOfWeek]);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return 0;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | - (void)calendarViewShouldDismiss:(OACalendarView *)aCalendarView;
|
---|
| 101 | {
|
---|
| 102 | [okButton performClick: aCalendarView];
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | @end
|
---|
| 106 |
|
---|
| 107 | @implementation PSCalendarController (NSWindowNotifications)
|
---|
| 108 |
|
---|
| 109 | - (void)windowWillClose:(NSNotification *)notification;
|
---|
| 110 | {
|
---|
| 111 | [self autorelease];
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | @end |
---|