I couldn't find any examples that uses these commands anywhere under Blue GUI 2.0. I looked at the help files for syntax and usage for the scrollbar commands, but I still having trouble getting them to work the way I want.
Approach:
1. Get image file with openDialog() (.bmp, .jpg, .png, .tga)
2. Copy image to a canvas gadget (Cool function written by Freddix.)
3. Check canvas size XY against main window size
4. If canvas size is greater than mainWin, then create scrollbars accordingly. This is to move the unviewable portions of the canvas in the main windiw into view.
Problem #1 - is getting the current position of scrollbar thumb without releasing the mouse buttom.
Problem #2 - is getting the ScrollbarThumbSize set correctly. It should directly correlate to the difference between the main window size and the canvas size. I am not sure how to properly figure this.
It seems that the amount the of setScrollbarRange divides the max distance between the arrows where the scrollbar thumb can travel by the differences of the min/maz range values. Say, the range was between is 1 to 10, then there is ten equally divided portions for the scrollbar thumb to move to. If you move the thumb in the scrollbar slightly, it just snaps back to the current range value, unless you have move it far enough to get to the next range value.
I want to make the thumb be in accordance to the travel difference by pixel. Does that make sense? If there is a difference between the canvas size and the main window size of 50 pixels, then the scrollbar thumb size should be 50 pixel minus the max scroll range distance bewteen the arrows in the scrollbar. After this, I want the scroll bar thumb to move by pixel 50 pixels (values) either way to reflect how much to scroll the canvas in the main window. Is this possible?
Here is my source code:
` Main source file
startBlue "***","******"
sync off
#include "gui.dba"
` Declare Global Variables For Windows Gadget Handles
global mainWin
global mainMenu
global fileMenu
global editMenu
global viewMenu
global imageMenu
global helpMenu
global popupMenu
global mainStatusBar
global fileName as string
global fileType as string="BMP (Windows bitmap)|*.bmp|JPG (JPEG file interchange format)|*.jpg|PNG (Portable network graphics)|*.png|TGA (Targa file format)|*.tga"
global currentImage
global sliderX
global sliderY
global fillPanel
createUI()
do
setStatusText mainStatusBar,0,str$(mousePosx())+", "+str$(mousePosY())
getEvent
processEvents()
loop
function createUI()
local Title as string="Image Autographer v1.0"
proWin=createGadgetFromWindow(mainWindow())
setGadgetVisible proWin,0
desktopWin=createGadgetFromWindow(desktopWindow())
mainWin=createWindow(0,0,gadgetWidth(desktopWin),gadgetHeight(desktopWin),Title,WINDOW_FIXED,0,0,0)
mainMenu=createMenu(mainWin)
mainStatusBar=createStatusBar(mainWin)
popupMenu=createPopupMenu()
addMenuItem mainMenu,"File",5
addMenuItem mainMenu,"Edit",1
addMenuItem mainMenu,"View",2
addMenuItem mainMenu,"Image",3
addMenuItem mainMenu,"Help",4
fileMenu=createSubMenu(mainMenu,5)
editMenu=createSubMenu(mainMenu,1)
viewMenu=createSubMenu(mainMenu,2)
imageMenu=createSubMenu(mainMenu,3)
helpMenu=createSubMenu(mainMenu,4)
addMenuItem fileMenu,"Open",6
addMenuSplitter fileMenu
addMenuItem fileMenu,"Save",7
addmenuItem fileMenu,"Save As",8
addMenuSplitter fileMenu
addMenuItem fileMenu,"Batch Process",9
addMenuSplitter fileMenu
addMenuItem fileMenu,"Close",10
addMenuItem fileMenu,"Exit",11
addMenuItem editMenu,"Undo",12
addMenuItem editMenu,"Redo",13
addMenuSplitter editMenu
addMenuItem editMenu,"Cut",14
addMenuItem editMenu,"Copy",15
addMenuItem editMenu,"Paste",16
addMenuItem helpMenu,"About",17
addMenuItem popupMenu,"Item 16",18
addMenuItem popupMenu,"Item 17",19
addMenuItem popupMenu,"Item 18",20
blockMenuOptions()
endfunction
Function processEvents()
if eventType()=MENU_CLICK
select eventData()
case 6 `Load single image file
openSingleFile()
endcase
case 7 `Save single image file
saveSingleFile(0)
endcase
case 8 `Save image file as
saveSingleFile(1)
endcase
case 9 `Batch Process
batchProcess()
endcase
case 10 `Close file
closeSingleFile()
endcase
case 11 `Exit
savePrompt():end
endcase
endselect
activateGadget mainWin
endif
if mouseclick()=2
`Show Popup Menu
result=showPopupMenu(popupMenu)
message "You clicked item " + str$(result)
endif
if eventSource()=sliderX
moveGadget currentImage,0-getScrollbarPosition(sliderX),gadgetY(currentImage)
endif
if eventSource()=sliderY
moveGadget currentImage,gadgetX(currentImage),0-getScrollbarPosition(sliderY)
endif
if eventSource()=mainWin and eventType()=WINDOW_CLOSE
savePrompt():end
endif
endfunction
Function openSingleFile()
savePrompt()
fileName=openDialog("Open Image File",fileType)
if fileName<>""
if file exist(fileName)=1
load bitmap fileName,1
imageSizeX=bitmap width(1)-1
imageSizeY=bitmap height(1)-1
get image 1,0,0,imageSizeX,imageSizeY,1
delete bitmap 1
currentImage=createCanvas(0,0,imageSizeX,imageSizeY,mainWin)
setCanvasImage(currentImage,1,1)
canvasWidth=gadgetWidth(currentImage)
canvasHeight=gadgetHeight(currentImage)
mainWidth=gadgetClientWidth(mainWin)
mainHeight=gadgetClientHeight(mainWin)
if canvasWidth>mainWidth
sliderX=createScrollBar(0,mainHeight-40,mainWidth,20,0,mainWin)
maxScrollX=canvasWidth-mainWidth
setScrollbarRange sliderX,0,maxScrollX
setScrollbarThumbSize sliderX,1
bringToFront sliderX
endif
if canvasHeight>mainHeight-20
if sliderX=0
sliderY=createScrollBar(mainWidth-20,0,20,mainHeight-20,1,mainWin)
maxScrollY=canvasHeight-mainHeight+20
setScrollbarRange sliderY,0,maxScrollY
setScrollbarThumbSize sliderY,1
bringToFront sliderY
else
sliderY=createScrollBar(mainWidth-20,0,20,mainHeight-40,1,mainWin)
maxScrollY=canvasHeight-(mainHeight-gadgetHeight(sliderX))+20
setScrollbarRange sliderY,0,maxScrollY
setScrollbarThumbSize sliderY,1
bringToFront sliderY
resizeGadget sliderX,gadgetWidth(sliderX)-gadgetWidth(sliderY),gadgetHeight(sliderX),0
maxScrollX=canvasWidth-(mainWidth-gadgetWidth(sliderY))
setScrollbarRange sliderX,0,maxScrollX
setScrollbarThumbSize sliderX,1
fillPanel=createPanel(gadgetWidth(sliderX),gadgetHeight(sliderY),mainWidth,mainHeight-20,mainWin)
bringToFront fillPanel
endif
endif
setMenuItemEnabled fileMenu,7,1
setMenuItemEnabled fileMenu,8,1
setMenuItemEnabled fileMenu,10,1
else
errorMessage "File Not Found!"
endif
endif
endfunction
Function saveSingleFile(saveAsOption)
if saveAsOption=1
saveFile:fileName=saveDialog("Save Image File As",fileType)
if fileName="" then exitfunction
if file exist(fileName)=1
option=questionMessage("Do you wish to over-write file?","FILE ALREADY EXISTS")
if option=0 then goto saveFile
endif
endif
getCanvasImage currentImage,2
save image fileName,2
delete image 2
if file exist(fileName)=0 then errorMessage "Could not write file: "+filename
endfunction
Function closeSingleFile()
if sliderX<>0 then sliderX=deleteGadget(sliderX):sliderX=0
if sliderY<>0 then sliderY=deleteGadget(sliderY):sliderY=0
if fillPanel<>0 then fillPanel=deleteGadget(fillPanel):fillPanel=0
currentImage=deleteGadget(currentImage):currentImage=0
blockMenuOptions()
endfunction
Function SavePrompt()
if currentImage<>0
option=questionMessage("Do you wish to save the current project?","Save Project")
if option=0 then closeSingleFile()
if option=1 then saveSingleFile(1)
endif
endfunction
Function batchProcess()
local dim fileBatch(1000) as string
local fileFolder as string
local fileFormat as string
fileFolder=folderDialog()
if fileFolder<>""
set dir fileFolder
perform checklist for files
for f=1 to checklist quantity()
fileFormat=right$(checklist string$(f),4)
if fileFormat=".bmp" or fileFormat=".jpg" or fileFormat=".png" or fileFormat=".tga"
inc fileCount
fileBatch(fileCount)=checklist string$(f)
endif
next f
if fileCount>0
option=questionMessage("Do you wish to proceed with batch processing?",str$(filecount)+" IMAGE FILES FOUND!")
if option=0 then exitfunction
else
message "No compatible files where found."
endif
endif
endfunction
Function blockMenuOptions()
setMenuItemEnabled fileMenu,7,0
setMenuItemEnabled fileMenu,8,0
setMenuItemEnabled fileMenu,10,0
endfunction
Function setCanvasImage(gadgetNumber,imageNumber,memBlok)
make memblock from image memBlok,imageNumber
MbcXSize=memblock dword(memBlok,0)
MbcYSize=memblock dword(memBlok,4)
Depth=memblock dword(memBlok,8)/8
GadXSize=gadgetWidth(gadgetNumber)
GadYSize=gadgetHeight(gadgetNumber)
if GadXSize<XSize
XSize=GadXSize
else
XSize=MbcXSize
endif
if GadYSize<YSize
YSize=GadYSize
else
YSize=MbcYSize
endif
for YLoop=0 To YSize-1
for XLoop=0 to XSize-1
if Depth=4
_Ink=memblock dword(memBlok,12+(Depth*XLoop)+(YLoop*MbcXSize*Depth))
else
_Ink=memblock word(memBlok,12+(Depth*XLoop)+(YLoop*MbcXSize*Depth))
endif
drawPoint gadgetNumber,XLoop,YLoop,_Ink
next XLoop
next YLoop
paintGadget gadgetNumber
delete memblock memBlok
endFunction
The scrollbar is setup under the "openSingleFile()" function. The scrollbar values are retrieved in the "processEvents()" function.