1 /*
2  * ElevationGridDialog.cpp
3  *
4  * Copyright (C) 1999 Stephen F. White
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (see the file "COPYING" for details); if
18  * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19  * Cambridge, MA 02139, USA.
20  */
21 
22 #include "stdafx.h"
23 #include "ElevationGridDialog2.h"
24 #include "resource.h"
25 
26 #ifdef WIN32
27 # include <windows.h>
28 //#include <commctrl.h>
29 #else
30 # include <stdlib.h>
31 #endif
32 //#include <GL/gl.h>
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/timeb.h>
37 #include <Dialog2.h>
38 #include "swt.h"
39 //#include "swt_private.h"
40 
41 
ElevationGridDialog(SWND parent,int number,int depth)42 ElevationGridDialog::ElevationGridDialog(SWND parent, int number, int depth)
43   : Dialog(parent, IDD_ELEVATION)
44 {
45     _width = number;
46     _widthSwnd = swGetDialogItem(_dlg, IDC_WIDTH);
47     _depth = depth;
48     _depthSwnd = swGetDialogItem(_dlg, IDC_DEPTH);
49     LoadData();
50 }
51 
~ElevationGridDialog()52 ElevationGridDialog::~ElevationGridDialog()
53 {
54 }
55 
56 void
SaveData()57 ElevationGridDialog::SaveData()
58 {
59     char number[128];
60     swGetText(_widthSwnd, number, 128);
61     _width = atoi(number);
62     swGetText(_depthSwnd, number, 128);
63     _depth = atoi(number);
64 }
65 
66 bool
Validate()67 ElevationGridDialog::Validate()
68 {
69     return _width > 0 && _depth > 0;
70 }
71 
72 void
LoadData()73 ElevationGridDialog::LoadData()
74 {
75     char number[128];
76     snprintf(number, 128, "%d", _width);
77     swSetText(_widthSwnd, number);
78     snprintf(number, 128, "%d", _depth);
79     swSetText(_depthSwnd, buf);
80 
81 }
82 
83 int
GetCharacterPosition(SWND swnd,char * number)84 ElevationGridDialog::GetCharacterPosition(SWND swnd, char* number)
85 {
86     char compare_string[128];
87     char zero_string[128];
88     for(int k=0;k<127;k++)
89     {
90         zero_string[k] = '0';
91         zero_string[k + 1] = 0;
92         compare_string[k] = 0;
93     }
94 
95     //Caret position at client window
96     int caretPosition = swGetTextCaretPos(swnd);
97 
98     //get character position
99     int str_width=0;
100     int j=0;
101     int nu_char;
102     for (nu_char=0; str_width < caretPosition - 1; nu_char++)
103     {
104         if(number[j] == compare_string[j])
105         {
106             mystrncpy_secure(number + j, zero_string + j, 2);
107         }
108 
109         char buf[128];
110         mystrncpy_secure(buf, number + nu_char, 2);
111         int char_width = swGetStringWidth(swGetDefaultFont(),
112                                           (const char*) buf);
113         str_width =str_width + char_width;
114         j++;
115     }
116     return nu_char;
117 }
118 
119 void
OnCommand(int id)120 ElevationGridDialog::OnCommand(int id)
121 {
122     if (id == ID_INCREASE)
123     {
124         swGetText(_widthSwnd, number, 128);
125 
126         // nu_char: number of character, which included in "number"
127         int nu_char = GetCharacterPosition(_widthSwnd, number);
128 
129         //Increase integer
130         char buf[128];
131         char temporary[128];
132         mystrncpy_secure(temporary, number, 128);
133         mystrncpy_secure(buf, number + nu_char , 2);
134 
135         int i = atoi(buf);
136         i = i + 1;
137 
138         if (i == 10)
139             i = 0;
140 
141         mysnprintf(buf,128,"%d",i);
142         mystrncpy_secure(number + nu_char , buf, 2);
143         mystrncpy_secure(number + nu_char + 1, temporary + nu_char + 1, 128 - nu_char - 1);
144 
145         swSetText(_widthSwnd, number);
146         swInvalidateWindow(_dlg);
147     }
148 
149     if (id == ID_DECREASE)
150     {
151         swGetText(_widthSwnd, number, 128);
152 
153     // nu_char: number of character, which included in "number"
154         int nu_char = GetCharacterPosition(_widthSwnd, number);
155 
156     //Decrease integer
157         char buf[128];
158         char temporary[128];
159         mystrncpy_secure(temporary, number, 128);
160         mystrncpy_secure(buf, number + nu_char , 2);
161 
162         int i = atoi(buf);
163         i = i - 1;
164 
165         if (i < 0)
166             i = 9;
167 
168         mysnprintf(buf,128,"%d",i);
169         mystrncpy_secure(number + nu_char , buf, 2);
170         mystrncpy_secure(number + nu_char + 1, temporary + nu_char + 1, 128 - nu_char - 1);
171 
172         swSetText(_widthSwnd, number);
173         swInvalidateWindow(_dlg);
174     }
175 
176     if (id == ID_MOVE_RIGHT)
177     {
178     //Caret position at client window
179         int caretPosition = swGetTextCaretPos(_widthSwnd);
180         swGetText(_widthSwnd, buf, 128);
181 
182     //Move caret position
183         int str_width=0;
184         int char_width=0;
185         int i=0;
186 
187         do
188         {
189             char buf[128];
190             mystrncpy_secure(buf, number + i, 2);
191             char_width = swGetStringWidth(swGetDefaultFont(), (const char*) buf);
192             str_width =str_width + char_width;
193             i++;
194         } while (str_width < caretPosition);
195         caretPosition = str_width + 1;
196         swSetTextCaretPos(_widthSwnd, caretPosition);
197         swInvalidateWindow(_dlg);
198     }
199 
200     if (id == ID_MOVE_LEFT)
201     {
202     //Caret position at client window
203         int caretPosition = swGetTextCaretPos(_widthSwnd);
204         swGetText(_widthSwnd, number, 128);
205 
206     //Move caret position
207         int str_width=0;
208         int char_width=0;
209 
210         for (int i=0; str_width < caretPosition - 1; i++)
211         {
212             char buf[128];
213             mystrncpy_secure(buf, number + i, 2);
214             char_width = swGetStringWidth(swGetDefaultFont(), (const char*) buf);
215             str_width = str_width + char_width;
216         }
217         caretPosition = str_width + 1 - char_width;
218         swSetTextCaretPos(_widthSwnd, caretPosition);
219         swInvalidateWindow(_dlg);
220     }
221 
222 ////  CreateCaret(_widthSwnd->hWnd, (HBITMAP) NULL, 1, swGetFontHeight(swGetDefaultFont()));
223 ////  ShowCaret(_widthSwnd->hWnd);
224 
225     Dialog::OnCommand(id);
226 
227 }
228