Blender V2.61 - r43446

context.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Blender Foundation (2008).
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <string.h>
00032 #include <stddef.h>
00033 
00034 #include "MEM_guardedalloc.h"
00035 
00036 #include "DNA_scene_types.h"
00037 #include "DNA_screen_types.h"
00038 #include "DNA_space_types.h"
00039 #include "DNA_view3d_types.h"
00040 #include "DNA_windowmanager_types.h"
00041 #include "DNA_object_types.h"
00042 
00043 #include "RNA_access.h"
00044 
00045 #include "BLI_listbase.h"
00046 #include "BLI_string.h"
00047 
00048 #include "BKE_context.h"
00049 #include "BKE_main.h"
00050 #include "BKE_screen.h"
00051 
00052 #ifdef WITH_PYTHON
00053 #include "BPY_extern.h"
00054 #endif
00055 
00056 /* struct */
00057 
00058 struct bContext {
00059     int thread;
00060 
00061     /* windowmanager context */
00062     struct {
00063         struct wmWindowManager *manager;
00064         struct wmWindow *window;
00065         struct bScreen *screen;
00066         struct ScrArea *area;
00067         struct ARegion *region;
00068         struct ARegion *menu;
00069         struct bContextStore *store;
00070         const char *operator_poll_msg; /* reason for poll failing */
00071     } wm;
00072     
00073     /* data context */
00074     struct {
00075         struct Main *main;
00076         struct Scene *scene;
00077 
00078         int recursion;
00079         int py_init; /* true if python is initialized */
00080         void *py_context;
00081     } data;
00082     
00083     /* data evaluation */
00084     struct {
00085         int render;
00086     } eval;
00087 };
00088 
00089 /* context */
00090 
00091 bContext *CTX_create(void)
00092 {
00093     bContext *C;
00094     
00095     C= MEM_callocN(sizeof(bContext), "bContext");
00096 
00097     return C;
00098 }
00099 
00100 bContext *CTX_copy(const bContext *C)
00101 {
00102     bContext *newC= MEM_dupallocN((void*)C);
00103 
00104     return newC;
00105 }
00106 
00107 void CTX_free(bContext *C)
00108 {
00109     MEM_freeN(C);
00110 }
00111 
00112 /* store */
00113 
00114 bContextStore *CTX_store_add(ListBase *contexts, const char *name, PointerRNA *ptr)
00115 {
00116     bContextStoreEntry *entry;
00117     bContextStore *ctx, *lastctx;
00118 
00119     /* ensure we have a context to put the entry in, if it was already used
00120      * we have to copy the context to ensure */
00121     ctx= contexts->last;
00122 
00123     if(!ctx || ctx->used) {
00124         if(ctx) {
00125             lastctx= ctx;
00126             ctx= MEM_dupallocN(lastctx);
00127             BLI_duplicatelist(&ctx->entries, &lastctx->entries);
00128         }
00129         else
00130             ctx= MEM_callocN(sizeof(bContextStore), "bContextStore");
00131 
00132         BLI_addtail(contexts, ctx);
00133     }
00134 
00135     entry= MEM_callocN(sizeof(bContextStoreEntry), "bContextStoreEntry");
00136     BLI_strncpy(entry->name, name, sizeof(entry->name));
00137     entry->ptr= *ptr;
00138 
00139     BLI_addtail(&ctx->entries, entry);
00140 
00141     return ctx;
00142 }
00143 
00144 void CTX_store_set(bContext *C, bContextStore *store)
00145 {
00146     C->wm.store= store;
00147 }
00148 
00149 bContextStore *CTX_store_copy(bContextStore *store)
00150 {
00151     bContextStore *ctx;
00152 
00153     ctx= MEM_dupallocN(store);
00154     BLI_duplicatelist(&ctx->entries, &store->entries);
00155 
00156     return ctx;
00157 }
00158 
00159 void CTX_store_free(bContextStore *store)
00160 {
00161     BLI_freelistN(&store->entries);
00162     MEM_freeN(store);
00163 }
00164 
00165 void CTX_store_free_list(ListBase *contexts)
00166 {
00167     bContextStore *ctx;
00168 
00169     while((ctx= contexts->first)) {
00170         BLI_remlink(contexts, ctx);
00171         CTX_store_free(ctx);
00172     }
00173 }
00174 
00175 /* is python initialied? */
00176 int CTX_py_init_get(bContext *C)
00177 {
00178     return C->data.py_init;
00179 }
00180 void CTX_py_init_set(bContext *C, int value)
00181 {
00182     C->data.py_init= value;
00183 }
00184 
00185 void *CTX_py_dict_get(const bContext *C)
00186 {
00187     return C->data.py_context;
00188 }
00189 void CTX_py_dict_set(bContext *C, void *value)
00190 {
00191     C->data.py_context= value;
00192 }
00193 
00194 /* window manager context */
00195 
00196 wmWindowManager *CTX_wm_manager(const bContext *C)
00197 {
00198     return C->wm.manager;
00199 }
00200 
00201 wmWindow *CTX_wm_window(const bContext *C)
00202 {
00203     return C->wm.window;
00204 }
00205 
00206 bScreen *CTX_wm_screen(const bContext *C)
00207 {
00208     return C->wm.screen;
00209 }
00210 
00211 ScrArea *CTX_wm_area(const bContext *C)
00212 {
00213     return C->wm.area;
00214 }
00215 
00216 SpaceLink *CTX_wm_space_data(const bContext *C)
00217 {
00218     return (C->wm.area)? C->wm.area->spacedata.first: NULL;
00219 }
00220 
00221 ARegion *CTX_wm_region(const bContext *C)
00222 {
00223     return C->wm.region;
00224 }
00225 
00226 void *CTX_wm_region_data(const bContext *C)
00227 {
00228     return (C->wm.region)? C->wm.region->regiondata: NULL;
00229 }
00230 
00231 struct ARegion *CTX_wm_menu(const bContext *C)
00232 {
00233     return C->wm.menu;
00234 }
00235 
00236 struct ReportList *CTX_wm_reports(const bContext *C)
00237 {
00238     if (C->wm.manager)
00239         return &(C->wm.manager->reports);
00240 
00241     return NULL;
00242 }
00243 
00244 View3D *CTX_wm_view3d(const bContext *C)
00245 {
00246     if(C->wm.area && C->wm.area->spacetype==SPACE_VIEW3D)
00247         return C->wm.area->spacedata.first;
00248     return NULL;
00249 }
00250 
00251 RegionView3D *CTX_wm_region_view3d(const bContext *C)
00252 {
00253     if(C->wm.area && C->wm.area->spacetype==SPACE_VIEW3D)
00254         if(C->wm.region)
00255             return C->wm.region->regiondata;
00256     return NULL;
00257 }
00258 
00259 struct SpaceText *CTX_wm_space_text(const bContext *C)
00260 {
00261     if(C->wm.area && C->wm.area->spacetype==SPACE_TEXT)
00262         return C->wm.area->spacedata.first;
00263     return NULL;
00264 }
00265 
00266 struct SpaceConsole *CTX_wm_space_console(const bContext *C)
00267 {
00268     if(C->wm.area && C->wm.area->spacetype==SPACE_CONSOLE)
00269         return C->wm.area->spacedata.first;
00270     return NULL;
00271 }
00272 
00273 struct SpaceImage *CTX_wm_space_image(const bContext *C)
00274 {
00275     if(C->wm.area && C->wm.area->spacetype==SPACE_IMAGE)
00276         return C->wm.area->spacedata.first;
00277     return NULL;
00278 }
00279 
00280 struct SpaceButs *CTX_wm_space_buts(const bContext *C)
00281 {
00282     if(C->wm.area && C->wm.area->spacetype==SPACE_BUTS)
00283         return C->wm.area->spacedata.first;
00284     return NULL;
00285 }
00286 
00287 struct SpaceFile *CTX_wm_space_file(const bContext *C)
00288 {
00289     if(C->wm.area && C->wm.area->spacetype==SPACE_FILE)
00290         return C->wm.area->spacedata.first;
00291     return NULL;
00292 }
00293 
00294 struct SpaceSeq *CTX_wm_space_seq(const bContext *C)
00295 {
00296     if(C->wm.area && C->wm.area->spacetype==SPACE_SEQ)
00297         return C->wm.area->spacedata.first;
00298     return NULL;
00299 }
00300 
00301 struct SpaceOops *CTX_wm_space_outliner(const bContext *C)
00302 {
00303     if(C->wm.area && C->wm.area->spacetype==SPACE_OUTLINER)
00304         return C->wm.area->spacedata.first;
00305     return NULL;
00306 }
00307 
00308 struct SpaceNla *CTX_wm_space_nla(const bContext *C)
00309 {
00310     if(C->wm.area && C->wm.area->spacetype==SPACE_NLA)
00311         return C->wm.area->spacedata.first;
00312     return NULL;
00313 }
00314 
00315 struct SpaceTime *CTX_wm_space_time(const bContext *C)
00316 {
00317     if(C->wm.area && C->wm.area->spacetype==SPACE_TIME)
00318         return C->wm.area->spacedata.first;
00319     return NULL;
00320 }
00321 
00322 struct SpaceNode *CTX_wm_space_node(const bContext *C)
00323 {
00324     if(C->wm.area && C->wm.area->spacetype==SPACE_NODE)
00325         return C->wm.area->spacedata.first;
00326     return NULL;
00327 }
00328 
00329 struct SpaceLogic *CTX_wm_space_logic(const bContext *C)
00330 {
00331     if(C->wm.area && C->wm.area->spacetype==SPACE_LOGIC)
00332         return C->wm.area->spacedata.first;
00333     return NULL;
00334 }
00335 
00336 struct SpaceIpo *CTX_wm_space_graph(const bContext *C)
00337 {
00338     if(C->wm.area && C->wm.area->spacetype==SPACE_IPO)
00339         return C->wm.area->spacedata.first;
00340     return NULL;
00341 }
00342 
00343 struct SpaceAction *CTX_wm_space_action(const bContext *C)
00344 {
00345     if(C->wm.area && C->wm.area->spacetype==SPACE_ACTION)
00346         return C->wm.area->spacedata.first;
00347     return NULL;
00348 }
00349 
00350 struct SpaceInfo *CTX_wm_space_info(const bContext *C)
00351 {
00352     if(C->wm.area && C->wm.area->spacetype==SPACE_INFO)
00353         return C->wm.area->spacedata.first;
00354     return NULL;
00355 }
00356 
00357 struct SpaceUserPref *CTX_wm_space_userpref(const bContext *C)
00358 {
00359     if(C->wm.area && C->wm.area->spacetype==SPACE_USERPREF)
00360         return C->wm.area->spacedata.first;
00361     return NULL;
00362 }
00363 
00364 struct SpaceClip *CTX_wm_space_clip(const bContext *C)
00365 {
00366     if(C->wm.area && C->wm.area->spacetype==SPACE_CLIP)
00367         return C->wm.area->spacedata.first;
00368     return NULL;
00369 }
00370 
00371 void CTX_wm_manager_set(bContext *C, wmWindowManager *wm)
00372 {
00373     C->wm.manager= wm;
00374     C->wm.window= NULL;
00375     C->wm.screen= NULL;
00376     C->wm.area= NULL;
00377     C->wm.region= NULL;
00378 }
00379 
00380 void CTX_wm_window_set(bContext *C, wmWindow *win)
00381 {
00382     C->wm.window= win;
00383     C->wm.screen= (win)? win->screen: NULL;
00384     C->data.scene= (C->wm.screen)? C->wm.screen->scene: NULL;
00385     C->wm.area= NULL;
00386     C->wm.region= NULL;
00387 }
00388 
00389 void CTX_wm_screen_set(bContext *C, bScreen *screen)
00390 {
00391     C->wm.screen= screen;
00392     C->data.scene= (C->wm.screen)? C->wm.screen->scene: NULL;
00393     C->wm.area= NULL;
00394     C->wm.region= NULL;
00395 }
00396 
00397 void CTX_wm_area_set(bContext *C, ScrArea *area)
00398 {
00399     C->wm.area= area;
00400     C->wm.region= NULL;
00401 }
00402 
00403 void CTX_wm_region_set(bContext *C, ARegion *region)
00404 {
00405     C->wm.region= region;
00406 }
00407 
00408 void CTX_wm_menu_set(bContext *C, ARegion *menu)
00409 {
00410     C->wm.menu= menu;
00411 }
00412 
00413 void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
00414 {
00415     C->wm.operator_poll_msg= msg;
00416 }
00417 
00418 const char *CTX_wm_operator_poll_msg_get(bContext *C)
00419 {
00420     return C->wm.operator_poll_msg;
00421 }
00422 
00423 /* data context utility functions */
00424 
00425 struct bContextDataResult {
00426     PointerRNA ptr;
00427     ListBase list;
00428     const char **dir;
00429     short type; /* 0: normal, 1: seq */
00430 };
00431 
00432 static int ctx_data_get(bContext *C, const char *member, bContextDataResult *result)
00433 {
00434     int done= 0, recursion= C->data.recursion;
00435     int ret= 0;
00436 
00437     memset(result, 0, sizeof(bContextDataResult));
00438 #ifdef WITH_PYTHON
00439     if(CTX_py_dict_get(C)) {
00440         return BPY_context_member_get(C, member, result);
00441 //      if (BPY_context_member_get(C, member, result))
00442 //          return 1;
00443     }
00444 #endif
00445     /* we check recursion to ensure that we do not get infinite
00446      * loops requesting data from ourselfs in a context callback */
00447 
00448     /* Ok, this looks evil...
00449      * if(ret) done= -(-ret | -done);
00450      *
00451      * Values in order of importance
00452      * (0, -1, 1) - Where 1 is highest priority
00453      * */
00454     if(done!=1 && recursion < 1 && C->wm.store) {
00455         bContextStoreEntry *entry;
00456 
00457         C->data.recursion= 1;
00458 
00459         entry= BLI_rfindstring(&C->wm.store->entries, member, offsetof(bContextStoreEntry, name));
00460         if(entry) {
00461             result->ptr= entry->ptr;
00462             done= 1;
00463         }
00464     }
00465     if(done!=1 && recursion < 2 && C->wm.region) {
00466         C->data.recursion= 2;
00467         if(C->wm.region->type && C->wm.region->type->context) {
00468             ret = C->wm.region->type->context(C, member, result);
00469             if(ret) done= -(-ret | -done);
00470 
00471         }
00472     }
00473     if(done!=1 && recursion < 3 && C->wm.area) {
00474         C->data.recursion= 3;
00475         if(C->wm.area->type && C->wm.area->type->context) {
00476             ret = C->wm.area->type->context(C, member, result);
00477             if(ret) done= -(-ret | -done);
00478         }
00479     }
00480     if(done!=1 && recursion < 4 && C->wm.screen) {
00481         bContextDataCallback cb= C->wm.screen->context;
00482         C->data.recursion= 4;
00483         if(cb) {
00484             ret = cb(C, member, result);
00485             if(ret) done= -(-ret | -done);
00486         }
00487     }
00488 
00489     C->data.recursion= recursion;
00490 
00491     return done;
00492 }
00493 
00494 static void *ctx_data_pointer_get(const bContext *C, const char *member)
00495 {
00496     bContextDataResult result;
00497 
00498     if(C && ctx_data_get((bContext*)C, member, &result)==1)
00499         return result.ptr.data;
00500 
00501     return NULL;
00502 }
00503 
00504 static int ctx_data_pointer_verify(const bContext *C, const char *member, void **pointer)
00505 {
00506     bContextDataResult result;
00507 
00508     /* if context is NULL, pointer must be NULL too and that is a valid return */
00509     if (C == NULL) {
00510         *pointer= NULL;
00511         return 1;
00512     }
00513     else if(ctx_data_get((bContext*)C, member, &result)==1) {
00514         *pointer= result.ptr.data;
00515         return 1;
00516     }
00517     else {
00518         *pointer= NULL;
00519         return 0;
00520     }
00521 }
00522 
00523 static int ctx_data_collection_get(const bContext *C, const char *member, ListBase *list)
00524 {
00525     bContextDataResult result;
00526 
00527     if(ctx_data_get((bContext*)C, member, &result)==1) {
00528         *list= result.list;
00529         return 1;
00530     }
00531 
00532     list->first= NULL;
00533     list->last= NULL;
00534 
00535     return 0;
00536 }
00537 
00538 PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
00539 {
00540     bContextDataResult result;
00541 
00542     if(ctx_data_get((bContext*)C, member, &result)==1)
00543         return result.ptr;
00544     else
00545         return PointerRNA_NULL;
00546 }
00547 
00548 PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
00549 {
00550     PointerRNA ptr = CTX_data_pointer_get(C, member);
00551 
00552     if(ptr.data && RNA_struct_is_a(ptr.type, type))
00553         return ptr;
00554     
00555     return PointerRNA_NULL;
00556 }
00557 
00558 ListBase CTX_data_collection_get(const bContext *C, const char *member)
00559 {
00560     bContextDataResult result;
00561 
00562     if(ctx_data_get((bContext*)C, member, &result)==1) {
00563         return result.list;
00564     }
00565     else {
00566         ListBase list= {NULL, NULL};
00567         return list;
00568     }
00569 }
00570 
00571 /* 1:found,  -1:found but not set,  0:not found */
00572 int CTX_data_get(const bContext *C, const char *member, PointerRNA *r_ptr, ListBase *r_lb, short *r_type)
00573 {
00574     bContextDataResult result;
00575     int ret= ctx_data_get((bContext*)C, member, &result);
00576 
00577     if(ret==1) {
00578         *r_ptr= result.ptr;
00579         *r_lb= result.list;
00580         *r_type= result.type;
00581     }
00582     else {
00583         memset(r_ptr, 0, sizeof(*r_ptr));
00584         memset(r_lb, 0, sizeof(*r_lb));
00585         *r_type= 0;
00586     }
00587 
00588     return ret;
00589 }
00590 
00591 static void data_dir_add(ListBase *lb, const char *member)
00592 {
00593     LinkData *link;
00594     
00595     if(strcmp(member, "scene") == 0) /* exception */
00596         return;
00597 
00598     if(BLI_findstring(lb, member, offsetof(LinkData, data)))
00599         return;
00600     
00601     link= MEM_callocN(sizeof(LinkData), "LinkData");
00602     link->data= (void*)member;
00603     BLI_addtail(lb, link);
00604 }
00605 
00606 ListBase CTX_data_dir_get(const bContext *C)
00607 {
00608     bContextDataResult result;
00609     ListBase lb;
00610     int a;
00611 
00612     memset(&lb, 0, sizeof(lb));
00613 
00614     if(C->wm.store) {
00615         bContextStoreEntry *entry;
00616 
00617         for(entry=C->wm.store->entries.first; entry; entry=entry->next)
00618             data_dir_add(&lb, entry->name);
00619     }
00620     if(C->wm.region && C->wm.region->type && C->wm.region->type->context) {
00621         memset(&result, 0, sizeof(result));
00622         C->wm.region->type->context(C, "", &result);
00623 
00624         if(result.dir)
00625             for(a=0; result.dir[a]; a++)
00626                 data_dir_add(&lb, result.dir[a]);
00627     }
00628     if(C->wm.area && C->wm.area->type && C->wm.area->type->context) {
00629         memset(&result, 0, sizeof(result));
00630         C->wm.area->type->context(C, "", &result);
00631 
00632         if(result.dir)
00633             for(a=0; result.dir[a]; a++)
00634                 data_dir_add(&lb, result.dir[a]);
00635     }
00636     if(C->wm.screen && C->wm.screen->context) {
00637         bContextDataCallback cb= C->wm.screen->context;
00638         memset(&result, 0, sizeof(result));
00639         cb(C, "", &result);
00640 
00641         if(result.dir)
00642             for(a=0; result.dir[a]; a++)
00643                 data_dir_add(&lb, result.dir[a]);
00644     }
00645 
00646     return lb;
00647 }
00648 
00649 int CTX_data_equals(const char *member, const char *str)
00650 {
00651     return (strcmp(member, str) == 0);
00652 }
00653 
00654 int CTX_data_dir(const char *member)
00655 {
00656     return member[0] == '\0';
00657 }
00658 
00659 void CTX_data_id_pointer_set(bContextDataResult *result, ID *id)
00660 {
00661     RNA_id_pointer_create(id, &result->ptr);
00662 }
00663 
00664 void CTX_data_pointer_set(bContextDataResult *result, ID *id, StructRNA *type, void *data)
00665 {
00666     RNA_pointer_create(id, type, data, &result->ptr);
00667 }
00668 
00669 void CTX_data_id_list_add(bContextDataResult *result, ID *id)
00670 {
00671     CollectionPointerLink *link;
00672 
00673     link= MEM_callocN(sizeof(CollectionPointerLink), "CTX_data_id_list_add");
00674     RNA_id_pointer_create(id, &link->ptr);
00675 
00676     BLI_addtail(&result->list, link);
00677 }
00678 
00679 void CTX_data_list_add(bContextDataResult *result, ID *id, StructRNA *type, void *data)
00680 {
00681     CollectionPointerLink *link;
00682 
00683     link= MEM_callocN(sizeof(CollectionPointerLink), "CTX_data_list_add");
00684     RNA_pointer_create(id, type, data, &link->ptr);
00685 
00686     BLI_addtail(&result->list, link);
00687 }
00688 
00689 int ctx_data_list_count(const bContext *C, int (*func)(const bContext*, ListBase*))
00690 {
00691     ListBase list;
00692 
00693     if(func(C, &list)) {
00694         int tot= BLI_countlist(&list);
00695         BLI_freelistN(&list);
00696         return tot;
00697     }
00698     else
00699         return 0;
00700 }
00701 
00702 void CTX_data_dir_set(bContextDataResult *result, const char **dir)
00703 {
00704     result->dir= dir;
00705 }
00706 
00707 void CTX_data_type_set(bContextDataResult *result, short type)
00708 {
00709     result->type= type;
00710 }
00711 
00712 short CTX_data_type_get(bContextDataResult *result)
00713 {
00714     return result->type;
00715 }
00716 
00717 /* data context */
00718 
00719 Main *CTX_data_main(const bContext *C)
00720 {
00721     Main *bmain;
00722 
00723     if(ctx_data_pointer_verify(C, "blend_data", (void*)&bmain))
00724         return bmain;
00725     else
00726         return C->data.main;
00727 }
00728 
00729 void CTX_data_main_set(bContext *C, Main *bmain)
00730 {
00731     C->data.main= bmain;
00732 }
00733 
00734 Scene *CTX_data_scene(const bContext *C)
00735 {
00736     Scene *scene;
00737 
00738     if(ctx_data_pointer_verify(C, "scene", (void*)&scene))
00739         return scene;
00740     else
00741         return C->data.scene;
00742 }
00743 
00744 int CTX_data_mode_enum(const bContext *C)
00745 {
00746     Object *obedit= CTX_data_edit_object(C);
00747 
00748     if(obedit) {
00749         switch(obedit->type) {
00750             case OB_MESH:
00751                 return CTX_MODE_EDIT_MESH;
00752             case OB_CURVE:
00753                 return CTX_MODE_EDIT_CURVE;
00754             case OB_SURF:
00755                 return CTX_MODE_EDIT_SURFACE;
00756             case OB_FONT:
00757                 return CTX_MODE_EDIT_TEXT;
00758             case OB_ARMATURE:
00759                 return CTX_MODE_EDIT_ARMATURE;
00760             case OB_MBALL:
00761                 return CTX_MODE_EDIT_METABALL;
00762             case OB_LATTICE:
00763                 return CTX_MODE_EDIT_LATTICE;
00764         }
00765     }
00766     else {
00767         Object *ob = CTX_data_active_object(C);
00768 
00769         if(ob) {
00770             if(ob->mode & OB_MODE_POSE) return CTX_MODE_POSE;
00771             else if(ob->mode & OB_MODE_SCULPT)  return CTX_MODE_SCULPT;
00772             else if(ob->mode & OB_MODE_WEIGHT_PAINT) return CTX_MODE_PAINT_WEIGHT;
00773             else if(ob->mode & OB_MODE_VERTEX_PAINT) return CTX_MODE_PAINT_VERTEX;
00774             else if(ob->mode & OB_MODE_TEXTURE_PAINT) return CTX_MODE_PAINT_TEXTURE;
00775             else if(ob->mode & OB_MODE_PARTICLE_EDIT) return CTX_MODE_PARTICLE;
00776         }
00777     }
00778 
00779     return CTX_MODE_OBJECT;
00780 }
00781 
00782 
00783 /* would prefer if we can use the enum version below over this one - Campbell */
00784 /* must be aligned with above enum  */
00785 static const char *data_mode_strings[] = {
00786     "mesh_edit",
00787     "curve_edit",
00788     "surface_edit",
00789     "text_edit",
00790     "armature_edit",
00791     "mball_edit",
00792     "lattice_edit",
00793     "posemode",
00794     "sculpt_mode",
00795     "weightpaint",
00796     "vertexpaint",
00797     "imagepaint",
00798     "particlemode",
00799     "objectmode",
00800     NULL
00801 };
00802 const char *CTX_data_mode_string(const bContext *C)
00803 {
00804     return data_mode_strings[CTX_data_mode_enum(C)];
00805 }
00806 
00807 void CTX_data_scene_set(bContext *C, Scene *scene)
00808 {
00809     C->data.scene= scene;
00810 }
00811 
00812 ToolSettings *CTX_data_tool_settings(const bContext *C)
00813 {
00814     Scene *scene = CTX_data_scene(C);
00815 
00816     if(scene)
00817         return scene->toolsettings;
00818     else
00819         return NULL;
00820 }
00821 
00822 int CTX_data_selected_nodes(const bContext *C, ListBase *list)
00823 {
00824     return ctx_data_collection_get(C, "selected_nodes", list);
00825 }
00826 
00827 int CTX_data_selected_editable_objects(const bContext *C, ListBase *list)
00828 {
00829     return ctx_data_collection_get(C, "selected_editable_objects", list);
00830 }
00831 
00832 int CTX_data_selected_editable_bases(const bContext *C, ListBase *list)
00833 {
00834     return ctx_data_collection_get(C, "selected_editable_bases", list);
00835 }
00836 
00837 int CTX_data_selected_objects(const bContext *C, ListBase *list)
00838 {
00839     return ctx_data_collection_get(C, "selected_objects", list);
00840 }
00841 
00842 int CTX_data_selected_bases(const bContext *C, ListBase *list)
00843 {
00844     return ctx_data_collection_get(C, "selected_bases", list);
00845 }
00846 
00847 int CTX_data_visible_objects(const bContext *C, ListBase *list)
00848 {
00849     return ctx_data_collection_get(C, "visible_objects", list);
00850 }
00851 
00852 int CTX_data_visible_bases(const bContext *C, ListBase *list)
00853 {
00854     return ctx_data_collection_get(C, "visible_bases", list);
00855 }
00856 
00857 int CTX_data_selectable_objects(const bContext *C, ListBase *list)
00858 {
00859     return ctx_data_collection_get(C, "selectable_objects", list);
00860 }
00861 
00862 int CTX_data_selectable_bases(const bContext *C, ListBase *list)
00863 {
00864     return ctx_data_collection_get(C, "selectable_bases", list);
00865 }
00866 
00867 struct Object *CTX_data_active_object(const bContext *C)
00868 {
00869     return ctx_data_pointer_get(C, "active_object");
00870 }
00871 
00872 struct Base *CTX_data_active_base(const bContext *C)
00873 {
00874     return ctx_data_pointer_get(C, "active_base");
00875 }
00876 
00877 struct Object *CTX_data_edit_object(const bContext *C)
00878 {
00879     return ctx_data_pointer_get(C, "edit_object");
00880 }
00881 
00882 struct Image *CTX_data_edit_image(const bContext *C)
00883 {
00884     return ctx_data_pointer_get(C, "edit_image");
00885 }
00886 
00887 struct Text *CTX_data_edit_text(const bContext *C)
00888 {
00889     return ctx_data_pointer_get(C, "edit_text");
00890 }
00891 
00892 struct MovieClip *CTX_data_edit_movieclip(const bContext *C)
00893 {
00894     return ctx_data_pointer_get(C, "edit_movieclip");
00895 }
00896 
00897 struct EditBone *CTX_data_active_bone(const bContext *C)
00898 {
00899     return ctx_data_pointer_get(C, "active_bone");
00900 }
00901 
00902 int CTX_data_selected_bones(const bContext *C, ListBase *list)
00903 {
00904     return ctx_data_collection_get(C, "selected_bones", list);
00905 }
00906 
00907 int CTX_data_selected_editable_bones(const bContext *C, ListBase *list)
00908 {
00909     return ctx_data_collection_get(C, "selected_editable_bones", list);
00910 }
00911 
00912 int CTX_data_visible_bones(const bContext *C, ListBase *list)
00913 {
00914     return ctx_data_collection_get(C, "visible_bones", list);
00915 }
00916 
00917 int CTX_data_editable_bones(const bContext *C, ListBase *list)
00918 {
00919     return ctx_data_collection_get(C, "editable_bones", list);
00920 }
00921 
00922 struct bPoseChannel *CTX_data_active_pose_bone(const bContext *C)
00923 {
00924     return ctx_data_pointer_get(C, "active_pose_bone");
00925 }
00926 
00927 int CTX_data_selected_pose_bones(const bContext *C, ListBase *list)
00928 {
00929     return ctx_data_collection_get(C, "selected_pose_bones", list);
00930 }
00931 
00932 int CTX_data_visible_pose_bones(const bContext *C, ListBase *list)
00933 {
00934     return ctx_data_collection_get(C, "visible_pose_bones", list);
00935 }
00936