151 lines
8.6 KiB
C#
151 lines
8.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using Office = Microsoft.Office.Core;
|
|
|
|
namespace PlotDirector.WordAddIn
|
|
{
|
|
[ComVisible(true)]
|
|
public sealed class NativeRibbon : Office.IRibbonExtensibility
|
|
{
|
|
private readonly Func<WordAdapter> adapter;
|
|
private readonly Dictionary<string, string> displayed = new Dictionary<string, string>();
|
|
private readonly Dictionary<string, object> playbackIcons = new Dictionary<string, object>();
|
|
private Office.IRibbonUI ui;
|
|
|
|
public NativeRibbon(Func<WordAdapter> adapter) { this.adapter = adapter; }
|
|
|
|
public string GetCustomUI(string ribbonID)
|
|
{
|
|
using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("PlotDirector.WordAddIn.Ribbon.xml")))
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
public void OnLoad(Office.IRibbonUI ribbonUI)
|
|
{
|
|
ui = ribbonUI;
|
|
displayed.Clear();
|
|
}
|
|
|
|
public void Invalidate()
|
|
{
|
|
if (ui == null) return;
|
|
try
|
|
{
|
|
Refresh("PlotDirectorStatus", GetStatus(null));
|
|
Refresh("PlotDirectorHold",adapter()?.FocusWindowKey+GetFocusHeld(null));Refresh("PlotDirectorFocusState",adapter()?.FocusWindowKey+GetFocusState(null));
|
|
Refresh("PlotDirectorBook", GetBookTip(null));
|
|
Refresh("PlotDirectorChapter", GetChapterTip(null));
|
|
Refresh("PlotDirectorScene", GetSceneTip(null));
|
|
Refresh("PlotDirectorSyncStatus", GetSyncStatus(null) + "\n" + GetSyncTip(null));
|
|
Refresh("PlotDirectorSyncProgress", GetSyncProgress(null));
|
|
var enabled = CanOpenBook(null).ToString();
|
|
Refresh("PlotDirectorContext", enabled);
|
|
Refresh("PlotDirectorWeb", enabled);
|
|
Refresh("PlotDirectorSynchronise", enabled);
|
|
Refresh("PlotDirectorRepair", enabled);
|
|
Refresh("PlotDirectorAudioPlay", $"{AudioShowPlay(null)}:{AudioCanPlay(null)}");
|
|
Refresh("PlotDirectorAudioPause", $"{AudioShowPause(null)}:{AudioCanPause(null)}");
|
|
Refresh("PlotDirectorAudioBack", $"{AudioShowSeek(null)}:{AudioCanBack(null)}");
|
|
Refresh("PlotDirectorAudioForward", $"{AudioShowSeek(null)}:{AudioCanForward(null)}");
|
|
}
|
|
catch (COMException) { }
|
|
}
|
|
|
|
private void Refresh(string id, string value)
|
|
{
|
|
if (displayed.TryGetValue(id, out var previous) && previous == value) return;
|
|
ui.InvalidateControl(id);
|
|
displayed[id] = value;
|
|
}
|
|
|
|
public void RestoreTitle(Office.IRibbonControl control, string text)
|
|
{
|
|
ui?.InvalidateControl(control.Id);
|
|
}
|
|
|
|
public bool GetFocusHeld(Office.IRibbonControl control)=>adapter()?.WindowFocusHeld(control?.Context)==true;
|
|
public string GetFocusAction(Office.IRibbonControl control)=>GetFocusHeld(control)?"Release Hold":"Hold";
|
|
public string GetFocusState(Office.IRibbonControl control)=>adapter()?.WindowFocusState(control?.Context)??"Following";
|
|
public string GetFocusTip(Office.IRibbonControl control)=>GetFocusHeld(control)?"Release Hold: catch up to shared manuscript focus. Content synchronisation stays active.":"Hold this Word window at its local position. Other windows and content synchronisation continue.";
|
|
public void ToggleFocusHold(Office.IRibbonControl control,bool pressed){adapter()?.SetFocusHeld(pressed);Invalidate();}
|
|
public bool CanOpenBook(Office.IRibbonControl control) => adapter()?.HasRibbonBook == true;
|
|
public string GetBookTip(Office.IRibbonControl control) => adapter()?.RibbonBook ?? "No linked book";
|
|
public string GetChapterTip(Office.IRibbonControl control) => adapter()?.RibbonChapter ?? "Chapter unverified";
|
|
public string GetSceneTip(Office.IRibbonControl control) => adapter()?.RibbonScene ?? "Scene unverified";
|
|
public string GetSyncStatus(Office.IRibbonControl control) => adapter()?.ChapterStatusLabel ?? "Not connected";
|
|
public string GetSyncProgress(Office.IRibbonControl control) => adapter()?.RibbonSyncProgress ?? " ";
|
|
public string GetSyncTip(Office.IRibbonControl control) => adapter()?.ChapterStatusTip ?? "Open Companion to connect.";
|
|
public string GetStatus(Office.IRibbonControl control) => adapter()?.ConnectionState == "connected" ? "Companion connected" : "Companion offline";
|
|
public void OpenContext(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-context");
|
|
public void OpenPlotDirector(Office.IRibbonControl control) => adapter()?.OpenWritingWorkspace("tray-workspace");
|
|
public void Synchronise(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(false);
|
|
public void Repair(Office.IRibbonControl control) => adapter()?.RequestChapterSynchronisation(true);
|
|
public void Troubleshooting(Office.IRibbonControl control) => adapter()?.ShowTroubleshooting();
|
|
public void RemoveLegacyMarkers(Office.IRibbonControl control) => adapter()?.RemoveLegacyMarkers();
|
|
public void OpenCompanion(Office.IRibbonControl control) => adapter()?.OpenCompanion();
|
|
public void AudioPlay(Office.IRibbonControl control) => adapter()?.RequestAudio("play");
|
|
public void AudioPause(Office.IRibbonControl control) => adapter()?.RequestAudio("pause");
|
|
public void AudioBack(Office.IRibbonControl control) => adapter()?.RequestAudio("back15");
|
|
public void AudioForward(Office.IRibbonControl control) => adapter()?.RequestAudio("forward15");
|
|
|
|
public object GetPlaybackIcon(Office.IRibbonControl control)
|
|
{
|
|
if (playbackIcons.TryGetValue(control.Id, out var icon)) return icon;
|
|
|
|
using (var bitmap = new Bitmap(32, 32))
|
|
using (var graphics = Graphics.FromImage(bitmap))
|
|
using (var fill = new SolidBrush(Color.FromArgb(69, 157, 216)))
|
|
using (var outline = new Pen(Color.FromArgb(26, 58, 80), 1))
|
|
using (var shape = new GraphicsPath())
|
|
{
|
|
graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
|
switch (control.Id)
|
|
{
|
|
case "PlotDirectorAudioPlay":
|
|
shape.AddPolygon(new[] { new Point(8, 4), new Point(27, 16), new Point(8, 28) });
|
|
break;
|
|
case "PlotDirectorAudioPause":
|
|
shape.AddRectangle(new Rectangle(7, 5, 6, 22));
|
|
shape.AddRectangle(new Rectangle(19, 5, 6, 22));
|
|
break;
|
|
case "PlotDirectorAudioBack":
|
|
case "PlotDirectorAudioForward":
|
|
shape.AddPolygon(new[] { new Point(3, 7), new Point(15, 16), new Point(3, 25) });
|
|
shape.AddPolygon(new[] { new Point(16, 7), new Point(28, 16), new Point(16, 25) });
|
|
if (control.Id == "PlotDirectorAudioBack")
|
|
{
|
|
graphics.TranslateTransform(31, 0);
|
|
graphics.ScaleTransform(-1, 1);
|
|
}
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
graphics.FillPath(fill, shape);
|
|
graphics.DrawPath(outline, shape);
|
|
icon = RibbonPicture.FromImage(bitmap);
|
|
}
|
|
playbackIcons.Add(control.Id, icon);
|
|
return icon;
|
|
}
|
|
|
|
private sealed class RibbonPicture : System.Windows.Forms.AxHost
|
|
{
|
|
private RibbonPicture() : base("") { }
|
|
public static object FromImage(Image image) => GetIPictureDispFromPicture(image);
|
|
}
|
|
public bool AudioShowPlay(Office.IRibbonControl control) => adapter()?.HasAudioControls==true && adapter()?.AudioPlaying==false;
|
|
public bool AudioShowPause(Office.IRibbonControl control) => adapter()?.HasAudioControls==true && adapter()?.AudioPlaying==true;
|
|
public bool AudioShowSeek(Office.IRibbonControl control) => adapter()?.HasAudioControls==true;
|
|
public bool AudioCanPlay(Office.IRibbonControl control) => adapter()?.CanAudio("play")==true;
|
|
public bool AudioCanPause(Office.IRibbonControl control) => adapter()?.CanAudio("pause")==true;
|
|
public bool AudioCanBack(Office.IRibbonControl control) => adapter()?.CanAudio("back15")==true;
|
|
public bool AudioCanForward(Office.IRibbonControl control) => adapter()?.CanAudio("forward15")==true;
|
|
}
|
|
}
|