comparison gtk4/dw.c @ 2279:70d666a0636f

GTK4: Update dw_draw_*() to use the new APIs to draw onto widgets... Not fuctional yet, gdk_draw_context_get_frame_region() is returning NULL.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 03 Feb 2021 01:29:05 +0000
parents 18028fa53ef2
children 7902be60c542
comparison
equal deleted inserted replaced
2278:18028fa53ef2 2279:70d666a0636f
5471 * y: Y coordinate. 5471 * y: Y coordinate.
5472 */ 5472 */
5473 void dw_draw_point(HWND handle, HPIXMAP pixmap, int x, int y) 5473 void dw_draw_point(HWND handle, HPIXMAP pixmap, int x, int y)
5474 { 5474 {
5475 cairo_t *cr = NULL; 5475 cairo_t *cr = NULL;
5476 GdkDrawingContext *dc = NULL; 5476 GdkDrawContext *dc = NULL;
5477 cairo_region_t *clip = NULL; 5477 const cairo_region_t *clip = NULL;
5478 5478
5479 if(handle) 5479 if(handle)
5480 { 5480 {
5481 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5481 GtkNative *native = gtk_widget_get_native(handle);
5482 #if GTK3 5482 GdkSurface *surface = gtk_native_get_surface(native);
5483 GdkWindow *window = gtk_widget_get_window(handle); 5483
5484 /* Safety check for non-existant windows */ 5484 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5485 if(!window || !GDK_IS_WINDOW(window)) 5485 {
5486 clip = gdk_draw_context_get_frame_region(dc);
5487 gdk_draw_context_begin_frame(dc, clip);
5488 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5489 }
5490 else
5486 return; 5491 return;
5487 clip = gdk_window_get_clip_region(window);
5488 dc = gdk_window_begin_draw_frame(window, clip);
5489 cr = gdk_drawing_context_get_cairo_context(dc);
5490 #endif
5491 } 5492 }
5492 else if(pixmap) 5493 else if(pixmap)
5493 cr = cairo_create(pixmap->image); 5494 cr = cairo_create(pixmap->image);
5494 if(cr) 5495 if(cr)
5495 { 5496 {
5497 5498
5498 gdk_cairo_set_source_rgba(cr, foreground); 5499 gdk_cairo_set_source_rgba(cr, foreground);
5499 cairo_set_line_width(cr, 1); 5500 cairo_set_line_width(cr, 1);
5500 cairo_move_to(cr, x, y); 5501 cairo_move_to(cr, x, y);
5501 cairo_stroke(cr); 5502 cairo_stroke(cr);
5502 if(clip)
5503 cairo_region_destroy(clip);
5504 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5505 #if GTK3
5506 /* If we are using a drawing context... 5503 /* If we are using a drawing context...
5507 * we don't own the cairo context so don't destroy it. 5504 * we don't own the cairo context so don't destroy it.
5508 */ 5505 */
5509 if(dc) 5506 if(dc)
5510 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5507 gdk_draw_context_end_frame(dc);
5511 else 5508 else
5512 #else
5513 if(!dc)
5514 #endif
5515 cairo_destroy(cr); 5509 cairo_destroy(cr);
5516 } 5510 }
5517 } 5511 }
5518 5512
5519 /* Draw a line on a window (preferably a render window). 5513 /* Draw a line on a window (preferably a render window).
5526 * y2: Second Y coordinate. 5520 * y2: Second Y coordinate.
5527 */ 5521 */
5528 void dw_draw_line(HWND handle, HPIXMAP pixmap, int x1, int y1, int x2, int y2) 5522 void dw_draw_line(HWND handle, HPIXMAP pixmap, int x1, int y1, int x2, int y2)
5529 { 5523 {
5530 cairo_t *cr = NULL; 5524 cairo_t *cr = NULL;
5531 GdkDrawingContext *dc = NULL; 5525 GdkDrawContext *dc = NULL;
5532 cairo_region_t *clip = NULL; 5526 const cairo_region_t *clip = NULL;
5533 5527
5534 if(handle) 5528 if(handle)
5535 { 5529 {
5536 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5530 GtkNative *native = gtk_widget_get_native(handle);
5537 #if GTK3 5531 GdkSurface *surface = gtk_native_get_surface(native);
5538 GdkWindow *window = gtk_widget_get_window(handle); 5532
5539 /* Safety check for non-existant windows */ 5533 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5540 if(!window || !GDK_IS_WINDOW(window)) 5534 {
5535 clip = gdk_draw_context_get_frame_region(dc);
5536 gdk_draw_context_begin_frame(dc, clip);
5537 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5538 }
5539 else
5541 return; 5540 return;
5542 clip = gdk_window_get_clip_region(window);
5543 dc = gdk_window_begin_draw_frame(window, clip);
5544 cr = gdk_drawing_context_get_cairo_context(dc);
5545 #endif
5546 } 5541 }
5547 else if(pixmap) 5542 else if(pixmap)
5548 cr = cairo_create(pixmap->image); 5543 cr = cairo_create(pixmap->image);
5549 if(cr) 5544 if(cr)
5550 { 5545 {
5553 gdk_cairo_set_source_rgba(cr, foreground); 5548 gdk_cairo_set_source_rgba(cr, foreground);
5554 cairo_set_line_width(cr, 1); 5549 cairo_set_line_width(cr, 1);
5555 cairo_move_to(cr, x1, y1); 5550 cairo_move_to(cr, x1, y1);
5556 cairo_line_to(cr, x2, y2); 5551 cairo_line_to(cr, x2, y2);
5557 cairo_stroke(cr); 5552 cairo_stroke(cr);
5558 if(clip)
5559 cairo_region_destroy(clip);
5560 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5561 #if GTK3
5562 /* If we are using a drawing context... 5553 /* If we are using a drawing context...
5563 * we don't own the cairo context so don't destroy it. 5554 * we don't own the cairo context so don't destroy it.
5564 */ 5555 */
5565 if(dc) 5556 if(dc)
5566 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5557 gdk_draw_context_end_frame(dc);
5567 else 5558 else
5568 #else
5569 if(!dc)
5570 #endif
5571 cairo_destroy(cr); 5559 cairo_destroy(cr);
5572 } 5560 }
5573 } 5561 }
5574 5562
5575 /* Draw a closed polygon on a window (preferably a render window). 5563 /* Draw a closed polygon on a window (preferably a render window).
5583 */ 5571 */
5584 void dw_draw_polygon(HWND handle, HPIXMAP pixmap, int flags, int npoints, int *x, int *y) 5572 void dw_draw_polygon(HWND handle, HPIXMAP pixmap, int flags, int npoints, int *x, int *y)
5585 { 5573 {
5586 cairo_t *cr = NULL; 5574 cairo_t *cr = NULL;
5587 int z; 5575 int z;
5588 GdkDrawingContext *dc = NULL; 5576 GdkDrawContext *dc = NULL;
5589 cairo_region_t *clip = NULL; 5577 const cairo_region_t *clip = NULL;
5590 5578
5591 if(handle) 5579 if(handle)
5592 { 5580 {
5593 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5581 GtkNative *native = gtk_widget_get_native(handle);
5594 #if GTK3 5582 GdkSurface *surface = gtk_native_get_surface(native);
5595 GdkWindow *window = gtk_widget_get_window(handle); 5583
5596 /* Safety check for non-existant windows */ 5584 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5597 if(!window || !GDK_IS_WINDOW(window)) 5585 {
5586 clip = gdk_draw_context_get_frame_region(dc);
5587 gdk_draw_context_begin_frame(dc, clip);
5588 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5589 }
5590 else
5598 return; 5591 return;
5599 clip = gdk_window_get_clip_region(window);
5600 dc = gdk_window_begin_draw_frame(window, clip);
5601 cr = gdk_drawing_context_get_cairo_context(dc);
5602 #endif
5603 } 5592 }
5604 else if(pixmap) 5593 else if(pixmap)
5605 cr = cairo_create(pixmap->image); 5594 cr = cairo_create(pixmap->image);
5606 if(cr) 5595 if(cr)
5607 { 5596 {
5618 cairo_line_to(cr, x[z], y[z]); 5607 cairo_line_to(cr, x[z], y[z]);
5619 } 5608 }
5620 if(flags & DW_DRAW_FILL) 5609 if(flags & DW_DRAW_FILL)
5621 cairo_fill(cr); 5610 cairo_fill(cr);
5622 cairo_stroke(cr); 5611 cairo_stroke(cr);
5623 if(clip) 5612 /* If we are using a drawing context...
5624 cairo_region_destroy(clip);
5625 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5626 #if GTK3
5627 /* If we are using a drawing context...
5628 * we don't own the cairo context so don't destroy it. 5613 * we don't own the cairo context so don't destroy it.
5629 */ 5614 */
5630 if(dc) 5615 if(dc)
5631 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5616 gdk_draw_context_end_frame(dc);
5632 else 5617 else
5633 #else
5634 if(!dc)
5635 #endif
5636 cairo_destroy(cr); 5618 cairo_destroy(cr);
5637 } 5619 }
5638 } 5620 }
5639 5621
5640 /* Draw a rectangle on a window (preferably a render window). 5622 /* Draw a rectangle on a window (preferably a render window).
5648 * height: Height of rectangle. 5630 * height: Height of rectangle.
5649 */ 5631 */
5650 void dw_draw_rect(HWND handle, HPIXMAP pixmap, int flags, int x, int y, int width, int height) 5632 void dw_draw_rect(HWND handle, HPIXMAP pixmap, int flags, int x, int y, int width, int height)
5651 { 5633 {
5652 cairo_t *cr = NULL; 5634 cairo_t *cr = NULL;
5653 GdkDrawingContext *dc = NULL; 5635 GdkDrawContext *dc = NULL;
5654 cairo_region_t *clip = NULL; 5636 const cairo_region_t *clip = NULL;
5655 5637
5656 if(handle) 5638 if(handle)
5657 { 5639 {
5658 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5640 GtkNative *native = gtk_widget_get_native(handle);
5659 #if GTK3 5641 GdkSurface *surface = gtk_native_get_surface(native);
5660 GdkWindow *window = gtk_widget_get_window(handle); 5642
5661 /* Safety check for non-existant windows */ 5643 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5662 if(!window || !GDK_IS_WINDOW(window)) 5644 {
5645 clip = gdk_draw_context_get_frame_region(dc);
5646 gdk_draw_context_begin_frame(dc, clip);
5647 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5648 }
5649 else
5663 return; 5650 return;
5664 clip = gdk_window_get_clip_region(window);
5665 dc = gdk_window_begin_draw_frame(window, clip);
5666 cr = gdk_drawing_context_get_cairo_context(dc);
5667 #endif
5668 } 5651 }
5669 else if(pixmap) 5652 else if(pixmap)
5670 cr = cairo_create(pixmap->image); 5653 cr = cairo_create(pixmap->image);
5671 if(cr) 5654 if(cr)
5672 { 5655 {
5682 cairo_line_to(cr, x + width, y + height); 5665 cairo_line_to(cr, x + width, y + height);
5683 cairo_line_to(cr, x + width, y); 5666 cairo_line_to(cr, x + width, y);
5684 if(flags & DW_DRAW_FILL) 5667 if(flags & DW_DRAW_FILL)
5685 cairo_fill(cr); 5668 cairo_fill(cr);
5686 cairo_stroke(cr); 5669 cairo_stroke(cr);
5687 if(clip) 5670 /* If we are using a drawing context...
5688 cairo_region_destroy(clip);
5689 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5690 #if GTK3
5691 /* If we are using a drawing context...
5692 * we don't own the cairo context so don't destroy it. 5671 * we don't own the cairo context so don't destroy it.
5693 */ 5672 */
5694 if(dc) 5673 if(dc)
5695 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5674 gdk_draw_context_end_frame(dc);
5696 else 5675 else
5697 #else
5698 if(!dc)
5699 #endif
5700 cairo_destroy(cr); 5676 cairo_destroy(cr);
5701 } 5677 }
5702 } 5678 }
5703 5679
5704 /* Draw an arc on a window (preferably a render window). 5680 /* Draw an arc on a window (preferably a render window).
5715 * y2: Y coordinate of second segment of arc. 5691 * y2: Y coordinate of second segment of arc.
5716 */ 5692 */
5717 void API dw_draw_arc(HWND handle, HPIXMAP pixmap, int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2) 5693 void API dw_draw_arc(HWND handle, HPIXMAP pixmap, int flags, int xorigin, int yorigin, int x1, int y1, int x2, int y2)
5718 { 5694 {
5719 cairo_t *cr = NULL; 5695 cairo_t *cr = NULL;
5720 GdkDrawingContext *dc = NULL; 5696 GdkDrawContext *dc = NULL;
5721 cairo_region_t *clip = NULL; 5697 const cairo_region_t *clip = NULL;
5722 5698
5723 if(handle) 5699 if(handle)
5724 { 5700 {
5725 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5701 GtkNative *native = gtk_widget_get_native(handle);
5726 #if GTK3 5702 GdkSurface *surface = gtk_native_get_surface(native);
5727 GdkWindow *window = gtk_widget_get_window(handle); 5703
5728 /* Safety check for non-existant windows */ 5704 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5729 if(!window || !GDK_IS_WINDOW(window)) 5705 {
5706 clip = gdk_draw_context_get_frame_region(dc);
5707 gdk_draw_context_begin_frame(dc, clip);
5708 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5709 }
5710 else
5730 return; 5711 return;
5731 clip = gdk_window_get_clip_region(window);
5732 dc = gdk_window_begin_draw_frame(window, clip);
5733 cr = gdk_drawing_context_get_cairo_context(dc);
5734 #endif
5735 } 5712 }
5736 else if(pixmap) 5713 else if(pixmap)
5737 cr = cairo_create(pixmap->image); 5714 cr = cairo_create(pixmap->image);
5738 if(cr) 5715 if(cr)
5739 { 5716 {
5761 cairo_arc(cr, xorigin, yorigin, r, a1, a2); 5738 cairo_arc(cr, xorigin, yorigin, r, a1, a2);
5762 } 5739 }
5763 if(flags & DW_DRAW_FILL) 5740 if(flags & DW_DRAW_FILL)
5764 cairo_fill(cr); 5741 cairo_fill(cr);
5765 cairo_stroke(cr); 5742 cairo_stroke(cr);
5766 if(clip) 5743 /* If we are using a drawing context...
5767 cairo_region_destroy(clip);
5768 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5769 #if GTK3
5770 /* If we are using a drawing context...
5771 * we don't own the cairo context so don't destroy it. 5744 * we don't own the cairo context so don't destroy it.
5772 */ 5745 */
5773 if(dc) 5746 if(dc)
5774 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5747 gdk_draw_context_end_frame(dc);
5775 else 5748 else
5776 #else
5777 if(!dc)
5778 #endif
5779 cairo_destroy(cr); 5749 cairo_destroy(cr);
5780 } 5750 }
5781 } 5751 }
5782 5752
5783 /* Draw text on a window (preferably a render window). 5753 /* Draw text on a window (preferably a render window).
5791 void dw_draw_text(HWND handle, HPIXMAP pixmap, int x, int y, const char *text) 5761 void dw_draw_text(HWND handle, HPIXMAP pixmap, int x, int y, const char *text)
5792 { 5762 {
5793 cairo_t *cr = NULL; 5763 cairo_t *cr = NULL;
5794 PangoFontDescription *font; 5764 PangoFontDescription *font;
5795 char *tmpname, *fontname = "monospace 10"; 5765 char *tmpname, *fontname = "monospace 10";
5796 GdkDrawingContext *dc = NULL; 5766 GdkDrawContext *dc = NULL;
5797 cairo_region_t *clip = NULL; 5767 const cairo_region_t *clip = NULL;
5798 5768
5799 if(!text) 5769 if(!text)
5800 return; 5770 return;
5801 5771
5802 if(handle) 5772 if(handle)
5803 { 5773 {
5804 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */ 5774 GtkNative *native = gtk_widget_get_native(handle);
5805 #if GTK3 5775 GdkSurface *surface = gtk_native_get_surface(native);
5806 GdkWindow *window = gtk_widget_get_window(handle); 5776
5807 /* Safety check for non-existant windows */ 5777 if((dc = GDK_DRAW_CONTEXT(gdk_surface_create_cairo_context(surface))))
5808 if(!window || !GDK_IS_WINDOW(window)) 5778 {
5779 clip = gdk_draw_context_get_frame_region(dc);
5780 gdk_draw_context_begin_frame(dc, clip);
5781 cr = gdk_cairo_context_cairo_create(GDK_CAIRO_CONTEXT(dc));
5782 }
5783 else
5809 return; 5784 return;
5810 clip = gdk_window_get_clip_region(window);
5811 dc = gdk_window_begin_draw_frame(window, clip);
5812 cr = gdk_drawing_context_get_cairo_context(dc);
5813 if((tmpname = (char *)g_object_get_data(G_OBJECT(handle), "_dw_fontname"))) 5785 if((tmpname = (char *)g_object_get_data(G_OBJECT(handle), "_dw_fontname")))
5814 fontname = tmpname; 5786 fontname = tmpname;
5815 #endif
5816 } 5787 }
5817 else if(pixmap) 5788 else if(pixmap)
5818 { 5789 {
5819 if(pixmap->font) 5790 if(pixmap->font)
5820 fontname = pixmap->font; 5791 fontname = pixmap->font;
5864 } 5835 }
5865 g_object_unref(context); 5836 g_object_unref(context);
5866 } 5837 }
5867 pango_font_description_free(font); 5838 pango_font_description_free(font);
5868 } 5839 }
5869 if(clip)
5870 cairo_region_destroy(clip);
5871 /* TODO: Figure out how to do this in GTK4 with no GdkWindow */
5872 #if GTK3
5873 /* If we are using a drawing context... 5840 /* If we are using a drawing context...
5874 * we don't own the cairo context so don't destroy it. 5841 * we don't own the cairo context so don't destroy it.
5875 */ 5842 */
5876 if(dc) 5843 if(dc)
5877 gdk_window_end_draw_frame(gtk_widget_get_window(handle), dc); 5844 gdk_draw_context_end_frame(dc);
5878 else 5845 else
5879 #else
5880 if(!dc)
5881 #endif
5882 cairo_destroy(cr); 5846 cairo_destroy(cr);
5883 } 5847 }
5884 } 5848 }
5885 5849
5886 /* Query the width and height of a text string. 5850 /* Query the width and height of a text string.