Sorry, that was caused by an error converting integer into double values. Cairo uses doubles between 0 and 1 for RGB instead of integer values from 0 to 255, so I had to convert from integer to double values. You have to changeken_anthony wrote:The most severe for me is that only RGB values of 0 or 255 work. Any value from 1 to 254 is treated as zero.
Code: Select all
ColorPart = (Color & $FF) / 255
Polygon()\ColorRed = ColorPart
Code: Select all
ColorPart = Color & $FF
Polygon()\ColorRed = ColorPart / 255
You are using only one callback with a global list of polygons, so always all polygons for all forms are drawn on each window refresh. If using multiple windows you have to take care that only the polygons for a specific window are drawn. I have therefore modified your code and extended the procedure CreatePolygon() and the linked list Polygon() with a further parameter Form, so that the callback can discriminate the Form causing a refresh and draw only the polygons for that form. I have taken your code and for easiness of testing I have combined your 3 files into one example:ken_anthony wrote:Question: Why is the green polygon showing up on the second form?
Code: Select all
EnableExplicit
Global wMain, wSecond
Enumeration
#m1
EndEnumeration
Structure Point
x.D
y.D
EndStructure
Structure PolygonEntry
ColorRed.D
ColorGreen.D
ColorBlue.D
Fill.L
List PolygonPoint.Point()
Form.I
EndStructure
Global MainForm.I, SecondForm.l
Declare WidgetExposeHandler(*a,*b)
Global NewList Polygon.PolygonEntry()
Global NewList PolygonPoint.Point()
; IncludeFile "/home/ken/PureBasic/polygon.Shardik" ;This file hasn't changed from first test. Use the same one.
; IncludeFile "/home/ken/PureBasic/Second.form"
Procedure Load_data()
Define i.I
For i = 1 To 5
AddElement(PolygonPoint())
Read.D PolygonPoint()\x
Read.D PolygonPoint()\y
Next i
EndProcedure
Load_data()
DataSection
; -- Pentagon
Data.D 0, 0
Data.D 150, 0
Data.D 175, 75
Data.D 150, 200
Data.D 50, 125
EndDataSection
; Third file is same as first test.
ImportC ""
gdk_cairo_create(*Drawable.GdkDrawable)
EndImport
ImportC "-lcairo"
cairo_close_path(*CairoContext)
cairo_destroy(*CairoContext)
cairo_fill(*CairoContext)
cairo_line_to(*CairoContext, x.D, y.D)
cairo_set_line_width(*CairoContext, LineWidth.D)
cairo_set_source_rgb(*CairoContext, Red.D, Green.D, Blue.D)
cairo_stroke(*CairoContext)
EndImport
Procedure CreatePolygon(List PolygonPoint.Point(), x.D, y.D, Color.L, Fill.L, Form.I)
Shared Polygon.PolygonEntry()
Protected ColorPart.L
Protected i.I
AddElement(Polygon())
ForEach PolygonPoint()
AddElement(Polygon()\PolygonPoint())
Polygon()\PolygonPoint()\x = x + PolygonPoint()\x
Polygon()\PolygonPoint()\y = y + PolygonPoint()\y
Next
ColorPart = Color & $FF
Polygon()\ColorRed = ColorPart / 255
ColorPart = (Color >> 8) & $FF
Polygon()\ColorGreen = ColorPart / 255
ColorPart = (Color >> 16) & $FF
Polygon()\ColorBlue = ColorPart / 255
Polygon()\Fill = Fill
Polygon()\Form = Form
EndProcedure
Procedure DrawPolygons(*Widget.GtkWidget)
Shared Polygon.PolygonEntry()
Protected CairoContext.I = gdk_cairo_create(*Widget\window)
ForEach Polygon()
If *Widget = Polygon()\Form
If Polygon()\Fill
ForEach Polygon()\PolygonPoint()
cairo_line_to(CairoContext, Polygon()\PolygonPoint()\x, Polygon()\PolygonPoint()\y)
Next
cairo_close_path(CairoContext)
cairo_set_source_rgb(CairoContext, Polygon()\ColorRed, Polygon()\ColorGreen, Polygon()\ColorBlue)
cairo_fill(CairoContext)
Else
cairo_set_line_width(CairoContext, 1)
cairo_set_source_rgb(CairoContext, Polygon()\ColorRed, Polygon()\ColorGreen, Polygon()\ColorBlue)
ForEach Polygon()\PolygonPoint()
cairo_line_to(CairoContext, Polygon()\PolygonPoint()\x, Polygon()\PolygonPoint()\y)
Next
cairo_close_path(CairoContext)
cairo_stroke(CairoContext)
EndIf
EndIf
Next
cairo_destroy(CairoContext)
EndProcedure
ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose)
DrawPolygons(*Widget.GtkWidget)
EndProcedure
; Second file: "Second.form"
Procedure wSecond_Events(event, SecondForm)
Select event
Case #PB_Event_CloseWindow
End
Case #PB_Event_Menu
Select EventMenu()
Case #m1
CreatePolygon(PolygonPoint(), 200, 150, RGB(0, 255, 0), #True, SecondForm)
WidgetExposeHandler(SecondForm, 0)
EndSelect
EndSelect
EndProcedure
Procedure OpenSecondForm()
wSecond = OpenWindow(#PB_Any, 350, 150, 500, 500, "Second Form", #PB_Window_SizeGadget)
SetWindowColor(wSecond, 0)
SecondForm = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(wSecond))), 0)
g_signal_connect_data_(SecondForm, "expose-event", @WidgetExposeHandler(), 0, 0, #G_CONNECT_AFTER)
CreateMenu(#PB_Any, WindowID(wSecond))
MenuTitle("Test")
MenuItem(#m1, "Do_test")
Repeat : wSecond_Events(WaitWindowEvent(), SecondForm) : ForEver
EndProcedure
; ----- Display window and define expose handler
Procedure OpenMain()
wMain = OpenWindow(#PB_Any, 270, 100, 500, 500, "Polygon demo", #PB_Window_SizeGadget)
SetWindowColor(wMain, 0)
MainForm = g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(wMain))), 0)
g_signal_connect_data_(MainForm, "expose-event", @WidgetExposeHandler(), 0, 0, #G_CONNECT_AFTER)
CreatePolygon(PolygonPoint(), 250, 200, RGB(255, 255, 0), #True, MainForm) ; THIS SHOULD BE YELLOW, NOT GREEN.
WidgetExposeHandler(MainForm, 0)
CreateMenu(#PB_Any, WindowID(wMain))
MenuTitle("Open")
MenuItem(#m1, "2nd form")
EndProcedure
Procedure wMain_Events(event)
Select event
Case #PB_Event_CloseWindow
End
Case #PB_Event_Menu
Select EventMenu()
Case #m1
OpenSecondForm()
EndSelect
EndSelect
EndProcedure
OpenMain()
Repeat : wMain_Events(WaitWindowEvent()) : ForEver