PowerBASIC Forums
  Source Code
  Bare Bones Spinner for Newbies

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   Bare Bones Spinner for Newbies
Gösta H. Lovgren-2
Member
posted March 20, 2004 08:35 PM     Click Here to See the Profile for Gösta H. Lovgren-2     Edit/Delete Message   Reply w/Quote
This is more appropriate for Newbies rather than the old dogs.

Please post any questions/comments/complaints here:
http://www.powerbasic.com/support/forums/Forum4/HTML/010194.html


'*************************************************************************************'
'*************************************************************************************'
' '
' Bare Bones Spinner example '
' '
' (Tip - Click on the center icon just above (Pencil on Paper) to Cut&Paste '
' into your program editor. It will preserve the spacing and formatting.) '
' '
' Below is, I believe, pretty simple code to help guide a NewBy to PB_Win '
' to get an idea how a neat Spinner can be used in the program. '
' '
' The descriptively named variables should help. '
' '
' With the exception of constants identified in the WinAPi all variables used here '
' have been defined by me and aren't necessary to the PB environment. '
' '
' All that needs to be done to get this to run is change the path in the #Include(s) '
' to wherever you have Win32Api.Inc (supplied with PB Win) located. '
' ' '
' (A big nod goes to Borje Hagsten whose great examples got me over the top '
' with Spinners - as well as a lot of other stuff.) '
' '
' '
' '
' Gösta H. Lovgren '
' gosta@SwedesDock.com '
' http://www.SwedesDock.com '
' http://www.PondersBible.com '
'*************************************************************************************'
'*************************************************************************************'
'
#Compile Exe
'
#Include "d:\Power Basic\WinApi\WIN32API.INC"
#Include "d:\Power Basic\WinApi\COMMCTRL.INC" 'contains Spinner info

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' You can disable support (and code) for the various common controls by
' defining the following constants in your code *before* the #INCLUDE
' statement.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%NOANIMATE = 1
%NOBUTTON = 1
%NOCOMBO = 1
%NODATETIMEPICK = 1
%NODRAGLIST = 1
%NOEDIT = 1
%NOFLATSBAPIS = 1
%NOHEADER = 1
%NOHOTKEY = 1
%NOIMAGELIST = 1
%NOIPADDRESS = 1
%NOLIST = 1
%NOLISTVIEW = 1
%NOMONTHCAL = 1
%NONATIVEFONTCTL = 1
%NOPAGESCROLLER = 1
%NOPROGRESS = 1
%NOREBAR = 1
%NOSTATUSBAR = 1
%NOTABCONTROL = 1
%NOTOOLBAR = 1
%NOTOOLTIPS = 1
%NOTRACKBAR = 1
%NOTREEVIEW = 1
%NOUPDOWN = 1
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Declare Function Spinner_Up_Down(Id AS LONG) AS LONG

DefLng a-z ' forces all variables to be LONG unless explicitly identified

Global hDlg AS Dword 'Main dialog handle
'Note - "hDlg" is the conventional variable name used by'
' PB programmers for the main dialog identifier. '

Global pNmud AS NM_UPDOWN Ptr 'Pointer to Structure used by Spinner


'
'
'
'Program constants I Use
%Character_Width = 4 ' used to calculate label widths, etc.
%Box_Height = 12 ' used to calculate text box and label heights, etc.
%Cream = 252 + (241 * 256) + (167 * 256 * 256) 'bg color for the main dialog
%Win_Gray = &hEAE9E8

'

' Assign Id Numbers as Constants for Controls.
%TB_Date = 200
%TB_Date_Label = 201
%Day_Spinner = 202
%Month_Spinner = 203
%Year_Spinner = 204
%TB_Month = 205
%TB_Day = 206
%Tb_Names = 207
%Tb_Names_Label = 208
%Names_Spinner = 209

'keep consistent style wherever used
%Spinner_Style = %WS_CHILD Or _
%WS_VISIBLE Or _
%UDS_WRAP Or _
%UDS_ARROWKEYS

'*************************************************************************************'
'*************************************************************************************'
' Note CallBacks are somewhat analogous to INPUT routines in DOS. They are a lot more '
' than that, though, as you will learn eventually. '
'*************************************************************************************'
'Main Callback Processor
CallBack Function CB_Main_Processor AS LONG
'
Select Case CbMsg ' This is TO determine the message TYPE returned by the program
'
Case %WM_INITDIALOG '<- for processing that needs to occur when the program loads '
' It only gets hit once when the program starts.
Call InitCommonControls 'so we can use contents of COMMCTRL.INC
Call Globals_Setup
'
'
Case %WM_COMMAND 'This processes command messages sent by Windows
'
Case %WM_Notify 'for Spinners
pNmud = CbLParam
id = CbCtl
Select Case Id 'CB-> CbMsg-> %Wm_Notify-> CbCtl
Case %Day_Spinner To %Year_Spinner, _
%Names_Spinner
Function = Spinner_Up_Down(Id)
End Select
End Select
End Function
'*************************************************************************************'


'****************************************************************************************

'*************************************************************************************'
'*************************************************************************************'
Sub Globals_Setup
'
'
Global Version$ 'Header used for Main Dialog and sometimes elsewhere by me
Global Day AS LONG
Global Month AS LONG
Global Year AS LONG

Global Days$()
Global Months$()
Global Neat_Guys$()
Global Name_Showing AS LONG
'
Data Sun, Mon, Tues, Wednes, Thurs, Fri, Satur
Data January, February, March, April, May, June, July, August, September
Data October, November, December

Dim Days$(7)
For x = 1 To 7
Days$(x) = Read$(x) & "day"
Next x

Dim Months$(12)
For x = 1 To 12
Months$(x) = Read$(x + 7)
Next x

'apologies to the dozens of names left but but I can only type so many {grin}
Data Gösta H. Lovgren, Borje Hagstrom, Michael Mathias, Clay Clear, Paul Squires
Dim Neat_Guys$(5)
For x = 1 To 5
Neat_Guys$(x) = Read$(x + 7 + 12)
Next x

'used to start
x$ = Date$
Month = Val(Left$(x$,2))
Day = Val(Mid$(x$,4,2))
Year = Val(Right$(x$,4))

End Sub
'*************************************************************************************'

'*************************************************************************************'
'*************************************************************************************'
' '
' Programs always begin at PBMain even though it's not at the top of the code. '
' '
'*************************************************************************************'
Function PBMAIN () AS LONG
'
Version$ = " version 1.00 from Carolelle Publishing®" & _
" (" & Chr$(169)& "2004) "
'
Call Main_Display_Setup 'where the main window is drawn '
'Note the main window can be drawn here. A procedure is '
' not required but I find it cleaner coding this way. '
'
' Control Set Focus Hdlg, %TB_Sample_Text_Box 'start out with cursor here

'
'display the main window and get to the action
Dialog Show Modal hDlg Call CB_Main_Processor
'
End Function
'*************************************************************************************


'*************************************************************************************
'*************************************************************************************
Sub Main_Display_Setup 'where the main window is drawn
'
Caption$ = "Bare Bones - Spinner Example - " & Version$
Col = 20 'Column to begin drawing the Dialog from the side of the screen
Row = %Box_Height * 5 'Row to begin drawing the Dialog from the top og the screen
Width_of_Dialog = 330
Height_of_Dialog = 200
Dialog New hDlg, Caption$, _
, Row, _ 'Not specifying a row or column automatically centers the dialog
Width_of_Dialog, Height_of_Dialog,_
%WS_SYSMENU Or _ ' but are included to just for demonstration '
%WS_CAPTION Or _ 'Note - not all these Styles are absolutely necessary. '
%WS_THICKFRAME Or _
%WM_HELP Or _
%WS_Border Or _ 'doesn't seem to do anything '
%WS_MINIMIZEBOX, _
%WS_Ex_WindowEdge, _ 'an "Extended Style
To hDlg 'assign an internal handle to "hDlg"

'neither of next are really needed but demonstrate a hint of the visual power of PB '
Dialog Font "Arial", 8
Dialog Set Color hDlg, -1&, %Cream
'
Call Date_TB_Setup
Call Names_SetUp
End Sub
'********
Sub Names_SetUp

Row = %Box_Height * 10
Col = 150

l$ = $CrLf & "Really Neat Guys"
Label_Width = Len(l$) * %Character_Width
Control Add Label, hDlg, %Tb_Names_Label, _
l$, _
Col + 1, Row, _
Label_Width - 2, %Box_Height * 2, _
%SS_Center
Control Add Frame, hDlg, %TB_Names_Label, "", _
Col -1, Row - 5, _
Label_Width + 2, (%Box_Height * 2) + 6, _
%SS_Right

Wdth = 20 * %Character_Width
Control Add TextBox, hDlg, %Tb_Names,"Neat Guys", _
Col + Label_Width + 5, Row, _
Wdth, %Box_Height, _
%ES_LEFT, %WS_EX_CLIENTEDGE

'Now set up Spinners alongside
Ovr = - 14 'put to left of the Label
Control Add $UPDOWN_CLASS, hDlg, %Names_Spinner, "", _
Col + Ovr, Row, _
50, %Box_Height * 2, _ 'Note much bigger here - the width doesn't change though
%Spinner_Style ' I don't know why.


End Sub
'********

Sub Date_TB_Setup
Row = 5
Col = 20

l$ = "Dates to make your head spin"
Label_Width = Len(l$) * %Character_Width
Control Add Label, hDlg, %TB_Date_Label, _
l$, _
Col + 1, Row, _
Label_Width - 2, %Box_Height, _
%SS_Center
Control Add Frame, hDlg, %TB_Date_Label, "", _
Col, Row -4, _
Label_Width + 2, %Box_Height + 6, _
%SS_Right

l$ = " 99/99/9999" 'widest possible date in proportional fonts
Wdth = Len(l$) * %Character_Width
Control Add TextBox, hDlg, %TB_Date, Date$, _
Col + Label_Width + 5, Row, _
Wdth, %Box_Height, _
%ES_LEFT, %WS_EX_CLIENTEDGE

'Now set up Spinners underneath
Dwn = %Box_Height 'Move down directly under Date TextBox
Ovr = Label_Width + 5 'Move over to Month
Control Add $UPDOWN_CLASS, hDlg, %Month_Spinner, "", _
Col + Ovr, Row + Dwn, _
18, 12, _
%Spinner_Style

Ovr = Ovr + 12 'Move over to Day
Control Add $UPDOWN_CLASS, hDlg, %Day_Spinner, "", _
Col + Ovr, Row + Dwn, _
18, 12, _
%Spinner_Style

Ovr = Ovr + 16 'Move over to Year
Control Add $UPDOWN_CLASS, hDlg, %Year_Spinner, "", _
Col + Ovr, Row + Dwn, _
18, 12, _
%Spinner_Style


Wdth = 15 * %Character_Width
Col = Col + Ovr + 25
Control Add TextBox, hDlg, %TB_Month, "Month", _
Col, Row, _
Wdth, %Box_Height, _
%ES_LEFT, %WS_EX_CLIENTEDGE

Wdth = 15 * %Character_Width
Col = Col + Wdth + 5
Control Add TextBox, hDlg, %TB_Day, "Day", _
Col, Row, _
Wdth, %Box_Height, _
%ES_LEFT, %WS_EX_CLIENTEDGE

End Sub
'*************************************************************************************


'****************************************************************************************
'****************************************************************************************
Function Spinner_Up_Down(Id AS LONG) AS LONG

If @pNmud.hdr.code = %UDN_DELTAPOS Then

Select Case Id
Case %Day_Spinner
udPos = Day + @pNmud.iDelta
If udPos > 31 Then udPos = 31
If udPos < 1 Then udPos = 1
Day = udPos
Call Date_Changed

Case %Month_Spinner
udPos = Month + @pNmud.iDelta
If udPos > 12 Then udPos = 12
If udPos < 1 Then udPos = 1
Month = udPos
Call Date_Changed

Case %Year_Spinner
udPos = Year + @pNmud.iDelta
If udPos > 2099 Then udPos = 2099
If udPos < 1980 Then udPos = 1980
Year = udPos
Call Date_Changed

Case %Names_Spinner
udPos = Name_Showing + @pNmud.iDelta
If udPos > 5 Then udPos = 1
If udPos < 1 Then udPos = 5
Name_Showing = udPos
Control Set Text hDlg, %Tb_Names, Neat_Guys$(Name_Showing)

If InStr(Neat_Guys$(Name_Showing), "Gösta") Or _
InStr(Neat_Guys$(Name_Showing), "Borje") Then
Control Set Color hDlg, %Tb_Names_Label, %Yellow, %Blue
Control Redraw hDlg, %Tb_Names_Label
ElseIf InStr(Neat_Guys$(Name_Showing), "Michael") Then
Control Set Color hDlg, %Tb_Names_Label, %Black, %Win_Gray
Control Redraw hDlg, %Tb_Names_Label
Else
Control Set Color hDlg, %Tb_Names_Label, %White, %Red
Control Redraw hDlg, %Tb_Names_Label
End If

End Select
End If
End Function
'****************************************************************************************

'*************************************************************************************
'*************************************************************************************
Sub Date_Changed
l$ = Using$("##/##/####", Month, Day, Year)
Control Set Text hDlg, %TB_Date, l$
Control Set Text hDlg, %TB_Month, Months$(Month)

'Note this is not meant to be accurate, only illustrative
d = Day Mod 7 'Guess a day
If d < 1 Then d = 7 'JIC
If d > 7 Then d = 1 'JIC
Control Set Text hDlg, %TB_Day, Days$(d)

End Sub
'*************************************************************************************


'*************************************************************************************
'*************************************************************************************
'
'
'
'
'
' end of Bare Bones - Spinner '
'
'Tip - Ask a lot of questions on the Power Basic Forums. '
' They are the best in the world. '
'
'Tip - Makes use of the Color Options in your editor to make comments, strings, '
' keywords, etc. stand out. For example I use a Yellow background for Comments '
' and a Green bg for Strings. '
'
'Tip - Invest in the JellyFish Pro editor. It's designed for Power Basic and '
' has a lot more features than the PB editor. '
'

[This message has been edited by Gösta H. Lovgren-2 (edited March 21, 2004).]

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c