CHOOSEFILE (Sample)

From SEGGER Wiki
Jump to: navigation, search
CHOOSEFILE_Usage.c
CHOOSEFILE Usage.png
File(s) required
  • CHOOSEFILE_Usage.c
Runs in simulation Yes
Runs on target Yes
Download CHOOSEFILE_Usage.c

This sample demonstrates the usage of CHOOSEFILE dialogs.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : CHOOSEFILE_Usage.c
Purpose     : Sample that demonstrates how to use CHOOSEFILE
              dialogues in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include <Windows.h>

#include "DIALOG.h"
#include "MESSAGEBOX.h"

/*********************************************************************
*
*       Types
*
**********************************************************************
*/
typedef struct {
  U32 Mask;
  char c;
} ATTRIB;

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static ATTRIB _aAttrib[] = {
  { FILE_ATTRIBUTE_READONLY , 'R' },
  { FILE_ATTRIBUTE_HIDDEN   , 'H' },
  { FILE_ATTRIBUTE_SYSTEM   , 'S' },
  { FILE_ATTRIBUTE_DIRECTORY, 'D' },
  { FILE_ATTRIBUTE_ARCHIVE  , 'A' },
  { FILE_ATTRIBUTE_NORMAL   , 'N' },
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _GetData
*/
static int _GetData(CHOOSEFILE_INFO * pInfo) {
  static HANDLE   hFind;
  static int      NewDir;
  static char     acDrive [_MAX_DRIVE];
  static char     acDir   [_MAX_DIR];
  static char     acName  [_MAX_FNAME];
  static char     acExt   [_MAX_EXT];
  static char     acMask  [_MAX_PATH];
  static char     acPath  [_MAX_PATH];
  static char     acAttrib[10] = {0};
  WIN32_FIND_DATA Context;
  int             i, r;
  char            c;

  switch (pInfo->Cmd) {
  case CHOOSEFILE_FINDFIRST:
    if (hFind != 0) {
      FindClose(hFind);
    }
    //
    // Split path into drive and directory
    //
    _splitpath(pInfo->pRoot, acDrive, acDir, NULL, NULL);
    NewDir = 1;
    //
    // Do not 'break' here...
    //
  case CHOOSEFILE_FINDNEXT:
    if (NewDir) {
      _makepath(acMask, acDrive, acDir, NULL, NULL);
      strcat(acMask, pInfo->pMask);
      hFind = FindFirstFile(acMask, &Context);
      if (hFind == INVALID_HANDLE_VALUE) {
        FindClose(hFind);
        hFind = 0;
        return 1;
      }
    } else {
      r = FindNextFile(hFind, &Context);
      if (r == 0) {
        FindClose(hFind);
        hFind = 0;
        return 1;
      }
    }
    NewDir = 0;
    //
    // Generate attribute string (pInfo->pAttrib)
    //
    for (i = 0; i < GUI_COUNTOF(_aAttrib); i++) {
      c = (Context.dwFileAttributes & _aAttrib[i].Mask) ? _aAttrib[i].c : '-';
      acAttrib[i] = c;
    }
    //
    // Make name and extension (pInfo->pName, pInfo->pExt)
    //
    if ((Context.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
      _splitpath(Context.cFileName, NULL, NULL, acName, acExt);
    } else {
      strcpy(acName, Context.cFileName);
      acExt[0] = 0;
    }
    //
    // Pass data to dialog
    //
    pInfo->pAttrib = acAttrib;
    pInfo->pName   = acName;
    pInfo->pExt    = acExt;
    pInfo->SizeL   = Context.nFileSizeLow;
    pInfo->SizeH   = Context.nFileSizeHigh;
    pInfo->Flags   = (Context.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
                     ? CHOOSEFILE_FLAG_DIRECTORY : 0;
  }
  return 0;
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  char            acDirLocal[5];
  char            acDirWin[_MAX_PATH];
  char            acDirTmp[_MAX_PATH];
  char            acDirSys[_MAX_PATH];
  WM_HWIN         hWin;
  char          * apDrives[4] = { acDirLocal, acDirWin, acDirTmp, acDirSys };
  char            acMask[4];
  CHOOSEFILE_INFO Info;

  //
  // Init GUI and enable multi-buffering to avoid flickering
  //
  GUI_Init();
  WM_MULTIBUF_Enable(1);
  WM_SetDesktopColor(GUI_BLACK);
  //
  // Set variables
  //
  memset(&Info, 0, sizeof(Info));
  memcpy(acDirLocal, "C:\\", sizeof(acDirLocal));
  //
  // Set the search mask to look for all files.
  // Format: <filename>.<filetype>
  // Example: Setting your search mask to "*.c" would result in only finding C files.
  //
  memcpy(acMask, "*.*", sizeof(acMask));
  //
  // Get Windows directories
  //
  GetWindowsDirectory(acDirWin, sizeof(acDirWin));
  GetTempPath        (sizeof(acDirTmp), acDirTmp);
  GetSystemDirectory (acDirSys, sizeof(acDirSys));
  strcat(acDirWin, "\\");
  strcat(acDirSys, "\\");
  //
  // Edit header of dialogue
  //
  FRAMEWIN_SetDefaultFont(GUI_FONT_20_ASCII);
  FRAMEWIN_SetDefaultTextAlign(GUI_TA_HCENTER);
  //
  // Fill the choosefile info struct with a pointer to a custom GetData() function
  // and we add as well a search mask.
  //
  Info.pfGetData = _GetData;
  Info.pMask     = acMask;
  CHOOSEFILE_EnableToolTips();
  CHOOSEFILE_SetTopMode(0);
  hWin = CHOOSEFILE_Create(WM_HBKWIN, 0, 0, LCD_GetXSize(), LCD_GetYSize(), apDrives, GUI_COUNTOF(apDrives), 0, "File Dialog", 0, &Info);
  //
  // Make the window resizeable and moveable
  //
  FRAMEWIN_SetResizeable(hWin, 1);
  FRAMEWIN_SetMoveable(hWin, 1);
  //
  // Execute choosefile dialogue and show messagebox when the dialogue is closed
  //
  GUI_ExecCreatedDialog(hWin);
  hWin = MESSAGEBOX_Create(Info.pRoot, "File:", 0);

  while (1) {
    GUI_Delay(100);
  }
}