copy of docs from 2.4x for python modules that have been kept
[blender.git] / source / blender / editors / space_view3d / view3d_header.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2004-2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "DNA_armature_types.h"
34 #include "DNA_ID.h"
35 #include "DNA_image_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_texture_types.h"
43 #include "DNA_userdef_types.h" /* U.smooth_viewtx */
44 #include "DNA_view3d_types.h"
45 #include "DNA_windowmanager_types.h"
46
47 #include "RNA_access.h"
48
49 #include "MEM_guardedalloc.h"
50
51 #include "BKE_action.h"
52 #include "BKE_brush.h"
53 #include "BKE_context.h"
54 #include "BKE_curve.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_displist.h"
57 #include "BKE_effect.h"
58 #include "BKE_global.h"
59 #include "BKE_image.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_mesh.h"
63 #include "BKE_modifier.h"
64 #include "BKE_paint.h"
65 #include "BKE_particle.h"
66 #include "BKE_screen.h"
67 #include "BKE_utildefines.h" /* for VECCOPY */
68
69 #include "ED_armature.h"
70 #include "ED_particle.h"
71 #include "ED_object.h"
72 #include "ED_mesh.h"
73 #include "ED_util.h"
74 #include "ED_screen.h"
75 #include "ED_transform.h"
76 #include "ED_types.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 #include "RNA_access.h"
82 #include "RNA_define.h"
83 #include "RNA_enum_types.h"
84
85 #include "BIF_gl.h"
86 #include "BIF_glutil.h"
87
88 #include "BLI_math.h"
89 #include "BLI_blenlib.h"
90 #include "BLI_editVert.h"
91
92 #include "UI_interface.h"
93 #include "UI_interface_icons.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 #include "view3d_intern.h"
98
99
100 /* View3d->modeselect 
101  * This is a bit of a dodgy hack to enable a 'mode' menu with icons+labels
102  * rather than those buttons.
103  * I know the implementation's not good - it's an experiment to see if this
104  * approach would work well
105  *
106  * This can be cleaned when I make some new 'mode' icons.
107  */
108
109 #define TEST_EDITMESH   if(obedit==0) return; \
110                                                 if( (v3d->lay & obedit->lay)==0 ) return;
111
112 /* XXX port over */     
113 extern void borderselect();
114
115 /* view3d handler codes */
116 #define VIEW3D_HANDLER_BACKGROUND       1
117 #define VIEW3D_HANDLER_PROPERTIES       2
118 #define VIEW3D_HANDLER_OBJECT           3
119 #define VIEW3D_HANDLER_PREVIEW          4
120 #define VIEW3D_HANDLER_MULTIRES         5
121 #define VIEW3D_HANDLER_TRANSFORM        6
122 #define VIEW3D_HANDLER_GREASEPENCIL 7
123 #define VIEW3D_HANDLER_BONESKETCH       8
124
125 /* end XXX ************* */
126
127 static void do_view3d_header_buttons(bContext *C, void *arg, int event);
128
129 #define B_SCENELOCK 101
130 #define B_FULL          102
131 #define B_HOME          103
132 #define B_VIEWBUT       104
133 #define B_PERSP         105
134 #define B_MODESELECT 108
135 #define B_SEL_VERT      110
136 #define B_SEL_EDGE      111
137 #define B_SEL_FACE      112
138 #define B_MAN_TRANS     116
139 #define B_MAN_ROT       117
140 #define B_MAN_SCALE     118
141 #define B_NDOF          119     
142 #define B_MAN_MODE      120
143 #define B_REDR          122
144 #define B_NOP           123
145
146 // XXX quickly ported across
147 static void handle_view3d_lock(bContext *C) 
148 {
149         Main *bmain= CTX_data_main(C);
150         Scene *scene= CTX_data_scene(C);
151         ScrArea *sa= CTX_wm_area(C);
152         View3D *v3d= CTX_wm_view3d(C);
153         
154         if (v3d != NULL && sa != NULL) {
155                 if(v3d->localvd==NULL && v3d->scenelock && sa->spacetype==SPACE_VIEW3D) {
156                         /* copy to scene */
157                         scene->lay= v3d->lay;
158                         scene->camera= v3d->camera;
159
160                         /* not through notifiery, listener don't have context
161                            and non-open screens or spaces need to be updated too */
162                         ED_view3d_scene_layers_update(bmain, scene);
163                         
164                         /* notifiers for scene update */
165                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER, scene);
166                 }
167         }
168 }
169
170 static int layers_exec(bContext *C, wmOperator *op)
171 {
172         Scene *scene= CTX_data_scene(C);
173         ScrArea *sa= CTX_wm_area(C);
174         View3D *v3d= sa->spacedata.first;
175         int nr= RNA_int_get(op->ptr, "nr");
176         int toggle= RNA_boolean_get(op->ptr, "toggle");
177         
178         if(nr < 0)
179                 return OPERATOR_CANCELLED;
180         
181         
182         if(nr == 0) {
183                 /* all layers */
184                 v3d->lay |= (1<<20)-1;
185
186                 if(!v3d->layact)
187                         v3d->layact= 1;
188         }
189         else {
190                 nr--;
191
192                 if(RNA_boolean_get(op->ptr, "extend")) {
193                         if(toggle && v3d->lay & (1<<nr) && (v3d->lay & ~(1<<nr)))
194                                 v3d->lay &= ~(1<<nr);
195                         else
196                                 v3d->lay |= (1<<nr);
197                 } else
198                         v3d->lay = (1<<nr);
199                 
200                 /* set active layer, ensure to always have one */
201                 if(v3d->lay & (1<<nr))
202                    v3d->layact= 1<<nr;
203                 else if((v3d->lay & v3d->layact)==0) {
204                         int bit= 0;
205
206                         while(bit<32) {
207                                 if(v3d->lay & (1<<bit)) {
208                                         v3d->layact= 1<<bit;
209                                         break;
210                                 }
211                                 bit++;
212                         }
213                 }
214         }
215         
216         if(v3d->scenelock) handle_view3d_lock(C);
217         
218         /* new layers might need unflushed events events */
219         DAG_scene_update_flags(scene, v3d->lay);        /* tags all that moves and flushes */
220
221         ED_area_tag_redraw(sa);
222         
223         return OPERATOR_FINISHED;
224 }
225
226 /* applies shift and alt, lazy coding or ok? :) */
227 /* the local per-keymap-entry keymap will solve it */
228 static int layers_invoke(bContext *C, wmOperator *op, wmEvent *event)
229 {
230         if(event->ctrl || event->oskey)
231                 return OPERATOR_PASS_THROUGH;
232         
233         if(event->shift)
234                 RNA_boolean_set(op->ptr, "extend", 1);
235         
236         if(event->alt) {
237                 int nr= RNA_int_get(op->ptr, "nr") + 10;
238                 RNA_int_set(op->ptr, "nr", nr);
239         }
240         layers_exec(C, op);
241         
242         return OPERATOR_FINISHED;
243 }
244
245 int layers_poll(bContext *C)
246 {
247         return (ED_operator_view3d_active(C) && CTX_wm_view3d(C)->localvd==NULL);
248 }
249
250 void VIEW3D_OT_layers(wmOperatorType *ot)
251 {
252         /* identifiers */
253         ot->name= "Layers";
254         ot->description= "Toggle layer(s) visibility.";
255         ot->idname= "VIEW3D_OT_layers";
256         
257         /* api callbacks */
258         ot->invoke= layers_invoke;
259         ot->exec= layers_exec;
260         ot->poll= layers_poll;
261         
262         /* flags */
263         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
264         
265         RNA_def_int(ot->srna, "nr", 1, 0, 20, "Number", "The layer number to set, zero for all layers", 0, 20);
266         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Add this layer to the current view layers");
267         RNA_def_boolean(ot->srna, "toggle", 1, "Toggle", "Toggle the layer");
268 }
269
270 static char *view3d_modeselect_pup(Scene *scene)
271 {
272         Object *ob= OBACT;
273         static char string[1024];
274         static char formatstr[] = "|%s %%x%d %%i%d";
275         char *str = string;
276
277         str += sprintf(str, "Mode: %%t");
278         
279         str += sprintf(str, formatstr, "Object Mode", OB_MODE_OBJECT, ICON_OBJECT_DATA);
280         
281         if(ob==NULL) return string;
282         
283         /* if active object is editable */
284         if ( ((ob->type == OB_MESH)
285                 || (ob->type == OB_CURVE) || (ob->type == OB_SURF) || (ob->type == OB_FONT)
286                 || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) {
287                 
288                 str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
289         }
290         else if (ob->type == OB_ARMATURE) {
291                 if (ob->mode & OB_MODE_POSE)
292                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT|OB_MODE_POSE, ICON_EDITMODE_HLT);
293                 else
294                         str += sprintf(str, formatstr, "Edit Mode", OB_MODE_EDIT, ICON_EDITMODE_HLT);
295         }
296
297         if (ob->type == OB_MESH) {
298
299                 str += sprintf(str, formatstr, "Sculpt Mode", OB_MODE_SCULPT, ICON_SCULPTMODE_HLT);
300                 str += sprintf(str, formatstr, "Vertex Paint", OB_MODE_VERTEX_PAINT, ICON_VPAINT_HLT);
301                 str += sprintf(str, formatstr, "Texture Paint", OB_MODE_TEXTURE_PAINT, ICON_TPAINT_HLT);
302                 str += sprintf(str, formatstr, "Weight Paint", OB_MODE_WEIGHT_PAINT, ICON_WPAINT_HLT);
303         }
304
305         
306         /* if active object is an armature */
307         if (ob->type==OB_ARMATURE) {
308                 str += sprintf(str, formatstr, "Pose Mode", OB_MODE_POSE, ICON_POSE_HLT);
309         }
310
311         if (ob->particlesystem.first || modifiers_findByType(ob, eModifierType_Cloth) || modifiers_findByType(ob, eModifierType_Softbody)) {
312                 str += sprintf(str, formatstr, "Particle Mode", OB_MODE_PARTICLE_EDIT, ICON_PARTICLEMODE);
313         }
314
315         return (string);
316 }
317
318
319 static void do_view3d_header_buttons(bContext *C, void *arg, int event)
320 {
321         wmWindow *win= CTX_wm_window(C);
322         ToolSettings *ts= CTX_data_tool_settings(C);
323         ScrArea *sa= CTX_wm_area(C);
324         View3D *v3d= sa->spacedata.first;
325         Object *obedit = CTX_data_edit_object(C);
326         EditMesh *em= NULL;
327         int ctrl= win->eventstate->ctrl, shift= win->eventstate->shift;
328         PointerRNA props_ptr;
329         
330         if(obedit && obedit->type==OB_MESH) {
331                 em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
332         }
333         /* watch it: if sa->win does not exist, check that when calling direct drawing routines */
334
335         switch(event) {
336         case B_REDR:
337                 ED_area_tag_redraw(sa);
338                 break;
339                 
340         case B_MODESELECT:
341                 WM_operator_properties_create(&props_ptr, "OBJECT_OT_mode_set");
342                 RNA_enum_set(&props_ptr, "mode", v3d->modeselect);
343                 WM_operator_name_call(C, "OBJECT_OT_mode_set", WM_OP_EXEC_REGION_WIN, &props_ptr);
344                 WM_operator_properties_free(&props_ptr);
345                 break;          
346                 
347         case B_SEL_VERT:
348                 if(em) {
349                         if(shift==0 || em->selectmode==0)
350                                 em->selectmode= SCE_SELECT_VERTEX;
351                         ts->selectmode= em->selectmode;
352                         EM_selectmode_set(em);
353                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
354                         ED_undo_push(C, "Selectmode Set: Vertex");
355                 }
356                 break;
357         case B_SEL_EDGE:
358                 if(em) {
359                         if(shift==0 || em->selectmode==0){
360                                 if( (em->selectmode ^ SCE_SELECT_EDGE) == SCE_SELECT_VERTEX){
361                                         if(ctrl) EM_convertsel(em, SCE_SELECT_VERTEX,SCE_SELECT_EDGE); 
362                                 }
363                                 em->selectmode = SCE_SELECT_EDGE;
364                         }
365                         ts->selectmode= em->selectmode;
366                         EM_selectmode_set(em);
367                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
368                         ED_undo_push(C, "Selectmode Set: Edge");
369                 }
370                 break;
371         case B_SEL_FACE:
372                 if(em) {
373                         if( shift==0 || em->selectmode==0){
374                                 if( ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_VERTEX) || ((ts->selectmode ^ SCE_SELECT_FACE) == SCE_SELECT_EDGE)){
375                                         if(ctrl) EM_convertsel(em, (ts->selectmode ^ SCE_SELECT_FACE),SCE_SELECT_FACE);
376                                 }
377                                 em->selectmode = SCE_SELECT_FACE;
378                         }
379                         ts->selectmode= em->selectmode;
380                         EM_selectmode_set(em);
381                         WM_event_add_notifier(C, NC_GEOM|ND_SELECT, obedit->data);
382                         ED_undo_push(C, "Selectmode Set: Face");
383                 }
384                 break;  
385
386         case B_MAN_TRANS:
387                 if( shift==0 || v3d->twtype==0) {
388                         v3d->twtype= V3D_MANIP_TRANSLATE;
389                 }
390         ED_area_tag_redraw(sa);
391         break;
392         case B_MAN_ROT:
393                 if( shift==0 || v3d->twtype==0) {
394             v3d->twtype= V3D_MANIP_ROTATE;
395                 }
396         ED_area_tag_redraw(sa);
397                 break;
398         case B_MAN_SCALE:
399                 if( shift==0 || v3d->twtype==0) {
400             v3d->twtype= V3D_MANIP_SCALE;
401                 }
402         ED_area_tag_redraw(sa);
403                 break;
404         case B_NDOF:
405         ED_area_tag_redraw(sa);
406                 break;
407         case B_MAN_MODE:
408         ED_area_tag_redraw(sa);
409                 break;
410         default:
411                 break;
412         }
413
414         if(obedit && obedit->type==OB_MESH)
415                 BKE_mesh_end_editmesh(obedit->data, em);
416 }
417
418 /* Returns the icon associated with an object mode */
419 static int object_mode_icon(int mode)
420 {
421         EnumPropertyItem *item = object_mode_items;
422         
423         while(item->name != NULL) {
424                 if(item->value == mode)
425                         return item->icon;
426                 ++item;
427         }
428
429         return ICON_OBJECT_DATAMODE;
430 }
431
432 void uiTemplateHeader3D(uiLayout *layout, struct bContext *C)
433 {
434         bScreen *screen= CTX_wm_screen(C);
435         ScrArea *sa= CTX_wm_area(C);
436         View3D *v3d= sa->spacedata.first;
437         Scene *scene= CTX_data_scene(C);
438         ToolSettings *ts= CTX_data_tool_settings(C);
439         PointerRNA v3dptr, toolsptr, sceneptr;
440         Object *ob= OBACT;
441         Object *obedit = CTX_data_edit_object(C);
442         uiBlock *block;
443         uiLayout *row;
444         
445         RNA_pointer_create(&screen->id, &RNA_Space3DView, v3d, &v3dptr);        
446         RNA_pointer_create(&scene->id, &RNA_ToolSettings, ts, &toolsptr);
447         RNA_pointer_create(&scene->id, &RNA_Scene, scene, &sceneptr);
448
449         block= uiLayoutGetBlock(layout);
450         uiBlockSetHandleFunc(block, do_view3d_header_buttons, NULL);
451
452         /* other buttons: */
453         uiBlockSetEmboss(block, UI_EMBOSS);
454         
455         /* mode */
456         if(ob)
457                 v3d->modeselect = ob->mode;
458         else
459                 v3d->modeselect = OB_MODE_OBJECT;
460                 
461         v3d->flag &= ~V3D_MODE;
462         
463         /* not sure what the v3d->flag is useful for now... modeselect is confusing */
464         if(obedit) v3d->flag |= V3D_EDITMODE;
465         if(ob && (ob->mode & OB_MODE_POSE)) v3d->flag |= V3D_POSEMODE;
466         if(ob && (ob->mode & OB_MODE_VERTEX_PAINT)) v3d->flag |= V3D_VERTEXPAINT;
467         if(ob && (ob->mode & OB_MODE_WEIGHT_PAINT)) v3d->flag |= V3D_WEIGHTPAINT;
468         if(ob && (ob->mode & OB_MODE_TEXTURE_PAINT)) v3d->flag |= V3D_TEXTUREPAINT;
469         if(paint_facesel_test(ob)) v3d->flag |= V3D_FACESELECT;
470
471         uiBlockBeginAlign(block);
472         uiDefIconTextButS(block, MENU, B_MODESELECT, object_mode_icon(v3d->modeselect), view3d_modeselect_pup(scene) , 
473                           0,0,126,20, &(v3d->modeselect), 0, 0, 0, 0, "Mode");
474         uiBlockEndAlign(block);
475         
476         /* Draw type */
477         uiItemR(layout, "", 0, &v3dptr, "viewport_shading", UI_ITEM_R_ICON_ONLY);
478
479         if (obedit==NULL && ((ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)))) {
480                 /* Manipulators aren't used in weight paint mode */
481                 
482                 PointerRNA meshptr;
483
484                 RNA_pointer_create(&ob->id, &RNA_Mesh, ob->data, &meshptr);
485                 uiItemR(layout, "", 0, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY);
486         } else {
487                 char *str_menu;
488
489                 row= uiLayoutRow(layout, 1);
490                 uiItemR(row, "", 0, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY);
491                 uiItemR(row, "", 0, &v3dptr, "pivot_point_align", UI_ITEM_R_ICON_ONLY);
492
493                 /* NDOF */
494                 /* Not implemented yet
495                  if (G.ndofdevice ==0 ) {
496                         uiDefIconTextButC(block, ICONTEXTROW,B_NDOF, ICON_NDOF_TURN, ndof_pup(), 0,0,XIC+10,YIC, &(v3d->ndofmode), 0, 3.0, 0, 0, "Ndof mode");
497                 
498                         uiDefIconButC(block, TOG, B_NDOF,  ICON_NDOF_DOM,
499                                       0,0,XIC,YIC,
500                                       &v3d->ndoffilter, 0, 1, 0, 0, "dominant axis");   
501                 }
502                  */
503
504                 /* Transform widget / manipulators */
505                 row= uiLayoutRow(layout, 1);
506                 uiItemR(row, "", 0, &v3dptr, "manipulator", UI_ITEM_R_ICON_ONLY);
507                 block= uiLayoutGetBlock(row);
508                 
509                 if(v3d->twflag & V3D_USE_MANIPULATOR) {
510                         uiDefIconButBitS(block, TOG, V3D_MANIP_TRANSLATE, B_MAN_TRANS, ICON_MAN_TRANS, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Translate manipulator mode");
511                         uiDefIconButBitS(block, TOG, V3D_MANIP_ROTATE, B_MAN_ROT, ICON_MAN_ROT, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Rotate manipulator mode");
512                         uiDefIconButBitS(block, TOG, V3D_MANIP_SCALE, B_MAN_SCALE, ICON_MAN_SCALE, 0,0,XIC,YIC, &v3d->twtype, 1.0, 0.0, 0, 0, "Scale manipulator mode");
513                 }
514                         
515                 if (v3d->twmode > (BIF_countTransformOrientation(C) - 1) + V3D_MANIP_CUSTOM) {
516                         v3d->twmode = 0;
517                 }
518                         
519                 str_menu = BIF_menustringTransformOrientation(C, "Orientation");
520                 uiDefButS(block, MENU, B_MAN_MODE, str_menu,0,0,70,YIC, &v3d->twmode, 0, 0, 0, 0, "Transform Orientation");
521                 MEM_freeN(str_menu);
522         }
523                 
524         if(obedit==NULL && v3d->localvd==NULL) {
525                 int ob_lay = ob ? ob->lay : 0;
526                 
527                 /* Layers */
528                 if (v3d->scenelock)
529                         uiTemplateLayers(layout, &sceneptr, "visible_layers", &v3dptr, "used_layers", ob_lay);
530                 else
531                         uiTemplateLayers(layout, &v3dptr, "visible_layers", &v3dptr, "used_layers", ob_lay);
532
533                 /* Scene lock */
534                 uiItemR(layout, "", 0, &v3dptr, "lock_camera_and_layers", UI_ITEM_R_ICON_ONLY);
535         }
536 }