Example #1
0
/*
 * Erzeugt die ganzen Events für das drawing area mittels lambdas.
 *
 * Parameter:
 *     drawingarea - Die drawing area auf der gezeichnet werden soll
 *
 * Zu den Schnittstellenkommentaren: Leider gibts das anscheinend nicht direkt
 *                                   in go (jedenfall kein @param etc.) :/
 */
func createEvents(drawingarea *gtk.DrawingArea) {
	// Wir brauchen ne pixmap in der wir die Pixeldaten speichern
	var pixmap *gdk.Pixmap
	// gdk.GC ist einfach eine Sammlung an Eigenschaften und Einstellungen zum zeichnen
	var gc *gdk.GC
	p := point{x: -1, y: -1}

	drawingarea.Connect("configure-event", func() {
		// wenns schon ne pixmap gibt, lösche diese
		if pixmap != nil {
			pixmap.Unref()
		}

		// hole pixmap und stuff
		allocation := drawingarea.GetAllocation()
		pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)

		// weißen Hintergrund zeichnen:
		gc = gdk.NewGC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.NewColor("white"))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)

		// Vorder- und Hintergrundfarbe setzen
		gc.SetRgbFgColor(gdk.NewColor("black"))
		gc.SetRgbBgColor(gdk.NewColor("red"))
	})

	// dieses event wird ausgeführt wenn auf das widget geklickt wurde
	drawingarea.Connect("button-press-event", func(ctx *glib.CallbackContext) {
		// Argumente holen
		arg := ctx.Args(0)
		// irgend son pointer auf ein objekt holen in dem mehr Infos stehen
		mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg))
		// Position ausgeben
		fmt.Println("Geklickte Position: ", p)

		// hier ist meine eigene Logik:
		// Wenn noch kein Klick registriert wurde ist p.x == p.y == -1 und da soll noch nichts gezeichnet werden
		// erst wenn der zweite klick kommt soll zwischen diesem und dem alten eine Linie gezeichnet werden
		p_neu := point{x: int(mev.X), y: int(mev.Y)}
		if p.x != -1 && p.y != -1 {
			pixmap.GetDrawable().DrawLine(gc, p.x, p.y, p_neu.x, p_neu.y)
			drawingarea.GetWindow().Invalidate(nil, false)
		}
		// Position des neuen klicks speichern, es ist also der neue startpunkt der nächsten linie
		p = p_neu
	})

	// dieses event wird ausgeführt wenn drawingarea.GetWindow().Invalidate(nil, false) aufgerufen wird
	drawingarea.Connect("expose-event", func() {
		if pixmap != nil {
			drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	// Events müssen manuell spezifiziert werden.
	// Hier werden immer MASKs übergeben, hier also eben die BUTTON_PRESS_MASK,
	// welche erst in einen int gecastet werden muss
	drawingarea.SetEvents(int(gdk.BUTTON_PRESS_MASK))
}
Example #2
0
func NewGraph() *Graph {
	graph := &Graph{*gtk.NewDrawingArea(), nil, nil, -20.0, 20.0, -2.0, 2.0, 1, 10.0, 0.1, 1.0, nil, nil}
	graph.Connect("expose_event", func() {
		if graph.pixmap != nil {
			graph.GetWindow().GetDrawable().DrawDrawable(graph.gc, graph.pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	graph.Connect("configure_event", func(ctx *glib.CallbackContext) {
		if graph.pixmap != nil {
			graph.pixmap.Unref()
		}
		allocation := graph.GetAllocation()
		graph.pixmap = gdk.NewPixmap(graph.GetWindow().GetDrawable(),
			allocation.Width,
			allocation.Height,
			-1)
		graph.gc = gdk.NewGC(graph.pixmap.GetDrawable())
		graph.setScale()
		graph.plot()
	})

	graph.SetEvents(int(gdk.EXPOSE | gdk.CONFIGURE))
	return graph
}
Example #3
0
func configure_board(vbox *gtk.VBox) {
	drawingarea = gtk.NewDrawingArea()
	drawingarea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		var allocation gtk.Allocation
		drawingarea.GetAllocation(&allocation)
		pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)
		gc = gdk.NewGC(pixmap.GetDrawable())
		display_init_grid(gc, pixmap)
	})

	drawingarea.Connect("button-press-event", func(ctx *glib.CallbackContext) {
		// Check if the game is running and if player click in the goban
		if stopGame == true || stopClick == true {
			return
		}
		if gdkwin == nil {
			gdkwin = drawingarea.GetWindow()
		}
		arg := ctx.Args(0)
		mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg))
		var mt gdk.ModifierType
		var x, y int
		if mev.IsHint != 0 {
			gdkwin.GetPointer(&x, &y, &mt)
		} else {
			x, y = int(mev.X), int(mev.Y)
		}
		x = ((x - INTER/2) / INTER)
		y = ((y - INTER/2) / INTER)
		if x < 0 || x >= 19 || y < 0 || y >= 19 {
			return
		}
		// end check
		good, vic := event_play(x, y)
		if good && iamode && stopGame == false && stopClick == false && vic == 0 {
			calc_ai()
		}
		if good && !iamode && stopGame == false && stopClick == false && hint {
			calcHint <- true
		}
	})

	drawingarea.Connect("expose-event", func() {
		if pixmap != nil {
			drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	drawingarea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK))

	vbox.Add(drawingarea)
}
Example #4
0
func (mw *MapWidget) configure() {
	if mw.pixmap != nil {
		mw.pixmap.Unref()
	}

	alloc := mw.dArea.GetAllocation()
	mw.w = alloc.Width
	mw.h = alloc.Height

	if !mw.isInit {
		mw.offX = -(mw.w / 2)
		mw.offZ = -(mw.h / 2)
		mw.isInit = true
	}

	mw.updateChunkBounds()

	mw.pixmap = gdk.NewPixmap(mw.dArea.GetWindow().GetDrawable(), mw.w, mw.h, 24)
	mw.pixmapGC = gdk.NewGC(mw.pixmap.GetDrawable())

	mw.drawBg()
	mw.updateGUI()
}
Example #5
0
func colorBox(c *gdk.Color) *gtk.DrawingArea {
	dArea := gtk.NewDrawingArea()
	var pixmap *gdk.Pixmap
	var gc *gdk.GC

	dArea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		alloc := dArea.GetAllocation()
		pixmap = gdk.NewPixmap(dArea.GetWindow().GetDrawable(), alloc.Width, alloc.Height, 24)
		gc = gdk.NewGC(pixmap.GetDrawable())
		gc.SetRgbFgColor(c)
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
	})

	dArea.Connect("expose-event", func() {
		if pixmap != nil {
			dArea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	return dArea
}
func Init(title string) {
	gtk.Init(nil)
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetPosition(gtk.WIN_POS_CENTER)
	window.SetTitle(title)
	window.SetIconName("gtk-dialog-info")
	window.Connect("destroy", func(ctx *glib.CallbackContext) {
		fmt.Println("got destroy!", ctx.Data().(string))
		gtk.MainQuit()
	}, "foo")

	//--------------------------------------------------------
	// GtkVBox
	//--------------------------------------------------------
	vbox := gtk.NewVBox(false, 1)

	//--------------------------------------------------------
	// GtkMenuBar
	//--------------------------------------------------------
	menubar := gtk.NewMenuBar()
	vbox.PackStart(menubar, false, false, 0)

	//--------------------------------------------------------
	// GtkDrawable
	//--------------------------------------------------------

	drawingarea := gtk.NewDrawingArea()
	//var gdkwin *gdk.Window
	var pixmap *gdk.Pixmap
	var gc *gdk.GC
	drawingarea.Connect("configure-event", func() {
		println("Configuring drawingArea!")
		if pixmap != nil {
			pixmap.Unref()
		}
		allocation := drawingarea.GetAllocation()
		pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)
		gc = gdk.NewGC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.NewColor("white"))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
		gc.SetRgbFgColor(gdk.NewColor("black"))
		gc.SetRgbBgColor(gdk.NewColor("white"))
		pixmap.GetDrawable().DrawRectangle(gc, false, 0, 0, 10, 10)
	})

	drawingarea.Connect("expose-event", func() {
		println("Exposing DrawingArea!")
		if pixmap != nil {
			drawingarea.GetWindow().GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
		}
	})

	vbox.Add(drawingarea)

	//--------------------------------------------------------
	// GtkScale
	//--------------------------------------------------------
	scale := gtk.NewHScaleWithRange(0, 100, 1)
	scale.Connect("value-changed", func() {
		//fmt.Println("scale:", int(scale.GetValue()))
	})
	vbox.Add(scale)

	window.Add(vbox)
	window.SetSizeRequest(600, 600)
	window.ShowAll()
	gtk.Main()
}
Example #7
0
func main() {
	gtk.Init(&os.Args)
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetTitle("GTK DrawingArea")
	window.Connect("destroy", gtk.MainQuit)

	vbox := gtk.NewVBox(true, 0)
	vbox.SetBorderWidth(5)
	drawingarea := gtk.NewDrawingArea()

	var p1, p2 point
	var gdkwin *gdk.Window
	var pixmap *gdk.Pixmap
	var gc *gdk.GC
	p1.x = -1
	p1.y = -1
	colors := []string{
		"black",
		"gray",
		"blue",
		"purple",
		"red",
		"orange",
		"yellow",
		"green",
		"darkgreen",
	}

	drawingarea.Connect("configure-event", func() {
		if pixmap != nil {
			pixmap.Unref()
		}
		allocation := drawingarea.GetAllocation()
		pixmap = gdk.NewPixmap(drawingarea.GetWindow().GetDrawable(), allocation.Width, allocation.Height, 24)
		gc = gdk.NewGC(pixmap.GetDrawable())
		gc.SetRgbFgColor(gdk.NewColor("white"))
		pixmap.GetDrawable().DrawRectangle(gc, true, 0, 0, -1, -1)
		gc.SetRgbFgColor(gdk.NewColor(colors[0]))
		gc.SetRgbBgColor(gdk.NewColor("white"))
	})

	drawingarea.Connect("motion-notify-event", func(ctx *glib.CallbackContext) {
		arg := ctx.Args(0)
		mev := *(**gdk.EventMotion)(unsafe.Pointer(&arg))
		var mt gdk.ModifierType
		if mev.IsHint != 0 {
			gdkwin.GetPointer(&p2.x, &p2.y, &mt)
		} else {
			p2.x, p2.y = int(mev.X), int(mev.Y)
		}
		if p1.x != -1 && p2.x != -1 && (gdk.EventMask(mt)&gdk.BUTTON_PRESS_MASK) != 0 {
			pixmap.GetDrawable().DrawLine(gc, p1.x, p1.y, p2.x, p2.y)
			gdkwin.Invalidate(nil, false)
		}
		colors = append(colors[1:], colors[0])
		gc.SetRgbFgColor(gdk.NewColor(colors[0]))
		p1 = p2
	})

	drawingarea.Connect("expose-event", func() {
		if pixmap == nil {
			return
		}
		gdkwin.GetDrawable().DrawDrawable(gc, pixmap.GetDrawable(), 0, 0, 0, 0, -1, -1)
	})

	drawingarea.SetEvents(int(gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK | gdk.BUTTON_PRESS_MASK))
	vbox.Add(drawingarea)

	window.Add(vbox)
	window.SetSizeRequest(400, 400)
	window.ShowAll()

	gdkwin = drawingarea.GetWindow()

	gtk.Main()
}
Example #8
0
// emptyPixmap creates an empty pixmap.
func emptyPixmap(w, h, depth int) *gdk.Pixmap {
	// The underlying C function would create an empty, unbound pixmap, if the drawable paramater was a NULL pointer.
	// Since simply passing a nil value would result in a panic (dereferencing a nil pointer), we use a new gdk.Drawable.
	// gdk.Drawable contains a C pointer which is NULL by default. So passing a new(gdk.Drawable) does the trick.
	return gdk.NewPixmap(new(gdk.Drawable), w, h, depth)
}