[futurebasic] Re: [FB] Trouble using DataBrowser For Dummies with FBtoC

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : January 2008 : Group Archive : Group : All Groups

From: Joe Lertola <joefb@...>
Date: Fri, 11 Jan 2008 23:48:57 -0500
On Jan 11, 2008, at 10:06 AM, Bernie wrote:

>
> I said:
>
>> Until I get my brain into browser gear, here's a workaround. Add  
>> the following function to DBData.incl (below the Populate Browser  
>> bookmark may be a good place):
>>
>> < ... >
>>
>
> 1st gear: More efficient and more meaningful function name.




Thanks Brenie,

Below is the demo with your fix incorporated.

One other question about DataBrowsers: Is it safe to close a window  
that contains a DataBrowser or is there some kind of clean up that  
must be done to prevent a memory leak?

Thanks again,
-Joe

include "DBData.incl"
'key points marked in the code below
'< 1 > React to click and double click
'< 2 > Programatically select an item in the browser
'< 3 > Replace browser with new content
'< 4 > Find the row number of the selected item

begin globals
dim as ControlRef @gBrowser
end globals

begin enum 1
_Untitled1Wnd
end enum

begin enum 1
_cMyEdit
_cResetDataBrowserBtn
_tellMeBtn
end enum

// cBrowser propertyIDs
_cBrowserC001 = _"C001"

'~'5
'this function returns the row of the first selected
'item in the dataBrowser or zero if none are selected
local fn getFirstSelectedRow( browser as HIViewRef )

dim as OSErr err
dim as unsigned long x, @numItems

err = fn GetDataBrowserItemCount (browser,¬
_kDataBrowserNoItem,0,_kDataBrowserItemAnyState, @numItems)
long if err = _false

for x = 1 to numItems
if fn IsDataBrowserItemSelected ¬
(browser, x) then exit for
next x

long if x > numItems'did not find any selected
x = 0'return zero
end if

xelse'some error happened
x = 0
end if

end fn = x'return row of 1st selected item
'~'5
'this function clears all of the items from the dataBrowser
'------------
local mode
local fn DBDataRemoveAllBrowserItems( browser as HIViewRef )
'~'1
call RemoveDataBrowserItems( browser, 0, 0, #0, 0 )
fn DBDataClearArray( browser )
end fn
'~'5

local fn SetHeaderButtonDesc( title as Str255, titleOffset as long, ¬
contentType as long, initialOrder as long, minWidth as long, ¬
maxWidth as long, columnDesc as ^DatabrowserListViewColumnDesc )
'~'1
columnDesc.headerBtnDesc.version = _kDataBrowserListViewLatestHeaderDesc
columnDesc.headerBtnDesc.btnContentInfo.contentType = contentType
columnDesc.headerBtnDesc.titleString = fn CFSTR( title )
columnDesc.headerBtnDesc.titleOffset = titleOffset
columnDesc.headerBtnDesc.initialOrder = initialOrder
columnDesc.headerBtnDesc.minimumWidth = minWidth
columnDesc.headerBtnDesc.maximumWidth = maxWidth
end fn
'~'5

local fn SetPropertyDesc( propertyID as DataBrowserPropertyID, ¬
propertyType as DataBrowserPropertyType, ¬
propertyFlags as DataBrowserPropertyFlags, ¬
columnDesc as ^DatabrowserListViewColumnDesc )
'~'1
columnDesc.propertyDesc.propertyID = propertyID
columnDesc.propertyDesc.propertyType = propertyType
columnDesc.propertyDesc.propertyFlags = propertyFlags
end fn
'~'5

local fn AddColumns_cBrowser( browser as ControlRef )
'~'1
dim as DatabrowserListViewColumnDesc columnDesc
dim as OSStatus ignore

// C001
BlockZero( @columnDesc, sizeof(columnDesc) )
columnDesc.headerBtnDesc.version = _kDataBrowserListViewLatestHeaderDesc
fn SetHeaderButtonDesc( "My browser", 0, _kControlContentTextOnly, ¬
_kDataBrowserOrderIncreasing, 30, 500, @columnDesc )
fn SetPropertyDesc( _cBrowserC001, _kDataBrowserTextType,¬
0¬
  , @columnDesc )
fn DBDataSetPropertyType( browser, _cBrowserC001, ¬
_kDataBrowserTextType )
call AddDataBrowserListViewColumn( browser, columnDesc, ¬
_kDataBrowserListViewAppendColumn )
ignore = fn SetDataBrowserTableViewNamedColumnWidth¬
( browser, _cBrowserC001, 167 )

end fn
'~'5

local fn CreateDataBrowser_cBrowser as ControlRef
'~'1
dim as Rect @ r
dim as ControlRef @ browser
dim as OSStatus ignore

// create
SetRect( r, 360, 32, 548, 374 )

ignore = fn CreateDataBrowserControl( window( _wndRef ), r, ¬
_kDataBrowserListView, @browser )

// scroll bars
ignore = fn SetDataBrowserHasScrollBars( browser, _false, _true )

// hilite style
ignore = fn SetDataBrowserTableViewHiliteStyle( browser, ¬
_kDataBrowserTableViewFillHilite )

// selection flags
call SetDataBrowserSelectionFlags( browser, _kDataBrowserSelectOnlyOne )

// add columns
fn AddColumns_cBrowser( browser )

// initial selection
ignore = fn RevealDataBrowserItem( browser, 1, _cBrowserC001, ¬
_kDataBrowserRevealWithoutSelecting )


end fn = browser
'~'5
'this function shows how to add content to the dataBrowser
local fn InitBrowserData( browser as ControlRef )
'~'1
dim as Str255 s(26)
dim as long item, itemID, count, selectRow
dim as OSStatus ignore

s(1) = "Alpha"
s(2) = "Bravo"
s(3) = "Charlie"
s(4) = "Delta"
s(5) = "Echo"
s(6) = "Foxtrot"
s(7) = "Golf"
s(8) = "Hotel"
s(9) = "India"
s(10) = "Juliet"
s(11) = "Kilo"
s(12) = "Lima"
s(13) = "Mike"
s(14) = "November"
s(15) = "Oscar"
s(16) = "Papa"
s(17) = "Quebec"
s(18) = "Romeo"
s(19) = "Sierra"
s(20) = "Tango"
s(21) = "Uniform"
s(22) = "Victor"
s(23) = "Whisky"
s(24) = "Xray"
s(25) = "Yankee"
s(26) = "Zulu"

itemID = 0
count = rnd(26)
for item = 1 to count
itemID++
fn DBDataSetPString( browser, _cBrowserC001, itemID, str$(item) ¬
+ " " + s(rnd(26)) )
next item

fn DBDataPopulateBrowser( browser )

selectRow = itemID / 2:if selectRow = 0 then selectRow = 1
'< 2 > this line shows how to programatically select
'one row in the dataBrowser
ignore = fn RevealDataBrowserItem( gBrowser, selectRow, ¬
_cBrowserC001,  
_kDataBrowserRevealOnly)'_kDataBrowserRevealAndCenterInView )

def SetButtonTextString(_cMyEdit, "Browser should contain:" ¬
+ str$(itemID) + " items")

end fn

'~'5
'< 1 > this function shows how to react to a click or
'double click on one of the dataBrowser's rows
Clear Local Fn MyItemNotification( browser As ControlRef, ¬
itemID As DataBrowserItemID, ¬
message As DataBrowserItemNotification )

Select message
Case _kDataBrowserItemSelected' Item has has been selected
def SetButtonTextString(_cMyEdit, "Row selected:" + str$(itemID))

Case _kDataBrowserItemDoubleClicked' The user double clicked on an item.
def SetButtonTextString(_cMyEdit, "Double clicked:" + str$(itemID))

End Select

End fn

'~'5
local fn BuildUntitled1Wnd
'~'1
dim as Rect r
dim as WindowAttributes wa
dim as OSErr err
'dim as ControlRef @ browser

wa = _kWindowCloseBoxAttribute¬
  _kWindowCollapseBoxAttribute¬
  _kWindowFullZoomAttribute¬
  _kWindowResizableAttribute

//SetRect( r, 0, 0, 576, 444 )// Optional FB auto-centre
SetRect( r, 638, 63, 1214, 507 )
appearance window -_Untitled1Wnd, "Untitled 1", @r,  
_kDocumentWindowClass, wa
def SetWindowBackground( _kThemeActiveDialogBackgroundBrush, _zTrue )
err = fn SetAutomaticControlDragTrackingEnabledForWindow¬
( window( _wndRef ), _zTrue )

SetRect(r, 23, 30, 279, 46)
appearance button _cMyEdit, _activeBtn,,,,, @r, _kControlEditTextProc
def SetButtonTextString(_cMyEdit, "Data Browser Item")

SetRect(r, 20, 75, 282, 95)
appearance button _cResetDataBrowserBtn, _activeBtn,,,,¬
"Reset Data Browser", @r, _kControlPushButtonProc

SetRect(r, 20, 105, 282, 125)
appearance button _tellMeBtn, _activeBtn,,,,¬
"Which row is selected?", @r, _kControlPushButtonProc

gBrowser = fn CreateDataBrowser_cBrowser()

appearance window _Untitled1Wnd
end fn

'~'5
local fn Init
'~'1
fn BuildUntitled1Wnd
fn InitBrowserData( gBrowser )
fn DBDataInstallUserItemNotificationCB( gBrowser, @Fn  
MyItemNotification )
fn DBDataSetAlternatingRowColors( gBrowser , _true )
end fn

'~'5
local fn DoDialog
'~'1
'dim as Rect r
dim as long ev, id, selected
dim as str255 msg$

ev = dialog( 0 )
id = dialog( ev )
select ev
case _wndClick : window id
case _wndClose
case _wndRefresh
case _wndResized
case _wndActivate
case _btnClick
select window( _outputWnd )
case _Untitled1Wnd
select id
case _cResetDataBrowserBtn

'< 3 > this is how to replace the content of a dataBrowser with new  
items
fn DBDataRemoveAllBrowserItems( gBrowser )
fn InitBrowserData( gBrowser )

case _tellMeBtn

'< 4 > this is how to detect which row of the dataBrowser is selected
selected = fn getFirstSelectedRow( gBrowser )
long if selected
msg$ = "Row" + str$(selected) + " is selected"
xelse
msg$ = "No row is selected"
end if
def SetButtonTextString( _cMyEdit, msg$ )

end select
end select
case _preview
select id
case _preMenuClick
case _preWndGrow
end select
end select
end fn

'~'5
fn Init

on dialog fn DoDialog

do
HandleEvents
until gFBQuit