Simple windowed application: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|ooRexx}}: significantly shortened)
Line 1,154: Line 1,154:


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>/* REXX ***************************************************************************************
<lang oorexx>#!/usr/bin/rexx
* 18.06.2014 Walter Pachl shortened from Rony Flatscher's bsf4oorexx (see sourceforge) samples
/*
Name: "ShowCount.rxj"
* Look there for ShowCount.rxj
* bsf4oorexx lets the ooRexx program use Java classes

**********************************************************************************************/
Invoke: "rexx Showcount.rxj"
or
"rexxj Showcount.rxj" (Windows)
or
"rexxj.sh Showcount.rxj" (Unix)

Purpose: create a little resizable window with a push button to count and reveal
the number of times the button was pressed

Needs: Apache's "Bean Scripting Framework" (BSF), part of the BSF4ooRexx distribution

Date: 2001-04-30 Rony Flatscher presented this at the 12th International Rexx Symposium
Changed: 2005-12-28, added Apache license
2008-08-23, ---rgf, if using BSF.CLS, then do not use BSF() directly (or
remove the first three chars from its result string)
2009-07-03, ---Walter Pachl add help and improve a little bit
2010-05-16, rgf, - adapting example to take full advantage of BSF4ooRexx
- replacing bsf.addEventListener[WithEventObject] with a Rexx event handler
2012-06-09, rgf, - inhibit callbacks from Java after Rexx ends (if Rexx loaded Java)
2014-06-17 Walter Pachl copied from bsf4oorexx samples to rosettacode.org
*/

If arg(1)='?' Then Do
Parse Source source
Say source
Say 'This program displays a window with a button at its bottom'
Say 'In the middle of this window you will be told'
Say ' how often you have pressed this very button.'
Exit
End

hi=BsfInvokedBy()
Select
When hi=1 then say "This Rexx program was invoked by Java!"
When hi=2 then say "This Rexx program was invoked by Rexx, JVM loaded by Rexx!"
Otherwise Do; say "No JVM present, we got troubles ..."
Exit
End
End


-- Create a userData directory
userData=.directory~new -- a directory which will be passed to Rexx with the event
userData=.directory~new -- a directory which will be passed to Rexx with the event


Line 1,207: Line 1,166:
rexxCloseEH =.RexxCloseAppEventHandler~new -- Rexx event handler
rexxCloseEH =.RexxCloseAppEventHandler~new -- Rexx event handler
-- Create Java RexxProxy for the Rexx event handler
-- Create Java RexxProxy for the Rexx event handler
rpCloseEH=BsfCreateRexxProxy(rexxCloseEH, , "java.awt.event.WindowListener" )
rpCloseEH=BsfCreateRexxProxy(rexxCloseEH,,"java.awt.event.WindowListener" )
win~addWindowListener(rpCloseEH) -- add RexxProxy event handler
win~addWindowListener(rpCloseEH) -- add RexxProxy event handler


-- create a Java push button
-- create a Java push button
but=.bsf~new("java.awt.Button", "Press me!")
but=.bsf~new("java.awt.Button","Press me!")
-- Create a RexxProxy for the button Rexx event handler
-- Create a RexxProxy for the button Rexx event handler
rp=BsfCreateRexxProxy(.RexxShowCountEventHandler~new, userData, "java.awt.event.ActionListener")
rp=BsfCreateRexxProxy(.RexxShowCountEventHandler~new,userData,"java.awt.event.ActionListener")
but~addActionListener(rp) -- add RexxProxy event handler
but~addActionListener(rp) -- add RexxProxy event handler


-- create a Java label, set it to show the text centered
lab=.bsf~new("java.awt.Label") -- create a Java label,set it to show the text centered
userData~label=lab -- save label object for later use in event handler
lab=.bsf~new("java.awt.Label")
lab~setAlignment(lab~center) -- set alignment to center
userData~label=lab -- save label object for later use in event handler
lab~setText("Button was not yet pressed") -- assign initial text to the label
lab~setAlignment(lab~center) -- set alignment to center
lab~setText("Button was not yet pressed") -- assign a text to the label


win ~~add("Center", lab) ~~add("South", but) -- add the label and the button to the frame
win ~~add("Center",lab) ~~add("South",but) -- add the label and the button to the frame
win ~~pack -- now calculate all widget dimensions
win ~~pack -- now calculate all widget dimensions
call enlargeWidth win,but,90 -- enlarge the width of the frame and the button
call enlargeWidth win,but,120 -- enlarge the width of the frame and the button


win ~~setVisible(.true) ~~toFront -- make frame visible and move it to the front
win ~~setVisible(.true) ~~toFront -- make frame visible and move it to the front


userData~i=0 -- set counter to 0
userData~i=0 -- set counter to 0


rexxCloseEH~waitForExit -- wait until we are allowed to end the program
rexxCloseEH~waitForExit -- wait until we are allowed to end the program


-- if Java was loaded by Rexx, then terminate Java's RexxEngine to inhibit callbacks from Java
-- if Java was loaded by Rexx,then terminate Java's RexxEngine to inhibit callbacks from Java
call BSF.terminateRexxEngine
call BSF.terminateRexxEngine



::requires BSF.CLS -- get Java support
::requires BSF.CLS -- get Java support
Line 1,240: Line 1,197:
/* enlarge the width of the frame and of the button without using a layout manager */
/* enlarge the width of the frame and of the button without using a layout manager */
::routine enlargeWidth
::routine enlargeWidth
use arg win, but, addToWidth
use arg win,but,addToWidth
winDim=win~getSize -- get frame's dimension

winDim~width+=addToWidth -- increase width
say "---"
win~setSize(winDim) -- set frame's dimension
say "win_size (standard):" win~getSize~toString
say "but_size (standard):" but~getSize~toString

winDim=win~getSize -- get frame's dimension
winDim~width+=addToWidth -- increase width
win~setSize(winDim) -- set frame's dimension

say "win_size (enlarged):" win~getSize~toString
say "but_size (enlarged):" but~getSize~toString
say "---"



/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* Rexx event handler to set "close app" indicator */
/* Rexx event handler to set "close app" indicator */
::class RexxCloseAppEventHandler
::class RexxCloseAppEventHandler
::method init -- constructor
::method init -- constructor
expose closeApp
expose closeApp
closeApp = .false -- if set to .true, then it is safe to close the app
closeApp = .false -- if set to .true, then it is safe to close the app


::attribute closeApp -- indicates whether app should be closed
::attribute closeApp -- indicates whether app should be closed


::method unknown -- intercept unhandled events, do nothing
::method unknown -- intercept unhandled events,do nothing


::method windowClosing -- event method (from WindowListener)
::method windowClosing -- event method (from WindowListener)
expose closeApp
expose closeApp
closeApp=.true -- indicate that the app should close
closeApp=.true -- indicate that the app should close


::method waitForExit -- method blocks until attribute is set to .true
::method waitForExit -- method blocks until attribute is set to .true
expose closeApp
expose closeApp
guard on when closeApp=.true
guard on when closeApp=.true



/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
Line 1,279: Line 1,225:
::class RexxShowCountEventHandler
::class RexxShowCountEventHandler
::method actionPerformed
::method actionPerformed
use arg eventObject, slotDir
use arg eventObject,slotDir
call showCount slotDir~userData
call showCount slotDir~userData



/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
::routine ShowCount -- query and display current size
::routine ShowCount -- increment counter and show text
use arg userData
use arg userData
userData~i+=1 -- increment counter in directory object

i=userData~i+1 -- get and increment counter, save in local var "i"
Select -- construct text part
When userData~i=1 Then how_often='once'
userData~i=i -- save current value in directory object
When userData~i=2 Then how_often='twice'
Select
When i=1 Then text='once'
When userData~i=3 Then how_often='three times'
Otherwise how_often=userData~i 'times'
When i=2 Then text='twice'
When i=3 Then text='three times'
Otherwise text=i 'times'
End
End
userData~label~setText("Button was pressed" text)</lang>
userData~label~setText("Button was pressed" how_often) -- display text</lang>
Output:
<pre>rexx ShowCount.rxj
This Rexx program was invoked by Rexx, JVM loaded by Rexx!
---
win_size (standard): java.awt.Dimension[width=180,height=91]
but_size (standard): java.awt.Dimension[width=162,height=23]
win_size (enlarged): java.awt.Dimension[width=270,height=91]
but_size (enlarged): java.awt.Dimension[width=252,height=23]
---</pre>


=={{header|Oz}}==
=={{header|Oz}}==