public void HighlightWord(ScreenWord sw, Color color)
        {
            if (sw == null)
                return;

            //Graphics g = PrimaryBG.Graphics;

            secondaryHDC = PrimaryBG.Graphics.GetHdc();

            SetBkColor(secondaryHDC, ColorTranslator.ToWin32(color));
            SetBkMode(secondaryHDC, 2);
            SetTextColor(secondaryHDC, 0);

            IntPtr last_font = SelectObject(secondaryHDC, textFont.ToHfont());

            TextOut(secondaryHDC, sw.X1, VMargin + sw.Line * lineHeight, sw.Word, sw.Word.Length);

            DeleteObject(last_font);

            PrimaryBG.Graphics.ReleaseHdc(secondaryHDC);

            //TextRenderer.DrawText(g, sw.Word, textFont, new Point(sw.X1, VMargin + sw.Line * lineHeight),
            //    Color.Black, color, TextFormatFlags.NoPadding | TextFormatFlags.SingleLine);
        }
        public void SetNippingFrameByScreenWord(byte side, ScreenWord sw)
        {
            Frame f = (Frame)NippingFrame.Frame(side);

            if (sw == null || sw.Prev == null)
                f.Visible = false;
            else
            {
                Frame hf = (Frame)HighlightedFrame.Frame(side);
                f.Visible = true;
                f.FramePen = SuggestedPen;
                f.Line1 = hf.Line1;
                f.X1 = hf.X1;
                f.Line2 = sw.Prev.Line;
                f.X2 = sw.Prev.FX2;
            }
        }
        private void RenderText(Graphics g, int pairIndex, ref int offset, ref int cLine, byte side)
        {
            TextPair p = PText.TextPairs[pairIndex];

            RenderedTextInfo renderedInfo = p.RenderedInfo(side);

            if (!renderedInfo.Valid)
                return;

            int newColor;

            if (!EditMode && (NotFitOnScreenBase(pairIndex)))
                newColor = 2;
            else if ((LayoutMode == LayoutMode_Alternating))
            {
                switch (AlternatingColorScheme)
                {
                    case FileUsageInfo.AlternatingColorScheme_GreenBlack:
                        if (Reversed == (side == 2))
                            newColor = 3;
                        else
                            newColor = 1;
                        break;
                    case FileUsageInfo.AlternatingColorScheme_BlackGreen:
                        if (Reversed == (side == 2))
                            newColor = 1;
                        else
                            newColor = 3;
                        break;
                    default:
                        newColor = 1;
                        break;
                }

            }
            else
                newColor = 1;

            if (newColor != currentTextColor)
            {
                currentTextColor = newColor;

                switch(newColor)
                {
                    case 1:
                        SetTextColor(secondaryHDC, ColorTranslator.ToWin32(Color.Black));
                        break;
                    case 2:
                        SetTextColor(secondaryHDC, ColorTranslator.ToWin32(Color.Gray));
                        break;
                    case 3:
                        SetTextColor(secondaryHDC, ColorTranslator.ToWin32(Color.ForestGreen));
                        break;
                }

            }

            Collection<WordInfo> list = p.ComputedWords(side);

            int x;
            int y = -1;

            ScreenWord prev_screen_word = null;
            ScreenWord s = null;
            List<ScreenWord> l = null;

            int prev_y = -1;

            if (list != null)
                for (int i = 0; i < list.Count; i++)
                {
                    WordInfo r = list[i];

                    y = cLine + r.Line;

                    if (y < 0)
                        continue;

                    if (y >= NumberOfScreenLines)
                    {
                        renderedInfo.Line2 = -1;
                        renderedInfo.X2 = 0;
                        return;
                    }

                    s = new ScreenWord();

                    if (prev_screen_word != null)
                    {
                        s.Prev = prev_screen_word;
                        prev_screen_word.Next = s;
                    }

                    prev_screen_word = s;

                    s.FX1 = r.X1;

                    x = s.FX1 + offset;

                    if (y != prev_y)
                    {
                        if (!wordsOnScreen.TryGetValue(y, out l))
                        {
                            l = new List<ScreenWord>();
                            wordsOnScreen.Add(y, l);
                        }
                        prev_y = y;

                        if (FirstRenderedPair == -1)
                            FirstRenderedPair = pairIndex;

                        if (pairIndex > LastRenderedPair)
                            LastRenderedPair = pairIndex;

                    }

                    string wrd = r.Word;

                    TextOut(secondaryHDC, x, VMargin + y * lineHeight, wrd, wrd.Length);

                    s.PairIndex = pairIndex;
                    s.Pos = r.Pos;
                    s.Side = side;
                    s.X1 = x;
                    s.FX2 = r.X2;
                    s.X2 = s.FX2 + offset;
                    s.Line = y;
                    s.Word = wrd;

                    l.Add(s);

                }
        }
        public void ProcessMousePosition(bool forced, bool renderRequired)
        {
            // Let's check whether the cursor points to a Word

            bool needToRender = false;

            int word_x = -1;
            int line = GetLineNumberAtPosition(LastMouseY);

            ScreenWord found_word = GetWordAtPosition(LastMouseX, LastMouseY);

            if (found_word != null)
                word_x = found_word.X1;

            if (forced || mouse_text_line != line || mouse_text_x != word_x)
            {

                mouse_text_word = found_word;
                mouse_text_line = line;
                mouse_text_x = word_x;

                if (EditMode)
                {
                    if (found_word == null
                        || HighlightedPair != -1 && found_word.PairIndex != HighlightedPair)
                        MouseCurrentWord = null;
                    else
                        MouseCurrentWord = found_word;

                    if (renderRequired)
                        needToRender = true;
                }
                else
                {

                    if (LayoutMode == LayoutMode_Advanced && AdvancedMode_ShowPopups)
                    {

                        int newPair = (mouse_text_word == null ? -1 : mouse_text_word.PairIndex);

                        if (newPair != Advanced_HighlightedPair)
                        {
                            Advanced_HighlightedPair = newPair;

                            popUpInfo.visible = false;

                            if (newPair == -1)
                            {
                                AdvancedHighlightFrame.Visible = false;
                            }
                            else
                            {

                                TextPair p = PText[newPair];

                                RenderedTextInfo r = (Reversed ? p.RenderedInfo2 : p.RenderedInfo1);

                                if (r.Valid)
                                {

                                    AdvancedHighlightFrame.Visible = true;
                                    AdvancedHighlightFrame.Side = mouse_text_word.Side;
                                    AdvancedHighlightFrame.Line1 = r.Line1;
                                    AdvancedHighlightFrame.Line2 = r.Line2;
                                    AdvancedHighlightFrame.X1 = r.X1;
                                    AdvancedHighlightFrame.X2 = r.X2;

                                    byte trSide = (Reversed ? (byte)1 : (byte)2);

                                    Collection<CommonWordInfo> words = new Collection<CommonWordInfo>();

                                    int occLength = 0;

                                    int maxWidth = Width - 2 * PanelMargin;

                                    int height = 0;

                                    Collection<WordInfo> c = p.ComputedWords(trSide);

                                    if (c == null || c.Count == 0)
                                    {
                                        ProcessTextFromPair(p, trSide, ref occLength, words, ref height, ref maxWidth, NumberOfScreenLines);
                                        ParallelText.InsertWords(words, 0);
                                        c = p.ComputedWords(trSide);

                                    }

                                    if (c != null && c.Count != 0)
                                    {

                                        if (c[0].Line > 0)
                                        {
                                            int linesToDec = c[0].Line;

                                            foreach (WordInfo wi in c)
                                                wi.Line -= linesToDec;
                                        }

                                        DeterminePopupPosition(c, r, maxWidth);

                                    }

                                }

                            }

                            needToRender = true;
                        }

                    }

                    if (!SelectionFinished && mouse_text_word != null)
                    {
                        // Update second part of the selection
                        if (Selection2Pair != mouse_text_word.PairIndex
                            || Selection2Position != mouse_text_word.Pos)
                        {
                            Selection2Pair = mouse_text_word.PairIndex;
                            Selection2Position = mouse_text_word.Pos;

                            UpdateSelectionFrame();

                            if (renderRequired)
                                needToRender = true;
                        }

                    }

                }

            }

            if (needToRender)
                Render();
        }