Skip to content

Commit c198a96

Browse files
Modified codes
1 parent 154a0dc commit c198a96

21 files changed

+230
-338
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ GoPaint is a paint program written in Go similar to the Windows 10's built-in dr
33

44
I started leanring Go programming language recently and I wanted to make a complete application or maybe like clone of another application. So I ended up creating this paint program. Actually this program is my first ever Go project and I'm glad to share it with everyone :D
55

6-
This is only an alpha which means it probably has many buggs and not so stable. I will try to improve the codes over time and if anyone else want to help improving the codes, you are welcomed :)
6+
This is only an alpha which means it probably has many buggs and not so stable. I will try to improve the codes over time and if anyone else want to help improving the codes, you are welcomed.
77

8+
Lots of techniques that I used in order to implement various features of the application is kinda hardcoded and I believe those stufs can be done in more easier ways but to be very honest I had very fun making this project and I was kinda experimenting with the codes and techniques. In the very beginning of making this project I only planned making a small clone of the Windows 10 paint then I ended up experimenting with it. For example, for the Text Tool, I implemented my own text editing system (still incomplete) instead of using a text edit control from windows api. But again, I had fun doing it and I learned a lot from it. Hope you guys will find it helpful too.
9+
10+
This paint application is mainly targated for windows since I used all the native windows api functions and gdi32, gdi+ for all the graphical stuffs.
11+
12+
Any kind of suggestions are welcomed!
813

914
![preview](images/preview.jpg)
1015

__debug_bin

-1 KB
Binary file not shown.

canvas.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ type DrawingCanvas struct {
3535

3636
func NewDrawingCanvas(parent Window) *DrawingCanvas {
3737
canvas := &DrawingCanvas{Window: NewWindow()}
38-
canvas.init(parent)
38+
canvas.Init(parent)
3939
return canvas
4040
}
4141

42-
func (canvas *DrawingCanvas) init(parent Window) {
42+
func (canvas *DrawingCanvas) Init(parent Window) {
4343
logInfo("initializing canvas...")
4444
canvas.firstMove = true
4545
canvas.gridPen = NewDashPen(1, NewRgb(120, 120, 120))
4646
canvas.Create("", win.WS_CHILD|win.WS_VISIBLE|win.WS_CLIPCHILDREN, 10, 10, 10, 10, parent)
47-
canvas.SetPaintEventHandler(canvas.paint)
48-
canvas.SetMouseMoveEventHandler(canvas.mouseMove)
49-
canvas.SetMouseDownEventHandler(canvas.mouseDown)
50-
canvas.SetMouseUpEventHandler(canvas.mouseUp)
47+
canvas.SetPaintEventHandler(canvas.Paint)
48+
canvas.SetMouseMoveEventHandler(canvas.MouseMove)
49+
canvas.SetMouseDownEventHandler(canvas.MouseDown)
50+
canvas.SetMouseUpEventHandler(canvas.MouseUp)
5151
canvas.SetMouseWheelEventHandler(func(e *MouseWheelEvent) {
5252
work := mainWindow.workspace
5353
if e.WheelDelta > 0 {
@@ -72,8 +72,8 @@ func (canvas *DrawingCanvas) init(parent Window) {
7272
tool.keyPressEvent(&e)
7373
}
7474
})
75-
canvas.SetSetCursorEventHandler(canvas.updateCursor)
76-
canvas.SetResizeEventHandler(canvas.onResize)
75+
canvas.SetSetCursorEventHandler(canvas.UpdateCursor)
76+
canvas.SetResizeEventHandler(canvas.OnResize)
7777
logInfo("Done initializing canvas")
7878
}
7979

@@ -97,7 +97,7 @@ func (canvas *DrawingCanvas) Dispose() {
9797
}
9898
}
9999

100-
func (canvas *DrawingCanvas) updateCursor() bool {
100+
func (canvas *DrawingCanvas) UpdateCursor() bool {
101101
tool := mainWindow.tools.GetCurrentTool()
102102
if tool != nil {
103103
var winpt win.POINT
@@ -138,7 +138,7 @@ func (canvas *DrawingCanvas) NewImage(width, height int) {
138138
}
139139
canvas.image = newImage
140140
canvas.SetSize(width, height)
141-
canvas.updateStatus()
141+
canvas.UpdateStatus()
142142
}
143143

144144
func (canvas *DrawingCanvas) Resize(width, height int) {
@@ -154,7 +154,7 @@ func (canvas *DrawingCanvas) Resize(width, height int) {
154154
// We allocate new data with given new size
155155
// then we copy/draw the old data/image into it
156156
newImage := NewDrawingImage(width, height)
157-
color := getColorBackground()
157+
color := GetColorBackground()
158158
newImage.Clear(&color)
159159

160160
rect2 := canvas.image.Bounds()
@@ -168,7 +168,7 @@ func (canvas *DrawingCanvas) Resize(width, height int) {
168168
canvas.image = newImage
169169

170170
canvas.SetSize(width, height)
171-
canvas.updateStatus()
171+
canvas.UpdateStatus()
172172
}
173173

174174
func (canvas *DrawingCanvas) OpenImage(filename string) bool {
@@ -220,7 +220,7 @@ func (canvas *DrawingCanvas) OpenImage(filename string) bool {
220220
}
221221
canvas.image = newImage
222222
canvas.SetSize(width, height)
223-
canvas.updateStatus()
223+
canvas.UpdateStatus()
224224
canvas.Repaint()
225225
log.Println("Done opening image")
226226
return true
@@ -247,7 +247,7 @@ func (canvas *DrawingCanvas) SaveImage(filePath string) bool {
247247
return true
248248
}
249249

250-
func (canvas *DrawingCanvas) updateStatus() {
250+
func (canvas *DrawingCanvas) UpdateStatus() {
251251
size := canvas.GetSize()
252252
scz := mainWindow.statusCanvasSize
253253
if scz != nil {
@@ -263,7 +263,7 @@ func (canvas *DrawingCanvas) updateStatus() {
263263
}
264264
}
265265

266-
func (canvas *DrawingCanvas) updateMousePosStatus() {
266+
func (canvas *DrawingCanvas) UpdateMousePosStatus() {
267267
status := mainWindow.statusMousePos
268268
wndRect := canvas.GetWindowRect()
269269
ptScreen := app.GetCursorPos()
@@ -276,7 +276,7 @@ func (canvas *DrawingCanvas) updateMousePosStatus() {
276276
}
277277
}
278278

279-
func (canvas *DrawingCanvas) mouseDown(pt *Point, mbutton int) {
279+
func (canvas *DrawingCanvas) MouseDown(pt *Point, mbutton int) {
280280
win.SetCapture(canvas.GetHandle())
281281
tool := mainWindow.tools.GetCurrentTool()
282282
if canvas.firstMove {
@@ -297,7 +297,7 @@ func (canvas *DrawingCanvas) mouseDown(pt *Point, mbutton int) {
297297
canvas.lastPt = *pt
298298
}
299299

300-
func (canvas *DrawingCanvas) mouseUp(pt *Point, mbutton int) {
300+
func (canvas *DrawingCanvas) MouseUp(pt *Point, mbutton int) {
301301
tool := mainWindow.tools.GetCurrentTool()
302302
if canvas.firstMove {
303303
canvas.lastPt = *pt
@@ -317,7 +317,7 @@ func (canvas *DrawingCanvas) mouseUp(pt *Point, mbutton int) {
317317
win.ReleaseCapture()
318318
}
319319

320-
func (canvas *DrawingCanvas) mouseMove(mousepoint *Point, mbutton int) {
320+
func (canvas *DrawingCanvas) MouseMove(mousepoint *Point, mbutton int) {
321321
pt := *mousepoint
322322
tool := mainWindow.tools.GetCurrentTool()
323323
if canvas.firstMove {
@@ -333,12 +333,12 @@ func (canvas *DrawingCanvas) mouseMove(mousepoint *Point, mbutton int) {
333333
canvas: canvas,
334334
}
335335
tool.mouseMoveEvent(&e)
336-
canvas.updateMousePosStatus()
336+
canvas.UpdateMousePosStatus()
337337
canvas.RepaintVisible()
338338
canvas.lastPt = pt
339339
}
340340

341-
func (canvas *DrawingCanvas) onResize(rect *Rect) {
341+
func (canvas *DrawingCanvas) OnResize(rect *Rect) {
342342
logInfo("canvas resize...")
343343
if canvas.mhdc != 0 {
344344
win.DeleteDC(canvas.mhdc)
@@ -408,7 +408,7 @@ func (canvas *DrawingCanvas) RepaintVisible() {
408408
canvas.InvalidateRect(&rect, false)
409409
}
410410

411-
func (canvas *DrawingCanvas) drawGridLines(g *Graphics, rect *Rect, rcVisible *Rect) {
411+
func (canvas *DrawingCanvas) DrawGridLines(g *Graphics, rect *Rect, rcVisible *Rect) {
412412
g.SelectObject(canvas.gridPen)
413413
for x := 10; x <= rect.Right; x += 10 {
414414
if x > rcVisible.Left && x < rcVisible.Right {
@@ -422,7 +422,7 @@ func (canvas *DrawingCanvas) drawGridLines(g *Graphics, rect *Rect, rcVisible *R
422422
}
423423
}
424424

425-
func (canvas *DrawingCanvas) paint(g *Graphics, rect *Rect) {
425+
func (canvas *DrawingCanvas) Paint(g *Graphics, rect *Rect) {
426426
if canvas.context == nil {
427427
return
428428
}
@@ -440,7 +440,7 @@ func (canvas *DrawingCanvas) paint(g *Graphics, rect *Rect) {
440440
//image.memdc, rcVisible.Left, rcVisible.Top, rcVisible.Width(), rcVisible.Height())
441441

442442
if mainWindow.bShowGridlines.IsToggled() {
443-
canvas.drawGridLines(gmem, rect, &rcVisible)
443+
canvas.DrawGridLines(gmem, rect, &rcVisible)
444444
}
445445

446446
var winpt win.POINT

dialogs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ type PropertiesDialog struct {
2020

2121
func NewResizeDialog(parent Window) *ResizeDialog {
2222
dlg := &ResizeDialog{Dialog: NewDialog()}
23-
dlg.init(parent)
23+
dlg.Init(parent)
2424
return dlg
2525
}
2626

27-
func (dlg *ResizeDialog) init(parent Window) {
27+
func (dlg *ResizeDialog) Init(parent Window) {
2828
logInfo("Initialize Resize dialog...")
2929
dlg.Dialog.Initialize(parent, "Resize", 300, 320)
3030

@@ -53,11 +53,11 @@ func (dlg *ResizeDialog) init(parent Window) {
5353

5454
func NewPropertiesDialog(parent Window) *PropertiesDialog {
5555
dlg := &PropertiesDialog{Dialog: NewDialog()}
56-
dlg.init(parent)
56+
dlg.Init(parent)
5757
return dlg
5858
}
5959

60-
func (dlg *PropertiesDialog) init(parent Window) {
60+
func (dlg *PropertiesDialog) Init(parent Window) {
6161
logInfo("Initialize Properties dialog...")
6262
dlg.Dialog.Initialize(parent, "Image Properties", 340, 360)
6363

drawingimage.go

-114
Original file line numberDiff line numberDiff line change
@@ -85,40 +85,7 @@ func (image *DrawingImage) HasFilePath() bool {
8585

8686
func (image *DrawingImage) Clear(color *gdiplus.Color) {
8787
image.context.Clear(color)
88-
/*
89-
//i := 0
90-
//bitmapArray := di.Pix
91-
for y := di.Bounds().Min.Y; y != di.Bounds().Max.Y; y++ {
92-
for x := di.Bounds().Min.X; x != di.Bounds().Max.X; x++ {
93-
di.Set(x, y, c.AsRGBA())
94-
95-
//bitmapArray[i+0] = 255
96-
//bitmapArray[i+1] = 0
97-
//bitmapArray[i+2] = 0
98-
//bitmapArray[i+3] = 255
99-
//i += 4
100-
}
101-
}
102-
*/
103-
}
104-
105-
/*
106-
107-
func (image *DrawingImage) AquireData(im image.Image) {
108-
i := 0
109-
bitmapArray := image.Pix
110-
for y := im.Bounds().Min.Y; y != im.Bounds().Max.Y; y++ {
111-
for x := im.Bounds().Min.X; x != im.Bounds().Max.X; x++ {
112-
r, g, b, a := im.At(x, y).RGBA()
113-
bitmapArray[i+0] = byte(b >> 8)
114-
bitmapArray[i+1] = byte(g >> 8)
115-
bitmapArray[i+2] = byte(r >> 8)
116-
bitmapArray[i+3] = byte(a >> 8)
117-
i += 4
118-
}
119-
}
12088
}
121-
*/
12289

12390
func (image *DrawingImage) SizeOnDisk() (asString string, available bool) {
12491
if image.sizeOnDisk > 0 {
@@ -142,84 +109,3 @@ func (image *DrawingImage) LastSaved() (asString string, available bool) {
142109
}
143110
return "", false
144111
}
145-
146-
// DrawImage draws image
147-
func DrawImage(g *Graphics, image *DrawingImage, x, y int) {
148-
/*
149-
width := 100
150-
height := 100
151-
152-
var bi win.BITMAPV5HEADER
153-
bi.BiSize = uint32(unsafe.Sizeof(bi))
154-
bi.BiWidth = int32(width)
155-
bi.BiHeight = -int32(height)
156-
bi.BiPlanes = 1
157-
bi.BiBitCount = 32
158-
bi.BiCompression = win.BI_BITFIELDS
159-
// The following mask specification specifies a supported 32 BPP
160-
// alpha format for Windows XP.
161-
bi.BV4RedMask = 0x00FF0000
162-
bi.BV4GreenMask = 0x0000FF00
163-
bi.BV4BlueMask = 0x000000FF
164-
bi.BV4AlphaMask = 0xFF000000
165-
166-
hdc := win.GetDC(0)
167-
defer win.ReleaseDC(0, hdc)
168-
169-
var lpBits unsafe.Pointer
170-
// Create the DIB section with an alpha channel.
171-
hbitmap := win.CreateDIBSection(hdc, &bi.BITMAPINFOHEADER, win.DIB_RGB_COLORS, &lpBits, 0, 0)
172-
173-
bitsPerPixel := 32
174-
length := ((width*bitsPerPixel + 31) / 32) * 4 * height
175-
// Slice memory layout
176-
var sl = struct {
177-
addr uintptr
178-
len int
179-
cap int
180-
}{uintptr(lpBits), length, length}
181-
182-
// Use unsafe to turn sl into a []byte.
183-
bitmapArray := *(*[]byte)(unsafe.Pointer(&sl))
184-
185-
i := 0
186-
for y := 0; y != height; y++ {
187-
for x := 0; x != width; x++ {
188-
bitmapArray[i+3] = 255
189-
bitmapArray[i+2] = 255
190-
bitmapArray[i+1] = 0
191-
bitmapArray[i+0] = 0
192-
i += 4
193-
}
194-
}
195-
196-
memDC := win.CreateCompatibleDC(g.GetHDC())
197-
win.SelectObject(memDC, win.HGDIOBJ(hbitmap))
198-
199-
gbitmap := NewGraphics(memDC)
200-
gbitmap.DrawLine(10, 10, 100, 100, Rgb(0, 255, 0))
201-
202-
win.BitBlt(g.GetHDC(), 0, 0, int32(width), int32(height), memDC, 0, 0, win.SRCCOPY)
203-
204-
win.DeleteObject(win.HGDIOBJ(hbitmap))
205-
*/
206-
207-
var bf win.BLENDFUNCTION
208-
bf.BlendOp = AC_SRC_OVER
209-
bf.BlendFlags = 0
210-
bf.SourceConstantAlpha = 255
211-
bf.AlphaFormat = win.AC_SRC_ALPHA
212-
213-
memDC := win.CreateCompatibleDC(0)
214-
prevObj := win.SelectObject(memDC, win.HGDIOBJ(image.hbitmap))
215-
216-
width := int32(image.Width())
217-
height := int32(image.Height())
218-
219-
//win.BitBlt(g.GetHDC(), 0, 0, int32(width), int32(height), memDC, 0, 0, win.SRCCOPY)
220-
win.AlphaBlend(g.GetHDC(), int32(x), int32(y), width, height, memDC, 0, 0, width, height, bf)
221-
222-
win.SelectObject(memDC, prevObj)
223-
win.DeleteDC(memDC)
224-
225-
}

0 commit comments

Comments
 (0)