use wincom::{ComObject, Variant};
use windows::Win32::System::Com::IDispatch;
use crate::color::IvgColor;
use crate::document::{IvgDocument, IvgDocuments};
use crate::geometry::{IvgRect, IvgSnapPoint};
use crate::layer::IvgLayer;
use crate::page::{IvgPage, IvgPageSizes};
use crate::shape::{IvgShape, IvgShapeRange};
use crate::structs::{
IvgStructAlignProperties, IvgStructCreateOptions, IvgStructExportOptions,
IvgStructFontProperties, IvgStructHyphenationSettings, IvgStructImportOptions,
IvgStructOpenOptions, IvgStructPaletteOptions, IvgStructPasteOptions,
IvgStructSaveAsOptions, IvgStructSpaceProperties,
};
use crate::types::{progid_application, APP_INTER_IID};
use crate::view::{IvgWindow, IvgWindows};
pub struct IvgApplication {
disp: ComObject,
}
impl IvgApplication {
pub fn new(version: &str) -> Option<Self> {
ComObject::new_from_name(&progid_application(version), APP_INTER_IID)
.ok()
.map(|disp| Self { disp })
}
pub fn try_new(version: &str) -> wincom::Result<Self> {
ComObject::new_from_name(&progid_application(version), APP_INTER_IID)
.map(|disp| Self { disp })
}
fn prop_string(&self, name: &str) -> Option<String> {
self.disp.get_property(name).ok()?.to_string().ok()
}
fn prop_i64(&self, name: &str) -> Option<i64> {
self.disp.get_property(name).ok()?.to_i64().ok()
}
fn prop_bool(&self, name: &str) -> Option<bool> {
self.disp.get_property(name).ok()?.to_bool().ok()
}
fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
self.disp
.get_property(name)
.ok()?
.to_idispatch().ok()
}
fn put(&self, name: &str, v: Variant) -> bool {
self.disp.set_property(name, vec![v]).is_ok()
}
pub fn version(&self) -> Option<String> {
self.prop_string("version")
}
pub fn version_major(&self) -> Option<i64> {
self.prop_i64("versionmajor")
}
pub fn version_minor(&self) -> Option<i64> {
self.prop_i64("versionminor")
}
pub fn version_build(&self) -> Option<i64> {
self.prop_i64("versionbuild")
}
pub fn path(&self) -> Option<String> {
self.prop_string("Path")
}
pub fn config_path(&self) -> Option<String> {
self.prop_string("ConfigPath")
}
pub fn setup_path(&self) -> Option<String> {
self.prop_string("SetupPath")
}
pub fn program_path(&self) -> Option<String> {
self.prop_string("ProgramPath")
}
pub fn addon_path(&self) -> Option<String> {
self.prop_string("AddonPath")
}
pub fn user_data_path(&self) -> Option<String> {
self.prop_string("UserDataPath")
}
pub fn user_workspace_path(&self) -> Option<String> {
self.prop_string("UserWorkspacePath")
}
pub fn language_path(&self) -> Option<String> {
self.prop_string("LanguagePath")
}
pub fn help_file(&self) -> Option<String> {
self.prop_string("HelpFile")
}
pub fn visible(&self) -> Option<bool> {
self.prop_bool("Visible")
}
pub fn set_visible(&self, v: bool) -> bool {
self.put("Visible", Variant::from_bool(v))
}
pub fn documents(&self) -> Option<IvgDocuments> {
self.prop_dispatch("Documents").map(IvgDocuments::new)
}
pub fn active_document(&self) -> Option<IvgDocument> {
self.prop_dispatch("ActiveDocument").map(IvgDocument::new)
}
pub fn open_document(&self, src: impl Into<String>) -> bool {
let args = vec![
Variant::from_str(&src.into()),
Variant::from_i64(0),
];
self.disp.invoke_method("OpenDocument", args).is_ok()
}
pub fn open_document_ex(
&self,
src: impl Into<String>,
options: &IvgStructOpenOptions,
) -> Option<IvgDocument> {
let args = vec![
Variant::from_str(&src.into()),
options.as_variant(),
];
self.disp
.invoke_method("OpenDocumentEx", args)
.ok()?
.to_idispatch().ok()
.map(IvgDocument::new)
}
pub fn create_document(&self) -> Option<IvgDocument> {
self.disp
.invoke_method("CreateDocument", vec![])
.ok()?
.to_idispatch().ok()
.map(IvgDocument::new)
}
pub fn create_document_ex(
&self,
options: &IvgStructCreateOptions,
) -> Option<IvgDocument> {
let args = vec![options.as_variant()];
self.disp
.invoke_method("CreateDocumentEx", args)
.ok()?
.to_idispatch().ok()
.map(IvgDocument::new)
}
pub fn create_document_from_template(
&self,
template: impl Into<String>,
include_graphics: bool,
) -> Option<IvgDocument> {
let args = vec![
Variant::from_str(&template.into()),
Variant::from_bool(include_graphics),
];
self.disp
.invoke_method("CreateDocumentFromTemplate", args)
.ok()?
.to_idispatch().ok()
.map(IvgDocument::new)
}
pub fn active_page(&self) -> Option<IvgPage> {
self.prop_dispatch("ActivePage").map(IvgPage::new)
}
pub fn active_layer(&self) -> Option<IvgLayer> {
self.prop_dispatch("ActiveLayer").map(IvgLayer::new)
}
pub fn active_shape(&self) -> Option<IvgShape> {
self.prop_dispatch("ActiveShape").map(IvgShape::new)
}
pub fn active_selection_range(&self) -> Option<IvgShapeRange> {
self.prop_dispatch("ActiveSelectionRange").map(IvgShapeRange::new)
}
pub fn page_sizes(&self) -> Option<IvgPageSizes> {
self.prop_dispatch("PageSizes").map(IvgPageSizes::new)
}
pub fn active_window(&self) -> Option<IvgWindow> {
self.prop_dispatch("ActiveWindow").map(IvgWindow::new)
}
pub fn windows(&self) -> Option<IvgWindows> {
self.prop_dispatch("Windows").map(IvgWindows::new)
}
pub fn create_rgb_color(&self, r: i32, g: i32, b: i32) -> Option<IvgColor> {
let args = vec![
Variant::from_i64(r as i64),
Variant::from_i64(g as i64),
Variant::from_i64(b as i64),
];
self.disp
.invoke_method("CreateRGBColor", args)
.ok()?
.to_idispatch().ok()
.map(IvgColor::new)
}
pub fn create_cmyk_color(
&self,
c: i32,
m: i32,
y: i32,
k: i32,
) -> Option<IvgColor> {
let args = vec![
Variant::from_i64(c as i64),
Variant::from_i64(m as i64),
Variant::from_i64(y as i64),
Variant::from_i64(k as i64),
];
self.disp
.invoke_method("CreateCMYKColor", args)
.ok()?
.to_idispatch().ok()
.map(IvgColor::new)
}
pub fn create_gray_color(&self, gray: i32) -> Option<IvgColor> {
let args = vec![Variant::from_i64(gray as i64)];
self.disp
.invoke_method("CreateGrayColor", args)
.ok()?
.to_idispatch().ok()
.map(IvgColor::new)
}
pub fn create_color(&self, color_str: impl Into<String>) -> Option<IvgColor> {
let args = vec![Variant::from_str(&color_str.into())];
self.disp
.invoke_method("CreateColor", args)
.ok()?
.to_idispatch().ok()
.map(IvgColor::new)
}
pub fn create_export_options(&self) -> Option<IvgStructExportOptions> {
self.invoke_factory(
"CreateStructExportOptions",
IvgStructExportOptions::from_disp,
)
}
pub fn create_save_as_options(&self) -> Option<IvgStructSaveAsOptions> {
self.invoke_factory(
"CreateStructSaveAsOptions",
IvgStructSaveAsOptions::from_disp,
)
}
pub fn create_import_options(&self) -> Option<IvgStructImportOptions> {
self.invoke_factory(
"CreateStructImportOptions",
IvgStructImportOptions::from_disp,
)
}
pub fn create_palette_options(&self) -> Option<IvgStructPaletteOptions> {
self.invoke_factory(
"CreateStructPaletteOptions",
IvgStructPaletteOptions::from_disp,
)
}
pub fn create_open_options(&self) -> Option<IvgStructOpenOptions> {
self.invoke_factory(
"CreateStructOpenOptions",
IvgStructOpenOptions::from_disp,
)
}
pub fn create_create_options(&self) -> Option<IvgStructCreateOptions> {
self.invoke_factory(
"CreateStructCreateOptions",
IvgStructCreateOptions::from_disp,
)
}
pub fn create_paste_options(&self) -> Option<IvgStructPasteOptions> {
self.invoke_factory(
"CreateStructPasteOptions",
IvgStructPasteOptions::from_disp,
)
}
pub fn create_font_properties(&self) -> Option<IvgStructFontProperties> {
self.invoke_factory(
"CreateStructFontProperties",
IvgStructFontProperties::from_disp,
)
}
pub fn create_align_properties(&self) -> Option<IvgStructAlignProperties> {
self.invoke_factory(
"CreateStructAlignProperties",
IvgStructAlignProperties::from_disp,
)
}
pub fn create_space_properties(&self) -> Option<IvgStructSpaceProperties> {
self.invoke_factory(
"CreateStructSpaceProperties",
IvgStructSpaceProperties::from_disp,
)
}
pub fn create_hyphenation_settings(
&self,
) -> Option<IvgStructHyphenationSettings> {
self.invoke_factory(
"CreateStructHyphenationSettings",
IvgStructHyphenationSettings::from_disp,
)
}
fn invoke_factory<T>(
&self,
method: &str,
from_disp: fn(IDispatch) -> T,
) -> Option<T> {
self.disp
.invoke_method(method, vec![])
.ok()?
.to_idispatch().ok()
.map(from_disp)
}
pub fn create_rect(
&self,
x: f64,
y: f64,
w: f64,
h: f64,
) -> Option<IvgRect> {
let args = vec![
Variant::from_f64(x),
Variant::from_f64(y),
Variant::from_f64(w),
Variant::from_f64(h),
];
self.disp
.invoke_method("CreateRect", args)
.ok()?
.to_idispatch().ok()
.map(IvgRect::new)
}
pub fn create_snap_point(&self, x: f64, y: f64) -> Option<IvgSnapPoint> {
let args = vec![Variant::from_f64(x), Variant::from_f64(y)];
self.disp
.invoke_method("CreateSnapPoint", args)
.ok()?
.to_idispatch().ok()
.map(IvgSnapPoint::new)
}
pub fn unit(&self) -> Option<i64> {
self.prop_i64("Unit")
}
pub fn set_unit(&self, unit: i32) -> bool {
self.put("Unit", Variant::from_i64(unit as i64))
}
pub fn convert_units(&self, value: f64, from: i32, to: i32) -> Option<f64> {
crate::geometry::units::convert_by_i32(value, from, to)
}
pub fn evaluate(&self, expr: impl Into<String>) -> Option<Variant> {
let args = vec![Variant::from_str(&expr.into())];
self.disp.invoke_method("Evaluate", args).ok()
}
pub fn refresh(&self) -> bool {
self.disp.invoke_method("Refresh", vec![]).is_ok()
}
pub fn quit(&self) -> bool {
self.disp.invoke_method("Quit", vec![]).is_ok()
}
pub fn from_dispatch(disp: IDispatch) -> Self {
Self {
disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH),
}
}
pub fn from_disp(disp: IDispatch) -> Self {
Self::from_dispatch(disp)
}
pub fn raw(&self) -> &ComObject {
&self.disp
}
}