User Module: Select Image File\s

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

User Module: Select Image File\s

Post by collectordave »

A simple user module to request an image filename or list of image filenames from a user.

The module acts in two ways one looking for multiple files where the user can select the folder in which the files are and where the user is asked for a single image file name where the navigation is the same but when clicking on a single file name a thumbnail is displayed.

The selected filename are stored in an array in the module readable after the form is closed.

Now updated so the code compiles and works on Windows and MAC. Can anyone can try this on Linux?

The first part is a module for application global constants which may change due to the operating system.

First the global module code

App.pbi

Code: Select all

;{ ==Code Header Comment==============================
;        Name/title: frmMain.pb
;   Executable name: NA Paet development of Report Designer Module
;           Version: 1.0
;            Author: Collectordave
;     Collaborators: Amílcar Matos Pérez
;    Translation by: 
;       Create date: 24\01\2016
; Previous releases: 
; This Release Date: 01\03\2016
;  Compiler version: PureBasic 5.41 (x64),PureBasic 5.4LTS(X86)
;         Copyright: (C)2016
;           License: 
;         Libraries: 
;     English Forum: 
;      French Forum: 
;      German Forum: 
;  Tested platforms: Windows,MAC OSX El Kapitan
;       Description: Module for application global Constants and OS specific procedures
; ====================================================
;.......10........20........30........40........50........60........70........80
;}

DeclareModule App
  
  ;OS Specific details
  CompilerSelect #PB_Compiler_OS
    
    CompilerCase   #PB_OS_MacOS
        
      #DefaultFolder = "/Volumes"
      
    CompilerCase   #PB_OS_Linux 
    
    CompilerCase   #PB_OS_Windows

      #DefaultFolder = "C:\"  
    
  CompilerEndSelect

EndDeclareModule

Module App

    
EndModule
Just changes the default folder for the Explorertreegadget

Now the module code:

SelectImages.pbi

Code: Select all

;{ ==Code Header Comment==============================
;        Name/title: SelectImages.pbi
;   Executable name: NA Part development of Report Designer Module
;           Version: 1.0
;            Author: Collectordave
;     Collaborators: Amílcar Matos Pérez
;    Translation by: 
;       Create date: 24\01\2016
; Previous releases: 
; This Release Date: 01\03\2016
;  Compiler version: PureBasic 5.41 (x64),PureBasic 5.4LTS(X86)
;         Copyright: (C)2016
;           License: 
;         Libraries: 
;     English Forum: 
;      French Forum: 
;      German Forum: 
;  Tested platforms: Windows,MAC OSX El Kapitan
;       Description: Module to select image files either singly or as whole folder
;      Requirements: Needs App.pbi to be included in main file
; ====================================================
;.......10........20........30........40........50........60........70........80
;}
DeclareModule SelectImages
  
  Global Window_ID
  Global OkPressed.i
  Global Pattern.s
  Global Multiple.i
  Global Dim FileNames.s(1)
  
  Declare open()
  Declare Event_Handler(Event)
  
EndDeclareModule
  
Module SelectImages
  
  UseJPEGImageDecoder()
  UsePNGImageDecoder()
  UseJPEG2000ImageDecoder()
  UseTGAImageDecoder()
  UseTIFFImageDecoder()

  Global ScaleHeight.f,ScaleWidth.f
  
  Global Directory.s,FileName.s
  
  Global btnOk.i,btnCancel.i,lstFiles.i,FolderExplorer.i,ImageDisplay.i
  
Procedure GetFiles(Directory.s)
   
  ClearGadgetItems(lstFiles)
  If ExamineDirectory(0, Directory, Pattern)  
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        AddGadgetItem (lstFiles, -1, DirectoryEntryName(0))
      EndIf
    Wend
    FinishDirectory(0)
  EndIf

EndProcedure 

Procedure Open()
  
  OkPressed = #False
  
  If Multiple = #True
    Window_ID = OpenWindow(#PB_Any, 0, 0, 435, 300, "Select Images", #PB_Window_ScreenCentered)
    btnOk = ButtonGadget(#PB_Any, 250, 260, 80, 30, "Ok")
    btnCancel = ButtonGadget(#PB_Any, 350, 260, 80, 30, "Cancel")
    lstFiles = ListViewGadget(#PB_Any, 250, 10, 180, 240)   
    DisableGadget(lstFiles,#True)
  Else
    Window_ID = OpenWindow(#PB_Any, 0, 0, 650, 300, "Select Image", #PB_Window_ScreenCentered)
    btnOk = ButtonGadget(#PB_Any, 470, 260, 80, 30, "Ok")
    btnCancel = ButtonGadget(#PB_Any, 560, 260, 80, 30, "Cancel")
    lstFiles = ListViewGadget(#PB_Any, 250, 10, 180, 240)    
  EndIf
  FolderExplorer = ExplorerTreeGadget(#PB_Any, 10, 10, 230, 240, App::#DefaultFolder, #PB_Explorer_NoFiles )
  ImageBack = ImageGadget(#PB_Any, 440, 10, 200, 200, 0, #PB_Image_Border)
  ImageDisplay = ImageGadget(#PB_Any, 440, 10, 200, 200, 0, #PB_Image_Border)  
  
  ;Create a black image for background
  SetGadgetState(ImageBack,ImageID(CreateImage(#PB_Any,200 ,200 , 32,RGB(0,0,0))))
  
EndProcedure

Procedure Event_Handler(Event)

  Select Event

    Case #PB_Event_Gadget
      Select EventGadget()
        Case FolderExplorer
          Directory = GetGadgetText(FolderExplorer)
          GetFiles(GetGadgetText(FolderExplorer))
        Case lstFiles
          FileName = GetGadgetText(lstFiles)
          If Multiple = #False
            
           
            ;Debug Directory + FileName
            If LoadImage(PageImage, Directory + FileName)
              If ImageWidth(PageImage) > ImageHeight(PageImage)
                ScaleHeight = (190/ ImageWidth(PageImage)) * ImageHeight(PageImage)
                ScaleWidth = 190
                x = 440
                y = ((190 - ScaleHeight)/2) + 10
              Else
                ScaleWidth = (190/ ImageHeight(PageImage)) * ImageWidth(PageImage)
                ScaleHeight = 190
                y = 10
                x = ((190 - ScaleWidth)/2) + 440
              EndIf
              ResizeImage(PageImage, ScaleWidth, ScaleHeight)
              SetGadgetState(ImageDisplay,ImageID(PageImage))
              ResizeGadget(ImageDisplay, x, y, #PB_Ignore, #PB_Ignore)
            EndIf
          EndIf
        Case btnOk
          OkPressed = #True 
          If Multiple = #True
            Number_Of_Files = CountGadgetItems(lstFiles)
            If Number_Of_Files > 0
              ReDim FileNames(Number_Of_Files)
              For iLoop = 0 To Number_Of_Files - 1
                FileNames(iloop) = Directory + GetGadgetItemText(lstFiles,iloop)
              Next iloop
            EndIf
          Else
            ReDim FileNames(1)
            FileNames(0) = Directory + GetGadgetText(lstFiles)
          EndIf
          CloseWindow(Window_ID)
          Window_ID = -1
          
        Case btnCancel
          OkPressed = #False
          ReDim FileNames(1)
          FileNames(0) = ""
          CloseWindow(Window_ID)
          Window_ID = -1
          
      EndSelect
        
  EndSelect
  
EndProcedure

EndModule
Now a simple test programme

Code: Select all

;{ ==Code Header Comment==============================
;        Name/title: SelectImages.pbi
;   Executable name: NA Part development of Report Designer Module
;           Version: 1.0
;            Author: Collectordave
;     Collaborators: Amílcar Matos Pérez
;    Translation by: 
;       Create date: 24\01\2016
; Previous releases: 
; This Release Date: 01\03\2016
;  Compiler version: PureBasic 5.41 (x64),PureBasic 5.4LTS(X86)
;         Copyright: (C)2016
;           License: 
;         Libraries: 
;     English Forum: 
;      French Forum: 
;      German Forum: 
;  Tested platforms: Windows,MAC OSX El Kapitan
;       Description: Simple application to demonstrate selecting image files
; ====================================================
;.......10........20........30........40........50........60........70........80
;}EnableExplicit

IncludeFile "App.pbi"
IncludeFile "SelectImages.pbi"

Global frmMain.i

Global btnSelectSingle, btnSelectmulti,strSingleFile,lstFiles.i

Define Event.i,iLoop.i

Procedure Event_Handler(Event)
  
  Select Event
    Case #PB_Event_CloseWindow
      End

    Case #PB_Event_Gadget
      Select EventGadget()
        Case btnSelectSingle
          If Not IsWindow(SelectImages::Window_ID)
            SelectImages::Pattern = "*.jpg" ;Set for image type
            SelectImages::Multiple = #False
            SelectImages::Open()
          EndIf
        
        Case btnSelectmulti
          If Not IsWindow(SelectImages::Window_ID)
            SelectImages::Pattern = "*.jpg" ;Set for image type
            SelectImages::Multiple = #True
            SelectImages::Open()
          EndIf
          
      EndSelect
      
  EndSelect

EndProcedure

  frmMain = OpenWindow(#PB_Any, 20, 20, 600, 200, "", #PB_Window_SystemMenu)
  btnSelectSingle = ButtonGadget(#PB_Any, 10, 10, 100, 20, "Select Image")
  strSingleFile = StringGadget(#PB_Any, 120, 10, 470, 20, "")
  btnSelectmulti = ButtonGadget(#PB_Any, 10, 40, 100, 20, "Select Images")
  lstFiles = ListViewGadget(#PB_Any, 120, 40, 470, 150)

  
  Repeat
    Event = WaitWindowEvent()
      
      Select EventWindow()
          
        Case frmMain
          Event_Handler(Event)
          
        Case SelectImages::Window_ID

          SelectImages::Event_Handler(Event)
          If SelectImages::OkPressed = #True
            If SelectImages::Multiple = #False
              SetGadgetText(strSingleFile,SelectImages::FileNames(0))
            Else
              ClearGadgetItems(lstFiles)
              For iLoop = 0 To ArraySize(SelectImages::FileNames()) -1
                AddGadgetItem(lstFiles,-1,SelectImages::FileNames(iLoop))
              Next iLoop
              
            EndIf
          Else
            ;Cancel Pressed so do nothing
            
          EndIf
          
      EndSelect
  ForEver
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: User Module: Select Image File\s

Post by collectordave »

Superceded by me learning a little more.

The openfile requester on Windows and MAC can display images as you select them so just not needed,

I do not know about Linux so leving it here just in case.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply