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