1 /*
2  * Copyright (C) 2002 - David W. Durham
3  *
4  * This file is part of ReZound, an audio editing application.
5  *
6  * ReZound is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License,
9  * or (at your option) any later version.
10  *
11  * ReZound is distributed in the hope that it will be useful, but
12  * 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
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
19  */
20 
21 #include "FXDiskEntityParamValue.h"
22 
23 #include <stdlib.h>
24 
25 #include <stdexcept>
26 
27 #include <istring>
28 
29 #include "CFrontendHooks.h"
30 #include "settings.h"
31 
32 #include <CNestedDataFile/CNestedDataFile.h>
33 #include "../backend/ASoundTranslator.h"
34 
35 #include "utils.h"
36 
37 /*
38 	- This is the text entry widget used over and over by ReZound on action dialogs
39 	- Its purpose is to select a constant value for a parameter to an action
40 */
41 
42 FXDEFMAP(FXDiskEntityParamValue) FXDiskEntityParamValueMap[]=
43 {
44 	//Message_Type				ID					Message_Handler
45 
46 	FXMAPFUNC(SEL_COMMAND,			FXDiskEntityParamValue::ID_ENTITYNAME_TEXTBOX,	FXDiskEntityParamValue::onEntityNameTextBoxChange),
47 	FXMAPFUNC(SEL_COMMAND,			FXDiskEntityParamValue::ID_BROWSE_BUTTON,		FXDiskEntityParamValue::onBrowseButton),
48 };
49 
FXIMPLEMENT(FXDiskEntityParamValue,FXVerticalFrame,FXDiskEntityParamValueMap,ARRAYNUMBER (FXDiskEntityParamValueMap))50 FXIMPLEMENT(FXDiskEntityParamValue,FXVerticalFrame,FXDiskEntityParamValueMap,ARRAYNUMBER(FXDiskEntityParamValueMap))
51 
52 FXDiskEntityParamValue::FXDiskEntityParamValue(FXComposite *p,int opts,const char *_name,const string initialEntityName,DiskEntityTypes _entityType) :
53 	FXVerticalFrame(p,opts|FRAME_RAISED |LAYOUT_FILL_X, 0,0,0,0, 2,2,2,2, 0,0),
54 
55 	name(_name),
56 
57 	entityType(_entityType),
58 
59 	hFrame(new FXHorizontalFrame(this,LAYOUT_FILL_X,0,0,0,0, 0,0,0,0, 2,0)),
60 		titleLabel(new FXLabel(hFrame,gettext(_name),NULL,LABEL_NORMAL|LAYOUT_CENTER_Y)),
61 		entityNameTextBox(new FXTextField(hFrame,8,this,ID_ENTITYNAME_TEXTBOX, TEXTFIELD_NORMAL | LAYOUT_CENTER_Y|LAYOUT_FILL_X)),
62 		browseButton(new FXButton(hFrame,_("&Browse"),NULL,this,ID_BROWSE_BUTTON)),
63 			// ??? if this widget is ever going to be used for anything other than sound files, then I need to conditionally show this checkButton
64 	openAsRawCheckButton(entityType==detAudioFilename ? new FXCheckButton(this,_("Open as &Raw")) : NULL),
65 
66 	textFont(getApp()->getNormalFont())
67 {
68 	if(openAsRawCheckButton && !ASoundTranslator::findRawTranslator())
69 		openAsRawCheckButton->hide();
70 
71 	// create a smaller font to use
72         FXFontDesc d;
73         textFont->getFontDesc(d);
74         d.size-=10;
75         textFont=new FXFont(getApp(),d);
76 
77 	entityNameTextBox->setText(initialEntityName.c_str());
78 
79 	if(entityType==detAudioFilename)
80 		setFontOfAllChildren(this,textFont);
81 }
82 
~FXDiskEntityParamValue()83 FXDiskEntityParamValue::~FXDiskEntityParamValue()
84 {
85 	delete textFont;
86 }
87 
onEntityNameTextBoxChange(FXObject * sender,FXSelector sel,void * ptr)88 long FXDiskEntityParamValue::onEntityNameTextBoxChange(FXObject *sender,FXSelector sel,void *ptr)
89 {
90 	// just verify something ???
91 	return 1;
92 }
93 
onBrowseButton(FXObject * sender,FXSelector sel,void * ptr)94 long FXDiskEntityParamValue::onBrowseButton(FXObject *sender,FXSelector sel,void *ptr)
95 {
96 	switch(entityType)
97 	{
98 	case detAudioFilename:
99 		{
100 			string filename;
101 			bool dummy=false;
102 			bool openAsRaw=false;
103 			if(gFrontendHooks->promptForOpenSoundFilename(filename,dummy,openAsRaw))
104 				setEntityName(filename,openAsRaw);
105 		}
106 		break;
107 
108 	case detGeneralFilename:
109 		{
110 			const string filename=FXFileDialog::getOpenFilename(this,name.c_str(),"").text();
111 			if(filename!="")
112 				setEntityName(filename);
113 		}
114 		break;
115 
116 	case detDirectory:
117 		{
118 			FXDirDialog d(this,"Select Directory");
119 			d.setDirectory(getEntityName().c_str());
120 			if(d.execute())
121 				setEntityName(d.getDirectory().text());
122 		}
123 		break;
124 
125 	default:
126 		throw runtime_error(string(__func__)+" -- internal error -- unhandled entityType: "+istring(entityType));
127 	}
128 
129 	return 1;
130 }
131 
getEntityName() const132 const string FXDiskEntityParamValue::getEntityName() const
133 {
134 	return decodeFilenamePresetParameter(entityNameTextBox->getText().text());
135 }
136 
getOpenAsRaw() const137 const bool FXDiskEntityParamValue::getOpenAsRaw() const
138 {
139 	if(openAsRawCheckButton!=NULL)
140 		return openAsRawCheckButton->getCheck();
141 	else
142 		return false;
143 }
144 
setEntityName(const string entityName,bool openAsRaw)145 void FXDiskEntityParamValue::setEntityName(const string entityName,bool openAsRaw)
146 {
147 	entityNameTextBox->setText(entityName.c_str());
148 	if(openAsRawCheckButton!=NULL)
149 		openAsRawCheckButton->setCheck(openAsRaw);
150 }
151 
getName() const152 const string FXDiskEntityParamValue::getName() const
153 {
154 	return name;
155 }
156 
setTipText(const FXString & text)157 void FXDiskEntityParamValue::setTipText(const FXString &text)
158 {
159 	titleLabel->setTipText(text);
160 	entityNameTextBox->setTipText(text);
161 }
162 
getTipText() const163 FXString FXDiskEntityParamValue::getTipText() const
164 {
165 	return titleLabel->getTipText();
166 }
167 
getEntityType() const168 const FXDiskEntityParamValue::DiskEntityTypes FXDiskEntityParamValue::getEntityType() const
169 {
170 	return entityType;
171 }
172 
readFromFile(const string & prefix,CNestedDataFile * f)173 void FXDiskEntityParamValue::readFromFile(const string &prefix,CNestedDataFile *f)
174 {
175 	const string key=prefix DOT getName();
176 	const string v=f->getValue<string>(key);
177 	setEntityName(v,f->getValue<bool>(key+" AsRaw"));
178 }
179 
writeToFile(const string & prefix,CNestedDataFile * f)180 void FXDiskEntityParamValue::writeToFile(const string &prefix,CNestedDataFile *f)
181 {
182 	const string key=prefix DOT getName();
183 	f->setValue<string>(key,encodeFilenamePresetParameter(entityNameTextBox->getText().text()));
184 
185 	if(openAsRawCheckButton!=NULL)
186 		f->setValue<bool>(key+" AsRaw",openAsRawCheckButton->getCheck());
187 }
188 
189 
190