Two in one:
[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 #include "BSE_time.h"
74
75 #include "blendef.h"
76 #include "mydevice.h"
77
78 void do_sound_buttons(unsigned short event)
79 {
80         ID *id, *idtest;
81         int nr;
82         char name[256];
83
84         switch(event) {
85
86         case B_SOUNDBROWSE: 
87                 if(G.ssound->sndnr== -2) {
88                         activate_databrowse((ID *)G.ssound->sound, ID_SO, 0,
89                                                                                         B_SOUNDBROWSE, &G.ssound->sndnr, do_sound_buttons);
90                         return;
91                 }
92                 if (G.ssound->sndnr < 0) break;
93                 if (G.ssound->sndnr == 32766) {
94                         if (G.ssound && G.ssound->sound) strcpy(name, G.ssound->sound->name);
95                         else strcpy(name, U.sounddir);
96                         activate_fileselect(FILE_SPECIAL, "SELECT WAV FILE", name,
97                                                                                         load_space_sound);
98                 } else {
99                         nr= 1;
100                         id= (ID *)G.ssound->sound;
101
102                         idtest= G.main->sound.first;
103                         while(idtest) {
104                                 if(nr==G.ssound->sndnr) {
105                                         break;
106                                 }
107                                 nr++;
108                                 idtest= idtest->next;
109                         }
110
111                         if(idtest==0) { /* no new */
112                                 return;
113                         }
114
115                         if(idtest!=id) {
116                                 G.ssound->sound= (bSound *)idtest;
117                                 if(idtest->us==0) idtest->us= 1;
118                                 allqueue(REDRAWSOUND, 0);
119                         }
120                 }
121
122                 break;
123         case B_SOUNDBROWSE2:    
124                 id = (ID *)G.buts->lockpoin;
125                 if(G.buts->texnr == -2) {
126                         activate_databrowse(id, ID_SO, 0, B_SOUNDBROWSE2,
127                                                                                         &G.buts->texnr, do_sound_buttons);
128                         return;
129                 }
130                 if (G.buts->texnr < 0) break;
131                 if (G.buts->texnr == 32766) {
132                         if (id) strcpy(name, ((bSound *)id)->name);
133                         else strcpy(name, U.sounddir);
134                         activate_fileselect(FILE_SPECIAL, "SELECT WAV FILE", name, load_sound_buttons);
135                 } 
136                 else {
137                         nr= 1;
138
139                         idtest= G.main->sound.first;
140                         while (idtest) {
141                                 if(nr == G.buts->texnr) {
142                                         break;
143                                 }
144                                 nr++;
145                                 idtest = idtest->next;
146                         }
147
148                         if (idtest == 0) {      /* geen new */
149                                 return;
150                         }
151
152                         if (idtest != id) {
153                                 G.buts->lockpoin = (bSound *)idtest;
154                                 if(idtest->us==0) idtest->us= 1;
155                                 allqueue(REDRAWBUTSSCENE, 0);
156                         }
157                 }
158                 break;
159
160         case B_SOUNDHOME:
161                 if(G.ssound->sound==NULL) {
162                         G.v2d->tot.xmin= G.scene->r.sfra;
163                         G.v2d->tot.xmax= G.scene->r.efra;
164                 }
165                 G.v2d->cur= G.v2d->tot;
166                 test_view2d(G.v2d, curarea->winx, curarea->winy);
167                 view2d_do_locks(curarea, V2D_LOCK_COPY);
168                 scrarea_queue_winredraw(curarea);
169                 break;
170         }
171 }
172
173 static void do_sound_viewmenu(void *arg, int event)
174 {
175         extern int play_anim(int mode);
176         
177         switch(event) {
178                 case 1: /* Play Back Animation */
179                         play_anim(0);
180                         break;
181                 case 2: /* Play Back Animation in All */
182                         play_anim(1);
183                         break;
184                 case 3: /* View All */
185                         do_sound_buttons(B_SOUNDHOME);
186                         break;
187                 case 4: /* Maximize Window */
188                         /* using event B_FULL */
189                         break;
190                 case 5: /* jump to next marker */
191                         nextprev_marker(1);
192                         break;
193                 case 6: /* jump to previous marker */
194                         nextprev_marker(-1);
195                         break;
196         }
197         allqueue(REDRAWVIEW3D, 0);
198 }
199
200 static uiBlock *sound_viewmenu(void *arg_unused)
201 {
202         uiBlock *block;
203         short yco= 0, menuwidth=120;
204         
205         block= uiNewBlock(&curarea->uiblocks, "sound_viewmenu", 
206                                           UI_EMBOSSP, UI_HELV, curarea->headwin);
207         uiBlockSetButmFunc(block, do_sound_viewmenu, NULL);
208         
209         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Jump To Next Marker|PageUp", 0, yco-=20, 
210                                          menuwidth, 19, NULL, 0.0, 0.0, 0, 5, "");
211         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Jump To Prev Marker|PageDown", 0, yco-=20, 
212                                          menuwidth, 19, NULL, 0.0, 0.0, 0, 6, "");
213         
214         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
215         
216         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation|Alt A", 0, yco-=20, 
217                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
218         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation in 3D View|Alt Shift A", 0, yco-=20,
219                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 2, "");
220
221         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
222         
223         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "View All|Home", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 3, "");
224                 
225         if (!curarea->full) 
226                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Maximize Window|Ctrl UpArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
227         else 
228                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Tile Window|Ctrl DownArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
229         
230         if(curarea->headertype==HEADERTOP) {
231                 uiBlockSetDirection(block, UI_DOWN);
232         }
233         else {
234                 uiBlockSetDirection(block, UI_TOP);
235                 uiBlockFlipOrder(block);
236         }
237
238         uiTextBoundsBlock(block, 50);
239         
240         return block;
241 }
242
243 static void do_sound_markermenu(void *arg, int event)
244 {       
245         switch(event)
246         {
247                 case 1:
248                         add_marker(CFRA);
249                         break;
250                 case 2:
251                         duplicate_marker();
252                         break;
253                 case 3:
254                         remove_marker();
255                         break;
256                 case 4:
257                         rename_marker();
258                         break;
259                 case 5:
260                         transform_markers('g', 0);
261                         break;
262         }
263         
264         allqueue(REDRAWTIME, 0);
265         allqueue(REDRAWIPO, 0);
266         allqueue(REDRAWACTION, 0);
267         allqueue(REDRAWNLA, 0);
268         allqueue(REDRAWSOUND, 0);
269 }
270
271 static uiBlock *sound_markermenu(void *arg_unused)
272 {
273         uiBlock *block;
274         short yco= 0, menuwidth=120;
275
276         block= uiNewBlock(&curarea->uiblocks, "sound_markermenu", 
277                                           UI_EMBOSSP, UI_HELV, curarea->headwin);
278         uiBlockSetButmFunc(block, do_sound_markermenu, NULL);
279
280         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Add Marker|M", 0, yco-=20, 
281                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
282         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Duplicate Marker|Shift D", 0, yco-=20, 
283                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 2, "");
284         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Delete Marker|X", 0, yco-=20,
285                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 3, "");
286                                          
287         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
288
289         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "(Re)Name Marker|Ctrl M", 0, yco-=20,
290                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 4, "");
291         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Grab/Move Marker|G", 0, yco-=20,
292                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 5, "");
293         
294         
295         if(curarea->headertype==HEADERTOP) {
296                 uiBlockSetDirection(block, UI_DOWN);
297         }
298         else {
299                 uiBlockSetDirection(block, UI_TOP);
300                 uiBlockFlipOrder(block);
301         }
302
303         uiTextBoundsBlock(block, 50);
304
305         return block;
306 }
307
308
309 void sound_buttons(void)
310 {
311         uiBlock *block;
312         short xco, xmax;
313         char naam[256];
314         char ch[20];
315         
316         sprintf(naam, "header %d", curarea->headwin);
317         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->headwin);
318
319         if(area_is_active_area(curarea)) uiBlockSetCol(block, TH_HEADER);
320         else uiBlockSetCol(block, TH_HEADERDESEL);
321         
322         curarea->butspacetype= SPACE_SOUND;
323
324         xco = 8;
325         
326         uiDefIconTextButC(block, ICONTEXTROW,B_NEWSPACE, ICON_VIEW3D, 
327                                           windowtype_pup(), xco, 0, XIC+10, YIC, 
328                                           &(curarea->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
329                                           "Displays Current Window Type. "
330                                           "Click for menu of available types.");
331
332         xco += XIC + 14;
333
334         uiBlockSetEmboss(block, UI_EMBOSSN);
335         if (curarea->flag & HEADER_NO_PULLDOWN) {
336                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
337                                           ICON_DISCLOSURE_TRI_RIGHT,
338                                           xco,2,XIC,YIC-2,
339                                           &(curarea->flag), 0, 0, 0, 0, 
340                                           "Show pulldown menus");
341         }
342         else {
343                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
344                                           ICON_DISCLOSURE_TRI_DOWN,
345                                           xco,2,XIC,YIC-2,
346                                           &(curarea->flag), 0, 0, 0, 0, 
347                                           "Hide pulldown menus");
348         }
349         uiBlockSetEmboss(block, UI_EMBOSS);
350         xco+=XIC;
351
352         if((curarea->flag & HEADER_NO_PULLDOWN)==0) {
353                 /* pull down menus */
354                 uiBlockSetEmboss(block, UI_EMBOSSP);
355         
356                 xmax= GetButStringLength("View");
357                 uiDefPulldownBut(block, sound_viewmenu, NULL, 
358                                           "View", xco, -2, xmax-3, 24, "");
359                 xco+= xmax;
360
361                 xmax= GetButStringLength("Marker");
362                 uiDefPulldownBut(block, sound_markermenu, NULL, 
363                                           "Marker", xco, -2, xmax-3, 24, "");
364                 xco+= xmax;
365         }
366
367         uiBlockSetEmboss(block, UI_EMBOSS);
368         xco= std_libbuttons(block, xco+8, 0, 0, NULL, B_SOUNDBROWSE, ID_SO, 0, (ID *)G.ssound->sound, 0, &(G.ssound->sndnr), 1, 0, 0, 0, 0);    
369
370         if(G.ssound->sound) {
371                 bSound *sound= G.ssound->sound;
372
373                 if (sound->sample && sound->sample->len)
374                 {
375                         if (sound->sample->channels == 1)
376                                 strcpy(ch, "Mono");
377                         else if (sound->sample->channels == 2)
378                                 strcpy(ch, "Stereo");
379                         else
380                                 strcpy(ch, "Unknown");
381                         
382                         sprintf(naam, "Sample: %s, %d bit, %d Hz, %d samples", ch, sound->sample->bits, sound->sample->rate, sound->sample->len);
383                         cpack(0x0);
384                         glRasterPos2i(xco+10, 5);
385                         BMF_DrawString(uiBlockGetCurFont(block), naam);
386                 }
387                 else
388                 {
389                         sprintf(naam, "No sample info available.");
390                         cpack(0x0);
391                         glRasterPos2i(xco+10, 5);
392                         BMF_DrawString(uiBlockGetCurFont(block), naam);
393                 }
394                 
395         }
396
397         /* always as last  */
398         curarea->headbutlen= xco+2*XIC;
399
400         uiDrawBlock(block);
401 }
402
403 /* the next two functions are also called from fileselect: */
404
405 void load_space_sound(char *str)
406 {
407         bSound *sound;
408
409         sound= sound_new_sound(str);
410         if (sound) {
411                 if (G.ssound) {
412                         G.ssound->sound= sound;
413                 }
414         } else {
415                 error("Not a valid sample: %s", str);
416         }
417
418         allqueue(REDRAWSOUND, 0);
419         allqueue(REDRAWBUTSLOGIC, 0);
420 }
421
422 void load_sound_buttons(char *str)
423 {
424         bSound *sound;
425
426         sound= sound_new_sound(str);
427         if (sound) {
428                 if (curarea && curarea->spacetype==SPACE_BUTS) {
429                         if (G.buts->mainb == CONTEXT_SCENE) {
430                                 if( G.buts->tab[CONTEXT_SCENE]==TAB_SCENE_SOUND )
431                                         G.buts->lockpoin = sound;
432                         }
433                 }
434         } 
435         else {
436                 error("Not a valid sample: %s", str);
437         }
438
439         allqueue(REDRAWBUTSSCENE, 0);
440 }