Skip to main content

WxBackend

Struct WxBackend 

Source
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:

§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,

Source

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.

Source

pub fn clear(&self)

Clear the device context.

Source

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.

Source

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,

Source§

type ErrorType = Error

The error type reported by the backend
Source§

fn get_size(&self) -> (u32, u32)

Get the dimension of the drawing backend in pixels
Source§

fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Ensure the backend is ready to draw
Source§

fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Finalize the drawing step and present all the changes. This is used as the real-time rendering support. The backend may implement in the following way, when ensure_prepared is called it checks if it needs a fresh buffer and present is called rendering all the pending changes on the screen.
Source§

fn draw_pixel( &mut self, point: BackendCoord, color: BackendColor, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a pixel on the drawing backend Read more
Source§

fn draw_line<S: BackendStyle>( &mut self, from: BackendCoord, to: BackendCoord, style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a line on the drawing backend Read more
Source§

fn draw_path<S: BackendStyle, I: IntoIterator<Item = BackendCoord>>( &mut self, path: I, style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a path on the drawing backend Read more
Source§

fn draw_circle<S: BackendStyle>( &mut self, center: BackendCoord, radius: u32, style: &S, fill: bool, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a circle on the drawing backend Read more
Source§

fn draw_rect<S: BackendStyle>( &mut self, upper_left: BackendCoord, bottom_right: BackendCoord, style: &S, fill: bool, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a rectangle on the drawing backend Read more
Source§

fn fill_polygon<S: BackendStyle, I: IntoIterator<Item = BackendCoord>>( &mut self, vert: I, style: &S, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Source§

fn draw_text<TStyle: BackendTextStyle>( &mut self, text: &str, style: &TStyle, pos: BackendCoord, ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Draw a text on the drawing backend Read more
Source§

fn estimate_text_size<TStyle: BackendTextStyle>( &self, text: &str, style: &TStyle, ) -> Result<(u32, u32), DrawingErrorKind<Self::ErrorType>>

Estimate the size of the horizontal text if rendered on this backend. This is important because some of the backend may not have font ability. Thus this allows those backend reports proper value rather than ask the font rasterizer for that. Read more
Source§

fn blit_bitmap( &mut self, pos: BackendCoord, (iw, ih): (u32, u32), src: &[u8], ) -> Result<(), DrawingErrorKind<Self::ErrorType>>

Blit a bitmap on to the backend. Read more

Auto Trait Implementations§

§

impl<'context, C> Freeze for WxBackend<'context, C>

§

impl<'context, C> RefUnwindSafe for WxBackend<'context, C>
where C: RefUnwindSafe,

§

impl<'context, C> Send for WxBackend<'context, C>
where C: Sync,

§

impl<'context, C> Sync for WxBackend<'context, C>
where C: Sync,

§

impl<'context, C> Unpin for WxBackend<'context, C>

§

impl<'context, C> UnsafeUnpin for WxBackend<'context, C>

§

impl<'context, C> UnwindSafe for WxBackend<'context, C>
where C: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.