private void btnStartMR_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (btnStartMR.Checked)
                {
                    btnStartMR.BackColor = Color.LimeGreen;
                    txtChannelClear(0); // all

                    if (Audio.SDRmode)
                    {
                        if (ROBOT)
                        {
                            rtbCH1.Select(rtbCH1.Text.Length, 0);
                            rtbCH1.SelectionColor = Color.LawnGreen;
                            detect_call = false;
                            detect_loc = false;
                            detect_name = false;
                            detect_qth = false;
                            detect_rst = false;
                            detect_info = false;
                            detection = false;
                        }

                        G59Init();
                        CurrentBand = current_band;
                        Audio.callback_return = 0;
                        tx_pwr = tbG59PWR.Value;
                        OutputPowerUpdate();

                        if (cwDecoder == null)
                            cwDecoder = new CWDecode(this);

                        if (cwDecoder.AudioEvent1 == null)
                            cwDecoder.AudioEvent1 = new AutoResetEvent(false);

                        SetRXOn(0, 2, false);
                        SetRXOn(0, 3, false);
                        cwDecoder.CWdecodeStart();

                        if (rtty == null)
                            rtty = new RTTY(this);

                        if (rtty.AudioEventRX1 == null)
                            rtty.AudioEventRX1 = new AutoResetEvent(false);

                        rtty.RTTYStart();

                        if (op_mode_vfoA == Mode.RTTY)
                            SetRXOn(0, 1, true);

                        if (psk == null)
                            psk = new PSK(this);

                        if (psk.AudioEvent1 == null)
                            psk.AudioEvent1 = new AutoResetEvent(false);

                        psk.PSKStart();
                        VFOA = vfoa;
                        VFOB = vfob;
                        tbFilterWidth_Scroll(this, EventArgs.Empty);
                        tbSQL_Scroll(this, EventArgs.Empty);
                        lblPan_Click(this, EventArgs.Empty);
                        btnRX2On_CheckedChanged(this, EventArgs.Empty);

                        if (!btnMute.Checked)
                            Audio.Volume = tbAFGain.Value;

                        Audio.ScopeLevel = tbAFGain.Value * 5;
                        SetTRX(0, false);   // RX

                        if (!Audio.Start())
                        {
                            btnStartMR.Checked = false;
                            return;
                        }

                        SetAGC(agc_mode);
                        tbRFGain_Scroll(this, EventArgs.Empty);
                        NBvals = nb_vals;
                        SetNB(0, 0, btnNB.Checked);
                        SetNB(0, 1, btnNB.Checked);
                        VFOA = vfoa;    // refresh
                        VFOB = vfob;
                        btnStartMR.Text = "Stop";

                        if (Audio.SDRmode)
                        {
                            txtFilterWidth.Visible = true;
                            lblFilterwidth.Visible = true;
                            txtFilterWidth.Text = tbFilterWidth.Value.ToString() + "Hz";
                        }
                        else
                        {
                            txtFilterWidth.Visible = false;
                            tbFilterWidth.Visible = false;
                            lblFilterwidth.Visible = false;
                        }

                        runDisplay = true;
                        display_thread = new Thread(new ThreadStart(RunDisplay));
                        display_thread.Name = "Display Thread";
                        display_thread.Priority = ThreadPriority.Normal;
                        display_thread.IsBackground = true;
                        display_thread.Start();

                        if (Audio.SDRmode)
                        {
                            Smeter_thread = new Thread(new ThreadStart(Smeter_thread_function));
                            Smeter_thread.Name = "Smeter Thread";
                            Smeter_thread.Priority = ThreadPriority.Normal;
                            Smeter_thread.IsBackground = true;
                            Smeter_thread.Start();

                            /*wbir_run = true;

                            if (wbir_thread == null || !wbir_thread.IsAlive)
                            {
                                wbir_thread = new Thread(new ThreadStart(WBIR_thread));
                                wbir_thread.Name = "WBIR Thread";
                                wbir_thread.Priority = ThreadPriority.Normal;
                                wbir_thread.IsBackground = true;
                            }

                            WBIR_state = WBIR_State.DelayAdapt;
                            wbir_tuned = true;
                            wbir_thread.Start();*/
                        }
                    }
                    else
                    {
                        if (!mrIsRunning)
                        {
                            mrIsRunning = true;

                            if (!btnMute.Checked)
                                Audio.Volume = tbVolume.Value * 10;

                            Audio.ScopeLevel = tbVolume.Value;
                            Audio.InputLevel = (double)tbInputLevel.Value; ;
                            Audio.callback_return = 0;

                            if (cwDecoder == null)
                                cwDecoder = new CWDecode(this);

                            if (cwDecoder.AudioEvent1 == null)
                                cwDecoder.AudioEvent1 = new AutoResetEvent(false);

                            cwDecoder.SQL = (double)(tbMRSquelch.Value / 20000.0);

                            EnsureMRWindow();

                            if (topWindow == 0)
                            {
                                btnStartMR.Checked = false;
                                return;
                            }

                            cwDecoder.CWdecodeStart();

                            Audio.Start();

                            int runButton = 0;
                            int panel = 0;
                            int subPanel = 0;

                            while (true)
                            {
                                panel = msg.getWindowIdEx(topWindow, panel, "TPanel", null);
                                if (panel == 0)
                                    break;
                                // Test if it has panel with subpanel with empty caption
                                subPanel = msg.getWindowIdEx(panel, 0, "TPanel", "");
                                if (subPanel == 0)
                                    break;

                                // Test if that sub-panel has toolbar
                                runButton = msg.getWindowIdEx(subPanel, 0, "TToolBar", null);
                                if (runButton == 0)
                                    break;
                            }

                            if (runButton == 0)
                            {
                                btnStartMR.Checked = false;
                                return;
                            }
                            else
                            {
                                msg.sendWindowsMessage(runButton, WM_LBUTTONDOWN, 0, (10 << 16) + 10);
                                msg.sendWindowsMessage(runButton, WM_LBUTTONUP, 0, (10 << 16) + 10);
                                udLOGMyNR.Value = 1;
                                mrIsRunning = true;
                                btnStartMR.Text = "Stop";
                                runDisplay = true;
                                display_thread = new Thread(new ThreadStart(RunDisplay));
                                display_thread.Name = "Display Thread";
                                display_thread.Priority = ThreadPriority.Normal;
                                display_thread.IsBackground = true;
                                display_thread.Start();
                            }
                        }
                    }
                }
                else
                {
                    btnStartMR.BackColor = Color.WhiteSmoke;

                    if (Audio.SDRmode)
                    {
                        MOX = false;
                        TUNE = false;
                        wbir_run = false;
                        Thread.Sleep(100);
                        runDisplay = false;

                        switch (op_mode_vfoA)
                        {
                            case Mode.CW:
                                cwDecoder.CWdecodeStop();
                                break;

                            case Mode.RTTY:
                                rtty.RTTYStop();
                                break;

                            case Mode.BPSK31:
                            case Mode.BPSK63:
                            case Mode.BPSK125:
                            case Mode.BPSK250:
                            case Mode.QPSK31:
                            case Mode.QPSK63:
                            case Mode.QPSK125:
                            case Mode.QPSK250:
                                psk.PSKStop();
                                break;
                        }

                        Thread.Sleep(100);
                        Audio.callback_return = 2;
                        Thread.Sleep(100);
                        Audio.StopAudio();
                        Thread.Sleep(100);
                        btnStartMR.Text = "Start";
                    }
                    else
                    {
                        if (mrIsRunning)
                        {
                            runDisplay = false;
                            mrIsRunning = false;
                            Audio.callback_return = 2;
                            Thread.Sleep(100);
                            Audio.StopAudio();
                            Thread.Sleep(100);
                            cwDecoder.CWdecodeStop();
                            Thread.Sleep(100);
                            cwDecoder.CWDecodeClose();
                            cwDecoder = null;

                            EnsureMRWindow();

                            if (topWindow == 0)
                                return;

                            int runButton = 0;
                            int panel = 0;
                            int subPanel = 0;

                            while (true)
                            {
                                panel = msg.getWindowIdEx(topWindow, panel, "TPanel", null);
                                if (panel == 0)
                                    break;
                                // Test if it has panel with subpanel with empty caption
                                subPanel = msg.getWindowIdEx(panel, 0, "TPanel", "");
                                if (subPanel == 0)
                                    break;

                                // Test if that sub-panel has toolbar
                                runButton = msg.getWindowIdEx(subPanel, 0, "TToolBar", null);
                                if (runButton == 0)
                                    break;
                            }

                            if (runButton == 0)
                                return;
                            else
                            {
                                msg.sendWindowsMessage(runButton, WM_LBUTTONDOWN, 0, (10 << 16) + 10);
                                msg.sendWindowsMessage(runButton, WM_LBUTTONUP, 0, (10 << 16) + 10);
                                mrIsRunning = false;
                                btnStartMR.Text = "Start";
                            }
                        }
                    }
                }

                if (btnStartMR.Checked)
                    pwr = true;
                else
                    pwr = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Morse Runner not running?\n" + ex.ToString());
                btnStartMR.Text = "Start";
            }
        }
        public CWExpert()
        {
            booting = true;
            InitializeComponent();
            float dpi = this.CreateGraphics().DpiX;
            this.AutoScaleMode = AutoScaleMode.Dpi;
            SizeF d = this.AutoScaleDimensions;
            d.Height = dpi;
            d.Width = dpi;
            this.AutoScaleDimensions = d;
            float ratio = dpi / 96.0f;
            string font_name = this.Font.Name;
            float size = (float)(8.25 / ratio);
            System.Drawing.Font new_font = new System.Drawing.Font(font_name, size);
            this.Font = new_font;
            font_name = txtVFOA.Font.Name;
            size = 14.25f / ratio;
            new_font = new System.Drawing.Font(font_name, size);
            txtVFOA.Font = new_font;
            size = 9.75f / ratio;
            new_font = new System.Drawing.Font(font_name, size);
            txtVFOB.Font = new_font;
            txtLosc.Font = new_font;
            font_name = this.menuStrip1.Font.Name;
            this.menuStrip1.Font = new System.Drawing.Font(font_name, size);
            this.menuStrip1.Size = new Size(this.menuStrip1.Width, (int)(25 / ratio));
            this.PerformAutoScale();
            this.PerformLayout();

            SetStyle(ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();
            this.Text = programVersion;
            tx_image_phase_table = new float[(int)Band.LAST];
            tx_image_gain_table = new float[(int)Band.LAST];
            rx_image_phase_table = new float[(int)Band.LAST];
            rx_image_gain_table = new float[(int)Band.LAST];
            msg = new MessageHelper();
            edits = new TextEdit[3];
            DB.AppDataPath = Application.StartupPath;
            DB.Init();
            Audio.MainForm = this;
            PA19.PA_Initialize();
            #if DirectX
            DX.MainForm = this;
            #endif
            Display_GDI.MainForm = this;
            SetupForm = new Setup(this);
            booting = false;
            DXClusterForm = new DXClusterClient(this, SetupForm.txtTelnetHostAddress.Text.ToString(),
                SetupForm.txtStnCALL.Text.ToString(), SetupForm.txtStnName.Text.ToString(),
                SetupForm.txtStnQTH.Text.ToString());
            SetupForm.chkIPV6_CheckedChanged(this, EventArgs.Empty);
            display_event = new AutoResetEvent(false);
            output_ring_buf = new RingBuffer(32768);
            mon_ring_buf = new RingBuffer(32768);
            tbFilterWidth.Visible = false;
            picMonitor_bmp = new Bitmap(picMonitor.Width, picMonitor.Height,
                System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            #if(DirectX)
            if (VideoDriver == DisplayDriver.DIRECTX)
            {
                DirectXInit();
            }
            else
            #endif
            {
                SMeter.displayEngine = AnalogGAuge.AGauge.DisplayDriver.GDI;
                Display_GDI.Target = picPanadapter;
                Display_GDI.Init(this);

                if (File.Exists(".\\picDisplay.png"))
                {
                    picPanadapter.BackgroundImage = Image.FromFile(".\\picDisplay.png");
                    picWaterfall.BackgroundImage = Image.FromFile(".\\picDisplay.png");
                }
            }

            display_buffer = new float[4096];
            AlwaysOnTop = always_on_top;
            txtFilterWidth.Visible = false;
            tbFilterWidth.Visible = false;
            lblFilterwidth.Visible = false;
            menuStrip1.BackColor = Color.Black;
            menuStrip1.ForeColor = Color.White;
            tbAFGain_Scroll(this, EventArgs.Empty);
            genesis = new G59(this.Handle);
            btnCH1.BackColor = Color.LimeGreen;
            btnCH2.BackColor = Color.WhiteSmoke;

            if (Audio.SDRmode)
            {
                genesis.booting = false;
                G59Init();
                genesis.si570_i2c_address = 170;
                genesis.si570_fxtal = 114272200.0;
                genesis.HSDiv = 11;
                bool conn = genesis.Connected;

                if (conn)
                {
                    lblUSB.BackColor = Color.Green;
                }
                else
                {
                    lblUSB.BackColor = Color.Red;
                }
            }

            cwDecoder = new CWDecode(this);
            cwEncoder = new CWEncode(this);
            rtty = new RTTY(this);
            psk = new PSK(this);
            cwEncoder.CWSpedd = (int)SetupForm.udCWSpeed.Value;
            cwDecoder.rx_only = SetupForm.chkRXOnly.Checked;
            cwEncoder.TXPhase = (float)SetupForm.udTXPhase.Value;
            cwEncoder.TXGain = (float)SetupForm.udTXGain.Value;
            cwEncoder.TXIfShift = (float)SetupForm.udTXIfShift.Value;
            cwEncoder.TXOffDelay = (int)SetupForm.udTXOffDelay.Value;
            cwEncoder.SetKeyerSpeed((float)SetupForm.udCWSpeed.Value);
            cwEncoder.SetKeyerWeight((int)SetupForm.udCWWeight.Value);
            cwEncoder.SetKeyerIambic(SetupForm.chkG59Iambic.Checked);
            chkAFC_CheckedChanged(this, EventArgs.Empty);
            psk.TXPreamble = psk_preamble;

            if (SetupForm.chkG59IambicBmode.Checked)
                cwEncoder.SetKeyerMode(1);
            else
                cwEncoder.SetKeyerMode(0);

            rtty.TXPhase = (float)SetupForm.udTXPhase.Value;
            rtty.TXGain = (float)SetupForm.udTXGain.Value;

            if (!once)
            {
                SetupSDR(Application.StartupPath.ToString() + "\\wisdom");
                ReleaseUpdate();
                SetSampleRate(Audio.SampleRate);
                ResizeSDR(0, 4096);
                SetAGC(agc_mode);

                ProcessSampleThreadController[] pstc = new ProcessSampleThreadController[1];
                audio_process_thread = new Thread[1];

                for (uint proc_thread = 0; proc_thread < 1; proc_thread++)
                {
                    pstc[proc_thread] = new ProcessSampleThreadController(proc_thread);
                    audio_process_thread[proc_thread] = new Thread(new ThreadStart(pstc[proc_thread].ProcessSampleThread));
                    audio_process_thread[proc_thread].Name = "Audio Process Thread " + proc_thread.ToString();
                    audio_process_thread[proc_thread].Priority = ThreadPriority.Highest;
                    audio_process_thread[proc_thread].IsBackground = true;
                    audio_process_thread[proc_thread].Start();
                }

                once = true;
            }

            get_state = true;
            GetState();
            get_state = false;
            DB.LOGFilePath = log_file_path;
            DB.LOG_Init();
            InitLOG();
            keyboard = new Keyboard(this);
            recorder = new Recorder(this);

            double vfoa_freq, vfob_freq, losc_freq;
            int filter_vfoA, filter_vfoB, zoom, pan;
            DB.GetBandStack(current_band.ToString(), out vfoa_freq, out vfob_freq, out losc_freq, out filter_vfoA,
                out filter_vfoB, out zoom, out pan);
            LOSC = losc_freq;
            VFOA = vfoa_freq;
            VFOB = vfob_freq;
            FilterWidthVFOA = filter_vfoA;
            FilterWidthVFOB = filter_vfoB;
            tbZoom.Value = zoom;
            tbPan.Value = pan;
            lblPan_Click(null, null);

            switch (op_mode_vfoA)
            {
                case Mode.BPSK31:
                case Mode.BPSK63:
                case Mode.BPSK125:
                case Mode.BPSK250:
                case Mode.QPSK31:
                case Mode.QPSK63:
                case Mode.QPSK125:
                case Mode.QPSK250:
                    this.grpChannels.Text = "VFOA " + op_mode_vfoA.ToString() + "     VFOB " +
                    op_mode_vfoB.ToString() + "     ";
                    grpMonitor.Text = "Mode PSK";
                    break;
            }

            if (iq_correction == IQ_correction.FIXED)
            {
                SetRXIQGainPhase(1, rx_image_gain_table[(int)current_band],
                    rx_image_phase_table[(int)current_band]);
                RX_phase_gain();
            }
            else if (iq_correction == IQ_correction.WBIR)
            {
                SetRXIQGainPhase(2, 0.0f, 0.0f);
                RX_phase_gain();
            }

            dxcc = new DXCC();
            dxcc.Init(SetupForm.txtStnLOC.Text.ToString());

            try
            {
                if (use_telnet)
                    telnet_server = new TelnetServer(SetupForm.txtTelnetHostAddress.Text.ToString(),
                        (int)SetupForm.udTelnetServerPort.Value, SetupForm.chkIPV6.Checked);

                SetupForm.chkIPV6_CheckedChanged(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
            }
        }