void Window1_CloseWindow_Event(void)
{
    if(Quitrequest()) terminated = TRUE;
}

void Menu_Info_MenuPick_Event(void)
{
    Inforequest();
}

void Menu_Quit_MenuPick_Event(void)
{
    if(Quitrequest()) terminated = TRUE;
}

void Menu_Select_All_MenuPick_Event(void)
{
    Checkbox_Handle_onoff(TRUE);
}

void Menu_Select_None_MenuPick_Event(void)
{
    Checkbox_Handle_onoff(FALSE);
}

void Scan_in_LhA_Event(void)
{
    GetAttr(GA_Selected, Gadget1[21], (ULONG *) &winbuff);
    in_lha = winbuff;
    Checkbox_Handle(!in_lha);
    RefreshSetGadgetAttrs(Gadget1[29], Window1, NULL, GA_Disabled, in_lha || !all_or_one_cd, TAG_DONE);
}

void All_or_one_CD_Event(void)
{
    GetAttr(CHOOSER_Selected, Gadget1[22], (ULONG *) &winbuff);
    all_or_one_cd = winbuff;
    if(all_or_one_cd) Checkbox_Handle(TRUE);
    else Checkbox_Handle(!in_lha);
    RefreshSetGadgetAttrs(Gadget1[21], Window1, NULL, GA_Disabled, all_or_one_cd, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[29], Window1, NULL, GA_Disabled, !all_or_one_cd, TAG_DONE);
}

void Search_Button_Event(void)
{
    char output[12];
    char buffer1[11];
    char dirbuffer[300][11];
    char filebuffer[21];
    BOOL term;
    BPTR file;
    WORD i, j, k, l;
    FLOAT calc, calc1;
    FLOAT position1 = 0, position2 = 0, position3 = 0, position4 = 0;
    ULONG counter = 0;

    entryzahl = 0;
    Checkbox_Handle(TRUE);
    RefreshSetGadgetAttrs(Gadget1[1], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[2], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[3], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[4], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[5], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[21], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[22], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[23], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[24], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[29], Window1, NULL, GA_Disabled, TRUE, TAG_DONE);

    if((pattern[0][0]) || (pattern[1][0]) || (pattern[2][0]) || (pattern[3][0]) || (pattern[4][0]))
    {
        if(all_or_one_cd == 0)
        {
            /* nur eine CD */
            if(in_lha)
            {
                /* suche in LhAs */
                for(j = 0;j < 14;j++)
                {
                    GetAttr(GA_Selected, Gadget1[24], (ULONG *) &winbuff); term = winbuff;
                    GetAttr(GA_Selected, Gadget1[j + 6], (ULONG *) &winbuff);
                    if(winbuff)
                    {
                        file = NULL;
                        switch(j)
                        {
                            case  0: file = Open("CD0:Aminet/biz/INDEX",  MODE_OLDFILE); break;
                            case  1: file = Open("CD0:Aminet/comm/INDEX", MODE_OLDFILE); break;
                            case  2: file = Open("CD0:Aminet/demo/INDEX", MODE_OLDFILE); break;
                            case  3: file = Open("CD0:Aminet/dev/INDEX",  MODE_OLDFILE); break;
                            case  4: file = Open("CD0:Aminet/disk/INDEX", MODE_OLDFILE); break;
                            case  5: file = Open("CD0:Aminet/docs/INDEX", MODE_OLDFILE); break;
                            case  6: file = Open("CD0:Aminet/game/INDEX", MODE_OLDFILE); break;
                            case  7: file = Open("CD0:Aminet/gfx/INDEX",  MODE_OLDFILE); break;
                            case  8: file = Open("CD0:Aminet/hard/INDEX", MODE_OLDFILE); break;
                            case  9: file = Open("CD0:Aminet/misc/INDEX", MODE_OLDFILE); break;
                            case 10: file = Open("CD0:Aminet/mods/INDEX", MODE_OLDFILE); break;
                            case 11: file = Open("CD0:Aminet/mus/INDEX",  MODE_OLDFILE); break;
                            case 12: file = Open("CD0:Aminet/pix/INDEX",  MODE_OLDFILE); break;
                            case 13: file = Open("CD0:Aminet/text/INDEX", MODE_OLDFILE); break;
                            case 14: file = Open("CD0:Aminet/util/INDEX", MODE_OLDFILE); break;
                        }
                        if(file)
                        {
                            Seek(file, 0, OFFSET_END);
                            position2 += Seek(file, 0, OFFSET_BEGINNING);
                            Close(file);
                        }
                    }
                    if(term) break;
                }
                for(j = 0;j < 14;j++)
                {
                    GetAttr(GA_Selected, Gadget1[j + 6], (ULONG *) &winbuff);
                    if(winbuff)
                    {
                        file = NULL;
                        switch(j)
                        {
                            case  0: file = Open("CD0:Aminet/biz/INDEX",  MODE_OLDFILE); break;
                            case  1: file = Open("CD0:Aminet/comm/INDEX", MODE_OLDFILE); break;
                            case  2: file = Open("CD0:Aminet/demo/INDEX", MODE_OLDFILE); break;
                            case  3: file = Open("CD0:Aminet/dev/INDEX",  MODE_OLDFILE); break;
                            case  4: file = Open("CD0:Aminet/disk/INDEX", MODE_OLDFILE); break;
                            case  5: file = Open("CD0:Aminet/docs/INDEX", MODE_OLDFILE); break;
                            case  6: file = Open("CD0:Aminet/game/INDEX", MODE_OLDFILE); break;
                            case  7: file = Open("CD0:Aminet/gfx/INDEX",  MODE_OLDFILE); break;
                            case  8: file = Open("CD0:Aminet/hard/INDEX", MODE_OLDFILE); break;
                            case  9: file = Open("CD0:Aminet/misc/INDEX", MODE_OLDFILE); break;
                            case 10: file = Open("CD0:Aminet/mods/INDEX", MODE_OLDFILE); break;
                            case 11: file = Open("CD0:Aminet/mus/INDEX",  MODE_OLDFILE); break;
                            case 12: file = Open("CD0:Aminet/pix/INDEX",  MODE_OLDFILE); break;
                            case 13: file = Open("CD0:Aminet/text/INDEX", MODE_OLDFILE); break;
                            case 14: file = Open("CD0:Aminet/util/INDEX", MODE_OLDFILE); break;
                        }
                        if(file)
                        {
                            filezahl = 0;
                            Seek(file, 0, OFFSET_END);
                            position1 = Seek(file, 0, OFFSET_BEGINNING);
                            FGets(file, puffer, 100);
                            FGets(file, puffer, 100);
                            while((FGets(file, puffer, 100)) && (filezahl < 300))
                            {
                                stringcopywithoutspace(filebuffer, puffer);
                                for(l = 0;l < 10;l++) buffer1[l] = puffer[l + 19];
                                buffer1[10] = NULL;
                                stringcopywithoutspace(dirbuffer[filezahl], buffer1);
                                strcpy(executepuffer[filezahl], "Run >NIL: lha v CD0:Aminet/");
                                strcat(executepuffer[filezahl], dirbuffer[filezahl]);
                                strcat(executepuffer[filezahl], "/");
                                strcat(executepuffer[filezahl], filebuffer);
                                strcat(executepuffer[filezahl], " >RAM:T/AminetSearcher-LhA.output");
                                filezahl++;
                                GetAttr(GA_Selected, Gadget1[24], (ULONG *) &winbuff); term = winbuff;
                                if(term) break;
                            }
                            Close(file);
                            for(k = 0;k < filezahl;k++)
                            {
/*****************************************************************************/
/** Dieser Abschnitt stimmt noch nicht ganz ! (und ich weiß nicht warum!)   **/
/** Irgendwie soll nun der Aufruf von LhA erfolgen, aber nach diesem Aufruf **/
/** in der Richtung wie:                                                    **/
/**                         SystemTags(executepuffer[k], TAG_DONE);         **/
/** oder                                                                    **/
/**                         Execute(executepuffer[k], NULL, NULL);          **/
/** kann man das Programm, aus welchem Grund auch immer, nicht mehr zum     **/
/** öffnen des Output-Files bewegen.                                        **/
/*****************************************************************************/
                                calc = (FLOAT) k / (FLOAT) filezahl * 100;
                                calc1 = ((calc / 100) * position1 + position3) / position2 * 100;
                                SetGadgetAttrs(Gadget1[26], Window1, NULL, FUELGAUGE_Level, (LONG) calc, TAG_DONE);
                                SetGadgetAttrs(Gadget1[27], Window1, NULL, FUELGAUGE_Level, (LONG) calc1, TAG_DONE);
                                SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, dirbuffer[k], TAG_DONE);
                                GetAttr(GA_Selected, Gadget1[24], (ULONG *) &winbuff); term = winbuff;
                                if(term) break;
                            }
                            position3 += position1;
                        }
                        else Errorrequest();
                    }
                    if(term) break;
                }
            }
            else
            {
                /* suche im Index */
                file = Open("CD0:Aminet/INDEX", MODE_OLDFILE);
                if(file)
                {
                    Seek(file, 0, OFFSET_END);
                    position1 = Seek(file, 0, OFFSET_BEGINNING);
                    FGets(file, puffer, 100);
                    FGets(file, puffer, 100);
                    FGets(file, puffer, 100);
                    FGets(file, puffer, 100);
                    FGets(file, puffer, 100);
                    FGets(file, puffer, 100);
                    while((FGets(file, puffer, 100)) && (entryzahl < 1024))
                    {
                        makelower(convert, puffer);
                        make_entry();
                        counter++;
                        if(counter == 50)
                        {
                            counter = 0;
                            position2 = Seek(file, 0, OFFSET_CURRENT);
                            calc = position2 / position1 * 100;
                            for(i = 0;i < 11;i++) output[i] = puffer[19 + i];
                            output[11] = NULL;
                            SetGadgetAttrs(Gadget1[26], Window1, NULL, FUELGAUGE_Level, (LONG) calc, TAG_DONE);
                            SetGadgetAttrs(Gadget1[27], Window1, NULL, FUELGAUGE_Level, (LONG) calc, TAG_DONE);
                            SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, output, TAG_DONE);
                            GetAttr(GA_Selected, Gadget1[24], (ULONG *) &winbuff); term = winbuff;
                        }
                        if(term) break;
                    }
                    Close(file);
                }
                else Errorrequest();
            }
        }
        else
        {
            /* alle CDs */
            if(single) file = Open("CD0:Lists/Single_Dir.doc", MODE_OLDFILE);
            else file = Open("CD0:Lists/Aminet_Dir.doc", MODE_OLDFILE);
            if(file)
            {
                Seek(file, 0, OFFSET_END);
                position1 = Seek(file, 0, OFFSET_BEGINNING);
                FGets(file, puffer, 100);
                FGets(file, puffer, 100);
                FGets(file, puffer, 100);
                FGets(file, puffer, 100);
                FGets(file, puffer, 100);
                FGets(file, puffer, 100);
                while((FGets(file, puffer, 100)) && (entryzahl < 1024))
                {
                    makelower(convert, puffer);
                    make_entry();
                    counter++;
                    if(counter == 100)
                    {
                        counter = 0;
                        position2 = Seek(file, 0, OFFSET_CURRENT);
                        calc = position2 / position1 * 100;
                        for(i = 0;i < 11;i++) output[i] = puffer[19 + i];
                        output[11] = NULL;
                        SetGadgetAttrs(Gadget1[26], Window1, NULL, FUELGAUGE_Level, (LONG) calc, TAG_DONE);
                        SetGadgetAttrs(Gadget1[27], Window1, NULL, FUELGAUGE_Level, (LONG) calc, TAG_DONE);
                        SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, output, TAG_DONE);
                        GetAttr(GA_Selected, Gadget1[24], (ULONG *) &winbuff); term = winbuff;
                    }
                    if(term) break;
                }
                Close(file);
            }
            else Errorrequest();
        }
    }
    else Nopatternrequest();
    RefreshSetGadgetAttrs(Gadget1[1], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[2], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[3], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[4], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[5], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[21], Window1, NULL, GA_Disabled, all_or_one_cd, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[22], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[23], Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[24], Window1, NULL, GA_Disabled, TRUE, GA_Selected, FALSE, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[29], Window1, NULL, GA_Disabled, !all_or_one_cd, TAG_DONE);
    SetGadgetAttrs(Gadget1[26], Window1, NULL, FUELGAUGE_Level, 100, TAG_DONE);
    SetGadgetAttrs(Gadget1[27], Window1, NULL, FUELGAUGE_Level, 100, TAG_DONE);
    SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, "Status: ok", TAG_DONE);
    Checkbox_Handle(!in_lha || all_or_one_cd);
}

void Listbrowser_Event(void)
{
    char buffer1[20];
    LONG entry;
    WORD choose, i = 0;

    IntuiMessage = GT_GetIMsg(Window1->UserPort);
    if(DoubleClick(Seconds, Micros, IntuiMessage->Seconds, IntuiMessage->Micros))
    {
        GetAttr(LISTBROWSER_Selected, Gadget1[25], (ULONG *) &winbuff);
        entry = winbuff;
        if(entry != -1)
        {
            choose = Extractrequest();
            if(choose == 1)
            {
                strcpy(puffer, "SYS:Utilities/Multiview CD0:Aminet/");
                stringcopywithoutspace(buffer1, founded_files_list_col3_raw[entry]);
                strcat(puffer, buffer1);
                strcat(puffer, "/");
                stringcopywithoutspace(buffer1, founded_files_list_col2_raw[entry]);
                strcat(puffer, buffer1);
                while(puffer[i]) i++;
                puffer[i - 4] = NULL;
                strcat(puffer, ".readme");
                Execute(puffer, NULL, NULL);
                SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, "Status: ok", TAG_DONE);
            }
            if(choose == 2)
            {
                strcpy(puffer, "Run >NIL: lha x CD0:Aminet/");
                stringcopywithoutspace(buffer1, founded_files_list_col3_raw[entry]);
                strcat(puffer, buffer1);
                strcat(puffer, "/");
                stringcopywithoutspace(buffer1, founded_files_list_col2_raw[entry]);
                strcat(puffer, buffer1);
                strcat(puffer, " RAM:");
                Execute(puffer, NULL, NULL);
                SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, "Extracted to RAM:", TAG_DONE);
            }
            if(choose == 3)
            {
                SetGadgetAttrs(Gadget1[28], Window1, NULL, STRINGA_TextVal, "Status: ok", TAG_DONE);
            }
        }
    }
    Seconds = IntuiMessage->Seconds;
    Micros = IntuiMessage->Micros;
    GT_ReplyIMsg(IntuiMessage);
}

void Checkbox_Handle(BOOL disabled)
{
    RefreshSetGadgetAttrs(Gadget1[6], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[7], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[8], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[9], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[10], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[11], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[12], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[13], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[14], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[15], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[16], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[17], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[18], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[19], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[20], Window1, NULL, GA_Disabled, disabled, TAG_DONE);
}

void Checkbox_Handle_onoff(BOOL onoff)
{
    RefreshSetGadgetAttrs(Gadget1[6], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[7], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[8], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[9], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[10], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[11], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[12], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[13], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[14], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[15], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[16], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[17], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[18], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[19], Window1, NULL, GA_Selected, onoff, TAG_DONE);
    RefreshSetGadgetAttrs(Gadget1[20], Window1, NULL, GA_Selected, onoff, TAG_DONE);
}

