program T_navod4

!! K navodu pro modul Japigraf
!! Prace s vlastnimi procedurami JAPI - dve canvas na frame
!! USE Japigraf+USE rgbcolor umozni vyuzit nektere procedury Japigraf a rgbcolor
!! Start procedurou StartJapi() nastavi S_width,S_height,AspRatio
!! a umozni pouzivat normalni souradnice misto pixelu a funkce Nx2Pix, Ny2Pix
!! Castecna nebo uplna rekonstrukce promenne TYPE(ViewPortInfo) :: infVP
!! umozni vyuzivat dalsi procedury z Japigraf (1.parametr maji infVP)


use std_type
		! vsechny realne promenne v Japigraf jsou REAL(KIND=DP)
		! g95 umoznuje zkraceny zapis, napr. REAL(DP) misto REAL(KIND=DP)
use japi
use japigraf
use rgbcolor

implicit none

character(len=*),parameter :: CRLF = char(13)//char(10)

integer :: frame, canvas1, canvas2
	! normalni souradnice leveho horniho rohu + rozmer frame
real(DP) :: NposxFR, NposyFR, NsirkaFR, NvyskaFR
	! normalni souradnice pro polohu a rozmer canvas1
real(DP) :: NposxC1, NposyC1, NsirkaC1, NvyskaC1
	! normalni souradnice pro polohu a rozmer canvas2
real(DP) :: NposxC2, NposyC2, NsirkaC2, NvyskaC2
	! pro zadani RGB barvy
type(rgbcol) :: BGcolorFR, BGcolorC1, BGcolorC2
	! promenne ktere po castecnem naplneni dovoli volat 
	! nektere procedury Japigraf
type(ViewPortInfo) :: infVP1,infVP2
	! promenne pro ulozeni prevodu Nx2Pix,... pro canvas1,canvas2
integer :: PxC1,PyC1,PsirkaC1,PvyskaC1,PxC2,PyC2,PsirkaC2,PvyskaC2	
real(DP) :: uxmin,uymin,uxmax,uymax
	! pomocne promenne
character(len=60) :: text
type(Pbod) :: poloha
real(DP) :: dux, duy

! *****************  zahajeni volanim StartJapi() ***********************
! Nastavi S_width,S_height,AspRatio
! umozni pouzivani normalnich souradnic s pouzitim prevodu Nx2Pix, Ny2Pix
call StartJapi()

! ****************   pokracovani ****************************************
! nastavit frame a na nem dve canvas
	! oro frame
	! Normalni souradnice se vztahuji k cele obrazovce
NposxFR = 0.2
NposyFR = 0.1
NsirkaFR = 0.7
NvyskaFR = 0.6
BGcolorFR = lavender

	! Pro canvas se normalni souradnice vztahuji k frame !!!
	! pro canvas1
NposxC1 = 0.05
NposyC1 = 0.2
NsirkaC1 = 0.3
NvyskaC1 = 0.35
BGcolorC1 = AntiqueWhite
	! pro canvas2
NposxC2 = 0.4
NposyC2 = 0.1
NsirkaC2 = 0.25
NvyskaC2 = 0.45
BGcolorC2 = MintCream

	! definice frame
frame = j_frame("Program T_navod2")
call j_setsize(frame,Nx2Pix(NsirkaFR),Ny2Pix(NvyskaFR))
call j_setpos(frame,Nx2Pix(NposxFR),Ny2Pix(NposyFR))
BGcolorFR = lavender
call j_setcolorbg(frame,BGcolorFR%r,BGcolorFR%g,BGcolorFR%b)

	! promenne v pixelech pro canvas1
PsirkaC1 = Nx2pix(NsirkaC1)
PvyskaC1 = Ny2Pix(NvyskaC1)
PxC1 = Nx2pix(NposxC1)
PyC1 = Ny2Pix(NposyC1)
	! definice canvas1
canvas1 = j_canvas(frame,PsirkaC1,PvyskaC1)
call j_setpos(canvas1,PxC1,PyC1)
call NastavBGbarvu(canvas1,BGcolorC1)

	! totez pro canvas2 pomoci deklarovane procedury NastavCanvasI
	! parametry jsou v normalnich souradnicich
call NastavCanvasI(frame,NposxC2,NposyC2,NsirkaC2,NvyskaC2,BGcolorC2,canvas2)
	! promenne v pixelech pro canvas2

!********** vse Zobrazit ***************
call j_show(frame)

! ======================= KRESLENI NA CANVAS =================================

! Vytvorime promennou TYPE(ViewPortInfo), ktera nam umozni volat nektere
! procedury z modulu Japigraf (maji prvni parametr infVP)
! staci naplnit jen ty komponenty, ktere prislusna procedura pouziva
! je mozne libovolne volat procedury z JAPI a pripustne z Japigraf
!
!=========================== Pro canvas1 ======================================
	! Naplneni jen nekterych polozek infVP1
infVP1%frame=frame
infVP1%canvas=canvas1

	! Nakreslit uhlopricku a kruznici na canvas1; souradnice se zadavaji
	! v pixelech, levy horni roh canvas ma souradnice (0,0)
	! procedura z Japigraf pro nastaveni foreground barvy
call SetFGcolor(infVP1,AliceBlue)	! potrebuje jen infVP1%canvas
call j_fillcircle(canvas1,PsirkaC1/2,PvyskaC1/2,PsirkaC1/3)
call SetFGcolor(infVP1,SteelBlue)
call j_drawcircle(canvas1,PsirkaC1/2,PvyskaC1/2,PsirkaC1/3)
call SetFGcolor(infVP1,Gray30)  			! jeste osy
call j_drawline(canvas1,psirkaC1/2,0,psirkaC1/2,pvyskaC1)
call j_drawline(canvas1,0,pvyskaC1/2,psirkaC1,pvyskaC1/2)

!======================== Pro canvas2 ========================================
	! uzivatelske promenne
uxmin = 0.0
uxmax = 10.0
uymin = 0.0
uymax =10.0
PsirkaC2 = Nx2pix(NsirkaC2)
PvyskaC2 = Ny2Pix(NvyskaC2)
PxC2 = Nx2pix(NposxC2)
PyC2 = Ny2Pix(NposyC2)
dux = (uxmax-uxmin)/10
duy = (uymax-uymin)/10
!=============== Vytvoreni vsech slozek promenne infVP2 ======================
	! infVP2 Nastavime tak, ze PlotArea splyva s canvas2
infVP2%frame=frame
infVP2%canvas=canvas2
infVP2%canvasUPinfo = UPinfo(userCO(uxmin-dux,uxmax+dux,uymin-duy,uymax+duy),&
							 objCO(PxC2,PyC2,PsirkaC2,PvyskaC2))
infVP2%PlotAreaUPinfo = UPinfo(userCO(uxmin,uxmax,uymin,uymax),&
								objCO(PxC2,PyC2,PsirkaC2,PvyskaC2))
infVP2%pix2ux = PsirkaC2/abs(uxmax-uxmin)							
infVP2%pix2uy = PvyskaC2/abs(uymax-uymin)
infVP2%canvasRGBcolor = BGcolorC2
infVP2%PlotAreaRGBcolor = BGcolorC2
!**************** konec uplne konstrukce infVP2 **************************
!
!================ pridame standardni menu s infVP2 ============================
call AddStdMenu(infVP2)

!========================== Nakreslit osy =====================================
	! Parametry: OdX, DoX, Ypoloha,krok deleni,dale rozdelit na dilku,
	!			 barva osy a poisu, kreslit/nekreslit grid, barva pro grid 	
call osaX(infVP2,uxmin,uxmax,1.0_DP,2.0_DP,2,blue,.true.,gray85)
call osaY(infVP2,uymin,uymax,1.0_DP,2.0_DP,2,blue,.true.,grey85)
	! Nasledne zavolani PlotLabels NEBUDE MIT ZADNY EFEKT, protoze pise
	! na canvas VNE PlotArea (u nas obe oblasti splyvaji)
Call PlotLabels(infVP2,"osa X","Osa Y", "Test",red)	

call ULine(infVP2,Ubod(2.0_DP,2.0_DP),Ubod(8.0_DP,8.0_DP),DarkRed)
call PlotSymbol5(infVP2,Ubod(2.0_DP,2.0_DP),ctvr5f,red)
call PlotSymbol5(infVP2,Ubod(8.0_DP,8.0_DP),diam5f,red)
text = "Canvas2"
	! Napsat text na canvas2
	! PARAMETRY: jmeno pisma,styl,velikost,RGBcol
	! jmeno pisma : J_COURIER, J_HELVETIA, J_TIMES, J_DIALOGIN, J_DIALOGOUT
	! styl pisma  : J_PLAIN, J_BOLD, J_ITALIC
poloha = Ubod2Pbod(infVP2,Ubod((uxmax-uxmin)/3,uymax+duy/5))
Call PisText(infVP2,poloha,navy,"Toto je "//text,&
			velikost=14,jmenofontu=J_DIALOGOUT)

! ***** Znovu neco nakreslit na canvas1 - vyznacit stred kruznice *****

call SetFGcolor(infVP1,green)		! z infVP1 potrebuje jen canvas
call j_fillcircle(canvas1,PsirkaC1/2,PvyskaC1/2,4) ! plna kruznice ve stredu

call StartStdMenu(infVP2)

!print *,"ENTER ukonci program"
!read *

call StopJapi()

!------------------------------------------------------------------------------
contains
!------------------------------------------------------------------------------

Subroutine NastavBGbarvu(obj,barva)
	! barva se zada jako rgb color, ale vola se procedura
	! j_setcolorbg, ktera vyzaduje slozky zvlast
	integer,intent(in) :: obj
	type(rgbcol),intent(in) :: barva
	call j_setcolorbg(obj,barva%r,barva%g,barva%b)
end subroutine NastavBgbarvu

Subroutine NastavPolohu(obj,Nxpos,Nypos)
	integer,intent(in) :: obj
	real(DP),intent(in) :: Nxpos,Nypos
	call j_setsize(obj,Nx2Pix(Nxpos),Ny2Pix(Nypos))
end Subroutine NastavPolohu

subroutine NastavCanvasI(frame,Nxpos,Nypos,Nsirka,Nvyska,barvaBG,canvas)
	! umozni nastaveni vice canvas na jedno frame
	! poloha a velikost v normalnich souradnicich, kter0 se v tuto chvili
	! uz nevztahuji k cele obrazovce, ale k frame !!!
	integer,intent(in) :: frame
	real(DP),intent(in) :: Nxpos,Nypos,Nsirka,Nvyska
	type(rgbcol),intent(in) :: barvaBG 
	integer,intent(out) :: canvas
	canvas = j_canvas(frame,Nx2Pix(Nsirka),Ny2Pix(Nvyska))
	call j_setpos(canvas,Nx2Pix(Nxpos),Ny2Pix(Nypos))
	call j_setcolorbg(canvas,barvaBG%r,barvaBG%g,barvaBG%b)
end subroutine NastavCanvasI	


end program T_navod4
