[Bf-codereview] Popup menu layout inherits context store from button (issue 5975065)

lukas.toenne at googlemail.com lukas.toenne at googlemail.com
Tue Apr 3 09:05:12 CEST 2012


Reviewers: bf-codereview_blender.org, brechtvl,

Description:
This is my attempt to solve a problem i had with operator menus. I am
using uiLayoutSetContextPointer to add the bNode pointer to the draw
context in nodes, so operator buttons can work on a specific node
(similarly used in modifiers and logic bricks).

Problem is that, while it works nicely for standard buttons, it fails to
pass the context pointer to popup menu layouts. This patch copies the
bContextStore from the original uiBut to the new popup layout.

@Brecht: Campbell told me you implemented this feature in the first
place, so would be nice if you could review.

Please review this at http://codereview.appspot.com/5975065/

Affected files:
   source/blender/blenkernel/BKE_context.h
   source/blender/blenkernel/intern/context.c
   source/blender/editors/include/UI_interface.h
   source/blender/editors/interface/interface_layout.c
   source/blender/editors/interface/interface_regions.c


Index: source/blender/blenkernel/intern/context.c
===================================================================
--- source/blender/blenkernel/intern/context.c	(revision 45360)
+++ source/blender/blenkernel/intern/context.c	(working copy)
@@ -141,6 +141,35 @@
  	return ctx;
  }

+bContextStore *CTX_store_add_all(ListBase *contexts, bContextStore  
*context)
+{
+	bContextStoreEntry *entry, *tentry;
+	bContextStore *ctx, *lastctx;
+
+	/* ensure we have a context to put the entries in, if it was already used
+	 * we have to copy the context to ensure */
+	ctx= contexts->last;
+
+	if (!ctx || ctx->used) {
+		if (ctx) {
+			lastctx= ctx;
+			ctx= MEM_dupallocN(lastctx);
+			BLI_duplicatelist(&ctx->entries, &lastctx->entries);
+		}
+		else
+			ctx= MEM_callocN(sizeof(bContextStore), "bContextStore");
+
+		BLI_addtail(contexts, ctx);
+	}
+
+	for (tentry= context->entries.first; tentry; tentry= tentry->next) {
+		entry= MEM_dupallocN(tentry);
+		BLI_addtail(&ctx->entries, entry);
+	}
+
+	return ctx;
+}
+
  void CTX_store_set(bContext *C, bContextStore *store)
  {
  	C->wm.store= store;
Index: source/blender/blenkernel/BKE_context.h
===================================================================
--- source/blender/blenkernel/BKE_context.h	(revision 45360)
+++ source/blender/blenkernel/BKE_context.h	(working copy)
@@ -117,6 +117,7 @@
  /* Stored Context */

  bContextStore *CTX_store_add(ListBase *contexts, const char *name,  
PointerRNA *ptr);
+bContextStore *CTX_store_add_all(ListBase *contexts, bContextStore  
*context);
  void CTX_store_set(bContext *C, bContextStore *store);
  bContextStore *CTX_store_copy(bContextStore *store);
  void CTX_store_free(bContextStore *store);
Index: source/blender/editors/include/UI_interface.h
===================================================================
--- source/blender/editors/include/UI_interface.h	(revision 45360)
+++ source/blender/editors/include/UI_interface.h	(working copy)
@@ -48,6 +48,7 @@
  struct wmOperator;
  struct AutoComplete;
  struct bContext;
+struct bContextStore;
  struct Panel;
  struct PanelType;
  struct PointerRNA;
@@ -692,6 +693,7 @@

  void uiLayoutSetFunc(uiLayout *layout, uiMenuHandleFunc handlefunc, void  
*argv);
  void uiLayoutSetContextPointer(uiLayout *layout, const char *name, struct  
PointerRNA *ptr);
+void uiLayoutContextCopy(uiLayout *layout, struct bContextStore *context);
  const char *uiLayoutIntrospect(uiLayout *layout); // XXX - testing
  void uiLayoutOperatorButs(const struct bContext *C, struct uiLayout  
*layout, struct wmOperator *op, int (*check_prop)(struct PointerRNA *,  
struct PropertyRNA *), const char label_align, const short flag);
  struct MenuType *uiButGetMenuType(uiBut *but);
Index: source/blender/editors/interface/interface_regions.c
===================================================================
--- source/blender/editors/interface/interface_regions.c	(revision 45360)
+++ source/blender/editors/interface/interface_regions.c	(working copy)
@@ -2404,16 +2404,18 @@
  	/* some enums reversing is strange, currently we have no good way to
  	 * reverse some enum's but not others, so reverse all so the first menu
  	 * items are always close to the mouse cursor */
+	else {
  #if 0
-	else {
  		/* if this is an rna button then we can assume its an enum
  		 * flipping enums is generally not good since the order can be
  		 * important [#28786] */
  		if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
  			pup->block->flag |= UI_BLOCK_NO_FLIP;
  		}
+#endif
+		if (but->context)
+			uiLayoutContextCopy(pup->layout, but->context);
  	}
-#endif

  	if (str) {
  		/* menu is created from a string */
Index: source/blender/editors/interface/interface_layout.c
===================================================================
--- source/blender/editors/interface/interface_layout.c	(revision 45360)
+++ source/blender/editors/interface/interface_layout.c	(working copy)
@@ -2722,7 +2722,13 @@
  	layout->context = CTX_store_add(&block->contexts, name, ptr);
  }

+void uiLayoutContextCopy(uiLayout *layout, bContextStore *context)
+{
+	uiBlock *block= layout->root->block;
+	layout->context= CTX_store_add_all(&block->contexts, context);
+}

+
  /* introspect funcs */
  #include "BLI_dynstr.h"





More information about the Bf-codereview mailing list