1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::prelude::*;
use crate::utils::FlString;
use fltk_sys::printer::*;
use std::ffi::CString;

/**
    Defines a printer object.
    Example usage:
    ```rust,no_run
    use fltk::{prelude::*, *};
    let mut but = button::Button::default();
    but.set_callback(|widget| {
        let mut printer = printer::Printer::default();
        if printer.begin_job(1).is_ok() {
            printer.begin_page().ok();
            let (width, height) = printer.printable_rect();
            draw::set_draw_color(enums::Color::Black);
            draw::set_line_style(draw::LineStyle::Solid, 2);
            draw::draw_rect(0, 0, width, height);
            draw::set_font(enums::Font::Courier, 12);
            printer.set_origin(width / 2, height / 2);
            printer.print_widget(widget, -widget.width() / 2, -widget.height() / 2);
            printer.end_page().ok();
            printer.end_job();
        }
    });
    ```
*/
pub struct Printer {
    inner: *mut Fl_Printer,
}

impl Printer {
    /// Creates a printer object
    pub fn default() -> Self {
        unsafe {
            let ptr = Fl_Printer_new();
            assert!(!ptr.is_null());
            Printer { inner: ptr }
        }
    }

    /// Begins a print job.
    /// `pagecount` The total number of pages to be created. Use 0 if this number is unknown
    /// Returns a tuple (frompage, topage) indicating the chosen pages by the user
    /// # Errors
    /// Errors on failure to print
    pub fn begin_job(&mut self, pagecount: i32) -> Result<(Option<i32>, Option<i32>), FltkError> {
        let mut frompage_ = 0;
        let mut topage_ = 0;
        unsafe {
            if Fl_Printer_begin_job(
                self.inner,
                pagecount as i32,
                &mut frompage_,
                &mut topage_,
                std::ptr::null_mut(),
            ) == 0
            {
                let from = if frompage_ == 0 {
                    None
                } else {
                    Some(frompage_ as i32)
                };
                let to = if topage_ == 0 {
                    None
                } else {
                    Some(topage_ as i32)
                };
                Ok((from, to))
            } else {
                Err(FltkError::Internal(FltkErrorKind::FailedToRun))
            }
        }
    }

    /// End the print page
    /// # Errors
    /// Errors on failure to end the page
    pub fn end_page(&mut self) -> Result<(), FltkError> {
        unsafe {
            if Fl_Printer_end_page(self.inner) == 0 {
                Ok(())
            } else {
                Err(FltkError::Internal(FltkErrorKind::FailedToRun))
            }
        }
    }

    /// Ends the print job
    pub fn end_job(&mut self) {
        unsafe { Fl_Printer_end_job(self.inner) }
    }

    /// Begins a print page
    /// # Errors
    /// Errors on failure to begin the page
    pub fn begin_page(&mut self) -> Result<(), FltkError> {
        unsafe {
            if Fl_Printer_begin_page(self.inner) == 0 {
                Ok(())
            } else {
                Err(FltkError::Internal(FltkErrorKind::FailedToRun))
            }
        }
    }

    /// Returns the width and height of the printable rect
    pub fn printable_rect(&self) -> (i32, i32) {
        unsafe {
            let mut x = 0;
            let mut y = 0;
            Fl_Printer_printable_rect(self.inner, &mut x, &mut y);
            (x, y)
        }
    }

    /// Returns the coordinates of the printable margins.
    /// (left, top, right, bottom)
    pub fn margins(&self) -> (i32, i32, i32, i32) {
        unsafe {
            let mut left = 0;
            let mut top = 0;
            let mut right = 0;
            let mut bottom = 0;
            Fl_Printer_margins(self.inner, &mut left, &mut top, &mut right, &mut bottom);
            (left, top, right, bottom)
        }
    }

    /// Get the origin coordinates of the printable rect
    pub fn origin(&self) -> (i32, i32) {
        unsafe {
            let mut x = 0;
            let mut y = 0;
            Fl_Printer_origin(self.inner, &mut x, &mut y);
            (x, y)
        }
    }

    /// Set the origin coordinates of the printable rect
    pub fn set_origin(&mut self, x: i32, y: i32) {
        unsafe { Fl_Printer_set_origin(self.inner, x, y) }
    }

    /// Scale the printable rect
    pub fn scale(&mut self, scale_x: f32, scale_y: f32) {
        unsafe { Fl_Printer_scale(self.inner, scale_x, scale_y) }
    }

    /// Rotate the printable rect
    pub fn rotate(&mut self, angle: f32) {
        unsafe { Fl_Printer_rotate(self.inner, angle) }
    }

    /// Translate the printable rect
    pub fn translate(&mut self, x: i32, y: i32) {
        unsafe { Fl_Printer_translate(self.inner, x, y) }
    }

    /// Untranslate the printable rect
    pub fn untranslate(&mut self) {
        unsafe { Fl_Printer_untranslate(self.inner) }
    }

    /// Check whether the printer is the current printer
    pub fn is_current(&self) -> bool {
        unsafe { Fl_Printer_is_current(self.inner as *mut _) != 0 }
    }

    /// Set the printer to be the current printer
    pub fn set_current(&mut self) {
        unsafe { Fl_Printer_set_current(self.inner) }
    }

    /// Print a widget
    pub fn print_widget<W: WidgetExt>(&self, widget: &W, delta_x: i32, delta_y: i32) {
        unsafe {
            Fl_Printer_print_widget(
                self.inner,
                widget.as_widget_ptr() as *mut _,
                delta_x,
                delta_y,
            )
        }
    }

    /// Print a window
    pub fn print_window<W: WindowExt>(&self, win: &W, x_offset: i32, y_offset: i32) {
        unsafe {
            Fl_Printer_print_window(
                self.inner,
                win.as_widget_ptr() as *mut _,
                x_offset,
                y_offset,
            )
        }
    }

    /// Set the dialog "Title"
    pub fn set_dialog_title(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_title(msg.into_raw()) }
    }

    /// Set the dialog "Printer"
    pub fn set_dialog_printer(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_printer(msg.into_raw()) }
    }

    /// Set dialog "Range"
    pub fn set_dialog_range(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_range(msg.into_raw()) }
    }

    /// Set dialog "Copies"
    pub fn set_dialog_copies(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_copies(msg.into_raw()) }
    }

    /// Set dialog "All"
    pub fn set_dialog_all(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_all(msg.into_raw()) }
    }

    /// Set dialog "Pages"
    pub fn set_dialog_pages(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_pages(msg.into_raw()) }
    }

    /// Set dialog "From"
    pub fn set_dialog_from(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_from(msg.into_raw()) }
    }

    /// Set dialog "To"
    pub fn set_dialog_to(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_to(msg.into_raw()) }
    }

    /// Set dialog "Properties"
    pub fn set_dialog_properties(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_properties(msg.into_raw()) }
    }

    /// Set dialog "Number of copies"
    pub fn set_dialog_copy_number(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_copyNo(msg.into_raw()) }
    }

    /// Set dialog "Print" button
    pub fn set_dialog_print_button(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_print_button(msg.into_raw()) }
    }

    /// Set dialog "Cancel" button
    pub fn set_dialog_cancel_button(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_cancel_button(msg.into_raw()) }
    }

    /// Set dialog "Print to file" button
    pub fn set_dialog_print_to_file(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_dialog_print_to_file(msg.into_raw()) }
    }

    /// Set property "Title"
    pub fn set_property_title(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_title(msg.into_raw()) }
    }

    /// Set property "Page Size"
    pub fn set_property_pagesize(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_pagesize(msg.into_raw()) }
    }

    /// Set property "Mode"
    pub fn set_property_mode(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_mode(msg.into_raw()) }
    }

    /// Set property "Use"
    pub fn set_property_use(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_use(msg.into_raw()) }
    }

    /// Set property "Save"
    pub fn set_property_save(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_save(msg.into_raw()) }
    }

    /// Set property "Cancel"
    pub fn set_property_cancel(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_Printer_set_property_cancel(msg.into_raw()) }
    }
}

impl Drop for Printer {
    fn drop(&mut self) {
        unsafe { Fl_Printer_delete(self.inner) }
    }
}