]> asedeno.scripts.mit.edu Git - PuTTY_svn.git/blob - mac/macevlog.c
Crude Event Log implementation for the Mac. I'm fairly convinced now that
[PuTTY_svn.git] / mac / macevlog.c
1 /* $Id: macevlog.c,v 1.1 2003/02/07 01:38:12 ben Exp $ */
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);
45
46 static void mac_createeventlog(Session *s)
47 {
48     Rect view;
49 #if TARGET_API_MAC_CARBON
50     Rect controlrect;
51 #endif
52     ListBounds bounds = { 0, 0, 0, 1 }; /* 1 column, 0 rows */
53     Point csize = { 0, 0 };
54     GrafPtr saveport;
55     long fondsize;
56     WinInfo *wi;
57
58     s->eventlog_window = GetNewWindow(wEventLog, NULL, (WindowPtr)-1);
59     wi = smalloc(sizeof(*wi));
60     wi->s = s;
61     wi->wtype = wEventLog;
62     SetWRefCon(s->eventlog_window, (long)wi);
63     GetPort(&saveport);
64     SetPort((GrafPtr)GetWindowPort(s->eventlog_window));
65     fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
66     TextFont(HiWord(fondsize));
67     TextSize(LoWord(fondsize));
68     SetPort(saveport);
69 #if TARGET_API_MAC_CARBON
70     GetPortBounds(GetWindowPort(s->eventlog_window), &view);
71 #else
72     view = s->eventlog_window->portRect;
73 #endif
74     view.right -= 15; /* Scrollbar */
75     s->eventlog = LNew(&view, &bounds, csize, 0, s->eventlog_window,
76                        TRUE, TRUE, FALSE, TRUE);
77     mac_adjusteventlogscrollbar(s);
78 #if TARGET_API_MAC_CARBON
79     SetListSelectionFlags(s->eventlog, lExtendDrag | lNoDisjoint | lNoExtend);
80 #else
81     (*s->eventlog)->selFlags = lExtendDrag | lNoDisjoint | lNoExtend;
82 #endif
83     ShowWindow(s->eventlog_window);
84 }
85
86 void mac_freeeventlog(Session *s)
87 {
88
89     if (s->eventlog != NULL)
90         LDispose(s->eventlog);
91     if (s->eventlog_window != NULL) {
92         sfree((WinInfo *)GetWRefCon(s->eventlog_window));
93         DisposeWindow(s->eventlog_window);
94     }
95 }
96
97 /*
98  * FIXME: logevent() should be passed a frontend handle, but backends have to
99  * have a terminal handle instead, because they pass it to from_backend(),
100  * so we accept a terminal handle here as well, and hope no-one tries to call
101  * us with sensible arguments.
102  */
103 void logevent(void *frontend, char *str)
104 {
105     Terminal *term = frontend;
106     Session *s = term->frontend;
107     ListBounds bounds;
108     Cell cell = { 0, 0 };
109
110     if (s->eventlog == NULL)
111         mac_createeventlog(s);
112     if (s->eventlog == NULL)
113         return;
114
115 #if TARGET_API_MAC_CARBON
116     GetListDataBounds(s->eventlog, &bounds);
117 #else
118     bounds = (*s->eventlog)->dataBounds;
119 #endif
120     cell.v = bounds.bottom;
121     LAddRow(1, cell.v, s->eventlog);
122     LSetCell(str, strlen(str), cell, s->eventlog);
123 }
124
125 static void mac_draweventloggrowicon(Session *s)
126 {
127     Rect clip;
128     RgnHandle savergn;
129
130     SetPort((GrafPtr)GetWindowPort(s->eventlog_window));
131     /*
132      * Stop DrawGrowIcon giving us space for a horizontal scrollbar
133      * See Tech Note TB575 for details.
134      */
135 #if TARGET_API_MAC_CARBON
136     GetPortBounds(GetWindowPort(s->eventlog_window), &clip);
137 #else
138     clip = s->eventlog_window->portRect;
139 #endif
140     clip.left = clip.right - 15;
141     savergn = NewRgn();
142     GetClip(savergn);
143     ClipRect(&clip);
144     DrawGrowIcon(s->eventlog_window);
145     SetClip(savergn);
146     DisposeRgn(savergn);
147 }
148
149 /*
150  * For some reason, LNew doesn't seem to respect the hasGrow
151  * parameter, so we hammer the scrollbar into shape ourselves.
152  */
153 static void mac_adjusteventlogscrollbar(Session *s)
154 {
155 #if TARGET_API_MAC_CARBON
156     Rect winrect;
157
158     GetPortBounds(GetWindowPort(s->eventlog_window), &winrect);
159     SizeControl(GetListVerticalScrollBar(s->eventlog),
160                 16, winrect.bottom - 13);
161 #else
162     SizeControl((*s->eventlog)->vScroll,
163                 16, s->eventlog_window->portRect.bottom - 13);
164 #endif
165 }
166
167 void mac_clickeventlog(WindowPtr window, EventRecord *event)
168 {
169     Session *s = mac_windowsession(window);
170     Point mouse;
171     GrafPtr saveport;
172
173     GetPort(&saveport);
174     SetPort((GrafPtr)GetWindowPort(window));
175     mouse = event->where;
176     GlobalToLocal(&mouse);
177     LClick(mouse, event->modifiers, s->eventlog);
178     SetPort(saveport);
179 }
180
181 void mac_groweventlog(WindowPtr window, EventRecord *event)
182 {
183     Session *s = mac_windowsession(window);
184     Rect limits;
185     long grow_result;
186 #if TARGET_API_MAC_CARBON
187     Rect rect;
188     Point cellsize;
189 #else
190     GrafPtr saveport;
191 #endif
192
193     SetRect(&limits, 15, 0, SHRT_MAX, SHRT_MAX);
194     grow_result = GrowWindow(window, event->where, &limits);
195     if (grow_result == 0) return;
196     SizeWindow(window, LoWord(grow_result), HiWord(grow_result), TRUE);
197     LSize(LoWord(grow_result) - 15, HiWord(grow_result), s->eventlog);
198     mac_adjusteventlogscrollbar(s);
199     /* We would use SetListCellSize in Carbon, but it's missing. */
200     (*s->eventlog)->cellSize.h = LoWord(grow_result) - 15;
201 #if TARGET_API_MAC_CARBON
202     cellsize.h = LoWord(grow_result) - 15;
203     GetListViewBounds(s->eventlog, &rect);
204     InvalWindowRect(window, &rect);
205 #else
206     GetPort(&saveport);
207     SetPort(window);
208     InvalRect(&(*s->eventlog)->rView);
209     SetPort(saveport);
210 #endif
211 }
212
213 void mac_activateeventlog(WindowPtr window, EventRecord *event)
214 {
215     Session *s = mac_windowsession(window);
216     int active = (event->modifiers & activeFlag) != 0;
217
218     LActivate(active, s->eventlog);
219     mac_draweventloggrowicon(s);
220 }
221
222 void mac_updateeventlog(WindowPtr window)
223 {
224     Session *s = mac_windowsession(window);
225 #if TARGET_API_MAC_CARBON
226     RgnHandle visrgn;
227 #endif
228
229     SetPort((GrafPtr)GetWindowPort(window));
230     BeginUpdate(window);
231 #if TARGET_API_MAC_CARBON
232     visrgn = NewRgn();
233     GetPortVisibleRegion(GetWindowPort(window), visrgn);
234     LUpdate(visrgn, s->eventlog);
235     DisposeRgn(visrgn);
236 #else
237     LUpdate(window->visRgn, s->eventlog);
238 #endif
239     mac_draweventloggrowicon(s);
240     EndUpdate(window);
241 }
242
243 void mac_showeventlog(Session *s)
244 {
245
246     ShowWindow(s->eventlog_window);
247 }
248
249 /*
250  * Local Variables:
251  * c-file-style: "simon"
252  * End:
253  */