copyright © 2004-2013 - curt hill common dialogs easily obtaining file names in devc++ windows...
DESCRIPTION
Common File Dialog Box What is it? –This is the Windows standard way to select a file name –There are two flavors: open existing or create new one Where: Dialogs tab There are two of importance today: –wxOpenFileDialog and wxSaveFileDialog Neither will open a file –But they will obtain a file name Copyright © Curt HillTRANSCRIPT
![Page 1: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/1.jpg)
Copyright © 2004-2013 - Curt Hill
Common Dialogs
Easily Obtaining File Names in DevC++ Windows Programs
![Page 2: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/2.jpg)
Programs that Use Files• Some programs use exactly the
same file each time– Usually an initialization file in the
same place every time• Most programs can use any file of
the proper format• Typing the file name into a text
box is awkward and error prone• Let Windows show us where it is
with a common dialog boxCopyright © 2004-2013 - Curt Hill
![Page 3: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/3.jpg)
Common File Dialog Box• What is it?
– This is the Windows standard way to select a file name
– There are two flavors: open existing or create new one
• Where: Dialogs tab• There are two of importance today:
– wxOpenFileDialog and wxSaveFileDialog
• Neither will open a file– But they will obtain a file name
Copyright © 2004-2013 - Curt Hill
![Page 4: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/4.jpg)
What you can do• A common dialog box allows the
user to avoid typing and• Change directory or disk• Choose a file• Change the filter• Accept the chosen file or cancel
Copyright © 2004-2013 - Curt Hill
![Page 5: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/5.jpg)
Example from PowerPoint
Copyright © 2004-2013 - Curt Hill
![Page 6: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/6.jpg)
General• Unlike most components they are
non-visual• They can be seen on the form
during design time• Invisible at execution time• They are not seen until executed
Copyright © 2004-2013 - Curt Hill
![Page 7: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/7.jpg)
DevC++ Design Time
Copyright © 2004-2013 - Curt Hill
![Page 8: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/8.jpg)
After Drop
Copyright © 2004-2013 - Curt Hill
![Page 9: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/9.jpg)
Using• Once put on form we still have to
use it• General process is:• Display box• Determine if open or cancel was
done• If Open get the file name • Convert this file to a C style string
pass it to the file
Copyright © 2004-2013 - Curt Hill
![Page 10: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/10.jpg)
Modal Dialog Boxes• In Windows there are two styles of
dialog box:– Modal– Non-modal
• A non-modal may be displayed, but it does not interfere with the parent window
• A Modal dialog box prevents any input to the parent– It must be dismissed before the parent
is usableCopyright © 2004-2013 - Curt Hill
![Page 11: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/11.jpg)
ShowModal Method• The ShowModal method takes no
parameters and shows the dialog• ShowModal returns an integer that
tells what happened• If this is equal to wxID_OK then the
user approved the file name– Otherwise they cancelled– All file processing should be done only
if a wxID_OK was returned
Copyright © 2004-2013 - Curt Hill
![Page 12: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/12.jpg)
Design Properties• Message – The contents of the title
bar when Common dialog is displayed
• Name – The name of the component– This will always be referenced from
code• Most other things need to be
referenced using methods
Copyright © 2004-2013 - Curt Hill
![Page 13: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/13.jpg)
GetPath Method• This is used to obtain the filename
– It also contains the entire directory path– GetFilename only gives the file name
without the path• It returns a wxString• ifstreams and ofstreams need a C
style string• This needs to be converted in order
to pass to a constructor or open method
• How?Copyright © 2004-2013 - Curt Hill
![Page 14: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/14.jpg)
String Conversion• This is relatively easy• Use the wxString method: c_str()
– ifstream inf( OpenDialog1-> GetPath ().c_str());
• You may do in two steps:– wxString name = OpenDialog->GetPath();
– ifstream inf(name.c_str());
Copyright © 2004-2013 - Curt Hill
![Page 15: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/15.jpg)
Sample Code• Suppose that OpenDial is the object• The following code will open a file: int ret = OpenDialog->ShowModal();if(ret == wxID_OK){
• ifstream inf( OpenDial->GetPath() .c_str()); while(inf) { … } } // end of if
Copyright © 2004-2013 - Curt Hill
![Page 16: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/16.jpg)
Executed
Copyright © 2004-2013 - Curt Hill
![Page 17: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/17.jpg)
Filters• By default a common dialog box
will show every file in the current directory– The directory may be changed
• Often we are not interested in every kind of file– A filter allows us to only observe the
files of interest• DevC++ does not allow the filter
to be set at design time– Done with SetWildcard method
Copyright © 2004-2013 - Curt Hill
![Page 18: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/18.jpg)
Filter Pairs• Each pair consists of two pieces
– A name to be displayed– An expression that describes what to
show• The two are separated by a
vertical bar• The expression may include ? or *
– ? any one character– * any number of any characters
Copyright © 2004-2013 - Curt Hill
![Page 19: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/19.jpg)
Format• Thus we set:• OpenDialog1->SetWildcard( "Data|*.dat|All|*.* ");
• The odd entries are display• The even entries are the actual filters• See next screen for image
Copyright © 2004-2013 - Curt Hill
![Page 20: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/20.jpg)
Show Filters
Copyright © 2004-2013 - Curt Hill
![Page 21: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs](https://reader036.vdocuments.us/reader036/viewer/2022062911/5a4d1bdd7f8b9ab0599ddb26/html5/thumbnails/21.jpg)
Last Thoughts• There are many other options
– Most of these we do not need• The FileName is persistent• It will show as the last one used in
subsequent executions
Copyright © 2004-2013 - Curt Hill