|
| 1 | +import CGtk |
| 2 | + |
| 3 | +/// Allows drawing with cairo. |
| 4 | +/// |
| 5 | +/// <picture><source srcset="drawingarea-dark.png" media="(prefers-color-scheme: dark)"><img alt="An example GtkDrawingArea" src="drawingarea.png"></picture> |
| 6 | +/// |
| 7 | +/// It’s essentially a blank widget; you can draw on it. After |
| 8 | +/// creating a drawing area, the application may want to connect to: |
| 9 | +/// |
| 10 | +/// - The [signal@Gtk.Widget::realize] signal to take any necessary actions |
| 11 | +/// when the widget is instantiated on a particular display. |
| 12 | +/// (Create GDK resources in response to this signal.) |
| 13 | +/// |
| 14 | +/// - The [signal@Gtk.DrawingArea::resize] signal to take any necessary |
| 15 | +/// actions when the widget changes size. |
| 16 | +/// |
| 17 | +/// - Call [method@Gtk.DrawingArea.set_draw_func] to handle redrawing the |
| 18 | +/// contents of the widget. |
| 19 | +/// |
| 20 | +/// The following code portion demonstrates using a drawing |
| 21 | +/// area to display a circle in the normal widget foreground |
| 22 | +/// color. |
| 23 | +/// |
| 24 | +/// ## Simple GtkDrawingArea usage |
| 25 | +/// |
| 26 | +/// ```c |
| 27 | +/// static void |
| 28 | +/// draw_function (GtkDrawingArea *area, |
| 29 | +/// cairo_t *cr, |
| 30 | +/// int width, |
| 31 | +/// int height, |
| 32 | +/// gpointer data) |
| 33 | +/// { |
| 34 | +/// GdkRGBA color; |
| 35 | +/// |
| 36 | +/// cairo_arc (cr, |
| 37 | +/// width / 2.0, height / 2.0, |
| 38 | +/// MIN (width, height) / 2.0, |
| 39 | +/// 0, 2 * G_PI); |
| 40 | +/// |
| 41 | +/// gtk_widget_get_color (GTK_WIDGET (area), |
| 42 | +/// &color); |
| 43 | +/// gdk_cairo_set_source_rgba (cr, &color); |
| 44 | +/// |
| 45 | +/// cairo_fill (cr); |
| 46 | +/// } |
| 47 | +/// |
| 48 | +/// int |
| 49 | +/// main (int argc, char **argv) |
| 50 | +/// { |
| 51 | +/// gtk_init (); |
| 52 | +/// |
| 53 | +/// GtkWidget *area = gtk_drawing_area_new (); |
| 54 | +/// gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100); |
| 55 | +/// gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100); |
| 56 | +/// gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area), |
| 57 | +/// draw_function, |
| 58 | +/// NULL, NULL); |
| 59 | +/// return 0; |
| 60 | +/// } |
| 61 | +/// ``` |
| 62 | +/// |
| 63 | +/// The draw function is normally called when a drawing area first comes |
| 64 | +/// onscreen, or when it’s covered by another window and then uncovered. |
| 65 | +/// You can also force a redraw by adding to the “damage region” of the |
| 66 | +/// drawing area’s window using [method@Gtk.Widget.queue_draw]. |
| 67 | +/// This will cause the drawing area to call the draw function again. |
| 68 | +/// |
| 69 | +/// The available routines for drawing are documented in the |
| 70 | +/// [Cairo documentation](https://www.cairographics.org/manual/); GDK |
| 71 | +/// offers additional API to integrate with Cairo, like [func@Gdk.cairo_set_source_rgba] |
| 72 | +/// or [func@Gdk.cairo_set_source_pixbuf]. |
| 73 | +/// |
| 74 | +/// To receive mouse events on a drawing area, you will need to use |
| 75 | +/// event controllers. To receive keyboard events, you will need to set |
| 76 | +/// the “can-focus” property on the drawing area, and you should probably |
| 77 | +/// draw some user-visible indication that the drawing area is focused. |
| 78 | +/// |
| 79 | +/// If you need more complex control over your widget, you should consider |
| 80 | +/// creating your own `GtkWidget` subclass. |
| 81 | +open class DrawingArea: Widget { |
| 82 | + /// Creates a new drawing area. |
| 83 | + public convenience init() { |
| 84 | + self.init( |
| 85 | + gtk_drawing_area_new() |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + override func didMoveToParent() { |
| 90 | + super.didMoveToParent() |
| 91 | + |
| 92 | + let handler0: |
| 93 | + @convention(c) (UnsafeMutableRawPointer, Int, Int, UnsafeMutableRawPointer) -> Void = |
| 94 | + { _, value1, value2, data in |
| 95 | + SignalBox2<Int, Int>.run(data, value1, value2) |
| 96 | + } |
| 97 | + |
| 98 | + addSignal(name: "resize", handler: gCallback(handler0)) { |
| 99 | + [weak self] (param0: Int, param1: Int) in |
| 100 | + guard let self = self else { return } |
| 101 | + self.resize?(self, param0, param1) |
| 102 | + } |
| 103 | + |
| 104 | + let handler1: |
| 105 | + @convention(c) (UnsafeMutableRawPointer, OpaquePointer, UnsafeMutableRawPointer) -> Void = |
| 106 | + { _, value1, data in |
| 107 | + SignalBox1<OpaquePointer>.run(data, value1) |
| 108 | + } |
| 109 | + |
| 110 | + addSignal(name: "notify::content-height", handler: gCallback(handler1)) { |
| 111 | + [weak self] (param0: OpaquePointer) in |
| 112 | + guard let self = self else { return } |
| 113 | + self.notifyContentHeight?(self, param0) |
| 114 | + } |
| 115 | + |
| 116 | + let handler2: |
| 117 | + @convention(c) (UnsafeMutableRawPointer, OpaquePointer, UnsafeMutableRawPointer) -> Void = |
| 118 | + { _, value1, data in |
| 119 | + SignalBox1<OpaquePointer>.run(data, value1) |
| 120 | + } |
| 121 | + |
| 122 | + addSignal(name: "notify::content-width", handler: gCallback(handler2)) { |
| 123 | + [weak self] (param0: OpaquePointer) in |
| 124 | + guard let self = self else { return } |
| 125 | + self.notifyContentWidth?(self, param0) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// The content height. |
| 130 | + @GObjectProperty(named: "content-height") public var contentHeight: Int |
| 131 | + |
| 132 | + /// The content width. |
| 133 | + @GObjectProperty(named: "content-width") public var contentWidth: Int |
| 134 | + |
| 135 | + /// Emitted once when the widget is realized, and then each time the widget |
| 136 | + /// is changed while realized. |
| 137 | + /// |
| 138 | + /// This is useful in order to keep state up to date with the widget size, |
| 139 | + /// like for instance a backing surface. |
| 140 | + public var resize: ((DrawingArea, Int, Int) -> Void)? |
| 141 | + |
| 142 | + public var notifyContentHeight: ((DrawingArea, OpaquePointer) -> Void)? |
| 143 | + |
| 144 | + public var notifyContentWidth: ((DrawingArea, OpaquePointer) -> Void)? |
| 145 | +} |
0 commit comments