3 * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
6 * Functions to save the results as a textfile or a drawfile
11 #include "DeskLib:Menu.h"
12 #include "DeskLib:Save.h"
13 #include "DeskLib:Template.h"
14 #include "DeskLib:Window.h"
18 /* The window handle of the save window */
19 static window_handle tSaveWindow
= 0;
21 /* Xfer_send box fields */
24 #define CANCEL_BUTTON (-1)
25 #define FILENAME_ICON 2
29 * saveas - a wrapper around Save_InitSaveWindowhandler
32 saveas(int iFileType
, char *szOutfile
, size_t tEstSize
,
33 save_filesaver save_function
, void *pvReference
)
37 if (tSaveWindow
== 0) {
38 tSaveWindow
= Window_Create("xfer_send", template_TITLEMIN
);
40 Icon_SetText(tSaveWindow
, FILENAME_ICON
, szOutfile
);
41 Window_Show(tSaveWindow
, open_UNDERPOINTER
);
42 (void)Save_InitSaveWindowHandler(tSaveWindow
, FALSE
, TRUE
, TRUE
,
43 DRAG_SPRITE
, OK_BUTTON
, CANCEL_BUTTON
, FILENAME_ICON
,
44 save_function
, NULL
, NULL
, tEstSize
, iFileType
, pvReference
);
48 bWrite2File(void *pvBytes
, size_t tSize
, FILE *pFile
, const char *szFilename
)
50 if (fwrite(pvBytes
, sizeof(char), tSize
, pFile
) != tSize
) {
51 werr(0, "I can't write to '%s'", szFilename
);
55 } /* end of bWrite2File */
58 * bText2File - Save the generated draw file to a Text file
61 bText2File(char *szFilename
, void *pvHandle
)
65 drawfile_object
*pObj
;
68 int iToGo
, iX
, iYtopPrev
, iHeight
, iLines
;
69 BOOL bFirst
, bIndent
, bSuccess
;
71 TRACE_MSG("bText2File");
73 fail(szFilename
== NULL
|| szFilename
[0] == '\0');
74 fail(pvHandle
== NULL
);
78 pDiag
= (diagram_type
*)pvHandle
;
79 pFile
= fopen(szFilename
, "w");
81 werr(0, "I can't open '%s' for writing", szFilename
);
86 iHeight
= (int)lWord2DrawUnits20(DEFAULT_FONT_SIZE
);
88 fail(pDiag
->tInfo
.length
< offsetof(drawfile_diagram
, objects
));
89 iToGo
= pDiag
->tInfo
.length
- offsetof(drawfile_diagram
, objects
);
91 pcTmp
= (const char *)pDiag
->tInfo
.data
+
92 offsetof(drawfile_diagram
, objects
);
93 while (iToGo
> 0 && bSuccess
) {
94 pObj
= (drawfile_object
*)pcTmp
;
96 case drawfile_TYPE_TEXT
:
97 pText
= &pObj
->data
.text
;
98 /* Compute the number of lines */
99 iLines
= (iYtopPrev
- pText
->bbox
.max
.y
+
100 iHeight
/ 2) / iHeight
;
101 DBG_DEC_C(iLines
< 0, iYtopPrev
);
102 DBG_DEC_C(iLines
< 0, pText
->bbox
.max
.y
);
104 bIndent
= iLines
> 0 || bFirst
;
106 /* Print the newlines */
107 while (iLines
> 0 && bSuccess
) {
108 bSuccess
= bWrite2File("\n",
109 1, pFile
, szFilename
);
112 /* Print the indentation */
113 if (bIndent
&& bSuccess
) {
114 for (iX
= Drawfile_ScreenToDraw(8);
115 iX
<= pText
->bbox
.min
.x
&& bSuccess
;
116 iX
+= Drawfile_ScreenToDraw(16)) {
117 bSuccess
= bWrite2File(" ",
118 1, pFile
, szFilename
);
124 /* Print the text object */
125 bSuccess
= bWrite2File(pText
->text
,
126 strlen(pText
->text
), pFile
, szFilename
);
127 /* Setup for the next object */
128 iYtopPrev
= pText
->bbox
.max
.y
;
129 iHeight
= pText
->bbox
.max
.y
- pText
->bbox
.min
.y
;
131 case drawfile_TYPE_FONT_TABLE
:
132 case drawfile_TYPE_PATH
:
133 case drawfile_TYPE_SPRITE
:
134 case drawfile_TYPE_JPEG
:
135 /* These are not relevant in a textfile */
145 DBG_DEC_C(iToGo
!= 0, iToGo
);
147 bSuccess
= bWrite2File("\n", 1, pFile
, szFilename
);
151 vSetFiletype(szFilename
, FILETYPE_TEXT
);
153 (void)remove(szFilename
);
154 werr(0, "Unable to save textfile '%s'", szFilename
);
157 } /* end of bText2File */
160 * bSaveTextfile - save the diagram as a text file
163 bSaveTextfile(event_pollblock
*pEvent
, void *pvReference
)
166 size_t tRecLen
, tNbrRecs
, tEstSize
;
168 TRACE_MSG("bSaveTextfile");
170 fail(pEvent
== NULL
);
171 fail(pvReference
== NULL
);
173 pDiag
= (diagram_type
*)pvReference
;
175 switch (pEvent
->type
) {
176 case event_SEND
: /* From a menu */
177 fail(pEvent
->data
.message
.header
.action
!= message_MENUWARN
);
178 if (menu_currentopen
!= pDiag
->pSaveMenu
||
179 pEvent
->data
.message
.data
.menuwarn
.selection
[0] !=
184 case event_KEY
: /* From a key short cut */
185 if (pEvent
->data
.key
.caret
.window
!= pDiag
->tMainWindow
) {
190 DBG_DEC(pEvent
->type
);
194 tRecLen
= sizeof(drawfile_text
) + DEFAULT_SCREEN_WIDTH
* 2 / 3;
195 tNbrRecs
= pDiag
->tInfo
.length
/ tRecLen
+ 1;
196 tEstSize
= tNbrRecs
* DEFAULT_SCREEN_WIDTH
* 2 / 3;
199 saveas(FILETYPE_TEXT
, "WordText", tEstSize
, bText2File
, pDiag
);
201 } /* end of bSaveTextfile */
204 * bDraw2File - Save the generated draw file to a Draw file
206 * Remark: This is not a simple copy action. The origin of the
207 * coordinates (0,0) must move from the top-left corner to the
208 * bottom-left corner.
211 bDraw2File(char *szFilename
, void *pvHandle
)
214 diagram_type
*pDiagram
;
216 drawfile_object
*pObj
;
217 drawfile_text
*pText
;
218 drawfile_path
*pPath
;
219 drawfile_sprite
*pSprite
;
220 drawfile_jpeg
*pJpeg
;
223 int iYadd
, iToGo
, iSize
;
226 TRACE_MSG("bDraw2File");
228 fail(szFilename
== NULL
|| szFilename
[0] == '\0');
229 fail(pvHandle
== NULL
);
231 NO_DBG_MSG(szFilename
);
233 pDiagram
= (diagram_type
*)pvHandle
;
234 pFile
= fopen(szFilename
, "wb");
236 werr(0, "I can't open '%s' for writing", szFilename
);
239 iToGo
= pDiagram
->tInfo
.length
;
241 pcTmp
= pDiagram
->tInfo
.data
;
242 bSuccess
= bWrite2File(pcTmp
,
243 offsetof(drawfile_diagram
, bbox
), pFile
, szFilename
);
245 pcTmp
+= offsetof(drawfile_diagram
, bbox
);
246 iToGo
-= offsetof(drawfile_diagram
, bbox
);
247 pBbox
= (wimp_box
*)pcTmp
;
248 iYadd
= -pBbox
->min
.y
;
249 pBbox
->min
.y
+= iYadd
;
250 pBbox
->max
.y
+= iYadd
;
251 bSuccess
= bWrite2File(pcTmp
,
252 sizeof(*pBbox
), pFile
, szFilename
);
253 iToGo
-= sizeof(*pBbox
);
255 pcTmp
+= sizeof(*pBbox
);
259 while (iToGo
> 0 && bSuccess
) {
260 pObj
= (drawfile_object
*)pcTmp
;
262 switch (pObj
->type
) {
263 case drawfile_TYPE_FONT_TABLE
:
264 bSuccess
= bWrite2File(pcTmp
,
265 iSize
, pFile
, szFilename
);
269 case drawfile_TYPE_TEXT
:
270 pText
= &pObj
->data
.text
;
271 /* First correct the coordinates */
272 pText
->bbox
.min
.y
+= iYadd
;
273 pText
->bbox
.max
.y
+= iYadd
;
274 pText
->base
.y
+= iYadd
;
275 /* Now write the information to file */
276 bSuccess
= bWrite2File(pcTmp
,
277 iSize
, pFile
, szFilename
);
281 case drawfile_TYPE_PATH
:
282 pPath
= &pObj
->data
.path
;
283 /* First correct the coordinates */
284 pPath
->bbox
.min
.y
+= iYadd
;
285 pPath
->bbox
.max
.y
+= iYadd
;
286 /* Now write the information to file */
287 bSuccess
= bWrite2File(pPath
,
288 sizeof(*pPath
), pFile
, szFilename
);
289 pcTmp
+= sizeof(*pPath
);
290 iSize
= pObj
->size
- sizeof(*pPath
);
291 fail(iSize
< 14 * sizeof(int));
292 /* Second correct the path coordinates */
293 piPath
= xmalloc(iSize
);
294 memcpy(piPath
, pcTmp
, iSize
);
300 bSuccess
= bWrite2File(piPath
,
301 iSize
, pFile
, szFilename
);
304 piPath
= xfree(piPath
);
307 case drawfile_TYPE_SPRITE
:
308 pSprite
= &pObj
->data
.sprite
;
309 /* First correct the coordinates */
310 pSprite
->bbox
.min
.y
+= iYadd
;
311 pSprite
->bbox
.max
.y
+= iYadd
;
312 /* Now write the information to file */
313 bSuccess
= bWrite2File(pcTmp
,
314 iSize
, pFile
, szFilename
);
318 case drawfile_TYPE_JPEG
:
319 pJpeg
= &pObj
->data
.jpeg
;
320 /* First correct the coordinates */
321 pJpeg
->bbox
.min
.y
+= iYadd
;
322 pJpeg
->bbox
.max
.y
+= iYadd
;
323 pJpeg
->trfm
.entries
[2][1] += iYadd
;
324 /* Now write the information to file */
325 bSuccess
= bWrite2File(pcTmp
,
326 iSize
, pFile
, szFilename
);
336 DBG_DEC_C(iToGo
!= 0, iToGo
);
339 vSetFiletype(szFilename
, FILETYPE_DRAW
);
341 (void)remove(szFilename
);
342 werr(0, "Unable to save drawfile '%s'", szFilename
);
345 } /* end of bDraw2File */
348 * bSaveDrawfile - save the diagram as a draw file
351 bSaveDrawfile(event_pollblock
*pEvent
, void *pvReference
)
356 TRACE_MSG("bSaveDrawfile");
358 fail(pEvent
== NULL
);
359 fail(pvReference
== NULL
);
361 pDiag
= (diagram_type
*)pvReference
;
363 switch (pEvent
->type
) {
364 case event_SEND
: /* From a menu */
365 fail(pEvent
->data
.message
.header
.action
!= message_MENUWARN
);
366 if (menu_currentopen
!= pDiag
->pSaveMenu
||
367 pEvent
->data
.message
.data
.menuwarn
.selection
[0] !=
372 case event_KEY
: /* From a key short cut */
373 if (pEvent
->data
.key
.caret
.window
!= pDiag
->tMainWindow
) {
378 DBG_DEC(pEvent
->type
);
382 tEstSize
= pDiag
->tInfo
.length
;
385 saveas(FILETYPE_DRAW
, "WordDraw", tEstSize
, bDraw2File
, pDiag
);
387 } /* end of bSaveDrawfile */