]> asedeno.scripts.mit.edu Git - pssh.git/blob - forms/displayprefsform.c
My 2007-03-18 release:
[pssh.git] / forms / displayprefsform.c
1 /**********
2  * Copyright (c) 2004-2005 Greg Parker.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
23  **********/
24
25 // fixme allow subsystems to register with Prefs for change notices 
26 // (rather than hardcoding those here)
27
28 #include "includes.h"
29 #include "formutils.h"
30 #include "data/prefs.h"
31 #include "ssh/ssh.h"
32 #include "rsrc/rsrc.h"
33 #include "forms/resize.h"
34
35 #include "displayprefsform.h"
36
37
38 extern struct ssh_session_t *ss;
39
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;
46
47 static void LoadDisplayPrefsForm(void)
48 {
49     uint32_t value;
50     char *str;
51
52     // font
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
59
60     // colors
61     value = PrefsGetInt(prefTerminalForeColor, blackColor);
62     if (value < 8) {
63         ListPtr lst = PrvGetObjectByID(DisplayPrefsFormForeColorListID);
64         LstSetSelection(lst, value);
65         PrvSetControlLabel(DisplayPrefsFormForeColorTriggerID, 
66                            LstGetSelectionText(lst, value));
67     }
68     value = PrefsGetInt(prefTerminalBackColor, whiteColor);
69     if (value < 8) {
70         ListPtr lst = PrvGetObjectByID(DisplayPrefsFormBackColorListID);
71         LstSetSelection(lst, value);
72         PrvSetControlLabel(DisplayPrefsFormBackColorTriggerID, 
73                            LstGetSelectionText(lst, value));
74     }
75
76     // scrollback lines
77     value = PrefsGetInt(prefScrollbackLines, defaultScrollbackLines);
78     if (value > maxScrollbackLines) value = defaultScrollbackLines;
79     {
80         char buf[5];
81         snprintf(buf, sizeof(buf), "%lu", value);
82         PrvSetFieldToValueByID(DisplayPrefsFormScrollbackFieldID, buf);
83     }
84
85     // scroll to bottom
86     value = PrefsGetInt(prefScrollDownOnActivity, defaultScrollDownOnActivity);
87     PrvSetControlValue(DisplayPrefsFormActivityCheckboxID, value ? 1 : 0);
88     value = PrefsGetInt(prefScrollDownOnTyping, defaultScrollDownOnTyping);
89     PrvSetControlValue(DisplayPrefsFormTypingCheckboxID, value ? 1 : 0);
90
91     // bell
92     value = PrefsGetInt(prefBellBeep, defaultBellBeep);
93     PrvSetControlValue(DisplayPrefsFormBeepCheckboxID, value ? 1 : 0);
94     value = PrefsGetInt(prefBellFlash, defaultBellFlash);
95     PrvSetControlValue(DisplayPrefsFormFlashCheckboxID, value ? 1 : 0);
96 }
97
98
99 static void SaveDisplayPrefsForm(void)
100 {
101     uint32_t value;
102     int selection;
103     Boolean sshChanged = false;
104
105     // font
106     value = PrefsGetInt(prefTerminalFont, font4x6);
107     if (PrvGetControlValue(DisplayPrefsFormSmallFontCheckboxID) && value != font4x6) {
108         PrefsPutInt(prefTerminalFont, font4x6);
109         sshChanged = true;
110     } else if (PrvGetControlValue(DisplayPrefsFormLargeFontCheckboxID) && value != font6x10) {
111         PrefsPutInt(prefTerminalFont, font6x10);
112         sshChanged = true;
113     }
114
115     // color
116     value = PrefsGetInt(prefTerminalForeColor, defaultForeColor);
117     selection = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormForeColorListID));
118     if (selection != value  &&  selection != noListSelection) {
119         PrefsPutInt(prefTerminalForeColor, selection);
120         sshChanged = true;
121     }
122     value = PrefsGetInt(prefTerminalBackColor, defaultBackColor);
123     selection = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormBackColorListID));
124     if (selection != value  &&  selection != noListSelection) {
125         PrefsPutInt(prefTerminalBackColor, selection);
126         sshChanged = true;
127     }
128
129     // scrollback lines
130     {
131         char buf[5];
132         uint32_t lines;
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);
139                 sshChanged = true;
140             }
141         }
142     }
143
144     // scroll to bottom
145     value = PrefsGetInt(prefScrollDownOnActivity, defaultScrollDownOnActivity);
146     selection = PrvGetControlValue(DisplayPrefsFormActivityCheckboxID);
147     if (value != selection) {
148         PrefsPutInt(prefScrollDownOnActivity, selection);
149         sshChanged = true;
150     }
151     value = PrefsGetInt(prefScrollDownOnTyping, defaultScrollDownOnTyping);
152     selection = PrvGetControlValue(DisplayPrefsFormTypingCheckboxID);
153     if (value != selection) {
154         PrefsPutInt(prefScrollDownOnTyping, selection);
155         sshChanged = true;
156     }
157
158     // bell
159     value = PrefsGetInt(prefBellBeep, defaultBellBeep);
160     selection = PrvGetControlValue(DisplayPrefsFormBeepCheckboxID);
161     if (value != selection) {
162         PrefsPutInt(prefBellBeep, selection);
163         sshChanged = true;
164     }
165     value = PrefsGetInt(prefBellFlash, defaultBellFlash);
166     selection = PrvGetControlValue(DisplayPrefsFormFlashCheckboxID);
167     if (value != selection) {
168         PrefsPutInt(prefBellFlash, selection);
169         sshChanged = true;
170     }
171
172     if (sshChanged) ssh_reread_prefs(ss);
173 }
174
175
176 static void InvalidateSampleText(void)
177 {
178     EventType e2;
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);
186 }
187
188
189 Boolean DisplayPrefsFormHandleEvent(EventPtr e)
190 {
191     FormPtr frmP = FrmGetActiveForm();
192
193     if (ResizeHandleEvent(e)) return true;
194
195     switch (e->eType) {
196     case frmOpenEvent:
197         FrmSetGadgetHandler(FrmGetActiveForm(), 
198                             PrvGetObjectIndexByID(DisplayPrefsFormSampleTextGadgetID),
199                             SampleTextEventHandler);
200         LoadDisplayPrefsForm();
201         FrmDrawForm(frmP);
202         return true;
203
204     case popSelectEvent: {
205         // force sample text to redraw with new colors
206         InvalidateSampleText();
207         return false;
208     }
209
210     case ctlSelectEvent:
211         switch (e->data.ctlSelect.controlID) {
212         case DisplayPrefsFormOKButtonID:
213             // save values; leave form
214             SaveDisplayPrefsForm();
215             FrmReturnToForm(0);
216             return true;
217
218         case DisplayPrefsFormCancelButtonID:
219             // DON'T save values; leave form
220             FrmReturnToForm(0);
221             return true;
222
223         // checkboxes
224
225         case DisplayPrefsFormSmallFontCheckboxID:
226             PrvSetControlValue(DisplayPrefsFormLargeFontCheckboxID, 0);
227             InvalidateSampleText();
228             return true;
229         case DisplayPrefsFormLargeFontCheckboxID:
230             PrvSetControlValue(DisplayPrefsFormSmallFontCheckboxID, 0);
231             InvalidateSampleText();
232             return true;
233
234         default:
235             return false;
236         }
237
238     case frmCloseEvent:
239         return false;
240
241     default:
242         return false;
243     }
244 }
245
246 // fixme
247 extern void set_palm_color(int fg_color, int bg_color);
248
249 static void DrawSampleText(RectangleType bounds)
250 {
251     uint32_t fg_color;
252     uint32_t bg_color;
253     char *str = "Sample terminal text";
254     Int16 width, height;
255     Int16 x, y;
256
257     WinPushDrawState();
258     
259     fg_color = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormForeColorListID));
260     bg_color = LstGetSelection(PrvGetObjectByID(DisplayPrefsFormBackColorListID));
261     set_palm_color(fg_color, bg_color);
262     
263     if (PrvGetControlValue(DisplayPrefsFormSmallFontCheckboxID)) {
264         FntSetFont(font_for_screen(font4x6));
265     } else {
266         FntSetFont(font_for_screen(font6x10));
267     }
268
269     width = FntCharsWidth(str, strlen(str));
270     height = FntLineHeight();
271
272     x = bounds.topLeft.x + (bounds.extent.x / 2) - (width / 2);
273     y = bounds.topLeft.y + (bounds.extent.y / 2) - (height / 2);
274     
275     WinEraseRectangle(&bounds, 0);
276     WinDrawChars(str, strlen(str), x, y);
277     
278     WinPopDrawState();
279 }
280
281
282 static Boolean SampleTextEventHandler(struct FormGadgetTypeInCallback *gadget, 
283                                       UInt16 cmd, void *paramP)
284 {
285     Boolean handled = false; 
286
287     switch (cmd) { 
288     case formGadgetDrawCmd: 
289         // Sent to active gadgets any time form is  
290         // drawn or redrawn. 
291         DrawSampleText(gadget->rect);
292         handled = true; 
293         break; 
294
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.  
301             // do nothing
302             handled = true; 
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);
307             handled = true;
308         } 
309         break; 
310     }
311
312     case formGadgetDeleteCmd:  
313         // Perform any cleanup prior to deletion. 
314         break; 
315
316     case formGadgetEraseCmd:  
317         // FrmHideObject takes care of this if you  
318         // return false.  
319         handled = false; 
320         break; 
321
322     default:
323         handled = false;
324         break;
325     } 
326
327     return handled; 
328