#!/boot/home/config/bin/python

import os
import sys

import BApplication
from BStringItem import BStringItem
from BListView import BListView
from BScrollView import BScrollView
from BWindow import BWindow
from BMessage import BMessage

from InterfaceKit import B_FOLLOW_ALL,B_TITLED_WINDOW,B_WILL_DRAW,B_NOT_RESIZABLE,B_NOT_ZOOMABLE,B_FOLLOW_LEFT,B_FOLLOW_TOP,B_FANCY_BORDER,B_SINGLE_SELECTION_LIST,B_MULTIPLE_SELECTION_LIST

from AppKit import B_QUIT_REQUESTED

#  Note that this isn't subclass, it's just a place to put code
#  and data associated with this scroll/list view.  If it needed
#  to intercept some virtual callback function associated with the
#  BScrollView, it would inherit from it.
#
class ScrollView:
	HiWhat = 32
	def __init__(self, rect, name):
		self.lv = BListView(rect, name, B_MULTIPLE_SELECTION_LIST,
			B_FOLLOW_ALL)
		for i in os.listdir('.'):
			item = BStringItem(i)
			self.lv.AddItem(item)
		msg = BMessage(self.HiWhat)
		self.lv.SetInvocationMessage(msg)
		self.sv = BScrollView('ScrollView', self.lv, B_FOLLOW_ALL,
			B_WILL_DRAW, 0, 1, B_FANCY_BORDER)
	def topview(self):
		return self.sv
	def listview(self):
		return self.lv
	def checkup(self, msg):
		count = self.lv.CountItems()
		i = 0
		while i < count:
			c = self.lv.CurrentSelection(i)
			if c >= 0:
				t = self.lv.ItemAt(c)
				print c, t, ':', t.Text()
				#
				#  NB, "i" is not an index into the
				#  list, like "c" is.  We don't search
				#  from "c + 1" in the list - we search
				#  for selected item "i + 1"
				#
				i = i + 1
			else:
				break

class ScrollWindow(BWindow):
	def __init__(self, frame):
		BWindow.__init__(self, frame, 'Scroll', B_TITLED_WINDOW,
			B_NOT_ZOOMABLE)
		l, t, r, b = self.Bounds()
		self.view = ScrollView((l, t, r - 14.0, b), 'ScrollView')
		self.AddChild(self.view.topview())
		#  PgUp/Dn don't work scrollbars until BListView has focus.
		self.view.listview().MakeFocus(1)
	def MessageReceived(self, msg):
		if msg.what == self.view.HiWhat:
			self.view.checkup(msg)
		else:
			BWindow.MessageReceived(self, msg)
	def QuitRequested(self):
		BApplication.be_app.PostMessage(B_QUIT_REQUESTED)
		return 1

class ScrollApplication(BApplication.BApplication):
	def __init__(self):
		BApplication.BApplication.__init__(self, "application/x-vnd.Be-ScrollWorldSample")
	def ReadyToRun(self):
		window = ScrollWindow((100.0, 80.0, 460.0, 320.0))
		window.Show()

myApplication = ScrollApplication()
myApplication.Run()
