Blender V2.61 - r43446

space_buttons.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) 2008 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * 
00022  * Contributor(s): Blender Foundation
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00032 #include <string.h>
00033 #include <stdio.h>
00034 
00035 #include "MEM_guardedalloc.h"
00036 
00037 #include "BLI_blenlib.h"
00038 #include "BLI_math.h"
00039 #include "BLI_rand.h"
00040 #include "BLI_utildefines.h"
00041 
00042 #include "BKE_context.h"
00043 #include "BKE_screen.h"
00044 
00045 #include "ED_space_api.h"
00046 #include "ED_screen.h"
00047 
00048 #include "BIF_gl.h"
00049 
00050 #include "WM_api.h"
00051 #include "WM_types.h"
00052 
00053 #include "UI_resources.h"
00054 #include "UI_view2d.h"
00055 
00056 #include "ED_render.h"
00057 
00058 #include "buttons_intern.h" // own include
00059 
00060 /* ******************** default callbacks for buttons space ***************** */
00061 
00062 static SpaceLink *buttons_new(const bContext *UNUSED(C))
00063 {
00064     ARegion *ar;
00065     SpaceButs *sbuts;
00066     
00067     sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
00068     sbuts->spacetype= SPACE_BUTS;
00069     sbuts->align= BUT_VERTICAL;
00070 
00071     /* header */
00072     ar= MEM_callocN(sizeof(ARegion), "header for buts");
00073     
00074     BLI_addtail(&sbuts->regionbase, ar);
00075     ar->regiontype= RGN_TYPE_HEADER;
00076     ar->alignment= RGN_ALIGN_TOP;
00077     
00078 #if 0
00079     /* context area */
00080     ar= MEM_callocN(sizeof(ARegion), "context area for buts");
00081     BLI_addtail(&sbuts->regionbase, ar);
00082     ar->regiontype= RGN_TYPE_CHANNELS;
00083     ar->alignment= RGN_ALIGN_TOP;
00084 #endif
00085 
00086     /* main area */
00087     ar= MEM_callocN(sizeof(ARegion), "main area for buts");
00088     
00089     BLI_addtail(&sbuts->regionbase, ar);
00090     ar->regiontype= RGN_TYPE_WINDOW;
00091     
00092     return (SpaceLink *)sbuts;
00093 }
00094 
00095 /* not spacelink itself */
00096 static void buttons_free(SpaceLink *sl)
00097 {   
00098     SpaceButs *sbuts= (SpaceButs*) sl;
00099 
00100     if(sbuts->path)
00101         MEM_freeN(sbuts->path);
00102     
00103     if(sbuts->texuser) {
00104         ButsContextTexture *ct= sbuts->texuser;
00105         BLI_freelistN(&ct->users);
00106         MEM_freeN(ct);
00107     }
00108 }
00109 
00110 /* spacetype; init callback */
00111 static void buttons_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
00112 {
00113     SpaceButs *sbuts= sa->spacedata.first;
00114 
00115     /* auto-align based on size */
00116     if(sbuts->align == BUT_AUTO || !sbuts->align) {
00117         if(sa->winx > sa->winy)
00118             sbuts->align= BUT_HORIZONTAL;
00119         else
00120             sbuts->align= BUT_VERTICAL;
00121     }
00122 }
00123 
00124 static SpaceLink *buttons_duplicate(SpaceLink *sl)
00125 {
00126     SpaceButs *sbutsn= MEM_dupallocN(sl);
00127     
00128     /* clear or remove stuff from old */
00129     sbutsn->path= NULL;
00130     sbutsn->texuser= NULL;
00131     
00132     return (SpaceLink *)sbutsn;
00133 }
00134 
00135 /* add handlers, stuff you only do once or on area/region changes */
00136 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
00137 {
00138     wmKeyMap *keymap;
00139 
00140     ED_region_panels_init(wm, ar);
00141 
00142     keymap= WM_keymap_find(wm->defaultconf, "Property Editor", SPACE_BUTS, 0);
00143     WM_event_add_keymap_handler(&ar->handlers, keymap);
00144 }
00145 
00146 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
00147 {
00148     /* draw entirely, view changes should be handled here */
00149     SpaceButs *sbuts= CTX_wm_space_buts(C);
00150     int vertical= (sbuts->align == BUT_VERTICAL);
00151 
00152     buttons_context_compute(C, sbuts);
00153 
00154     if(sbuts->mainb == BCONTEXT_SCENE)
00155         ED_region_panels(C, ar, vertical, "scene", sbuts->mainb);
00156     else if(sbuts->mainb == BCONTEXT_RENDER)
00157         ED_region_panels(C, ar, vertical, "render", sbuts->mainb);
00158     else if(sbuts->mainb == BCONTEXT_WORLD)
00159         ED_region_panels(C, ar, vertical, "world", sbuts->mainb);
00160     else if(sbuts->mainb == BCONTEXT_OBJECT)
00161         ED_region_panels(C, ar, vertical, "object", sbuts->mainb);
00162     else if(sbuts->mainb == BCONTEXT_DATA)
00163         ED_region_panels(C, ar, vertical, "data", sbuts->mainb);
00164     else if(sbuts->mainb == BCONTEXT_MATERIAL)
00165         ED_region_panels(C, ar, vertical, "material", sbuts->mainb);
00166     else if(sbuts->mainb == BCONTEXT_TEXTURE)
00167         ED_region_panels(C, ar, vertical, "texture", sbuts->mainb);
00168     else if(sbuts->mainb == BCONTEXT_PARTICLE)
00169         ED_region_panels(C, ar, vertical, "particle", sbuts->mainb);
00170     else if(sbuts->mainb == BCONTEXT_PHYSICS)
00171         ED_region_panels(C, ar, vertical, "physics", sbuts->mainb);
00172     else if(sbuts->mainb == BCONTEXT_BONE)
00173         ED_region_panels(C, ar, vertical, "bone", sbuts->mainb);
00174     else if(sbuts->mainb == BCONTEXT_MODIFIER)
00175         ED_region_panels(C, ar, vertical, "modifier", sbuts->mainb);
00176     else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
00177         ED_region_panels(C, ar, vertical, "constraint", sbuts->mainb);
00178     else if(sbuts->mainb == BCONTEXT_BONE_CONSTRAINT)
00179         ED_region_panels(C, ar, vertical, "bone_constraint", sbuts->mainb);
00180 
00181     sbuts->re_align= 0;
00182     sbuts->mainbo= sbuts->mainb;
00183 }
00184 
00185 static void buttons_operatortypes(void)
00186 {
00187     WM_operatortype_append(BUTTONS_OT_toolbox);
00188     WM_operatortype_append(BUTTONS_OT_file_browse);
00189     WM_operatortype_append(BUTTONS_OT_directory_browse);
00190 }
00191 
00192 static void buttons_keymap(struct wmKeyConfig *keyconf)
00193 {
00194     wmKeyMap *keymap= WM_keymap_find(keyconf, "Property Editor", SPACE_BUTS, 0);
00195     
00196     WM_keymap_add_item(keymap, "BUTTONS_OT_toolbox", RIGHTMOUSE, KM_PRESS, 0, 0);
00197 }
00198 
00199 /* add handlers, stuff you only do once or on area/region changes */
00200 static void buttons_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
00201 {
00202     UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
00203 }
00204 
00205 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
00206 {
00207     /* clear */
00208     UI_ThemeClearColor(ED_screen_area_active(C)?TH_HEADER:TH_HEADERDESEL);
00209     glClear(GL_COLOR_BUFFER_BIT);
00210     
00211     /* set view2d view matrix for scrolling (without scrollers) */
00212     UI_view2d_view_ortho(&ar->v2d);
00213     
00214     buttons_header_buttons(C, ar);
00215 
00216     /* restore view matrix? */
00217     UI_view2d_view_restore(C);
00218 }
00219 
00220 /* draw a certain button set only if properties area is currently
00221  * showing that button set, to reduce unnecessary drawing. */
00222 static void buttons_area_redraw(ScrArea *sa, short buttons)
00223 {
00224     SpaceButs *sbuts= sa->spacedata.first;
00225     
00226     /* if the area's current button set is equal to the one to redraw */
00227     if(sbuts->mainb == buttons)
00228         ED_area_tag_redraw(sa);
00229 }
00230 
00231 /* reused! */
00232 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
00233 {
00234     SpaceButs *sbuts= sa->spacedata.first;
00235 
00236     /* context changes */
00237     switch(wmn->category) {
00238         case NC_SCENE:
00239             switch(wmn->data) {
00240                 case ND_RENDER_OPTIONS:
00241                     buttons_area_redraw(sa, BCONTEXT_RENDER);
00242                     break;
00243                 case ND_FRAME:
00244                     /* any buttons area can have animated properties so redraw all */
00245                     ED_area_tag_redraw(sa);
00246                     sbuts->preview= 1;
00247                     break;
00248                 case ND_OB_ACTIVE:
00249                     ED_area_tag_redraw(sa);
00250                     sbuts->preview= 1;
00251                     break;
00252                 case ND_KEYINGSET:
00253                     buttons_area_redraw(sa, BCONTEXT_SCENE);
00254                     break;
00255                 case ND_RENDER_RESULT:
00256                     break;
00257                 case ND_MODE:
00258                 case ND_LAYER:
00259                 default:
00260                     ED_area_tag_redraw(sa);
00261                     break;
00262             }
00263             break;
00264         case NC_OBJECT:
00265             switch(wmn->data) {
00266                 case ND_TRANSFORM:
00267                     buttons_area_redraw(sa, BCONTEXT_OBJECT);
00268                     buttons_area_redraw(sa, BCONTEXT_DATA); /* autotexpace flag */
00269                     break;
00270                 case ND_POSE:
00271                     buttons_area_redraw(sa, BCONTEXT_DATA);
00272                 case ND_BONE_ACTIVE:
00273                 case ND_BONE_SELECT:
00274                     buttons_area_redraw(sa, BCONTEXT_BONE);
00275                     buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
00276                     break;
00277                 case ND_MODIFIER:
00278                     if(wmn->action == NA_RENAME)
00279                         ED_area_tag_redraw(sa);
00280                     else
00281                         buttons_area_redraw(sa, BCONTEXT_MODIFIER);
00282                         buttons_area_redraw(sa, BCONTEXT_PHYSICS);
00283                     break;
00284                 case ND_CONSTRAINT:
00285                     buttons_area_redraw(sa, BCONTEXT_CONSTRAINT);
00286                     buttons_area_redraw(sa, BCONTEXT_BONE_CONSTRAINT);
00287                     break;
00288                 case ND_PARTICLE:
00289                     if (wmn->action == NA_EDITED)
00290                         buttons_area_redraw(sa, BCONTEXT_PARTICLE);
00291                     sbuts->preview= 1;
00292                     break;
00293                 case ND_DRAW:
00294                     buttons_area_redraw(sa, BCONTEXT_OBJECT);
00295                     buttons_area_redraw(sa, BCONTEXT_DATA);
00296                     buttons_area_redraw(sa, BCONTEXT_PHYSICS);
00297                 case ND_SHADING:
00298                 case ND_SHADING_DRAW:
00299                     /* currently works by redraws... if preview is set, it (re)starts job */
00300                     sbuts->preview= 1;
00301                     break;
00302                 default:
00303                     /* Not all object RNA props have a ND_ notifier (yet) */
00304                     ED_area_tag_redraw(sa);
00305                     break;
00306             }
00307             break;
00308         case NC_GEOM:
00309             switch(wmn->data) {
00310                 case ND_SELECT:
00311                 case ND_DATA:
00312                     ED_area_tag_redraw(sa);
00313                     break;
00314             }
00315             break;
00316         case NC_MATERIAL:
00317             ED_area_tag_redraw(sa);
00318             switch(wmn->data) {
00319                 case ND_SHADING:
00320                 case ND_SHADING_DRAW:
00321                 case ND_NODES:
00322                     /* currently works by redraws... if preview is set, it (re)starts job */
00323                     sbuts->preview= 1;
00324                     break;
00325             }                   
00326             break;
00327         case NC_WORLD:
00328             buttons_area_redraw(sa, BCONTEXT_WORLD);
00329             sbuts->preview= 1;
00330             break;
00331         case NC_LAMP:
00332             buttons_area_redraw(sa, BCONTEXT_DATA);
00333             sbuts->preview= 1;
00334             break;
00335         case NC_BRUSH:
00336             buttons_area_redraw(sa, BCONTEXT_TEXTURE);
00337             break;
00338         case NC_TEXTURE:
00339         case NC_IMAGE:
00340             ED_area_tag_redraw(sa);
00341             sbuts->preview= 1;
00342             break;
00343         case NC_SPACE:
00344             if(wmn->data == ND_SPACE_PROPERTIES)
00345                 ED_area_tag_redraw(sa);
00346             break;
00347         case NC_ID:
00348             if(wmn->action == NA_RENAME)
00349                 ED_area_tag_redraw(sa);
00350             break;
00351         case NC_ANIMATION:
00352             switch(wmn->data) {
00353                 case ND_KEYFRAME:
00354                     if (wmn->action == NA_EDITED)
00355                         ED_area_tag_redraw(sa);
00356                     break;
00357             }
00358             break;
00359         case NC_NODE:
00360             if(wmn->action==NA_SELECTED) {
00361                 ED_area_tag_redraw(sa);
00362                 /* new active node, update texture preview */
00363                 if(sbuts->mainb == BCONTEXT_TEXTURE)
00364                     sbuts->preview= 1;
00365             }
00366             break;
00367         /* Listener for preview render, when doing an global undo. */
00368         case NC_WINDOW:
00369             ED_area_tag_redraw(sa);
00370             sbuts->preview= 1;
00371             break;
00372     }
00373 
00374     if(wmn->data == ND_KEYS)
00375         ED_area_tag_redraw(sa);
00376 }
00377 
00378 /* only called once, from space/spacetypes.c */
00379 void ED_spacetype_buttons(void)
00380 {
00381     SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
00382     ARegionType *art;
00383     
00384     st->spaceid= SPACE_BUTS;
00385     strncpy(st->name, "Buttons", BKE_ST_MAXNAME);
00386     
00387     st->new= buttons_new;
00388     st->free= buttons_free;
00389     st->init= buttons_init;
00390     st->duplicate= buttons_duplicate;
00391     st->operatortypes= buttons_operatortypes;
00392     st->keymap= buttons_keymap;
00393     st->listener= buttons_area_listener;
00394     st->context= buttons_context;
00395     
00396     /* regions: main window */
00397     art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
00398     art->regionid = RGN_TYPE_WINDOW;
00399     art->init= buttons_main_area_init;
00400     art->draw= buttons_main_area_draw;
00401     art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
00402     BLI_addhead(&st->regiontypes, art);
00403 
00404     buttons_context_register(art);
00405     
00406     /* regions: header */
00407     art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
00408     art->regionid = RGN_TYPE_HEADER;
00409     art->prefsizey= HEADERY;
00410     art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
00411     
00412     art->init= buttons_header_area_init;
00413     art->draw= buttons_header_area_draw;
00414     BLI_addhead(&st->regiontypes, art);
00415 
00416     BKE_spacetype_register(st);
00417 }
00418