More go fmt. That completes the main package go fmt. Won't bother with test or tools.

This commit is contained in:
Pietro Gagliardi 2014-06-10 16:03:10 -04:00
parent a210775287
commit 5fa1bd22e2
2 changed files with 68 additions and 66 deletions

94
area.go
View File

@ -4,10 +4,10 @@ package ui
import ( import (
"fmt" "fmt"
"sync"
"image" "image"
"unsafe"
"reflect" "reflect"
"sync"
"unsafe"
) )
// Area represents a blank canvas upon which programs may draw anything and receive arbitrary events from the user. // Area represents a blank canvas upon which programs may draw anything and receive arbitrary events from the user.
@ -28,12 +28,12 @@ import (
// to lead to trouble. // to lead to trouble.
// [FOR FUTURE PLANNING Use TextArea instead, providing a TextAreaHandler.] // [FOR FUTURE PLANNING Use TextArea instead, providing a TextAreaHandler.]
type Area struct { type Area struct {
lock sync.Mutex lock sync.Mutex
created bool created bool
sysData *sysData sysData *sysData
handler AreaHandler handler AreaHandler
initwidth int initwidth int
initheight int initheight int
} }
// AreaHandler represents the events that an Area should respond to. // AreaHandler represents the events that an Area should respond to.
@ -82,25 +82,25 @@ type AreaHandler interface {
// If the user clicked on the Area to switch to the Window it is contained in from another window in the OS, the Area will receive a MouseEvent for that click. // If the user clicked on the Area to switch to the Window it is contained in from another window in the OS, the Area will receive a MouseEvent for that click.
type MouseEvent struct { type MouseEvent struct {
// Pos is the position of the mouse in the Area at the time of the event. // Pos is the position of the mouse in the Area at the time of the event.
Pos image.Point Pos image.Point
// If the event was generated by a mouse button being pressed, Down contains the ID of that button. // If the event was generated by a mouse button being pressed, Down contains the ID of that button.
// Otherwise, Down contains 0. // Otherwise, Down contains 0.
// If Down contains nonzero, the Area will also receive keyboard focus. // If Down contains nonzero, the Area will also receive keyboard focus.
Down uint Down uint
// If the event was generated by a mouse button being released, Up contains the ID of that button. // If the event was generated by a mouse button being released, Up contains the ID of that button.
// Otherwise, Up contains 0. // Otherwise, Up contains 0.
// If both Down and Up are 0, the event represents mouse movement (with optional held buttons for dragging; see below). // If both Down and Up are 0, the event represents mouse movement (with optional held buttons for dragging; see below).
// Down and Up shall not both be nonzero. // Down and Up shall not both be nonzero.
Up uint Up uint
// If Down is nonzero, Count indicates the number of clicks: 1 for single-click, 2 for double-click, 3 for triple-click, and so on. // If Down is nonzero, Count indicates the number of clicks: 1 for single-click, 2 for double-click, 3 for triple-click, and so on.
// The order of events will be Down:Count=1 -> Up -> Down:Count=2 -> Up -> Down:Count=3 -> Up -> ... // The order of events will be Down:Count=1 -> Up -> Down:Count=2 -> Up -> Down:Count=3 -> Up -> ...
Count uint Count uint
// Modifiers is a bit mask indicating the modifier keys being held during the event. // Modifiers is a bit mask indicating the modifier keys being held during the event.
Modifiers Modifiers Modifiers Modifiers
// Held is a slice of button IDs that indicate which mouse buttons are being held during the event. // Held is a slice of button IDs that indicate which mouse buttons are being held during the event.
// Held will not include Down and Up. // Held will not include Down and Up.
@ -111,7 +111,7 @@ type MouseEvent struct {
// Whether or not a drag into an Area generates MouseEvents is implementation-defined. // Whether or not a drag into an Area generates MouseEvents is implementation-defined.
// Whether or not a drag over an Area when the program is inactive generates MouseEvents is also implementation-defined. // Whether or not a drag over an Area when the program is inactive generates MouseEvents is also implementation-defined.
// Moving the mouse over an Area when the program is inactive and no buttons are held will, however, generate MouseEvents. // Moving the mouse over an Area when the program is inactive and no buttons are held will, however, generate MouseEvents.
Held []uint Held []uint
} }
// HeldBits returns Held as a bit mask. // HeldBits returns Held as a bit mask.
@ -156,24 +156,24 @@ type KeyEvent struct {
// - '\n' if the typewriter Enter key was pressed // - '\n' if the typewriter Enter key was pressed
// - '\b' if the typewriter Backspace key was pressed // - '\b' if the typewriter Backspace key was pressed
// If this value is zero, see ExtKey. // If this value is zero, see ExtKey.
Key byte Key byte
// If Key is zero, ExtKey contains a predeclared identifier // If Key is zero, ExtKey contains a predeclared identifier
// naming an extended key. See ExtKey for details. // naming an extended key. See ExtKey for details.
// If both Key and ExtKey are zero, a Modifier by itself // If both Key and ExtKey are zero, a Modifier by itself
// was pressed. Key and ExtKey will not both be nonzero. // was pressed. Key and ExtKey will not both be nonzero.
ExtKey ExtKey ExtKey ExtKey
// If both Key and ExtKey are zero, Modifier will contain exactly one of its bits set, indicating which Modifier was pressed or released. // If both Key and ExtKey are zero, Modifier will contain exactly one of its bits set, indicating which Modifier was pressed or released.
// As with Modifiers itself, there is no way to differentiate between left and right modifier keys. // As with Modifiers itself, there is no way to differentiate between left and right modifier keys.
// As such, the result of pressing and/or releasing both left and right of the same Modifier is system-defined. // As such, the result of pressing and/or releasing both left and right of the same Modifier is system-defined.
// Furthermore, the result of holding down a Key or ExtKey, then pressing a Modifier, and then releasing the original key is system-defined. // Furthermore, the result of holding down a Key or ExtKey, then pressing a Modifier, and then releasing the original key is system-defined.
// Under no condition shall Key, ExtKey, AND Modifier all be zero. // Under no condition shall Key, ExtKey, AND Modifier all be zero.
Modifier Modifiers Modifier Modifiers
// Modifiers contains all the modifier keys currently being held at the time of the KeyEvent. // Modifiers contains all the modifier keys currently being held at the time of the KeyEvent.
// If Modifier is nonzero, Modifiers will not contain Modifier itself. // If Modifier is nonzero, Modifiers will not contain Modifier itself.
Modifiers Modifiers Modifiers Modifiers
// If Up is true, the key was released; if not, the key was pressed. // If Up is true, the key was released; if not, the key was pressed.
// There is no guarantee that all pressed keys shall have // There is no guarantee that all pressed keys shall have
@ -181,14 +181,15 @@ type KeyEvent struct {
// programs while holding the key down, then releases the key). // programs while holding the key down, then releases the key).
// Keys that have been held down are reported as multiple // Keys that have been held down are reported as multiple
// key press events. // key press events.
Up bool Up bool
} }
// ExtKey represents keys that are not in the typewriter section of the keyboard. // ExtKey represents keys that are not in the typewriter section of the keyboard.
type ExtKey uintptr type ExtKey uintptr
const ( const (
Escape ExtKey = iota + 1 Escape ExtKey = iota + 1
Insert // equivalent to "Help" on Apple keyboards Insert // equivalent to "Help" on Apple keyboards
Delete Delete
Home Home
End End
@ -198,7 +199,7 @@ const (
Down Down
Left Left
Right Right
F1 // F1..F12 are guaranteed to be consecutive F1 // F1..F12 are guaranteed to be consecutive
F2 F2
F3 F3
F4 F4
@ -210,8 +211,8 @@ const (
F10 F10
F11 F11
F12 F12
N0 // numpad keys; independent of Num Lock state N0 // numpad keys; independent of Num Lock state
N1 // N0..N9 are guaranteed to be consecutive N1 // N0..N9 are guaranteed to be consecutive
N2 N2
N3 N3
N4 N4
@ -226,7 +227,7 @@ const (
NSubtract NSubtract
NMultiply NMultiply
NDivide NDivide
_nextkeys // for sanity check _nextkeys // for sanity check
) )
// EffectiveKey returns e.Key if it is set. // EffectiveKey returns e.Key if it is set.
@ -241,7 +242,7 @@ func (e KeyEvent) EffectiveKey() byte {
k := e.ExtKey k := e.ExtKey
switch { switch {
case k >= N0 && k <= N9: case k >= N0 && k <= N9:
return byte(k - N0) + '0' return byte(k-N0) + '0'
case k == NDot: case k == NDot:
return '.' return '.'
case k == NEnter: case k == NEnter:
@ -262,11 +263,12 @@ func (e KeyEvent) EffectiveKey() byte {
// There is no way to differentiate between left and right modifier keys. // There is no way to differentiate between left and right modifier keys.
// As such, what KeyEvents get sent if the user does something unusual with both of a certain modifier key at once is undefined. // As such, what KeyEvents get sent if the user does something unusual with both of a certain modifier key at once is undefined.
type Modifiers uintptr type Modifiers uintptr
const ( const (
Ctrl Modifiers = 1 << iota // the keys labelled Ctrl or Control on all platforms Ctrl Modifiers = 1 << iota // the keys labelled Ctrl or Control on all platforms
Alt // the keys labelled Alt or Option or Meta on all platforms Alt // the keys labelled Alt or Option or Meta on all platforms
Shift // the Shift keys Shift // the Shift keys
Super // the Super keys on platforms that have one, or the Windows keys on Windows, or the Command keys on Mac OS X Super // the Super keys on platforms that have one, or the Windows keys on Windows, or the Command keys on Mac OS X
) )
func checkAreaSize(width int, height int, which string) { func checkAreaSize(width int, height int, which string) {
@ -283,10 +285,10 @@ func NewArea(width int, height int, handler AreaHandler) *Area {
panic("handler passed to NewArea() must not be nil") panic("handler passed to NewArea() must not be nil")
} }
return &Area{ return &Area{
sysData: mksysdata(c_area), sysData: mksysdata(c_area),
handler: handler, handler: handler,
initwidth: width, initwidth: width,
initheight: height, initheight: height,
} }
} }
@ -336,11 +338,11 @@ func (a *Area) make(window *sysData) error {
func (a *Area) setRect(x int, y int, width int, height int, rr *[]resizerequest) { func (a *Area) setRect(x int, y int, width int, height int, rr *[]resizerequest) {
*rr = append(*rr, resizerequest{ *rr = append(*rr, resizerequest{
sysData: a.sysData, sysData: a.sysData,
x: x, x: x,
y: y, y: y,
width: width, width: width,
height: height, height: height,
}) })
} }
@ -373,16 +375,16 @@ func toARGB(i *image.RGBA, memory uintptr, memstride int) {
nextp := p + i.Stride nextp := p + i.Stride
nextq := q + memstride nextq := q + memstride
for x := i.Rect.Min.X; x < i.Rect.Max.X; x++ { for x := i.Rect.Min.X; x < i.Rect.Max.X; x++ {
argb := uint32(i.Pix[p + 3]) << 24 // A argb := uint32(i.Pix[p+3]) << 24 // A
argb |= uint32(i.Pix[p + 0]) << 16 // R argb |= uint32(i.Pix[p+0]) << 16 // R
argb |= uint32(i.Pix[p + 1]) << 8 // G argb |= uint32(i.Pix[p+1]) << 8 // G
argb |= uint32(i.Pix[p + 2]) // B argb |= uint32(i.Pix[p+2]) // B
// the magic of conversion // the magic of conversion
native := (*[4]byte)(unsafe.Pointer(&argb)) native := (*[4]byte)(unsafe.Pointer(&argb))
realbits[q + 0] = native[0] realbits[q+0] = native[0]
realbits[q + 1] = native[1] realbits[q+1] = native[1]
realbits[q + 2] = native[2] realbits[q+2] = native[2]
realbits[q + 3] = native[3] realbits[q+3] = native[3]
p += 4 p += 4
q += 4 q += 4
} }

View File

@ -3,8 +3,8 @@
package ui package ui
import ( import (
"unsafe"
"image" "image"
"unsafe"
) )
// #include <stdlib.h> // #include <stdlib.h>
@ -28,10 +28,10 @@ func areaView_drawRect(self C.id, rect C.struct_xrect) {
s := getSysData(self) s := getSysData(self)
// no need to clear the clip rect; the NSScrollView does that for us (see the setDrawsBackground: call in objc_darwin.m) // no need to clear the clip rect; the NSScrollView does that for us (see the setDrawsBackground: call in objc_darwin.m)
// rectangles in Cocoa are origin/size, not point0/point1; if we don't watch for this, weird things will happen when scrolling // rectangles in Cocoa are origin/size, not point0/point1; if we don't watch for this, weird things will happen when scrolling
cliprect := image.Rect(int(rect.x), int(rect.y), int(rect.x + rect.width), int(rect.y + rect.height)) cliprect := image.Rect(int(rect.x), int(rect.y), int(rect.x+rect.width), int(rect.y+rect.height))
max := C.frame(self) max := C.frame(self)
cliprect = image.Rect(0, 0, int(max.width), int(max.height)).Intersect(cliprect) cliprect = image.Rect(0, 0, int(max.width), int(max.height)).Intersect(cliprect)
if cliprect.Empty() { // no intersection; nothing to paint if cliprect.Empty() { // no intersection; nothing to paint
return return
} }
i := s.handler.Paint(cliprect) i := s.handler.Paint(cliprect)
@ -42,10 +42,10 @@ func areaView_drawRect(self C.id, rect C.struct_xrect) {
func parseModifiers(e C.id) (m Modifiers) { func parseModifiers(e C.id) (m Modifiers) {
const ( const (
_NSShiftKeyMask = 1 << 17 _NSShiftKeyMask = 1 << 17
_NSControlKeyMask = 1 << 18 _NSControlKeyMask = 1 << 18
_NSAlternateKeyMask = 1 << 19 _NSAlternateKeyMask = 1 << 19
_NSCommandKeyMask = 1 << 20 _NSCommandKeyMask = 1 << 20
) )
mods := uintptr(C.modifierFlags(e)) mods := uintptr(C.modifierFlags(e))
@ -77,7 +77,7 @@ func areaMouseEvent(self C.id, e C.id, click bool, up bool) {
} }
me.Modifiers = parseModifiers(e) me.Modifiers = parseModifiers(e)
which := uint(C.buttonNumber(e)) + 1 which := uint(C.buttonNumber(e)) + 1
if which == 3 { // swap middle and right button numbers if which == 3 { // swap middle and right button numbers
which = 2 which = 2
} else if which == 2 { } else if which == 2 {
which = 3 which = 3
@ -89,22 +89,22 @@ func areaMouseEvent(self C.id, e C.id, click bool, up bool) {
// this already works the way we want it to so nothing special needed like with Windows and GTK+ // this already works the way we want it to so nothing special needed like with Windows and GTK+
me.Count = uint(C.clickCount(e)) me.Count = uint(C.clickCount(e))
} else { } else {
which = 0 // reset for Held processing below which = 0 // reset for Held processing below
} }
// the docs do say don't use this for tracking (mouseMoved:) since it returns the state now, and mouse move events work by tracking, but as far as I can tell dragging the mouse over the inactive window does not generate an event on Mac OS X, so :/ (tracking doesn't touch dragging anyway except during mouseEntered: and mouseExited:, which we don't handle, and the only other tracking message, cursorChanged:, we also don't handle (yet...? need to figure out if this is how to set custom cursors or not), so) // the docs do say don't use this for tracking (mouseMoved:) since it returns the state now, and mouse move events work by tracking, but as far as I can tell dragging the mouse over the inactive window does not generate an event on Mac OS X, so :/ (tracking doesn't touch dragging anyway except during mouseEntered: and mouseExited:, which we don't handle, and the only other tracking message, cursorChanged:, we also don't handle (yet...? need to figure out if this is how to set custom cursors or not), so)
held := C.pressedMouseButtons() held := C.pressedMouseButtons()
if which != 1 && (held & 1) != 0 { // button 1 if which != 1 && (held&1) != 0 { // button 1
me.Held = append(me.Held, 1) me.Held = append(me.Held, 1)
} }
if which != 2 && (held & 4) != 0 { // button 2; mind the swap if which != 2 && (held&4) != 0 { // button 2; mind the swap
me.Held = append(me.Held, 2) me.Held = append(me.Held, 2)
} }
if which != 3 && (held & 2) != 0 { // button 3 if which != 3 && (held&2) != 0 { // button 3
me.Held = append(me.Held, 3) me.Held = append(me.Held, 3)
} }
held >>= 3 held >>= 3
for i := uint(4); held != 0; i++ { for i := uint(4); held != 0; i++ {
if which != i && (held & 1) != 0 { if which != i && (held&1) != 0 {
me.Held = append(me.Held, i) me.Held = append(me.Held, i)
} }
held >>= 1 held >>= 1
@ -173,8 +173,8 @@ func areaView_flagsChanged(self C.id, e C.id) {
// Mac OS X sends this event on both key up and key down. // Mac OS X sends this event on both key up and key down.
// Fortunately -[e keyCode] IS valid here, so we can simply map from key code to Modifiers, get the value of [e modifierFlags], and check if the respective bit is set or not — that will give us the up/down state // Fortunately -[e keyCode] IS valid here, so we can simply map from key code to Modifiers, get the value of [e modifierFlags], and check if the respective bit is set or not — that will give us the up/down state
keyCode := uintptr(C.keyCode(e)) keyCode := uintptr(C.keyCode(e))
mod, ok := keycodeModifiers[keyCode] // comma-ok form to avoid adding entries mod, ok := keycodeModifiers[keyCode] // comma-ok form to avoid adding entries
if !ok { // unknown modifier; ignore if !ok { // unknown modifier; ignore
return return
} }
ke.Modifiers = parseModifiers(e) ke.Modifiers = parseModifiers(e)