cebc9e9afd63686d982458a93c39d1e619566963
[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                 scrarea_queue_winredraw(curarea);
167                 break;
168         }
169 }
170
171 static void do_sound_viewmenu(void *arg, int event)
172 {
173         extern int play_anim(int mode);
174         
175         switch(event) {
176                 case 1: /* Play Back Animation */
177                         play_anim(0);
178                         break;
179                 case 2: /* Play Back Animation in All */
180                         play_anim(1);
181                         break;
182                 case 3: /* View All */
183                         do_sound_buttons(B_SOUNDHOME);
184                         break;
185                 case 4: /* Maximize Window */
186                         /* using event B_FULL */
187                         break;
188         }
189         allqueue(REDRAWVIEW3D, 0);
190 }
191
192 static uiBlock *sound_viewmenu(void *arg_unused)
193 {
194         uiBlock *block;
195         short yco= 0, menuwidth=120;
196         
197         block= uiNewBlock(&curarea->uiblocks, "sound_viewmenu", 
198                                           UI_EMBOSSP, UI_HELV, curarea->headwin);
199         uiBlockSetButmFunc(block, do_sound_viewmenu, NULL);
200         
201         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation|Alt A", 0, yco-=20, 
202                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 1, "");
203         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "Play Back Animation in 3D View|Alt Shift A", 0, yco-=20,
204                                          menuwidth, 19, NULL, 0.0, 0.0, 1, 2, "");
205
206         uiDefBut(block, SEPR, 0, "",        0, yco-=6, menuwidth, 6, NULL, 0.0, 0.0, 0, 0, "");
207         
208         uiDefIconTextBut(block, BUTM, 1, ICON_BLANK1, "View All|Home", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 1, 3, "");
209                 
210         if (!curarea->full) 
211                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Maximize Window|Ctrl UpArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
212         else 
213                 uiDefIconTextBut(block, BUTM, B_FULL, ICON_BLANK1, "Tile Window|Ctrl DownArrow", 0, yco-=20, menuwidth, 19, NULL, 0.0, 0.0, 0, 4, "");
214         
215         if(curarea->headertype==HEADERTOP) {
216                 uiBlockSetDirection(block, UI_DOWN);
217         }
218         else {
219                 uiBlockSetDirection(block, UI_TOP);
220                 uiBlockFlipOrder(block);
221         }
222
223         uiTextBoundsBlock(block, 50);
224         
225         return block;
226 }
227
228 void sound_buttons(void)
229 {
230         uiBlock *block;
231         short xco, xmax;
232         char naam[256];
233         char ch[20];
234         
235         sprintf(naam, "header %d", curarea->headwin);
236         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->headwin);
237
238         if(area_is_active_area(curarea)) uiBlockSetCol(block, TH_HEADER);
239         else uiBlockSetCol(block, TH_HEADERDESEL);
240         
241         curarea->butspacetype= SPACE_SOUND;
242
243         xco = 8;
244         
245         uiDefIconTextButC(block, ICONTEXTROW,B_NEWSPACE, ICON_VIEW3D, 
246                                           windowtype_pup(), xco, 0, XIC+10, YIC, 
247                                           &(curarea->butspacetype), 1.0, SPACEICONMAX, 0, 0, 
248                                           "Displays Current Window Type. "
249                                           "Click for menu of available types.");
250
251         xco += XIC + 14;
252
253         uiBlockSetEmboss(block, UI_EMBOSSN);
254         if (curarea->flag & HEADER_NO_PULLDOWN) {
255                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
256                                           ICON_DISCLOSURE_TRI_RIGHT,
257                                           xco,2,XIC,YIC-2,
258                                           &(curarea->flag), 0, 0, 0, 0, 
259                                           "Show pulldown menus");
260         }
261         else {
262                 uiDefIconButBitS(block, TOG, HEADER_NO_PULLDOWN, B_FLIPINFOMENU, 
263                                           ICON_DISCLOSURE_TRI_DOWN,
264                                           xco,2,XIC,YIC-2,
265                                           &(curarea->flag), 0, 0, 0, 0, 
266                                           "Hide pulldown menus");
267         }
268         uiBlockSetEmboss(block, UI_EMBOSS);
269         xco+=XIC;
270
271         if((curarea->flag & HEADER_NO_PULLDOWN)==0) {
272                 /* pull down menus */
273                 uiBlockSetEmboss(block, UI_EMBOSSP);
274         
275                 xmax= GetButStringLength("View");
276                 uiDefPulldownBut(block, sound_viewmenu, NULL, 
277                                           "View", xco, -2, xmax-3, 24, "");
278                 xco+= xmax;
279
280         }
281
282         uiBlockSetEmboss(block, UI_EMBOSS);
283         xco= std_libbuttons(block, xco+8, 0, 0, NULL, B_SOUNDBROWSE, (ID *)G.ssound->sound, 0, &(G.ssound->sndnr), 1, 0, 0, 0, 0);      
284
285         if(G.ssound->sound) {
286                 bSound *sound= G.ssound->sound;
287
288                 if (sound->sample && sound->sample->len)
289                 {
290                         if (sound->sample->channels == 1)
291                                 strcpy(ch, "Mono");
292                         else if (sound->sample->channels == 2)
293                                 strcpy(ch, "Stereo");
294                         else
295                                 strcpy(ch, "Unknown");
296                         
297                         sprintf(naam, "Sample: %s, %d bit, %d Hz, %d samples", ch, sound->sample->bits, sound->sample->rate, sound->sample->len);
298                         cpack(0x0);
299                         glRasterPos2i(xco+10, 5);
300                         BMF_DrawString(uiBlockGetCurFont(block), naam);
301                 }
302                 else
303                 {
304                         sprintf(naam, "No sample info available.");
305                         cpack(0x0);
306                         glRasterPos2i(xco+10, 5);
307                         BMF_DrawString(uiBlockGetCurFont(block), naam);
308                 }
309                 
310         }
311
312         /* always as last  */
313         curarea->headbutlen= xco+2*XIC;
314
315         uiDrawBlock(block);
316 }
317
318 /* the next two functions are also called from fileselect: */
319
320 void load_space_sound(char *str)
321 {
322         bSound *sound;
323
324         sound= sound_new_sound(str);
325         if (sound) {
326                 if (G.ssound) {
327                         G.ssound->sound= sound;
328                 }
329         } else {
330                 error("Not a valid sample: %s", str);
331         }
332
333         allqueue(REDRAWSOUND, 0);
334         allqueue(REDRAWBUTSLOGIC, 0);
335 }
336
337 void load_sound_buttons(char *str)
338 {
339         bSound *sound;
340
341         sound= sound_new_sound(str);
342         if (sound) {
343                 if (curarea && curarea->spacetype==SPACE_BUTS) {
344                         if (G.buts->mainb == CONTEXT_SCENE) {
345                                 if( G.buts->tab[CONTEXT_SCENE]==TAB_SCENE_SOUND )
346                                         G.buts->lockpoin = sound;
347                         }
348                 }
349         } 
350         else {
351                 error("Not a valid sample: %s", str);
352         }
353
354         allqueue(REDRAWBUTSSCENE, 0);
355 }