Form1.cs 6.72 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebCam;
using ImageFilters;
using System.IO;
using MoyaAdminLib;
using System.Diagnostics;
using ThermoPrinterLibrary;

namespace MoyaSignup
{
    public partial class Form1 : Form
    {
        ThermoPrinter thermoPrinter;

        //public delegate void ThermoPrinterEvent(ThermoPrinter.Models model);
        //public event ThermoPrinterEvent SetPrinter;

        public Form1()
        {
            RestClient.ApiApplicationKey = Properties.Settings.Default.ApiApplicationKey;
            RestClient.ApiPass = Properties.Settings.Default.ApiPass;
            RestClient.ApiUser = Properties.Settings.Default.ApiUser;
            RestClient.ApiURL = Properties.Settings.Default.ApiURL;
            InitializeComponent();
        }


        enum Language
        {
            Finnish = 0,
            English = 1,
        }
//        Language currentLanguage = Language.English;

        /*
        void changed()
        {
            if (!pictureTaken )
            {

                    this.lblMessage.Text = "Ota kuva ensin painamalla 'Käytä tätä kuvaa' nappia";
                
                this.lblMessage.ForeColor = Color.Red;
                this.btnSave.Enabled = false;
                return;
            }
           
            this.lblMessage.ForeColor = Color.Green;

                this.lblMessage.Text = "OK, varmista tietojen oikeellisuus ja tallenna";
            
            this.btnSave.Enabled = true;
        }*/


        public void SetPrinter(ThermoPrinter.Models model)
        {
            thermoPrinter = new ThermoPrinter(model);
            
            try
            {
                thermoPrinter.Start(Properties.Settings.Default.printerPort);
                thermoPrinter.SetBarcodeWidth(ThermoPrinter.BarcodeWidths.Small);
                thermoPrinter.SetCharacterAlignment(ThermoPrinter.CharacterAlignments.Center);
                thermoPrinter.SetPositionHRI(ThermoPrinter.HRIPrintingPositions.Below);
            }
            catch
            {
                thermoPrinter = null;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*if (AutoUpdateLib.AutoUpdateCore.CheckForUpdates(null))
            {
                Application.ExitThread();		//program is required to close by autoupdate (probably updated)
                return;
            }*/

            if (Debugger.IsAttached)
                this.WindowState = FormWindowState.Normal;

            ApiSettings form = new ApiSettings();
            form.ShowDialog();

            if(Properties.Settings.Default.printerPort != null && Properties.Settings.Default.ThermoPrinter > 0)
            {
                ThermoPrinter.Models model = (ThermoPrinter.Models)Properties.Settings.Default.ThermoPrinter;
                SetPrinter(model);
            }

            userLoginControl1.SetPrinter += SetPrinter;

        }

        bool pictureTaken;

        private void btnFinnish_Click(object sender, EventArgs e)
        {

        }

        private void btnEnglish_Click(object sender, EventArgs e)
        {

        }


        
        bool checkSaveSafety()
        {
            return true;
        }
         
        /*
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkSaveSafety())
            {
                this.btnSave.Enabled = false;
                return;
            }

            this.Cursor = Cursors.WaitCursor;
            this.btnSave.Enabled = false;
            this.Enabled = false;
            this.btnSave.Refresh();

            
            //savePicture(pid);


            MessageBox.Show("Tallennus onnistui.\r\nMene infotiskille, ja kerro heille tämä numero, niin he opastavat sinut eteenpäin.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            
            this.Enabled = true;
            this.Cursor = Cursors.Default;
        }*/


        bool allowShutdown;
        public bool AllowShutdown {
            get { return allowShutdown; }
            set { allowShutdown = value; }
        }
        /*
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            changed();
        }*/

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!allowShutdown)
            {
                e.Cancel = true;
            }
            else
            {
                if (thermoPrinter != null)
                    thermoPrinter.Stop();
            }

        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (!allowShutdown)
                this.Activate();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                allowShutdown = true;
                Application.Exit();
                return;
            } else if (e.Alt && e.KeyCode == Keys.F5)
            {
                //userDetailsEditor1.SetPrinter(ThermoPrinterLibrary.ThermoPrinter.Models.IDP3210);
                //MessageBox.Show("Printteri: IDP3210");
                
            }
        }
        
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.Control && e.KeyCode == Keys.S)
            {
                allowShutdown = true;
                this.Close();
                return;
            }
        }

        
        
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {/*
            if (e.Button == MouseButtons.Right)
            {
                allowShutdown = true;
                Application.Exit();
                return;
            }*/
        }
        
        
        
        private void button1_Click(object sender, EventArgs e)
        {
            allowShutdown = true;
            Application.Exit();
        }

        private void userLoginControl1_LoginOk(object sender, EventArgs e)
        {
            userLoginControl1.Visible = false;
            userDetailsEditor1.LoadUser(userLoginControl1.CurrentUser);
            userDetailsEditor1.ThermoPrinter = thermoPrinter;
            userLoginControl1.Clear();
            userDetailsEditor1.Visible = true;
            //userLoginControl1.CurrentUser;
        }

        private void userDetailsEditor1_Load(object sender, EventArgs e)
        {

        }

        private void userDetailsEditor1_CloseView(object sender, EventArgs e)
        {
            userDetailsEditor1.Visible = false;
            userLoginControl1.Visible = true;
        }

        
    }
}