private void btnFromPoint_Click2(object sender, EventArgs e)
 {
     using (Selection form = new Selection("Choose Point", _ToPointNames, cboFromPoint.SelectedIndex))
     {
         if (form.ShowDialog() == DialogResult.OK)
         {
              cboFromPoint.SelectedIndex = form.selection;
         }
     }
 }
        private void btnPoly_Click2(object sender, EventArgs e)
        {
            List<string> opts = new List<string>();

            foreach(TtPolygon poly in _Polygons)
            {
                opts.Add(poly.Name);
            }

            using (Selection form = new Selection("Select Polygon", opts, cboPoly.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    cboPoly.SelectedIndex = form.selection;
                }
            }
        }
        private void btnMeta_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();

            foreach (TtMetaData m in _Meta)
            {
                items.Add(m.Name);
            }

            using (Selection form = new Selection("Choose MetaData", items, cboMeta.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    cboMeta.SelectedIndex = form.selection;
                }
            }
        }
        private void btnDOP_Click2(object sender, EventArgs e)
        {
            List<string> cboItems = new List<string>();

            foreach (object item in cboDOP.Items)
            {
                cboItems.Add(item.ToString());
            }

            using (Selection form = new Selection("Dilution of Precision", cboItems, cboDOP.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    btnDOP.Text = cboItems[form.selection].ToString();
                    cboDOP.SelectedIndex = form.selection;

                    Values.Settings.DeviceOptions.Filter_T5_DOP_TYPE = form.selection;
                }
            }
        }
 private void btnLoc_Click2(object sender, EventArgs e)
 {
     using (Selection form = new Selection("Point Location", lstLoc, lstLoc.IndexOf(loc.ToString())))
     {
         if (form.ShowDialog() == DialogResult.OK)
         {
             loc = (PointLocation)Enum.Parse(typeof(PointLocation), lstLoc[form.selection], true);
             btnLoc.Text = loc.ToString();
             cboLoc.SelectedIndex = form.selection;
         }
     }
 }
        private void btnPolygon_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();
            items = PolyNames.Keys.ToList();

            using (Selection form = new Selection("Select Polygon", items, items.IndexOf(CurrPoly.Name)))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    ChangePolygon(PolyNames[items[form.selection]]);
                }
            }
        }
        private void btnOp_Click2(object sender, EventArgs e)
        {
            FlashPoint = false;

            List<string> _ops = new List<string>();

            _ops.Add("GPS");
            _ops.Add("SideShot");
            _ops.Add("Take5");
            _ops.Add("Walk");
            _ops.Add("Traverse");
            _ops.Add("Quondam");
            _ops.Add("WayPoint");

            using (Selection f = new Selection("Operations", _ops, cbOp.SelectedIndex))
            {
                if (f.ShowDialog() == DialogResult.OK)
                {
                    cbOp.SelectedIndex = f.selection;
                    btnOp.Text = cbOp.Text;
                }
            }
        }
        private void btnCompassList_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();

            items.Add("Silva");
            items.Add("Suunto");
            items.Add("Other");

            using (Selection form = new Selection("Laser / Rangefinder", items, 0))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    Current.Compass = items[form.selection];
                    txtCompass.Text = Current.Compass;
                    _dirty = true;
                }
            }
        }
        private void btnLaserList_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();

            items.Add("TruPulse 360");
            items.Add("TruPulse 200");
            items.Add("TruPulse Other");
            items.Add("Impulse 200");
            items.Add("Impulse 100");
            items.Add("Impulse Other");
            items.Add("Logger Tape");
            items.Add("Nylon Tape");
            items.Add("Chains");
            items.Add("Other");

            using(Selection form = new Selection("Laser / Rangefinder", items, 0))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    Current.Laser = items[form.selection];
                    txtLaser.Text = Current.Laser;
                    _dirty = true;
                }
            }
        }
        private void btnPoly_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();

            foreach (TtPolygon poly in _Polygons)
            {
                items.Add(poly.Name);
            }

            using (Selection form = new Selection("Polygons", items, cboPoly.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    cboPoly.SelectedIndex = form.selection;
                    btnPoly.Text = _Polygons[form.selection].Name;
                }
            }
        }
        private void btnAutoFillGps_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>(new string[]{
               "Allegro MX",
               "Archer",
               "Archer 2",
               "Garmin 60",
               "Garmin 62",
               "Garmin 76",
               "Juno SB",
               "Juno 3B",
               "Juno 5B",
               "Mesa",
               "Trimble Geo7X",
               "Trimble Geo 6000",
               "Trimble Nomad G",
               "Trimble R1",
               "Trimble XT",
               "Trimble XM",
               "Trimble Yuma",
               "Trimble Yuma 2"
            });

            using (Selection form = new Selection("GPS Receiver", items, 0))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    Current.Receiver = items[form.selection];
                    txtGPS.Text = Current.Receiver;
                    _dirty = true;
                }
            }
        }
        private void btnStart_Click2(object sender, EventArgs e)
        {
            List<string> lstPoints = dicPointNames.Values.ToList().ConvertAll(p => p.ToString());
            lstPoints.Insert(0,"Random");

            using (Selection form = new Selection("Start Point Selection", lstPoints, cboStart.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    if (form.selection == 0)
                    {
                        CurrPoint = null;
                    }
                    else
                    {
                        CurrPoint = Points[dicPointNames.Keys.ToList()[form.selection -1]];
                    }

                    cboStart.SelectedIndex = form.selection;

                    if (CurrPoint == null)
                        btnStart.Text = "<Random>";
                    else
                        btnStart.Text = CurrPoint.PID.ToString();
                }
            }
        }
        private void btnSample_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();
            items.Add(SampleType.Percent.ToString());
            items.Add(SampleType.Points.ToString());

            using (Selection form = new Selection("Sample Selection", items, cboSample.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    if (form.selection == 0)
                    {
                        sType = SampleType.Percent;
                    }
                    else
                    {
                        sType = SampleType.Points;
                    }

                    cboSample.SelectedIndex = form.selection;
                    btnSample.Text = sType.ToString();
                }
            }
        }
        private void btnPoly_Click2(object sender, EventArgs e)
        {
            using (Selection form = new Selection("Polygon Selection", lstPoly, cboPoly.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    PolyCN = _Polys[form.selection].CN;
                    btnPoly.Text = _Polys[form.selection].Name;
                    cboPoly.SelectedIndex = form.selection;

                    ChangePoly(PolyCN);
                }
            }
        }
 private void btnToPoly_Click2(object sender, EventArgs e)
 {
     using (Selection form = new Selection("Choose Polygon", _PolyNames, cboToPoly.SelectedIndex))
     {
         if (form.ShowDialog() == DialogResult.OK)
         {
             cboToPoly.SelectedIndex = form.selection;
         }
     }
 }
        private void pointNavigationCtrl_JumpPoint2(object sender)
        {
            List<string> opts = new List<string>();
            foreach (TtMetaData p in MetaData)
            {
                opts.Add(p.Name);
            }

            using (Selection form = new Selection("Points", opts, CurrIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    ChangeMeta(MetaData[form.selection].CN);
                    pointNavigationCtrl.UpdateIndex(form.selection);
                }
            }
        }
        public bool ImportText(bool latLng, bool multiPoly, bool usePID,
            bool useIndex, bool useElev, bool elevFeet, bool useComment, bool useBound)
        {
            try
            {
                string line, tmp;

                int pidIndex = -1, xIndex = -1, yIndex = -1, zIndex = -1,
                    cmtIndex = -1, polyIndex = -1, bndIndex = -1, iIndex = -1;

                int mX = 0, mY = 0, mZ = 0;

                _Polygons = new Dictionary<string, TtPolygon>();
                _PolyIndexes = new Dictionary<string, int>();
                _IdToCN = new Dictionary<string, string>();
                _Points = new List<TtPoint>();

                int requiredFields = 2 + (multiPoly ? 1 : 0) + (usePID ? 1 : 0) +
                    (useIndex ? 1 : 0) + (useElev ? 1 : 0);

                if (!multiPoly)
                {
                    int polyCount = (int)dal.GetPolyCount();
                    polyCount++;
                    _Poly = new TtPolygon(1000 * polyCount + 10);
                    _Poly.Name = String.Format("Poly {0}", polyCount);
                    _Polygons.Add(_Poly.CN, _Poly);
                }

                using (StreamReader sr = new StreamReader(_FileName))
                {
                    if (sr.EndOfStream)
                    {
                        MessageBox.Show("Empty File");
                        return false;
                    }

                    line = sr.ReadLine();

                    #region Get Fields

                    List<string> strOrder = line.Split(',').ToList();

                    if (strOrder.Count < 2)
                    {
                        MessageBox.Show("Invalid File Info");
                        return false;
                    }
                    else
                    {

                        for (int i = 0; i < strOrder.Count; i++)
                        {
                            #region Parse Fields
                            switch (strOrder[i].ToLower())
                            {
            #if !(PocketPC || WindowsCE || Mobile)
                                    //pc only
                                case "x":
                                case "adjx":
                                case "xadj":
                                    if (!latLng)
                                    {
                                        xIndex = i;
                                        mX++;
                                    }
                                    break;
                                case "lon":
                                case "lng":
                                case "long":
                                case "longitude":
                                    if (latLng)
                                    {
                                        xIndex = i;
                                        mX++;
                                    }
                                    break;
                                case "y":
                                case "adjy":
                                case "yadj":
                                    if (!latLng)
                                    {
                                        yIndex = i;
                                        mY++;
                                    }
                                    break;
                                case "lat":
                                case "latitude":
                                    if (latLng)
                                    {
                                        yIndex = i;
                                        mY++;
                                    }
                                    break;
                                case "z":
                                case "adjz":
                                case "zadj":
                                case "elev":
                                case "elevation":
                                case "altitide":
                                    zIndex = i;
                                    mZ++;
                                    break;
                                    //pc only
            #endif
                                case "pid":
                                case "id":
                                case "point id":
                                    pidIndex = i;
                                    break;
                                case "cmt":
                                case "comment":
                                case "note":
                                case "notes":
                                    cmtIndex = i;
                                    break;
                                case "poly":
                                case "polygon":
                                case "poly name":
                                case "polygon name":
                                case "poly index":
                                case "polygon index":
                                    polyIndex = i;
                                    break;
                                case "bnd":
                                case "bound":
                                case "onbnd":
                                case "onbound":
                                case "boundary":
                                case "onboundary":
                                    bndIndex = i;
                                    break;
                                case "idx":
                                case "indx":
                                case "index":
                                    iIndex = i;
                                    break;
                                default:
                                    //unknown field
                                    break;
                            }
                            #endregion
                        }
            #if !(PocketPC || WindowsCE || Mobile)

                        using (TextImportFieldSelectionForm form =
                            new TextImportFieldSelectionForm(
                        pidIndex, xIndex, yIndex, zIndex, cmtIndex, polyIndex,
                        bndIndex, iIndex, strOrder.ToArray()))
                        {
                            if (form.ShowDialog() == DialogResult.OK)
                            {

                                pidIndex = form.iPID;
                                xIndex = form.iX;
                                yIndex = form.iY;
                                zIndex = form.iZ;
                                cmtIndex = form.iComment;
                                polyIndex = form.iPoly;
                                bndIndex = form.iBound;
                                iIndex = form.iIndex;
                                elevFeet = form.bFeet;
                                latLng = form.bLatLon;
                            }
                            else
                            {
                                return false;
                            }
                        }

            #else
                        #region Unknown / Dup Fields
                        if (xIndex < 0 || mX > 1)
                        {
                            using (Selection form = new Selection("Select X value field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    form.Width = form.Width + 100;
                                    if (form.selection > -1)
                                        xIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }

                        if (yIndex < 0 || mY > 1)
                        {
                            using (Selection form = new Selection("Select Y value field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    if (form.selection > -1)
                                        yIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }

                        if (useElev && (zIndex < 0 || mZ > 1))
                        {
                            using (Selection form = new Selection("Select Elevation field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    if (form.selection > -1)
                                        zIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }

                        if (usePID && pidIndex < 0)
                        {
                            using (Selection form = new Selection("Select Point ID field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    if (form.selection > -1)
                                        pidIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }

                        if (multiPoly && polyIndex < 0)
                        {
                            using (Selection form = new Selection("Select Poly ID field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    if (form.selection > -1)
                                        polyIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }

                        if (useIndex && iIndex < 0)
                        {
                            using (Selection form = new Selection("Select Point Index field", strOrder, 0))
                            {
                                if (form.ShowDialog() == DialogResult.OK)
                                {
                                    if (form.selection > -1)
                                        iIndex = form.selection;
                                }
                                else
                                    return false;
                            }
                        }
                        #endregion
            #endif
                    }

                    #endregion

                    int index = 0;

                    #region Parse Data
                    while (!sr.EndOfStream)
                    {
                        line = sr.ReadLine();

                        List<string> details = line.Split(',').ToList();

                        if (details.Count < requiredFields)
                            continue;

                        GpsPoint p = new GpsPoint();

                        if (multiPoly || polyIndex > -1)
                        {
                            tmp = details[polyIndex];

                            if (tmp.Length > 0)
                            {
                                if(_IdToCN.ContainsKey(tmp))
                                {
                                    _Poly = _Polygons[_IdToCN[tmp]];
                                }
                                else
                                {
                                    _Poly = new TtPolygon(1000 * (_Polygons.Count + 1) + 10);
                                    _Poly.Name = details[polyIndex];

                                    _Polygons.Add(_Poly.CN, _Poly);
                                    _IdToCN.Add(_Poly.Name, _Poly.CN);
                                    _PolyIndexes.Add(_Poly.CN, 0);
                                }
                            }
                            else
                                continue;
                        }

                        p.PolyCN = _Poly.CN;
                        p.PolyName = _Poly.Name;
                        p.MetaDefCN = _Meta.CN;

                        if (!usePID || polyIndex < 0)
                        {
                            if (_Points.Count > 0)
                            {
                                p.PID = PointNaming.NamePoint(_Points.Last(), _Poly);
                            }
                            else
                            {
                                p.PID = PointNaming.NameFirstPoint(_Poly);
                            }
                        }
                        else
                        {
                            tmp = details[pidIndex];
                            if (tmp.Length > 0 && tmp.IsInteger())
                            {
                                p.PID = Convert.ToInt32(tmp);
                            }
                            else
                            {
                                PointNaming.NamePoint(_Points.Last(), _Poly);
                            }
                        }

                        try
                        {
                            tmp = details[xIndex];
                            if (tmp.Length > 0 && tmp.IsDouble())
                            {
                                p.UnAdjX = Convert.ToDouble(tmp);
                            }
                            else
                                p.UnAdjX = 0;

                            tmp = details[yIndex];
                            if (tmp.Length > 0)
                                p.UnAdjY = Convert.ToDouble(tmp);
                            else
                                p.UnAdjY = 0;

                            if (useElev)
                            {
                                tmp = details[zIndex];
                                if (tmp.Length > 0 && tmp.IsDouble())
                                {
                                    if (elevFeet)
                                        p.UnAdjZ = TtUtils.ConvertToMeters(tmp.ToDouble(), Unit.FEET_TENTH);
                                    else
                                        p.UnAdjZ = tmp.ToDouble();
                                }
                                else
                                {
                                    p.UnAdjZ = 0;
                                }
                            }

                            if (latLng)
                            {
                                double x, y;

                                TtUtils.LatLontoUTM(p.UnAdjY, p.UnAdjX, _Meta.Zone, out y, out x);

                                p.UnAdjX = x;
                                p.UnAdjY = y;
                            }

                        }
                        catch
                        {
                            continue;
                        }

                        if (cmtIndex > -1)
                        {
                            p.Comment = details[cmtIndex];
                        }

                        if (useIndex && iIndex > -1)
                        {
                            tmp = details[iIndex];
                            if (tmp.IsInteger())
                            {
                                p.Index = tmp.ToInteger();
                                index = (int)(p.Index + 1);
                            }
                            else continue;
                        }
                        else
                        {
                            if (multiPoly || polyIndex > -1)
                            {
                                p.Index = _PolyIndexes[p.PolyCN];
                                _PolyIndexes[p.PolyCN]++;
                            }
                            else
                            {
                                p.Index = index;
                                index++;
                            }
                        }

                        if (bndIndex > -1)
                        {
                            tmp = details[bndIndex];
                            if (tmp.IsBool())
                            {
                                p.OnBnd = tmp.ToBool();
                            }
                        }
                        else
                        {
                            p.OnBnd = true;
                        }

                        p.GroupCN = Values.MainGroup.CN;
                        p.GroupName = Values.MainGroup.Name;

                        _Points.Add(p);
                    }
                    #endregion

                    foreach (TtPolygon p in _Polygons.Values)
                    {
                        dal.InsertPolygon(p);
                    }

                    dal.InsertPoints(_Points);

                    PolygonAdjuster.Adjust(dal);

                    return true;
                }
            }
            catch (Exception ex)
            {
                TtUtils.WriteError(ex.Message, "ImportFormnLogic:ParseTxt", ex.StackTrace);
                MessageBox.Show(@"An Error has occured while importing data.
            Make sure other applications are not using the file which is being imported.
            Check the error log for complete details.", "Import Error");
            }

            return false;
        }
        private void pointNavigationCtrl_JumpPoint2(object sender)
        {
            pointInfoCtrl.FlashPoint = false;

            List<string> opts = new List<string>();
            foreach (TtPoint p in _Points)
            {
                opts.Add(p.PID.ToString());
            }

            using (Selection form = new Selection("Points", opts, CurrPointIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    ChangePoint(_PointCNs[form.selection]);
                    pointNavigationCtrl.UpdateIndex(form.selection);
                    LockControls(true);
                }
            }
        }
        private void btnSwitchPoly_Click2(object sender, EventArgs e)
        {
            List<string> items = new List<string>();
            items = PolyNames.Keys.ToList();

            using (Selection form = new Selection("Select Polygon", items, items.IndexOf(CurrPoly.Name)))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    string polySwitchCN = PolyNames[items[form.selection]];
                    string polySwitchName = items[form.selection];

                    foreach (ListViewItem item in lstPoints.Items)
                    {
                        if (item.Checked)
                        {
                            string pointCN = item.SubItems[4].Text;
                            TtPoint point = Points[CurrPoly.CN][pointCN];

                            point.PolyCN = polySwitchCN;
                            point.PolyName = polySwitchName;

                            Points[polySwitchCN].Add(point.CN, point);
                            Points[CurrPoly.CN].Remove(point.CN);
                        }
                    }

                    LoadPolygon();
                    _recalc = true;
                }
            }
        }
        private void walkInfoCtrl1_AddToWalk2()
        {
            List<string> groups = new List<string>();
            List<string> gcns = new List<string>();
            //Values.GroupManager.Groups.Values.Select(group => group.Name);

            //foreach (TtGroup group in Values.GroupManager.Groups.Values)
            foreach (TtGroup group in _Groups.Values)
            {
                groups.Add(group.Name);
                gcns.Add(group.CN);
            }

            using (Selection form = new Selection("Groups", groups, 0))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    UpdatedPoint.GroupName = groups[form.selection];
                    UpdatedPoint.GroupCN = gcns[form.selection];
                    _dirty = true;
                    SavePoint();
                    ChangePolygon();
                    ChangePoint(CurrPointIndex);
                }
            }
        }
        private void btnGpsPort_Click2(object sender, EventArgs e)
        {
            List<string> cboItems = new List<string>();
            string selection = null;

            foreach (string item in cboGpsPort.Items)
            {
                cboItems.Add(item);
            }

            using (Selection form = new Selection("GPS Port", cboItems, cboGpsPort.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    selection = cboItems[form.selection].ToString();
                    getPortSelection(selection);
                }
            }
        }
        private void btnRecOpen_Click2(object sender, EventArgs e)
        {
            List<string> cboItems = new List<string>();

            foreach (RecentProject item in Values.Settings.ProjectOptions.RecOpen)
            {
                cboItems.Add(System.IO.Path.GetFileName(item.File));
            }

            using (Selection form = new Selection("Recent Files", cboItems, -1))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    if (form.selection > -1)
                    {
                        if (File.Exists(Values.Settings.ProjectOptions.RecOpen[form.selection].File))
                            OpenTtFile(Values.Settings.ProjectOptions.RecOpen[form.selection].File);
                        else
                            MessageBox.Show("Error, File does not Exist");
                    }
                }
            }
        }
        private void btnFixType_Click2(object sender, EventArgs e)
        {
            List<string> cboItems = new List<string>();

            foreach (object item in cboFixType.Items)
            {
                cboItems.Add(item.ToString());
            }

            using (Selection form = new Selection("Fix Type", cboItems, cboFixType.SelectedIndex))
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    btnFixType.Text = cboItems[form.selection].ToString();
                    cboFixType.SelectedIndex = form.selection;

                    Values.Settings.DeviceOptions.Filter_T5_FixType = form.selection;
                }
            }
        }
 private void btnDist_Click2(object sender, EventArgs e)
 {
     using (Selection form = new Selection("Measurement Type", lstDist, lstDist.IndexOf(dist.ToString())))
     {
         if (form.ShowDialog() == DialogResult.OK)
         {
             dist = (Unit)Enum.Parse(typeof(Unit), lstDist[form.selection], true);
             btnDist.Text = dist.ToString();
             cboDist.SelectedIndex = form.selection;
         }
     }
 }