;
; ------------------------------------------------------------
;
;   PureBasic - MDI example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

; As MDIGadget is a Windows only gadget, it won't run on Linux or OS X
;
CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CompilerError "MDIGadget() is only supported on Windows"
CompilerEndIf


Structure MDIWindow
  ; info about the loaded image
  Image.i
  ImageWidth.l
  ImageHeight.l
  
  ; Our MDI Window  
  Window.i

  ; gadget numbers
  ScrollAreaGadget.i
  ImageGadget.i
EndStructure

Global NewList MDIWindow.MDIWindow()

#WINDOW = 0
#TOOLBAR = 0
#MENU = 0

#MDI_Base = 1

Enumeration
  #MENU_Open
  #MENU_Close
  #MENU_CloseAll
  #MENU_Quit
  
  #MENU_TileV
  #MENU_TileH
  #MENU_Cascade
  #MENU_Arrange
  #MENU_Previous
  #MENU_Next
  
  #MENU_FirstMDI
EndEnumeration

UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

Procedure SelectMDIWindow(Window)

  ForEach MDIWindow()
    If MDIWindow()\Window = Window
      ProcedureReturn 1
    EndIf
  Next

EndProcedure

#GADGET_MDI = 0

#WindowFlags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget

If OpenWindow(#WINDOW, 0, 0, 800, 600, "MDI ImageViewer", #WindowFlags)
  If CreateMenu(#MENU, WindowID(#WINDOW))
    MenuTitle("File")
      MenuItem(#MENU_Open, "Open")
      MenuItem(#MENU_Close, "Close")
      MenuItem(#MENU_CloseAll, "Close All")
      MenuBar()
      MenuItem(#MENU_QUit, "Quit")      
    MenuTitle("Windows")
      MenuItem(#MENU_TileV, "Tile vertically")
      MenuItem(#MENU_TileH, "Tile horizontally")
      MenuItem(#MENU_Cascade, "Cascade")
      MenuItem(#MENU_Previous, "Previous")
      MenuItem(#MENU_Next, "Next")
      
      ; MDI subwindows will get added here
  EndIf
  
  If CreateToolBar(#TOOLBAR, WindowID(#WINDOW))
    ToolBarStandardButton(#MENU_Open, #PB_ToolBarIcon_Open)
    ToolBarStandardButton(#MENU_Close, #PB_ToolBarIcon_Delete)
    ToolBarSeparator()
    ToolBarStandardButton(#MENU_Previous, #PB_ToolBarIcon_Undo)
    ToolBarStandardButton(#MENU_Next, #PB_ToolBarIcon_Redo)
  EndIf
  
  MDIGadget(#GADGET_MDI, 0, 0, 0, 0, 1, #MENU_FirstMDI, #PB_MDI_AutoSize)

  Quit = 0
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_CloseWindow
      Window = EventWindow()
      
      If Window = #WINDOW ; Out main window is closed, just quit
        Quit = 1
        
      Else ; It's one of our MDI children
      
        If SelectMDIWindow(Window) ; Check if it's one of our MDI window
          CloseWindow(Window)
          FreeImage(MDIWindow()\Image)
          DeleteElement(MDIWindow())   
        EndIf
    
      EndIf
    
    ElseIf Event = #PB_Event_Menu
      Select EventMenu()
        Case #MENU_Open
          FileName$ = OpenFileRequester("Open Image", DefautFile$, "Image Files (*.bmp,*.jpg,*.tiff,*.png,*.tga)|*.bmp;*.jpg;*.tiff;*.png;*.tga|All Files (*.*)|*.*", 0, #PB_Requester_MultiSelection)
          While FileName$
            DefaultFile$ = FileName$
            
            Image = LoadImage(#PB_Any, FileName$)
            If Image
            
              LastElement(MDIWindow())
              AddElement(MDIWindow())
              
              Item = ListIndex(MDIWindow())
              
              MDIWindow()\Image       = Image            
              MDIWIndow()\ImageWidth  = ImageWidth(Image)  ; ***
              MDIWindow()\ImageHeight = ImageHeight(Image)
              
              MDIWindow()\Window = AddGadgetItem(#GADGET_MDI, -1, FileName$)
              
                Width  = WindowWidth (MDIWindow()\Window)
                Height = WindowHeight(MDIWindow()\Window)
              
                MDIWindow()\ScrollAreaGadget = ScrollAreaGadget(#PB_Any, 0, 0, Width, Height, MDIWindow()\ImageWidth, MDIWindow()\ImageHeight, 10)
                  MDIWindow()\ImageGadget = ImageGadget(#PB_Any, 0, 0, MDIWindow()\ImageWidth, MDIWindow()\ImageHeight, ImageID(Image))
                CloseGadgetList()
                          
            Else
              MessageRequester("Image Viewer","Could not load image: "+FileName$)
            EndIf            
            
            FileName$ = NextSelectedFileName()
          Wend
        
        Case #MENU_Close
          Window = GetGadgetState(#GADGET_MDI)
          If Window <> -1          
            CloseWindow(Window)
          
            SelectMDIWindow(Window)
            FreeImage(MDIWindow()\Image)
            DeleteElement(MDIWindow())
          EndIf
        
        Case #MENU_CloseAll
                            
          ForEach MDIWindow()
            CloseWindow(MDIWindow()\Window)
            FreeImage(MDIWindow()\Image)  
          Next
          ClearList(MDIWindow())
        
        Case #MENU_Quit
          Quit = 1     
  
        Case #MENU_TileV
          SetGadgetState(#GADGET_MDI, #PB_MDI_TileVertically)
          
        Case #MENU_TileH
          SetGadgetState(#GADGET_MDI, #PB_MDI_TileHorizontally)
                
        Case #MENU_Cascade
          SetGadgetState(#GADGET_MDI, #PB_MDI_Cascade)
        
        Case #MENU_Arrange
          SetGadgetState(#GADGET_MDI, #PB_MDI_Arrange)
        
        Case #MENU_Previous
          SetGadgetState(#GADGET_MDI, #PB_MDI_Previous)
        
        Case #MENU_Next
          SetGadgetState(#GADGET_MDI, #PB_MDI_Next)
        
      EndSelect
      
      
    ElseIf Event = #PB_Event_SizeWindow
      
      Window = EventWindow()
      
      If SelectMDIWindow(Window)
        ResizeGadget(MDIWindow()\ScrollAreaGadget, 0, 0, WindowWidth(Window), WindowHeight(Window))
      EndIf
              
    EndIf
    
  Until Quit = 1  
EndIf