2 * Copyright (c) 2004-2005 Greg Parker. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY GREG PARKER ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 // fixme allow subsystems to register with Prefs for change notices
26 // (rather than hardcoding those here)
29 #include "formutils.h"
30 #include "data/prefs.h"
32 #include "rsrc/rsrc.h"
33 #include "forms/resize.h"
35 #include "displayprefsform.h"
38 extern struct ssh_session_t *ss;
40 static void LoadDisplayPrefsForm(void) FORMS_SEGMENT;
41 static void SaveDisplayPrefsForm(void) FORMS_SEGMENT;
42 static Boolean SampleTextEventHandler(struct FormGadgetTypeInCallback *gadget,
43 UInt16 cmd, void *paramP) FORMS_SEGMENT;
44 static void InvalidateSampleText(void) FORMS_SEGMENT;
45 static void DrawSampleText(RectangleType bounds) FORMS_SEGMENT;
47 static void LoadDisplayPrefsForm(void)
53 value = PrefsGetInt(prefTerminalFont, font4x6);
54 PrvSetControlValue(DisplayPrefsFormSmallFontCheckboxID,
55 value == font4x6 ? 1 : 0);
56 PrvSetControlValue(DisplayPrefsFormLargeFontCheckboxID,
57 value == font6x10 ? 1 : 0);
58 // else some future font size - ignore
61 value = PrefsGetInt(prefTerminalForeColor, blackColor);
63 ListPtr lst = PrvGetObjectByID(DisplayPrefsFormForeColorListID);
64 LstSetSelection(lst, value);
65 PrvSetControlLabel(DisplayPrefsFormForeColorTriggerID,
66 LstGetSelectionText(lst, value));
68 value = PrefsGetInt(prefTerminalBackColor, whiteColor);
70 ListPtr lst = PrvGetObjectByID(DisplayPrefsFormBackColorListID);
71 LstSetSelection(lst, value);
72 PrvSetControlLabel(DisplayPrefsFormBackColorTriggerID,
73 LstGetSelectionText(lst, value));
77 value = PrefsGetInt(prefScrollbackLines, defaultScrollbackLines);
78 if (value > maxScrollbackLines) value = defaultScrollbackLines;
81 snprintf(buf, sizeof(buf), "%lu", value);
82 PrvSetFieldToValueByID(DisplayPrefsFormScrollbackFieldID, buf);
86 value = PrefsGetInt(prefScrollDownOnActivity, defaultScrollDownOnActivity);
87 PrvSetControlValue(DisplayPrefsFormActivityCheckboxID, value ? 1 : 0);
88 value = PrefsGetInt(prefScrollDownOnTyping, defaultScrollDownOnTyping);
89 PrvSetControlValue(DisplayPrefsFormTypingCheckboxID, value ? 1 : 0);
92 value = PrefsGetInt(prefBellBeep, defaultBellBeep);
93 PrvSetControlValue(DisplayPrefsFormBeepCheckboxID, value ? 1 : 0);
94 value = PrefsGetInt(prefBellFlash, defaultBellFlash);
95 PrvSetControlValue(DisplayPrefsFormFlashCheckboxID, value ? 1 : 0);
99 static void SaveDisplayPrefsForm(void)
103 Boolean sshChanged = false;
106 value = PrefsGetInt(prefTerminalFont, font4x6);
107 if (PrvGetControlValue(DisplayPrefsFormSmallFontCheckboxID) && value != font4x6) {
108 PrefsPutInt(prefTerminalFont, font4x6);
110 } else if (PrvGetControlValue(DisplayPrefsFormLargeFontCheckboxID) && value != font6x10) {
111 PrefsPutInt(prefTerminalFont, font6x10);
116 value = PrefsGetInt(prefTerminalForeColor, defaultForeColor);
117 selection = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormForeColorListID));
118 if (selection != value && selection != noListSelection) {
119 PrefsPutInt(prefTerminalForeColor, selection);
122 value = PrefsGetInt(prefTerminalBackColor, defaultBackColor);
123 selection = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormBackColorListID));
124 if (selection != value && selection != noListSelection) {
125 PrefsPutInt(prefTerminalBackColor, selection);
133 value = PrefsGetInt(prefScrollbackLines, defaultScrollbackLines);
134 PrvCopyFieldContents(DisplayPrefsFormScrollbackFieldID, buf);
135 if (strlen(buf) > 0) {
136 lines = StrAToI(buf);
137 if (value != lines) {
138 PrefsPutInt(prefScrollbackLines, lines);
145 value = PrefsGetInt(prefScrollDownOnActivity, defaultScrollDownOnActivity);
146 selection = PrvGetControlValue(DisplayPrefsFormActivityCheckboxID);
147 if (value != selection) {
148 PrefsPutInt(prefScrollDownOnActivity, selection);
151 value = PrefsGetInt(prefScrollDownOnTyping, defaultScrollDownOnTyping);
152 selection = PrvGetControlValue(DisplayPrefsFormTypingCheckboxID);
153 if (value != selection) {
154 PrefsPutInt(prefScrollDownOnTyping, selection);
159 value = PrefsGetInt(prefBellBeep, defaultBellBeep);
160 selection = PrvGetControlValue(DisplayPrefsFormBeepCheckboxID);
161 if (value != selection) {
162 PrefsPutInt(prefBellBeep, selection);
165 value = PrefsGetInt(prefBellFlash, defaultBellFlash);
166 selection = PrvGetControlValue(DisplayPrefsFormFlashCheckboxID);
167 if (value != selection) {
168 PrefsPutInt(prefBellFlash, selection);
172 if (sshChanged) ssh_reread_prefs(ss);
176 static void InvalidateSampleText(void)
179 e2.eType = frmGadgetMiscEvent;
180 e2.data.gadgetMisc.gadgetID = DisplayPrefsFormSampleTextGadgetID;
181 e2.data.gadgetMisc.gadgetP =
182 PrvGetObjectByID(e2.data.gadgetMisc.gadgetID);
183 e2.data.gadgetMisc.selector = 0;
184 e2.data.gadgetMisc.dataP = NULL;
185 EvtAddEventToQueue(&e2);
189 Boolean DisplayPrefsFormHandleEvent(EventPtr e)
191 FormPtr frmP = FrmGetActiveForm();
193 if (ResizeHandleEvent(e)) return true;
197 FrmSetGadgetHandler(FrmGetActiveForm(),
198 PrvGetObjectIndexByID(DisplayPrefsFormSampleTextGadgetID),
199 SampleTextEventHandler);
200 LoadDisplayPrefsForm();
204 case popSelectEvent: {
205 // force sample text to redraw with new colors
206 InvalidateSampleText();
211 switch (e->data.ctlSelect.controlID) {
212 case DisplayPrefsFormOKButtonID:
213 // save values; leave form
214 SaveDisplayPrefsForm();
218 case DisplayPrefsFormCancelButtonID:
219 // DON'T save values; leave form
225 case DisplayPrefsFormSmallFontCheckboxID:
226 PrvSetControlValue(DisplayPrefsFormLargeFontCheckboxID, 0);
227 InvalidateSampleText();
229 case DisplayPrefsFormLargeFontCheckboxID:
230 PrvSetControlValue(DisplayPrefsFormSmallFontCheckboxID, 0);
231 InvalidateSampleText();
247 extern void set_palm_color(int fg_color, int bg_color);
249 static void DrawSampleText(RectangleType bounds)
253 char *str = "Sample terminal text";
259 fg_color = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormForeColorListID));
260 bg_color = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormBackColorListID));
261 set_palm_color(fg_color, bg_color);
263 if (PrvGetControlValue(DisplayPrefsFormSmallFontCheckboxID)) {
264 FntSetFont(font_for_screen(font4x6));
266 FntSetFont(font_for_screen(font6x10));
269 width = FntCharsWidth(str, strlen(str));
270 height = FntLineHeight();
272 x = bounds.topLeft.x + (bounds.extent.x / 2) - (width / 2);
273 y = bounds.topLeft.y + (bounds.extent.y / 2) - (height / 2);
275 WinEraseRectangle(&bounds, 0);
276 WinDrawChars(str, strlen(str), x, y);
282 static Boolean SampleTextEventHandler(struct FormGadgetTypeInCallback *gadget,
283 UInt16 cmd, void *paramP)
285 Boolean handled = false;
288 case formGadgetDrawCmd:
289 // Sent to active gadgets any time form is
291 DrawSampleText(gadget->rect);
295 case formGadgetHandleEventCmd: {
296 EventPtr e = (EventPtr)paramP;
297 // Sent when form receives a gadget event.
298 // paramP points to EventType structure.
299 if (e->eType == frmGadgetEnterEvent) {
300 // penDown in gadget's bounds.
303 } else if (e->eType == frmGadgetMiscEvent) {
304 // This event is sent by your application
305 // when it needs to send info to the gadget
306 DrawSampleText(gadget->rect);
312 case formGadgetDeleteCmd:
313 // Perform any cleanup prior to deletion.
316 case formGadgetEraseCmd:
317 // FrmHideObject takes care of this if you