screenshot operator now adds file extension in the file selector and has its own...
authorCampbell Barton <ideasman42@gmail.com>
Tue, 24 Apr 2012 02:01:23 +0000 (02:01 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 24 Apr 2012 02:01:23 +0000 (02:01 +0000)
source/blender/blenkernel/BKE_image.h
source/blender/blenkernel/intern/image.c
source/blender/editors/screen/screendump.c
source/blender/editors/space_image/image_ops.c
source/blender/makesdna/DNA_scene_types.h
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_operators.c

index f54376455513ad5b7091b79c62e42acfab28cf44..c2112d1e1695258fc02bddbfe7cb511c01e74fa5 100644 (file)
@@ -68,6 +68,8 @@ char  BKE_imtype_valid_depths(const char imtype);
 
 char    BKE_imtype_from_arg(const char *arg);
 
 
 char    BKE_imtype_from_arg(const char *arg);
 
+void    BKE_imformat_defaults(struct ImageFormatData *im_format);
+
 struct anim *openanim(const char *name, int flags, int streamindex);
 
 void   image_de_interlace(struct Image *ima, int odd);
 struct anim *openanim(const char *name, int flags, int streamindex);
 
 void   image_de_interlace(struct Image *ima, int odd);
index 27779ef81077bb36f02a9db23ab226a18e920366..6e0330f531698171d02d38c1b3046b018cefc5d9 100644 (file)
@@ -1163,6 +1163,15 @@ int BKE_add_image_extension(char *string, const char imtype)
        }
 }
 
        }
 }
 
+void BKE_imformat_defaults(ImageFormatData *im_format)
+{
+       memset(im_format, 0, sizeof(*im_format));
+       im_format->planes = R_IMF_PLANES_RGB;
+       im_format->imtype = R_IMF_IMTYPE_PNG;
+       im_format->quality = 90;
+       im_format->compress = 90;
+}
+
 /* could allow access externally - 512 is for long names, 64 is for id names */
 typedef struct StampData {
        char    file[512];
 /* could allow access externally - 512 is for long names, 64 is for id names */
 typedef struct StampData {
        char    file[512];
index e12e6b0ef829f5d8b842ca4e945a134aaf9f932c..22a1770412f3772881445e90be2894047c4b8c77 100644 (file)
@@ -56,6 +56,8 @@
 #include "RNA_access.h"
 #include "RNA_define.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
 
+#include "UI_interface.h"
+
 #include "WM_types.h"
 #include "WM_api.h"
 
 #include "WM_types.h"
 #include "WM_api.h"
 
@@ -69,6 +71,8 @@ typedef struct ScreenshotData {
        unsigned int *dumprect;
        int dumpsx, dumpsy;
        rcti crop;
        unsigned int *dumprect;
        int dumpsx, dumpsy;
        rcti crop;
+
+       ImageFormatData im_format;
 } ScreenshotData;
 
 /* get shot from frontbuffer */
 } ScreenshotData;
 
 /* get shot from frontbuffer */
@@ -113,9 +117,13 @@ static int screenshot_data_create(bContext *C, wmOperator *op)
                scd->dumpsx= dumpsx;
                scd->dumpsy= dumpsy;
                scd->dumprect= dumprect;
                scd->dumpsx= dumpsx;
                scd->dumpsy= dumpsy;
                scd->dumprect= dumprect;
-               if (sa)
+               if (sa) {
                        scd->crop= sa->totrct;
                        scd->crop= sa->totrct;
-               op->customdata= scd;
+               }
+
+               BKE_imformat_defaults(&scd->im_format);
+
+               op->customdata = scd;
 
                return TRUE;
        }
 
                return TRUE;
        }
@@ -164,20 +172,13 @@ static int screenshot_exec(bContext *C, wmOperator *op)
 
        if (scd) {
                if (scd->dumprect) {
 
        if (scd) {
                if (scd->dumprect) {
-                       Scene *scene= CTX_data_scene(C);
                        ImBuf *ibuf;
                        char path[FILE_MAX];
 
                        RNA_string_get(op->ptr, "filepath", path);
                        ImBuf *ibuf;
                        char path[FILE_MAX];
 
                        RNA_string_get(op->ptr, "filepath", path);
-
-                       BLI_strncpy(G.ima, path, sizeof(G.ima));
                        BLI_path_abs(path, G.main->name);
 
                        BLI_path_abs(path, G.main->name);
 
-                       /* BKE_add_image_extension() checks for if extension was already set */
-                       if (scene->r.scemode & R_EXTENSION)
-                               if (strlen(path)<FILE_MAX-5)
-                                       BKE_add_image_extension(path, scene->r.im_format.imtype);
-
+                       /* operator ensures the extension */
                        ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
                        ibuf->rect= scd->dumprect;
 
                        ibuf= IMB_allocImBuf(scd->dumpsx, scd->dumpsy, 24, 0);
                        ibuf->rect= scd->dumprect;
 
@@ -185,7 +186,11 @@ static int screenshot_exec(bContext *C, wmOperator *op)
                        if (!RNA_boolean_get(op->ptr, "full"))
                                screenshot_crop(ibuf, scd->crop);
 
                        if (!RNA_boolean_get(op->ptr, "full"))
                                screenshot_crop(ibuf, scd->crop);
 
-                       BKE_write_ibuf(ibuf, path, &scene->r.im_format);
+                       if (scd->im_format.planes == R_IMF_PLANES_BW) {
+                               /* bw screenshot? - users will notice if it fails! */
+                               IMB_color_to_bw(ibuf);
+                       }
+                       BKE_write_ibuf(ibuf, path, &scd->im_format);
 
                        IMB_freeImBuf(ibuf);
                }
 
                        IMB_freeImBuf(ibuf);
                }
@@ -200,8 +205,9 @@ static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event)
        if (screenshot_data_create(C, op)) {
                if (RNA_struct_property_is_set(op->ptr, "filepath"))
                        return screenshot_exec(C, op);
        if (screenshot_data_create(C, op)) {
                if (RNA_struct_property_is_set(op->ptr, "filepath"))
                        return screenshot_exec(C, op);
-               
-               RNA_string_set(op->ptr, "filepath", G.ima);
+
+               /* extension is added by 'screenshot_check' after */
+               RNA_string_set(op->ptr, "filepath", G.relbase_valid ? G.main->name : "//screen");
                
                WM_event_add_fileselect(C, op);
        
                
                WM_event_add_fileselect(C, op);
        
@@ -210,21 +216,52 @@ static int screenshot_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event)
        return OPERATOR_CANCELLED;
 }
 
        return OPERATOR_CANCELLED;
 }
 
+static int screenshot_check(bContext *UNUSED(C), wmOperator *op)
+{
+       ScreenshotData *scd = op->customdata;
+       return WM_operator_filesel_ensure_ext_imtype(op, scd->im_format.imtype);
+}
+
 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
 {
        screenshot_data_free(op);
        return OPERATOR_CANCELLED;
 }
 
 static int screenshot_cancel(bContext *UNUSED(C), wmOperator *op)
 {
        screenshot_data_free(op);
        return OPERATOR_CANCELLED;
 }
 
+static int screenshot_draw_check_prop(PointerRNA *UNUSED(ptr), PropertyRNA *prop)
+{
+       const char *prop_id = RNA_property_identifier(prop);
+
+       return !(strcmp(prop_id, "filepath") == 0);
+}
+
+static void screenshot_draw(bContext *UNUSED(C), wmOperator *op)
+{
+       uiLayout *layout = op->layout;
+       ScreenshotData *scd = op->customdata;
+       PointerRNA ptr;
+
+       /* image template */
+       RNA_pointer_create(NULL, &RNA_ImageFormatSettings, &scd->im_format, &ptr);
+       uiTemplateImageSettings(layout, &ptr);
+
+       /* main draw call */
+       RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
+       uiDefAutoButsRNA(layout, &ptr, screenshot_draw_check_prop, '\0');
+}
+
+
 void SCREEN_OT_screenshot(wmOperatorType *ot)
 {
        ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
        ot->idname = "SCREEN_OT_screenshot";
        
        ot->invoke = screenshot_invoke;
 void SCREEN_OT_screenshot(wmOperatorType *ot)
 {
        ot->name = "Save Screenshot"; /* weak: opname starting with 'save' makes filewindow give save-over */
        ot->idname = "SCREEN_OT_screenshot";
        
        ot->invoke = screenshot_invoke;
+       ot->check = screenshot_check;
        ot->exec = screenshot_exec;
        ot->exec = screenshot_exec;
-       ot->poll = WM_operator_winactive;
        ot->cancel = screenshot_cancel;
        ot->cancel = screenshot_cancel;
+       ot->ui = screenshot_draw;
+       ot->poll = WM_operator_winactive;
        
        ot->flag = 0;
        
        
        ot->flag = 0;
        
index 8b1ff4136b9625959103782090018744a0d9d0be..2c688990a8f61c1d44ee3ae41f139390ae691c78 100644 (file)
@@ -1001,11 +1001,7 @@ typedef struct {
 
 static void save_image_options_defaults(SaveImageOptions *simopts)
 {
 
 static void save_image_options_defaults(SaveImageOptions *simopts)
 {
-       memset(&simopts->im_format, 0, sizeof(simopts->im_format));
-       simopts->im_format.planes = R_IMF_PLANES_RGB;
-       simopts->im_format.imtype = R_IMF_IMTYPE_PNG;
-       simopts->im_format.quality = 90;
-       simopts->im_format.compress = 90;
+       BKE_imformat_defaults(&simopts->im_format);
        simopts->filepath[0] = '\0';
 }
 
        simopts->filepath[0] = '\0';
 }
 
@@ -1246,13 +1242,7 @@ static int image_save_as_exec(bContext *C, wmOperator *op)
 static int image_save_as_check(bContext *UNUSED(C), wmOperator *op)
 {
        ImageFormatData *imf = op->customdata;
 static int image_save_as_check(bContext *UNUSED(C), wmOperator *op)
 {
        ImageFormatData *imf = op->customdata;
-       char filepath[FILE_MAX];
-       RNA_string_get(op->ptr, "filepath", filepath);
-       if (BKE_add_image_extension(filepath, imf->imtype)) {
-               RNA_string_set(op->ptr, "filepath", filepath);
-               return TRUE;
-       }
-       return FALSE;
+       return WM_operator_filesel_ensure_ext_imtype(op, imf->imtype);
 }
 
 static int image_save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
 }
 
 static int image_save_as_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
index e21ca52ecb34fba1e8d519f23e37c8e918755be4..d2df799bc943539ea2099f516f1c07d4b35032fd 100644 (file)
@@ -249,7 +249,7 @@ typedef struct ImageFormatData {
        char depth;    /* bits per channel, R_IMF_CHAN_DEPTH_8 -> 32,
                        * not a flag, only set 1 at a time */
 
        char depth;    /* bits per channel, R_IMF_CHAN_DEPTH_8 -> 32,
                        * not a flag, only set 1 at a time */
 
-       char planes  ; /* - R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA */
+       char planes;   /* - R_IMF_PLANES_BW, R_IMF_PLANES_RGB, R_IMF_PLANES_RGBA */
        char flag;     /* generic options for all image types, alpha zbuffer */
 
        char quality;  /* (0 - 100), eg: jpeg quality */
        char flag;     /* generic options for all image types, alpha zbuffer */
 
        char quality;  /* (0 - 100), eg: jpeg quality */
index a794cf10a866ddd9a30fc8281684f0931ea26fcf..4c81a0a865469f38f1df7b11bda92cdf1754440b 100644 (file)
@@ -166,6 +166,7 @@ int                 WM_enum_search_invoke(struct bContext *C, struct wmOperator *op, struct wm
 int                    WM_operator_confirm             (struct bContext *C, struct wmOperator *op, struct wmEvent *event);
                /* invoke callback, file selector "filepath" unset + exec */
 int                    WM_operator_filesel             (struct bContext *C, struct wmOperator *op, struct wmEvent *event);
 int                    WM_operator_confirm             (struct bContext *C, struct wmOperator *op, struct wmEvent *event);
                /* invoke callback, file selector "filepath" unset + exec */
 int                    WM_operator_filesel             (struct bContext *C, struct wmOperator *op, struct wmEvent *event);
+int         WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const char imtype);
                        /* poll callback, context checks */
 int                    WM_operator_winactive   (struct bContext *C);
                        /* invoke callback, exec + redo popup */
                        /* poll callback, context checks */
 int                    WM_operator_winactive   (struct bContext *C);
                        /* invoke callback, exec + redo popup */
index c9306f6dcf78c068f1d71d8827beeb06f699b2d0..514c159c87b79871ccef08950b3372c4de3a84ea 100644 (file)
@@ -65,6 +65,7 @@
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_idprop.h"
 #include "BKE_context.h"
 #include "BKE_depsgraph.h"
 #include "BKE_idprop.h"
+#include "BKE_image.h"
 #include "BKE_library.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_library.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
@@ -808,6 +809,22 @@ int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
        }
 }
 
        }
 }
 
+int WM_operator_filesel_ensure_ext_imtype(wmOperator *op, const char imtype)
+{
+       PropertyRNA *prop;
+       char filepath[FILE_MAX];
+       /* dont NULL check prop, this can only run on ops with a 'filepath' */
+       prop = RNA_struct_find_property(op->ptr, "filepath");
+       RNA_property_string_get(op->ptr, prop, filepath);
+       if (BKE_add_image_extension(filepath, imtype)) {
+               RNA_property_string_set(op->ptr, prop, filepath);
+               /* note, we could check for and update 'filename' here,
+                * but so far nothing needs this. */
+               return TRUE;
+       }
+       return FALSE;
+}
+
 /* default properties for fileselect */
 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display)
 {
 /* default properties for fileselect */
 void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display)
 {