debug build option WITH_PYTHON_UI_INFO, so you can right click and edit the python...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 20 Oct 2011 00:48:00 +0000 (00:48 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 20 Oct 2011 00:48:00 +0000 (00:48 +0000)
CMakeLists.txt
source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_intern.h
source/blender/editors/space_text/space_text.c
source/blender/python/generic/py_capi_utils.c
source/blender/python/generic/py_capi_utils.h
source/blender/windowmanager/intern/wm_operators.c

index 6cfda3a..6e3247a 100644 (file)
@@ -213,6 +213,10 @@ mark_as_advanced(WITH_CXX_GUARDEDALLOC)
 option(WITH_ASSERT_ABORT "Call abort() when raising an assertion through BLI_assert()" OFF)
 mark_as_advanced(WITH_ASSERT_ABORT)
 
+option(WITH_PYTHON_UI_INFO "Allow navigating to UI source from the context menu" OFF)
+mark_as_advanced(WITH_PYTHON_UI_INFO)
+
+
 if(APPLE)
        if(NOT CMAKE_OSX_ARCHITECTURES)
                set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING
@@ -1326,6 +1330,10 @@ if(WITH_ASSERT_ABORT)
        add_definitions(-DWITH_ASSERT_ABORT)
 endif()
 
+if(WITH_PYTHON_UI_INFO)
+       add_definitions(-DWITH_PYTHON_UI_INFO)
+endif()
+
 # message(STATUS "Using CFLAGS: ${CMAKE_C_FLAGS}")
 # message(STATUS "Using CXXFLAGS: ${CMAKE_CXX_FLAGS}")
 
index b6c72c2..ac1fab3 100644 (file)
@@ -2559,6 +2559,24 @@ static uiBut *ui_def_but(uiBlock *block, int type, int retval, const char *str,
        if(block->curlayout)
                ui_layout_add_but(block->curlayout, but);
 
+#ifdef WITH_PYTHON_UI_INFO
+       {
+               extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
+
+               const char *fn;
+               int lineno= -1;
+               PyC_FileAndNum_Safe(&fn, &lineno);
+               if (lineno != -1) {
+                       BLI_strncpy(but->py_dbg_fn, fn, sizeof(but->py_dbg_fn));
+                       but->py_dbg_ln= lineno;
+               }
+               else {
+                       but->py_dbg_fn[0]= '\0';
+                       but->py_dbg_ln= -1;
+               }
+       }
+#endif /* WITH_PYTHON_UI_INFO */
+
        return but;
 }
 
index f4400d2..4df9d7f 100644 (file)
@@ -4438,6 +4438,17 @@ static int ui_but_menu(bContext *C, uiBut *but)
                }
        }
 
+#ifdef WITH_PYTHON_UI_INFO
+       if (but->py_dbg_ln != -1) {
+               PointerRNA ptr_props;
+
+               WM_operator_properties_create(&ptr_props, "WM_OT_text_edit");
+               RNA_string_set(&ptr_props, "filepath", but->py_dbg_fn);
+               RNA_int_set(&ptr_props, "line", but->py_dbg_ln);
+               uiItemFullO(layout, "WM_OT_text_edit", "Edit Source", ICON_NONE, ptr_props.data, WM_OP_EXEC_DEFAULT, 0);
+       }
+#endif /* WITH_PYTHON_UI_INFO */
+
        uiPupMenuEnd(C, pup);
 
        return 1;
index 12e9d39..c584339 100644 (file)
@@ -252,6 +252,11 @@ struct uiBut {
        
                /* pointer back */
        uiBlock *block;
+
+#ifdef WITH_PYTHON_UI_INFO
+       char py_dbg_fn[240];
+       int py_dbg_ln;
+#endif
 };
 
 struct uiBlock {
index 47f051e..51f4f1e 100644 (file)
@@ -136,7 +136,12 @@ static void text_listener(ScrArea *sa, wmNotifier *wmn)
 
                        switch(wmn->data) {
                                case ND_DISPLAY:
+                                       ED_area_tag_redraw(sa);
+                                       break;
                                case ND_CURSOR:
+                                       if(st->text && st->text == wmn->reference)
+                                               text_scroll_to_cursor(st, sa);
+
                                        ED_area_tag_redraw(sa);
                                        break;
                        }
index 1bccc8a..e658532 100644 (file)
@@ -184,6 +184,15 @@ void PyC_FileAndNum(const char **filename, int *lineno)
        }
 }
 
+void PyC_FileAndNum_Safe(const char **filename, int *lineno)
+{
+       if(!PYC_INTERPRETER_ACTIVE) {
+               return;
+       }
+
+       PyC_FileAndNum(filename, lineno);
+}
+
 /* Would be nice if python had this built in */
 PyObject *PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
 {
index 03a8637..f38ce20 100644 (file)
@@ -36,6 +36,7 @@ PyObject *            PyC_ExceptionBuffer(void);
 PyObject *             PyC_Object_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...);
 PyObject *             PyC_Err_Format_Prefix(PyObject *exception_type_prefix, const char *format, ...);
 void                   PyC_FileAndNum(const char **filename, int *lineno);
+void                   PyC_FileAndNum_Safe(const char **filename, int *lineno); /* checks python is running */
 int                            PyC_AsArray(void *array, PyObject *value, const int length, const PyTypeObject *type, const short is_double, const char *error_prefix);
 
 /* follow http://www.python.org/dev/peps/pep-0383/ */
index b0183d0..4924457 100644 (file)
 
 static GHash *global_ops_hash= NULL;
 
+#ifdef WITH_PYTHON_UI_INFO
+#  include "DNA_text_types.h"
+#  include "BKE_text.h"
+#endif
+
 /* ************ operator API, exported ********** */
 
 
@@ -3505,6 +3510,79 @@ static void operatortype_ghash_free_cb(wmOperatorType *ot)
        MEM_freeN(ot);
 }
 
+#ifdef WITH_PYTHON_UI_INFO
+
+static ScrArea *biggest_text_view(bContext *C)
+{
+       bScreen *sc= CTX_wm_screen(C);
+       ScrArea *sa, *big= NULL;
+       int size, maxsize= 0;
+
+       for(sa= sc->areabase.first; sa; sa= sa->next) {
+               if(sa->spacetype==SPACE_TEXT) {
+                       size= sa->winx * sa->winy;
+                       if(size > maxsize) {
+                               maxsize= size;
+                               big= sa;
+                       }
+               }
+       }
+       return big;
+}
+
+static int wm_text_edit_exec(bContext *C, wmOperator *op)
+{
+       Main *bmain= CTX_data_main(C);
+       Text *text;
+
+       char filepath[240];
+       int line= RNA_int_get(op->ptr, "line");
+       RNA_string_get(op->ptr, "filepath", filepath);
+
+       for (text=bmain->text.first; text; text=text->id.next) {
+               if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
+                       break;
+               }
+       }
+
+       if (text == NULL) {
+               text= add_text(filepath, bmain->name);
+       }
+
+       if (text == NULL) {
+               BKE_reportf(op->reports, RPT_WARNING, "file: '%s' can't be opened", filepath);
+               return OPERATOR_CANCELLED;
+       }
+       else {
+               /* naughty!, find text area to set, not good behavior
+                * but since this is a dev tool lets allow it - campbell */
+               ScrArea *sa= biggest_text_view(C);
+               if(sa) {
+                       SpaceText *st= sa->spacedata.first;
+                       st->text= text;
+               }
+
+               txt_move_toline(text, line - 1, FALSE);
+               WM_event_add_notifier(C, NC_TEXT|ND_CURSOR, text);
+       }
+
+       return OPERATOR_FINISHED;
+}
+
+static void WM_OT_text_edit(wmOperatorType *ot)
+{
+       ot->name= "Edit Text File";
+       ot->idname= "WM_OT_text_edit";
+
+       ot->exec= wm_text_edit_exec;
+
+       RNA_def_string_file_path(ot->srna, "filepath", "", FILE_MAX, "Path", "");
+       RNA_def_int(ot->srna, "line", 0, INT_MIN, INT_MAX, "Line", "", 0, INT_MAX);
+}
+
+#endif /* WITH_PYTHON_UI_INFO */
+
+
 /* ******************************************************* */
 /* called on initialize WM_exit() */
 void wm_operatortype_free(void)
@@ -3548,6 +3626,10 @@ void wm_operatortype_init(void)
        WM_operatortype_append(WM_OT_collada_import);
 #endif
 
+#ifdef WITH_PYTHON_UI_INFO
+       WM_operatortype_append(WM_OT_text_edit);
+#endif
+
 }
 
 /* circleselect-like modal operators */