forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / gfx / lunapaint / src / app / project.c
blob2fa9bcfb1f18edbcd84c37902000b18a9307a75e
1 /****************************************************************************
2 * *
3 * project.c -- Lunapaint, *
4 * http://developer.berlios.de/projects/lunapaintami/ *
5 * Copyright (C) 2006, 2007, Hogne Titlestad <hogga@sub-ether.org> *
6 * Copyright (C) 2009-2011 LunaPaint Development Team *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software Foundation, *
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 * *
22 ****************************************************************************/
24 #include "project.h"
26 void CreateProjectWindow ( struct WindowList *lst )
29 STRPTR windowtitle = NULL;
30 get ( lst->win, MUIA_Window_Title, &windowtitle );
32 lst->projectWin = WindowObject,
33 MUIA_Window_ScreenTitle, (IPTR) VERSION, //__(MSG_PRJ_SCR),
34 MUIA_Window_Title, __(MSG_PRJ_WIN),
35 MUIA_Window_Screen, ( IPTR )lunaPubScreen,
36 MUIA_Window_ID, MAKE_ID('L','P','P','J'),
37 MUIA_Window_CloseGadget, TRUE,
38 WindowContents, ( IPTR )VGroup,
39 Child, ( IPTR )GroupObject,
40 MUIA_Frame, MUIV_Frame_Group,
41 Child, ( IPTR )TextObject,
42 MUIA_Text_Contents, __(MSG_PRJ_NAME),
43 End,
44 Child, ( IPTR )( lst->projName = StringObject,
45 MUIA_String_MaxLen, 128,
46 MUIA_Frame, MUIV_Frame_String,
47 MUIA_String_Contents, windowtitle,
48 MUIA_CycleChain, 1,
49 End ),
50 Child, ( IPTR )TextObject,
51 MUIA_Text_Contents, __(MSG_PRJ_AUTHOR),
52 End,
53 Child, ( IPTR )( lst->projAuthor = StringObject,
54 MUIA_String_MaxLen, 128,
55 MUIA_Frame, MUIV_Frame_String,
56 MUIA_String_Contents, ( IPTR )"",
57 MUIA_CycleChain, 1,
58 End ),
59 End,
60 Child, ( IPTR )( lst->projBtnOk = SimpleButton ( _(MSG_BUTTON_OK) ) ),
61 End,
62 End;
64 // Attach some standard methods to this gui
66 DoMethod ( PaintApp, OM_ADDMEMBER, ( IPTR )lst->projectWin );
68 DoMethod (
69 lst->projectWin, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
70 lst->projectWin, 3, MUIM_Set, MUIA_Window_Open, FALSE
72 DoMethod (
73 lst->projBtnOk, MUIM_Notify, MUIA_Pressed, FALSE,
74 lst->win, 3, MUIM_CallHook, ( IPTR )&lst->projHook, ( APTR )lst
78 void DestroyProjectWindow ( struct WindowList *lst )
80 DoMethod ( PaintApp, OM_REMMEMBER, ( IPTR )lst->projectWin );
81 MUI_DisposeObject ( lst->projectWin );
82 lst->projectWin = NULL;
85 void SaveProject ( struct WindowList *lst )
87 if ( lst->filename == NULL ) return;
89 // Set up the header
90 char version[ 16 ] = "Lunapaint_v1";
91 STRPTR author = ( STRPTR )XGET ( lst->projAuthor, MUIA_String_Contents );
92 STRPTR projname = ( STRPTR )XGET ( lst->projName, MUIA_String_Contents );
94 struct LunapaintHeader header;
95 memcpy ( header.version, version, 16 );
96 memcpy ( header.projectName, projname, strlen ( projname ) + 1 );
97 memcpy ( header.author, author, strlen ( author ) + 1 );
98 header.width = ( short )lst->canvas->width;
99 header.height = ( short )lst->canvas->height;
100 header.layerCount = ( short )lst->canvas->totalLayers;
101 header.frameCount = ( short )lst->canvas->totalFrames;
102 // There's always 3 objects for the layers
103 // one object for the opacity and one for the
104 // layer name and one for the layer visibility:
105 header.objectCount = 3 * lst->canvas->totalLayers;
107 BPTR outFile;
108 if ( ( outFile = Open ( lst->filename, MODE_NEWFILE ) ) == BNULL )
109 return;
111 // Write the header
112 Write ( outFile, &header, sizeof ( struct LunapaintHeader ) );
114 // Write description with size of desc first
115 // TODO: make this work
116 STRPTR text = " ";
117 int length = 1;
118 Write ( outFile, &length, 4 );
119 Write ( outFile, text, length );
121 // Write all the layers out!
122 int buffers = lst->canvas->totalLayers * lst->canvas->totalFrames;
123 int gfxsize = lst->canvas->width * lst->canvas->height * 8;
124 struct gfxbuffer *buf = lst->canvas->buffer;
125 int i = 0; for ( ; i < buffers; i++ )
127 Write ( outFile, buf->buf, gfxsize );
128 buf = buf->nextbuf;
131 // Write all the objects -----------------------------------
133 buf = lst->canvas->buffer;
134 struct LunaObjDesc objdesc;
135 int i = 0;
136 while ( buf != NULL )
138 int layer = i % lst->canvas->totalLayers;
139 int frame = i / lst->canvas->totalLayers;
140 // Write opacity
141 objdesc = ( struct LunaObjDesc ){ LunaObj_LayerOpacity, layer, frame, 1 };
142 Write ( outFile, &objdesc, sizeof ( struct LunaObjDesc ) );
143 Write ( outFile, &buf->opacity, 1 );
144 // Write layer visibility
145 objdesc = ( struct LunaObjDesc ){ LunaObj_LayerVisibility, layer, frame, 1 };
146 Write ( outFile, &objdesc, sizeof ( struct LunaObjDesc ) );
147 char visible = buf->visible ? 1 : 0;
148 Write ( outFile, &visible, 1 );
149 if ( buf->name != NULL && strlen ( buf->name ) > 0 )
151 // Write layer name
152 objdesc = ( struct LunaObjDesc ){ LunaObj_LayerName, layer, frame, strlen ( buf->name ) };
153 Write ( outFile, &objdesc, sizeof ( struct LunaObjDesc ) );
154 Write ( outFile, buf->name, strlen ( buf->name ) );
156 // Next
157 buf = buf->nextbuf;
158 i++;
161 // Done with layer objects ---------------------------------
163 Close ( outFile );
166 void LoadProject ( unsigned char *filename, BOOL useCurrentCanvas )
168 if ( filename == NULL ) return;
170 // Get a new filename container
171 unsigned char *tmpFilename = AllocVec ( strlen ( filename ) + 1, MEMF_CLEAR );
172 strcpy ( tmpFilename, filename );
174 BPTR inputFile;
175 if ( ( inputFile = Open ( tmpFilename, MODE_OLDFILE ) ) != BNULL )
177 // Get the lunapaint header
178 struct LunapaintHeader *header = AllocVec ( sizeof ( struct LunapaintHeader ), MEMF_CLEAR );
179 Read ( inputFile, header, sizeof ( struct LunapaintHeader ) );
180 // test if the header is correct
181 char verTpl[ ] = "Lunapaint_v1";
182 int p = 0; for ( p = 0; p < sizeof(verTpl) - 1; p++ )
184 if ( verTpl[ p ] != header->version[ p ] )
186 FreeVec ( tmpFilename );
187 FreeVec ( header );
188 Close ( inputFile );
189 ShowAlert(_(MSG_PRJ_ALERT_NOLUNA));
190 return;
194 // Read in the description
195 int desclength;
196 Read ( inputFile, &desclength, 4 );
197 STRPTR text = AllocVec ( desclength + 1, MEMF_CLEAR );
198 Read ( inputFile, text, desclength );
200 // Load in object
201 // <not implemented>
203 // Load in the bitmap data
204 int buffers = header->frameCount * header->layerCount;
205 int framesize = header->width * header->height * 8;
206 struct gfxbuffer *buf = AllocVec ( sizeof ( struct gfxbuffer ), MEMF_CLEAR );
207 struct gfxbuffer *tmp = buf;
208 int i = 0; for ( ; i < buffers; i++ )
210 tmp->buf = AllocVec ( framesize, MEMF_CLEAR );
211 tmp->opacity = 100; //
212 tmp->visible = TRUE; // <- defaults
213 tmp->name = AllocVec ( 2, MEMF_CLEAR ); //
214 Read ( inputFile, tmp->buf, framesize );
215 if ( i + 1 < buffers )
217 tmp->nextbuf = AllocVec ( sizeof ( struct gfxbuffer ), MEMF_CLEAR );
218 tmp = tmp->nextbuf;
220 else
221 tmp->nextbuf = NULL;
223 // Layer objects
224 if ( header->objectCount > 0 )
226 for ( i = 0; i < header->objectCount; i++ )
228 struct LunaObjDesc desc;
229 Read ( inputFile, &desc, sizeof ( struct LunaObjDesc ) );
230 switch ( desc.type )
232 case LunaObj_LayerOpacity:
234 char opacity;
235 Read ( inputFile, &opacity, 1 );
236 struct gfxbuffer *b = getGfxbufferFromList ( buf, desc.layer, desc.frame, header->layerCount, header->frameCount );
237 b->opacity = opacity;
239 break;
240 case LunaObj_LayerVisibility:
242 BOOL visible;
243 char vis;
244 Read ( inputFile, &vis, 1 );
245 visible = vis == 1 ? TRUE : FALSE;
246 struct gfxbuffer *b = getGfxbufferFromList ( buf, desc.layer, desc.frame, header->layerCount, header->frameCount );
247 b->visible = visible;
249 break;
250 case LunaObj_LayerName:
252 unsigned char *name = AllocVec ( desc.datalength + 1, MEMF_ANY|MEMF_CLEAR );
253 Read ( inputFile, name, desc.datalength );
254 struct gfxbuffer *b = getGfxbufferFromList ( buf, desc.layer, desc.frame, header->layerCount, header->frameCount );
255 if ( b->name ) FreeVec ( b->name ); // Free the dummy allocation
256 b->name = AllocVec ( desc.datalength + 1, MEMF_ANY|MEMF_CLEAR ); // +1 = null terminator
257 strncpy ( b->name, name, desc.datalength );
258 FreeVec ( name );
260 break;
261 default:
262 break;
267 // We're done reading now
268 Close ( inputFile );
270 // If loading new
271 if ( !useCurrentCanvas )
272 addCanvaswindow ( header->width, header->height, header->layerCount, header->frameCount, FALSE );
273 // Loading over old
274 else
276 globalActiveCanvas->totalLayers = header->layerCount;
277 globalActiveCanvas->totalFrames = header->frameCount;
278 Destroy_Buffer ( globalActiveCanvas );
280 globalActiveCanvas->buffer = buf;
281 setActiveBuffer ( globalActiveCanvas );
284 if ( globalActiveWindow->filename != NULL )
285 FreeVec ( globalActiveWindow->filename );
286 globalActiveWindow->filename = tmpFilename;
288 set ( globalActiveWindow->win, MUIA_Window_Title, ( IPTR )header->projectName );
289 set ( globalActiveWindow->projName, MUIA_String_Contents, ( IPTR )header->projectName );
290 set ( globalActiveWindow->projAuthor, MUIA_String_Contents, ( IPTR )header->author );
292 // TODO: Do something with the project description
294 if ( !useCurrentCanvas )
295 set ( globalActiveWindow->win, MUIA_Window_Open, TRUE );
297 // Free description
298 FreeVec ( header );
299 FreeVec ( text );
300 return;
302 FreeVec ( tmpFilename );