pub struct WxBackend<'context, C>where
C: DeviceContext,{ /* private fields */ }Expand description
Bridge struct to allow plotters to plot on a wxdragon::DeviceContext.
This backend works with any wxdragon::DeviceContext that implements the
required drawing primitives. For example:
wxdragon::AutoBufferedPaintDCfor drawing on awxdragon::Panelin a GUI application.wxdragon::MemoryDCfor off-screen drawing to awxdragon::Bitmap.
§How to use
Create an app with a wxdragon::Panel, and use the panel’s on_paint
handler to create a new wxdragon::AutoBufferedPaintDC (device context)
each time, wrap it in a WxBackend and then draw on it.
use plotters::prelude::*;
use plotters::style::text_anchor::{HPos, Pos, VPos};
use plotters_wxdragon::WxBackend;
use wxdragon::{self as wx, WindowEvents, WxWidget};
struct DrawingPanel {
panel: wx::Panel,
}
impl DrawingPanel {
fn new(parent: &wx::Frame) -> Self {
let panel = wx::PanelBuilder::new(parent).build();
panel.set_background_style(wx::BackgroundStyle::Paint);
// Register the paint handler with a move closure
panel.on_paint(move |_event| {
// Create a device context (wxdragon has several types)
// and a plotters backend
let dc = wx::AutoBufferedPaintDC::new(&panel);
let mut backend = WxBackend::new(&dc);
// Create a plotters plot as you would with any other backend
backend
.draw_rect((300, 250), (500, 350), &BLACK, false)
.unwrap();
let style = TextStyle::from(("monospace", 32.0).into_font())
.pos(Pos::new(HPos::Center, VPos::Center));
backend
.draw_text("hello, world", &style, (400, 300))
.unwrap();
// Call present() when you are ready
backend.present().expect("present");
});
// Handle SIZE events to refresh when the window size changes
panel.on_size(move |_event| {
panel.refresh(true, None);
});
Self { panel }
}
}
impl std::ops::Deref for DrawingPanel {
type Target = wx::Panel;
fn deref(&self) -> &Self::Target {
&self.panel
}
}
let _ = wxdragon::main(|_| {
let frame = wx::Frame::builder()
.with_title("Getting started")
// with this, wx produces a canvas of size 800 x 600
.with_size(wx::Size::new(852, 689))
.build();
let drawing_panel = DrawingPanel::new(&frame);
// Initial paint
drawing_panel.refresh(true, None);
frame.show(true);
});Implementations§
Source§impl<'context, C> WxBackend<'context, C>where
C: DeviceContext,
impl<'context, C> WxBackend<'context, C>where
C: DeviceContext,
Sourcepub fn new(context: &'context C) -> WxBackend<'context, C>
pub fn new(context: &'context C) -> WxBackend<'context, C>
Creates a new WxBackend from a wxdragon::DeviceContext.
The DeviceContext is initialized with a white background color and
transparent background mode.
Sourcepub fn set_background_color(&self, color: Colour)
pub fn set_background_color(&self, color: Colour)
Set the background color of the device context.
This setting affects the global background, and also the fill color of text labels.
Sourcepub fn set_background_mode(&self, mode: BackgroundMode)
pub fn set_background_mode(&self, mode: BackgroundMode)
Set the background mode of the device context.
This settings affects the fill color of text labels. Use
BackgroundMode::Solid if you want text labels to have a solid
background, otherwise leave the default
BackgroundMode::Transparent.
Trait Implementations§
Source§impl<'context, C> DrawingBackend for WxBackend<'context, C>where
C: DeviceContext,
impl<'context, C> DrawingBackend for WxBackend<'context, C>where
C: DeviceContext,
Source§fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>
fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>
Source§fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>
fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>
ensure_prepared is called
it checks if it needs a fresh buffer and present is called rendering all the
pending changes on the screen.