[gnome-subtitles] Fixed column width mode. More efficient but limits character counts to 99.
- From: Pedro Daniel da Rocha Melo e Castro <pcastro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-subtitles] Fixed column width mode. More efficient but limits character counts to 99.
- Date: Sun, 17 Jul 2011 16:39:56 +0000 (UTC)
commit 96a1888dee435e14d4c2596decc02b080e190c1b
Author: Pedro Castro <pedro gnomesubtitles org>
Date: Sun Jul 17 14:26:29 2011 +0100
Fixed column width mode. More efficient but limits character counts to 99.
.../Ui/Edit/SubtitleEditTextViewMargin.cs | 61 ++++++--------------
1 files changed, 17 insertions(+), 44 deletions(-)
---
diff --git a/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextViewMargin.cs b/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextViewMargin.cs
index 9deab9a..0202e9e 100644
--- a/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextViewMargin.cs
+++ b/src/GnomeSubtitles/Ui/Edit/SubtitleEditTextViewMargin.cs
@@ -23,11 +23,15 @@ using System;
namespace GnomeSubtitles.Ui.Edit {
+//TODO possible improvements: draw text once with newlines and Spacing
public class SubtitleEditTextViewMargin {
private int marginCharWidth = -1; //pixels
private int marginSpace = 4; //pixels
- private int marginMinDigits = 2; //the minimum number of digits for margin width (1 would make the margin adjust with more than 9 chars, so it's better to keep a minimum of 2 to avoid constant adjustment
- private int marginDigitCount = 2;
+ private int marginDigitCount = 2; //we always show ammount for 2 digits, which should account for "normal" lengths (above 99, lengths are marked with 99+)
+ private int marginNumbersWidth = -1;
+ private int marginWidth = -1;
+ private int marginMaxCharCount = 99;
+ private String marginMaxCharCountString = "99.";
/* Cached GCs and Pango Layout */
private Gdk.GC bgGC = null;
@@ -49,20 +53,17 @@ public class SubtitleEditTextViewMargin {
public void DrawMargin (TextView textView, Gdk.Window window) {
/* Get char count info */
int[,] info;
- int maxCharCount;
- GetCharCountDrawInfo(textView, out info, out maxCharCount);
+ GetCharCountDrawInfo(textView, out info);
/* Do some calculations */
- int marginNumbersWidth = marginDigitCount * this.marginCharWidth;
- int marginNumbersX = textView.Allocation.Width - this.marginSpace - marginNumbersWidth;
-
+ int marginNumbersX = textView.Allocation.Width - this.marginSpace - this.marginNumbersWidth;
+
/* Draw line */
- int marginWidth = (this.marginSpace * 2) + marginNumbersWidth;
- int marginLineX = textView.Allocation.Width - marginWidth;
+ int marginLineX = textView.Allocation.Width - this.marginWidth;
window.DrawLine(this.lineGC, marginLineX, 0, marginLineX, textView.Allocation.Height);
/* Draw background area */
- window.DrawRectangle(this.bgGC, true, marginLineX+1, 0, marginWidth-1, textView.Allocation.Height);
+ window.DrawRectangle(this.bgGC, true, marginLineX+1, 0, this.marginWidth-1, textView.Allocation.Height);
/* Draw text */
int infoCount = info.GetLength(0);
@@ -70,16 +71,16 @@ public class SubtitleEditTextViewMargin {
int charCount = info[i, 0];
int y = info[i, 1];
- this.textLayout.SetText(charCount.ToString());
+ String charCountText = (charCount > this.marginMaxCharCount ? this.marginMaxCharCountString : charCount.ToString());
+ this.textLayout.SetText(charCountText);
Pango.Rectangle layoutRect = GetPangoLayoutRect(this.textLayout);
window.DrawLayout(this.textGC, marginNumbersX, y - layoutRect.Height/2, this.textLayout);
}
}
- private void GetCharCountDrawInfo (TextView textView, out int[,] info, out int maxCharCount) {
+ private void GetCharCountDrawInfo (TextView textView, out int[,] info) {
if (textView.Buffer.LineCount == 0) {
info = null;
- maxCharCount = 0;
return; //shouldn't happen, but just to make sure
}
@@ -98,16 +99,12 @@ public class SubtitleEditTextViewMargin {
/* Initializations */
info = new int[lineCount, 2];
- maxCharCount = -1;
/* Process start iter */
int startLineCharCount = startIter.CharsInLine - (lineCount > 1 ? 1 : 0); //subtract 1 for newline if there are >1 lines
info[0, 0] = startLineCharCount; //Char Count
Gdk.Rectangle startIterLocation = textView.GetIterLocation(startIter);
info[0, 1] = startIterLocation.Bottom - (startIterLocation.Height/2) - minVisibleY; //Y
- if (startLineCharCount > maxCharCount) {
- maxCharCount = startLineCharCount;
- }
/* If only 1 line, return */
if (lineCount == 1) {
@@ -121,9 +118,6 @@ public class SubtitleEditTextViewMargin {
info[i, 0] = charCount;
Gdk.Rectangle iterLocation = textView.GetIterLocation(iter);
info[i, 1] = iterLocation.Bottom - (iterLocation.Height/2) - minVisibleY; //Y
- if (charCount > maxCharCount) {
- maxCharCount = charCount;
- }
}
/* Process end iter */
@@ -131,9 +125,6 @@ public class SubtitleEditTextViewMargin {
info[lineCount-1, 0] = endLineCharCount;
Gdk.Rectangle endIterLocation = textView.GetIterLocation(endIter);
info[lineCount-1, 1] = endIterLocation.Bottom - (endIterLocation.Height/2) - minVisibleY; //Y
- if (endLineCharCount > maxCharCount) {
- maxCharCount = endLineCharCount;
- }
}
private Pango.Rectangle GetPangoLayoutRect (Pango.Layout layout) {
@@ -141,22 +132,6 @@ public class SubtitleEditTextViewMargin {
layout.GetPixelExtents(out inkRect, out logicalRect);
return logicalRect;
}
-
-
- private int CalcDigitCount (TextBuffer buffer, int marginMinDigits) {
- int maxChars = -1;
- int lineCount = buffer.LineCount;
- for (int line = 0 ; line < lineCount; line++) {
- TextIter iter = buffer.GetIterAtLine(line);
- int chars = iter.CharsInLine - (line == lineCount - 1 ? 0 : 1); //Subtract 1 for newline (except for the last line)
- if (chars > maxChars) {
- maxChars = chars;
- }
- }
-
- int digitCount = CountDigitsInNumber(maxChars);
- return Math.Max(digitCount, this.marginMinDigits);
- }
private int CountDigitsInNumber (int number) {
return (number == 0 ? 1 : (int)Math.Floor(Math.Log10(number)) + 1); //assuming the number is positive, otherwise would need to use abs() too
@@ -188,10 +163,12 @@ public class SubtitleEditTextViewMargin {
this.textLayout.SetText("0");
Pango.Rectangle layoutRect = GetPangoLayoutRect(this.textLayout);
this.marginCharWidth = layoutRect.Width;
+ this.marginNumbersWidth = this.marginDigitCount * this.marginCharWidth;
+ this.marginWidth = (this.marginSpace * 2) + this.marginNumbersWidth;
+
/* Events */
textView.ExposeEvent += OnExposeEvent;
- textView.Buffer.Changed += OnBufferChanged; //To calculate margin digit count (based on the largest line char count)
textView.StyleSet += OnStyleSet; //To update colors if the style is changed
textView.Parent.ExposeEvent += OnScrolledWindowExposeEvent;
}
@@ -207,10 +184,6 @@ public class SubtitleEditTextViewMargin {
Refresh(); //Necessary for artifacts not to appear when scrolling
}
- private void OnBufferChanged (object o, EventArgs args) {
- this.marginDigitCount = CalcDigitCount(o as TextBuffer, this.marginMinDigits);
- }
-
private void OnStyleSet (object o, StyleSetArgs args) {
SetGCs();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]