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