#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

#define MENU_OUVRIR 11
#define MENU_QUITTER 12
#define MENU_IMAGENORMALE 21
#define MENU_IMAGECONTRASTEE 22
#define MENU_CANAL1 31
#define MENU_CANAL2 32
#define MENU_CANAL3 33
#define MENU_CANALMOYENNE 34

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

//----------------------------------
// Ici, les variables globales

int ** tableau1;
int ** tableau2;
int ** tableau3;

int histo1[256];
int histo2[256];
int histo3[256];
int histo[256];

long nbLig = 0;
long nbCol = 0;
long nbLigF = 0;
long nbColF = 0;

int imageChargee = 0;

int aAfficher = 0;

// ...

// Fin des variables globales
//----------------------------------

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;
        
	//----------------------------------
    // Creation du menu
	
    HMENU sousMenuF;
    sousMenuF=CreateMenu();
    AppendMenu(sousMenuF,MF_STRING,MENU_OUVRIR,"&Ouvrir");
    AppendMenu(sousMenuF,MF_SEPARATOR,0,NULL);
    AppendMenu(sousMenuF,MF_STRING,MENU_QUITTER,"&Quitter");
    
    HMENU sousMenuI;
    sousMenuI=CreateMenu();
    AppendMenu(sousMenuI,MF_STRING,MENU_IMAGENORMALE,"Normale");
    AppendMenu(sousMenuI,MF_STRING,MENU_IMAGECONTRASTEE,"Contrastée");
    
    HMENU sousMenuH;
    sousMenuH=CreateMenu();
    AppendMenu(sousMenuH,MF_STRING,MENU_CANAL1,"Canal &1");
    AppendMenu(sousMenuH,MF_STRING,MENU_CANAL2,"Canal &2");
    AppendMenu(sousMenuH,MF_STRING,MENU_CANAL3,"Canal &3");
    AppendMenu(sousMenuH,MF_SEPARATOR,0,NULL);
    AppendMenu(sousMenuH,MF_STRING,MENU_CANALMOYENNE,"&Moyenne");
    
    HMENU hMenu;
    hMenu=CreateMenu();
    AppendMenu(hMenu,MF_POPUP,(int)sousMenuF,"&Fichier");
    AppendMenu(hMenu,MF_POPUP,(int)sousMenuI,"&Image");
    AppendMenu(hMenu,MF_POPUP,(int)sousMenuH,"&Histogramme");
	
	// Fin creation du menu
	//----------------------------------

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           // Une modif :
           "Image satellite",   /* Title Text */
           WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL , /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           // Une modif :
           hMenu,                
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//----------------------------------
	// Debut
    // Handle sur un device context
    HDC hdc;
    // structure de dessin
    PAINTSTRUCT ps;
    
    // ... declarations locales necessaires, par exemple :
    int x; 
    int y;
    
	
    // fin 
	//----------------------------------

    switch (message)                  /* handle the messages */
    {
		//----------------------------------
        // Debut
        case WM_CREATE:
            break;
        
        
        
        case WM_PAINT:   
			// Exemple minimal ˆ completer
                hdc = BeginPaint(hwnd,&ps);
                if (aAfficher == MENU_OUVRIR) {
                   // On affiche les caractéristiques de base de l'image : nblignes, nbcolonnes, date, heure
                }
                else if (aAfficher == MENU_IMAGENORMALE) {
                    // On affiche l'image avec les niveaux lus dans le fichier
                }
                else if (aAfficher == MENU_IMAGECONTRASTEE) {
                    // On affiche l'image avec des valeurs corrigées pour un meilleur contraste
                }
                else if (aAfficher == MENU_CANAL1) {
                    // On affiche l'histogramme du canal 1
                }
                else if (aAfficher == MENU_CANAL2) {
                    // On affiche l'histogramme du canal 2
                }
                else if (aAfficher == MENU_CANAL3) {
                    // On affiche l'histogramme du canal 3
                }
                else if (aAfficher == MENU_CANALMOYENNE) {
                    // On affiche l'histogramme moyen
                }
        
                EndPaint(hwnd, &ps);
            }    
            break;
        
        
        case WM_COMMAND:
            // Gestion des menus
            switch (wParam) {
                case MENU_OUVRIR :
                    // On retrouve le nombre de lignes et le nombre de colonnes
                    initLignesColonnes("C:\\ModuleC\\data\\FDO0003.PER",&nbLig,&nbCol);
                    // ...
                    
                    // On remplit les tableaux depuis les informations des différents fichiers
					// ...
                    
                    // On génère les histogrammes de couleurs
					// ...
                                      
                    aAfficher = MENU_OUVRIR;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;
                    
                case MENU_IMAGENORMALE :
                    aAfficher = MENU_IMAGENORMALE;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;
                    
                case MENU_IMAGECONTRASTEE :
                    aAfficher = MENU_IMAGECONTRASTEE;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;            
                    
                case MENU_CANAL1 :
                    aAfficher = MENU_CANAL1;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;    
                
                case MENU_CANAL2 :
                    aAfficher = MENU_CANAL2;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;        
                    
                case MENU_CANAL3 :
                    aAfficher = MENU_CANAL3;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;   
                    
                case MENU_CANALMOYENNE :
                    aAfficher = MENU_CANALMOYENNE;
                    // On provoque un repaint
                    InvalidateRect(hwnd, NULL, 1);
                    break;        
                    
                case MENU_QUITTER :
                    // Menu quitter
                    PostQuitMessage (0);
                    break;
                    
            }
            break;
            
        // fin 
		//----------------------------------
       
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


