Upload
geraldine-gray
View
217
Download
0
Embed Size (px)
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 Hill
Citation preview
Copyright © 2004-2013 - Curt Hill
Common Dialogs
Easily Obtaining File Names in DevC++ Windows Programs
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
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
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
Example from PowerPoint
Copyright © 2004-2013 - Curt Hill
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
DevC++ Design Time
Copyright © 2004-2013 - Curt Hill
After Drop
Copyright © 2004-2013 - Curt Hill
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
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
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
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
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
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
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
Executed
Copyright © 2004-2013 - Curt Hill
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
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
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
Show Filters
Copyright © 2004-2013 - Curt Hill
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