PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CFileDialog.ahk

http://github.com/Skiouros/Macro
AutoHotKey | 92 lines | 33 code | 10 blank | 49 comment | 3 complexity | 16dc14f7d1335b9186999a6c9515231a MD5 | raw file
 1/*
 2Class: CFileDialog
 3This class is used for open/save file dialogs.
 4*/
 5Class CFileDialog
 6{
 7	/*
 8	Variable: Mode
 9	Possible values:
10	- Open: Shows a dialog to select a file which is opened.
11	- Save: Shows a dialog to select a file which is saved.
12	*/
13	;Variable: Multi
14	;If true, multiple files can be selected.
15	
16	;Variable: FileMustExist
17	;If true, the dialog requires that the file exists. Often used to open files.
18	
19	;Variable: PathMustExist
20	;If true, the dialog requires that the path exists.
21	
22	;Variable: CreateNewFilePrompt
23	;If true, the dialog will ask the user to create a new file if it does not exist.
24	
25	;Variable: OverwriteFilePrompt
26	;If true, the dialog asks the user to overwrite an existing file.
27	
28	;Variable: FollowShortcuts
29	;If true(default), shortcuts are followed. Otherwise the shortcut file itself will be used.
30	
31	;Variable: InitialDirectory
32	;Initial directory of the dialog.
33	
34	;Variable: Filename
35	;Initial filename in the filename edit field.
36	
37	;Variable: Title
38	;Title of the dialog window.
39	
40	;Variable: Filter
41	;File extension filter. AHK only supports a single user defined entry, but it may have different file extensions. An example would be "Audio files (*.mp3;*.wav)".
42	
43	/*
44	Constructor: __New
45	Creates the instance but does not show the dialog. It can be used to store a configuration of the dialog that can be reused.
46	
47	Parameters:
48		Mode - Possible values:
49				- Open: Shows a dialog to select a file which is opened.
50				- Save: Shows a dialog to select a file which is saved.
51	
52	Returns:
53	An instance of the <CFileDialog> class.
54	*/
55	__New(Mode="")
56	{
57		this.Mode := Mode ? Mode : "Open"
58		this.Multi := 0
59		this.FileMustExist := 0
60		this.PathMustExist := 0
61		this.CreateNewFilePrompt := 0
62		this.OverwriteFilePrompt := 0
63		this.FollowShortcuts := 1
64		this.InitialDirectory := ""
65		this.Filename := ""
66		this.Title := ""
67		this.Filter := ""
68	}
69	__Delete()
70	{
71	}
72	/*
73	Function: Show
74	Shows a file dialog window. To show a modal window, set OwnDialogs := 1 for the GUI thread that calls this.
75	
76	Returns:
77	1 if the user confirmed the selection and pressed OK, 0 if the file selection was cancelled.
78	*/
79	Show()
80	{
81		FileSelectFile, result, % (this.Multi ? "M" : "" ) (this.Mode = "Open" ? "" : "S") ((this.FileMustExist > 0) + (this.PathMustExist>0) * 2 + (this.CreateNewFilePrompt > 0) * 8 + (this.OverwriteFilePrompt > 0) * 16 + (this.FollowShortcuts = 0) * 32), % this.InitialDirectory (this.InitialDirectory && this.Filename ? "\" : "") this.Filename, % this.Title, % this.Filter
82		if(Multi)
83		{
84			this.Filenames := Array()
85			Loop, Parse, result, `n
86				this.Filenames.Insert(A_LoopField)
87		}
88		else
89			this.Filename := result
90		return result != ""
91	}
92}