1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlg.h"
18 #include "wx/dialog.h"
19 #include "wx/filedlg.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_CLASS(wxFileDialog
, wxDialog
)
28 char *wxFileSelector(const char *title
,
29 const char *defaultDir
, const char *defaultFileName
,
30 const char *defaultExtension
, const char *filter
, int flags
,
31 wxWindow
*parent
, int x
, int y
)
33 // If there's a default extension specified but no filter, we create a suitable
37 if ( defaultExtension
&& !filter
)
38 filter2
= wxString("*.") + wxString(defaultExtension
) ;
42 wxString defaultDirString
;
44 defaultDirString
= defaultDir
;
46 defaultDirString
= "";
48 wxString defaultFilenameString
;
50 defaultFilenameString
= defaultFileName
;
52 defaultFilenameString
= "";
54 wxFileDialog
fileDialog(parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
));
56 if ( fileDialog
.ShowModal() == wxID_OK
)
58 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
65 char *wxFileSelectorEx(const char *title
,
66 const char *defaultDir
,
67 const char *defaultFileName
,
68 int* defaultFilterIndex
,
76 wxFileDialog
fileDialog(parent
, title
? title
: "", defaultDir
? defaultDir
: "",
77 defaultFileName
? defaultFileName
: "", filter
? filter
: "", flags
, wxPoint(x
, y
));
79 if ( fileDialog
.ShowModal() == wxID_OK
)
81 *defaultFilterIndex
= fileDialog
.GetFilterIndex();
82 strcpy(wxBuffer
, (const char *)fileDialog
.GetPath());
89 // TODO: Motif file selector code
91 char *wxFileSelectorAnswer
= NULL
;
92 Bool wxFileSelectorReturned
= FALSE
;
94 void wxFileSelCancel(Widget fs
, XtPointer client_data
, XmFileSelectionBoxCallbackStruct
*cbs
)
96 wxFileSelectorAnswer
= NULL
;
97 wxFileSelectorReturned
= TRUE
;
100 void wxFileSelOk(Widget fs
, XtPointer client_data
, XmFileSelectionBoxCallbackStruct
*cbs
)
102 char *filename
= NULL
;
103 if (!XmStringGetLtoR(cbs
->value
, XmSTRING_DEFAULT_CHARSET
, &filename
)) {
104 wxFileSelectorAnswer
= NULL
;
105 wxFileSelectorReturned
= TRUE
;
108 if (wxFileSelectorAnswer
) delete[] wxFileSelectorAnswer
;
109 wxFileSelectorAnswer
= copystring(filename
);
112 wxFileSelectorReturned
= TRUE
;
117 char *wxMotifFileSelector(char *message
,
118 char *default_path
, char *default_filename
,
119 char *default_extension
, char *wildcard
, int flags
,
120 wxWindow
*parent
, int x
, int y
)
123 static char fileBuf
[512];
124 Widget parentWidget
= 0;
127 if (parent
->IsKindOf(CLASSINFO(wxFrame
)))
128 parentWidget
= ((wxFrame
*)parent
)->frameShell
;
129 else if (parent
->IsKindOf(CLASSINFO(wxDialogBox
)))
130 parentWidget
= ((wxDialogBox
*)parent
)->dialogShell
;
132 parentWidget
= (Widget
)parent
->handle
;
134 else if (wxTheApp
->wx_frame
)
135 parentWidget
= wxTheApp
->wx_frame
->frameShell
;
137 Widget fileSel
= XmCreateFileSelectionDialog(parentWidget
, "file_selector", NULL
, 0);
138 XtUnmanageChild(XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_HELP_BUTTON
));
140 Widget shell
= XtParent(fileSel
);
143 XtVaSetValues(shell
, XmNtitle
, message
, NULL
);
145 char *entirePath
= NULL
;
147 if (default_path
&& default_filename
)
149 sprintf(wxBuffer
, "%s/%s", default_path
, default_filename
);
150 entirePath
= copystring(wxBuffer
);
152 else if (default_path
&& !default_filename
)
154 sprintf(wxBuffer
, "%s/", default_path
);
155 entirePath
= copystring(wxBuffer
);
157 else if ((!default_path
) && default_filename
)
159 sprintf(wxBuffer
, "%s", default_filename
);
160 entirePath
= copystring(wxBuffer
);
165 Widget selectionWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_TEXT
);
166 XmTextSetString(selectionWidget
, entirePath
);
173 sprintf(wxBuffer
, "%s/%s", default_path
, wildcard
);
175 sprintf(wxBuffer
, "%s", wildcard
);
177 Widget filterWidget
= XmFileSelectionBoxGetChild(fileSel
, XmDIALOG_FILTER_TEXT
);
178 XmTextSetString(filterWidget
, wxBuffer
);
179 XmFileSelectionDoSearch(fileSel
, NULL
);
182 // Suggested by Terry Gitnick, 16/9/97, because of change in Motif
183 // file selector on Solaris 1.5.1.
186 XmString thePath
= XmStringCreateLtoR (default_path
,
187 XmSTRING_DEFAULT_CHARSET
);
189 XtVaSetValues (fileSel
,
190 XmNdirectory
, thePath
,
193 XmStringFree(thePath
);
196 XtAddCallback(fileSel
, XmNcancelCallback
, (XtCallbackProc
)wxFileSelCancel
, (XtPointer
)NULL
);
197 XtAddCallback(fileSel
, XmNokCallback
, (XtCallbackProc
)wxFileSelOk
, (XtPointer
)NULL
);
199 //#if XmVersion > 1000
200 // I'm not sure about what you mean with XmVersion.
201 // If this is for Motif1.1/Motif1.2, then check XmVersion>=1200
202 // (Motif1.1.4 ==> XmVersion 1100 )
203 // Nevertheless, I put here a #define, so anyone can choose in (I)makefile...
205 #if !DEFAULT_FILE_SELECTOR_SIZE
206 int width
= wxFSB_WIDTH
;
207 int height
= wxFSB_HEIGHT
;
208 XtVaSetValues(fileSel
,
211 XmNresizePolicy
, XmRESIZE_NONE
,
215 XtManageChild(fileSel
);
217 if (wxFileSelectorAnswer
)
218 delete[] wxFileSelectorAnswer
;
220 wxFileSelectorAnswer
= NULL
;
221 wxFileSelectorReturned
= FALSE
;
225 XtAddGrab(XtParent(fileSel
), TRUE
, FALSE
);
227 while (!wxFileSelectorReturned
)
229 XtAppNextEvent(wxTheApp
->appContext
, &event
);
230 XtDispatchEvent(&event
);
232 XtRemoveGrab(XtParent(fileSel
));
234 XmUpdateDisplay(wxTheApp
->topLevel
); // Experimental
236 // XtDestroyWidget(fileSel);
237 XtUnmapWidget(XtParent(fileSel
));
238 XtDestroyWidget(XtParent(fileSel
));
240 // Now process all events, because otherwise
241 // this might remain on the screen
242 XSync(XtDisplay(wxTheApp
->topLevel
), FALSE
);
243 while (XtAppPending(wxTheApp
->appContext
))
245 XFlush(XtDisplay(wxTheApp
->topLevel
));
246 XtAppNextEvent(wxTheApp
->appContext
, &event
);
247 XtDispatchEvent(&event
);
250 if (wxFileSelectorAnswer
)
252 strcpy(fileBuf
, wxFileSelectorAnswer
);
259 wxFileDialog::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
260 const wxString
& defaultDir
, const wxString
& defaultFileName
, const wxString
& wildCard
,
261 long style
, const wxPoint
& pos
)
264 m_dialogStyle
= style
;
267 m_fileName
= defaultFileName
;
269 m_wildCard
= wildCard
;
273 int wxFileDialog::ShowModal()
279 // Generic file load/save dialog
281 wxDefaultFileSelector(bool load
, const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
283 char *ext
= (char *)extension
;
288 str
= "Load %s file";
290 str
= "Save %s file";
291 sprintf(prompt
, wxGetTranslation(str
), what
);
293 if (*ext
== '.') ext
++;
295 sprintf(wild
, "*.%s", ext
);
297 return wxFileSelector (prompt
, NULL
, default_name
, ext
, wild
, 0, parent
);
300 // Generic file load dialog
302 wxLoadFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
304 return wxDefaultFileSelector(TRUE
, what
, extension
, default_name
, parent
);
308 // Generic file save dialog
310 wxSaveFileSelector(const char *what
, const char *extension
, const char *default_name
, wxWindow
*parent
)
312 return wxDefaultFileSelector(FALSE
, what
, extension
, default_name
, parent
);