forked from Me1000/Cappuccino-Sample-Controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.j
985 lines (757 loc) · 31.7 KB
/
AppController.j
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*
* AppController.j
* SampleControls
*
* Created by You on February 2, 2011.
* Copyright 2011, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
var defaultViewRect;
var dataStructure = {name: "Root", sub: [
{name: "Music", sub: ["file1.mp3", {name: "Daft Punk", sub: ["Digital Love", "One More Time", "High Life", "Veridis Quo"]}, "AwesomeSong.mp3", "AnotherAwesomeSong.mp3"]},
{name: "Pictures", sub: ["Picture1.jpg", "Picture2.jpg", "Picture3.jpg", "Picture4.jpg"]},
{name: "Code", sub: [
{name: "SampleControls", sub: ["Item two", "Item three", "Item four"]},
{name: "Cappuccino", sub: [{name: "AppKit", sub: ["AppKit.j", {name: "Resources", sub: ["Image.png", "AboutPanel.cib", "Window.png", "Arist.psd"]}, "CPBrowser.j", "CPTableView.j", "CPButton.j"]}, "Foundation.j", "CPTableView.j", "Jakefile"]},
{name: "280Slides", sub: ["SlideController.j", "YouTubeController.j", "logo.png", "Side.j"]},
{name: "Atlas", sub: ["AwesomeController.j", "Icon.png", "Jakefile", "Server.js"]}
]
},
{name: "System", sub: [{name: "Library", sub: ["SpecialFile", "AnotherFile", "SecretStuff", "Scarry Stuff"]}]},
{name: "Applications", sub: ["XCode.app", "Atlas.app", "Kaleidoscope.app", "Issues.app"]},
]
};
@implementation AppController : CPObject
{
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView],
frame = [contentView frame];
var tabview = [[CPTabView alloc] initWithFrame:CGRectMake(15, 15, frame.size.width - 30, frame.size.height - 30)];
[tabview setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
defaultViewRect = CGRectMake(15, 15, frame.size.width - 60, frame.size.height - 90);
var items = ["generalControls", "TableView", "OutlineView", "Browser", "WindowsAndAlerts"];
for (var i = 0, c = [items count]; i < c; i++)
{
var tab = [[CPTabViewItem alloc] initWithIdentifier:items[i]];
[tab setLabel:items[i]];
[tab setView:objj_msgSend(SampleControlTabs, items[i], nil)];
[tabview addTabViewItem:tab];
}
[contentView addSubview:tabview];
[theWindow orderFront:self];
// Uncomment the following line to turn on the standard menu bar.
[CPMenu setMenuBarVisible:YES];
}
@end
/*!
This class contains class methods which returns a view that will be contained in the tab view
*/
@implementation SampleControlTabs : CPObject
+ (CPView)generalHud
{
var view = [[CPView alloc] initWithFrame:CGRectMake(0,0,390,285)];
var scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(0,0,390,285)];
[scroll setDocumentView:view];
[[scroll verticalScroller] setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[[scroll horizontalScroller] setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[scroll setValue:[CPColor clearColor] forThemeAttribute:"bottom-corner-color"];
[scroll setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
[scroll setAutohidesScrollers:YES];
[[scroll bottomCornerView] setBackgroundColor:[CPColor clearColor]];
var label = [CPTextField labelWithTitle:"Buttons"];
[label setFrameOrigin:CGPointMake(55, 20)];
[label setTextColor:[CPColor whiteColor]];
[view addSubview:label]
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 40, 120, 24)];
[button setTitle:"Standard Button"];
[button setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[view addSubview:button];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 75, 120, 24)];
[button setTitle:"Round Button"];
[button setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[button setThemeState:CPButtonStateBezelStyleRounded];
[view addSubview:button];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 110, 120, 24)];
[button setTitle:"Default Button"];
[button setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[button setKeyEquivalent:CPCarriageReturnCharacter];
[view addSubview:button];
/*
Other
*/
var label = [CPTextField labelWithTitle:"Other Controls"];
[label setFrameOrigin:CGPointMake(175, 20)];
[label setTextColor:[CPColor whiteColor]];
[view addSubview:label];
var control = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(175, 40, 0, 24)];
[control setSegmentCount:3];
[control setWidth:50 forSegment:0];
[control setWidth:50 forSegment:1];
[control setWidth:50 forSegment:2];
[control setLabel:"Seg 1" forSegment:0];
[control setLabel:"Seg 2" forSegment:1];
[control setLabel:"Seg 3" forSegment:2];
[control setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[view addSubview:control];
var control = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(175, 75, 0, 24)];
[control setSegmentCount:3];
[control setWidth:50 forSegment:0];
[control setWidth:50 forSegment:1];
[control setWidth:50 forSegment:2];
[control setLabel:"Seg 1" forSegment:0];
[control setLabel:"Seg 2" forSegment:1];
[control setLabel:"Seg 3" forSegment:2];
[control setTrackingMode:CPSegmentSwitchTrackingSelectAny];
[control setSelected:YES forSegment:0];
[control setSelected:YES forSegment:2];
[control setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[view addSubview:control];
var control1 = [[CPSlider alloc] initWithFrame:CGRectMake(175, 100, 150, 24)];
[control1 setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[control setFloatValue:25.0];
[view addSubview:control1];
var control2 = [[CPSlider alloc] initWithFrame:CGRectMake(175, 135, 24, 120)];
[control2 setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[view addSubview:control2];
var control3 = [[CPSlider alloc] initWithFrame:CGRectMake(215, 150, 32,32)];
[control3 setTheme:[CPTheme themeNamed:"Aristo-HUD"]];
[control3 setSliderType:CPCircularSlider];
[view addSubview:control3];
// bind the two sliders
[control1 setTarget:control2];
[control1 setAction:@selector(takeFloatValueFrom:)];
[control2 setTarget:control1];
[control2 setAction:@selector(takeFloatValueFrom:)];
return scroll;
}
+ (CPView)generalControls
{
var view = [[CPView alloc] initWithFrame:defaultViewRect];
var label = [CPTextField labelWithTitle:"Buttons"];
[label setFrameOrigin:CGPointMake(55, 20)];
[view addSubview:label]
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 40, 120, 24)];
[button setTitle:"Standard Button"];
[view addSubview:button];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 75, 120, 24)];
[button setTitle:"Round Button"];
[button setThemeState:CPButtonStateBezelStyleRounded];
[view addSubview:button];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 110, 120, 24)];
[button setTitle:"Default Button"];
[button setKeyEquivalent:CPCarriageReturnCharacter];
[view addSubview:button];
var button = [CPCheckBox checkBoxWithTitle:"Checkbox"];
[button setFrameOrigin:CGPointMake(15, 145)];
[button setAllowsMixedState:YES];
[view addSubview:button];
var button = [CPRadio radioWithTitle:"Radio"];
[button setFrameOrigin:CGPointMake(15, 170)];
[view addSubview:button];
var button = [[CPPopUpButton alloc] initWithFrame:CGRectMake(15, 195, 120, 24) pullsDown:NO];
[button addItemsWithTitles:["Popup Button", "Item 2", "Item 3"]];
[view addSubview:button];
var button = [[CPPopUpButton alloc] initWithFrame:CGRectMake(15, 235, 120, 24) pullsDown:YES];
[button addItemsWithTitles:["Pulldown Button", "Item 2", "Item 3"]];
[view addSubview:button];
var control = [[CPButton alloc] initWithFrame:CGRectMake(15, 280, 120, 24)];
[control setTitle:"Disable All"];
[control setTarget:view];
[control setAction:@selector(toggleEnabledSubviews:)];
[view addSubview:control];
/*
Text
*/
var label = [CPTextField labelWithTitle:"Textfields"];
[label setFrameOrigin:CGPointMake(220, 20)];
[view addSubview:label];
var text = [[CPTextField alloc] initWithFrame:CGRectMake(190, 40, 120, 28)];
[text setStringValue:"Standard Text"];
[text setEditable:YES];
[text setBezeled:YES];
[view addSubview:text];
var text = [[CPTextField alloc] initWithFrame:CGRectMake(190, 75, 120, 28)];
[text setPlaceholderString:"Placeholder Text"];
[text setEditable:YES];
[text setBezeled:YES];
[view addSubview:text];
var text = [[CPTextField alloc] initWithFrame:CGRectMake(190, 105, 120, 30)];
[text setPlaceholderString:"Rounded Text"];
[text setEditable:YES];
[text setBezeled:YES];
[text setBezelStyle:CPTextFieldRoundedBezel];
[view addSubview:text];
var text = [[CPTextField alloc] initWithFrame:CGRectMake(190, 142, 120, 30)];
[text setStringValue:"Label"];
[view addSubview:text];
var text = [[CPSearchField alloc] initWithFrame:CGRectMake(190, 172, 120, 30)];
[text setStringValue:"Search Field"];
[view addSubview:text];
/*
Other
*/
var label = [CPTextField labelWithTitle:"Other Controls"];
[label setFrameOrigin:CGPointMake(380, 20)];
[view addSubview:label];
var control = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(340, 40, 0, 24)];
[control setSegmentCount:3];
[control setWidth:50 forSegment:0];
[control setWidth:50 forSegment:1];
[control setWidth:50 forSegment:2];
[control setLabel:"Seg 1" forSegment:0];
[control setLabel:"Seg 2" forSegment:1];
[control setLabel:"Seg 3" forSegment:2];
[view addSubview:control];
var control = [[CPSegmentedControl alloc] initWithFrame:CGRectMake(340, 75, 0, 24)];
[control setSegmentCount:3];
[control setWidth:50 forSegment:0];
[control setWidth:50 forSegment:1];
[control setWidth:50 forSegment:2];
[control setLabel:"Seg 1" forSegment:0];
[control setLabel:"Seg 2" forSegment:1];
[control setLabel:"Seg 3" forSegment:2];
[control setTrackingMode:CPSegmentSwitchTrackingSelectAny];
[control setSelected:YES forSegment:0];
[control setSelected:YES forSegment:2];
[view addSubview:control];
var control1 = [[CPSlider alloc] initWithFrame:CGRectMake(340, 100, 150, 24)];
[control setFloatValue:25.0];
[view addSubview:control1];
var control2 = [[CPSlider alloc] initWithFrame:CGRectMake(340, 135, 24, 120)];
[view addSubview:control2];
var control3 = [[CPSlider alloc] initWithFrame:CGRectMake(385, 150, 32,32)];
[control3 setSliderType:CPCircularSlider];
[view addSubview:control3];
// bind the two sliders
[control1 setTarget:control2];
[control1 setAction:@selector(takeFloatValueFrom:)];
[control2 setTarget:control1];
[control2 setAction:@selector(takeFloatValueFrom:)];
var progress = [[CPProgressIndicator alloc] initWithFrame:CGRectMake(340, 265, 120, 15)];
[view addSubview:progress];
var callback = function(){
var value = [progress doubleValue];
if (this.increment && value >= 100)
this.increment = NO;
else if (!this.increment && value <= 0)
this.increment = YES;
[progress incrementBy:(this.increment) ? 1 : -1];
}
callback.increment = YES;
var timer = [CPTimer scheduledTimerWithTimeInterval:.05 callback:callback repeats:YES];
var control = [[CPProgressIndicator alloc] initWithFrame:CGRectMake(340, 295, 64, 64)];
[control setStyle:CPProgressIndicatorSpinningStyle];
[view addSubview:control];
/*
Token
*/
var label = [CPTextField labelWithTitle:"More Awesome Stuff"];
[label setFrameOrigin:CGPointMake(535, 20)];
[view addSubview:label];
var control = [[CPTokenField alloc] initWithFrame:CGRectMake(535, 40, 200, 100)];
[control setObjectValue:["token", "field"]];
[control setEditable:YES];
[control setDelegate:[TokenFieldDelegate new]];
[control setCompletionDelay:0];
[view addSubview:control];
var text = [[CPTextField alloc] initWithFrame:CGRectMake(535, 150, 200, 31)];
[text setBezeled:YES];
[text setVerticalAlignment:CPCenterVerticalTextAlignment];
[text setStringValue:"0"];
[view addSubview:text];
var control = [[CPStepper alloc] initWithFrame:CGRectMake(712, 153, 19, 26)];
[control setTarget:text];
[control setAction:@selector(takeIntValueFrom:)];
[view addSubview:control];
return view;
}
+ (CPView)TableView
{
var view = [[CPView alloc] initWithFrame:defaultViewRect];
/*
Regular selection style
*/
var scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(15, 25, CGRectGetWidth(defaultViewRect)/2 - 50, CGRectGetHeight(defaultViewRect))];
[scroll setBorderType:CPLineBorder];
[view addSubview:scroll];
var table = [[CPTableView alloc] initWithFrame:CGRectMakeZero()],
columnCount = 3;
//add image column
var column = [[CPTableColumn alloc] initWithIdentifier:"Image"],
imageView = [[CPImageView alloc] initWithFrame:CGRectMakeZero()];
[[column headerView] setStringValue:"Image"];
[imageView setImageScaling:CPScaleNone];
[column setDataView:imageView];
[table addTableColumn:column];
[table setUsesAlternatingRowBackgroundColors:YES];
[table registerForDraggedTypes:["testType"]];
while (columnCount--)
{
var name = "Column " + ABS(columnCount - 3),
column = [[CPTableColumn alloc] initWithIdentifier:name];
[[column headerView] setStringValue:name];
[column setEditable:YES];
if (columnCount === 2)
var desc = [CPSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
else
var desc = [CPSortDescriptor sortDescriptorWithKey:@"col"+columnCount ascending:YES];
[column setSortDescriptorPrototype:desc];
[table addTableColumn:column];
}
var dataSourceAndDelegate = [[TableViewDataSource alloc] init];
[table setDataSource:dataSourceAndDelegate];
[table setDelegate:dataSourceAndDelegate];
[table setAllowsMultipleSelection:YES];
[scroll setDocumentView:table];
/*!
Source List selection style
*/
var scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(CGRectGetMaxX([scroll frame]) + 25, 25, CGRectGetWidth(defaultViewRect)/2 - 50, CGRectGetHeight(defaultViewRect))];
[scroll setBorderType:CPLineBorder];
[view addSubview:scroll];
var table = [[CPTableView alloc] initWithFrame:CGRectMakeZero()],
columnCount = 3;
//add image column
var column = [[CPTableColumn alloc] initWithIdentifier:"Image"],
imageView = [[CPImageView alloc] initWithFrame:CGRectMakeZero()];
[[column headerView] setStringValue:"Image"];
[imageView setImageScaling:CPScaleNone];
[column setDataView:imageView];
[table addTableColumn:column];
while (columnCount--)
{
var name = "Column " + ABS(columnCount - 3),
column = [[CPTableColumn alloc] initWithIdentifier:name];
[[column headerView] setStringValue:name];
[table addTableColumn:column];
}
[table setDataSource:[[TableViewDataSource alloc] init]];
[table setSelectionHighlightStyle:CPTableViewSelectionHighlightStyleSourceList];
[table registerForDraggedTypes:["testType"]];
[scroll setDocumentView:table];
return view;
}
+ (CPView)OutlineView
{
var view = [[CPView alloc] initWithFrame:defaultViewRect];
var scroll = [[CPScrollView alloc] initWithFrame:CGRectMake(15, 20, 600, 400)],
ov = [[CPOutlineView alloc] initWithFrame:CGRectMakeZero()],
col = [[CPTableColumn alloc] initWithIdentifier:"ovcol"];
[[col headerView] setStringValue:"Outline Column"];
var dataDelegate = [[OutlineViewDataSourceDelegate alloc] init];
[dataDelegate setOv:ov];
[ov setDataSource:dataDelegate];
[ov setDelegate:dataDelegate];
[col setWidth:215];
[ov addTableColumn:col];
var col2 = [[CPTableColumn alloc] initWithIdentifier:"Column 2"]
[[col2 headerView] setStringValue:"Column 2"];
[ov addTableColumn:col2];
[scroll setDocumentView:ov];
[view addSubview:scroll];
[ov registerForDraggedTypes:["TestData"]];
return view;
}
+ (CPView)Browser
{
var view = [[CPView alloc] initWithFrame:defaultViewRect];
var box = [[CPBox alloc] initWithFrame:CGRectMake(15, 40, 650, 200)];
var browser = [[CPBrowser alloc] initWithFrame:CGRectMake(1, 1, 648, 198)];
[browser setDelegate:[[BrowserDelegate alloc] init]];
[browser setBackgroundColor:[CPColor whiteColor]];
[browser setMinColumnWidth:170];
[browser setAllowsMultipleSelection:NO];
[box addSubview:browser];
[view addSubview:box];
return view;
}
+ (CPView)WindowsAndAlerts
{
var view = [[CPView alloc] initWithFrame:defaultViewRect];
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(100, 100, 400, 400) styleMask:CPClosableWindowMask|CPResizableWindowMask];
[window setTitle:"Sample Window"];
[window setMinSize:CGSizeMake(100,100)];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 20, 100, 24)];
[button setTitle:"CPWindow"];
[button setTarget:window];
[button setAction:@selector(orderFront:)];
[view addSubview:button];
// platofrm windows
var platformWindow = [[CPPlatformWindow alloc] initWithContentRect:CGRectMake(100,100,775,400)],
window = [[CPWindow alloc] initWithContentRect:CGRectMake(100, 100, 775, 400) styleMask:CPClosableWindowMask|CPResizableWindowMask];
[window setTitle:"Sample Window"];
[window setMinSize:CGSizeMake(100,100)];
[window setFullBridge:YES];
[window setPlatformWindow:platformWindow];
[window setContentView:[self generalControls]];
var button = [[CPButton alloc] initWithFrame:CGRectMake(130, 20, 100, 24)];
[button setTitle:"CPPlatformWindow"];
[button setTarget:window];
[button setAction:@selector(orderFront:)];
[view addSubview:button];
// info alert
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Informational Alert"];
[alrt setMessageText:"Informational Alert"];
[alrt setInformativeText:"This CPAlert is used to display information modally. It can have a help button and an accessory view, and a supression button."];
[alrt setShowsHelp:YES];
[alrt setShowsSuppressionButton:YES];
[alrt setAlertStyle:CPInformationalAlertStyle];
[alrt addButtonWithTitle:"Okay"];
var button = [[CPButton alloc] initWithFrame:CGRectMake(245, 20, 100, 24)];
[button setTitle:"Info CPAlert"];
[button setTarget:alrt];
[button setAction:@selector(runModal)];
[view addSubview:button];
// Warn alert
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Warning Alert"];
[alrt setMessageText:"Warning Alert"];
[alrt setInformativeText:"This CPAlert is used to display warnings modally. It can have a help button and an accessory view, and a supression button."];
[alrt setShowsHelp:YES];
[alrt setShowsSuppressionButton:YES];
[alrt setAlertStyle:CPWarningAlertStyle];
[alrt addButtonWithTitle:"Okay"];
var button = [[CPButton alloc] initWithFrame:CGRectMake(370, 20, 100, 24)];
[button setTitle:"Warn CPAlert"];
[button setTarget:alrt];
[button setAction:@selector(runModal)];
[view addSubview:button];
// ERROR alert
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Error Alert"];
[alrt setMessageText:"Error Alert"];
[alrt setInformativeText:"This CPAlert is used to display errors modally. It can have a help button and an accessory view, and a supression button."];
[alrt setShowsHelp:YES];
[alrt setShowsSuppressionButton:YES];
[alrt setAlertStyle:CPCriticalAlertStyle];
[alrt addButtonWithTitle:"Okay"];
var button = [[CPButton alloc] initWithFrame:CGRectMake(495, 20, 100, 24)];
[button setTitle:"Error CPAlert"];
[button setTarget:alrt];
[button setAction:@selector(runModal)];
[view addSubview:button];
// SHEETS
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Informational Alert"];
[alrt setMessageText:"Informational Alert"];
[alrt setInformativeText:"CPAlerts can also be used as sheets! With the same options as before."];
[alrt setShowsHelp:YES];
[alrt setShowsSuppressionButton:YES];
[alrt setAlertStyle:CPInformationalAlertStyle];
[alrt addButtonWithTitle:"Okay"];
var controller = [[SheetController alloc] init];
[controller setSheet:alrt];
var button = [[CPButton alloc] initWithFrame:CGRectMake(15, 59, 100, 24)];
[button setTitle:"Attached Sheet"];
[button setTarget:controller];
[button setAction:@selector(beginSheet:)];
[view addSubview:button];
// color panel
var button = [[CPButton alloc] initWithFrame:CGRectMake(130, 59, 100, 24)];
[button setTitle:"Color Panel"];
[view addSubview:button];
var picker = [CPColorPanel sharedColorPanel];
[button setTarget:picker];
[button setAction:@selector(orderFront:)];
// HUD
var window = [[CPWindow alloc] initWithContentRect:CGRectMake(100, 100, 390, 285) styleMask:CPClosableWindowMask|CPResizableWindowMask|CPHUDBackgroundWindowMask];
[window setTitle:"HUD Window and Controls"];
[window setMinSize:CGSizeMake(200, 75)];
[[window contentView] addSubview:[self generalHud]];
var button = [[CPButton alloc] initWithFrame:CGRectMake(245, 59, 100, 24)];
[button setTitle:"HUD"];
[button setTarget:window];
[button setAction:@selector(orderFront:)];
[view addSubview:button];
return view;
}
@end
@implementation TableViewDataSource : CPObject
{
CPArray data;
}
- (id)init
{
self = [super init];
data = [ ];
var c = 500;
while (c--)
[data addObject:c];
return self;
}
- (int)numberOfRowsInTableView:(CPTableView)aTable
{
return [data count];
}
- (id)tableView:(CPTableView)aTable objectValueForTableColumn:(CPTableColumn)aCol row:(int)aRow
{
if ([aCol identifier] === "Image")
return [[CPImage alloc] initWithContentsOfFile:"http://cappuccino.org/images/favicon.png" size:CGSizeMake(16,16)];
else if ([aCol identifier] === "Column 1")
return "Row " + data[aRow];
else
return [aCol identifier];
}
- (BOOL)tableView:(CPTableView)aTable isGroupRow:(int)aRow
{
return data[aRow] % 10 === 0;
}
- (BOOL)tableView:(CPTableView)aTableView writeRowsWithIndexes:(CPIndexSet)rowIndexes toPasteboard:(CPPasteboard)pboard
{
[pboard declareTypes:[CPArray arrayWithObject:"testType"] owner:self];
return YES;
}
- (CPDragOperation)tableView:(CPTableView)aTableView
validateDrop:(id)info
proposedRow:(CPInteger)row
proposedDropOperation:(CPTableViewDropOperation)operation
{
/* if (aTableView === tableView)
[aTableView setDropRow:row dropOperation:CPTableViewDropOn];
else
[aTableView setDropRow:row dropOperation:CPTableViewDropAbove];
*/
return CPDragOperationMove;
}
- (BOOL)tableView:(CPTableView)aTableView acceptDrop:(id)info row:(int)row dropOperation:(CPTableViewDropOperation)operation
{
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Cool Features"];
[alrt setMessageText:"No reordering implemented"];
[alrt setInformativeText:"Since this is just a demo we haven't actually implement the drop behavior here."];
[alrt setAlertStyle:CPInformationalAlertStyle];
[alrt addButtonWithTitle:"Okay"];
[alrt runModal];
return NO;
}
- (void)tableView:(CPTableView)aTableView setObjectValue:(id)aValue forTableColumn:(CPTableColumn)tableColumn row:(int)row
{
data[row] = aValue;
}
- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
{
var newDescriptors = [aTableView sortDescriptors];
[data sortUsingDescriptors:newDescriptors];
[aTableView reloadData];
}
@end
@implementation TokenFieldDelegate : CPObject
{
CPArray states;
}
- (id)init
{
self = [super init];
states = ["Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Northern Marianas Islands", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Virgin Islands", "Washington", "West Virginia", "Wisconsin", "Wyoming"];
return self;
}
- (CPArray)tokenField:(CPTokenField)aTokenField completionsForSubstring:(CPString)aString indexOfToken:(int)anIndex indexOfSelectedItem:(int)selectedIndex
{
if (aString)
{
var matches = [],
search = [aString lowercaseString];
for (var i = 0, c = states.length; i < c; i++)
{
var state = [states[i] lowercaseString];
if ([state hasPrefix:search])
[matches addObject:states[i]];
}
return matches
}
else
return [];
}
@end
@implementation BrowserDelegate : CPObject
- (id)rootItemForBrowser:(CPBrowser)browser
{
return dataStructure;
}
- (int)browser:(CPBrowser)browser numberOfChildrenOfItem:(id)item
{
return item.sub.length;
}
- (id)browser:(CPBrowser)browser child:(int)index ofItem:(id)item
{
return [item.sub objectAtIndex:index];
}
- (BOOL)browser:(CPBrowser)browser isLeafItem:(id)item
{
return item.sub.length === 0;
}
- (id)browser:(CPBrowser)browser objectValueForItem:(id)item
{
return item.name || item;
}
@end
@implementation OutlineViewDataSourceDelegate : CPObject
{
id rootItem;
CPOutlineView ov @accessors;
}
- (id)init
{
self = [super init];
var path = [[CPBundle mainBundle] pathForResource:@"InitInfo.dict"],
request = [CPURLRequest requestWithURL:path],
connection = [CPURLConnection connectionWithRequest:request delegate:self];
return self;
}
- (void)connection:(CPURLConnection)connection didReceiveData:(CPString)dataString
{
if (!dataString)
return;
var data = [[CPData alloc] initWithRawString:dataString];
rootItem = [CPPropertyListSerialization propertyListFromData:data format:CPPropertyListXMLFormat_v1_0];
[ov reloadData];
}
- (int)outlineView:(CPOutlineView)theOutlineView numberOfChildrenOfItem:(id)theItem
{
if (theItem == nil)
theItem = rootItem;
if ([theItem isKindOfClass:[CPString class]])
return 0;
return [[theItem objectForKey:"Children"] count];
}
- (id)outlineView:(CPOutlineView)theOutlineView child:(int)theIndex ofItem:(id)theItem
{
if (theItem == nil)
theItem = rootItem;
return [[theItem objectForKey:"Children"] objectAtIndex:theIndex];
}
- (BOOL)outlineView:(CPOutlineView)theOutlineView isItemExpandable:(id)theItem
{
if (theItem == nil)
theItem = rootItem;
return ![theItem isKindOfClass:[CPString class]];
}
- (id)outlineView:(CPOutlineView)anOutlineView objectValueForTableColumn:(CPTableColumn)theColumn byItem:(id)theItem
{
if ([theItem isKindOfClass:[CPString class]])
return theItem;
return [theItem objectForKey:"Name"];
}
- (BOOL)outlineView:(CPOutlineView)anOutlineView writeItems:(CPArray)theItems toPasteboard:(CPPasteBoard)thePasteBoard
{
[thePasteBoard declareTypes:["TestData"] owner:self];
[thePasteBoard setData:[CPKeyedArchiver archivedDataWithRootObject:theItems] forType:"testData"];
return YES;
}
- (CPDragOperation)outlineView:(CPOutlineView)anOutlineView validateDrop:(id < CPDraggingInfo >)theInfo proposedItem:(id)theItem proposedChildIndex:(int)theIndex
{
if (theItem === nil)
[anOutlineView setDropItem:nil dropChildIndex:theIndex];
[anOutlineView setDropItem:theItem dropChildIndex:theIndex];
return CPDragOperationEvery;
}
- (BOOL)outlineView:(CPOutlineView)outlineView acceptDrop:(id < CPDraggingInfo >)theInfo item:(id)theItem childIndex:(int)theIndex
{
var alrt = [[CPAlert alloc] init];
[alrt setTitle:"Cool Features"];
[alrt setMessageText:"No reordering implemented"];
[alrt setInformativeText:"Since this is just a demo we haven't actually implement the drop behavior here."];
[alrt setAlertStyle:CPInformationalAlertStyle];
[alrt addButtonWithTitle:"Okay"];
[alrt runModal];
return NO;
}
@end
/*!
OV Node
*/
@implementation SimpleNodeData : CPObject
{
CPString name @accessors(property=name);
CPImage image @accessors(property=image);
BOOL container @accessors(property=container);
BOOL expandable @accessors(property=expandable);
BOOL selectable @accessors(property=selectable);
}
- (id)init
{
self = [super init];
name = @"Untitled";
expandable = YES;
selectable = YES;
container = YES;
return self;
}
- (id)initWithName:(CPString)aName
{
self = [self init];
name = aName;
return self;
}
+ (SimpleNodeData)nodeDataWithName:(CPString)aName
{
return [[SimpleNodeData alloc] initWithName:aName];
}
- (CPComparisonResult)compare:(id)anOther
{
// We want the data to be sorted by name, so we compare [self name] to [other name]
if ([anOther isKindOfClass:[SimpleNodeData class]])
return [name compare:[anOther name]];
return CPOrderedAscending;
}
- (CPString)description
{
return [CPString stringWithFormat:@"%@ - '%@' expandable: %d, selectable: %d, container: %d", [super description], name, expandable, selectable, container];
}
@end
@implementation SheetController : CPObject
{
CPAlert sheet @accessors;
}
- (void)beginSheet:(id)sender
{
[sheet beginSheetModalForWindow:[CPApp mainWindow]];
//[CPApp beginSheet:sheet modalForWindow:[CPApp mainWindow] modalDelegate:self didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) contextInfo:nil];
}
- (void)didEndSheet:(CPWindow)aSheet returnCode:(int)returnCode contextInfo:(id)contextInfo
{
}
@end
@implementation CPView (DemoExtras)
+ (CPMenu)defaultMenu
{
var menu = [[CPMenu alloc] init],
words = ["Views/Can have/Contextual menus/If you/Want" pathComponents];
for (var i = 0, c = [words count]; i < c; i++)
{
if (words[i] === "Contextual menus")
{
var item = [[CPMenuItem alloc] initWithTitle:words[i] action:nil keyEquivalent:nil],
sub = [[CPMenu alloc] init];
[sub addItemWithTitle:"And They" action:nil keyEquivalent:nil];
[sub addItemWithTitle:"Can Have" action:nil keyEquivalent:nil];
[sub addItemWithTitle:"Sub Menus" action:nil keyEquivalent:nil];
[item setSubmenu:sub]
[menu addItem:item];
}
else
[menu addItemWithTitle:words[i] action:nil keyEquivalent:nil];
}
return menu;
}
- (void)toggleEnabledSubviews:(id)sender
{
var subviews = [self subviews],
c = [subviews count];
while (c--)
{
var view = subviews[c];
if (view === sender)
{
[view setTitle:[view title] === "Disable All" ? "Enable All" : "Disable All"];
continue;
}
if ([view respondsToSelector:@selector(setEnabled:)])
[view setEnabled:![view isEnabled]];
}
}
@end