At last, merge the putty-gtk2 branch back into the trunk!
[u/mdw/putty] / mac / macpgkey.c
CommitLineData
f160b7b8 1/* $Id$ */
ef394767 2/*
3 * Copyright (c) 2003 Ben Harris
e02b0bc2 4 * Copyright (c) 1997-2003 Simon Tatham
ef394767 5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29/* Stuff to handle the key window in PuTTYgen */
30
31#include <MacTypes.h>
e02b0bc2 32#include <Controls.h>
ef394767 33#include <Dialogs.h>
34#include <MacWindows.h>
35
36#include "putty.h"
37#include "mac.h"
38#include "macpgrid.h"
e02b0bc2 39#include "ssh.h"
40
41/* ----------------------------------------------------------------------
42 * Progress report code. This is really horrible :-)
43 */
44#define PROGRESSRANGE 65535
45#define MAXPHASE 5
46struct progress {
47 int nphases;
48 struct {
49 int exponential;
50 unsigned startpoint, total;
51 unsigned param, current, n; /* if exponential */
52 unsigned mult; /* if linear */
53 } phases[MAXPHASE];
54 unsigned total, divisor, range;
55 ControlHandle progbar;
56};
57
58static void progress_update(void *param, int action, int phase, int iprogress)
59{
60 struct progress *p = (struct progress *) param;
61 unsigned progress = iprogress;
62 int position;
63
64 if (action < PROGFN_READY && p->nphases < phase)
65 p->nphases = phase;
66 switch (action) {
67 case PROGFN_INITIALISE:
68 p->nphases = 0;
69 break;
70 case PROGFN_LIN_PHASE:
71 p->phases[phase-1].exponential = 0;
72 p->phases[phase-1].mult = p->phases[phase].total / progress;
73 break;
74 case PROGFN_EXP_PHASE:
75 p->phases[phase-1].exponential = 1;
76 p->phases[phase-1].param = 0x10000 + progress;
77 p->phases[phase-1].current = p->phases[phase-1].total;
78 p->phases[phase-1].n = 0;
79 break;
80 case PROGFN_PHASE_EXTENT:
81 p->phases[phase-1].total = progress;
82 break;
83 case PROGFN_READY:
84 {
85 unsigned total = 0;
86 int i;
87 for (i = 0; i < p->nphases; i++) {
88 p->phases[i].startpoint = total;
89 total += p->phases[i].total;
90 }
91 p->total = total;
92 p->divisor = ((p->total + PROGRESSRANGE - 1) / PROGRESSRANGE);
93 p->range = p->total / p->divisor;
94 SetControlMaximum(p->progbar, p->range);
95 }
96 break;
97 case PROGFN_PROGRESS:
98 if (p->phases[phase-1].exponential) {
99 while (p->phases[phase-1].n < progress) {
100 p->phases[phase-1].n++;
101 p->phases[phase-1].current *= p->phases[phase-1].param;
102 p->phases[phase-1].current /= 0x10000;
103 }
104 position = (p->phases[phase-1].startpoint +
105 p->phases[phase-1].total - p->phases[phase-1].current);
106 } else {
107 position = (p->phases[phase-1].startpoint +
108 progress * p->phases[phase-1].mult);
109 }
110 SetControlValue(p->progbar, position / p->divisor);
111 break;
112 }
113}
ef394767 114
217b88d1 115static void mac_clickkey(WindowPtr window, EventRecord *event)
116{
117 short item;
118 DialogRef dialog;
e02b0bc2 119 KeyState *ks = mac_windowkey(window);
217b88d1 120
121 dialog = GetDialogFromWindow(window);
122 if (DialogSelect(event, &dialog, &item))
123 switch (item) {
124 case wiKeyGenerate:
e02b0bc2 125 SetControlMaximum(ks->progress, 1024);
2d3f7d38 126 ks->entropy = snewn(1024, unsigned int);
e02b0bc2 127 ks->entropy_required = 1024;
128 ks->entropy_got = 0;
129 ks->collecting_entropy = TRUE;
217b88d1 130 /* Do something */
131 break;
132 }
133}
134
135static void mac_activatekey(WindowPtr window, EventRecord *event)
136{
137 DialogRef dialog;
138 DialogItemType itemtype;
139 Handle itemhandle;
140 short item;
141 Rect itemrect;
142 int active;
143
144 dialog = GetDialogFromWindow(window);
145 active = (event->modifiers & activeFlag) != 0;
146 GetDialogItem(dialog, wiKeyGenerate, &itemtype, &itemhandle, &itemrect);
147 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
148 DialogSelect(event, &dialog, &item);
149}
150
151static void mac_updatekey(WindowPtr window)
152{
153#if TARGET_API_MAC_CARBON
154 RgnHandle rgn;
155#endif
156
157 BeginUpdate(window);
158#if TARGET_API_MAC_CARBON
159 rgn = NewRgn();
160 GetPortVisibleRegion(GetWindowPort(window), rgn);
161 UpdateDialog(GetDialogFromWindow(window), rgn);
162 DisposeRgn(rgn);
163#else
164 UpdateDialog(window, window->visRgn);
165#endif
166 EndUpdate(window);
167}
168
ef394767 169void mac_newkey(void)
170{
171 KeyState *ks;
172 WinInfo *wi;
e02b0bc2 173 Handle h;
174 short type;
175 Rect rect;
ef394767 176
34773273 177 ks = snew(KeyState);
ef394767 178 ks->box = GetNewDialog(wKey, NULL, (WindowPtr)-1);
e02b0bc2 179 GetDialogItem(ks->box, wiKeyProgress, &type, &h, &rect);
180 ks->progress = (ControlHandle)h;
34773273 181 wi = snew(WinInfo);
ef394767 182 memset(wi, 0, sizeof(*wi));
183 wi->ks = ks;
184 wi->wtype = wKey;
217b88d1 185 wi->update = &mac_updatekey;
186 wi->click = &mac_clickkey;
187 wi->activate = &mac_activatekey;
188 SetWRefCon(GetDialogWindow(ks->box), (long)wi);
189 ShowWindow(GetDialogWindow(ks->box));
ef394767 190}
191
192/*
193 * Local Variables:
194 * c-file-style: "simon"
195 * End:
196 */