00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <strings.h>
00036
00037 #include "FileManager.h"
00038 #include "FileMgr_page.h"
00039 #include "ink.h"
00040 #include "ink_draw.h"
00041 #include "ink_intersect.h"
00042
00043 #include "ScribbleCore.h"
00044 #include "ScribbleLog.h"
00045 #include "ScribbleIPC.h"
00046 #include "erbusy.h"
00047
00048 #include "ScribbleUtils.h"
00049 #include "ScribbleSaveThread.h"
00050
00051
00052
00053 void genNewCurrentStroke(PScribbleCoreCtx pScribbleCoreCtx)
00054 {
00055 pScribbleCoreCtx->pCurrStroke=construct_stroke();
00056 }
00057
00058 void initScribbleCoreCtx(PScribbleCoreCtx pScribbleCoreCtx)
00059 {
00060 pScribbleCoreCtx->pointsBuf.count=0;
00061 pScribbleCoreCtx->pink=NULL;
00062 pScribbleCoreCtx->pDelInk=construct_ink();
00063 genNewCurrentStroke(pScribbleCoreCtx);
00064 pScribbleCoreCtx->bNeedSave=FALSE;
00065 }
00066
00067
00068 void destroyScribbleCoreCtx(PScribbleCoreCtx pScribbleCoreCtx)
00069 {
00070 if(pScribbleCoreCtx->pDelInk)
00071 {
00072 destroy_ink(pScribbleCoreCtx->pDelInk);
00073 pScribbleCoreCtx->pDelInk=NULL;
00074 }
00075 if(pScribbleCoreCtx->pCurrStroke)
00076 {
00077 destroy_stroke(pScribbleCoreCtx->pCurrStroke);
00078 pScribbleCoreCtx->pCurrStroke=NULL;
00079 }
00080 }
00081
00082 void core_process_points (PScribbleUICtx pScribbleUICtx)
00083 {
00084
00085 PointsBuf *pPointsBuf=&pScribbleUICtx->scribbleCoreCtx.pointsBuf;
00086
00087 if (pPointsBuf->count > 0)
00088 {
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 ioctl (pScribbleUICtx->fbdev,FBIO_DRAWPIXELS,pPointsBuf);
00105 pPointsBuf->count = 0;
00106 }
00107
00108 }
00109
00110
00111 void core_flush_delstroke_onscreen(PScribbleUICtx pScribbleUICtx)
00112 {
00113
00114 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00115 PtrInk pDelInk=pScribbleCoreCtx->pDelInk;
00116 if( NULL==pDelInk || 0==pDelInk->nStrokes) return;
00117
00118 SB_LOGPRINTF("deleted strokes=%d\n",pDelInk->nStrokes);
00119 pScribbleCoreCtx->bNeedSave=TRUE;
00120 PtrStroke pCurStroke=pDelInk->firstStroke;
00121 while(pCurStroke)
00122 {
00123 int iPenDown=TRUE;
00124 PtrInkPoint pCurPoint=pCurStroke->firstPoint;
00125 while(pCurPoint)
00126 {
00127 if( NULL==pCurPoint->nextPoint)
00128 {
00129 iPenDown=False;
00130 }
00131 int iCurrPointNum=core_addpoint2buf(
00132 &pScribbleUICtx->scribbleCoreCtx.pointsBuf,
00133 pCurPoint->x,
00134 pCurPoint->y,
00135 pCurStroke->iPenSize,
00136 COLOR_WHITE,iPenDown);
00137
00138 if( PIXELBUFSIZE==iCurrPointNum )
00139 {
00140 core_process_points(pScribbleUICtx);
00141 }
00142 pCurPoint=pCurPoint->nextPoint;
00143 }
00144 pCurStroke=pCurStroke->nextStroke;
00145 }
00146 core_process_points ( pScribbleUICtx );
00147
00148 destroy_ink(pScribbleCoreCtx->pDelInk);
00149
00150 pScribbleCoreCtx->pDelInk=construct_ink();
00151 }
00152
00153
00154
00155 int core_addpoint2buf(PointsBuf *ppointsBuf,int x,int y,
00156 int iPenSize,unsigned char iHwColor,
00157 unsigned char iPenDown)
00158 {
00159 unsigned int count=ppointsBuf->count;
00160 if ( count < PIXELBUFSIZE)
00161 {
00162 ppointsBuf->points [count].x = x;
00163 ppointsBuf->points [count].y = y;
00164 ppointsBuf->points [count].size = iPenSize;
00165 ppointsBuf->points [count].color = iHwColor;
00166 ppointsBuf->points [count].pen_down = iPenDown;
00167 ppointsBuf->count++;
00168 }
00169 return ppointsBuf->count;
00170 }
00173 void core_add_point (PScribbleUICtx pScribbleUICtx,int x, int y)
00174 {
00175 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00176 PointsBuf *ppointsBuf=&pScribbleCoreCtx->pointsBuf;
00177
00178 core_addpoint2buf(ppointsBuf,x,y,
00179 getPenSize(),getPenColor(),pScribbleUICtx->pen_down);
00180
00181
00182 PtrInkPoint pPoint=construct_point ();
00183 pPoint->x=x;
00184 pPoint->y=y;
00185 PtrStroke pStroke=pScribbleCoreCtx->pCurrStroke;
00186 ink_add_point(pStroke, pPoint);
00187 if(!pScribbleUICtx->pen_down)
00188 {
00189
00190
00191
00192
00193
00194
00195
00196 pStroke->iPenSize=getPenSize();
00197
00198 Util_GdkColorFromHWCOLOR(&pStroke->gdkColor,getPenColor());
00199 ink_add_stroke(pScribbleCoreCtx->pink,pStroke);
00200
00201 draw_stroke(pStroke);
00202
00203
00204 flushPixMap(pScribbleUICtx);
00205 genNewCurrentStroke(pScribbleCoreCtx);
00206
00207 }
00208 pScribbleCoreCtx->bNeedSave=TRUE;
00209 }
00210
00211 void core_eraseStrokes(PScribbleUICtx pScribbleUICtx,GdkPoint *pCurPoint)
00212 {
00213 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00214 delStrokesByLine(pScribbleCoreCtx->pink,
00215 &pScribbleUICtx->lastPoint,
00216 pCurPoint,
00217 pScribbleCoreCtx->pDelInk);
00218
00219 }
00220
00221
00222 void core_eraseInterSectLines(PScribbleUICtx pScribbleUICtx,GdkPoint *pCurPoint)
00223 {
00224 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00225 delInterSectLines(pScribbleCoreCtx->pink,
00226 &pScribbleUICtx->lastPoint,
00227 pCurPoint);
00228 }
00229
00230
00231 int core_loadInk(PScribbleUICtx pScribbleUICtx)
00232 {
00233 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00234 destroyScribbleCoreCtx(pScribbleCoreCtx);
00235 initScribbleCoreCtx(pScribbleCoreCtx);
00236
00237 SB_TIMEDISPLAY("load ink file\n");
00238 pScribbleCoreCtx->pink=fm_loadCurrInk(&pScribbleUICtx->fileManager);
00239 if( NULL == pScribbleCoreCtx->pink)
00240 {
00241 SB_ERRORPRINTF("load ink error\n");
00242 return -1;
00243 }
00244 SB_TIMEDISPLAY("load ink succsessful(strokes=%d)\n",
00245 pScribbleCoreCtx->pink->nStrokes);
00246 return 0;
00247 }
00248
00249 void drawInitBgPixMap2CurrPixMap(PScribbleUICtx pScribbleUICtx)
00250 {
00251
00252 GtkWidget* widget=pScribbleUICtx->drawing_area;
00253
00254 gdk_draw_drawable(pScribbleUICtx->pixmap,
00255 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
00256 pScribbleUICtx->initBgPixMap,
00257 0,0,0,0,-1,-1);
00258 }
00259
00260
00261
00262 int loadPageAndResultImg(PScribbleUICtx pScribbleUICtx)
00263 {
00264 SB_LOGPRINTF("__start__\n");
00265
00266 if( 0!=core_loadInk(pScribbleUICtx) ) return -1;
00267
00268
00269 gboolean bNeedDrawInk=TRUE;
00270 PFileManager pFM=&pScribbleUICtx->fileManager;
00271
00272 const char* sCurrImgFile=getCurrImgFileName(pFM);
00273 if( NULL==pScribbleUICtx->sLastTmplImg
00274 || 0!=strcasecmp(sCurrImgFile,pScribbleUICtx->sLastTmplImg) )
00275 {
00276 GdkPixbuf *pbuf=NULL;
00277 SB_TIMEDISPLAY("load image..start\n");
00278
00279 char sAbsPath[MAX_FILENAME];
00280 sCurrImgFile=getAbsPathFromCurrScrib
00281 (sAbsPath,sizeof(sAbsPath),sCurrImgFile,pFM);
00282
00283
00284 GtkWidget* widget=pScribbleUICtx->drawing_area;
00285 int width=widget->allocation.width;
00286 int height=widget->allocation.height;
00287
00288
00289 if( 0!= util_loadImage_bysize(&pbuf,sCurrImgFile,width,height))
00290 {
00291 SB_ERRORPRINTF("load image\n(%s) error\n",sCurrImgFile);
00292 if(pbuf) g_object_unref(pbuf);
00293 return -1;
00294 }
00295 pScribbleUICtx->sLastTmplImg=sCurrImgFile;
00296
00297 if(pScribbleUICtx->initBgPixMap)
00298 {
00299 g_object_unref(pScribbleUICtx->initBgPixMap);
00300 }
00301 pScribbleUICtx->initBgPixMap=gdk_pixmap_new(widget->window,width,height,-1);
00302
00303 gdk_draw_rectangle (pScribbleUICtx->initBgPixMap, widget->style->white_gc, TRUE,
00304 0, 0, width, height);
00305
00306 int xPos, yPos, w, h;
00307 w = gdk_pixbuf_get_width(pbuf);
00308 h = gdk_pixbuf_get_height(pbuf);
00309 xPos = (width - w) / 2;
00310 yPos = (height - h) / 2;
00311
00312
00313 gdk_draw_pixbuf(pScribbleUICtx->initBgPixMap, NULL, pbuf,
00314 0, 0, xPos, yPos,-1,-1,
00315 GDK_RGB_DITHER_NONE, 0, 0);
00316
00317 if(pbuf) g_object_unref(pbuf);
00318
00319 SB_TIMEDISPLAY("load image success\n(%s)\n",sCurrImgFile);
00320 }
00321 drawInitBgPixMap2CurrPixMap(pScribbleUICtx);
00322 SB_TIMEDISPLAY("after draw bgPixMap\n");
00323 if(bNeedDrawInk)
00324 {
00325 draw_ink(pScribbleUICtx->scribbleCoreCtx.pink);
00326 }
00327 SB_TIMEDISPLAY("after draw ink\n");
00328 pScribbleUICtx->scribbleCoreCtx.bNeedSave=FALSE;
00329 SB_LOGPRINTF("__end_\n");
00330 return 0;
00331 }
00332
00333
00334
00335 int savePageAndResultImg(PScribbleUICtx pScribbleUICtx,gboolean bAppQuit)
00336 {
00337 PScribbleCoreCtx pScribbleCoreCtx=&pScribbleUICtx->scribbleCoreCtx;
00338 if(!pScribbleCoreCtx->bNeedSave && !bAppQuit)
00339 {
00340 SB_LOGPRINTF("__saved or no modification!__\n");
00341 return 0;
00342 }
00343
00344 SB_TIMEDISPLAY("__saving__\n");
00345 erbusy_blink();
00346
00347 PFileManager pFM=&pScribbleUICtx->fileManager;
00348 gboolean bNeedSaveBgPic=FALSE;
00349 if( pScribbleCoreCtx->bNeedSave )
00350 {
00351
00352 if( NULL==pScribbleCoreCtx->pink
00353 || 0==pScribbleCoreCtx->pink->nStrokes)
00354 {
00355
00356 fm_deleteOnePage(pFM,getCurrScribPage(pFM) );
00357
00358 deleteRelatedFiles(pFM,getCurrScribPage(pFM) );
00359 }
00360 else
00361 {
00362
00363 if( 0!=createDirHirachy(pFM) )
00364 {
00365 SB_ERRORPRINTF("error when create dir hirachy\n");
00366 erbusy_off();
00367 return -1;
00368 }
00369 SB_TIMEDISPLAY("create dir successful\n");
00370 bNeedSaveBgPic=TRUE;
00371 }
00372 }
00373
00374
00375 if(bAppQuit || bNeedSaveBgPic)
00376 {
00377
00378 fm_saveFileManager(pFM);
00379 }
00380 if (bNeedSaveBgPic)
00381 {
00382
00383 notifySave(pScribbleUICtx);
00384 setCurrentPageDirty(pFM,TRUE);
00385 fm_saveCurrPage(pFM);
00386 }
00387
00388 if (bAppQuit || bNeedSaveBgPic)
00389 {
00390 fm_saveIrexInk(pFM, bAppQuit);
00391 }
00392
00393 pScribbleCoreCtx->bNeedSave=FALSE;
00394 SB_TIMEDISPLAY("__saved done__\n");
00395
00396 return 0;
00397 }
00398
00399
00400 void flushPixMap(PScribbleUICtx pScribbleUICtx)
00401 {
00402 GtkWidget* w=pScribbleUICtx->drawing_area;
00403 gdk_draw_drawable(w->window,
00404 w->style->fg_gc[GTK_WIDGET_STATE(w)],
00405 pScribbleUICtx->pixmap,
00406 0,0,0,0,-1,-1);
00407 }
00408
00409
00410 int core_redrawBgAndStrks(PScribbleUICtx pScribbleUICtx)
00411 {
00412
00413 SB_TIMEDISPLAY("start");
00414 drawInitBgPixMap2CurrPixMap(pScribbleUICtx);
00415 draw_ink(pScribbleUICtx->scribbleCoreCtx.pink);
00416
00417 flushPixMap(pScribbleUICtx);
00418 SB_TIMEDISPLAY("end");
00419 return 0;
00420 }
00421