/*

CLASS {AL4_file_dialog|s_csharp}, NATIVE, UNTESTED :

IMPORT, ANYWAY: {Control}, {Dialog_file} .

NATIVE: "using System.Windows.Forms;" .

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using System.Runtime.InteropServices;

using AL4;
using AL4_project;

namespace AL4
{

    public partial class AL4Form : Form
    {

        public class PanelDialogFile : Panel
        {
            public FileDialog DialogObject;
        }

        /*
        // Create file dialog, either save dialog or open dialog.
        //---------------------------------------------------------------------
        
        FUN Create_file_dialog({Control} D|ialog_control,
            BOOL Save|_dialog) ==> INT
            :
            ----------------------------------- 'File open/save dialogue create'
            RESULT = do_create_file_dialog(D.Form_.Handle, D, Save) .

        FUN do_create_file_dialog(INT H|andle_column,
            {Control} D|ialog_control, BOOL Save|_dialog) ==> INT, 
            NATIVE :
            "     return AL4Form.Do_Create_dialog_file((int)X_H, X_D, X_Save);"
            .
        */
        public static int Do_Create_dialog_file(int H, Ref_obj X_D, bool X_Save)
        {
            AL4Column P = AL4Form.Get_parent_for_new_control(H);
            PanelDialogFile D = new PanelDialogFile();
            int Result = AL4Form.AllocDescriptor(D);
            D.Tag = X_D;
            D.Parent = P;
            D.Visible = false;
            D.Enabled = false;
            D.Size = new Size(0, 0);
            if (X_Save)
                D.DialogObject = new SaveFileDialog();
            else {
            	D.DialogObject = new OpenFileDialog();
            }
            return Result;
        }

        /*
        // Show file dialog, return TRUE if  file was selected.
        //-----------------------------------------------------
        FUN Show_file_dialog({Control} D|ialog_control) ==> BOOL, NATIVE:
			INT d|ialog_control_handle = D.Handle
			NATIVE @
            "    RESULT = AL4Form.Do_Show_file_dialog((int)X_d);" .
        */
        public static bool Do_Show_file_dialog(int X_D)
        {
            PanelDialogFile DP = (PanelDialogFile)AL4Form.All_descriptors[
            	(int)X_D];
            Ref_obj DRef = (Ref_obj)DP.Tag;
            C_Dialog_file CD = (C_Dialog_file)DRef.Obj;
            OpenFileDialog OD = null;
            SaveFileDialog SD = null;
            if (DP.DialogObject is OpenFileDialog)
            {
                OD = (OpenFileDialog)DP.DialogObject;
                OD.Title = CD.F_Title;
                OD.InitialDirectory = CD.F_Initial_path;
                OD.RestoreDirectory = OD.InitialDirectory != "";
                OD.DefaultExt = CD.F_Default_ext;
                try {
                	OD.Filter = CD.F_Filter;
                } catch(Exception){}
                OD.FileName = CD.F_Filename;
                OD.Multiselect = CD.F_Multiselect;
            }
            else
            {
                SD = (SaveFileDialog)DP.DialogObject;
                try {
	                SD.Title = CD.F_Title;
	                SD.InitialDirectory = CD.F_Initial_path;
	                SD.RestoreDirectory = SD.InitialDirectory != "";
	                SD.DefaultExt = CD.F_Default_ext;
	                SD.FileName = CD.F_Filename;
	                try {
	                	SD.Filter = CD.F_Filter;
	                } catch(Exception){}
                }
                catch(Exception) {}
            }
            bool Result = DP.DialogObject.ShowDialog() == DialogResult.OK;
            if (Result && OD != null) {
            	CD.F_Filename = "";
            	foreach (String f in OD.FileNames)
            	{
            		if (CD.F_Filename != "")
            			CD.F_Filename += ";";
                	CD.F_Filename += f;
            	} 
            }
            else if (OD != null) CD.F_Filename = OD.FileName;
            else if (SD != null) CD.F_Filename = SD.FileName;
            return Result;
        }
        
    }

}

/*
END
*/
