[TUTO] Programmation structurée

Informations pour bien débuter en PureBasic
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: [TUTO] Programmation structurée

Message par microdevweb »

Le module Countries_TAB

Code : Tout sélectionner

;********************************************************
; TUTO FAC
;********************************************************
; Author          : MicrodevWeb
; MODULE          : Countries_TAB
; VERSION         : 1
; DESIGNED WITH   : PB 5.62
; DOT IT          : liste des pays
;********************************************************
DeclareModule Countries_TAB
  Declare open(motherWindow)
EndDeclareModule

Module Countries_TAB
  EnableExplicit
  Global XML,DIALOG,ID_FORM,MOTHER_FORM
  Global tb,bt_new,bt_edit,bt_delete,bt_exit,currentID = -1
  
  
  ;-* PRIVATE FUNCTIONS
  Procedure makeXml()
    Protected txt.s = "<window name = 'form' width = '800' height = '600' text = 'Liste des Pays'"+
                      " flags = '#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered'>"+
                      "  <hbox expand ='item:1'>"+
                      "     <vbox>"+
                      "         <listicon name = 'tb' text = 'Nom'  flags = '#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection '/>"+
                      "     </vbox>"+
                      "     <vbox expand ='no' width= '100'>"+
                      "         <button text = 'Nouveau' name = 'bt_new'/>"+
                      "         <button text = 'Editer' name = 'bt_edit'/>"+
                      "         <button text = 'Supprimer' name = 'bt_delete'/>"+
                      "         <empty/>"+
                      "         <button text = 'Quitter' name = 'bt_exit'/>"+
                      "     </vbox>"+
                      "  </hbox>"+
                      "</window>"
    XML = CatchXML(#PB_Any,@txt,StringByteLength(txt))
    If Not XML
      MessageRequester("XML ERROR","Cannot catch xml",#PB_MessageRequester_Error)
      End
    EndIf
  EndProcedure
  
  Procedure fillTable()
    Protected i
    Countries_DB::load()
    Countries_DB::resetRecord()
    While Countries_DB::nextRecord()
      AddGadgetItem(tb,-1,Countries_DB::getName())
      SetGadgetItemData(tb,i,Countries_DB::getId())
      i + 1
    Wend
  EndProcedure
  
  Procedure eventExit()
    CloseWindow(ID_FORM)
    DisableWindow(MOTHER_FORM,#False)
    SetActiveWindow(MOTHER_FORM)
  EndProcedure
  
  Procedure eventNew()
    currentID = -1
    Country_FIC::open(ID_FORM)
  EndProcedure
  
  Procedure eventEdit()
    If GetGadgetState(tb) <> -1
      currentID = GetGadgetState(tb) 
      Country_FIC::open(ID_FORM,GetGadgetItemData(tb,GetGadgetState(tb)))
    EndIf
  EndProcedure
  
  Procedure eventDelete()
    
  EndProcedure
  
  Procedure refreshTable()
    If currentID <> -1 ; mode édition
      SetGadgetItemText(tb,currentID,Countries_DB::getName())
    Else ; mode new
      AddGadgetItem(tb,-1,Countries_DB::getName())
      SetGadgetItemData(tb,CountGadgetItems(tb) - 1,Countries_DB::getId())
    EndIf
  EndProcedure
  ;}
  
  ;-* PUBLIC FUNCTIONS
  Procedure open(motherWindow)
    MOTHER_FORM = motherWindow
    makeXml()
    DIALOG = CreateDialog(#PB_Any)
    If OpenXMLDialog(DIALOG,XML,"form",0,0,0,0,WindowID(MOTHER_FORM))
      DisableWindow(MOTHER_FORM,#True)
      ; lecture des id
      ID_FORM = DialogWindow(DIALOG)
      tb = DialogGadget(DIALOG,"tb")
      bt_new = DialogGadget(DIALOG,"bt_new")
      bt_edit = DialogGadget(DIALOG,"bt_edit")
      bt_delete = DialogGadget(DIALOG,"bt_delete")
      bt_exit = DialogGadget(DIALOG,"bt_exit")
      ; remplisage de la table
      fillTable()
      SetGadgetState(tb,0)
      ; mise en place des écouteurs
      BindEvent(#PB_Event_CloseWindow,@eventExit(),ID_FORM)
      BindGadgetEvent(bt_new,@eventNew())
      BindGadgetEvent(bt_edit,@eventEdit())
      BindGadgetEvent(bt_delete,@eventDelete())
      BindGadgetEvent(bt_exit,@eventExit())
    Else
      End
    EndIf
  EndProcedure
  ;}
  
  ; mise en place du callback du module fiche
  Country_FIC::setCallback(@refreshTable())
EndModule
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: [TUTO] Programmation structurée

Message par microdevweb »

Le module Country_FIC.pbi

Code : Tout sélectionner

;********************************************************
; TUTO FAC
;********************************************************
; Author          : MicrodevWeb
; MODULE          : Country_FIC
; VERSION         : 1
; DESIGNED WITH   : PB 5.62
; DOT IT          : Fiche d'un pays
;********************************************************
DeclareModule Country_FIC
  Declare open(motherWindow,current_id = 0)
  Declare setCallback(*callback)
EndDeclareModule

Module Country_FIC
  Prototype refreshTable()
  Global XML,DIALOG,ID_FORM,MOTHER_FORM,bt_save,bt_cancel,st_name,gCurrent_id,title.s,refreshTable.refreshTable
  
  EnableExplicit
  ;-* PRIVATE FUNCTIONS
  Procedure makeXml()
    Protected txt.s = "<window name = 'form' width = '300' text = "+Chr(34)+title+Chr(34)+
                      " flags = '#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered'>"+
                      "  <vbox expand = 'item:1'>"+
                      "     <vbox expand = 'horizontal'>"+
                      "       <frame text = 'Nom'>"+
                      "         <string name = 'st_name'/>"+
                      "       </frame>"+
                      "     </vbox>"+
                      "     <hbox expand = 'no'>"+
                      "       <button name = 'bt_save' text = 'Valider'/>"+
                      "       <button name = 'bt_cancel' text = 'Annuler'/>"+
                      "     </hbox>"+
                      "  </vbox>"+
                      "</window>"
    XML = CatchXML(#PB_Any,@txt,StringByteLength(txt))
    If Not XML
      MessageRequester("XML ERROR","Cannot catch xml",#PB_MessageRequester_Error)
      End
    EndIf
  EndProcedure
  
  Procedure fillFields()
    If Countries_DB::find(gCurrent_id)
      SetGadgetText(st_name,Countries_DB::getName())
    EndIf
  EndProcedure
  
  Procedure eventExit()
    CloseWindow(ID_FORM)
    DisableWindow(MOTHER_FORM,#False)
    SetActiveWindow(MOTHER_FORM)
  EndProcedure
  
  Procedure eventSave()
    ; le nom est obligatoire
    If Not Len(GetGadgetText(st_name))
      MessageRequester("Champ obligatoire","Le nom est obligatoire")
      SetActiveGadget(st_name)
      ; pas de doublon sur le nom
    ElseIf Countries_DB::nameExist(GetGadgetText(st_name),gCurrent_id)
      MessageRequester("Erreur de doublon","Le nom existe déjà")
      SetActiveGadget(st_name)
    Else
      If gCurrent_id ; mode édition
        Countries_DB::setName(GetGadgetText(st_name))
        Countries_DB::save()
      Else ; mode nouveau
        Countries_DB::add(GetGadgetText(st_name))
      EndIf
      refreshTable()
      eventExit()
    EndIf
  EndProcedure
  ;}
  ;-* PUBLIC FUNCTIONS
  Procedure setCallback(*callback)
    refreshTable = *callback
  EndProcedure
  
  Procedure open(motherWindow,current_id = 0)
    gCurrent_id = current_id
    If current_id
      title = "Edition d'un pays"
    Else
      title = "Nouveau pays"
    EndIf
    MOTHER_FORM = motherWindow
    makeXml()
    DIALOG = CreateDialog(#PB_Any)
    If OpenXMLDialog(DIALOG,XML,"form",0,0,0,0,WindowID(MOTHER_FORM))
      DisableWindow(MOTHER_FORM,#True)
      ; lecture des id
      ID_FORM = DialogWindow(DIALOG)
      st_name = DialogGadget(DIALOG,"st_name")
      bt_save = DialogGadget(DIALOG,"bt_save")
      bt_cancel = DialogGadget(DIALOG,"bt_cancel")
      ; remplisage du champ
      If current_id
        fillFields()
      EndIf
      ; mise en place des écouteurs
      BindEvent(#PB_Event_CloseWindow,@eventExit(),ID_FORM)
      BindGadgetEvent(bt_cancel,@eventExit())
      BindGadgetEvent(bt_save,@eventSave())
    Else
      End
    EndIf
  EndProcedure
  ;}
EndModule
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
microdevweb
Messages : 1798
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

Re: [TUTO] Programmation structurée

Message par microdevweb »

Le main pour le teste (model et view)

Code : Tout sélectionner

;********************************************************
; TUTO FAC
;********************************************************
; Author          : MicrodevWeb
; MODULE          : Main
; VERSION         : 1
; DESIGNED WITH   : PB 5.62
; DOT IT          : Fichier principal 
;********************************************************

IncludePath "Model/"
XIncludeFile "DB.pbi"
XIncludeFile "Countries_DB.pbi"
XIncludeFile "Cities_DB.pbi"
XIncludeFile "Contacts_DB.pbi"
IncludePath "View/"
XIncludeFile "Country_FIC.pbi"
XIncludeFile "Countries_TAB.pbi"
XIncludeFile "Cities_TAB.pbi"
XIncludeFile "MainForm.pbi"
; création de la base de données
If Not DB::baseExists()
  Countries_DB::create()
  Cities_DB::create()
  Contacts_DB::create()
EndIf

MainForm::open()
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
omega
Messages : 617
Inscription : sam. 26/nov./2011 13:04
Localisation : Alger

Re: [TUTO] Programmation structurée

Message par omega »

Bonjour Microdevweb
As-tu reçu mon message en MP?
Win7 (x64) 64 bits Pb 5.72
Répondre