// Text of project QWERTY written on 11/20/95 at 3:34 PM
// Beginning of text file Project Data
/*
**      Newton Developer Technical Support Sample Code
**
**      QWERTY, Keyboard sample, no add-to-dictionary.
**
**      by J. Christopher Bell & Maurice Sharp, Newton Developer Technical Support
**
**      Copyright  1993-1995 by Apple Computer, Inc.  All rights reserved.
**
**      You may incorporate this sample code into your applications without
**      restriction.  This sample code has been provided "AS IS" and the
**      responsibility for its operation is 100% yours.  You are not
**      permitted to modify and redistribute the source as "DTS Sample Code."
**      If you are going to re-distribute the source, we require that you
**      make it clear in the source that the code was descended from
**      Apple-provided sample code, but that you've made changes.
*/



// application constants
constant kAppMaxWidth := 240 ;
constant kAppMaxHeight := 336 ;

// slot for the keyboard in the root view
constant kMyKeyboardSymbol := '|myAlphaKeyboard:PIEDTS|;

// grab the pictures for the keys
r := OpenResFileX(HOME & "pictures") ;
kbdReturnBitmap := GetPictAsBits("KeyboardReturn", nil);
kbdTabBitmap := GetPictAsBits("KeyboardTab", nil);
kbdLeftBitmap := GetPictAsBits("KeyboardLeftArrow", nil);
kbdRightBitmap := GetPictAsBits("KeyboardRightArrow", nil);
CloseResFileX(r);


// some constants for key values
constant kOneKey := 18 ;
constant kQKey := 12 ;

constant kQNormal := $q ;
constant kQShifted := $Q ;
constant kQOption := $\u0153 ;
constant kQOptionShift := $\u0152 ;
constant kOneOption := $\A1 ;
constant kOneOptionShift := $\u2044 ;

constant kDeleteKey := 51 ;
constant kTabKey := 48 ;
constant kReturnKey := 36 ;
constant kShiftKey := 56 ;
constant kCapsLockKey := 57 ;
constant kOptionKey := 58 ;
constant kSpaceKey := 49 ;
constant kLeftArrowKey := 0x7B ;
constant kRightArrowKey := 0x7C ;


// the keyboard row definitions
row0 := [	keyVUnit, keyVUnit,
				nil, 50, 	keyHHalf + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 18,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
		 		nil, 19,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 20,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 21,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 23,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 22,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 26,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 28,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 25,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 29,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 27,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 24,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
		  		"del", kDeleteKey,	keyHUnit + keyHHalf + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3
			];

row1 := [	keyVUnit, keyVUnit,
				kbdTabBitmap, kTabKey,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 12,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 13,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
		 		nil, 14,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 15,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 17, 	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 16,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 32,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 34, 	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 31,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 35,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 33,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 30, 	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 42,	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3
			];

row2 := [	keyVUnit, keyVUnit,
				"caps", kCapsLockKey, 	keyHUnit + keyHHalf + keyVUnit + keyFramed + keyRoundingUnit*3,
				nil, 0, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 1, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
		 		nil, 2, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 3, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 5, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 4, 		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 38,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 40,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 37,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 41,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 39,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				kbdReturnBitmap,
				     kReturnKey,		keyHUnit + keyHHalf + keyVUnit + keyAutoHilite + keyFramed + keyRoundingUnit*3
			];

row3 := [	keyVUnit, keyVUnit,
				"shift", kShiftKey,	2*keyHUnit + keyVUnit + keyFramed + keyRoundingUnit*3,
				nil, 6,			keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 7, 	 	keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
		 		nil, 8,			keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 9,			keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 11,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 45,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 46,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 43,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 47,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				nil, 44,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				"shift", kShiftKey,	2*keyHUnit + keyVUnit + keyFramed + keyRoundingUnit*3
			];

row4 := [	keyVUnit, keyVUnit,
				nil, nil,		1*keyHUnit + keyHHalf + keyHQuarter + keyVUnit + keySpacer,
				"option", kOptionKey,	2*keyHUnit + keyHHalf + keyVUnit + keyFramed + keyRoundingUnit*3,
				nil, kSpaceKey,		6*keyHUnit + keyHQuarter + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				kbdLeftBitmap,
				     kLeftArrowKey,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
				kbdRightBitmap,
				     kRightArrowKey,		keyHUnit + keyVUnit + keyFramed + keyAutoHilite + keyRoundingUnit*3,
			];
// End of text file Project Data
// Beginning of file myASCIIKb

// Before Script for "kb"
// Copyright 1993-1994 Apple Computer, Inc. All rights reserved.


kb :=
    {viewBounds: {top: 211, left: 0, right: 226, bottom: 295},
     debug: "kb",
     _proto: @187
    };

thePad :=
    {keyDefinitions: [ row0, row1, row2, row3, row4 ],
     viewJustify: 240,
     capsDownP:
       // returns true if the caps lock key is down
       func()
       begin
          if :optionDownP() then
             (KeyIn(kOneKey, nil) = kOneOption) and
                (KeyIn(kQKey, nil) = kQOptionShift) ;
          else
             (KeyIn(kOneKey, nil) = $1) and (KeyIn(kQKey, nil) = $Q) ;
       end,
     optionDownP:
       // returns true of the option key is down
       func()
       begin
          local key := KeyIn(kOneKey, nil) ;
          (key = kOneOption) or (key = kOneOptionShift) ;
       end,
     shiftDownP:
       // returns true if the shift key is down
       func()
       begin
          if :optionDownP() then
             (KeyIn(kOneKey, nil) = kOneOptionShift)
          else
             (KeyIn(kOneKey, nil) = $!);
       end,
     keyPressScript:
       func(key)
       begin
          local shiftDown := :shiftDownP() ;
          local capsDown := :capsDownP() ;
          local optionDown := :optionDownP() ;
          local keyChar := $\00 ;
          local keyView := GetView('viewFrontKey) ;
       
          if key = kShiftKey then
          begin
             KeyIn(kShiftKey, (not shiftDown)) ;
             if capsDown then
                KeyIn(kCapsLockKey, nil) ;
             :Dirty();
          end else
             if key = kCapsLockKey then
             begin
                KeyIn(kCapsLockKey, true) ;
                if shiftDown then
                   KeyIn(kShiftKey, nil) ;
                :Dirty();
             end
          else
             if key = kOptionKey then
             begin
                KeyIn(kOptionKey, (not optionDown));
                :Dirty() ;
             end
          else begin
             keyChar := KeyIn(key, true) ;
             KeyIn(key, nil) ;
       
             if shiftDown then
             begin
                KeyIn(kShiftKey, nil) ;
                :Dirty() ;
             end ;
             if optionDown then
             begin
                KeyIn(kOptionKey, nil) ;
                :Dirty() ;
             end ;
          end ;
          print(keyChar) ;
          if keyView then
             PostKeyString(keyView, "" & keyChar) ;
       end,
     viewBounds: {left: 0, top: 0, right: 0, bottom: 0},
     debug: "thePad",
     _proto: @188
    };
AddStepForm(kb, thePad);



_view000 := {_proto: @166};
AddStepForm(kb, _view000);



// After Script for "kb"
thisView := kb;
//Set the saveBounds to the viewBounds since this is probably what you want
thisView.saveBounds := Clone(thisView.viewBounds);


constant |layout_myASCIIKb| := kb;
// End of file myASCIIKb
// Beginning of file main.t

// Before Script for "myBase"
// Copyright 1993-1995 Apple Computer, Inc. All rights reserved.


myBase :=
    {title: "Simple QWERTY Keyboard",
     viewBounds: {left: 0, top: 2, right: 236, bottom: 270},
     viewSetupFormScript:
       func()
       begin
          a := GetAppParams();
          self.viewBounds := RelBounds(a.appAreaLeft, a.appAreaTop,
             MIN(a.appAreaWidth, kAppMaxWidth),
             MIN(a.appAreaHeight, kAppMaxHeight)) ;
       
       
          GetRoot().(EnsureInternal(kMyKeyboardSymbol)) := BuildContext(GetLayout("myAsciiKb")) ;
       end,
     viewQuitScript:
       func()
       begin
       	if getroot().(kMyKeyboardSymbol) exists and getview(getroot().(kMyKeyboardSymbol)) then
       		getroot().(kMyKeyboardSymbol):Close();
          RemoveSlot(GetRoot(), kMyKeyboardSymbol) ;
       end,
     debug: "myBase",
     _proto: @157
    };

ourPara :=
    {viewFlags: 7681,
     viewFormat: 12625,
     viewLineSpacing: 20,
     viewFont: 18434,
     viewBounds: {top: 30, left: 5, right: -5, bottom: 121},
     viewSetupFormScript:
       func()
       begin
          SetKeyView(self, 0);
          
          // now double-taps in this view will open OUR keyboard!
          self._keyboard := getroot().(kMyKeyboardSymbol);
       end,
     viewJustify: 48,
     debug: "ourPara",
     viewClass: 81
    };
AddStepForm(myBase, ourPara);



OpenIt_Button :=
    {text: "Open QWERTY Keyboard",
     buttonClickScript:
       func()
       begin
          getroot().(kMyKeyboardSymbol):Toggle();
       end,
     viewBounds: {left: 0, top: 186, right: 168, bottom: 206},
     viewJustify: 8388630,
     debug: "OpenIt_Button",
     _proto: @226
    };
AddStepForm(myBase, OpenIt_Button);



label :=
    {text: "Paragraph with custom keyboard",
     viewBounds: {left: 16, top: 128, right: 224, bottom: 152},
     viewJustify: 8388610,
     debug: "label",
     _proto: @218
    };
AddStepForm(myBase, label);




constant |layout_main.t| := myBase;
// End of file main.t



