%
% This file is a product of Sun Microsystems, Inc. and is provided for
% unrestricted use provided that this legend is included on all tape
% media and as a part of the software program in whole or part.  Users
% may copy or modify this file without charge, but are not authorized to
% license or distribute it to anyone else except as part of a product
% or program developed by the user.
% 
% THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
% WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
% PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
% 
% This file is provided with no support and without any obligation on the
% part of Sun Microsystems, Inc. to assist in its use, correction,
% modification or enhancement.
% 
% SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
% INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
% OR ANY PART THEREOF.
% 
% In no event will Sun Microsystems, Inc. be liable for any lost revenue
% or profits or other special, indirect and consequential damages, even
% if Sun has been advised of the possibility of such damages.
% 
% Sun Microsystems, Inc.
% 2550 Garcia Avenue
% Mountain View, California  94043
%
%
%	nterm.ps 1.5 88/02/10
%
%	Copyright (c) 1987 by Sun Microsystems, Inc.
%	Steve Isaac 12/18/87
%
% --- This file is part of the ps_initialize cdef routine in NeWS.cps
%

% --- Put everything in the userdict so that we will be able to reference things (ie.
%      the TextWindow and the TextCanvas) from callback routines
userdict begin
   % --- Global variables
   /Win			null		def  % the TextWindow
   /Text		null		def  % the TextCanvas
   /Can			null		def  % the canvas

   % --- Utility routines 
   /DoStuff { % - => -
   % --- Send the current primary selection to input
     10 dict begin
         /CurrentSelection null def
         /SelDict /PrimarySelection getselection def
         SelDict null ne {
            SelDict /ContentsAscii known {
                  /CurrentSelection SelDict /ContentsAscii get def
            } if
         } if
         CurrentSelection null ne {    
            createevent dup begin 
               /Name /InsertValue def
               /Action CurrentSelection def
               /Canvas Can def
            end sendevent   
         } if
     end
   } def

   /PutNeWS { % - => -
   % --- Put the primary selection onto the NeWS shelf
     10 dict begin
         /SelDict /PrimarySelection getselection def
         SelDict null ne {
            /SelDict1 SelDict length dict def
            SelDict SelDict1 copy
            SelDict1 /ShelfSelection setselection 
         } if
     end
   } def

   /GetNeWS { % - => -
   % --- Send the NeWS shelf selection to input
     10 dict begin
         /CurrentSelection null def
         /SelDict /ShelfSelection getselection def
         SelDict null ne {
            SelDict /ContentsAscii known {
                  /CurrentSelection SelDict /ContentsAscii get def
            } if
         } if
         CurrentSelection null ne {    
            createevent dup begin 
               /Name /InsertValue def
               /Action CurrentSelection def
               /Canvas Can def
            end sendevent   
          } if
     end
   } def

   /PutSunView { % - => -
   % --- Put the primary selection onto the SunView shelf
     (news_selection -1 >/tmp/winselection;set_selection 3 /tmp/winselection)
        forkunix
   } def

   /GetSunView { % - => -
   % --- Send the SunView shelf selection to input
   10 dict begin
      % --- Note: we use SelectionBuffer0 as a temporary holding place
      /SelectionBuffer0 clearselection
      (get_selection | news_selection -s -b0) forkunix
      % --- Wait for SelectionBuffer0 to be filled.
      { 
         /SelDict /SelectionBuffer0 getselection def
         SelDict null ne {
            exit
         } if 
         1 240 div sleep
      } loop
      /CurrentSelection null def
      SelDict /ContentsAscii known {
         /CurrentSelection SelDict /ContentsAscii get def
      } if
      CurrentSelection null ne {    
         createevent dup begin 
            /Name /InsertValue def
            /Action CurrentSelection def
            /Canvas Can def
         end sendevent   
      } if
   end
   } def

   % --- Begin Initialization
   /MyWindow NeWSWindow []
   classbegin
      % Isolate ClientMenu from other instances of DefaultWindow
      /ClientMenu	null	def
      /destroy { % - => -
         /destroy Text send
         T_EXIT tagprint
         /destroy super send
      } def
   classend def
   /Win framebuffer /new MyWindow send def
   { /IconLabel OriginatingHost def
     /FrameLabel TitleStripe
        OriginatingHost localhostname ne {
           ( on ) append OriginatingHost append
        } if
     def
     /IconImage /shell_scroll def
   } Win send
   % --- Need to do the reshape before adding scrollbars
   /reshapefromuser Win send
   /addVscroll Win send
   /addHscroll Win send 
   /HScrollbar Win /HScrollbar get def
   /VScrollbar Win /VScrollbar get def
   /Can Win /ClientCanvas get def
   /Text TextCanvasSize Can /new TextCanvas send def
   % --- Turn the text caret on here so /changefont will work correctly
   /oncaret Text send

   % --- Set defaults from UserProfile
   UserProfile /NtermCaretColor known {
      UserProfile /NtermCaretColor get dup 
      type /colortype eq {
         /setcaretcolor Text send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermCaretBlink known {
      UserProfile /NtermCaretBlink get dup 
      type dup /integertype eq exch /realtype eq or {
         null /setcaretblink Text send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermCaretDuty known {
      UserProfile /NtermCaretDuty get dup 
      type /realtype eq {
         null exch /setcaretblink Text send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermCaretShape known {
      UserProfile /NtermCaretShape get dup 
      type /nametype eq {
         /setcaretshape Text send pop
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermTextColor known {
      UserProfile /NtermTextColor get dup 
      type /colortype eq {
         dup
         /setfgcolor Text send
         /setfgcolor Win send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermFillColor known {
      UserProfile /NtermFillColor get dup 
      type /colortype eq {
         dup
         /setbgcolor Text send
         /setbgcolor Win send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermFontName known {
      UserProfile /NtermFontName get dup 
      type /nametype eq {
         null /changefont Text send
      }{
         pop
      } ifelse
   } if
   UserProfile /NtermFontHeight known {
      UserProfile /NtermFontHeight get dup 
      type /integertype eq {
         null exch /changefont Text send
      }{
         pop
      } ifelse
   } if

   % --- Hook the different object together via callback routines
   %     This must be done after the defaults so that we don't generate unwanted
   %     callbacks
   { /PaintClient {/fixdamage Text send} def } Win send
   { /ReshapeClient {/reshape Text send} def } Win send
   { /NotifyUser {
        null ItemValue /moveviewport Text send
     } def 
   } VScrollbar send
   { /ClientDrag {
        DoScroll null ItemValue /moveviewport Text send
     } def 
   } VScrollbar send
   { /NotifyUser {
        ItemValue null /moveviewport Text send
     } def 
   } HScrollbar send
   { /ClientDrag {
        DoScroll ItemValue null /moveviewport Text send
     } def 
   } HScrollbar send 
   { /KeyHitCallback { % key => -
         T_KEYHIT tagprint typedprint
     } def 
   } Text send
   { /InsertValueCallback { % string => -
        dup
        T_INSERTSELLENGTH tagprint length typedprint 
        T_INSERTSELSTRING tagprint typedprint 
     } def 
   } Text send
   { /ResizeCallback { % - => -
        getviewportsize T_SIZECHANGED tagprint 
        typedprint typedprint typedprint typedprint 
     } def 
   } Text send

   % --- Create menus     
   /colorsquare { % color keyword => -
      /paint eq
         {20 20 rect setcolor fill}
         {pop 20 20} ifelse
   } def

   /SetColor { % color => -
      /currentindex colorsmenu send 
      {
       0 { dup
           /setfgcolor Win send
           /setfgcolor Text send }
       1 { dup
           /setbgcolor Win send
           /setbgcolor Text send }
       2 { /setcaretcolor Text send}
      } case
      /paint Win send
   } def

    /shelfmenu [
        (Get)		{GetNeWS}
        (Put)		{PutNeWS}
        (Get SunView)	{GetSunView}
        (Put SunView)	{PutSunView}
    ] /new DefaultMenu send def
        
    /colormenu
        [1 dup dup rgbcolor [exch {colorsquare}] 
         .925 dup dup rgbcolor [exch {colorsquare}] 
         .875 .125 neg .250 { dup dup rgbcolor [exch {colorsquare}] } for
         ColorDict { exch pop [exch {colorsquare}] } forall]
        [{currentkey 0 get SetColor}]
	/new DefaultMenu send 
    def
    {   /LayoutStyle	[7 ColorDict length 1 index div ceiling exch 1 add] def
        /StrokeSelection? true def
        /CellHorizGap	2 def
        /CellVertGap	2 def
        /RetainCanvas?	true def
    } colormenu send

    /colorsmenu [
	(Text Color =>)	colormenu
	(Fill Color =>)	colormenu
	(Caret Color =>) colormenu
    ] /new DefaultMenu send def

    /FntPts 14 def
    /fontkey {findfont FntPts scalefont} def
    /fontaction {null /changefont Text send} def
    /fontmenu [
	[(Screen)		/Screen			fontkey]
	[(..Bold)		/Screen-Bold		fontkey]
	[()			/Screen-Bold		fontkey]
	[()			/Screen-Bold		fontkey]
	[(Courier)		/Courier		fontkey]
	[(..Bold)		/Courier-Bold		fontkey]
	[(..Oblique)		/Courier-Oblique	fontkey]
	[(..BoldOblique)	/Courier-BoldOblique	fontkey]
    ] [{currentkey 1 get /FontName get fontaction}] /new DefaultMenu send def
    {   /LayoutStyle	[4 2] def
        /RowMajor?	false def
        /CenterItems?	false def
    } fontmenu send

    /pointsizemenu
        [(6) (8) (10) (12) (14) (16) (18) (24) (32) (64)]
        [{null currentkey cvi /changefont Text send}]
    /new DefaultMenu send def
    {   /LayoutStyle	[2 5] def
        /CellHorizGap	5 def
    } pointsizemenu send

    { /ClientMenu [
		(Stuff)			{DoStuff}
		(Shelf)			shelfmenu
		(Points)		pointsizemenu
		(Fonts)			fontmenu
		(Colors)		colorsmenu
                /sun30			FrameMenu
      ] /new DefaultMenu send store
      {   /LayoutStyle	/Horizontal def
          /PullRightDelta	0 def
          /Border		2 def
          /CenterItems?		false def
          /PullRightDelta	0 def
      } ClientMenu send
    } Win send

   % --- The user can now start typing; let him know by showing the window.
   % --- Note that this must be done after the client menu is set up, since
   %     the window frame event manager (which handles the menu) is invoked here
   /map Win send 

   % --- Paint the entire window to ensure all color changes are visible
   /paint Win send
end % userdict begin

   % --- Typedprint constants
   /T_INITIALIZE 0 def
   /T_CHANGEFONT 1 def
   /T_GETSCREENSIZE 2 def
   /T_GETCURSORPOS 3 def
   /T_SETCURSORSHAPE 4 def
   /T_SIZECHANGED 5 def
   /T_KEYHIT 6 def
   /T_INSERTSELSTRING 7 def
   /T_INSERTSELLENGTH 8 def
   /T_GETLINELENGTH 9 def
   /T_EXIT 10 def

   % TextCanvas client interface routine definitions     
   /A { % ps_changefont(font, height, successfull => T_CHANGEFONT(successfull)
      /changefont Text send
   } def
   /B { % ps_writelines(insertflag,x,y) 
      /writelines Text send
   } def
   /C { % ps_writeatcaret()
      /writeatcaret Text send
   } def
   /E { % ps_deletestring(length, x, y)
      /deletestring Text send
   } def
   /F { % ps_insertline(numlines, y) 
      /insertline Text send
   } def
   /G { % ps_deleteline(numlines, y)
      /deleteline Text send
   } def
   /H { % ps_setscrollinglimits(beginrow, endrow)
      /setscrollinglimits Text send
   } def
   /I { % ps_removescrollinglimits()
      /removescrollinglimits Text send
   } def
   /J { % ps_clearscreen()
      /clearviewport Text send
   } def
   /K { % ps_flashscreen()
      /flashviewport Text send
   } def
   /L { % ps_getscreensize(col, row, pixwidth, pixheight) =>
        % T_GETSCREENSIZE(pixheight, pixwidht, row, col)
      /getviewportsize Text send
   } def
   /M { % ps_oncaret()
      /oncaret Text send
   } def
   /N { % ps_offcaret()
      /offcaret Text send
   } def
   /O { % ps_movecaret(x, y)
      /movecaret Text send
   } def
   /P { % ps_movecaretdelta(deltax, deltay)
      /movecaretdelta Text send
   } def
   /Q { % ps_getcaretpos(x, y) => T_GETCURSORPOS(y, x)
      /getcaretpos Text send
   } def
   /R { % ps_setcaretblink(blinktime, dutycycle)
      /setcaretblink Text send
   } def
   /S { % ps_setcaretcolor(r, g, b)
      /setcaretcolor Text send
   } def
   /T { % ps_setcaretshape(string shapename, successfull) => 
        % T_SETCURSORSHAPE(successfull)
      /setcaretshape Text send
   } def
   /U { % ps_reversevideo|ps_normalvideo()
      pop
   } def 
   /V { % ps_getlinelength(linenum,length) => T_GETLINELENGTH(length)
      /getlinelength Text send
   } def

   % --- Return screen size to client
   /getviewportsize Text send 
   T_INITIALIZE tagprint 
   typedprint typedprint typedprint typedprint