#define MUI_OBSOLETE

#include <exec/types.h>
#include <exec/memory.h>
#define ASM
#define Prototype  extern
#define Local      static

#ifdef __STORM__
#define REG(x)     register __ ## x
#define SAVEDS     __saveds
#define __stkargs
#define __argargs
#else
#ifdef _DCC
#define REG(x)     __ ## x
#define SAVEDS     __geta4
#endif
#endif

#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

#include <stdio.h>
#include <stdlib.h>
#include <clib/alib_protos.h>
#include <libraries/mui.h>

#ifdef __STORM__
#include <pragma/muimaster_lib.h>
#include <pragma/exec_lib.h>
#include <pragma/utility_lib.h>
#else
#ifdef _DCC
#include <proto/muimaster.h>
#include <clib/exec_protos.h>
#include <clib/utility_protos.h>
#endif
#endif

#include "mui/pkb_mcc.h"


Local     void fail( APTR, char * );
Local     void init( void );
int main( int, STRPTR * );

int
wbmain(struct WBStartup *wb_startup)
{
    return (main(0, (STRPTR *)wb_startup));
}

struct Library  *MUIMasterBase   = NULL;

int
main(int argc, STRPTR argv[])
{
    APTR  app,
      window,
      Ottava,
      cx,ch,cq,cr,
      sl,sl1,sl2,sl3,
      slA,slB,
      clow,chigh,
      but1,but2;

    static char *CY_0[] = {"OBIČAJNO","OBMOČNO","POSEBNO",NULL};
    static char *CY_1[] = {"BREZ VODILNE","ZGORNJA VODILNA","SPODNJA VODILNA",NULL};
    static char *CY_2[] = {"Z OSVEŽEVANJEM","BREZ OSVEŽEVANJA",NULL};

    init();


        /*------------------------------*/
app = ApplicationObject,
MUIA_Application_Title      , "Klaviatura",
MUIA_Application_Version    , "$VER: Klaviatura 0.001 (17 Jan 1999)",
MUIA_Application_Copyright  , "(c) 1999 Calogero Calě",
MUIA_Application_Author     , "Calogero Calě",
MUIA_Application_Description, "Uporabniški razred za klaviaturo.",
MUIA_Application_Base       , "Klaviatura",
SubWindow, window = WindowObject,
    MUIA_Window_Title, "Uporabniški razred za klaviaturo",
    MUIA_Window_ID   , MAKE_ID('C','L','S','1'),
    WindowContents, VGroup,
            Child, ColGroup(2),
                Child, cq = Cycle(CY_2),
                Child, cr = SimpleButton("Osveži"),
            End,

            Child, ColGroup(2),
                Child, ColGroup(2),
                    Child, CLabel("Sproščena tipka"),  Child,  sl  = Slider(0,131,0),
                    Child, CLabel("Pritisnjena tipka"),  Child,  sl1 = Slider(0,131,0),
                    Child, CLabel("Od"),        Child,  sl2 = Slider(0,131,0),
                    Child, CLabel("Do"),          Child,  sl3 = Slider(0,131,0),
                End,
                Child, ColGroup(2),
                    Child, CLabel("Dol"),       Child,  slA = Slider(0,131,0),
                    Child, CLabel("Gor"),         Child,  slB = Slider(0,131,0),

                    Child, CLabel("Najnižja nota:"),       Child,  clow  = TextObject,MUIA_Text_Contents,"000",End,
                    Child, CLabel("Najvišja nota:"),       Child,  chigh = TextObject,MUIA_Text_Contents,"000",End,
                End,
            End,
            Child, HGroup,
                GroupFrame,
                MUIA_Group_HorizSpacing, 2,
                Child,but1 = SimpleButton("Znova nastavi"),
                Child,but2 = SimpleButton("Območje 12~24"),
                Child,  cx = Cycle(CY_0),
                Child,  ch = Cycle(CY_1),
            End,

            Child, ScrollgroupObject,
                MUIA_Scrollgroup_FreeVert,FALSE,
                MUIA_Scrollgroup_Contents, Ottava = PkbObject,
                    VirtualFrame,
                    MUIA_Background, MUII_BACKGROUND,
                    MUIA_Pkb_Octv_Start, 1,
                    MUIA_Pkb_Octv_Range, 10,
                    MUIA_Pkb_Octv_Name,TRUE,
                    MUIA_Pkb_Octv_Base, 0,
                End,
            End,

        End, /* VGroup */
    End,  /* WindowObject */
End; /* ApplicationObject */
        /*------------------------------*/


    if (!app)
        fail(app,"Ne morem ustvariti aplikacije.");


    DoMethod(window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
             app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);


    DoMethod(cx,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
             Ottava, 3,
             MUIM_Set, MUIA_Pkb_Mode, MUIV_TriggerValue);

    DoMethod(ch,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
             Ottava, 3,
             MUIM_Set, MUIA_Pkb_Range_Head, MUIV_TriggerValue);

        /*Quiet */
    DoMethod(cq,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
             Ottava,3,
             MUIM_Set,MUIA_Pkb_Quiet,MUIV_TriggerValue);

        /*Refresh */
    DoMethod(cr,MUIM_Notify,MUIA_Pressed, FALSE,
             Ottava,1,MUIM_Pkb_Refresh);

        /*Reset */
    DoMethod(but1,MUIM_Notify,MUIA_Pressed, FALSE, 
             Ottava,1,MUIM_Pkb_Reset);

        /*Refresh */
    DoMethod(but2,MUIM_Notify,MUIA_Pressed, FALSE, 
             Ottava,3,
             MUIM_Pkb_Range,24,12);

        /* Down */
    DoMethod(slA,MUIM_Notify,MUIA_Numeric_Value, MUIV_EveryTime,
             Ottava,3,
             MUIM_Set,MUIA_Pkb_Key_Press,MUIV_TriggerValue);

        /* Up */
    DoMethod(slB,MUIM_Notify,MUIA_Numeric_Value, MUIV_EveryTime,
             Ottava,3,
             MUIM_Set,MUIA_Pkb_Key_Release,MUIV_TriggerValue);

        /* KeyRelease*/
    DoMethod(Ottava,MUIM_Notify,MUIA_Pkb_Key_Release, MUIV_EveryTime,
             sl, 3,
             MUIM_Set,MUIA_Numeric_Value,MUIV_TriggerValue);

        /* KeyCurrent */
    DoMethod(Ottava,MUIM_Notify,MUIA_Pkb_Current,    MUIV_EveryTime,
             sl1,3,
             MUIM_Set,MUIA_Numeric_Value,MUIV_TriggerValue);

        /* From */
    DoMethod(Ottava,MUIM_Notify,MUIA_Pkb_Range_Start,MUIV_EveryTime,
             sl2,3,
             MUIM_Set,MUIA_Numeric_Value,MUIV_TriggerValue);

        /* To */
    DoMethod(Ottava,MUIM_Notify,MUIA_Pkb_Range_End,  MUIV_EveryTime,
             sl3,3,
             MUIM_Set,MUIA_Numeric_Value,MUIV_TriggerValue);

    {
        ULONG *l;
        ULONG *h;
        static char buf[20];

        get(Ottava,MUIA_Pkb_Low,&l);
        sprintf(buf,"%ld",l);
        set(clow ,MUIA_Text_Contents,buf);

        get(Ottava,MUIA_Pkb_High,&h);
        sprintf(buf,"%ld",h);
        set(chigh,MUIA_Text_Contents,buf);
    }

    set(window,MUIA_Window_Open,TRUE);
    {
        ULONG sigs = 0;
        while (DoMethod(app,MUIM_Application_NewInput,&sigs) != MUIV_Application_ReturnID_Quit){
            if (sigs) {
                sigs = Wait(sigs | SIGBREAKF_CTRL_C);
                if (sigs & SIGBREAKF_CTRL_C) break; }
        }
    }
    set(window,MUIA_Window_Open,FALSE);


/*
** Shut down...
*/

    MUI_DisposeObject(app);             /* sprosti vse objekte. */
    fail(NULL,NULL);                    /* končaj, app je že sproščena. */

    return(0);
}

void
init()
{
    if ( !(MUIMasterBase = OpenLibrary("muimaster.library",17L)) )
        fail(NULL,"Potrebujem knjižnico muimaster.library");

}

void
fail(APTR app,char *str)
{
    if (app)           MUI_DisposeObject(app);
    if (str)           puts(str);
    if (MUIMasterBase) CloseLibrary(MUIMasterBase);

    exit(0);
}

