Address Book Exemple

Share your advanced PureBasic knowledge/code with the community.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Address Book Exemple

Post by Demivec »

I've udpated the code to PB V5.20. Here are the updated files.

Save this file as : Address book.pb

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Address Book Exemple
; File : Main source
; Version : 1.5.0
; Programmation : OK - Complete
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
; This source code it's just an exemple. You are 
; free to edit, modify, use or distribute.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

IncludeFile "Contact Structure.pb"
IncludeFile "Address book Structure.pb"
IncludeFile "Constants.pb"
IncludeFile "Procedures.pb"
IncludeFile "MainWindow and EventManager.pb"

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Opening AddressBook window
   
AddressBookWin()

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AddressBook Event Mananger

AddressBookEventManager(AddressBook)

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Save this one as : Contact Structure.pb

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY, MEAN IT !
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Type Editor
; Project name : Address Book Exemple
; File : The file name
; File Version : 1.5.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Structure definition >>>>>

Structure Contact
   
   LastName.s
   FirstName.s
   Town.s
   Number.s
   Street.s
   PostalCode.s
   Email.s
   Phone01.s
   Phone02.s
   Comments.s
   
EndStructure

; <<<<<<<<<<<<<<<<<<<<
; <<<<< Mutators >>>>>

Macro SetContactLastName(ContactA, P_LastName)

   ContactA\LastName = P_LastName

EndMacro

Macro SetContactFirstName(ContactA, P_FirstName)

   ContactA\FirstName = P_FirstName

EndMacro

Macro SetContactTown(ContactA, P_Town)

   ContactA\Town = P_Town

EndMacro

Macro SetContactNumber(ContactA, P_Number)

   ContactA\Number = P_Number

EndMacro

Macro SetContactStreet(ContactA, P_Street)

   ContactA\Street = P_Street

EndMacro

Macro SetContactPostalCode(ContactA, P_PostalCode)

   ContactA\PostalCode = P_PostalCode

EndMacro

Macro SetContactEmail(ContactA, P_Email)

   ContactA\Email = P_Email

EndMacro

Macro SetContactPhone01(ContactA, P_Phone01)

   ContactA\Phone01 = P_Phone01

EndMacro

Macro SetContactPhone02(ContactA, P_Phone02)

   ContactA\Phone02 = P_Phone02

EndMacro

Macro SetContactComments(ContactA, P_Comments)

   ContactA\Comments = P_Comments

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Observators >>>>>

Macro GetContactLastName(ContactA)

   ContactA\LastName

EndMacro

Macro GetContactFirstName(ContactA)

   ContactA\FirstName

EndMacro

Macro GetContactTown(ContactA)

   ContactA\Town

EndMacro

Macro GetContactNumber(ContactA)

   ContactA\Number

EndMacro

Macro GetContactStreet(ContactA)

   ContactA\Street

EndMacro

Macro GetContactPostalCode(ContactA)

   ContactA\PostalCode

EndMacro

Macro GetContactEmail(ContactA)

   ContactA\Email

EndMacro

Macro GetContactPhone01(ContactA)

   ContactA\Phone01

EndMacro

Macro GetContactPhone02(ContactA)

   ContactA\Phone02

EndMacro

Macro GetContactComments(ContactA)

   ContactA\Comments

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Simple Update >>>>>

Macro UpdateContact(ObjetA, P_LastName, P_FirstName, P_Town, P_Number, P_Street, P_PostalCode, P_Email, P_Phone01, P_Phone02, P_Comments)
   
   SetContactLastName(ObjetA, P_LastName)
   SetContactFirstName(ObjetA, P_FirstName)
   SetContactTown(ObjetA, P_Town)
   SetContactNumber(ObjetA, P_Number)
   SetContactStreet(ObjetA, P_Street)
   SetContactPostalCode(ObjetA, P_PostalCode)
   SetContactEmail(ObjetA, P_Email)
   SetContactPhone01(ObjetA, P_Phone01)
   SetContactPhone02(ObjetA, P_Phone02)
   SetContactComments(ObjetA, P_Comments)
   
EndMacro
 
 ; <<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Copy operator >>>>>

Macro CopyContact(ObjectA, ObjectB)
   
   SetContactLastName(ObjectB, GetContactLastName(ObjectA))
   SetContactFirstName(ObjectB, GetContactFirstName(ObjectA))
   SetContactTown(ObjectB, GetContactTown(ObjectA))
   SetContactNumber(ObjectB, GetContactNumber(ObjectA))
   SetContactStreet(ObjectB, GetContactStreet(ObjectA))
   SetContactPostalCode(ObjectB, GetContactPostalCode(ObjectA))
   SetContactEmail(ObjectB, GetContactEmail(ObjectA))
   SetContactPhone01(ObjectB, GetContactPhone01(ObjectA))
   SetContactPhone02(ObjectB, GetContactPhone02(ObjectA))
   SetContactComments(ObjectB, GetContactComments(ObjectA))
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Debug Macro >>>>>

Macro DebugContact(ObjectA)
   
   Debug GetContactLastName(ObjectA)
   Debug GetContactFirstName(ObjectA)
   Debug GetContactTown(ObjectA)
   Debug GetContactNumber(ObjectA)
   Debug GetContactStreet(ObjectA)
   Debug GetContactPostalCode(ObjectA)
   Debug GetContactEmail(ObjectA)
   Debug GetContactPhone01(ObjectA)
   Debug GetContactPhone02(ObjectA)
   Debug GetContactComments(ObjectA)
   
EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Read & Write Binary String <<<<<

Procedure WriteBinaryString(FileID, String.s)

  length.l = Len(String)
  WriteLong(FileID, length)
  WriteData(FileID, @String, length)

EndProcedure 

Procedure.s ReadBinaryString(FileID)

  length.l = ReadLong(FileID)
  String.s = Space(length)
  ReadData(FileID, @String, length)

  ProcedureReturn String
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Read Binary file >>>>>

Procedure ReadContact(FileID.l, *ObjectA.Contact)
   
   SetContactLastName(*ObjectA, ReadBinaryString(FileID))
   SetContactFirstName(*ObjectA, ReadBinaryString(FileID))
   SetContactTown(*ObjectA, ReadBinaryString(FileID))
   SetContactNumber(*ObjectA, ReadBinaryString(FileID))
   SetContactStreet(*ObjectA, ReadBinaryString(FileID))
   SetContactPostalCode(*ObjectA, ReadBinaryString(FileID))
   SetContactEmail(*ObjectA, ReadBinaryString(FileID))
   SetContactPhone01(*ObjectA, ReadBinaryString(FileID))
   SetContactPhone02(*ObjectA, ReadBinaryString(FileID))
   SetContactComments(*ObjectA, ReadBinaryString(FileID))
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Write Binary file >>>>>

Procedure WriteContact(FileID.l, *ObjectA.Contact)
   
   WriteBinaryString(FileID, GetContactLastName(*ObjectA))
   WriteBinaryString(FileID, GetContactFirstName(*ObjectA))
   WriteBinaryString(FileID, GetContactTown(*ObjectA))
   WriteBinaryString(FileID, GetContactNumber(*ObjectA))
   WriteBinaryString(FileID, GetContactStreet(*ObjectA))
   WriteBinaryString(FileID, GetContactPostalCode(*ObjectA))
   WriteBinaryString(FileID, GetContactEmail(*ObjectA))
   WriteBinaryString(FileID, GetContactPhone01(*ObjectA))
   WriteBinaryString(FileID, GetContactPhone02(*ObjectA))
   WriteBinaryString(FileID, GetContactComments(*ObjectA))

EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Sort by field offset >>>>>

Macro SortContactLastName()

   OffsetOf(Contact\LastName)

EndMacro

Macro SortContactFirstName()

   OffsetOf(Contact\FirstName)

EndMacro

Macro SortContactTown()

   OffsetOf(Contact\Town)

EndMacro

Macro SortContactNumber()

   OffsetOf(Contact\Number)

EndMacro

Macro SortContactStreet()

   OffsetOf(Contact\Street)

EndMacro

Macro SortContactPostalCode()

   OffsetOf(Contact\PostalCode)

EndMacro

Macro SortContactEmail()

   OffsetOf(Contact\Email)

EndMacro

Macro SortContactPhone01()

   OffsetOf(Contact\Phone01)

EndMacro

Macro SortContactPhone02()

   OffsetOf(Contact\Phone02)

EndMacro

Macro SortContactComments()

   OffsetOf(Contact\Comments)

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 234 ms <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Save this one as : Address book Structure.pb

[code; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY, MEAN IT !
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Type Editor
; Project name : Address Book Exemple
; File : AddressBook Structure and methods
; File Version : 1.5.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Structure definition >>>>>

Structure AddressBook

ContactQTY.l
ContactID.l
FileName.s

EndStructure

; <<<<<<<<<<<<<<<<<<<<
; <<<<< Mutators >>>>>

Macro SetAddressBookContactQTY(ObjetA, Contact_QTY)

ObjetA\ContactQTY = Contact_QTY

EndMacro

Macro SetAddressBookContactID(ObjetA, Contact_ID)

ObjetA\ContactID = Contact_ID

EndMacro

Macro SetAddressBookFileName(ObjetA, File_Name)

ObjetA\FileName = File_Name

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Observators >>>>>

Macro GetAddressBookContactQTY(ObjetA)

ObjetA\ContactQTY

EndMacro

Macro GetAddressBookContactID(ObjetA)

ObjetA\ContactID

EndMacro

Macro GetAddressBookFileName(ObjetA)

ObjetA\FileName

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 31 ms <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<[/code]

Save this one as : Constants.pb

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Address Book Exemple
; File : Contants, array, linked list, etc. 
; File Version : 1.5.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Windows Constant

Enumeration
   
   #Main_Win
   #Edit_Win
   
EndEnumeration

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Window Style contants

#Main_Win_Style = #PB_Window_ScreenCentered | #PB_Window_SystemMenu 
#Edit_Win_Style = #PB_Window_ScreenCentered | #PB_Window_SystemMenu 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Gadgets constants

Enumeration 
   
   #ComboBox_FirstNameList
   #Btn_Previous
   #Btn_Next
   #Btn_Modify
   #Btn_Delete
   #Btn_New
   
   #Frame_Info
   #Frame_Comments
   
   #Text_LastName
   #Text_PreLastName
   #Text_Town
   #Text_Number
   #Text_Street
   #Text_PostalCode
   #Text_Email
   #Text_HousePhone
   #Text_WorkPhone
   
   #String_LastName
   #String_FirstName
   #String_Town
   #String_Number
   #String_Street
   #String_PostalCode
   #String_Email
   #String_HousePhone
   #String_WorkPhone
   
   #Editor_Comments
   
   #Text_EDIT_LastName
   #Text_EDIT_FirstName
   #Text_EDIT_Town
   #Text_EDIT_Number
   #Text_EDIT_Street
   #Text_EDIT_PostalCode
   #Text_EDIT_Email
   #Text_EDIT_HousePhone
   #Text_EDIT_WorkPhone
   
   #String_EDIT_LastName
   #String_EDIT_FirstName
   #String_EDIT_Town
   #String_EDIT_Number
   #String_EDIT_Street
   #String_EDIT_PostalCode
   #String_EDIT_Email
   #String_EDIT_HousePhone
   #String_EDIT_WorkPhone
   
   #Editor_EDIT_Comments
   
   #Btn_EDIT_OK
   #Btn_EDIT_CANCEL
   
   #GUIFont
   
EndEnumeration 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

If LoadFont(#GUIFont,"Arial",8,#PB_Font_Bold)
   
   SetGadgetFont(#PB_Default, FontID(#GUIFont))
   
EndIf

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Init program mother Structure
   
SetAddressBookFileName(AddressBook.AddressBook, "My Address Book.dat")
   
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< TxtGUI Array Indexing Constants >>>>>

Enumeration
   
   #TxtGUI_ProgrName
   #TxtGUI_BtnPrevious
   #TxtGUI_BtnNext
   #TxtGUI_BtnModify
   #TxtGUI_BtnDelete
   #TxtGUI_BtnNew
   
   #TxtGUI_FrameInfo
   #TxtGUI_Comments
   #TxtGUI_LastName
   #TxtGUI_FirstName
   #TxtGUI_Town
   #TxtGUI_Number
   #TxtGUI_Street
   #TxtGUI_PostalCode
   #TxtGUI_Email
   #TxtGUI_HousePhone
   #TxtGUI_WorkPhone
   
   #TxtGUI_EditOk
   #TxtGUI_EditCancel
   
   #TxtGUI_WarningMessage00
   #TxtGUI_WarningMessage01
   
   #TxtGUI_dimsize
   
EndEnumeration

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Declare TextGUI Array >>>>>

Global Dim TextGUI.s(#TxtGUI_dimsize - 1)

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Init TextGUI Array >>>>>

TextGUI(#TxtGUI_ProgrName) = "Address book"
TextGUI(#TxtGUI_BtnPrevious) = "Previous"
TextGUI(#TxtGUI_BtnNext) = "Next"
TextGUI(#TxtGUI_BtnModify) = "Modify"
TextGUI(#TxtGUI_BtnDelete) = "Delete"
TextGUI(#TxtGUI_BtnNew) = "New"
TextGUI(#TxtGUI_FrameInfo) = "Information about selected contact"
TextGUI(#TxtGUI_Comments) = "Comments"
TextGUI(#TxtGUI_LastName) = "Last name :"
TextGUI(#TxtGUI_FirstName) = "First name :"
TextGUI(#TxtGUI_Town) = "Town :"
TextGUI(#TxtGUI_Number) = "Door Number :"
TextGUI(#TxtGUI_Street) = "Street :"
TextGUI(#TxtGUI_PostalCode) = "Postal code:"
TextGUI(#TxtGUI_Email) = "Email :"
TextGUI(#TxtGUI_HousePhone) = "House phone number :"
TextGUI(#TxtGUI_WorkPhone) = "Work phone number :"
TextGUI(#TxtGUI_EditOk) = "Ok"
TextGUI(#TxtGUI_EditCancel) = "Cancel"
TextGUI(#TxtGUI_WarningMessage00) = "Warning ! The delete command can't be undone." 
TextGUI(#TxtGUI_WarningMessage01) = "Click YES If you wish to delete %NAME% from the Address book."

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Save this one as : Procedures.pb

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project Name : Address Book Exemple
; File : Procedures
; Version : 1.5.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Macro Scrolling(Number, Minimum, Maximum, Increment)
   
   Number + Increment
   
   If Number > Maximum
      
      Number = Minimum
   
   ElseIf Number < Minimum
      
      Number = Maximum
      
   EndIf

EndMacro

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure PlugContactInGUI(*ObjectA.Contact)
   
   ; We flush all information in the main window before
   ; We put the Contact Informations in the main window
   
   For GadgetID = #String_LastName To #Editor_Comments
      SetGadgetText(GadgetID, "")
   Next 
   
   SetGadgetText(#String_LastName, GetContactLastName(*ObjectA))
   SetGadgetText(#String_FirstName, GetContactFirstName(*ObjectA))
   SetGadgetText(#String_Town, GetContactTown(*ObjectA))
   SetGadgetText(#String_Number, GetContactNumber(*ObjectA))
   SetGadgetText(#String_Street, GetContactStreet(*ObjectA))
   SetGadgetText(#String_PostalCode, GetContactPostalCode(*ObjectA))  
   SetGadgetText(#String_Email, GetContactEmail(*ObjectA))
   SetGadgetText(#String_HousePhone, GetContactPhone01(*ObjectA))
   SetGadgetText(#String_WorkPhone, GetContactPhone02(*ObjectA))
   SetGadgetText(#Editor_Comments, GetContactComments(*ObjectA))
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure FirstPlugging(List MyList.Contact())
   
   ; We sort the Address book by FirstName
   SortStructuredList(MyList(), #PB_Sort_Ascending, SortContactFirstName(), #PB_String)
   
   ; We clear ComboBox listing all contacts
   ClearGadgetItems(#ComboBox_FirstNameList)
   
   ; We fill the combobox gadget with address book Contact FirstName
   ForEach MyList()
      
      AddGadgetItem(#ComboBox_FirstNameList, - 1, GetContactFirstName(MyList()))
      
   Next 
   
   ; We show the first contact in the address book
   SelectElement(MyList(),0)
   PlugContactInGUI(MyList())
   SetGadgetState(#ComboBox_FirstNameList,0)
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure CreateFakeAdressBook(*Object.AddressBook, List MyList.Contact())
   
   If FileSize(GetAddressBookFileName(*Object)) = -1
      
      ClearList(MyList()) ; ---> ? ? ? ? ? ? Not needed I think
      
      ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; It's just for creating a fake address book in case we don't have one.
      
      AddElement(MyList())
      UpdateContact(MyList(),"Runner","Road", "Texas","3215","55th HighWay", "J0W 3P3","roadrunner@superfast.com","123-4587","987-6543","Personnage de dessin animé. Il court très très vite.")

      AddElement(MyList())
      UpdateContact(MyList(),"Coyote","Will","Texas","3225","55th HighWay", "J0W 3P7","vilcoyote@superfast.com","569-3387","987-4443","Personnage de dessin animé. Il tente d'attaper Road Runner !")
      
      SetAddressBookContactQTY(*Object, 1)
      
   EndIf 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure LoadAddressBook(*ObjectA.AddressBook, List MyList.Contact())
   
   If ReadFile(0, GetAddressBookFileName(*ObjectA)) <> 0
      
      Repeat
         
         AddElement(MyList())
         ReadContact(0, MyList())
         
      Until Eof(0)
      
      SetAddressBookContactQTY(*ObjectA, ListSize(MyList()) - 1)
      
      CloseFile(0)
      
   Else 
      
      CreateFakeAdressBook(*ObjectA, MyList())
      
   EndIf 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure SaveAddressBook(*ObjectA.AddressBook, List MyList.Contact())
   
   If CreateFile(0, GetAddressBookFileName(*ObjectA))
      
      ForEach MyList()
         
         WriteContact(0, MyList())
         
      Next
      
      CloseFile(0)
      
   EndIf 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure SelectContact(*ObjectA.AddressBook, List MyList.Contact())
   
   Index = GetGadgetState(#ComboBox_FirstNameList)
   
   If Index <> - 1
      SelectElement(MyList(),Index)
      PlugContactInGUI(MyList())
      SetAddressBookContactID(*ObjectA, Index)
   EndIf 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure ScrollContact(*ObjectA.AddressBook, List MyList.Contact(), Direction.b)
   
   Index =  GetAddressBookContactID(*ObjectA)
   Scrolling(Index, 0, GetAddressBookContactQTY(*ObjectA), Direction)
   
   SetGadgetState(#ComboBox_FirstNameList,Index)
   SelectElement(MyList(),Index)
   PlugContactInGUI(MyList())
   SetAddressBookContactID(*ObjectA, Index)
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure DeleteContact(*ObjectA.AddressBook, List MyList.Contact())
   
   Index = GetGadgetState(#ComboBox_FirstNameList)
   SelectElement(MyList(),Index)
   
   msg.s = TextGUI(#TxtGUI_WarningMessage00) + Chr(10)+Chr(13)
   msg + ReplaceString(TextGUI(#TxtGUI_WarningMessage01),"%NAME%",GetContactFirstName(MyList()) + " " +GetContactLastName(MyList()))
   
   If MessageRequester(TextGUI(#TxtGUI_ProgrName), msg, #MB_YESNO) = #IDYES
      
      DeleteElement(MyList())
      SetAddressBookContactQTY(*ObjectA, GetAddressBookContactQTY(*ObjectA) - 1)
      RemoveGadgetItem(#ComboBox_FirstNameList, Index)
      SetGadgetState(#ComboBox_FirstNameList,0)
      
      If ListSize(MyList()) <> 0
         
         SelectContact(*ObjectA, MyList())
         
      Else 
         
         PlugContactInGUI(NULL.Contact)
         
      EndIf 
      
   EndIf 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure TransferInfo()
   
   GadgetIDSource = #String_LastName
   
   For GadgetID = #String_EDIT_LastName To #Editor_EDIT_Comments
      
      SetGadgetText(GadgetID,GetGadgetText(GadgetIDSource))
      
      GadgetIDSource+1
      
   Next 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure EditContact(*ObjectA.AddressBook, Mode.b, List MyList.Contact())
   
   LastName.s = GetGadgetText(#String_EDIT_LastName)
   FirstName.s = GetGadgetText(#String_EDIT_FirstName)
   Town.s = GetGadgetText(#String_EDIT_Town)
   Number.s = GetGadgetText(#String_EDIT_Number)
   Street.s = GetGadgetText(#String_EDIT_Street)
   PostalCode.s = GetGadgetText(#String_EDIT_PostalCode)
   Email.s = GetGadgetText(#String_EDIT_Email)
   Phone01.s = GetGadgetText(#String_EDIT_HousePhone)
   Phone02.s = GetGadgetText(#String_EDIT_WorkPhone)
   Comments.s = GetGadgetText(#Editor_EDIT_Comments)
   
   If Mode = 1
      
      SelectElement(MyList(), GetAddressBookContactID(*ObjectA))
      
   Else 
      
      AddElement(MyList())
      SetAddressBookContactQTY(*ObjectA, GetAddressBookContactQTY(*ObjectA)+1)
      
   EndIf 
   
   UpdateContact(MyList(), LastName, FirstName, Town, Number, Street, PostalCode, Email, Phone01, Phone02, Comments)
   
   FirstPlugging(MyList())
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure ButtonGadgetGroup(FirstButtonID, LastButtonID, X, Y, Width, Height, Gap_Between_Gadget)
   
   For GadgetID = FirstButtonID To LastButtonID
       ButtonGadget(GadgetID, X, Y, Width, Height, "")
      X + Width + Gap_Between_Gadget
   Next
    
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure TextGadgetGroup(FirstTextID, LastTextID, X, Y, Width, Height, Gap_Between_Gadget)
   
   For GadgetID = FirstTextID To LastTextID
      TextGadget(GadgetID, X, Y, Width, Height, "",#PB_Text_Center)
      Y + Height + Gap_Between_Gadget
   Next
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure StringGadgetGroup(FirstStringID, LastStringID, X, Y, Width, Height, Gap_Between_Gadget)
    
    For GadgetID = FirstStringID To LastStringID
       StringGadget(GadgetID, X, Y, Width, Height, "", #PB_String_ReadOnly)
       Y + Height + Gap_Between_Gadget
    Next
    
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure StringGadgetGroupSTD(FirstStringID, LastStringID, X, Y, Width, Height, Gap_Between_Gadget)
   
   For GadgetID = FirstStringID To LastStringID
      StringGadget(GadgetID, X, Y, Width, Height, "")
      Y + Height + Gap_Between_Gadget
   Next
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure EditContactWin(*ObjectA.AddressBook, Title.s, Mode.b, List MyList.Contact())
   
   HideWindow(#Main_Win,1)
   
   If OpenWindow(#Edit_Win, 0, 0, 500, 480, Title, #Edit_Win_Style)
      
    
       
      TextGadgetGroup(#Text_EDIT_LastName,#Text_EDIT_WorkPhone, 5, 5, 130, 20, 5)
      StringGadgetGroupSTD(#String_EDIT_LastName,#String_EDIT_WorkPhone, 145, 5, 350, 20, 5)
      EditorGadget(#Editor_EDIT_Comments,5,GadgetY(#String_EDIT_WorkPhone)+30,490,200)
      
      ButtonGadgetGroup(#Btn_EDIT_OK, #Btn_EDIT_CANCEL, 275, 445, 100, 25, 5)
      SetGadgetText(#Btn_EDIT_OK,TextGUI(#TxtGUI_EditOk))
      SetGadgetText(#Btn_EDIT_CANCEL,TextGUI(#TxtGUI_EditCancel))
       

      
   EndIf
   
   TxtGUI_Index = #TxtGUI_LastName
   
   For GadgetID = #Text_EDIT_LastName To #Text_EDIT_WorkPhone
      
      SetGadgetText(GadgetID,TextGUI(TxtGUI_Index))
      TxtGUI_Index + 1
      
   Next 
   
   If Mode = 1
      TransferInfo()
   EndIf 
   
   Repeat
      
      EventID = WindowEvent()
      
      If EventID = #PB_Event_Gadget
         
         Select EventGadget()                 
            
            Case #Btn_EDIT_OK
               EditContact(*ObjectA,Mode, MyList())
               EventID = #PB_Event_CloseWindow
               
            Case #Btn_EDIT_CANCEL
               EventID = #PB_Event_CloseWindow
               
         EndSelect
      EndIf
      
   Until EventID = #PB_Event_CloseWindow
   
   HideWindow(#Main_Win,0)
   
   CloseWindow(#Edit_Win)
   
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<
; <<<< END OF FILE <<<<
; <<<<<<<<<<<<<<<<<<<<<
And finally save this one as : MainWindow and EventManager.pb

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Address Book Exemple
; File : Main window and the EventManager
; Version : 1.5.0
; Programmation : OK - Complete
; Programmed by : Guimauve
; Date : 27-08-2005
; Last update : 19-08-2013 by Demivec
; Coded for PureBasic V5.20
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure AddressBookWin()
   
   If OpenWindow(#Main_Win, 0, 0, 600, 480, TextGUI(#TxtGUI_ProgrName), #Main_Win_Style) <> 0
      
      ComboBoxGadget(#ComboBox_FirstNameList,5,5,200,20)
      ButtonGadgetGroup(#Btn_Previous, #Btn_New, 210, 5, 75, 25, 2)
      
      FrameGadget(#Frame_Info,5,35,590,250,"")
      TextGadgetGroup(#Text_LastName,#Text_WorkPhone, 10, 55, 130, 20, 5)
      StringGadgetGroup(#String_LastName,#String_WorkPhone, 145, 55, 440, 20, 5)
      
      FrameGadget(#Frame_Comments,5,290,590,185,"")
      
      EditorGadget(#Editor_Comments,10,310,580,160)
      SendMessage_(GadgetID(#Editor_Comments), #EM_SETOPTIONS, #ECOOP_OR, #ECO_READONLY)
      SendMessage_(GadgetID(#Editor_Comments), #EM_SETBKGNDCOLOR, 0, GetSysColor_(#COLOR_3DFACE))
         
   EndIf
   
   TxtGUI_Index = #TxtGUI_BtnPrevious
   
   For GadgetID = #Btn_Previous To #Text_WorkPhone
      
      SetGadgetText(GadgetID,TextGUI(TxtGUI_Index))
      TxtGUI_Index + 1
      
   Next 
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programmation - OK

Procedure AddressBookEventManager(*ObjectA.AddressBook)
   
   ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   ; Create Contact linked list
   
   Protected NewList MyAddressBook.Contact()
   
   ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   ; We load the address book from HardDrive
   
   LoadAddressBook(*ObjectA, MyAddressBook())
   
   ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   ; Display the first conctact + Miscallineous setting
   
   FirstPlugging(MyAddressBook())
   
   Repeat
      
      EventID = WindowEvent()
      
      Select EventID
         
         Case #PB_Event_Gadget
            
            Select EventGadget()
               
               Case #ComboBox_FirstNameList
                  SelectContact(*ObjectA, MyAddressBook())
                  
               Case #Btn_Previous
                  ScrollContact(*ObjectA, MyAddressBook(), -1)
                  
               Case #Btn_Next
                  ScrollContact(*ObjectA, MyAddressBook(), 1)
                  
               Case #Btn_New
                  EditContactWin(*ObjectA, TextGUI(#TxtGUI_BtnNew),0, MyAddressBook())
                  
               Case #Btn_Modify
                  EditContactWin(*ObjectA, TextGUI(#TxtGUI_BtnModify),1, MyAddressBook())
                  
               Case #Btn_Delete
                  DeleteContact(*ObjectA, MyAddressBook())
                  
            EndSelect
            
            ; Case #PB_Event_Menu ; We don't have Menu for the moment
            ; Select EventMenu()
            ; EndSelect
            
      EndSelect
      
   Until EventID = #PB_Event_CloseWindow
   
   ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   ; Before we close the program we automatically
   ; save the address book on hard drive to make 
   ; sure we always have an up to date address book.
   
   SaveAddressBook(*ObjectA, MyAddressBook())
   
   End
   
EndProcedure 

; <<<<<<<<<<<<<<<<<<<<<
; <<<< END OF FILE <<<<
; <<<<<<<<<<<<<<<<<<<<<
The updated working Copy operator and the Debug operator code were also included in the Constant Structures.pb file.

Enjoy! :)
Post Reply