Implemented TextField.OnChanged() on Windows.

This commit is contained in:
Pietro Gagliardi 2014-08-20 12:04:36 -04:00
parent ac1b8e3cca
commit e06e327155
3 changed files with 50 additions and 1 deletions

View File

@ -78,3 +78,29 @@ void checkboxSetChecked(HWND hwnd, BOOL c)
check = BST_UNCHECKED;
SendMessage(hwnd, BM_SETCHECK, check, 0);
}
static LRESULT CALLBACK textfieldSubProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR id, DWORD_PTR data)
{
switch (uMsg) {
case msgCOMMAND:
if (HIWORD(wParam) == EN_CHANGE) {
textfieldChanged((void *) data);
return 0;
}
return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
case WM_NCDESTROY:
if ((*fv_RemoveWindowSubclass)(hwnd, textfieldSubProc, id) == FALSE)
xpanic("error removing TextField subclass (which was for its own event handler)", GetLastError());
return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
default:
return (*fv_DefSubclassProc)(hwnd, uMsg, wParam, lParam);
}
xmissedmsg("TextField", "textfieldSubProc()", uMsg);
return 0; // unreached
}
void setTextFieldSubclass(HWND hwnd, void *data)
{
if ((*fv_SetWindowSubclass)(hwnd, textfieldSubProc, 0, (DWORD_PTR) data) == FALSE)
xpanic("error subclassing TextField to give it its own event handler", GetLastError());
}

View File

@ -2,12 +2,17 @@
package ui
import (
"unsafe"
)
// #include "winapi_windows.h"
import "C"
type textfield struct {
_hwnd C.HWND
_textlen C.LONG
changed *event
}
var editclass = toUTF16("EDIT")
@ -17,9 +22,11 @@ func startNewTextField(style C.DWORD) *textfield {
style | C.ES_AUTOHSCROLL | C.ES_LEFT | C.ES_NOHIDESEL | C.WS_TABSTOP,
C.WS_EX_CLIENTEDGE) // WS_EX_CLIENTEDGE without WS_BORDER will show the canonical visual styles border (thanks to MindChild in irc.efnet.net/#winprog)
t := &textfield{
_hwnd: hwnd,
_hwnd: hwnd,
changed: newEvent(),
}
C.controlSetControlFont(t._hwnd)
C.setTextFieldSubclass(t._hwnd, unsafe.Pointer(t))
return t
}
@ -39,6 +46,21 @@ func (t *textfield) SetText(text string) {
baseSetText(t, text)
}
func (t *textfield) OnChanged(f func()) {
t.changed.set(f)
}
func (t *textfield) Invalid(reason string) {
// TODO
}
//export textfieldChanged
func textfieldChanged(data unsafe.Pointer) {
t := (*textfield)(data)
println("changed")
t.changed.fire()
}
func (t *textfield) hwnd() C.HWND {
return t._hwnd
}

View File

@ -63,6 +63,7 @@ extern void setButtonSubclass(HWND, void *);
extern void setCheckboxSubclass(HWND, void *);
extern BOOL checkboxChecked(HWND);
extern void checkboxSetChecked(HWND, BOOL);
extern void setTextFieldSubclass(HWND, void *);
// init_windows.c
extern HINSTANCE hInstance;