Created
July 6, 2014 17:33
-
-
Save knocte/427edb84b95bb5ef8790 to your computer and use it in GitHub Desktop.
PangoGtkGui.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Gtk; | |
using System; | |
using Cairo; | |
namespace Nereid | |
{ | |
public class Program | |
{ | |
public static void Main (string [] args) | |
{ | |
Gtk.Application.Init (); | |
new GuiClient (); | |
Gtk.Application.Run (); | |
} | |
} | |
public class GuiClient : Window | |
{ | |
// Major Layout Components | |
private VBox primary_vbox; | |
private Widget track_info_container; | |
public GuiClient () : base ("bug") | |
{ | |
Show (); | |
} | |
protected override void OnShown () | |
{ | |
BuildPrimaryLayout (); | |
base.OnShown (); | |
} | |
private void BuildPrimaryLayout () | |
{ | |
primary_vbox = new VBox (); | |
var track_info_display = new ClassicTrackInfoDisplay (); | |
track_info_display.Show (); | |
track_info_container = new EditorBox (track_info_display); | |
track_info_container.Show (); | |
primary_vbox.PackStart (track_info_container, false, false, 0); | |
primary_vbox.Show (); | |
Add (primary_vbox); | |
track_info_display.Start (); | |
} | |
public class EditorBox : EventBox | |
{ | |
public EditorBox (Widget child) | |
{ | |
Child = child; | |
VisibleWindow = false; | |
} | |
} | |
} | |
internal static class CairoHelper | |
{ | |
public static string ColorGetHex (Cairo.Color color, bool withAlpha) | |
{ | |
if (withAlpha) { | |
return String.Format("#{0:x2}{1:x2}{2:x2}{3:x2}", (byte)(color.R * 255), (byte)(color.G * 255), | |
(byte)(color.B * 255), (byte)(color.A * 255)); | |
} else { | |
return String.Format("#{0:x2}{1:x2}{2:x2}", (byte)(color.R * 255), (byte)(color.G * 255), | |
(byte)(color.B * 255)); | |
} | |
} | |
} | |
public class SomeInfo { | |
public string DisplayArtistName { | |
get; | |
set; | |
} | |
public string DisplayAlbumTitle { | |
get; | |
set; | |
} | |
public string DisplayTrackTitle { | |
get; | |
set; | |
} | |
} | |
public class ClassicTrackInfoDisplay : Widget | |
{ | |
public ClassicTrackInfoDisplay () | |
{ | |
#if GTK3 | |
HasWindow = false; | |
#else | |
WidgetFlags |= WidgetFlags.NoWindow; | |
#endif | |
} | |
public void Start () | |
{ | |
var incoming_track = new SomeInfo { | |
DisplayArtistName = "theArtist", | |
DisplayAlbumTitle = "theAlbum", | |
DisplayTrackTitle = "theTitle" | |
}; | |
QueueDraw (); | |
current_track = incoming_track; | |
} | |
private Cairo.Color text_color; | |
private Cairo.Color text_light_color; | |
private SomeInfo current_track; | |
protected override bool | |
#if GTK3 | |
OnDrawn (Cairo.Context cr) { | |
#else | |
OnExposeEvent(Gdk.EventExpose evnt) { | |
Cairo.Context cr = Gdk.CairoHelper.Create (evnt.Window); | |
#endif | |
bool idle = current_track == null; | |
if (!Visible || !IsMapped || idle) { | |
return true; | |
} | |
RenderTrackInfo (cr, current_track); | |
return true; | |
} | |
protected virtual string GetFirstLineText (SomeInfo track) | |
{ | |
var result = String.Format ("<b>{0}</b>", GLib.Markup.EscapeText (track.DisplayTrackTitle)); | |
//return String.Format ("<span color=\"{0}\">{1}</span>", | |
// CairoHelper.ColorGetHex (text_color, false), | |
// result); | |
return result; | |
} | |
protected virtual string GetSecondLineText (SomeInfo track) | |
{ | |
string markup = GetByFrom (track.DisplayArtistName, track.DisplayAlbumTitle); | |
return String.Format ("<span color=\"{0}\">{1}</span>", | |
CairoHelper.ColorGetHex (text_color, false), | |
markup); | |
} | |
private string MarkupFormat (string fmt, params string [] args) | |
{ | |
string [] new_args = new string [args.Length + 2]; | |
new_args[0] = String.Format ("<span color=\"{0}\" size=\"small\">", | |
CairoHelper.ColorGetHex (text_light_color, false)); | |
new_args[1] = "</span>"; | |
for (int i = 0; i < args.Length; i++) { | |
new_args[i + 2] = GLib.Markup.EscapeText (args[i]); | |
} | |
return String.Format (fmt, new_args); | |
} | |
private string GetByFrom (string display_artist, string display_album) | |
{ | |
return MarkupFormat ("{0}by{1} {2} {0}from{1} {3}", display_artist, display_album); | |
} | |
private Pango.Layout first_line_layout; | |
private Pango.Layout second_line_layout; | |
#if GTK3 | |
protected override void OnGetPreferredHeight (out int minimum_height, out int natural_height) | |
{ | |
minimum_height = ComputeWidgetHeight (); | |
natural_height = minimum_height; | |
} | |
protected override void OnGetPreferredWidth (out int minimum_width, out int natural_width) | |
{ | |
minimum_width = ComputeWidgetHeight () * 20; | |
natural_width = minimum_width; | |
} | |
#else | |
protected override void OnSizeRequested (ref Requisition requisition) | |
{ | |
requisition.Height = ComputeWidgetHeight (); | |
requisition.Width = ComputeWidgetHeight () * 20; | |
} | |
#endif | |
private int ComputeWidgetHeight () | |
{ | |
int width, height; | |
Pango.Layout layout = new Pango.Layout (PangoContext); | |
layout.SetText ("W"); | |
layout.GetPixelSize (out width, out height); | |
layout.Dispose (); | |
return 2 * height; | |
} | |
protected void RenderTrackInfo (Context cr, SomeInfo track) | |
{ | |
double offset = Allocation.Height + 10; | |
double y = 0; | |
double x = offset; | |
double width = Allocation.Width - offset; | |
int unused, fl_height, sl_height; | |
int pango_width = (int)(width * Pango.Scale.PangoScale); | |
if (first_line_layout == null) { | |
first_line_layout = Pango.CairoHelper.CreateLayout (cr); | |
} | |
if (second_line_layout == null) { | |
second_line_layout = Pango.CairoHelper.CreateLayout (cr); | |
} | |
// Set up the text layouts | |
first_line_layout.Width = pango_width; | |
second_line_layout.Width = pango_width; | |
// Compute the layout coordinates | |
//first_line_layout.SetMarkup (GetFirstLineText (track)); | |
first_line_layout.SetMarkup (GetFirstLineText (track) + "__" + GetSecondLineText (track) + "___________"); | |
first_line_layout.GetPixelSize (out unused, out fl_height); | |
second_line_layout.SetMarkup (GetSecondLineText (track)); | |
second_line_layout.GetPixelSize (out unused, out sl_height); | |
y = (Allocation.Height - (fl_height + sl_height)) / 2; | |
cr.MoveTo (x, y); | |
#if GTK3 | |
cr.SetSourceColor (text_color); | |
#else | |
cr.Color = text_color; | |
#endif | |
Pango.CairoHelper.ShowLayout (cr, first_line_layout); | |
cr.MoveTo (x, y + fl_height); | |
Pango.CairoHelper.ShowLayout (cr, second_line_layout); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment