Skip to content

Commit

Permalink
Introduce Window and WindowBase as base for all windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Jul 24, 2013
1 parent 63ad6a7 commit 8681459
Show file tree
Hide file tree
Showing 47 changed files with 1,759 additions and 1,628 deletions.
37 changes: 20 additions & 17 deletions boxlayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ const (
)

type BoxLayout struct {
container Container
margins Margins
spacing int
orientation Orientation
widget2StretchFactor map[*WidgetBase]int
resetNeeded bool
container Container
margins Margins
spacing int
orientation Orientation
hwnd2StretchFactor map[HWND]int
resetNeeded bool
}

func newBoxLayout(orientation Orientation) *BoxLayout {
return &BoxLayout{
orientation: orientation,
widget2StretchFactor: make(map[*WidgetBase]int),
orientation: orientation,
hwnd2StretchFactor: make(map[HWND]int),
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func (l *BoxLayout) SetSpacing(value int) error {
}

func (l *BoxLayout) StretchFactor(widget Widget) int {
if factor, ok := l.widget2StretchFactor[widget.BaseWidget()]; ok {
if factor, ok := l.hwnd2StretchFactor[widget.Handle()]; ok {
return factor
}

Expand All @@ -127,14 +127,17 @@ func (l *BoxLayout) SetStretchFactor(widget Widget, factor int) error {
if l.container == nil {
return newError("container required")
}
if !l.container.Children().containsHandle(widget.BaseWidget().hWnd) {

handle := widget.Handle()

if !l.container.Children().containsHandle(handle) {
return newError("unknown widget")
}
if factor < 1 {
return newError("factor must be >= 1")
}

l.widget2StretchFactor[widget.BaseWidget()] = factor
l.hwnd2StretchFactor[handle] = factor

l.Update(false)
}
Expand All @@ -145,9 +148,9 @@ func (l *BoxLayout) SetStretchFactor(widget Widget, factor int) error {
func (l *BoxLayout) cleanupStretchFactors() {
widgets := l.container.Children()

for widget, _ := range l.widget2StretchFactor {
if !widgets.containsHandle(widget.BaseWidget().hWnd) {
delete(l.widget2StretchFactor, widget.BaseWidget())
for handle, _ := range l.hwnd2StretchFactor {
if !widgets.containsHandle(handle) {
delete(l.hwnd2StretchFactor, handle)
}
}
}
Expand Down Expand Up @@ -272,7 +275,7 @@ func (l *BoxLayout) MinSize() Size {
var s Size

for _, widget := range widgets {
min := widget.BaseWidget().minSizeEffective()
min := minSizeEffective(widget)

if l.orientation == Horizontal {
s.Width += min.Width
Expand Down Expand Up @@ -333,7 +336,7 @@ func (l *BoxLayout) Update(reset bool) error {
sortedWidgetInfo := widgetInfoList(make([]widgetInfo, len(widgets)))

for i, widget := range widgets {
sf := l.widget2StretchFactor[widget.BaseWidget()]
sf := l.hwnd2StretchFactor[widget.Handle()]
if sf == 0 {
sf = 1
}
Expand Down Expand Up @@ -485,7 +488,7 @@ func (l *BoxLayout) Update(reset bool) error {

if hdwp = DeferWindowPos(
hdwp,
widget.BaseWidget().hWnd,
widget.Handle(),
0,
int32(x),
int32(y),
Expand Down
4 changes: 2 additions & 2 deletions button.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func (b *Button) init() {
}

func (b *Button) Text() string {
return widgetText(b.hWnd)
return windowText(b.hWnd)
}

func (b *Button) SetText(value string) error {
if value == b.Text() {
return nil
}

if err := setWidgetText(b.hWnd, value); err != nil {
if err := setWindowText(b.hWnd, value); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type CheckBox struct {
func NewCheckBox(parent Container) (*CheckBox, error) {
cb := &CheckBox{}

if err := InitChildWidget(
if err := InitWidget(
cb,
parent,
"BUTTON",
Expand All @@ -33,7 +33,7 @@ func (*CheckBox) LayoutFlags() LayoutFlags {

func (cb *CheckBox) MinSizeHint() Size {
defaultSize := cb.dialogBaseUnitsToPixels(Size{50, 10})
textSize := cb.calculateTextSizeImpl("n" + widgetText(cb.hWnd))
textSize := cb.calculateTextSizeImpl("n" + windowText(cb.hWnd))

// FIXME: Use GetThemePartSize instead of GetSystemMetrics?
w := textSize.Width + int(GetSystemMetrics(SM_CXMENUCHECK))
Expand Down
10 changes: 5 additions & 5 deletions combobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func comboBoxEditWndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr

switch msg {
case WM_GETDLGCODE:
if root := rootWidget(cb); root != nil {
if dlg, ok := root.(dialogish); ok {
if form := ancestor(cb); form != nil {
if dlg, ok := form.(dialogish); ok {
if dlg.DefaultButton() != nil {
// If the ComboBox lives in a Dialog that has a
// DefaultButton, we won't swallow the return key.
Expand Down Expand Up @@ -88,7 +88,7 @@ func NewDropDownBox(parent Container) (*ComboBox, error) {
func newComboBoxWithStyle(parent Container, style uint32) (*ComboBox, error) {
cb := &ComboBox{prevCurIndex: -1, selChangeIndex: -1, precision: 2}

if err := InitChildWidget(
if err := InitWidget(
cb,
parent,
"COMBOBOX",
Expand Down Expand Up @@ -465,11 +465,11 @@ func (cb *ComboBox) CurrentIndexChanged() *Event {
}

func (cb *ComboBox) Text() string {
return widgetText(cb.hWnd)
return windowText(cb.hWnd)
}

func (cb *ComboBox) SetText(value string) error {
return setWidgetText(cb.hWnd, value)
return setWindowText(cb.hWnd, value)
}

func (cb *ComboBox) TextSelection() (start, end int) {
Expand Down
7 changes: 4 additions & 3 deletions composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ type Composite struct {
ContainerBase
}

func newCompositeWithStyle(parent Widget, style uint32) (*Composite, error) {
c := &Composite{}
func newCompositeWithStyle(parent Window, style uint32) (*Composite, error) {
c := new(Composite)
c.container = c
c.children = newWidgetList(c)
c.SetPersistent(true)

if err := InitChildWidget(
if err := InitWidget(
c,
parent,
compositeWindowClass,
Expand Down
69 changes: 52 additions & 17 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,31 @@ func shouldLayoutWidget(widget Widget) bool {

_, isSpacer := widget.(*Spacer)

return isSpacer || widget.BaseWidget().visible
return isSpacer || widget.AsWindowBase().visible
}

func DescendantByName(container Container, name string) Widget {
var widget Widget

walkDescendants(container.AsContainerBase(), func(w Widget) bool {
if w.Name() == name {
widget = w
return false
}

return true
})

if widget == nil {
return nil
}

return widget
}

type Container interface {
Widget
Window
AsContainerBase() *ContainerBase
Children() *WidgetList
Layout() Layout
SetLayout(value Layout) error
Expand All @@ -45,12 +65,27 @@ type Container interface {

type ContainerBase struct {
WidgetBase
container Container
layout Layout
children *WidgetList
dataBinder *DataBinder
persistent bool
}

func (cb *ContainerBase) init(container Container) error {
cb.container = container

return nil
}

func (cb *ContainerBase) AsWidgetBase() *WidgetBase {
return &cb.WidgetBase
}

func (cb *ContainerBase) AsContainerBase() *ContainerBase {
return cb
}

func (cb *ContainerBase) LayoutFlags() LayoutFlags {
if cb.layout == nil {
return 0
Expand Down Expand Up @@ -126,15 +161,15 @@ func (cb *ContainerBase) SetDataBinder(db *DataBinder) {
var boundWidgets []Widget

walkDescendants(cb.widget, func(w Widget) bool {
if w.BaseWidget().Handle() == cb.hWnd {
if w.Handle() == cb.hWnd {
return true
}

if c, ok := w.(Container); ok && c.DataBinder() != nil {
return false
}

for _, prop := range w.BaseWidget().name2Property {
for _, prop := range w.AsWindowBase().name2Property {
if _, ok := prop.Source().(string); ok {
boundWidgets = append(boundWidgets, w)
break
Expand Down Expand Up @@ -203,12 +238,12 @@ func (cb *ContainerBase) WndProc(hwnd HWND, msg uint32, wParam, lParam uintptr)
cmdId := LOWORD(uint32(wParam))
switch cmdId {
case IDOK, IDCANCEL:
root := rootWidget(cb)
if root == nil {
form := ancestor(cb)
if form == nil {
break
}

dlg, ok := root.(dialogish)
dlg, ok := form.(dialogish)
if !ok {
break
}
Expand Down Expand Up @@ -238,19 +273,19 @@ func (cb *ContainerBase) WndProc(hwnd HWND, msg uint32, wParam, lParam uintptr)
// Accelerator
}
} else {
// The widget that sent the notification shall handle it itself.
// The window that sent the notification shall handle it itself.
hWnd := HWND(lParam)
if widget := widgetFromHWND(hWnd); widget != nil {
widget.WndProc(hwnd, msg, wParam, lParam)
if window := windowFromHandle(hWnd); window != nil {
window.WndProc(hwnd, msg, wParam, lParam)
return 0
}
}

case WM_NOTIFY:
nmh := (*NMHDR)(unsafe.Pointer(lParam))
if widget := widgetFromHWND(nmh.HwndFrom); widget != nil {
// The widget that sent the notification shall handle it itself.
return widget.WndProc(hwnd, msg, wParam, lParam)
if window := windowFromHandle(nmh.HwndFrom); window != nil {
// The window that sent the notification shall handle it itself.
return window.WndProc(hwnd, msg, wParam, lParam)
}

case WM_SIZE, WM_SIZING:
Expand All @@ -267,8 +302,8 @@ func (cb *ContainerBase) onInsertingWidget(index int, widget Widget) (err error)
}

func (cb *ContainerBase) onInsertedWidget(index int, widget Widget) (err error) {
if parent := widget.Parent(); parent == nil || parent.BaseWidget().hWnd != cb.hWnd {
err = widget.SetParent(cb.widget.(Container))
if parent := widget.Parent(); parent == nil || parent.Handle() != cb.hWnd {
err = widget.SetParent(cb.container)
if err != nil {
return
}
Expand All @@ -286,7 +321,7 @@ func (cb *ContainerBase) onRemovingWidget(index int, widget Widget) (err error)
return
}

if widget.Parent().BaseWidget().hWnd == cb.hWnd {
if widget.Parent().Handle() == cb.hWnd {
err = widget.SetParent(nil)
}

Expand All @@ -303,7 +338,7 @@ func (cb *ContainerBase) onRemovedWidget(index int, widget Widget) (err error) {

func (cb *ContainerBase) onClearingWidgets() (err error) {
for _, widget := range cb.children.items {
if widget.Parent().BaseWidget().hWnd == cb.hWnd {
if widget.Parent().Handle() == cb.hWnd {
if err = widget.SetParent(nil); err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion customwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type CustomWidget struct {
func NewCustomWidget(parent Container, style uint, paint PaintFunc) (*CustomWidget, error) {
cw := &CustomWidget{paint: paint}

if err := InitChildWidget(
if err := InitWidget(
cw,
parent,
customWidgetWindowClass,
Expand Down
2 changes: 1 addition & 1 deletion databinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (db *DataBinder) SetBoundWidgets(boundWidgets []Widget) {
for _, widget := range boundWidgets {
widget := widget

for _, prop := range widget.BaseWidget().name2Property {
for _, prop := range widget.AsWindowBase().name2Property {
prop := prop
if _, ok := prop.Source().(string); !ok {
continue
Expand Down
2 changes: 1 addition & 1 deletion dateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DateEdit struct {
func newDateEdit(parent Container, style uint32) (*DateEdit, error) {
de := &DateEdit{}

if err := InitChildWidget(
if err := InitWidget(
de,
parent,
"SysDateTimePick32",
Expand Down
Loading

0 comments on commit 8681459

Please sign in to comment.