Date: 2022-08-02T03:13:17+00:00

Actual source code: draw.c

      2: /*
      3:        Provides the calling sequences for all the basic PetscDraw routines.
      4: */
      5: #include <petsc/private/drawimpl.h>
      6: #include <petscviewer.h>

      8: PetscClassId PETSC_DRAW_CLASSID;

     10: static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
     11: /*@C
     12:   PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
     13:   called from PetscFinalize().

     15:   Level: developer

     17: .seealso: PetscFinalize()
     18: @*/
     19: PetscErrorCode  PetscDrawFinalizePackage(void)
     20: {
     21:   PetscFunctionListDestroy(&PetscDrawList);
     22:   PetscDrawPackageInitialized = PETSC_FALSE;
     23:   PetscDrawRegisterAllCalled  = PETSC_FALSE;
     24:   return 0;
     25: }

     27: /*@C
     28:   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
     29:   from PetscDLLibraryRegister_petsc() when using dynamic libraries, and on the call to PetscInitialize()
     30:   when using shared or static libraries.

     32:   Level: developer

     34: .seealso: PetscInitialize()
     35: @*/
     36: PetscErrorCode  PetscDrawInitializePackage(void)
     37: {
     38:   char           logList[256];
     39:   PetscBool      opt,pkg;

     41:   if (PetscDrawPackageInitialized) return 0;
     42:   PetscDrawPackageInitialized = PETSC_TRUE;
     43:   /* Register Classes */
     44:   PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
     45:   PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
     46:   PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
     47:   PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
     48:   PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
     49:   PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
     50:   /* Register Constructors */
     51:   PetscDrawRegisterAll();
     52:   /* Process Info */
     53:   {
     54:     PetscClassId  classids[6];

     56:     classids[0] = PETSC_DRAW_CLASSID;
     57:     classids[1] = PETSC_DRAWAXIS_CLASSID;
     58:     classids[2] = PETSC_DRAWLG_CLASSID;
     59:     classids[3] = PETSC_DRAWHG_CLASSID;
     60:     classids[4] = PETSC_DRAWBAR_CLASSID;
     61:     classids[5] = PETSC_DRAWSP_CLASSID;
     62:     PetscInfoProcessClass("draw", 6, classids);
     63:   }
     64:   /* Process summary exclusions */
     65:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
     66:   if (opt) {
     67:     PetscStrInList("draw",logList,',',&pkg);
     68:     if (pkg) {
     69:       PetscLogEventExcludeClass(PETSC_DRAW_CLASSID);
     70:       PetscLogEventExcludeClass(PETSC_DRAWAXIS_CLASSID);
     71:       PetscLogEventExcludeClass(PETSC_DRAWLG_CLASSID);
     72:       PetscLogEventExcludeClass(PETSC_DRAWHG_CLASSID);
     73:       PetscLogEventExcludeClass(PETSC_DRAWBAR_CLASSID);
     74:       PetscLogEventExcludeClass(PETSC_DRAWSP_CLASSID);
     75:     }
     76:   }
     77:   /* Register package finalizer */
     78:   PetscRegisterFinalize(PetscDrawFinalizePackage);
     79:   return 0;
     80: }

     82: /*@
     83:    PetscDrawResizeWindow - Allows one to resize a window from a program.

     85:    Collective on PetscDraw

     87:    Input Parameters:
     88: +  draw - the window
     89: -  w,h - the new width and height of the window

     91:    Level: intermediate

     93: .seealso: PetscDrawCheckResizedWindow()
     94: @*/
     95: PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
     96: {
    100:   if (draw->ops->resizewindow) {
    101:     (*draw->ops->resizewindow)(draw,w,h);
    102:   }
    103:   return 0;
    104: }

    106: /*@
    107:    PetscDrawGetWindowSize - Gets the size of the window.

    109:    Not collective

    111:    Input Parameter:
    112: .  draw - the window

    114:    Output Parameters:
    115: .  w,h - the window width and height

    117:    Level: intermediate

    119: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
    120: @*/
    121: PetscErrorCode  PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
    122: {
    126:   if (w) *w = draw->w;
    127:   if (h) *h = draw->h;
    128:   return 0;
    129: }

    131: /*@
    132:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

    134:    Collective on PetscDraw

    136:    Input Parameter:
    137: .  draw - the window

    139:    Level: advanced

    141: .seealso: PetscDrawResizeWindow()

    143: @*/
    144: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
    145: {
    147:   if (draw->ops->checkresizedwindow) {
    148:     (*draw->ops->checkresizedwindow)(draw);
    149:   }
    150:   return 0;
    151: }

    153: /*@C
    154:    PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.

    156:    Not collective

    158:    Input Parameter:
    159: .  draw - the graphics context

    161:    Output Parameter:
    162: .  title - the title

    164:    Level: intermediate

    166: .seealso: PetscDrawSetTitle()
    167: @*/
    168: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,const char *title[])
    169: {
    172:   *title = draw->title;
    173:   return 0;
    174: }

    176: /*@C
    177:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

    179:    Collective on PetscDraw

    181:    Input Parameters:
    182: +  draw - the graphics context
    183: -  title - the title

    185:    Level: intermediate

    187:    Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
    188:    in the image.

    190:    A copy of the string is made, so you may destroy the
    191:    title string after calling this routine.

    193:    You can use PetscDrawAxisSetLabels() to indicate a title within the window

    195: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
    196: @*/
    197: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
    198: {
    201:   PetscFree(draw->title);
    202:   PetscStrallocpy(title,&draw->title);
    203:   if (draw->ops->settitle) {
    204:     (*draw->ops->settitle)(draw,draw->title);
    205:   }
    206:   return 0;
    207: }

    209: /*@C
    210:    PetscDrawAppendTitle - Appends to the title of a PetscDraw context.

    212:    Collective on PetscDraw

    214:    Input Parameters:
    215: +  draw - the graphics context
    216: -  title - the title

    218:    Note:
    219:    A copy of the string is made, so you may destroy the
    220:    title string after calling this routine.

    222:    Level: advanced

    224: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
    225: @*/
    226: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
    227: {
    230:   if (!title || !title[0]) return 0;

    232:   if (draw->title) {
    233:     size_t len1,len2;
    234:     char   *newtitle;
    235:     PetscStrlen(title,&len1);
    236:     PetscStrlen(draw->title,&len2);
    237:     PetscMalloc1(len1 + len2 + 1,&newtitle);
    238:     PetscStrcpy(newtitle,draw->title);
    239:     PetscStrcat(newtitle,title);
    240:     PetscFree(draw->title);
    241:     draw->title = newtitle;
    242:   } else {
    243:     PetscStrallocpy(title,&draw->title);
    244:   }
    245:   if (draw->ops->settitle) {
    246:     (*draw->ops->settitle)(draw,draw->title);
    247:   }
    248:   return 0;
    249: }

    251: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
    252: {
    253:   if (!draw->ops->save && !draw->ops->getimage) return 0;
    254:   PetscDrawSaveMovie(draw);
    255:   if (draw->savefinalfilename) {
    256:     draw->savesinglefile = PETSC_TRUE;
    257:     PetscDrawSetSave(draw,draw->savefinalfilename);
    258:     PetscDrawSave(draw);
    259:   }
    260:   PetscBarrier((PetscObject)draw);
    261:   return 0;
    262: }

    264: /*@
    265:    PetscDrawDestroy - Deletes a draw context.

    267:    Collective on PetscDraw

    269:    Input Parameters:
    270: .  draw - the drawing context

    272:    Level: beginner

    274: .seealso: PetscDrawCreate()

    276: @*/
    277: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
    278: {
    279:   if (!*draw) return 0;
    281:   if (--((PetscObject)(*draw))->refct > 0) return 0;

    283:   if ((*draw)->pause == -2) {
    284:     (*draw)->pause = -1;
    285:     PetscDrawPause(*draw);
    286:   }

    288:   /* if memory was published then destroy it */
    289:   PetscObjectSAWsViewOff((PetscObject)*draw);

    291:   PetscDrawDestroy_Private(*draw);

    293:   if ((*draw)->ops->destroy) {
    294:     (*(*draw)->ops->destroy)(*draw);
    295:   }
    296:   PetscDrawDestroy(&(*draw)->popup);
    297:   PetscFree((*draw)->title);
    298:   PetscFree((*draw)->display);
    299:   PetscFree((*draw)->savefilename);
    300:   PetscFree((*draw)->saveimageext);
    301:   PetscFree((*draw)->savemovieext);
    302:   PetscFree((*draw)->savefinalfilename);
    303:   PetscHeaderDestroy(draw);
    304:   return 0;
    305: }

    307: /*@
    308:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

    310:    Collective on PetscDraw

    312:    Input Parameter:
    313: .  draw - the original window

    315:    Output Parameter:
    316: .  popup - the new popup window

    318:    Level: advanced

    320: .seealso: PetscDrawScalePopup(), PetscDrawCreate()

    322: @*/
    323: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
    324: {

    328:   if (draw->popup) *popup = draw->popup;
    329:   else if (draw->ops->getpopup) {
    330:     (*draw->ops->getpopup)(draw,popup);
    331:     if (*popup) {
    332:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
    333:       (*popup)->pause = 0.0;
    334:       PetscDrawSetFromOptions(*popup);
    335:     }
    336:   } else *popup = NULL;
    337:   return 0;
    338: }

    340: /*@C
    341:   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed

    343:   Input Parameters:
    344: + draw - the drawing context
    345: - display - the X windows display

    347:   Level: advanced

    349: .seealso: PetscDrawCreate()

    351: @*/
    352: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
    353: {
    354:   PetscFree(draw->display);
    355:   PetscStrallocpy(display,&draw->display);
    356:   return 0;
    357: }

    359: /*@
    360:    PetscDrawSetDoubleBuffer - Sets a window to be double buffered.

    362:    Logically Collective on PetscDraw

    364:    Input Parameter:
    365: .  draw - the drawing context

    367:    Level: intermediate

    369: @*/
    370: PetscErrorCode  PetscDrawSetDoubleBuffer(PetscDraw draw)
    371: {
    373:   if (draw->ops->setdoublebuffer) {
    374:     (*draw->ops->setdoublebuffer)(draw);
    375:   }
    376:   return 0;
    377: }

    379: /*@C
    380:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
    381:         by the one process.

    383:    Collective on PetscDraw

    385:    Input Parameter:
    386: .  draw - the original window

    388:    Output Parameter:
    389: .  sdraw - the singleton window

    391:    Level: advanced

    393: .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

    395: @*/
    396: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
    397: {
    398:   PetscMPIInt    size;

    403:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
    404:   if (size == 1) {
    405:     PetscObjectReference((PetscObject)draw);
    406:     *sdraw = draw;
    407:   } else {
    408:     if (draw->ops->getsingleton) {
    409:       (*draw->ops->getsingleton)(draw,sdraw);
    410:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
    411:   }
    412:   return 0;
    413: }

    415: /*@C
    416:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
    417:         by the one process.

    419:    Collective on PetscDraw

    421:    Input Parameters:
    422: +  draw - the original window
    423: -  sdraw - the singleton window

    425:    Level: advanced

    427: .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

    429: @*/
    430: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
    431: {
    432:   PetscMPIInt    size;

    438:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
    439:   if (size == 1) {
    440:     if (draw == *sdraw) {
    441:       PetscObjectDereference((PetscObject)draw);
    442:       *sdraw = NULL;
    443:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
    444:   } else {
    445:     if (draw->ops->restoresingleton) {
    446:       (*draw->ops->restoresingleton)(draw,sdraw);
    447:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
    448:   }
    449:   return 0;
    450: }