2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-06-24 16:07:34 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2018-06-24 16:07:34 +02:00
|
|
|
*
|
|
|
|
|
* User defined menu API.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cstring>
|
2018-06-24 16:07:34 +02:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_listbase.h"
|
|
|
|
|
#include "BLI_string.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_userdef_types.h"
|
|
|
|
|
|
2024-01-21 19:26:31 +01:00
|
|
|
#include "BKE_blender_user_menu.hh"
|
2024-03-26 12:57:30 -04:00
|
|
|
#include "BKE_idprop.hh"
|
2018-06-24 16:07:34 +02:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Menu Type
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
bUserMenu *BKE_blender_user_menu_find(ListBase *lb, char space_type, const char *context)
|
|
|
|
|
{
|
2020-04-03 19:15:01 +02:00
|
|
|
LISTBASE_FOREACH (bUserMenu *, um, lb) {
|
2022-10-07 22:52:53 +11:00
|
|
|
if ((space_type == um->space_type) && STREQ(context, um->context)) {
|
2018-06-24 16:07:34 +02:00
|
|
|
return um;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2018-06-24 16:07:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bUserMenu *BKE_blender_user_menu_ensure(ListBase *lb, char space_type, const char *context)
|
|
|
|
|
{
|
|
|
|
|
bUserMenu *um = BKE_blender_user_menu_find(lb, space_type, context);
|
2023-07-17 10:46:26 +02:00
|
|
|
if (um == nullptr) {
|
|
|
|
|
um = static_cast<bUserMenu *>(MEM_callocN(sizeof(bUserMenu), __func__));
|
2018-06-24 16:07:34 +02:00
|
|
|
um->space_type = space_type;
|
|
|
|
|
STRNCPY(um->context, context);
|
|
|
|
|
BLI_addhead(lb, um);
|
|
|
|
|
}
|
|
|
|
|
return um;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Menu Item
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
bUserMenuItem *BKE_blender_user_menu_item_add(ListBase *lb, int type)
|
|
|
|
|
{
|
|
|
|
|
uint size;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-30 20:59:10 +02:00
|
|
|
if (type == USER_MENU_TYPE_SEP) {
|
|
|
|
|
size = sizeof(bUserMenuItem);
|
|
|
|
|
}
|
|
|
|
|
else if (type == USER_MENU_TYPE_OPERATOR) {
|
2018-06-24 16:07:34 +02:00
|
|
|
size = sizeof(bUserMenuItem_Op);
|
|
|
|
|
}
|
2018-06-30 12:08:08 +02:00
|
|
|
else if (type == USER_MENU_TYPE_MENU) {
|
|
|
|
|
size = sizeof(bUserMenuItem_Menu);
|
|
|
|
|
}
|
2018-06-30 20:59:10 +02:00
|
|
|
else if (type == USER_MENU_TYPE_PROP) {
|
|
|
|
|
size = sizeof(bUserMenuItem_Prop);
|
2018-06-24 16:07:34 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2018-07-31 20:44:49 +10:00
|
|
|
size = sizeof(bUserMenuItem);
|
2018-06-24 16:07:34 +02:00
|
|
|
BLI_assert(0);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
bUserMenuItem *umi = static_cast<bUserMenuItem *>(MEM_callocN(size, __func__));
|
2018-06-24 16:07:34 +02:00
|
|
|
umi->type = type;
|
|
|
|
|
BLI_addtail(lb, umi);
|
|
|
|
|
return umi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_blender_user_menu_item_free(bUserMenuItem *umi)
|
|
|
|
|
{
|
|
|
|
|
if (umi->type == USER_MENU_TYPE_OPERATOR) {
|
|
|
|
|
bUserMenuItem_Op *umi_op = (bUserMenuItem_Op *)umi;
|
|
|
|
|
if (umi_op->prop) {
|
|
|
|
|
IDP_FreeProperty(umi_op->prop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MEM_freeN(umi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_blender_user_menu_item_free_list(ListBase *lb)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
for (bUserMenuItem *umi = static_cast<bUserMenuItem *>(lb->first), *umi_next; umi;
|
2024-01-02 18:12:54 +01:00
|
|
|
umi = umi_next)
|
|
|
|
|
{
|
2018-06-24 16:07:34 +02:00
|
|
|
umi_next = umi->next;
|
|
|
|
|
BKE_blender_user_menu_item_free(umi);
|
|
|
|
|
}
|
|
|
|
|
BLI_listbase_clear(lb);
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
|
|
|
|
/** \} */
|