<commit_before>// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package walk

import (
	"os"
	"syscall"
	"unsafe"
)

import (
	. "walk/winapi"
	. "walk/winapi/user32"
)

type ComboBox struct {
	WidgetBase
	items                         *ComboBoxItemList
	prevSelIndex                  int
	selectedIndexChangedPublisher EventPublisher
}

func NewComboBox(parent Container) (*ComboBox, os.Error) {
	if parent == nil {
		return nil, newError("parent cannot be nil")
	}

	hWnd := CreateWindowEx(
		0, syscall.StringToUTF16Ptr("COMBOBOX"), nil,
		CBS_DROPDOWNLIST|WS_CHILD|WS_TABSTOP|WS_VISIBLE,
		0, 0, 0, 0, parent.BaseWidget().hWnd, 0, 0, nil)
	if hWnd == 0 {
		return nil, lastError("CreateWindowEx")
	}

	cb := &ComboBox{WidgetBase: WidgetBase{hWnd: hWnd, parent: parent}, prevSelIndex: -1}

	succeeded := false
	defer func() {
		if !succeeded {
			cb.Dispose()
		}
	}()

	cb.items = newComboBoxItemList(cb)

	cb.SetFont(defaultFont)

	if err := parent.Children().Add(cb); err != nil {
		return nil, err
	}

	widgetsByHWnd[hWnd] = cb

	succeeded = true
	return cb, nil
}

func (*ComboBox) LayoutFlags() LayoutFlags {
	return ShrinkHorz | GrowHorz
}

func (cb *ComboBox) PreferredSize() Size {
	return cb.dialogBaseUnitsToPixels(Size{50, 14})
}

func (cb *ComboBox) Items() *ComboBoxItemList {
	return cb.items
}

func (cb *ComboBox) SelectedIndex() int {
	return int(SendMessage(cb.hWnd, CB_GETCURSEL, 0, 0))
}

func (cb *ComboBox) SetSelectedIndex(value int) os.Error {
	index := int(SendMessage(cb.hWnd, CB_SETCURSEL, uintptr(value), 0))

	if index != value {
		return newError("invalid index")
	}

	return nil
}

func (cb *ComboBox) SelectedIndexChanged() *Event {
	return cb.selectedIndexChangedPublisher.Event()
}

func (cb *ComboBox) TextSelection() (start, end int) {
	SendMessage(cb.hWnd, CB_GETEDITSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
	return
}

func (cb *ComboBox) SetTextSelection(start, end int) {
	SendMessage(cb.hWnd, CB_SETEDITSEL, 0, uintptr(MAKELONG(uint16(start), uint16(end))))
}<commit_msg>ComboBox: Raise SelectedIndexChanged also when changed through SetSelectedIndex<commit_after>// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package walk

import (
	"os"
	"syscall"
	"unsafe"
)

import (
	. "walk/winapi"
	. "walk/winapi/user32"
)

type ComboBox struct {
	WidgetBase
	items                         *ComboBoxItemList
	prevSelIndex                  int
	selectedIndexChangedPublisher EventPublisher
}

func NewComboBox(parent Container) (*ComboBox, os.Error) {
	if parent == nil {
		return nil, newError("parent cannot be nil")
	}

	hWnd := CreateWindowEx(
		0, syscall.StringToUTF16Ptr("COMBOBOX"), nil,
		CBS_DROPDOWNLIST|WS_CHILD|WS_TABSTOP|WS_VISIBLE,
		0, 0, 0, 0, parent.BaseWidget().hWnd, 0, 0, nil)
	if hWnd == 0 {
		return nil, lastError("CreateWindowEx")
	}

	cb := &ComboBox{WidgetBase: WidgetBase{hWnd: hWnd, parent: parent}, prevSelIndex: -1}

	succeeded := false
	defer func() {
		if !succeeded {
			cb.Dispose()
		}
	}()

	cb.items = newComboBoxItemList(cb)

	cb.SetFont(defaultFont)

	if err := parent.Children().Add(cb); err != nil {
		return nil, err
	}

	widgetsByHWnd[hWnd] = cb

	succeeded = true
	return cb, nil
}

func (*ComboBox) LayoutFlags() LayoutFlags {
	return ShrinkHorz | GrowHorz
}

func (cb *ComboBox) PreferredSize() Size {
	return cb.dialogBaseUnitsToPixels(Size{50, 14})
}

func (cb *ComboBox) Items() *ComboBoxItemList {
	return cb.items
}

func (cb *ComboBox) SelectedIndex() int {
	return int(SendMessage(cb.hWnd, CB_GETCURSEL, 0, 0))
}

func (cb *ComboBox) SetSelectedIndex(value int) os.Error {
	index := int(SendMessage(cb.hWnd, CB_SETCURSEL, uintptr(value), 0))

	if index != value {
		return newError("invalid index")
	}

	if value != cb.prevSelIndex {
		cb.prevSelIndex = value
		cb.selectedIndexChangedPublisher.Publish()
	}

	return nil
}

func (cb *ComboBox) SelectedIndexChanged() *Event {
	return cb.selectedIndexChangedPublisher.Event()
}

func (cb *ComboBox) TextSelection() (start, end int) {
	SendMessage(cb.hWnd, CB_GETEDITSEL, uintptr(unsafe.Pointer(&start)), uintptr(unsafe.Pointer(&end)))
	return
}

func (cb *ComboBox) SetTextSelection(start, end int) {
	SendMessage(cb.hWnd, CB_SETEDITSEL, 0, uintptr(MAKELONG(uint16(start), uint16(end))))
}
{'fraction_non_alphanumeric': 0.1271186440677966, 'fraction_numerical': 0.012934879571810883, 'mean_word_length': 4.34564958283671, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2454007', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1580, 'n_words': 405}