1 /* NetHack 3.6	maccurs.c	$NHDT-Date: 1432512797 2015/05/25 00:13:17 $  $NHDT-Branch: master $:$NHDT-Revision: 1.9 $ */
2 /* Copyright (c) Jon W{tte, 1992.				  */
3 /* NetHack may be freely redistributed.  See license for details. */
4 
5 #include "hack.h"
6 #include "mactty.h"
7 #include "macwin.h"
8 
9 #if 1 /*!TARGET_API_MAC_CARBON*/
10 #include <Folders.h>
11 #include <TextUtils.h>
12 #include <Resources.h>
13 #endif
14 
15 static Boolean winFileInit = 0;
16 static unsigned char winFileName[32] = "\pNetHack Preferences";
17 static long winFileDir;
18 static short winFileVol;
19 
20 typedef struct WinPosSave {
21     char validPos;
22     char validSize;
23     short top;
24     short left;
25     short height;
26     short width;
27 } WinPosSave;
28 
29 static WinPosSave savePos[kLastWindowKind + 1];
30 
31 static void
InitWinFile(void)32 InitWinFile(void)
33 {
34     StringHandle sh;
35     long len;
36     short ref = 0;
37 
38     if (winFileInit) {
39         return;
40     }
41     /* We trust the glue. If there's an error, store in game dir. */
42     if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
43                    &winFileVol, &winFileDir)) {
44         winFileVol = 0;
45         winFileDir = 0;
46     }
47     sh = GetString(128);
48     if (sh && *sh) {
49         BlockMove(*sh, winFileName, **sh + 1);
50         ReleaseResource((Handle) sh);
51     }
52     if (HOpen(winFileVol, winFileDir, winFileName, fsRdPerm, &ref)) {
53         return;
54     }
55     len = sizeof(savePos);
56     if (!FSRead(ref, &len, savePos)) {
57         winFileInit = 1;
58     }
59     FSClose(ref);
60 }
61 
62 static void
FlushWinFile(void)63 FlushWinFile(void)
64 {
65     short ref;
66     long len;
67 
68     if (!winFileInit) {
69         if (!winFileName[0]) {
70             return;
71         }
72         HCreate(winFileVol, winFileDir, winFileName, MAC_CREATOR, PREF_TYPE);
73         HCreateResFile(winFileVol, winFileDir, winFileName);
74     }
75     if (HOpen(winFileVol, winFileDir, winFileName, fsWrPerm, &ref)) {
76         return;
77     }
78     winFileInit = 1;
79     len = sizeof(savePos);
80     (void) FSWrite(ref, &len, savePos); /* Don't care about error */
81     FSClose(ref);
82 }
83 
84 Boolean
RetrievePosition(short kind,short * top,short * left)85 RetrievePosition(short kind, short *top, short *left)
86 {
87     Point p;
88 
89     if (kind < 0 || kind > kLastWindowKind) {
90         dprintf("Retrieve Bad kind %d", kind);
91         return 0;
92     }
93     InitWinFile();
94     if (!savePos[kind].validPos) {
95         dprintf("Retrieve Not stored kind %d", kind);
96         return 0;
97     }
98     p.v = savePos[kind].top;
99     p.h = savePos[kind].left;
100     *left = p.h;
101     *top = p.v;
102     dprintf("Retrieve Kind %d Pt (%d,%d)", kind, p.h, p.v);
103     return (PtInRgn(p, GetGrayRgn()));
104 }
105 
106 Boolean
RetrieveSize(short kind,short top,short left,short * height,short * width)107 RetrieveSize(short kind, short top, short left, short *height, short *width)
108 {
109     Point p;
110 
111     if (kind < 0 || kind > kLastWindowKind) {
112         return 0;
113     }
114     InitWinFile();
115     if (!savePos[kind].validSize) {
116         return 0;
117     }
118     *width = savePos[kind].width;
119     *height = savePos[kind].height;
120     p.h = left + *width;
121     p.v = top + *height;
122     return PtInRgn(p, GetGrayRgn());
123 }
124 
125 static void
SavePosition(short kind,short top,short left)126 SavePosition(short kind, short top, short left)
127 {
128     if (kind < 0 || kind > kLastWindowKind) {
129         dprintf("Save bad kind %d", kind);
130         return;
131     }
132     InitWinFile();
133     savePos[kind].validPos = 1;
134     savePos[kind].top = top;
135     savePos[kind].left = left;
136     dprintf("Save kind %d pt (%d,%d)", kind, left, top);
137     FlushWinFile();
138 }
139 
140 static void
SaveSize(short kind,short height,short width)141 SaveSize(short kind, short height, short width)
142 {
143     if (kind < 0 || kind > kLastWindowKind) {
144         dprintf("Save bad kind %d", kind);
145         return;
146     }
147     InitWinFile();
148     savePos[kind].validSize = 1;
149     savePos[kind].width = width;
150     savePos[kind].height = height;
151     FlushWinFile();
152 }
153 
154 static short
GetWinKind(WindowPtr win)155 GetWinKind(WindowPtr win)
156 {
157     short kind;
158 
159     if (!CheckNhWin(win)) {
160         return -1;
161     }
162     kind = GetWindowKind(win) - WIN_BASE_KIND;
163     if (kind < 0 || kind > NHW_TEXT) {
164         return -1;
165     }
166     dprintf("In win kind %d (%lx)", kind, win);
167     switch (kind) {
168     case NHW_MAP:
169     case NHW_STATUS:
170     case NHW_BASE:
171         kind = kMapWindow;
172         break;
173     case NHW_MESSAGE:
174         kind = kMessageWindow;
175         break;
176     case NHW_MENU:
177         kind = kMenuWindow;
178         break;
179     default:
180         kind = kTextWindow;
181         break;
182     }
183     dprintf("Out kind %d", kind);
184     return kind;
185 }
186 
187 Boolean
RetrieveWinPos(WindowPtr win,short * top,short * left)188 RetrieveWinPos(WindowPtr win, short *top, short *left)
189 {
190     return RetrievePosition(GetWinKind(win), top, left);
191 }
192 
193 void
SaveWindowPos(WindowPtr win)194 SaveWindowPos(WindowPtr win)
195 {
196     Rect r;
197 
198     GetWindowBounds(win, kWindowContentRgn, &r);
199     SavePosition(GetWinKind(win), r.top, r.left);
200 }
201 
202 void
SaveWindowSize(WindowPtr win)203 SaveWindowSize(WindowPtr win)
204 {
205     short width, height;
206     Rect r;
207 
208     GetWindowBounds(win, kWindowContentRgn, &r);
209     width = r.right - r.left;
210     height = r.bottom - r.top;
211     SaveSize(GetWinKind(win), height, width);
212 }
213