What i don't like and what confuses me is, that there are several numbers needed when coding with XML Dialogs (e.g. Dialog Number, Window Number, XML Number).
Thats chaos for me and so i wrote a Module which makes the handling easier.
The Module is additionally also an Object/a Class, that is, you can have more instances of the same dialog, all of these mentioned numbers are now in one hand, encapsulated in that module, more, in that that instance.
A bit tricky appeared to be the connection to the public instance variables. I needed a second pointer with the same name of the object but with a '*' in front of it. Honestly, I can live with that solution.
Well, I didn't write the Class by hand, I wrote a small (and very naive implemented) Object Precompiler, it wrote the Class for me in a PB readable and compileable form.
I think I will make it possible to implement Modules without that OOP component also with my precompiler, simply because the declares and so on are a bit awkward, well, let's come back to topic.
At first the source class prior using the precompiler:
Code: Select all
Module Dialog
Structure object
window_no.i As Public
dialog_no.i As Public
xml_no.i As Public
EndStructure
Public Class xml_number ; Xml Number after NewCatch
; Helpers
Class Procedure CheckXMLTree(xml_no)
If xml_no=0 Or XMLStatus(xml_no)<>#PB_XML_Success
MessageRequester("XML error",
"XML error: " + XMLError(xml_no) + " (Line: " + XMLErrorLine(xml_no) + ")")
End
EndIf
EndProcedure
Class Procedure OpenXMLDialog_(xml_no, window_name.s, x, y)
Protected dialog_no = CreateDialog(#PB_Any)
If OpenXMLDialog(dialog_no, xml_no, window_name, x, y)=0
MessageRequester("Dialog error",
"Dialog error: " + DialogError(dialog_no))
End
EndIf
ProcedureReturn dialog_no
EndProcedure
; Constructors, Open the window
Public Class Procedure NewCatchXML(window_name.s, adress, size, free_xml=#False, x=0, y=0)
; new instance
Protected *this.object = _New_()
; create xml tree & check it
*this\xml_no = CatchXML(#PB_Any, adress, size)
xml_number = *this\xml_no
CheckXMLTree(*this\xml_no)
; create dialog window
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
If free_xml
FreeXML(*this\xml_no)
*this\xml_no = 0
xml_number = 0
EndIf
; get window number (e.g. EventWindow() = window_no)
*this\window_no = DialogWindow(*this\dialog_no)
; returns instance pointer
ProcedureReturn *this
EndProcedure
Public Class Procedure NewParseXML(window_name.s, string_name.s, free_xml=#False, x=0, y=0)
; new instance
Protected *this.object = _New_()
; create xml tree & check it
*this\xml_no = ParseXML(#PB_Any, string_name)
xml_number = *this\xml_no
CheckXMLTree(*this\xml_no)
; create dialog dialog
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
If free_xml
FreeXML(*this\xml_no)
*this\xml_no = 0
xml_number = 0
EndIf
; get window number (for e.g. EventWindow() = window_no)
*this\window_no = DialogWindow(*this\dialog_no)
; returns instance pointer
ProcedureReturn *this
EndProcedure
Public Class Procedure New(window_name.s, xml_no, x=0, y=0, free_xml=#False)
; new instance
Protected *this.object = _New_()
; check xml tree
*this\xml_no = xml_no
CheckXMLTree(xml_no)
; create dialog dialog
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
; get window number (for e.g. EventWindow() = window_no)
*this\window_no = DialogWindow(*this\dialog_no)
; returns instance pointer
ProcedureReturn *this
EndProcedure
; Destructor
Public Procedure Free(*this.object)
FreeDialog(*this\dialog_no)
_Free_(*this)
EndProcedure
EndModule
; Create a dialog Object with:
; Global Window.Dialog
; Window = Dialog::NewCatch(..)
; Create pointer to private instance variables (if needed)
; dont use _method_table_:
; Global *Window.Dialog::Variables
; *Window = Window <--- important
; Free:
; Window = Dialog::Free()
; -> Window will be 0, and can be tested
Code: Select all
; Instance Interface of Object with Public Instance Procedures (Methods)
Interface Dialog
Free.i()
EndInterface
DeclareModule Dialog
EnableExplicit
; Public Enumeration
; Public Structure
; Structure of Public Instance Variables (Attributes)
Structure Variables
*_method_table_
window_no.i
dialog_no.i
xml_no.i
EndStructure
; Declare Public Class Variables (Attributes)
Global xml_number.i
; Declare Public Class Procedures (Methods)
Declare.i New(window_name.s, xml_no, x=0, y=0, free_xml=#False)
Declare.i NewCatchXML(window_name.s, adress, size, free_xml=#False, x=0, y=0)
Declare.i NewParseXML(window_name.s, string_name.s, free_xml=#False, x=0, y=0)
EndDeclareModule
Module Dialog
EnableExplicit
; Private Enumeration
; Private Structure
; Structure of Private Instance Variables (Attributes)
Structure object Extends Variables
EndStructure
; Declare Private Class Variables (Attributes)
; Declare all Instance & Private Class Procedures (Methods)
Declare.i OpenXMLDialog_(xml_no, window_name.s, x, y)
Declare.i CheckXMLTree(xml_no)
Declare.i _Free_(*this.object)
Declare.i _New_()
Declare.i Free(*this.object)
; Procedures (Methods) Implementation
Procedure.i OpenXMLDialog_(xml_no, window_name.s, x, y)
Protected dialog_no = CreateDialog(#PB_Any)
If OpenXMLDialog(dialog_no, xml_no, window_name, x, y)=0
MessageRequester("Dialog error",
"Dialog error: " + DialogError(dialog_no))
End
EndIf
ProcedureReturn dialog_no
EndProcedure
Procedure.i CheckXMLTree(xml_no)
If xml_no=0 Or XMLStatus(xml_no)<>#PB_XML_Success
MessageRequester("XML error",
"XML error: " + XMLError(xml_no) + " (Line: " + XMLErrorLine(xml_no) + ")")
End
EndIf
EndProcedure
Procedure.i New(window_name.s, xml_no, x=0, y=0, free_xml=#False)
Protected *this.object = _New_()
*this\xml_no = xml_no
CheckXMLTree(xml_no)
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
*this\window_no = DialogWindow(*this\dialog_no)
ProcedureReturn *this
EndProcedure
Procedure.i _Free_(*this.object)
FreeStructure(*this)
EndProcedure
Procedure.i Free(*this.object)
FreeDialog(*this\dialog_no)
_Free_(*this)
EndProcedure
Procedure.i _New_()
Protected *new.object = AllocateStructure(object)
If *new: *new\_method_table_ = ?method_table
Else: MessageRequester("Runtime Class Allocation Error","Allocation of Object 'Dialog' failed."): End
EndIf
ProcedureReturn *new
EndProcedure
Procedure.i NewCatchXML(window_name.s, adress, size, free_xml=#False, x=0, y=0)
Protected *this.object = _New_()
*this\xml_no = CatchXML(#PB_Any, adress, size)
xml_number = *this\xml_no
CheckXMLTree(*this\xml_no)
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
If free_xml
FreeXML(*this\xml_no)
*this\xml_no = 0
EndIf
*this\window_no = DialogWindow(*this\dialog_no)
ProcedureReturn *this
EndProcedure
Procedure.i NewParseXML(window_name.s, string_name.s, free_xml=#False, x=0, y=0)
Protected *this.object = _New_()
*this\xml_no = ParseXML(#PB_Any, string_name)
xml_number = *this\xml_no
CheckXMLTree(*this\xml_no)
*this\dialog_no = OpenXMLDialog_(*this\xml_no, window_name, x, y)
If free_xml
FreeXML(*this\xml_no)
*this\xml_no = 0
EndIf
*this\window_no = DialogWindow(*this\dialog_no)
ProcedureReturn *this
EndProcedure
; DataSection of Public Instance Procedures (Methods)
DataSection
method_table:
Data.i @Free()
EndDataSection
EndModule
Greetings Tom and thank you

Testcode in next posting
This is also posted in german forum: http://forums.purebasic.com/german/view ... d6#p340305