Initial commit.
[amiga/xmodule.git] / App.c
1 /*
2 **      App.c
3 **
4 **      Copyright (C) 1994,95,96 by Bernardo Innocenti
5 **
6 **      Handle AppIcons & AppWindows
7 */
8
9 #include <exec/memory.h>
10 #include <workbench/workbench.h>
11
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include <proto/intuition.h>
15 #include <proto/wb.h>
16 #include <proto/icon.h>
17
18 #include "XModulePriv.h"
19 #include "Gui.h"
20
21
22 XDEF ULONG      AppSig = 0;
23 XDEF LONG       IconX = NO_ICON_POSITION;
24 XDEF LONG       IconY = NO_ICON_POSITION;
25 XDEF UBYTE      IconName[16];
26 XDEF BOOL       Iconified = FALSE;
27
28
29 static struct MsgPort           *AppPort        = NULL;
30 static struct AppIcon           *MyAppIcon      = NULL;
31 static struct DiskObject        *AppDObj        = NULL;
32
33
34
35 GLOBALCALL void HandleAppMessage (void)
36
37 /* App Window event handler.  Get Workbench message and call server */
38 {
39         struct AppMessage *am;
40
41         while (am = (struct AppMessage *) GetMsg (AppPort))
42         {
43                 switch (am->am_Type)
44                 {
45                         case AMTYPE_APPWINDOW:
46                                 (((struct WinUserData *) am->am_UserData)->DropIcon) (am);
47                                 break;
48
49                         case AMTYPE_APPICON:
50                                 if (am->am_NumArgs == 0)
51                                         DeIconify();
52                                 else if (am->am_UserData)
53                                         ((void (*) (struct AppMessage *am))(am->am_UserData)) (am);
54
55                                 break;
56
57                         default:
58                                 break;
59                 }
60
61                 ReplyMsg ((struct Message *) am);
62         }
63 }
64
65
66
67 GLOBALCALL void AddAppWin (struct WinUserData *wud)
68 {
69         wud->AppWin = AddAppWindowA (0, (ULONG)wud, wud->Win, AppPort, NULL);
70 }
71
72
73
74 GLOBALCALL void RemAppWin (struct WinUserData *wud)
75 {
76         struct Node             *succ;
77         struct Message  *msg;
78
79         RemoveAppWindow (wud->AppWin);
80         wud->AppWin = NULL;
81
82         /* Reply all pending messages for this window */
83
84         Forbid();
85
86         msg = (struct Message *) AppPort->mp_MsgList.lh_Head;
87
88         while (succ = msg->mn_Node.ln_Succ)
89         {
90                 if ((struct WinUserData *)(((struct AppMessage *)msg)->am_UserData) == wud)
91                 {
92                         Remove ((struct Node *)msg);
93                         ReplyMsg (msg);
94                 }
95                 msg = (struct Message *) succ;
96         }
97
98         Permit();
99 }
100
101
102
103 GLOBALCALL LONG CreateAppIcon (void (*handler) (struct AppMessage *am))
104 {
105         if (!AppPort) return RETURN_FAIL;
106
107         if (MyAppIcon) return RETURN_OK;
108
109         /* Get icon */
110         if ( !(AppDObj = GetProgramIcon() ))
111                 AppDObj = GetDefDiskObject (WBTOOL);
112
113         if (!AppDObj) return RETURN_FAIL;
114
115         /* Initialize AppIcon */
116         AppDObj->do_CurrentX = IconX;
117         AppDObj->do_CurrentY = IconY;
118
119         if (MyAppIcon = AddAppIconA (0, (ULONG)handler, IconName, AppPort, NULL, AppDObj, NULL))
120                 return RETURN_OK;
121
122         FreeDiskObject (AppDObj); AppDObj = NULL;
123         return RETURN_FAIL;
124 }
125
126
127
128 GLOBALCALL void DeleteAppIcon (void)
129 {
130         if (MyAppIcon)
131         {
132                 RemoveAppIcon (MyAppIcon); MyAppIcon = NULL;
133                 FreeDiskObject (AppDObj); AppDObj = NULL;
134         }
135 }
136
137
138
139 GLOBALCALL void Iconify (void)
140 {
141         if (!CreateAppIcon (ToolBoxDropIcon))
142         {
143                 CloseDownScreen();
144                 Iconified = TRUE;
145         }
146 }
147
148
149
150 GLOBALCALL void DeIconify (void)
151 {
152         if (!SetupScreen())
153         {
154                 Iconified = FALSE;
155                 if (!GuiSwitches.ShowAppIcon) DeleteAppIcon();
156         }
157 }
158
159
160 GLOBALCALL LONG SetupApp (void)
161 {
162         if (!(AppPort = CreateMsgPort()))
163                 return ERROR_NO_FREE_STORE;
164         AppSig = 1 << AppPort->mp_SigBit;
165         Signals |= AppSig;
166
167         if (GuiSwitches.ShowAppIcon)
168                 CreateAppIcon (ToolBoxDropIcon);
169
170         return RETURN_OK;
171 }
172
173
174
175 GLOBALCALL void CleanupApp (void)
176 {
177         if (AppPort)
178         {
179                 DeleteAppIcon();
180
181                 KillMsgPort (AppPort); AppPort = NULL;
182                 Signals &= ~AppSig; AppSig = 0;
183         }
184 }