File: DaliClockWidgetView.m

package info (click to toggle)
xdaliclock 2.48-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,328 kB
  • sloc: ansic: 8,377; sh: 3,522; objc: 2,029; makefile: 1,220; java: 1,044; javascript: 802; asm: 419; xml: 337; perl: 269; ruby: 5
file content (220 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* xdaliclock - a melting digital clock
 * Copyright (c) 1991-2013 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 */

#import "DaliClockWidgetView.h"

char *progname = "Dali Clock";   // digital.c wants this for error messages.


@implementation DaliClockWidgetView


+ (void)initialize;
{
  static BOOL initialized_p = NO;
  if (initialized_p)
    return;
  initialized_p = YES;

  NSUserDefaultsController *controller =
    [NSUserDefaultsController sharedUserDefaultsController];
  NSUserDefaults *defs = [controller defaults];
  [DaliClockView registerDefaults:defs];
  // Any Widget-specific preferences would go here.
  [DaliClockView setUserDefaultsController: controller];
}



/* initWithWebView is called as the Dashboard widget and its WebView
   are initialized, which is when the widget plug-in is loaded.  This
   is just an object initializer; DO NOT use the passed WebView to
   manipulate WebScriptObjects or anything in the WebView's hierarchy.
 */
- (id) initWithWebView:(WebView*)wv
{
  NSRect rect;
  rect.origin.x = 0;
  rect.origin.y = 0;
  rect.size.width = 100;
  rect.size.height = 30;
  self = [super initWithFrame:rect];
  webView = wv;

  [self setAutoDate:67];   // show date every now and then

  return self;
}

/* windowScriptObjectAvailable passes the JavaScript window object
   referring to the plug-in's parent window (in this case, the
   Dashboard widget).  We use that to register our plug-in as a var of
   the window object; This allows the plug-in to be referenced from
   JavaScript via window.<plugInName>, or just <plugInName>.
 */
- (void) windowScriptObjectAvailable:(WebScriptObject *) ws
{
  [ws setValue:self forKey:@"DaliClock"];
}

/* Prevent direct key access from JavaScript.
   Write accessor methods and expose those if necessary
 */
+ (BOOL) isKeyExcludedFromWebScript: (const char *) key
{
  return YES;
}

/* Used for convenience of WebScripting names below
 */
NSString * const kWebSelectorPrefix = @"web_";

/* This is where prefixing our JavaScript methods with web_ pays off:
   instead of a huge if/else trail to decide which methods to exclude,
   just check the selector names for kWebSelectorPrefix.
 */
+ (BOOL) isSelectorExcludedFromWebScript:(SEL)sel
{
  return !([NSStringFromSelector(sel) hasPrefix:kWebSelectorPrefix]);
}

/* Another simple implementation: take the first token of the Obj-C
 method signature and remove the web_ prefix. So web_hourStyle is
 called from JavaScript as DaliClock.hourStyle.
 */
+ (NSString *) webScriptNameForSelector:(SEL)sel
{
  NSString *ss = NSStringFromSelector(sel);

  if ([ss hasPrefix:kWebSelectorPrefix] &&
      ([ss length] > [kWebSelectorPrefix length])) {
    return [[[ss substringFromIndex:[kWebSelectorPrefix length]]
              componentsSeparatedByString: @":"]
             objectAtIndex: 0];
  }
  return nil;
}

/* JavaScript-ready methods
 */
- (id) web_hourStyle  { return [self valueForKey:@"hourStyle"];  }
- (id) web_timeStyle  { return [self valueForKey:@"timeStyle"];  }
- (id) web_dateStyle  { return [self valueForKey:@"dateStyle"];  }
- (id) web_cycleSpeed { return [self valueForKey:@"cycleSpeed"]; }
- (id) web_usesCountdownTimer {
  return [self valueForKey:@"usesCountdownTimer"];
}
- (id) web_countdownDate {
  return [self valueForKey:@"countdownDate"];
}
- (id) web_initialForegroundColor { 
  return [self valueForKey:@"initialForegroundColor"];
}
- (id) web_initialBackgroundColor {
  return [self valueForKey:@"initialBackgroundColor"];
}

- (void) web_setHourStyle: (id)arg {
  [self setValue:arg forKey:@"HourStyle"];
}
- (void) web_setTimeStyle: (id)arg {
  [self setValue:arg forKey:@"TimeStyle"];
}
- (void) web_setDateStyle: (id)arg {
  [self setValue:arg forKey:@"DateStyle"];
}
- (void) web_setCycleSpeed: (id)arg {
  [self setValue:arg forKey:@"CycleSpeed"];
}
- (void) web_setUsesCountdownTimer: (id)arg {
  [self setValue:arg forKey:@"UsesCountdownTimer"];
}
- (void) web_setCountdownDate: (id)arg {
  [self setValue:arg forKey:@"CountdownDate"];
}
- (void) web_setInitialForegroundColor: (id)arg {
  [self setValue:arg forKey:@"InitialForegroundColor"];
}
- (void) web_setInitialBackgroundColor: (id)arg {
  [self setValue:arg forKey:@"InitialBackgroundColor"];
}

- (void) web_setFrame: (WebScriptObject *)frame
{
  NSRect rect;
  rect.origin.x    = [[frame webScriptValueAtIndex:0] doubleValue];
  rect.origin.y    = [[frame webScriptValueAtIndex:1] doubleValue];
  rect.size.width  = [[frame webScriptValueAtIndex:2] doubleValue];
  rect.size.height = [[frame webScriptValueAtIndex:3] doubleValue];

  [self setFrame: rect];
  [self setHidden:YES];
  [webView addSubview:self];
}

- (void) web_show
{
  // Start all our timers if they aren't running.
  [self clockTick];
  [self colorTick];
  [self dateTick];

  [self setHidden:NO];
}

- (void) web_hide
{
  [self setHidden:YES];

  // Kill all our timers to avoid using any CPU.

/*
  if (clockTimer && [clockTimer isValid]) {
    [clockTimer invalidate];
    clockTimer = 0;
  }

  if (colorTimer && [colorTimer isValid]) {
    [colorTimer invalidate];
    colorTimer = 0;
  }

  if (dateTimer && [dateTimer isValid]) {
    [dateTimer invalidate];
    dateTimer = 0;
  }
*/
}


/* Announce our unwillingness to accept keyboard input.
 */
- (BOOL)acceptsFirstResponder
{
  return NO;
}


#if 0
- (void)setValue:(id)value forKey:(NSString *)key
{
  NSLog(@"setValue %@ = %@", key, value);
  [super setValue:value forKey:key];

  // Make sure the preferences get written right away.
  NSUserDefaultsController *ctl = [[self class] userDefaultsController];
  [ctl commitEditing];
  [ctl save:self];
}
#endif

@end