Basic main.pb and 2 Form Windows Code

Share your advanced PureBasic knowledge/code with the community.
SniffTheGlove
Enthusiast
Enthusiast
Posts: 122
Joined: Sat Nov 19, 2011 6:51 pm

Basic main.pb and 2 Form Windows Code

Post by SniffTheGlove »

For a while I have been coding in Console to do the jobs I needed. However I now wanted to do some windowed forms but I struggled to get a simple basic setup. The helps file are a little short and there are some snippets around in the forum for a 2 window setup but I got stuck and had to ask.

So I have now finished a simple 2 window setup that is compliant with the quirks of the Form Designer.

You can design your window forms in Forms Designer and not worry about Forms Designer changing to code as you switch between design/code mode.

I am having to use Global Variables to get the variables to work between your main.pb and the window forms.pbf
Hopefully the enumeration request I asked for may happen and then you can use enumerations instead of Globals.
http://www.purebasic.fr/english/viewtop ... 22&t=60582

Any way here is my basic project code.
1 main.pb
1 mainwindow.pbf
1 aboutwindow.pbf

main.pb has these setup for use.
Enable Explicit
Error Handling
Debug Info
Logging Info
Networking
SQLLite Database
If you don't need these then just delete the sections as required.

Code here is the code for main.pb

Code: Select all

; #############################################################################
; ### Enables explicit mode. All variables which are not explicitly declared with Define, Global, Protected or Static are not accepted
EnableExplicit
; #############################################################################
; ### Declaring the error handler
Declare HandleOppsError()
; #############################################################################
; ### Declaring the Debug mode messenger
Declare DebugInfo(DebugMessage.s)
; #############################################################################
; ### Initiate the error handler
OnErrorCall(@HandleOppsError())
; #############################################################################
; ### Initiate Network (TCP/IP Stack)
InitNetwork()
; #############################################################################
; ### Initiate the SQLite Database
UseSQLiteDatabase()
; #############################################################################
; ### Main Global Variables
Global Application_Log_File.s
Global Application_Debug.s
CompilerIf #PB_Compiler_Debugger
  Global Current_Directory.s = GetCurrentDirectory()
CompilerElse
  Global Current_Directory.s = GetPathPart(ProgramFilename())
CompilerEndIf
; #############################################################################
; ### Enumerate the main constants
Enumeration
  #MyLogFileNumber
  #MyErrorLogFileNumber
EndEnumeration
; #############################################################################
; ### Main error logging to file
Procedure WriteToErrorLogFile(Error_Log_File_Message.s)
  WriteString(#MyErrorLogFileNumber, FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date()) + " **ERROR** " + Error_Log_File_Message.s + #CRLF$)
  If Application_Debug.s = "Y"
    DebugInfo(" **ERROR** " + Error_Log_File_Message.s)
  EndIf
EndProcedure
; #############################################################################
; ### Main logging to file
Procedure WriteToLogFile(Log_File_Message.s)
  If Application_Log_File.s = "Y"
    WriteString(#MyLogFileNumber, FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date()) + " " + Log_File_Message.s + #CRLF$)
    If Application_Debug.s = "Y"
      DebugInfo(Log_File_Message.s)
    EndIf
  EndIf
EndProcedure
; #############################################################################
; ### Main procedure for error checking built-in procedures
Procedure HandleError(Result.l, Text.s)
  Define t_Result.l
  If Result.l = 0
    t_Result.l = WriteToErrorLogFile("Handle Error: " + Text.s)
    MessageRequester("Error", FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date()) + " HandleError: " + Text.s, #PB_MessageRequester_Ok)
    End
  Else
    ProcedureReturn Result.l
  EndIf
EndProcedure
; #############################################################################
; ### Program error handler
Procedure HandleOppsError()
  Define t_Result.l
  t_Result.l = WriteToErrorLogFile("Handle Opps Error: An Opps error occured: " + ErrorMessage())
  MessageRequester("Error", FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date()) + " Handle Opps Error: An Opps error occured: " + ErrorMessage(), #PB_MessageRequester_Ok)
  End
EndProcedure
; #############################################################################
; ### Main logging of Debug Information to the debug window
Procedure DebugInfo(Debug_Message.s)
  Debug FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date()) + " *** " + Debug_Message.s
EndProcedure
; #############################################################################
; ### Generate a current Date/Time Timestamp
Procedure.s CurrentDateTime_Timestamp()
  ProcedureReturn FormatDate("%yyyy/%mm/%dd - %hh:%ii:%ss", Date())
EndProcedure
; #############################################################################
; ### Generate current Timestamp
Procedure.l CurrentTimestamp()
  ProcedureReturn Date()
EndProcedure
; #############################################################################
; ### Check if the Database update worked
Procedure CheckDatabaseUpdate(Database.l, t_SQLQueryString.s)
  Define Result.l
  WriteToLogFile("CheckDatabaseUpdate: " + Str(Database.l) + "|" + t_SQLQueryString.s)
  Result.l = DatabaseUpdate(Database, t_SQLQueryString.s)
  If Result.l = 0
    WriteToLogFile("Error " + DatabaseError() + "CheckDatabaseUpdate: " + Str(Database.l) + "|" + t_SQLQueryString.s)
  EndIf
  ProcedureReturn Result
EndProcedure
; #############################################################################
; ### Checked if the Database query worked
Procedure CheckDatabaseQuery(Database.l, t_SQLQueryString.s)
  Define Result.l
  WriteToLogFile("CheckDatabaseQuery: " + Str(Database.l) + "|" + t_SQLQueryString.s)
  Result.l = DatabaseQuery(Database, t_SQLQueryString.s)
  If Result.l = 0
    WriteToLogFile("Error " + DatabaseError() + "CheckDatabaseQuery: " + Str(Database.l) + "|" + t_SQLQueryString.s)
  EndIf
  ProcedureReturn Result
EndProcedure
; #############################################################################
; ### Checked if the File Exisits
Procedure FileExist(File.s)
  If FileSize(File) >= 0
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure
; #############################################################################
; ### Program Application Variables
Define Application_Name.s = "Default Project Code"
Define Application_Version_Main_Number.l = 1
Define Application_Version_Build_Number.l = #PB_Editor_BuildCount
Define Application_Version_Count_Number.l = #PB_Editor_CompileCount
Define Application_Version_Variant_Number.l = 0
Define Application_Version_About.s = "Version: " + Str(Application_Version_Main_Number.l) + " Build:" + Str(Application_Version_Build_Number.l) + " Revision: " + Str(Application_Version_Count_Number.l) + " Variant: " + Str(Application_Version_Variant_Number.l)
Define Application_Version.s = Str(Application_Version_Main_Number.l) + "." + Str(Application_Version_Build_Number.l) + "." + Str(Application_Version_Count_Number.l) + "." + Str(Application_Version_Variant_Number.l)
Define Application_User_Agent.s = Application_Name.s + "/" + Application_Version.s
Define Application_Preferences_File.s = Application_Name.s + ".prefs"
Define Application_Log_Filename.s = Application_Name.s + ".log"
Define Application_Error_Log_Filename.s = Application_Name.s + " Error.log"
Define Application_Event.l
Define Application_Window.l
Define Application_Gadget.l
Define Application_Menu.l
Define Application_Quit = #False
; #############################################################################
; ### Program Global Variables
Global AboutWindow_Title.s = "About Win"
; #############################################################################
; ### Program Database Variables
Define Application_Database_Path.s = Current_Directory.s 
Define Application_Database_Filename.s = Application_Name.s + ".sqlite"
Define Application_Database_Data_Table.s = ""
Define Database_Number.l = -1
Define SQLQueryString.s = ""
; #############################################################################
; ### Main Include Files
XIncludeFile "MainWindow.pbf"
XIncludeFile "AboutWindow.pbf"
; #############################################################################
; ### Create ErrorLogFile
CreateFile(#MyErrorLogFileNumber, Current_Directory.s + Application_Error_Log_Filename.s )
; #############################################################################
; ### Create LogFile
HandleError(CreateFile(#MyLogFileNumber, Current_Directory.s + Application_Log_Filename.s),"Sorry, I can not create the log file " + Application_Log_Filename.s)
; #############################################################################
; ### Create Preferences File
If FileExist(Current_Directory.s + Application_Preferences_File.s)
  HandleError(OpenPreferences(Current_Directory.s + Application_Preferences_File.s),"Sorry, I can not read the preferences file " + Application_Preferences_File.s)
  PreferenceGroup("Global")
  Application_Log_File.s = UCase(ReadPreferenceString ("Application_Log_File", "N"))
  Application_Debug.s = UCase(ReadPreferenceString ("Application_Debug", "N"))
  ClosePreferences()
Else
  HandleError(CreatePreferences(Current_Directory.s + Application_Preferences_File.s, #PB_Preference_GroupSeparator),"Sorry, I can not create the preferences file " + Application_Preferences_File.s)
  WriteToLogFile("Preferences file created")
  PreferenceGroup("Global")
  WritePreferenceString("Application_Log_File", "N")
  WritePreferenceString("Application_Debug_File", "N")
  Application_Log_File.s =  "N"
  Application_Debug.s =  "N"
  ClosePreferences()
EndIf
; #############################################################################
; ### Create Database file if necessary
HandleError(OpenFile(0, Application_Database_Path.s + Application_Database_Filename.s),"Sorry, I can not open the database file " + Application_Database_Path.s + Application_Database_Filename.s)
CloseFile(0)
; #############################################################################
; ### Open the database file
Database_Number.l = HandleError(OpenDatabase(#PB_Any, Application_Database_Path.s + Application_Database_Filename.s, "", ""),"Sorry, I can not open the database " + Application_Database_Path.s + Application_Database_Filename.s)
WriteToLogFile("Database Opened")
; #############################################################################
; ### Check is Database file is initalized
HandleError(IsDatabase(Database_Number.l),"Sorry, There is a problem with the database file " + Application_Database_Path.s + Application_Database_Filename.s)
WriteToLogFile("Database Initialized")
; #############################################################################
; ### Main Include Files
XIncludeFile "MainWindow.pbf"
XIncludeFile "AboutWindow.pbf"
; #############################################################################
; ### End of application setup and initializing
; #############################################################################
; ### Open main application Window and place gadgets
; #############################################################################
OpenMainWindow()
Repeat
  Application_Event = WaitWindowEvent()
  Application_Window = EventWindow()
  Application_Gadget = EventGadget()
  
  Select Application_Window
    Case #MainWindow
      Select Application_Event
        Case #PB_Event_CloseWindow
          Application_Quit = #True
        Case #PB_Event_Gadget
          Select Application_Gadget
            Case  MainWindow_Button_OK
              DisableWindow(#MainWindow, #True)
              OpenAboutWindow()
              SetGadgetText(AboutWindow_Text_Box_Application_Name, Application_Name.s)
          EndSelect
      EndSelect
    Case #AboutWindow
      Select Application_Event
        Case #PB_Event_CloseWindow
          CloseWindow(#AboutWindow)
          SetActiveWindow(#MainWindow)
          DisableWindow(#MainWindow, #False)
        Case #PB_Event_Gadget
          Select Application_Gadget
            Case  AboutWindow_Button_OK
              CloseWindow(#AboutWindow)
              SetActiveWindow(#MainWindow)
              DisableWindow(#MainWindow, #False)
          EndSelect
      EndSelect
  EndSelect
Until Application_Quit = #True
End
Here is the mainwindow.pbf

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global MainWindow_Button_OK

Enumeration FormWindow
  #MainWindow
EndEnumeration


Procedure OpenMainWindow(x = 0, y = 0, width = 170, height = 80)
  OpenWindow(#MainWindow, x, y, width, height, "", #PB_Window_SystemMenu)
  MainWindow_Button_OK = ButtonGadget(#PB_Any, 30, 30, 100, 25, "About")
EndProcedure

Here is the aboutwindow.pbf

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global AboutWindow_Button_OK, AboutWindow_Text_Box_Application_Name, AboutWindow_Text_Box_Copyright

Enumeration FormWindow
  #AboutWindow
EndEnumeration


Procedure OpenAboutWindow(x = 0, y = 0, width = 360, height = 150)
  OpenWindow(#AboutWindow, x, y, width, height, AboutWindow_Title.s, #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  AboutWindow_Button_OK = ButtonGadget(#PB_Any, 250, 110, 100, 25, "Close")
  AboutWindow_Text_Box_Application_Name = TextGadget(#PB_Any, 10, 10, 100, 25, "")
  AboutWindow_Text_Box_Copyright = TextGadget(#PB_Any, 10, 110, 100, 25, "")
EndProcedure

I hope this helps people who are learing PB and would like a small default project to start with.

Any suggestions, reviews, notes will be helpful.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Basic main.pb and 2 Form Windows Code

Post by Danilo »

SniffTheGlove wrote:

Code: Select all

; [...]

CompilerIf #PB_Compiler_Debugger
    Global Current_Directory.s = GetCurrentDirectory()
CompilerElse
    Global Current_Directory.s = GetPathPart(ProgramFilename())
CompilerEndIf

; [...]

; #############################################################################
; ### Create ErrorLogFile
CreateFile(#MyErrorLogFileNumber, Current_Directory.s + Application_Error_Log_Filename.s )
; #############################################################################
; ### Create LogFile
HandleError(CreateFile(#MyLogFileNumber, Current_Directory.s + Application_Log_Filename.s),"Sorry, I can not create the log file " + Application_Log_Filename.s)
; #############################################################################
; ### Create Preferences File
If FileExist(Current_Directory.s + Application_Preferences_File.s)
    HandleError(OpenPreferences(Current_Directory.s + Application_Preferences_File.s),"Sorry, I can not read the preferences file " + Application_Preferences_File.s)
    PreferenceGroup("Global")
    Application_Log_File.s = UCase(ReadPreferenceString ("Application_Log_File", "N"))
    Application_Debug.s = UCase(ReadPreferenceString ("Application_Debug", "N"))
    ClosePreferences()
Else
    HandleError(CreatePreferences(Current_Directory.s + Application_Preferences_File.s, #PB_Preference_GroupSeparator),"Sorry, I can not create the preferences file " + Application_Preferences_File.s)
    WriteToLogFile("Preferences file created")
    PreferenceGroup("Global")
    WritePreferenceString("Application_Log_File", "N")
    WritePreferenceString("Application_Debug_File", "N")
    Application_Log_File.s =  "N"
    Application_Debug.s =  "N"
    ClosePreferences()
EndIf

; [...]
Any suggestions, reviews, notes will be helpful.
Please note that you can't write to the program directory if it is protected, for example "C:\Program Files\YourApp\".

See help for 'GetHomeDirectory()' for a starting point:
Remarks

The home directory is the directory for the user data (preferences, plugins etc.) of the current user. Read and write access should be possible in this directory.
SniffTheGlove wrote:

Code: Select all

Procedure HandleError(Result.l, Text.s)
    Define t_Result.l
    
; [...]

Procedure CheckDatabaseUpdate(Database.l, t_SQLQueryString.s)
    Define Result.l
    
; [...]

Procedure CheckDatabaseQuery(Database.l, t_SQLQueryString.s)
    Define Result.l
  
; [...]

Define Application_Event.l
Define Application_Window.l
Define Application_Gadget.l
Define Application_Menu.l
I suggest to use type .i for all of your .l variables, or use no type at all, which defaults to .i (Integer). Think about 64bit compatibility.


Do you use Unicode compiler settings? It is probably not a bad idea to write a BOM after opening the error and log files, see: WriteStringFormat(#File, Format)
Post Reply