New option; "Lock Time to Other Windows" for Ipo, Action, NLA and Time
[blender.git] / source / blender / src / header_sound.c
1 /**
2  * header_sound.c oct-2003
3  *
4  * Functions to draw the "Audio Timeline" window header
5  * and handle user events sent to it.
6  *
7  * $Id$
8  *
9  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version. The Blender
15  * Foundation also sells licenses for use in proprietary software under
16  * the Blender License.  See http://www.blender.org/BL/ for information
17  * about this.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA        02111-1307, USA.
27  *
28  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
29  * All rights reserved.
30  *
31  * The Original Code is: all of this file.
32  *
33  * Contributor(s): none yet.
34  *
35  * ***** END GPL/BL DUAL LICENSE BLOCK *****
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include "BMF_Api.h"
47 #include "BIF_language.h"
48
49 #include "DNA_ID.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_sound_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_view2d_types.h"
55 #include "DNA_userdef_types.h"
56
57 #include "BIF_editsound.h"
58 #include "BIF_gl.h"
59 #include "BIF_interface.h"
60 #include "BIF_previewrender.h"
61 #include "BIF_resources.h"
62 #include "BIF_screen.h"
63 #include "BIF_space.h"
64 #include "BIF_toolbox.h"
65 #include "BIF_butspace.h"
66
67 #include "BKE_global.h"
68 #include "BKE_main.h"
69
70 #include "BSE_drawipo.h"
71 #include "BSE_filesel.h"
72 #include "BSE_headerbuttons.h"
73
74 #include "blendef.h"
75 #include "mydevice.h"
76
77 void do_sound_buttons(unsigned short event)
78 {
79         ID *id, *idtest;
80         int nr;
81         char name[256];
82
83         switch(event) {
84
85         case B_SOUNDBROWSE: 
86                 if(G.ssound->sndnr== -2) {
87                         activate_databrowse((ID *)G.ssound->sound, ID_SO, 0,
88                                                                                         B_SOUNDBROWSE, &G.ssound->sndnr, do_sound_buttons);
89                         return;
90                 }
91                 if (G.ssound->sndnr < 0) break;
92                 if (G.ssound->sndnr == 32766) {
93                         if (G.ssound && G.ssound->sound) strcpy(name, G.ssound->sound->name);
94                         else strcpy(name, U.sounddir);
95                         activate_fileselect(FILE_SPECIAL, "SELECT WAV FILE", name,
96                                                                                         load_space_sound);
97                 } else {
98                         nr= 1;
99                         id= (ID *)G.ssound->sound;
100
101                         idtest= G.main->sound.first;
102                         while(idtest) {
103                                 if(nr==G.ssound->sndnr) {
104                                         break;
105                                 }
106                                 nr++;
107                                 idtest= idtest->next;
108                         }
109
110                         if(idtest==0) { /* no new */
111                                 return;
112                         }
113
114                         if(idtest!=id) {
115                                 G.ssound->sound= (bSound *)idtest;
116                                 if(idtest->us==0) idtest->us= 1;
117                                 allqueue(REDRAWSOUND, 0);
118                         }
119                 }
120
121                 break;
122         case B_SOUNDBROWSE2:    
123                 id = (ID *)G.buts->lockpoin;
124                 if(G.buts->texnr == -2) {
125                         activate_databrowse(id, ID_SO, 0, B_SOUNDBROWSE2,
126                                                                                         &G.buts->texnr, do_sound_buttons);
127                         return;
128                 }
129                 if (G.buts->texnr < 0) break;
130                 if (G.buts->texnr == 32766) {
131                         if (id) strcpy(name, ((bSound *)id)->name);
132                         else strcpy(name, U.sounddir);
133                         activate_fileselect(FILE_SPECIAL, "SELECT WAV FILE", name, load_sound_buttons);
134                 } 
135                 else {
136                         nr= 1;
137
138                         idtest= G.main->sound.first;
139                         while (idtest) {
140                                 if(nr == G.buts->texnr) {
141                                         break;
142                                 }
143                                 nr++;
144                                 idtest = idtest->next;
145                         }
146
147                         if (idtest == 0) {      /* geen new */
148                                 return;
149                         }
150
151                         if (idtest != id) {
152                                 G.buts->lockpoin = (bSound *)idtest;
153                                 if(idtest->us==0) idtest->us= 1;
154                                 allqueue(REDRAWBUTSSCENE, 0);
155                         }
156                 }
157                 break;
158
159         case B_SOUNDHOME:
160                 if(G.ssound->sound==NULL) {
161                         G.v2d->tot.xmin= G.scene->r.sfra;
162                         G.v2d->tot.xmax= G.scene->r.efra;
163                 }
164                 G.v2d->cur= G.v2d->tot;
165                 test_view2d(G.v2d, curarea->winx, curarea->winy);
166                 view2d_do_locks(curarea, V2D_LOCK_COPY);
167                 scrarea_queue_winredraw(curarea);
168                 break;
169         }
170 }
171
172 static void do_sound_viewmenu(void *arg, int event)
173 {
174         extern int play_anim(int mode);
175         
176         switch(event) {
177                 case 1: /* Play Back Animation */
178                         play_anim(0);
179                         break;
180                 case 2: /* Play Back Animation in All */
181                         play_anim(1);
182                         break;
183                 case 3: /* View All */
184                         do_sound_buttons(B_SOUNDHOME);
185                         break;
186                 case 4: /* Maximize Window */
187                         /* using event B_FULL */
188                         break;
189         }
190         allqueue(REDRAWVIEW3D, 0);
191 }
192
193 static uiBlock *sound_viewmenu(void *arg_unused)
194 {
195         uiBlock *block;
196         short yco= 0, menuwidth=120;
197         
198         block= uiNewBlock(&curarea->uiblocks, "sound_viewmenu", 
199                                           UI_EMBOSSP, UI_HELV, curarea->headwin);
200         uiBlockSetButmFunc(block, do_sound_viewmenu, NULL);
201         
202         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation|Alt A", 0, yco-=20, 
203                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
204         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation in 3D View|Alt Shift A", 0, yco-=20,
205                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 2, "");
206
207         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
208         
209         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "View All|Home", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 3, "");
210                 
211         if (!curarea->full) 
212                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Maximize Window|Ctrl UpArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
213         else 
214                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Tile Window|Ctrl DownArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
215         
216         if(curarea->headertype==HEADERTOP) {
217                 uiBlockSetDirection(block, UI_DOWN);
218         }
219         else {
220                 uiBlockSetDirection(block, UI_TOP);
221                 uiBlockFlipOrder(block);
222         }
223
224         uiTextBoundsBlock(block, 50);
225         
226         return block;
227 }
228
229 void sound_buttons(void)
230 {
231         uiBlock *block;
232         short xco, xmax;
233         char naam[256];
234         char ch[20];
235         
236         sprintf(naam, "header %d", curarea->headwin);
237         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->headwin);
238
239         if(area_is_active_area(curarea)) uiBlockSetCol(block, TH_HEADER);
240         else uiBlockSetCol(block, TH_HEADERDESEL);
241         
242         curarea->butspacetype= SPACE_SOUND;
243
244         xco = 8;
245         
246         uiDefIconTextButC(block, ICONTEXTROW,B_NEWSPACE, ICON_VIEW3D, 
247                                           windowtype_pup(), xco, 0, XIC+10, YIC, 
248                                           &(curarea->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
249                                           "Displays Current Window Type. "
250                                           "Click for menu of available types.");
251
252         xco += XIC + 14;
253
254         uiBlockSetEmboss(block, UI_EMBOSSN);
255         if (curarea->flag & HEADER_NO_PULLDOWN) {
256                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
257                                           ICON_DISCLOSURE_TRI_RIGHT,
258                                           xco,2,XIC,YIC-2,
259                                           &(curarea->flag), 0, 0, 0, 0, 
260                                           "Show pulldown menus");
261         }
262         else {
263                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
264                                           ICON_DISCLOSURE_TRI_DOWN,
265                                           xco,2,XIC,YIC-2,
266                                           &(curarea->flag), 0, 0, 0, 0, 
267                                           "Hide pulldown menus");
268         }
269         uiBlockSetEmboss(block, UI_EMBOSS);
270         xco+=XIC;
271
272         if((curarea->flag & HEADER_NO_PULLDOWN)==0) {
273                 /* pull down menus */
274                 uiBlockSetEmboss(block, UI_EMBOSSP);
275         
276                 xmax= GetButStringLength("View");
277                 uiDefPulldownBut(block, sound_viewmenu, NULL, 
278                                           "View", xco, -2, xmax-3, 24, "");
279                 xco+= xmax;
280
281         }
282
283         uiBlockSetEmboss(block, UI_EMBOSS);
284         xco= std_libbuttons(block, xco+8, 0, 0, NULL, B_SOUNDBROWSE, (ID *)G.ssound->sound, 0, &(G.ssound->sndnr), 1, 0, 0, 0, 0);      
285
286         if(G.ssound->sound) {
287                 bSound *sound= G.ssound->sound;
288
289                 if (sound->sample && sound->sample->len)
290                 {
291                         if (sound->sample->channels == 1)
292                                 strcpy(ch, "Mono");
293                         else if (sound->sample->channels == 2)
294                                 strcpy(ch, "Stereo");
295                         else
296                                 strcpy(ch, "Unknown");
297                         
298                         sprintf(naam, "Sample: %s, %d bit, %d Hz, %d samples", ch, sound->sample->bits, sound->sample->rate, sound->sample->len);
299                         cpack(0x0);
300                         glRasterPos2i(xco+10, 5);
301                         BMF_DrawString(uiBlockGetCurFont(block), naam);
302                 }
303                 else
304                 {
305                         sprintf(naam, "No sample info available.");
306                         cpack(0x0);
307                         glRasterPos2i(xco+10, 5);
308                         BMF_DrawString(uiBlockGetCurFont(block), naam);
309                 }
310                 
311         }
312
313         /* always as last  */
314         curarea->headbutlen= xco+2*XIC;
315
316         uiDrawBlock(block);
317 }
318
319 /* the next two functions are also called from fileselect: */
320
321 void load_space_sound(char *str)
322 {
323         bSound *sound;
324
325         sound= sound_new_sound(str);
326         if (sound) {
327                 if (G.ssound) {
328                         G.ssound->sound= sound;
329                 }
330         } else {
331                 error("Not a valid sample: %s", str);
332         }
333
334         allqueue(REDRAWSOUND, 0);
335         allqueue(REDRAWBUTSLOGIC, 0);
336 }
337
338 void load_sound_buttons(char *str)
339 {
340         bSound *sound;
341
342         sound= sound_new_sound(str);
343         if (sound) {
344                 if (curarea && curarea->spacetype==SPACE_BUTS) {
345                         if (G.buts->mainb == CONTEXT_SCENE) {
346                                 if( G.buts->tab[CONTEXT_SCENE]==TAB_SCENE_SOUND )
347                                         G.buts->lockpoin = sound;
348                         }
349                 }
350         } 
351         else {
352                 error("Not a valid sample: %s", str);
353         }
354
355         allqueue(REDRAWBUTSSCENE, 0);
356 }