f854b482 |
1 | /* $Id: macevlog.c,v 1.2 2003/02/15 16:22:15 ben Exp $ */ |
7dcd1f87 |
2 | /* |
3 | * Copyright (c) 2003 Ben Harris |
4 | * All rights reserved. |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person |
7 | * obtaining a copy of this software and associated documentation |
8 | * files (the "Software"), to deal in the Software without |
9 | * restriction, including without limitation the rights to use, |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or |
11 | * sell copies of the Software, and to permit persons to whom the |
12 | * Software is furnished to do so, subject to the following |
13 | * conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be |
16 | * included in all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
23 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 | * SOFTWARE. |
26 | */ |
27 | |
28 | #include <MacTypes.h> |
29 | #include <Lists.h> |
30 | #include <MacWindows.h> |
31 | #include <Quickdraw.h> |
32 | #include <Script.h> |
33 | #include <ToolUtils.h> |
34 | |
35 | #include <limits.h> |
36 | #include <string.h> |
37 | |
38 | #include "putty.h" |
39 | #include "mac.h" |
40 | #include "macresid.h" |
41 | #include "terminal.h" |
42 | |
43 | static void mac_draweventloggrowicon(Session *s); |
44 | static void mac_adjusteventlogscrollbar(Session *s); |
f854b482 |
45 | static void mac_clickeventlog(WindowPtr, EventRecord *); |
46 | static void mac_activateeventlog(WindowPtr, EventRecord *); |
47 | static void mac_groweventlog(WindowPtr, EventRecord *); |
48 | static void mac_updateeventlog(WindowPtr); |
7dcd1f87 |
49 | |
50 | static void mac_createeventlog(Session *s) |
51 | { |
52 | Rect view; |
53 | #if TARGET_API_MAC_CARBON |
54 | Rect controlrect; |
55 | #endif |
56 | ListBounds bounds = { 0, 0, 0, 1 }; /* 1 column, 0 rows */ |
57 | Point csize = { 0, 0 }; |
58 | GrafPtr saveport; |
59 | long fondsize; |
60 | WinInfo *wi; |
61 | |
62 | s->eventlog_window = GetNewWindow(wEventLog, NULL, (WindowPtr)-1); |
63 | wi = smalloc(sizeof(*wi)); |
f854b482 |
64 | memset(wi, 0, sizeof(*wi)); |
7dcd1f87 |
65 | wi->s = s; |
66 | wi->wtype = wEventLog; |
f854b482 |
67 | wi->click = &mac_clickeventlog; |
68 | wi->activate = &mac_activateeventlog; |
69 | wi->grow = &mac_groweventlog; |
70 | wi->update = &mac_updateeventlog; |
7dcd1f87 |
71 | SetWRefCon(s->eventlog_window, (long)wi); |
72 | GetPort(&saveport); |
73 | SetPort((GrafPtr)GetWindowPort(s->eventlog_window)); |
74 | fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize); |
75 | TextFont(HiWord(fondsize)); |
76 | TextSize(LoWord(fondsize)); |
77 | SetPort(saveport); |
78 | #if TARGET_API_MAC_CARBON |
79 | GetPortBounds(GetWindowPort(s->eventlog_window), &view); |
80 | #else |
81 | view = s->eventlog_window->portRect; |
82 | #endif |
83 | view.right -= 15; /* Scrollbar */ |
84 | s->eventlog = LNew(&view, &bounds, csize, 0, s->eventlog_window, |
85 | TRUE, TRUE, FALSE, TRUE); |
86 | mac_adjusteventlogscrollbar(s); |
87 | #if TARGET_API_MAC_CARBON |
88 | SetListSelectionFlags(s->eventlog, lExtendDrag | lNoDisjoint | lNoExtend); |
89 | #else |
90 | (*s->eventlog)->selFlags = lExtendDrag | lNoDisjoint | lNoExtend; |
91 | #endif |
92 | ShowWindow(s->eventlog_window); |
93 | } |
94 | |
95 | void mac_freeeventlog(Session *s) |
96 | { |
97 | |
98 | if (s->eventlog != NULL) |
99 | LDispose(s->eventlog); |
100 | if (s->eventlog_window != NULL) { |
101 | sfree((WinInfo *)GetWRefCon(s->eventlog_window)); |
102 | DisposeWindow(s->eventlog_window); |
103 | } |
104 | } |
105 | |
106 | /* |
107 | * FIXME: logevent() should be passed a frontend handle, but backends have to |
108 | * have a terminal handle instead, because they pass it to from_backend(), |
109 | * so we accept a terminal handle here as well, and hope no-one tries to call |
110 | * us with sensible arguments. |
111 | */ |
112 | void logevent(void *frontend, char *str) |
113 | { |
114 | Terminal *term = frontend; |
115 | Session *s = term->frontend; |
116 | ListBounds bounds; |
117 | Cell cell = { 0, 0 }; |
118 | |
119 | if (s->eventlog == NULL) |
120 | mac_createeventlog(s); |
121 | if (s->eventlog == NULL) |
122 | return; |
123 | |
124 | #if TARGET_API_MAC_CARBON |
125 | GetListDataBounds(s->eventlog, &bounds); |
126 | #else |
127 | bounds = (*s->eventlog)->dataBounds; |
128 | #endif |
129 | cell.v = bounds.bottom; |
130 | LAddRow(1, cell.v, s->eventlog); |
131 | LSetCell(str, strlen(str), cell, s->eventlog); |
132 | } |
133 | |
134 | static void mac_draweventloggrowicon(Session *s) |
135 | { |
136 | Rect clip; |
137 | RgnHandle savergn; |
138 | |
139 | SetPort((GrafPtr)GetWindowPort(s->eventlog_window)); |
140 | /* |
141 | * Stop DrawGrowIcon giving us space for a horizontal scrollbar |
142 | * See Tech Note TB575 for details. |
143 | */ |
144 | #if TARGET_API_MAC_CARBON |
145 | GetPortBounds(GetWindowPort(s->eventlog_window), &clip); |
146 | #else |
147 | clip = s->eventlog_window->portRect; |
148 | #endif |
149 | clip.left = clip.right - 15; |
150 | savergn = NewRgn(); |
151 | GetClip(savergn); |
152 | ClipRect(&clip); |
153 | DrawGrowIcon(s->eventlog_window); |
154 | SetClip(savergn); |
155 | DisposeRgn(savergn); |
156 | } |
157 | |
158 | /* |
159 | * For some reason, LNew doesn't seem to respect the hasGrow |
160 | * parameter, so we hammer the scrollbar into shape ourselves. |
161 | */ |
162 | static void mac_adjusteventlogscrollbar(Session *s) |
163 | { |
164 | #if TARGET_API_MAC_CARBON |
165 | Rect winrect; |
166 | |
167 | GetPortBounds(GetWindowPort(s->eventlog_window), &winrect); |
168 | SizeControl(GetListVerticalScrollBar(s->eventlog), |
169 | 16, winrect.bottom - 13); |
170 | #else |
171 | SizeControl((*s->eventlog)->vScroll, |
172 | 16, s->eventlog_window->portRect.bottom - 13); |
173 | #endif |
174 | } |
175 | |
176 | void mac_clickeventlog(WindowPtr window, EventRecord *event) |
177 | { |
178 | Session *s = mac_windowsession(window); |
179 | Point mouse; |
180 | GrafPtr saveport; |
181 | |
182 | GetPort(&saveport); |
183 | SetPort((GrafPtr)GetWindowPort(window)); |
184 | mouse = event->where; |
185 | GlobalToLocal(&mouse); |
186 | LClick(mouse, event->modifiers, s->eventlog); |
187 | SetPort(saveport); |
188 | } |
189 | |
f854b482 |
190 | static void mac_groweventlog(WindowPtr window, EventRecord *event) |
7dcd1f87 |
191 | { |
192 | Session *s = mac_windowsession(window); |
193 | Rect limits; |
194 | long grow_result; |
195 | #if TARGET_API_MAC_CARBON |
196 | Rect rect; |
197 | Point cellsize; |
198 | #else |
199 | GrafPtr saveport; |
200 | #endif |
201 | |
202 | SetRect(&limits, 15, 0, SHRT_MAX, SHRT_MAX); |
203 | grow_result = GrowWindow(window, event->where, &limits); |
204 | if (grow_result == 0) return; |
205 | SizeWindow(window, LoWord(grow_result), HiWord(grow_result), TRUE); |
206 | LSize(LoWord(grow_result) - 15, HiWord(grow_result), s->eventlog); |
207 | mac_adjusteventlogscrollbar(s); |
208 | /* We would use SetListCellSize in Carbon, but it's missing. */ |
209 | (*s->eventlog)->cellSize.h = LoWord(grow_result) - 15; |
210 | #if TARGET_API_MAC_CARBON |
211 | cellsize.h = LoWord(grow_result) - 15; |
212 | GetListViewBounds(s->eventlog, &rect); |
213 | InvalWindowRect(window, &rect); |
214 | #else |
215 | GetPort(&saveport); |
216 | SetPort(window); |
217 | InvalRect(&(*s->eventlog)->rView); |
218 | SetPort(saveport); |
219 | #endif |
220 | } |
221 | |
f854b482 |
222 | static void mac_activateeventlog(WindowPtr window, EventRecord *event) |
7dcd1f87 |
223 | { |
224 | Session *s = mac_windowsession(window); |
225 | int active = (event->modifiers & activeFlag) != 0; |
226 | |
227 | LActivate(active, s->eventlog); |
228 | mac_draweventloggrowicon(s); |
229 | } |
230 | |
f854b482 |
231 | static void mac_updateeventlog(WindowPtr window) |
7dcd1f87 |
232 | { |
233 | Session *s = mac_windowsession(window); |
234 | #if TARGET_API_MAC_CARBON |
235 | RgnHandle visrgn; |
236 | #endif |
237 | |
238 | SetPort((GrafPtr)GetWindowPort(window)); |
239 | BeginUpdate(window); |
240 | #if TARGET_API_MAC_CARBON |
241 | visrgn = NewRgn(); |
242 | GetPortVisibleRegion(GetWindowPort(window), visrgn); |
243 | LUpdate(visrgn, s->eventlog); |
244 | DisposeRgn(visrgn); |
245 | #else |
246 | LUpdate(window->visRgn, s->eventlog); |
247 | #endif |
248 | mac_draweventloggrowicon(s); |
249 | EndUpdate(window); |
250 | } |
251 | |
252 | void mac_showeventlog(Session *s) |
253 | { |
254 | |
255 | ShowWindow(s->eventlog_window); |
256 | } |
257 | |
258 | /* |
259 | * Local Variables: |
260 | * c-file-style: "simon" |
261 | * End: |
262 | */ |