Cleanup: add wm_utils.c for generic functions

This commit is contained in:
Campbell Barton 2019-05-20 16:52:49 +10:00
parent 3184460ff7
commit f606f58845
7 changed files with 89 additions and 48 deletions

@ -74,6 +74,7 @@ set(SRC
intern/wm_toolsystem.c
intern/wm_tooltip.c
intern/wm_uilist_type.c
intern/wm_utils.c
intern/wm_window.c
gizmo/intern/wm_gizmo.c
gizmo/intern/wm_gizmo_group.c

@ -796,7 +796,10 @@ void WM_tooltip_init(struct bContext *C, struct wmWindow *win);
void WM_tooltip_refresh(struct bContext *C, struct wmWindow *win);
double WM_tooltip_time_closed(void);
/* wmGenericUserData */
/* wm_utils.c */
struct wmGenericCallback *WM_generic_callback_steal(struct wmGenericCallback *callback);
void WM_generic_callback_free(struct wmGenericCallback *callback);
void WM_generic_user_data_free(struct wmGenericUserData *user_data);
#ifdef __cplusplus

@ -127,6 +127,12 @@ typedef struct wmGenericUserData {
bool use_free;
} wmGenericUserData;
typedef struct wmGenericCallback {
void (*exec)(struct bContext *C, void *user_data);
void *user_data;
void (*free_user_data)(void *user_data);
} wmGenericCallback;
/* ************** wmOperatorType ************************ */
/* flag */

@ -2855,7 +2855,7 @@ static void wm_block_file_close_cancel(bContext *C, void *arg_block, void *UNUSE
static void wm_block_file_close_discard(bContext *C, void *arg_block, void *arg_data)
{
wmGenericCallback *callback = wm_generic_callback_steal((wmGenericCallback *)arg_data);
wmGenericCallback *callback = WM_generic_callback_steal((wmGenericCallback *)arg_data);
/* Close the popup before executing the callback. Otherwise
* the popup might be closed by the callback, which will lead
@ -2864,12 +2864,12 @@ static void wm_block_file_close_discard(bContext *C, void *arg_block, void *arg_
UI_popup_block_close(C, win, arg_block);
callback->exec(C, callback->user_data);
wm_generic_callback_free(callback);
WM_generic_callback_free(callback);
}
static void wm_block_file_close_save(bContext *C, void *arg_block, void *arg_data)
{
wmGenericCallback *callback = wm_generic_callback_steal((wmGenericCallback *)arg_data);
wmGenericCallback *callback = WM_generic_callback_steal((wmGenericCallback *)arg_data);
bool execute_callback = true;
wmWindow *win = CTX_wm_window(C);
@ -2897,7 +2897,7 @@ static void wm_block_file_close_save(bContext *C, void *arg_block, void *arg_dat
if (execute_callback) {
callback->exec(C, callback->user_data);
}
wm_generic_callback_free(callback);
WM_generic_callback_free(callback);
}
static void wm_block_file_close_cancel_button(uiBlock *block, wmGenericCallback *post_action)
@ -3039,7 +3039,7 @@ static uiBlock *block_create__close_file_dialog(struct bContext *C, struct ARegi
static void free_post_file_close_action(void *arg)
{
wmGenericCallback *action = (wmGenericCallback *)arg;
wm_generic_callback_free(action);
WM_generic_callback_free(action);
}
void wm_close_file_dialog(bContext *C, wmGenericCallback *post_action)
@ -3054,25 +3054,4 @@ bool wm_file_or_image_is_modified(const bContext *C)
return !wm->file_saved || ED_image_should_save_modified(C);
}
void wm_generic_callback_free(wmGenericCallback *callback)
{
if (callback->free_user_data) {
callback->free_user_data(callback->user_data);
}
MEM_freeN(callback);
}
static void do_nothing(bContext *UNUSED(C), void *UNUSED(user_data))
{
}
wmGenericCallback *wm_generic_callback_steal(wmGenericCallback *callback)
{
wmGenericCallback *new_callback = MEM_dupallocN(callback);
callback->exec = do_nothing;
callback->free_user_data = NULL;
callback->user_data = NULL;
return new_callback;
}
/** \} */

@ -0,0 +1,71 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup wm
*
* Generic helper utilies that aren't assosiated with a particular area.
*/
#include "WM_types.h"
#include "WM_api.h"
#include "MEM_guardedalloc.h"
/* -------------------------------------------------------------------- */
/** \name Generic Callback
* \{ */
void WM_generic_callback_free(wmGenericCallback *callback)
{
if (callback->free_user_data) {
callback->free_user_data(callback->user_data);
}
MEM_freeN(callback);
}
static void do_nothing(struct bContext *UNUSED(C), void *UNUSED(user_data))
{
}
wmGenericCallback *WM_generic_callback_steal(wmGenericCallback *callback)
{
wmGenericCallback *new_callback = MEM_dupallocN(callback);
callback->exec = do_nothing;
callback->free_user_data = NULL;
callback->user_data = NULL;
return new_callback;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Generic User Data
* \{ */
void WM_generic_user_data_free(wmGenericUserData *wm_userdata)
{
if (wm_userdata->data && wm_userdata->use_free) {
if (wm_userdata->free_fn) {
wm_userdata->free_fn(wm_userdata->data);
}
else {
MEM_freeN(wm_userdata->data);
}
}
}
/** \} */

@ -2290,15 +2290,3 @@ void WM_opengl_context_release(void *context)
BLI_assert(GPU_framebuffer_active_get() == NULL);
GHOST_ReleaseOpenGLContext((GHOST_ContextHandle)context);
}
void WM_generic_user_data_free(wmGenericUserData *wm_userdata)
{
if (wm_userdata->data && wm_userdata->use_free) {
if (wm_userdata->free_fn) {
wm_userdata->free_fn(wm_userdata->data);
}
else {
MEM_freeN(wm_userdata->data);
}
}
}

@ -25,6 +25,7 @@
#define __WM_FILES_H__
struct Main;
struct wmGenericCallback;
struct wmOperatorType;
/* wm_files.c */
@ -40,15 +41,7 @@ void wm_homefile_read(struct bContext *C,
bool *r_is_factory_startup);
void wm_file_read_report(bContext *C, struct Main *bmain);
typedef struct wmGenericCallback {
void (*exec)(bContext *C, void *user_data);
void *user_data;
void (*free_user_data)(void *user_data);
} wmGenericCallback;
wmGenericCallback *wm_generic_callback_steal(wmGenericCallback *callback);
void wm_generic_callback_free(wmGenericCallback *callback);
void wm_close_file_dialog(bContext *C, wmGenericCallback *post_action);
void wm_close_file_dialog(bContext *C, struct wmGenericCallback *post_action);
bool wm_file_or_image_is_modified(const struct bContext *C);
void WM_OT_save_homefile(struct wmOperatorType *ot);