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