You need to log in to create posts and topics.
Tutorial_3
1,678 Posts
#1 · September 2, 2011, 5:16 pm
Quote from Forum Archives on September 2, 2011, 5:16 pmPosted by: svanvoorst <svanvoorst@...>
Now that we are able to open and close a window, we are going to need some controls (views) to fill it up. Part of the following has been previously published, but new controls have been added. Some controls work right out of the box; others will need to be hooked up later. Note that the NSStepper (little arrow) is connected to the display field without using a controller by using reciprocating Target/Action. It may also be used with a controller. If you want to see an image in the NSImageView, select one of your own .png images and put it in the folder with this demo. You will have to name it "myImage.png" or else change the name in the demo (two places). Un-REM the "include resources xxxx.png" line. Please note the many bezel styles for NSButton. Also note that the "grid" that they are printed in is unbalanced, ie it is not rectangular. There supposedly is more overhead with this arrangement than with NSMatrix, but the latter only does rectangular configurations and so far I have not found a way to construct it by column; it prefers construction by row. The main point that I would like to make is that many controls may be created using this common template. Once the control has been created, it must then be added as a subview of the window's contentView and may then be released.'----- start -----/*Template code:NSWhatever *ctrlName = [[NSWhatever alloc] initWithFrame:NSMakeRect( left, top, width, height )];[[window contentView] addSubview:ctrlName];S.Van Voorst {20110901}*///include resources "myImage.png"BeginCCode{NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[NSApplication sharedApplication];[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];// ****** Menubar ****** //id menubar = [[NSMenu new] autorelease];id appMenuItem = [[NSMenuItem new] autorelease];[menubar addItem:appMenuItem];[NSApp setMainMenu:menubar];id appMenu = [[NSMenu new] autorelease];id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"] autorelease];[appMenu addItem:quitMenuItem];[appMenuItem setSubmenu:appMenu];// ****** Window ******* //#define _wndW 650#define _wndH 530id window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )styleMask: NSClosableWindowMask | NSTitledWindowMaskbacking:NSBackingStoreBuffereddefer:NO];[window center];[window setTitle: @"Cocoa Controls - Programmatic" ];[window makeKeyAndOrderFront:nil];// ***** Text Field ***** //NSTextField *txtFld = [[NSTextField alloc] initWithFrame:NSMakeRect( 30, _wndH - 40, 240, 20 )];[[window contentView] addSubview:txtFld];[txtFld setStringValue:@"Welcome to Cocoa without a nib."];[txtFld release];// ***** Slider ****** //NSSlider *slider = [[NSSlider alloc] initWithFrame:NSMakeRect( 30, _wndH - 72, 240, 20 )];[[window contentView] addSubview:slider];[slider setMinValue:0];[slider setMaxValue:12];[slider setIntValue:8];[slider release];// ****** ComboBox ****** //NSComboBox *combo = [[NSComboBox alloc] initWithFrame:NSMakeRect( 30, _wndH - 102, 100, 24 )];[[window contentView] addSubview:combo];NSArray *fruit = [NSArray arrayWithObjects: @"Apples", @"Oranges", @"Peaches",nil];[combo addItemsWithObjectValues:fruit];[combo release];// ***** PopUp with Menu ****** //NSPopUpButton *pup = [[NSPopUpButton alloc] initWithFrame:NSMakeRect( 150, _wndH - 102, 120, 24 )];[[window contentView] addSubview:pup];[pup insertItemWithTitle:@"Item 1" atIndex:0];[pup insertItemWithTitle:@"Item 2" atIndex:1];[pup insertItemWithTitle:@"Item 3" atIndex:2];[pup release];// ****** ImageView ****** //NSImageView *imageView = [[NSImageView alloc] initWithFrame:NSMakeRect( 290, _wndH - 110, 100, 100 )];[[window contentView] addSubview:imageView];NSImage *image = [NSImage imageNamed:@"myImage.png"];[imageView setImageScaling:NSScaleToFit];[imageView setImage:image];[imageView release];// ****** NSTextView with Scroll ****** //NSScrollView *scrlView = [[NSScrollView alloc] initWithFrame:NSMakeRect( 410, _wndH - 140, 180, 120 )];[[window contentView] addSubview:scrlView];[scrlView setHasVerticalScroller: YES];NSTextView *txtView = [[NSTextView alloc] initWithFrame:NSMakeRect( 250, _wndH - 260, 136, 100 )];[scrlView setDocumentView: txtView];[txtView insertText:@"Enter text - Right click for menu."];[scrlView release];[txtView release];// ****** ColorWell ****** //NSColorWell *well = [[NSColorWell alloc] initWithFrame:NSMakeRect( 30, _wndH - 150, 60, 30 )];[[window contentView] addSubview:well];[well release];// ****** Date Picker ****** //NSDatePicker *datePick = [[NSDatePicker alloc] initWithFrame:NSMakeRect( 120, _wndH - 145, 200, 24 )];[[window contentView] addSubview:datePick];[datePick release];// ****** Level Indicator ***** //NSLevelIndicator *level = [[NSLevelIndicator alloc] initWithFrame:NSMakeRect( 30, _wndH - 180, 200, 18 )];[[window contentView] addSubview:level];[[level cell] setLevelIndicatorStyle: NSContinuousCapacityLevelIndicatorStyle];[level setMinValue:0];[level setMaxValue:12];[level setIntValue:6];[level release];/*NSRelevancyLevelIndicatorStyle, NSContinuousCapacityLevelIndicatorStyle,NSDiscreteCapacityLevelIndicatorStyle, NSRatingLevelIndicatorStyle*/// ***** Progress Indicator ***** //NSProgressIndicator *progress = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect( 30, _wndH - 220, 200, 20 )];[[window contentView] addSubview:progress];[progress release];// ****** Segmented Control ****** //NSSegmentedControl *seg = [[NSSegmentedControl alloc] initWithFrame:NSMakeRect( 30, _wndH - 260, 120, 24 )];[[window contentView] addSubview:seg];[seg release];[seg setSegmentCount:2];[seg setLabel:@"First" forSegment:0];[seg setLabel:@"Second" forSegment:1];// ****** Arrw/ValueFld ****** //NSStepper *arrw = [[NSStepper alloc] initWithFrame:NSMakeRect( 160, _wndH - 260, 22, 26 )];[[window contentView] addSubview:arrw];[arrw release];[arrw setContinuous:YES];[arrw setAutorepeat:YES];[arrw setValueWraps:NO];[arrw setMaxValue:100];[arrw setMinValue:0];[arrw setIntValue:60];NSTextField *valueFld = [[NSTextField alloc] initWithFrame:NSMakeRect( 180, _wndH - 257, 50, 20 )];[[window contentView] addSubview:valueFld];[valueFld release];[valueFld setAlignment:NSCenterTextAlignment];[valueFld takeStringValueFrom:arrw];// Make each target of the other[arrw setTarget:valueFld];[arrw setAction:@selector(takeIntValueFrom:)];[valueFld setTarget:arrw];[valueFld setAction:@selector(takeIntValueFrom:)];// ***** NSTabView ******* //NSTabView *tab = [[NSTabView alloc] initWithFrame:NSMakeRect( 340, _wndH - 260, 280, 100 )];NSTabViewItem* item1=[[NSTabViewItem alloc] initWithIdentifier:@"1"];[item1 setLabel:@"One"];[tab addTabViewItem:item1];[item1 release];NSTabViewItem* item2=[[NSTabViewItem alloc] initWithIdentifier:@"2"];[item2 setLabel:@"Two"];[tab addTabViewItem:item2];[item2 release];NSTabViewItem* item3=[[NSTabViewItem alloc] initWithIdentifier:@"3"];[item3 setLabel:@"Three"];[tab addTabViewItem:item3];[item3 release];[[window contentView] addSubview:tab];[tab release];// ***** NSMatrix ***** //int n;NSButtonCell *cell = [[NSButtonCell alloc]init];[cell setButtonType:NSRadioButton];// Width/height not critical if sizeToCells is used!id matrix = [[NSMatrix alloc] initWithFrame:NSMakeRect( 260, _wndH - 270, 100, 100 )mode:NSRadioModeMatrixprototype:cellnumberOfRows:3numberOfColumns:2];[matrix setCellSize:NSMakeSize( 34, 34 )];[matrix setIntercellSpacing: NSMakeSize( 3, 3 )];[matrix sizeToCells];NSArray *cellArray = [matrix cells];for ( n = 0; n <= 5; n++ ){[[cellArray objectAtIndex: n ] setTitle:[NSString stringWithFormat:@"%d", n+1] ];[[cellArray objectAtIndex: n ] setTag: n + 1 ];}[[window contentView] addSubview:matrix];[cell release];[matrix release];// ***** NSButton Types ****** //#define _top _wndH - 320#define _left 30#define _cellW 190#define _cellH 40#define _vg 10 //vertical gutter#define _hg 6NSButton *btn[14];NSInteger count, row, col;NSRect btnR;count = 1;for ( row = 0 ; row <= 4; row++ ){for ( col = 0; col <= 2; col++ ){if( row == 4 && col == 2 ) break; //Creates unbalanced gridbtnR = NSMakeRect( _left + (_cellW+_vg)*col, _top - (_cellH+_hg)*row, _cellW, _cellH );btn[count] = [[NSButton alloc]initWithFrame:btnR];[btn[count] setBezelStyle:count ];switch (count){case 1: [btn[count] setTitle: @"NSRoundedBezelStyle" ]; break;case 2: [btn[count] setTitle: @"NSRegularSquareBezelStyle" ]; break;case 3: [btn[count] setTitle: @"NSThickSquareBezelStyle" ]; break;case 4: [btn[count] setTitle: @"NSThickerSquareBezelStyle" ]; break;case 5: [btn[count] setTitle: @"" ]; break; // NSDisclosureBezelStylecase 6: [btn[count] setTitle: @"NSShadowlessSquareBezelStyle" ]; break;case 7: [btn[count] setTitle: @"" ]; break; // NSCircularBezelStylecase 8: [btn[count] setTitle: @"NSTexturedSquareBezelStyle" ]; break;case 9: [btn[count] setTitle: @"" ]; break; // NSHelpButtonBezelStylecase 10: [btn[count] setTitle: @"NSSmallSquareBezelStyle" ]; break;case 11: [btn[count] setTitle: @"NSTexturedRoundedBezelStyle" ]; break;case 12: [btn[count] setTitle: @"NSRoundRectBezelStyle" ]; break;case 13: [btn[count] setTitle: @"NSRecessedBezelStyle" ]; break;case 14: [btn[count] setTitle: @"" ]; break; // NSRoundedDisclosureBezelStyle}[[window contentView] addSubview: btn[count]];[btn[count] release];count++;}}[NSApp activateIgnoringOtherApps:YES];[NSApp run];[pool drain];}EndC'----- end ------Steve Van Voorst
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
Posted by: svanvoorst <svanvoorst@...>
Now that we are able to open and close a window, we are going to need some controls (views) to fill it up. Part of the following has been previously published, but new controls have been added. Some controls work right out of the box; others will need to be hooked up later. Note that the NSStepper (little arrow) is connected to the display field without using a controller by using reciprocating Target/Action. It may also be used with a controller. If you want to see an image in the NSImageView, select one of your own .png images and put it in the folder with this demo. You will have to name it "myImage.png" or else change the name in the demo (two places). Un-REM the "include resources xxxx.png" line. Please note the many bezel styles for NSButton. Also note that the "grid" that they are printed in is unbalanced, ie it is not rectangular. There supposedly is more overhead with this arrangement than with NSMatrix, but the latter only does rectangular configurations and so far I have not found a way to construct it by column; it prefers construction by row. The main point that I would like to make is that many controls may be created using this common template. Once the control has been created, it must then be added as a subview of the window's contentView and may then be released.
'----- start -----
/*
Template code:
NSWhatever *ctrlName = [[NSWhatever alloc] initWithFrame:NSMakeRect( left, top, width, height )];
[[window contentView] addSubview:ctrlName];
S.Van Voorst {20110901}
*/
//include resources "myImage.png"
BeginCCode
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// ****** Menubar ****** //
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
// ****** Window ******* //
#define _wndW 650
#define _wndH 530
id window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSClosableWindowMask | NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window center];
[window setTitle: @"Cocoa Controls - Programmatic" ];
[window makeKeyAndOrderFront:nil];
// ***** Text Field ***** //
NSTextField *txtFld = [[NSTextField alloc] initWithFrame:NSMakeRect( 30, _wndH - 40, 240, 20 )];
[[window contentView] addSubview:txtFld];
[txtFld setStringValue:@"Welcome to Cocoa without a nib."];
[txtFld release];
// ***** Slider ****** //
NSSlider *slider = [[NSSlider alloc] initWithFrame:NSMakeRect( 30, _wndH - 72, 240, 20 )];
[[window contentView] addSubview:slider];
[slider setMinValue:0];
[slider setMaxValue:12];
[slider setIntValue:8];
[slider release];
// ****** ComboBox ****** //
NSComboBox *combo = [[NSComboBox alloc] initWithFrame:NSMakeRect( 30, _wndH - 102, 100, 24 )];
[[window contentView] addSubview:combo];
NSArray *fruit = [NSArray arrayWithObjects: @"Apples", @"Oranges", @"Peaches",nil];
[combo addItemsWithObjectValues:fruit];
[combo release];
// ***** PopUp with Menu ****** //
NSPopUpButton *pup = [[NSPopUpButton alloc] initWithFrame:NSMakeRect( 150, _wndH - 102, 120, 24 )];
[[window contentView] addSubview:pup];
[pup insertItemWithTitle:@"Item 1" atIndex:0];
[pup insertItemWithTitle:@"Item 2" atIndex:1];
[pup insertItemWithTitle:@"Item 3" atIndex:2];
[pup release];
// ****** ImageView ****** //
NSImageView *imageView = [[NSImageView alloc] initWithFrame:NSMakeRect( 290, _wndH - 110, 100, 100 )];
[[window contentView] addSubview:imageView];
NSImage *image = [NSImage imageNamed:@"myImage.png"];
[imageView setImageScaling:NSScaleToFit];
[imageView setImage:image];
[imageView release];
// ****** NSTextView with Scroll ****** //
NSScrollView *scrlView = [[NSScrollView alloc] initWithFrame:NSMakeRect( 410, _wndH - 140, 180, 120 )];
[[window contentView] addSubview:scrlView];
[scrlView setHasVerticalScroller: YES];
NSTextView *txtView = [[NSTextView alloc] initWithFrame:NSMakeRect( 250, _wndH - 260, 136, 100 )];
[scrlView setDocumentView: txtView];
[txtView insertText:@"Enter text - Right click for menu."];
[scrlView release];
[txtView release];
// ****** ColorWell ****** //
NSColorWell *well = [[NSColorWell alloc] initWithFrame:NSMakeRect( 30, _wndH - 150, 60, 30 )];
[[window contentView] addSubview:well];
[well release];
// ****** Date Picker ****** //
NSDatePicker *datePick = [[NSDatePicker alloc] initWithFrame:NSMakeRect( 120, _wndH - 145, 200, 24 )];
[[window contentView] addSubview:datePick];
[datePick release];
// ****** Level Indicator ***** //
NSLevelIndicator *level = [[NSLevelIndicator alloc] initWithFrame:NSMakeRect( 30, _wndH - 180, 200, 18 )];
[[window contentView] addSubview:level];
[[level cell] setLevelIndicatorStyle: NSContinuousCapacityLevelIndicatorStyle];
[level setMinValue:0];
[level setMaxValue:12];
[level setIntValue:6];
[level release];
/*
NSRelevancyLevelIndicatorStyle, NSContinuousCapacityLevelIndicatorStyle,
NSDiscreteCapacityLevelIndicatorStyle, NSRatingLevelIndicatorStyle
*/
// ***** Progress Indicator ***** //
NSProgressIndicator *progress = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect( 30, _wndH - 220, 200, 20 )];
[[window contentView] addSubview:progress];
[progress release];
// ****** Segmented Control ****** //
NSSegmentedControl *seg = [[NSSegmentedControl alloc] initWithFrame:NSMakeRect( 30, _wndH - 260, 120, 24 )];
[[window contentView] addSubview:seg];
[seg release];
[seg setSegmentCount:2];
[seg setLabel:@"First" forSegment:0];
[seg setLabel:@"Second" forSegment:1];
// ****** Arrw/ValueFld ****** //
NSStepper *arrw = [[NSStepper alloc] initWithFrame:NSMakeRect( 160, _wndH - 260, 22, 26 )];
[[window contentView] addSubview:arrw];
[arrw release];
[arrw setContinuous:YES];
[arrw setAutorepeat:YES];
[arrw setValueWraps:NO];
[arrw setMaxValue:100];
[arrw setMinValue:0];
[arrw setIntValue:60];
NSTextField *valueFld = [[NSTextField alloc] initWithFrame:NSMakeRect( 180, _wndH - 257, 50, 20 )];
[[window contentView] addSubview:valueFld];
[valueFld release];
[valueFld setAlignment:NSCenterTextAlignment];
[valueFld takeStringValueFrom:arrw];
// Make each target of the other
[arrw setTarget:valueFld];
[arrw setAction:@selector(takeIntValueFrom:)];
[valueFld setTarget:arrw];
[valueFld setAction:@selector(takeIntValueFrom:)];
// ***** NSTabView ******* //
NSTabView *tab = [[NSTabView alloc] initWithFrame:NSMakeRect( 340, _wndH - 260, 280, 100 )];
NSTabViewItem* item1=[[NSTabViewItem alloc] initWithIdentifier:@"1"];
[item1 setLabel:@"One"];
[tab addTabViewItem:item1];
[item1 release];
NSTabViewItem* item2=[[NSTabViewItem alloc] initWithIdentifier:@"2"];
[item2 setLabel:@"Two"];
[tab addTabViewItem:item2];
[item2 release];
NSTabViewItem* item3=[[NSTabViewItem alloc] initWithIdentifier:@"3"];
[item3 setLabel:@"Three"];
[tab addTabViewItem:item3];
[item3 release];
[[window contentView] addSubview:tab];
[tab release];
// ***** NSMatrix ***** //
int n;
NSButtonCell *cell = [[NSButtonCell alloc]init];
[cell setButtonType:NSRadioButton];
// Width/height not critical if sizeToCells is used!
id matrix = [[NSMatrix alloc] initWithFrame:NSMakeRect( 260, _wndH - 270, 100, 100 )
mode:NSRadioModeMatrix
prototype:cell
numberOfRows:3
numberOfColumns:2];
[matrix setCellSize:NSMakeSize( 34, 34 )];
[matrix setIntercellSpacing: NSMakeSize( 3, 3 )];
[matrix sizeToCells];
NSArray *cellArray = [matrix cells];
for ( n = 0; n <= 5; n++ )
{
[[cellArray objectAtIndex: n ] setTitle:[NSString stringWithFormat:@"%d", n+1] ];
[[cellArray objectAtIndex: n ] setTag: n + 1 ];
}
[[window contentView] addSubview:matrix];
[cell release];
[matrix release];
// ***** NSButton Types ****** //
#define _top _wndH - 320
#define _left 30
#define _cellW 190
#define _cellH 40
#define _vg 10 //vertical gutter
#define _hg 6
NSButton *btn[14];
NSInteger count, row, col;
NSRect btnR;
count = 1;
for ( row = 0 ; row <= 4; row++ )
{
for ( col = 0; col <= 2; col++ )
{
if( row == 4 && col == 2 ) break; //Creates unbalanced grid
btnR = NSMakeRect( _left + (_cellW+_vg)*col, _top - (_cellH+_hg)*row, _cellW, _cellH );
btn[count] = [[NSButton alloc]initWithFrame:btnR];
[btn[count] setBezelStyle:count ];
switch (count)
{
case 1: [btn[count] setTitle: @"NSRoundedBezelStyle" ]; break;
case 2: [btn[count] setTitle: @"NSRegularSquareBezelStyle" ]; break;
case 3: [btn[count] setTitle: @"NSThickSquareBezelStyle" ]; break;
case 4: [btn[count] setTitle: @"NSThickerSquareBezelStyle" ]; break;
case 5: [btn[count] setTitle: @"" ]; break; // NSDisclosureBezelStyle
case 6: [btn[count] setTitle: @"NSShadowlessSquareBezelStyle" ]; break;
case 7: [btn[count] setTitle: @"" ]; break; // NSCircularBezelStyle
case 8: [btn[count] setTitle: @"NSTexturedSquareBezelStyle" ]; break;
case 9: [btn[count] setTitle: @"" ]; break; // NSHelpButtonBezelStyle
case 10: [btn[count] setTitle: @"NSSmallSquareBezelStyle" ]; break;
case 11: [btn[count] setTitle: @"NSTexturedRoundedBezelStyle" ]; break;
case 12: [btn[count] setTitle: @"NSRoundRectBezelStyle" ]; break;
case 13: [btn[count] setTitle: @"NSRecessedBezelStyle" ]; break;
case 14: [btn[count] setTitle: @"" ]; break; // NSRoundedDisclosureBezelStyle
}
[[window contentView] addSubview: btn[count]];
[btn[count] release];
count++;
}
}
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
[pool drain];
}
EndC
'----- end ------
Steve Van Voorst
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
Click for thumbs down.0Click for thumbs up.0