The latest version of this document can be found at www.broad.ology.org.uk/amiga/proaction/03_Adding_An_Image.html
1: #!python
2: #
3: ################################################################
4: #
5: # Anatomy of a ProAction Application.
6: #
7: # 03_Adding_An_Image.py
8: #
9: #
10: # Add a label Image to the layout.
11: #
12: ################################################################
13: #
14: #
15:
16: import sys
17: import os
18: import arexx
19:
20: class Application:
21:
22: def __init__(self):
23:
24: self.PORTNAME = "TestScript"
25: self.UNIQUE = True
26:
27: #
28: # HandleInput() receives commands at the ARexx port and acts accordingly.
29: # They may be commands from ProAction (the CLOSE command in this example)
30: # Or they maybe commands from other ARexx based scripts etc.
31: #
32:
33: def HandleInput(pyport,guikey):
34:
35: global app
36:
37: # Loop until die becomes non zero
38: die = 0
39: while die == 0:
40: # Wait for a message to arrive at our Port
41: pyport.wait()
42: # Get the first message (but there may be more than one)
43: msg = pyport.getmsg()
44: # Loop until all queued messages are processed
45: while msg:
46: cmd = msg.msg
47: msg.reply()
48: if cmd == "QUIT":
49: die = 1
50: break
51: elif cmd[:5] == "CLOSE":
52: # Window Close Button Pressed
53: die = 1
54: break
55: elif cmd[:8] == "GADGETUP":
56: # The format of this command is
57: # GADGETUP GUIID guikey GADGETID gid CODE code
58: # We uses the split() method of the python string object to neatly parse this.
59: # The value lguikey would allow us to deal with multiple GUI windows.
60: (dummy,dummy,lguikey,dummy,gid,dummy,code) = cmd.split()
61: if gid == app.ok_gid:
62: die = 1
63: print "OK Pressed"
64: else:
65: print cmd
66: msg = pyport.getmsg()
67:
68:
69: def DoGUI(pubscreen):
70:
71: global app
72:
73:
74: # Our script needs an incoming ARexx port to receive messages from
75: # ProAction. We create this using the arexx modules Port method
76: # Which returns a Port object
77:
78:
79:
80: pyport = arexx.Port(app.PORTNAME)
81: if pyport:
82: if app.UNIQUE:
83: if pyport.name != app.PORTNAME:
84: ErrorExit("ARexx Port " + app.PORTNAME + " already Exists!")
85:
86: # First we'll build our window.class tagslist
87: # Python can't use send StemVars at the moment so we'll
88: # use the TAGSTRING tecnique throughout.
89:
90: wintags = ""
91: wintags += "WA_Width,300,"
92: wintags += "WA_Height,100,"
93: wintags += "WA_DragBar,1,"
94: wintags += "WA_DepthGadget,1,"
95: wintags += "WA_SizeGadget,1,"
96: wintags += "WA_CloseGadget,1,"
97: wintags += "WA_Title,"+ sys.argv[0] + ","
98: wintags += "WA_PubScreenFallBack,1,"
99: wintags += "WA_PubScreenName," + pubscreen + ","
100: wintags += "WINDOW_Position,WPOS_CENTERSCREEN,"
101: wintags += "WA_Activate,1,"
102: wintags += "TAG_DONE"
103:
104: # Now a taglist to define our top level layout.gadget
105:
106: layouttags = ""
107: layouttags += "LAYOUT_Orientation,LAYOUT_ORIENT_VERT,"
108: layouttags += "LAYOUT_HorizAlignment,LALIGN_CENTER,"
109: layouttags += "TAG_DONE"
110:
111: # Tags for our label image.
112: labeltags = ""
113: labeltags += "LABEL_Justification,LJ_CENTER,"
114: # Notice in this next line we pass the newline as "*N" this is because the
115: # ProAction commands are parsed with ReadArgs, so muxt be "DOS Encoded"
116: labeltags += "LABEL_Text,A Label*NOn Two Lines,"
117: labeltags += "TAG_DONE"
118:
119:
120: (rc,rc2,guikey) = arexx.dorexx("PROACTION","CREATEGUI PORTNAME " + pyport.name + " TAGSTRING "" + wintags + """)
121: if rc == 0:
122: # we can now build our GUI here;
123:
124: # firstly we add the top level layout gadget, we use a special command
125: # ADDLAYOUT to do this, which in combination with ENDLAYOUT manages nesting
126: # of complex layout trees for us.
127:
128: (rc,rc2,current_layout_gid) = arexx.dorexx("PROACTION","ADDLAYOUT GUIID " + guikey + " TAGSTRING "" + layouttags + """)
129:
130: # Here we add a label image directly to the layout with the ADDIMAGE command
131:
132:
133: (rc,rc2,dummy) = arexx.dorexx("PROACTION","ADDIMAGE GUIID " + guikey + " IMAGECLASS "label.image" TAGSTRING "" + labeltags + """)
134:
135: # Now we add a button gadget. The ADDGADGET function adds the
136: # gadget to the currently active layout. We save the resulting ID
137: # in our Application class object app.
138:
139: (rc,rc2,app.ok_gid) = arexx.dorexx("PROACTION","ADDGADGET GUIID " + guikey + " GADGETCLASS "button.gadget" TAGSTRING "GA_Text,_OK,GA_RelVerify,1,TAG_DONE"")
140:
141: # End of top level layout.
142: (rc,rc2,current_layout_gid) = arexx.dorexx("PROACTION","ENDLAYOUT GUIID " + guikey )
143:
144: # Open our Window.
145:
146: (rc,rc2,result) = arexx.dorexx("PROACTION","OPENGUIWINDOW GUIID " + guikey)
147:
148: # Handle events at the ARexx port.
149: HandleInput(pyport,guikey)
150:
151: # All done now shutdown the GUI
152:
153: arexx.dorexx("PROACTION","DESTROYGUI GUIID " + guikey)
154: else: # pyport
155: ErrorExit("Couldn't create our ARexx port")
156:
157: #
158: # ErrorExit() throws up an RequestChoice notification to indicate
159: # a critical error.
160: #
161:
162: def ErrorExit(msg):
163: # Build our command as a string.
164: # We can use sys.argv[0] for the script name to make thsi more reusable.
165: command = "RequestChoice TYPE "ERROR" "" + sys.argv[0] + "" "" + msg + "" "OK" >NIL:"
166: # Call the command via the os.system()
167: os.system(command)
168: exit()
169:
170: #
171: # The support GetPorts() returns a list of public MsgPorts
172: # as a string
173: #
174:
175:
176: def GetPorts():
177: # Depending on the version of python we may have a useful
178: # function that retuns just what we need, so we use the try
179: # except syntax to test.
180:
181: try:
182: ports = os.getports()
183: ports = " ".join(ports)
184: except AttributeError:
185: # The getports function wasn't present so we call the REXX
186: # equivalent.
187: (rc,rc2,ports) = arexx.dorexx("REXX","return show('P')")
188: if rc != 0:
189: # We couldn't even call REXX somwthing serioiusly up here
190: # Bail out with a requester.
191: ErrorExit("Couldn't Find ARexx!")
192: return ports
193:
194:
195: # Now we need to check that ProAction is present an available to us,
196: # So first get a list of active ports
197:
198: ports = GetPorts()
199:
200: # Then check for ProActions port which will be "PROACTION".
201: # Port names are case sensitive.
202:
203: if -1 == ports.find("PROACTION"):
204: # No ProAction start it
205: os.system("RUN >"T:proactionpid" *>NIL: APPDIR:PROACTION")
206: os.system("C:WaitForPort PROACTION")
207: # Now check again
208: ports = GetPorts()
209: if -1 == ports.find("PROACTION"):
210: # Still not there :-(
211: ErrorExit("Unable to start or find ProAction GUIServer")
212:
213:
214: # Okay we should be good to go now
215: # Initialise our aplication
216:
217: app = Application();
218:
219: DoGUI("Workbench")
220: