File: u_toolbar.sru
Size: 15388
Date: Mon, 07 Apr 2008 21:32:44 +0200
$PBExportHeader$u_toolbar.sru
$PBExportComments$PB friendly MS toolbar
forward
global type u_toolbar from u_base
end type
type st_message from statictext within u_toolbar
end type
type st_1 from statictext within u_toolbar
end type
type ids_buttons from n_ds within u_toolbar
end type
end forward

global type u_toolbar from u_base
integer width = 645
integer height = 104
boolean border = true
boolean #tooltips = true
event resize pbm_size
event ke_dropdown ( readonly string buttonid,  readonly string buttontext,  readonly string buttondata,  readonly integer popx,  readonly integer popy )
event ke_buttonclicked ( readonly string buttonid,  readonly string buttontext,  readonly string buttondata )
event ke_dropdowncoordinate ( readonly integer buttonindex,  readonly integer popx,  readonly integer popy )
event ke_menuitemclicked ( readonly string buttonid )
event ke_routemenu ( )
st_message st_message
st_1 st_1
ids_buttons ids_buttons
end type
global u_toolbar u_toolbar

type prototypes
FUNCTION boolean InitCommonControlsEx( Ref INITCOMMONCONTROLS LPINITCOMMONCONTROLS) Library "comctl32.dll" alias for "InitCommonControlsEx;Ansi"
SUBROUTINE GetNMHDR( ref NMHDR d, long s, long l ) library 'kernel32.dll' alias for "RtlMoveMemory"
end prototypes

type variables
Public:
String #DataObject
Boolean #StandardImages = TRUE, #LargeIcons
Boolean #CreateOnParent, #Flat = TRUE, #Transparent = TRUE
Boolean #NoDivider = TRUE, #Wrappable, #ListStyle = TRUE, #HideClippedButtons = TRUE
Boolean #DoubleBuffer = TRUE, #TriggerResize = TRUE
boolean #UseTopMostParent
String #ImageListNormal, #ImageListHot, #ImageListDisabled
long Handle
Private:
string is_buttontext[], is_tooltips[], is_buttonid[], is_buttondata[]
tbbutton istr_buttons[]
long ToolBarHandle
long NMHDR_SIZE
CONSTANT string TOOLBAR_SVC = "n_svc_toolbar"
end variables
forward prototypes
protected subroutine of_init (ref n_svc_mgr anv_svc)
public function integer of_texttoid (readonly string as_text)
public function boolean of_isbuttonvisible (readonly string as_buttontext)
public function boolean of_isbuttonenabled (readonly string as_buttontext)
public function boolean of_isbuttonchecked (readonly string as_buttontext)
public function long of_gettoolbarhandle ()
public function window of_getparentwindow ()
public subroutine of_buttoncheck (readonly string as_buttontext, readonly boolean ab_set)
public subroutine of_buttonenable (readonly string as_buttontext, readonly boolean ab_set)
public subroutine of_buttonvisible (readonly string as_buttontext, readonly boolean ab_set)
public subroutine of_settoolbarbuttons (readonly datastore ads)
public subroutine of_settoolbarbuttons (readonly string as_dataobject)
public subroutine of_setimagelist (readonly long al_himl)
public subroutine of_resize (readonly integer ai_width, readonly integer ai_height)
public function long of_makemenuitems (ref str_menuitem astr_items[])
public subroutine of_popmenu (readonly integer ai_x, readonly integer ai_y)
public subroutine of_popmenu (readonly str_menuitem astr[], readonly integer ai_x, readonly integer ai_y)
public function integer of_getmaxwidth ()
public subroutine of_usetopmostparent (readonly boolean ab_topmost)
public function long of_getindexfromid (readonly string as_buttonid)
end prototypes

event resize;newwidth = this.width
newheight = this.height

IF ToolbarHandle <> 0 AND #TriggerResize THEN
   of_Resize(newwidth, newheight)
END IF
end event

event ke_dropdown(readonly string buttonid, readonly string buttontext, readonly string buttondata, readonly integer popx, readonly integer popy);//triggered when dropdown toolbar button is clicked
//use the popx and popy variables for display
end event

event ke_buttonclicked(readonly string buttonid, readonly string buttontext, readonly string buttondata);//triggered on toolbar button click
end event

event ke_dropdowncoordinate(readonly integer buttonindex, readonly integer popx, readonly integer popy);EVENT ke_dropdown(is_buttonid[buttonindex], &
               is_buttontext[buttonindex], &
               is_buttondata[buttonindex], &
               popx, &
               popy)
end event

event ke_menuitemclicked(readonly string buttonid);//fired from dynamic menu
long ll_index

ll_index = of_getindexfromid(buttonid)
EVENT ke_buttonclicked(buttonid, is_buttontext[ll_index], is_buttondata[ll_index])
end event

event ke_routemenu();//fired from dynamic menu
EVENT ke_menuitemclicked(STRING(Message.LongParm, "address"))
end event

protected subroutine of_init (ref n_svc_mgr anv_svc);INITCOMMONCONTROLS lICC

// Set the size of the structure (2 longs = 8 bytes)
lICC.dwSize = 2 * 4
lICC.dwICC  = CWin32.ICC_BAR_CLASSES

// Initialise the common controls
InitCommonControlsEx(lICC)

//let's get the size of NHMDR
n_svc_sizeof lnv_size
NMHDR lstr

anv_svc.of_LoadSvc(lnv_size, CSvc.SIZEOF)
NMHDR_SIZE = lnv_size.SizeOf(lstr)
end subroutine

public function integer of_texttoid (readonly string as_text);string ls_ret
long ll_count, n

ll_count = UPPERBOUND(is_buttontext)

FOR n = 1 to ll_count
   IF is_buttontext[n] = as_text THEN 
      RETURN n
   ELSEIF is_tooltips[n] = as_text THEN
      RETURN n
   ELSEIF is_buttonid[n] = as_text THEN
      RETURN n
   END IF
NEXT

RETURN CRet.FAILURE
end function

public function boolean of_isbuttonvisible (readonly string as_buttontext);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
RETURN lnv_toolbar.of_IsButtonVisible(ToolbarHandle, of_TextToID(as_buttontext))
end function

public function boolean of_isbuttonenabled (readonly string as_buttontext);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
RETURN lnv_toolbar.of_IsButtonEnabled(ToolbarHandle, of_TextToID(as_buttontext))
end function

public function boolean of_isbuttonchecked (readonly string as_buttontext);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
RETURN lnv_toolbar.of_IsButtonChecked(ToolbarHandle, of_TextToID(as_buttontext))
end function

public function long of_gettoolbarhandle ();RETURN ToolbarHandle
end function

public function window of_getparentwindow ();n_svc_mgr lnv_svc
n_svc_window lnv_w

lnv_svc.of_LoadSvc(lnv_w, CSvc.WINDOW)
RETURN lnv_w.of_GetParentWindow(this, NOT #UseTopMostParent)
end function

public subroutine of_buttoncheck (readonly string as_buttontext, readonly boolean ab_set);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_ButtonCheck(ToolbarHandle, of_TextToID(as_buttontext), ab_set)
end subroutine

public subroutine of_buttonenable (readonly string as_buttontext, readonly boolean ab_set);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_ButtonEnable(ToolbarHandle, of_TextToID(as_buttontext), ab_set)
end subroutine

public subroutine of_buttonvisible (readonly string as_buttontext, readonly boolean ab_set);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_ButtonVisible(ToolbarHandle, of_TextToID(as_buttontext), ab_set)
end subroutine

public subroutine of_settoolbarbuttons (readonly datastore ads);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar
blob lb

IF NOT ISVALID(ads) THEN RETURN
ads.GetFullState(lb)
ids_buttons.SetFullState(lb)

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_AddButtons(lnv_svc, &
               ToolbarHandle, &
               ids_buttons, &
               is_buttonid, &
               is_buttontext, &
               is_tooltips, &
               is_buttondata, &
               istr_buttons)

of_UseTopmostParent(#UseTopmostParent)
               
IF #CreateOnParent THEN of_Resize(this.width, this.height)

end subroutine

public subroutine of_settoolbarbuttons (readonly string as_dataobject);n_ds lds

lds = CREATE n_ds

IF lds.of_SetDataobject(as_dataobject) = CRet.SUCCESS THEN
   of_SetToolbarButtons(lds)
END IF

DESTROY lds
end subroutine

public subroutine of_setimagelist (readonly long al_himl);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_SetImageList(ToolbarHandle, al_himl)
end subroutine

public subroutine of_resize (readonly integer ai_width, readonly integer ai_height);n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_Resize(ToolbarHandle, ai_width, ai_height)
this.height = lnv_toolbar.of_GetToolbarHeight(ToolbarHandle, TRUE)
//update resize attrib
str_resize lstr
lstr = of_GetResizeAttrib()
lstr.bounds.bottom = this.height
of_SetResizeAttrib(lstr)

end subroutine

public function long of_makemenuitems (ref str_menuitem astr_items[]);long ll_count, n, ll_row, ll_upper
string ls_tmp
n_svc_mgr lnv_svc
n_svc_isempty lnv_check

lnv_svc.of_LoadSvc(lnv_check, CSvc.ISEMPTY)

ll_upper = UPPERBOUND(astr_items)

ll_count = ids_buttons.RowCount()
FOR n = 1 to ll_count
   ll_row ++
   //skip dropwhole
   IF ids_buttons.GetItemNumber(ll_row, "dropwhole") = 1 THEN
      ll_count --
      n --
      CONTINUE
   END IF
   
   IF ids_buttons.GetItemNumber(ll_row, "divider") = 1 THEN
      astr_items[ll_upper + n].menutext = "-"
   ELSE
      ls_tmp = ids_buttons.GetItemString(ll_row, "id")
      //skip not visible
      IF NOT of_isbuttonvisible(ls_tmp) THEN
         ll_count --
         n --
         CONTINUE
      END IF
      
      astr_items[ll_upper + n].data = ls_tmp
      astr_items[ll_upper + n].disable = NOT of_IsButtonEnabled(ls_tmp)
      astr_items[ll_upper + n].checked = of_IsButtonChecked(ls_tmp)
      
      ls_tmp = ids_buttons.GetItemString(ll_row, "text")
      IF lnv_check.of_IsEmpty(ls_tmp) THEN &
         ls_tmp = ids_buttons.GetItemString(ll_row, "tooltip")
      astr_items[ll_upper + n].menutext = ls_tmp
      astr_items[ll_upper + n].subscriber = this
      astr_items[ll_upper + n].eventname = "ke_routemenu"
   END IF
NEXT

RETURN ll_count
end function

public subroutine of_popmenu (readonly integer ai_x, readonly integer ai_y);str_menuitem lstr[]

of_MakeMenuitems(lstr)
of_PopMenu(lstr, ai_x, ai_y)
end subroutine

public subroutine of_popmenu (readonly str_menuitem astr[], readonly integer ai_x, readonly integer ai_y);n_svc_mgr lnv_svc
n_svc_menu lnv_m

lnv_svc.of_LoadSvc(lnv_m, CSvc.MENU)
lnv_m.of_createmenu(astr, ai_x, ai_y)
end subroutine

public function integer of_getmaxwidth ();n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar
tagsize lstr

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lstr = lnv_toolbar.of_GetMaxWidth(ToolbarHandle)
RETURN lstr.cx
end function

public subroutine of_usetopmostparent (readonly boolean ab_topmost);#UseTopMostParent = ab_topmost
end subroutine

public function long of_getindexfromid (readonly string as_buttonid);long ll_count, n, ll_ret

ll_ret = CRet.FAILURE

ll_count = UPPERBOUND(is_buttonid)
FOR n = 1 to ll_count
   IF is_buttonid[n] = as_buttonid THEN
      ll_ret = n
      EXIT
   END IF
NEXT

RETURN ll_ret
end function

on u_toolbar.create
int iCurrent
call super::create
this.st_message=create st_message
this.st_1=create st_1
this.ids_buttons=create ids_buttons
iCurrent=UpperBound(this.Control)
this.Control[iCurrent+1]=this.st_message
this.Control[iCurrent+2]=this.st_1
end on

on u_toolbar.destroy
call super::destroy
destroy(this.st_message)
destroy(this.st_1)
destroy(this.ids_buttons)
end on

event ke_preopen;call super::ke_preopen;st_1.Visible = FALSE
this.Border = FALSE

n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar
n_svc_resource lnv_res
n_svc_isempty lnv_check
n_svc_sizeof lnv_size
long llh, ll_style, ll_exstyle, ll_himl
powerobject lpo_parent
//boolean lb_toggle

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)

//init toolbar
of_Init(lnv_svc)

//create toolbar  
IF #CreateOnParent THEN
   lpo_parent = parent
   this.Visible = FALSE
ELSE
   lpo_parent = this
END IF

//build styles
ll_style = lnv_toolbar.of_BuildStyles(lnv_svc, &
            /*flat*/ #Flat, &
            /*liststyle*/ #ListStyle, &
            /*nodivider*/ #NoDivider, &
            /*tooltips*/ #Tooltips, &
            /*transparent*/ #Transparent, &
            /*ab_wrappable */ #Wrappable)

//build extended styles
ll_exstyle = lnv_toolbar.of_BuildExtendedStyles(lnv_svc, &
            /*doublebuffer*/ #DoubleBuffer, &
            /*hideclippedbuttons */ #HideClippedButtons)

//IF #CreateOnParent AND parent.Visible = FALSE THEN
// lb_toggle = TRUE
// parent.Visible = TRUE
//END IF

//create the toolbar
ToolbarHandle = lnv_toolbar.of_Create(lnv_svc, &
                  lpo_parent, &
                  ll_style, &
                  ll_exstyle, &
                  this.width, &
                  this.height)

//IF lb_toggle THEN parent.Visible = FALSE

//check for standardimages
IF #StandardImages THEN
   lnv_svc.of_LoadSvc(lnv_res, CSvc.RESOURCE)
   IF #LargeIcons THEN
      ll_himl = lnv_res.of_GetStandardImageList24(lnv_svc)
   ELSE
      ll_himl = lnv_res.of_GetStandardImageList16(lnv_svc)
   END IF
   of_SetImageList(ll_himl)
END IF

//set notification parent
lnv_toolbar.of_SetParent(ToolbarHandle, st_message)

//check for dataobject
lnv_svc.of_LoadSvc(lnv_check, CSvc.ISEMPTY)
IF NOT lnv_check.of_IsEmpty(#DataObject) THEN
   of_SetToolbarButtons(#DataObject)   
END IF

//assign to public member
Handle = ToolBarHandle

this.TriggerEvent("resize")
end event

event destructor;call super::destructor;n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)
lnv_toolbar.of_Destroy(ToolbarHandle)
end event

type st_message from statictext within u_toolbar
event notify pbm_notify
event command pbm_command
integer y = 144
integer width = 73
integer height = 52
integer textsize = -8
integer weight = 700
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
alignment alignment = center!
boolean focusrectangle = false
end type

event notify;NMHDR lstr_ret
n_svc_mgr lnv_svc
n_svc_toolbar lnv_toolbar

lnv_svc.of_LoadSvc(lnv_toolbar, TOOLBAR_SVC)

IF wparam < 50 THEN
   GetNMHDR(lstr_ret, lparam, NMHDR_SIZE)
   lnv_toolbar.of_NotifyButton(lnv_svc, lstr_ret.code + 10, lparam, is_tooltips) 
ELSEIF wparam = 150 THEN
   GetNMHDR(lstr_ret, lparam, NMHDR_SIZE)
   lnv_toolbar.of_NotifyToolbar(lnv_svc, ToolbarHandle, parent, lstr_ret.code, lparam, is_buttontext)
END IF
end event

event command;long ll_id

//bug?
IF NOT ISVALID(Message) THEN RETURN

ll_id = Message.WordParm

of_getindexfromid(STRING(ll_id))

parent.EVENT ke_buttonclicked( &
               is_buttonid[ll_id], &
               is_buttontext[ll_id], &
               is_buttondata[ll_id])
end event

type st_1 from statictext within u_toolbar
integer x = 110
integer y = 16
integer width = 411
integer height = 52
integer textsize = -8
integer weight = 700
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "Toolbar Control"
alignment alignment = center!
boolean focusrectangle = false
end type

type ids_buttons from n_ds within u_toolbar descriptor "pb_nvo" = "true" 
end type

on ids_buttons.create
call super::create
end on

on ids_buttons.destroy
call super::destroy
end on