bool PG_MessageObject::ProcessEvent(const SDL_Event* event) {
	SDL_Event e;

	// check if we are able to process messages
	if(!my_canReceiveMessages) {
		return false;
	}

	if(event->type != SDL_USEREVENT) {
		if(captureObject != this)
			if(!AcceptEvent(event)) {
				return false;
			}
	}

	if((captureObject != NULL) && (captureObject != this)) {
		return false;
	}

	while(SDL_PeepEvents(&e, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK) > 0);

	bool rc = false;

	// dispatch message
	switch(event->type) {
		case SDL_ACTIVEEVENT:
			rc = eventActive(&event->active) || sigActive(this, &event->active);
			break;

		case SDL_KEYDOWN:
			rc = eventKeyDown(&event->key) || sigKeyDown(this, &event->key);
			break;

		case SDL_KEYUP:
			rc = eventKeyUp(&event->key) || sigKeyUp(this, &event->key);;
			break;

		case SDL_MOUSEMOTION:
			rc = eventMouseMotion(&event->motion) || sigMouseMotion(this, &event->motion);
			break;

		case SDL_MOUSEBUTTONDOWN:
			rc = eventMouseButtonDown(&event->button) || sigMouseButtonDown(this, &event->button);
			break;

		case SDL_MOUSEBUTTONUP:
			rc = eventMouseButtonUp(&event->button) || sigMouseButtonUp(this, &event->button);
			break;

		case SDL_QUIT:
			rc = eventQuit(0, NULL, (unsigned long)&event->quit) || sigQuit(this);
			break;

		case SDL_SYSWMEVENT:
			rc = eventSysWM(&event->syswm) || sigSysWM(this, &event->syswm);
			break;

		case SDL_VIDEORESIZE:
			rc = eventResize(&event->resize) || sigVideoResize(this, &event->resize);
			break;

		default:
			rc = false;
			break;
	}

	return rc;
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    isStart = false;
    isHexWorkspace = true;

    mAbout = new AboutUi(this);

    threadBuffer = (unsigned char*) malloc(BUFFER_SIZE);
    hexBuffer = (char*) malloc(4000);

    mSerialThread = new SerialThread(threadBuffer,BUFFER_SIZE);

    numberOfCharacter = 0;

    temp.reserve(1000);
    asciiOriginalBuffer.reserve(1000);

    QPixmap pix = QPixmap(":/MainUi/Start.png");
    iconStart = new QIcon(pix);
    pix = QPixmap(":/MainUi/Stop.png");
    iconStop = new QIcon(pix);
    pix = QPixmap(":/MainUi/Ascii.png");
    iconAscii = new QIcon(pix);
    pix = QPixmap(":/MainUi/Hex.png");
    iconHex = new QIcon(pix);
    pix = QPixmap(":/MainUi/DSR_R.png");
    iconDSR_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/DTR_R.png");
    iconDTR_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/RTS_R.png");
    iconRTS_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/CTS_R.png");
    iconCTS_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/DCD_R.png");
    iconDCD_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/RNG_R.png");
    iconRNG_R = new QIcon(pix);
    pix = QPixmap(":/MainUi/DSR_V.png");
    iconDSR_V = new QIcon(pix);
    pix = QPixmap(":/MainUi/DTR_V.png");
    iconDTR_V = new QIcon(pix);
    pix = QPixmap(":/MainUi/RTS_V.png");
    iconRTS_V = new QIcon(pix);
    pix = QPixmap(":/MainUi/CTS_V.png");
    iconCTS_V = new QIcon(pix);
    pix = QPixmap(":/MainUi/DCD_V.png");
    iconDCD_V = new QIcon(pix);
    pix = QPixmap(":/MainUi/RNG_V.png");
    iconRNG_V = new QIcon(pix);

    mSerialPort = new QComboBox;
    Utils::setPortComList(mSerialPort);
    // Setup by setPortComList
    //mSerialPort->setCurrentIndex(0);

    mBaudrate = new QComboBox;
    QStringList baudrate;
    baudrate << tr("230400") << tr("115200") << tr("57600") << tr("56000") << tr("38400") << tr("28800") << tr("19200") << tr("14400") << tr("9600") << tr("4800") << tr("2400") << tr("1200") << tr("600") << tr("300") << tr("110");
    mBaudrate->addItems(baudrate);
    mBaudrate->setCurrentIndex(1);

    mStartStop = new QAction(tr("StartStop"), this);
    mStartStop->setShortcut(tr("Ctrl+Return"));
    mStartStop->setToolTip(tr("Start communication"));
    mStartStop->setStatusTip(tr("Start communication"));
    mStartStop->setWhatsThis(tr("Start communication"));
    mStartStop->setIcon(*iconStart);

    mHexAscii = new QAction(tr("HexAscii"), this);
    mHexAscii->setShortcut(tr("Ctrl+A"));
    mHexAscii->setToolTip(tr("Change to Ascii workspace"));
    mHexAscii->setStatusTip(tr("Change to Ascii workspace"));
    mHexAscii->setWhatsThis(tr("Change to Ascii workspace"));
    mHexAscii->setIcon(*iconAscii);

    mClearButton = new QAction(tr("Clear"), this);
    mClearButton->setShortcut(tr("Ctrl+E"));
    mClearButton->setToolTip(tr("Clean output ascii and hex"));
    mClearButton->setStatusTip(tr("Clean output ascii and hex"));
    mClearButton->setWhatsThis(tr("Clean output ascii and hex"));
    mClearButton->setIcon( QIcon(QPixmap(":/MainUi/Delete.png")));

    mConfigToolbar = addToolBar(tr("Pointer type"));
    mConfigToolbar->addWidget(mSerialPort);
    mConfigToolbar->addWidget(mBaudrate);
    mConfigToolbar->addAction(mStartStop);
    mConfigToolbar->addAction(mHexAscii);
    mConfigToolbar->addAction(mClearButton);

    ui->setupUi(this);

    connect( mStartStop, SIGNAL(triggered() ), this, SLOT(eventStartStop()) );
    connect( mHexAscii, SIGNAL(triggered() ), this, SLOT(eventHexAscii()) );
    connect( mClearButton, SIGNAL(triggered() ), this, SLOT(eventClearButton()) );
    connect( ui->actionQuit, SIGNAL(triggered() ), this, SLOT(eventQuit()) );
    connect( ui->actionAbout, SIGNAL(triggered() ), this, SLOT(eventAbout()) );
    connect( ui->actionSaveInputOutput, SIGNAL(triggered() ), this, SLOT(eventSaveInputOutput()) );
    connect( ui->actionSaveTerminalContent, SIGNAL(triggered() ), this, SLOT(eventSaveTerminalContent()) );
    connect( ui->actionSave_Input_Output, SIGNAL(triggered() ), this, SLOT(eventSaveInputOutput()) );
    connect( ui->actionSave_Terminal_Content, SIGNAL(triggered() ), this, SLOT(eventSaveTerminalContent()) );
    connect( mSerialPort, SIGNAL(currentIndexChanged(const QString &)),this, SLOT(eventSerialPort()));
    connect( mBaudrate, SIGNAL(currentIndexChanged(const QString &)),this, SLOT(eventBaudrate()));
    connect( ui->addOutputFrame, SIGNAL(clicked()), this, SLOT(eventAddOutputFrame()));
    connect( ui->deleteOutputFrame, SIGNAL(clicked()), this, SLOT(eventDeleteOutputFrame()));
    connect( ui->sendOutputFrame, SIGNAL(clicked()), this, SLOT(eventSendOutputFrame()));
    connect( ui->outputTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(eventDoubleClickOutputTable(QModelIndex)));
    connect( ui->addOutputFrameHex, SIGNAL(clicked()), this, SLOT(eventAddOutputFrameHex()));
    connect( ui->deleteOutputFrameHex, SIGNAL(clicked()), this, SLOT(eventDeleteOutputFrameHex()));
    connect( ui->sendOutputFrameHex, SIGNAL(clicked()), this, SLOT(eventSendOutputFrameHex()));
    connect( ui->outputTableHex, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(eventDoubleClickOutputTableHex(QModelIndex)));
    connect( ui->directAsciiText, SIGNAL(textChanged(QString)), this, SLOT(eventDirectAsciiChanged(QString)));
    connect( ui->directHexText, SIGNAL(textChanged(QString)), this, SLOT(eventDirectHexChanged(QString)));
    connect( ui->actionSetDTR, SIGNAL(triggered(bool)), this, SLOT(eventSetDTR(bool)));
    connect( ui->actionSetRTS, SIGNAL(triggered(bool)), this, SLOT(eventSetRTS(bool)));

    connect( ui->actionClear_log, SIGNAL(triggered()), this, SLOT(eventClearLog()));
    connect( ui->actionClear_Ascii_Hex_log, SIGNAL(triggered()), this, SLOT(eventClearAsciiHexLog()));
    connect( ui->actionClear_Ascii_log, SIGNAL(triggered()), this, SLOT(eventClearAsciiLog()));
    connect( ui->actionControl_Status, SIGNAL(triggered(bool)), this, SLOT(eventHideControlStatus(bool)));
    connect( ui->actionActivate_Control_Line, SIGNAL(triggered(bool)), this, SLOT(eventActivateControlLine(bool)) );

    connect( ui->actionOutput_Ascii, SIGNAL(triggered(bool)), this, SLOT(eventHideOutputAscii(bool)));
    connect( ui->actionOutput_Hex, SIGNAL(triggered(bool)), this, SLOT(eventHideOutputHex(bool)));
    connect( ui->actionLog, SIGNAL(triggered(bool)), this, SLOT(eventHideLog(bool)));
    connect( ui->actionOpenFrameProject, SIGNAL(triggered()), this, SLOT(eventOpenFrameProject()));

    ui->terminalOutputHex->setVerticalScrollBar(ui->verticalScrollBar);
    ui->terminalOutputAscii->setVerticalScrollBar(ui->verticalScrollBar);

    ui->dockOutputAscii->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
    ui->dockOutputHex->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
    ui->dockLog->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);

    QFont font = ui->terminalOutputAscii->font();
#ifdef Q_OS_DARWIN
    font.setPointSize(14);
#else
    font.setPointSize(10);
#endif
    ui->terminalOutputAscii->setFont(font);
    ui->terminalOutputHex->setFont(font);
    ui->terminalOutputAsciiOnly->setFont(font);

    ui->terminalOutputAsciiOnly->setVisible(false);

    if (Settings::getFirstStart()==true)
    {
        ui->dockLog->setVisible(Settings::getLogStatus());
        ui->actionLog->setChecked(Settings::getLogStatus());
        ui->dockOutputAscii->setVisible(Settings::getOutputAsciiStatus());
        ui->actionOutput_Ascii->setChecked(Settings::getOutputAsciiStatus());
        ui->dockOutputHex->setVisible(Settings::getOutputHexStatus());
        ui->actionOutput_Hex->setChecked(Settings::getOutputHexStatus());
        ui->ControlToolBar->setVisible(Settings::getControlStatus());
        ui->actionControl_Status->setChecked(Settings::getControlStatus());
        if (Settings::getTerminalStatus()==true)
        {
            eventHexAscii();
        }
    }
    else
    {
        Settings::setFirstStart(true);
        Settings::setControlStatus(true);
        Settings::setLogStatus(true);
        Settings::setOutputAsciiStatus(true);
        Settings::setOutputHexStatus(true);
        Settings::setTerminalStatus(false);
    }
}