OSDN Git Service

59a9af6636d796c9e9339458e6d826683040539c
[ffftp/ffftp.git] / taskwin.c
1 /*=============================================================================\r
2 *\r
3 *                                                               \83^\83X\83N\83E\83C\83\93\83h\83E\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 #define  STRICT\r
31 #include <windows.h>\r
32 #include <stdio.h>\r
33 #include <stdlib.h>\r
34 #include <string.h>\r
35 #include <mbstring.h>\r
36 #include <malloc.h>\r
37 #include <windowsx.h>\r
38 #include <commctrl.h>\r
39 #include <stdarg.h>\r
40 #include <winsock.h>\r
41 \r
42 #include "common.h"\r
43 #include "resource.h"\r
44 \r
45 #define TASK_BUFSIZE    (16*1024)\r
46 \r
47 \r
48 \r
49 \r
50 /*===== \8aO\95\94\8eQ\8fÆ =====*/\r
51 \r
52 extern int ClientWidth;\r
53 extern int SepaWidth;\r
54 extern int ListHeight;\r
55 \r
56 /* \90Ý\92è\92l */\r
57 extern int TaskHeight;\r
58 extern HFONT ListFont;\r
59 extern int DebugConsole;\r
60 \r
61 /*===== \83\8d\81[\83J\83\8b\82È\83\8f\81[\83N =====*/\r
62 \r
63 static HWND hWndTask = NULL;\r
64 static HANDLE DispLogSemaphore;\r
65 static HANDLE DispLogSemaphore2;\r
66 \r
67 \r
68 \r
69 /*----- \83^\83X\83N\83E\83C\83\93\83h\83E\82ð\8dì\90¬\82·\82é --------------------------------------------\r
70 *\r
71 *       Parameter\r
72 *               HWND hWnd : \90e\83E\83C\83\93\83h\83E\82Ì\83E\83C\83\93\83h\83E\83n\83\93\83h\83\8b\r
73 *               HINSTANCE hInst : \83C\83\93\83X\83^\83\93\83X\83n\83\93\83h\83\8b\r
74 *\r
75 *       Return Value\r
76 *               int \83X\83e\81[\83^\83X\r
77 *                       SUCCESS/FAIL\r
78 *----------------------------------------------------------------------------*/\r
79 \r
80 int MakeTaskWindow(HWND hWnd, HINSTANCE hInst)\r
81 {\r
82         int Sts;\r
83 \r
84         Sts = FAIL;\r
85         hWndTask = CreateWindowEx(/*WS_EX_STATICEDGE*/WS_EX_CLIENTEDGE,\r
86                         "EDIT", NULL,\r
87                         WS_CHILD | WS_BORDER | ES_AUTOVSCROLL | WS_VSCROLL | ES_MULTILINE | ES_READONLY | WS_CLIPSIBLINGS,\r
88                         0, TOOLWIN_HEIGHT*2+ListHeight+SepaWidth, ClientWidth, TaskHeight,\r
89                         hWnd, (HMENU)1500, hInst, NULL);\r
90 \r
91         if(hWndTask != NULL)\r
92         {\r
93                 SendMessage(hWndTask, EM_LIMITTEXT, TASK_BUFSIZE, 0);\r
94 \r
95                 if(ListFont != NULL)\r
96                         SendMessage(hWndTask, WM_SETFONT, (WPARAM)ListFont, MAKELPARAM(TRUE, 0));\r
97 \r
98                 ShowWindow(hWndTask, SW_SHOW);\r
99                 Sts = SUCCESS;\r
100 \r
101                 DispLogSemaphore = CreateSemaphore(NULL, 1, 1, NULL);\r
102                 DispLogSemaphore2 = CreateSemaphore(NULL, 1, 1, NULL);\r
103 \r
104         }\r
105         return(Sts);\r
106 }\r
107 \r
108 \r
109 /*----- \83^\83X\83N\83E\83C\83\93\83h\83E\82ð\8dí\8f\9c ------------------------------------------------\r
110 *\r
111 *       Parameter\r
112 *               \82È\82µ\r
113 *\r
114 *       Return Value\r
115 *               \82È\82µ\r
116 *----------------------------------------------------------------------------*/\r
117 \r
118 void DeleteTaskWindow(void)\r
119 {\r
120         CloseHandle(DispLogSemaphore);\r
121         CloseHandle(DispLogSemaphore2);\r
122         if(hWndTask != NULL)\r
123                 DestroyWindow(hWndTask);\r
124         return;\r
125 }\r
126 \r
127 \r
128 /*----- \83^\83X\83N\83E\83C\83\93\83h\83E\82Ì\83E\83C\83\93\83h\83E\83n\83\93\83h\83\8b\82ð\95Ô\82· ----------------------------\r
129 *\r
130 *       Parameter\r
131 *               \82È\82µ\r
132 *\r
133 *       Return Value\r
134 *               HWND \83E\83C\83\93\83h\83E\83n\83\93\83h\83\8b\r
135 *----------------------------------------------------------------------------*/\r
136 \r
137 HWND GetTaskWnd(void)\r
138 {\r
139         return(hWndTask);\r
140 }\r
141 \r
142 \r
143 /*----- \83^\83X\83N\83\81\83b\83Z\81[\83W\82ð\95\\8e¦\82·\82é --------------------------------------------\r
144 *\r
145 *       Parameter\r
146 *               char *szFormat : \83t\83H\81[\83}\83b\83g\95\8e\9a\97ñ\r
147 *               ... : \83p\83\89\83\81\81[\83^\r
148 *\r
149 *       Return Value\r
150 *               \82È\82µ\r
151 *----------------------------------------------------------------------------*/\r
152 \r
153 void SetTaskMsg(char *szFormat, ...)\r
154 {\r
155         int Pos;\r
156         va_list vaArgs;\r
157         char *szBuf;\r
158 //      DWORD Tmp;\r
159 \r
160 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "111\n", 4, &Tmp, NULL);\r
161         while(WaitForSingleObject(DispLogSemaphore, 1) == WAIT_TIMEOUT)\r
162                 BackgrndMessageProc();\r
163 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "222\n", 4, &Tmp, NULL);\r
164 \r
165         if(hWndTask != NULL)\r
166         {\r
167                 if((szBuf = malloc(10*1024+3)) != NULL)\r
168                 {\r
169                         va_start(vaArgs, szFormat);\r
170                         if(wvsprintf(szBuf, szFormat, vaArgs) != EOF)\r
171                         {\r
172 //#pragma aaa\r
173 //                              WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuf, strlen(szBuf), &Tmp, NULL);\r
174 //                              WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "\n", strlen("\n"), &Tmp, NULL);\r
175 \r
176                                 strcat(szBuf, "\r\n");\r
177                                 Pos = SendMessage(GetTaskWnd(), WM_GETTEXTLENGTH, 0, 0);\r
178 \r
179                                 /* \83e\83L\83X\83g\83T\83C\83Y\82Ì\83\8a\83~\83b\83g\92l\82ð\83`\83F\83b\83N */\r
180                                 if((Pos + strlen(szBuf)) >= TASK_BUFSIZE)\r
181                                 {\r
182                                         /* \83\8a\83~\83b\83g\82ð\89z\82¦\82»\82¤\82È\82ç\81A\90æ\93ª\95\94\95ª\82ð\90Ø\82è\8eÌ\82Ä\82é */\r
183                                         Pos = SendMessage(GetTaskWnd(), EM_LINEFROMCHAR, TASK_BUFSIZE/10, 0) + 1;\r
184                                         Pos = SendMessage(GetTaskWnd(), EM_LINEINDEX, Pos, 0);\r
185                                         SendMessage(GetTaskWnd(), EM_SETSEL, 0, Pos);\r
186                                         SendMessage(GetTaskWnd(), EM_REPLACESEL, FALSE, (LPARAM)"");\r
187 \r
188                                         Pos = SendMessage(GetTaskWnd(), WM_GETTEXTLENGTH, 0, 0);\r
189                                 }\r
190 \r
191                                 SendMessage(GetTaskWnd(), EM_SETSEL, Pos, Pos);\r
192                                 SendMessage(GetTaskWnd(), EM_REPLACESEL, FALSE, (LPARAM)szBuf);\r
193                         }\r
194                         va_end(vaArgs);\r
195                         free(szBuf);\r
196                 }\r
197         }\r
198 \r
199 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "333\n", 4, &Tmp, NULL);\r
200         ReleaseSemaphore(DispLogSemaphore, 1, NULL);\r
201 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "444\n", 4, &Tmp, NULL);\r
202 \r
203         return;\r
204 }\r
205 \r
206 \r
207 /*----- \83^\83X\83N\83\81\83b\83Z\81[\83W\82ð\83t\83@\83C\83\8b\82É\95Û\91\82·\82é ----------------------------------\r
208 *\r
209 *       Parameter\r
210 *               char *Fname : \83t\83@\83C\83\8b\96¼\r
211 *\r
212 *       Return Value\r
213 *               int \83X\83e\81[\83^\83X\r
214 *                       SUCCESS/FAIL\r
215 *----------------------------------------------------------------------------*/\r
216 \r
217 int SaveTaskMsg(char *Fname)\r
218 {\r
219         FILE *Strm;\r
220         int Size;\r
221         char *Buf;\r
222         int Sts;\r
223 \r
224 \r
225         Sts = FAIL;\r
226         Size = SendMessage(GetTaskWnd(), WM_GETTEXTLENGTH, 0, 0);\r
227         if((Buf = malloc(Size)) != NULL)\r
228         {\r
229                 if((Strm = fopen(Fname, "wb")) != NULL)\r
230                 {\r
231                         SendMessage(GetTaskWnd(), WM_GETTEXT, Size, (LPARAM)Buf);\r
232                         if(fwrite(Buf, strlen(Buf), 1, Strm) == 1)\r
233                                 Sts = SUCCESS;\r
234                         fclose(Strm);\r
235 \r
236                         if(Sts == FAIL)\r
237                                 _unlink(Fname);\r
238                 }\r
239                 free(Buf);\r
240         }\r
241         return(Sts);\r
242 }\r
243 \r
244 \r
245 /*----- \83^\83X\83N\93à\97e\82ð\83r\83\85\81[\83\8f\82Å\95\\8e¦ --------------------------------------------\r
246 *\r
247 *       Parameter\r
248 *               \82È\82µ\r
249 *\r
250 *       Return Value\r
251 *               \82È\82µ\r
252 *----------------------------------------------------------------------------*/\r
253 \r
254 void DispTaskMsg(void)\r
255 {\r
256         char Buf[FMAX_PATH+1];\r
257 \r
258         strcpy(Buf, AskTmpFilePath());\r
259         SetYenTail(Buf);\r
260         strcat(Buf, "_ffftp.tsk");\r
261 \r
262         if(SaveTaskMsg(Buf) == SUCCESS)\r
263         {\r
264                 AddTempFileList(Buf);\r
265                 ExecViewer(Buf, 0);\r
266         }\r
267         return;\r
268 }\r
269 \r
270 \r
271 /*----- \83f\83o\83b\83O\83R\83\93\83\\81[\83\8b\82É\83\81\83b\83Z\81[\83W\82ð\95\\8e¦\82·\82é ------------------------------\r
272 *\r
273 *       Parameter\r
274 *               char *szFormat : \83t\83H\81[\83}\83b\83g\95\8e\9a\97ñ\r
275 *               ... : \83p\83\89\83\81\81[\83^\r
276 *\r
277 *       Return Value\r
278 *               \82È\82µ\r
279 *----------------------------------------------------------------------------*/\r
280 \r
281 void DoPrintf(char *szFormat, ...)\r
282 {\r
283         va_list vaArgs;\r
284         char *szBuf;\r
285 //      DWORD Tmp;\r
286 \r
287 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "11111\n", 6, &Tmp, NULL);\r
288         while(WaitForSingleObject(DispLogSemaphore2, 1) == WAIT_TIMEOUT)\r
289                 BackgrndMessageProc();\r
290 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "22222\n", 6, &Tmp, NULL);\r
291 \r
292         if(DebugConsole == YES)\r
293         {\r
294                 if((szBuf = malloc(10*1024)) != NULL)\r
295                 {\r
296                         va_start(vaArgs, szFormat);\r
297                         if(wvsprintf(szBuf, szFormat, vaArgs) != EOF)\r
298                         {\r
299                                 SetTaskMsg("## %s", szBuf);\r
300 \r
301 //#pragma aaa\r
302 //                              WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuf, strlen(szBuf), &Tmp, NULL);\r
303 //                              WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "\n", strlen("\n"), &Tmp, NULL);\r
304                         }\r
305                         va_end(vaArgs);\r
306                         free(szBuf);\r
307                 }\r
308         }\r
309 \r
310 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "33333\n", 6, &Tmp, NULL);\r
311         ReleaseSemaphore(DispLogSemaphore2, 1, NULL);\r
312 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "44444\n", 6, &Tmp, NULL);\r
313 \r
314         return;\r
315 }\r
316 \r
317 \r
318 /*----- \83f\83o\83b\83O\83R\83\93\83\\81[\83\8b\82É\83\81\83b\83Z\81[\83W\82ð\95\\8e¦\82·\82é ------------------------------\r
319 *\r
320 *       Parameter\r
321 *               char *szFormat : \83t\83H\81[\83}\83b\83g\95\8e\9a\97ñ\r
322 *               ... : \83p\83\89\83\81\81[\83^\r
323 *\r
324 *       Return Value\r
325 *               \82È\82µ\r
326 *----------------------------------------------------------------------------*/\r
327 \r
328 void DoPrintf2(char *szFormat, ...)\r
329 {\r
330         va_list vaArgs;\r
331         char *szBuf;\r
332         DWORD Tmp;\r
333 \r
334 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "11111111\n", 9, &Tmp, NULL);\r
335         while(WaitForSingleObject(DispLogSemaphore2, 1) == WAIT_TIMEOUT)\r
336                 BackgrndMessageProc();\r
337 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "22222222\n", 9, &Tmp, NULL);\r
338 \r
339         if((szBuf = malloc(10*1024)) != NULL)\r
340         {\r
341                 va_start(vaArgs, szFormat);\r
342                 if(wvsprintf(szBuf, szFormat, vaArgs) != EOF)\r
343                 {\r
344                         WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szBuf, strlen(szBuf), &Tmp, NULL);\r
345                         WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "\n", strlen("\n"), &Tmp, NULL);\r
346                 }\r
347                 va_end(vaArgs);\r
348                 free(szBuf);\r
349         }\r
350 \r
351 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "33333333\n", 9, &Tmp, NULL);\r
352         ReleaseSemaphore(DispLogSemaphore2, 1, NULL);\r
353 //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), "44444444\n", 9, &Tmp, NULL);\r
354 \r
355         return;\r
356 }\r
357 \r
358 \r