]> O.S.I.I.S - jp/crowedit.git/commitdiff
selection in token mode
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 30 Aug 2017 10:48:03 +0000 (12:48 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 30 Aug 2017 10:48:03 +0000 (12:48 +0200)
src/SourceEditor.cs

index 8d904aff0400f88e42b8173e2da97ab643db6f56..123573af03556e4b2e34e40fa10f3a0dddec7871 100644 (file)
@@ -54,7 +54,7 @@ namespace Crow
        {
                #region CTOR
                public SourceEditor ():base()
-               {                       
+               {
                        formating.Add ((int)XMLParser.TokenType.AttributeName, new TextFormating (Color.DarkBlue, Color.Transparent));
                        formating.Add ((int)XMLParser.TokenType.ElementName, new TextFormating (Color.DarkRed, Color.Transparent));
                        formating.Add ((int)XMLParser.TokenType.ElementStart, new TextFormating (Color.Red, Color.Transparent));
@@ -483,67 +483,72 @@ namespace Crow
                                int curL = i + ScrollY;
                                if (curL >= parser.Tokens.Count)
                                        break;
-                               List<Token> tokens = parser.Tokens[curL];
-                               int lPtr = 0;
-
-                               for (int t = 0; t < tokens.Count; t++) {
-                                       string lstr = tokens [t].Content;
-                                       if (lPtr < ScrollX) {
-                                               if (lPtr - ScrollX + lstr.Length <= 0) {
-                                                       lPtr += lstr.Length;
-                                                       continue;
-                                               }
-                                               lstr = lstr.Substring (ScrollX - lPtr);
-                                               lPtr += ScrollX - lPtr;
-                                       }
-                                       Color bg = this.Background;
-                                       Color fg = this.Foreground;
+                               drawTokenLine (gr, curL, selectionInProgress);
+                       }
+               }
+               void drawTokenLine(Context gr, int curL, bool selectionInProgress) {
+                       List<Token> tokens = parser.Tokens[curL];
+                       int lPtr = 0;
 
-                                       if (formating.ContainsKey ((int)tokens [t].Type)) {
-                                               TextFormating tf = formating [(int)tokens [t].Type];
-                                               bg = tf.Background;
-                                               fg = tf.Foreground;
+                       for (int t = 0; t < tokens.Count; t++) {
+                               string lstr = tokens [t].Content;
+                               if (lPtr < ScrollX) {
+                                       if (lPtr - ScrollX + lstr.Length <= 0) {
+                                               lPtr += lstr.Length;
+                                               continue;
                                        }
+                                       lstr = lstr.Substring (ScrollX - lPtr);
+                                       lPtr += ScrollX - lPtr;
+                               }
+                               Color bg = this.Background;
+                               Color fg = this.Foreground;
+                               Color selbg = this.SelectionBackground;
+                               Color selfg = this.SelectionForeground;
+
+                               if (formating.ContainsKey ((int)tokens [t].Type)) {
+                                       TextFormating tf = formating [(int)tokens [t].Type];
+                                       bg = tf.Background;
+                                       fg = tf.Foreground;
+                               }
+
+                               gr.SetSourceColor (fg);
 
-                                       gr.SetSourceColor (fg);
+                               int x = cb.X + (int)((lPtr - ScrollX) * fe.MaxXAdvance);
+
+                               gr.MoveTo (x, cb.Y + fe.Ascent + fe.Height * i);
+                               gr.ShowText (lstr);
+                               gr.Fill ();
+
+                               if (selectionInProgress && curL >= selectionStart.Y && curL <= selectionEnd.Y &&
+                                       !(curL == selectionStart.Y && lPtr + lstr.Length <= selectionStart.X) &&
+                                       !(curL == selectionEnd.Y && selectionEnd.X <= lPtr)) {
+
+                                       double rLineX = x,
+                                       rLineY = cb.Y + i * fe.Height,
+                                       rLineW = lstr.Length * fe.MaxXAdvance;
 
-                                       int x = cb.X + (int)((lPtr - ScrollX) * fe.MaxXAdvance);
+                                       if ((curL == selectionStart.Y) && (selectionStart.X < lPtr + lstr.Length) && (selectionStart.X > lPtr)) {
+                                               rLineX += (selectionStart.X - lPtr) * fe.MaxXAdvance;
+                                               rLineW -= (selectionStart.X - lPtr) * fe.MaxXAdvance;
+                                       }
+                                       if ((curL == selectionEnd.Y) && (selectionEnd.X < lPtr + lstr.Length))
+                                               rLineW = (selectionEnd.X - lPtr) * fe.MaxXAdvance;
 
+                                       gr.Save ();
+                                       gr.Operator = Operator.Source;
+                                       gr.Rectangle (rLineX, rLineY, rLineW, fe.Height);
+                                       gr.SetSourceColor (selbg);
+                                       gr.FillPreserve ();
+                                       gr.Clip ();
+                                       gr.Operator = Operator.Over;
+                                       gr.SetSourceColor (selfg);
                                        gr.MoveTo (x, cb.Y + fe.Ascent + fe.Height * i);
                                        gr.ShowText (lstr);
                                        gr.Fill ();
-
-                                       lPtr += lstr.Length;
+                                       gr.Restore ();
                                }
 
-
-//                             if (selectionInProgress && curL >= selectionStart.Y && curL <= selectionEnd.Y) {
-//
-//                                     double rLineX = cb.X,
-//                                     rLineY = cb.Y + i * fe.Height,
-//                                     rLineW = lstr.Length * fe.MaxXAdvance;
-//
-//                                     System.Diagnostics.Debug.WriteLine ("sel start: " + selectionStart + " sel end: " + selectionEnd);
-//                                     if (curL == selectionStart.Y) {
-//                                             rLineX += (selectionStart.X - ScrollX) * fe.MaxXAdvance;
-//                                             rLineW -= selectionStart.X * fe.MaxXAdvance;
-//                                     }
-//                                     if (curL == selectionEnd.Y)
-//                                             rLineW -= (lstr.Length - selectionEnd.X) * fe.MaxXAdvance;
-//
-//                                     gr.Save ();
-//                                     gr.Operator = Operator.Source;
-//                                     gr.Rectangle (rLineX, rLineY, rLineW, fe.Height);
-//                                     gr.SetSourceColor (SelectionBackground);
-//                                     gr.FillPreserve ();
-//                                     gr.Clip ();
-//                                     gr.Operator = Operator.Over;
-//                                     gr.SetSourceColor (SelectionForeground);
-//                                     gr.MoveTo (cb.X, cb.Y + fe.Ascent + fe.Height * i);
-//                                     gr.ShowText (lstr);
-//                                     gr.Fill ();
-//                                     gr.Restore ();
-//                             }
+                               lPtr += lstr.Length;
                        }
                }
                protected override void onDraw (Context gr)
@@ -554,7 +559,7 @@ namespace Crow
                                drawParsed (gr);
                        else
                                draw(gr);
-                               
+
                }
                #endregion