/* TCHEDIT.C - Final Win32 Editor (Fixed Dialogs & Tree Logic) */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shellapi.h>
#include <shlobj.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TCHELP.H"

/* IDs */
#define ID_TREE      100
#define ID_BTN_SAVE  101
#define ID_BTN_NEW   102
#define ID_EDIT_AUTH 201
#define ID_EDIT_VER  202
#define ID_LIST_LINK 301
#define ID_EDIT_LINK 302
#define ID_BTN_ADDLNK 303
#define ID_BTN_REMLNK 304
#define ID_GRP_CONTENT 400
#define ID_CMB_LANG    401
#define ID_BTN_ADDLANG 402
#define ID_LIST_SEC    403
#define ID_BTN_ADDSEC  404
#define ID_EDIT_TITLE  405
#define ID_EDIT_BODY   406
#define ID_BTN_APPLY   407
#define ID_EDIT_LANG   408 
#define ID_EDIT_DESC   409

#define ID_BTN_MKDIR   500 
#define ID_BTN_DEL     501 

#define IDM_NEWPROJ    601
#define IDM_OPENPROJ   602
#define IDM_NEWFILE    603
#define IDM_EXIT       604
#define IDM_DELETE     605

HINSTANCE g_hInst;
HWND hMain, hTree;
HWND hAuth, hVer, hListLink, hEditLink, hCmbLang, hListSec, hTitle, hBody, hEditLang, hBtnAddLang, hEditDesc;

char g_ProjectDir[MAX_PATH] = "";
char g_CurrentFile[MAX_PATH] = "";
HelpData *g_pData = NULL;
int g_CurrentLangIdx = 0;
int g_CurrentSecIdx = -1;

/* --- INPUT DIALOG (FIXED) --- */
char g_InputBuf[64];
char g_InputTitle[64];
HWND hInputEdit;

BOOL CALLBACK SetFontProc(HWND child, LPARAM font) {
    SendMessage(child, WM_SETFONT, (WPARAM)font, TRUE);
    return TRUE;
}

LRESULT CALLBACK InputProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    if (msg == WM_INITDIALOG) {
        SetWindowText(hwnd, g_InputTitle);
        SetFocus(hInputEdit);
        return FALSE; /* False weil wir Focus manuell setzen */
    }
    /* FIX: DestroyWindow statt EndDialog */
    if (msg == WM_COMMAND && LOWORD(wParam) == 1) { 
        GetWindowText(hInputEdit, g_InputBuf, 63); 
        DestroyWindow(hwnd); 
        return 0; 
    }
    if (msg == WM_CLOSE) {
        DestroyWindow(hwnd);
        return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

int GetUserInput(HWND parent, char* title, char* outBuf) {
    WNDCLASS wc = {0}; HWND hDlg; MSG msg;
    strcpy(g_InputTitle, title); g_InputBuf[0] = 0;
    
    wc.lpfnWndProc = InputProc; wc.hInstance = g_hInst; wc.lpszClassName = "TCHInput";
    wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
    /* Nur registrieren wenn noch nicht da */
    if(!GetClassInfo(g_hInst, "TCHInput", &wc)) RegisterClass(&wc);
    
    hDlg = CreateWindowEx(WS_EX_DLGMODALFRAME,"TCHInput",title,WS_VISIBLE|WS_POPUP|WS_CAPTION|WS_SYSMENU,
        200,200,300,120,parent,NULL,g_hInst,NULL);
    
    CreateWindow("STATIC","Name:",WS_CHILD|WS_VISIBLE,10,10,50,20,hDlg,0,g_hInst,0);
    hInputEdit = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER,10,35,260,20,hDlg,0,g_hInst,0);
    CreateWindow("BUTTON","OK",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,100,65,80,25,hDlg,(HMENU)1,g_hInst,0);
    
    /* Manuell WM_INITDIALOG senden um Titel zu setzen */
    SendMessage(hDlg, WM_INITDIALOG, 0, 0);

    EnableWindow(parent, FALSE);
    /* Message Loop läuft bis Fenster zerstört ist */
    while(IsWindow(hDlg)) {
        if(GetMessage(&msg,0,0,0)) { 
            if(msg.message==WM_KEYDOWN && msg.wParam==VK_RETURN) SendMessage(hDlg,WM_COMMAND,1,0); 
            TranslateMessage(&msg); DispatchMessage(&msg); 
        }
    }
    EnableWindow(parent, TRUE); SetForegroundWindow(parent);
    if(strlen(g_InputBuf)>0) { strcpy(outBuf, g_InputBuf); return 1; }
    return 0;
}

/* --- DATEI SYSTEM --- */
void RegisterPath() {
    char exePath[MAX_PATH], pathFile[MAX_PATH], line[MAX_PATH];
    FILE *fRead, *fWrite; int found = 0; char* p;
    GetModuleFileName(NULL, exePath, MAX_PATH);
    p = strrchr(exePath, '\\'); if(p) *p=0; 
    wsprintf(pathFile, "%s\\Path", exePath);
    fRead = fopen(pathFile, "r");
    if (fRead) { while (fgets(line, MAX_PATH, fRead)) { line[strcspn(line, "\r\n")] = 0; if (lstrcmpi(line, g_ProjectDir) == 0) { found = 1; break; } } fclose(fRead); }
    if (!found && strlen(g_ProjectDir) > 0) { fWrite = fopen(pathFile, "a"); if (fWrite) { fprintf(fWrite, "%s\n", g_ProjectDir); fclose(fWrite); } }
}

/* Rekursiver Scan */
void ScanDirectory(char* dir, HTREEITEM hParent) {
    WIN32_FIND_DATA fd; HANDLE hFind; char search[MAX_PATH]; TV_INSERTSTRUCT tvis; HTREEITEM hItem; char* fullPath;
    wsprintf(search, "%s\\*.*", dir); hFind = FindFirstFile(search, &fd);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) continue;
            
            fullPath = (char*)malloc(MAX_PATH);
            wsprintf(fullPath, "%s\\%s", dir, fd.cFileName);
            
            tvis.hParent = hParent; 
            tvis.hInsertAfter = TVI_LAST; 
            tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE; 
            tvis.item.pszText = fd.cFileName;
            
            if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                tvis.item.iImage = 0; tvis.item.iSelectedImage = 0; 
                tvis.item.lParam = (LPARAM)fullPath;
                hItem = TreeView_InsertItem(hTree, &tvis);
                ScanDirectory(fullPath, hItem);
            } else {
                if (strstr(fd.cFileName, ".tchelp") || strstr(fd.cFileName, ".TCH") || strstr(fd.cFileName, ".tch")) {
                    tvis.item.iImage = 1; tvis.item.iSelectedImage = 1; 
                    tvis.item.lParam = (LPARAM)fullPath;
                    TreeView_InsertItem(hTree, &tvis);
                } else {
                    free(fullPath);
                }
            }
        } while (FindNextFile(hFind, &fd)); FindClose(hFind);
    }
}

/* FIX: Lädt Projekt mit Root-Knoten */
void LoadProject(char* projFile) {
    char* p; char title[256]; 
    TV_INSERTSTRUCT tvis; HTREEITEM hRoot; char* rootPathVal;
    
    /* Pfad auflösen */
    GetFullPathName(projFile, MAX_PATH, g_ProjectDir, NULL);
    
    /* Wenn projFile eine Datei ist (Project.tcproject), Ordner nehmen */
    if (!(GetFileAttributes(g_ProjectDir) & FILE_ATTRIBUTE_DIRECTORY)) {
        p = strrchr(g_ProjectDir, '\\'); if(p) *p=0; 
    }

    wsprintf(title, "TCH Studio - %s", g_ProjectDir); 
    SetWindowText(hMain, title);
    RegisterPath(); 
    TreeView_DeleteAllItems(hTree);
    
    /* FIX: Root Knoten erstellen */
    rootPathVal = (char*)malloc(MAX_PATH);
    strcpy(rootPathVal, g_ProjectDir);

    /* Namen extrahieren für Anzeige */
    p = strrchr(g_ProjectDir, '\\');
    
    tvis.hParent = TVI_ROOT; 
    tvis.hInsertAfter = TVI_LAST;
    tvis.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
    tvis.item.pszText = (p) ? (p+1) : g_ProjectDir; /* Nur Ordnername anzeigen */
    tvis.item.iImage = 0; 
    tvis.item.iSelectedImage = 0;
    tvis.item.lParam = (LPARAM)rootPathVal;
    
    hRoot = TreeView_InsertItem(hTree, &tvis);
    
    /* Inhalt scannen unter dem Root Knoten */
    ScanDirectory(g_ProjectDir, hRoot); 
    
    TreeView_Expand(hTree, hRoot, TVE_EXPAND);
}

char* GetSelectedPath() {
    HTREEITEM hItem = TreeView_GetSelection(hTree); TVITEM item;
    if (!hItem) return NULL;
    item.hItem = hItem; item.mask = TVIF_PARAM;
    if (TreeView_GetItem(hTree, &item)) return (char*)item.lParam;
    return NULL;
}

/* --- AKTIONEN (FIXED) --- */

void OnNewFile() {
    char name[64]; char path[MAX_PATH]; char* selPath; char* folder;
    
    if(strlen(g_ProjectDir)==0) { MessageBox(hMain, "Kein Projekt!", "Info", MB_OK); return; }
    
    folder = g_ProjectDir; /* Fallback */
    selPath = GetSelectedPath();
    
    /* FIX: Intelligente Ordnerwahl */
    if (selPath) {
        DWORD attr = GetFileAttributes(selPath);
        if (attr != 0xFFFFFFFF && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
            folder = selPath; /* Es ist ein Ordner (oder Root) */
        } else {
            /* Datei selektiert -> Parent Ordner nehmen */
            static char parent[MAX_PATH];
            char* p;
            strcpy(parent, selPath);
            p = strrchr(parent, '\\');
            if (p) { *p = 0; folder = parent; }
        }
    }

    if (GetUserInput(hMain, "Dateiname (ohne Endung)", name)) {
        wsprintf(path, "%s\\%s.tchelp", folder, name);
        if (!g_pData) g_pData = (HelpData*)malloc(sizeof(HelpData));
        TC_InitData(g_pData); 
        strcpy(g_pData->lang[0].key, "de"); 
        strcpy(g_pData->lang[0].desc, "Std"); 
        g_pData->lang_cnt = 1;
        
        if (TC_SaveXML(path, g_pData)) {
            LoadProject(g_ProjectDir); /* Refresh Tree */
        } else {
             MessageBox(hMain, "Fehler beim Erstellen!", "Error", MB_OK|MB_ICONERROR);
        }
    }
}

void OnNewFolder() {
    char name[64]; char path[MAX_PATH]; char* selPath; char* folder;
    
    if (strlen(g_ProjectDir) == 0) return;
    
    folder = g_ProjectDir;
    selPath = GetSelectedPath();
    
    /* FIX: Gleiche Logik wie bei File */
    if (selPath) {
        DWORD attr = GetFileAttributes(selPath);
        if (attr != 0xFFFFFFFF && (attr & FILE_ATTRIBUTE_DIRECTORY)) folder = selPath;
        else {
            static char parent[MAX_PATH]; char* p; strcpy(parent, selPath);
            p = strrchr(parent, '\\'); if (p) { *p = 0; folder = parent; }
        }
    }

    if (GetUserInput(hMain, "Ordnername", name)) {
        wsprintf(path, "%s\\%s", folder, name);
        if (CreateDirectory(path, NULL)) { 
            LoadProject(g_ProjectDir); 
        } else MessageBox(hMain, "Fehler beim Ordner!", "Error", MB_OK|MB_ICONERROR);
    }
}

void OnDeleteItem() {
    char* path = GetSelectedPath(); int res;
    if (!path) return;
    
    /* Schutz: Projekt-Root nicht löschen */
    if (lstrcmpi(path, g_ProjectDir) == 0) {
        MessageBox(hMain, "Projektordner kann nicht geloescht werden!", "Info", MB_OK);
        return;
    }

    res = MessageBox(hMain, "Wirklich loeschen?", "Loeschen", MB_YESNO|MB_ICONQUESTION);
    if (res == IDYES) {
        DWORD attr = GetFileAttributes(path); int ok = 0;
        if (attr & FILE_ATTRIBUTE_DIRECTORY) { 
            ok = RemoveDirectory(path); 
            if (!ok) MessageBox(hMain, "Ordner muss leer sein!", "Fehler", MB_OK); 
        }
        else ok = DeleteFile(path);
        
        if (ok) { 
            if (lstrcmpi(path, g_CurrentFile) == 0) { g_CurrentFile[0] = 0; /* Clear UI Logic could go here */ }
            LoadProject(g_ProjectDir); 
        }
    }
}

/* --- UI / LOGIK --- */

void PopulateEditor() {
    int i;
    if(!g_pData) return;
    SetWindowText(hAuth, g_pData->auth); SetWindowText(hVer, g_pData->ver);
    SendMessage(hListLink, LB_RESETCONTENT, 0, 0);
    for(i=0; i<g_pData->link_cnt; i++) SendMessage(hListLink, LB_ADDSTRING, 0, (LPARAM)g_pData->links[i]);
    SendMessage(hCmbLang, CB_RESETCONTENT, 0, 0);
    for(i=0; i<g_pData->lang_cnt; i++) SendMessage(hCmbLang, CB_ADDSTRING, 0, (LPARAM)g_pData->lang[i].key);
    SendMessage(hCmbLang, CB_SETCURSEL, g_CurrentLangIdx, 0);
    SetWindowText(hEditDesc, g_pData->lang[g_CurrentLangIdx].desc);
    SendMessage(hListSec, LB_RESETCONTENT, 0, 0);
    if(g_pData->lang_cnt > 0) {
        for(i=0; i<g_pData->lang[g_CurrentLangIdx].sec_cnt; i++) SendMessage(hListSec, LB_ADDSTRING, 0, (LPARAM)g_pData->lang[g_CurrentLangIdx].sec[i].name);
    }
    SetWindowText(hTitle, ""); SetWindowText(hBody, "");
}

void SaveFile() {
    int i;
    if (strlen(g_CurrentFile) == 0 || !g_pData) return;
    GetWindowText(hAuth, g_pData->auth, 64); GetWindowText(hVer, g_pData->ver, 32);
    g_pData->link_cnt = SendMessage(hListLink, LB_GETCOUNT, 0, 0);
    for(i=0; i<g_pData->link_cnt; i++) SendMessage(hListLink, LB_GETTEXT, i, (LPARAM)g_pData->links[i]);
    if(g_pData->lang_cnt > 0) { GetWindowText(hEditDesc, g_pData->lang[g_CurrentLangIdx].desc, 128); }
    if (g_CurrentSecIdx >= 0 && g_pData->lang_cnt > 0) {
        GetWindowText(hTitle, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].name, 64);
        GetWindowText(hBody, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].text, CONTENT_S);
    }
    if (TC_SaveXML(g_CurrentFile, g_pData)) MessageBox(hMain, "Gespeichert!", "Info", MB_OK);
    else MessageBox(hMain, "Fehler!", "Error", MB_OK|MB_ICONERROR);
}

void OnTreeSelect(LPARAM lParam) {
    char* path = (char*)lParam;
    if (path && !(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY)) {
        strcpy(g_CurrentFile, path);
        if (!g_pData) g_pData = (HelpData*)malloc(sizeof(HelpData));
        if (TC_LoadXML(g_CurrentFile, g_pData)) {
            g_CurrentLangIdx=0; g_CurrentSecIdx=-1; PopulateEditor();
        }
    }
}

void OnAddLink() { char buf[64]; GetWindowText(hEditLink, buf, 64); if(strlen(buf)>0) { SendMessage(hListLink, LB_ADDSTRING, 0, (LPARAM)buf); SetWindowText(hEditLink,""); } }
void OnAddLang() { char buf[10]; if (!g_pData) return; GetWindowText(hEditLang, buf, 10); if (strlen(buf)==0) return; if (g_pData->lang_cnt < MAX_L) { strcpy(g_pData->lang[g_pData->lang_cnt].key, buf); strcpy(g_pData->lang[g_pData->lang_cnt].desc, "Neu"); g_pData->lang[g_pData->lang_cnt].sec_cnt = 0; g_CurrentLangIdx = g_pData->lang_cnt; g_pData->lang_cnt++; PopulateEditor(); SetWindowText(hEditLang, ""); } else MessageBox(hMain, "Limit!", "Info", MB_OK); }
void OnSectionSelect() { int idx = SendMessage(hListSec, LB_GETCURSEL, 0, 0); if(!g_pData) return; if(g_CurrentSecIdx >= 0) { GetWindowText(hTitle, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].name, 64); GetWindowText(hBody, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].text, CONTENT_S); } g_CurrentSecIdx = idx; if(idx >= 0) { SetWindowText(hTitle, g_pData->lang[g_CurrentLangIdx].sec[idx].name); SetWindowText(hBody, g_pData->lang[g_CurrentLangIdx].sec[idx].text); } }
void OnAddSection() { int idx; if (!g_pData || g_pData->lang_cnt == 0) return; idx = g_pData->lang[g_CurrentLangIdx].sec_cnt; if (idx >= MAX_S) return; sprintf(g_pData->lang[g_CurrentLangIdx].sec[idx].name, "Sektion %d", idx+1); strcpy(g_pData->lang[g_CurrentLangIdx].sec[idx].text, ""); g_pData->lang[g_CurrentLangIdx].sec_cnt++; PopulateEditor(); SendMessage(hListSec, LB_SETCURSEL, idx, 0); OnSectionSelect(); }

void OnNewProject() { 
    char path[MAX_PATH]; BROWSEINFO bi; LPITEMIDLIST pidl; char projFile[MAX_PATH]; FILE* f; 
    ZeroMemory(&bi, sizeof(bi)); bi.hwndOwner = hMain; bi.lpszTitle="Neues Projekt (Ordner)"; bi.ulFlags=BIF_RETURNONLYFSDIRS; 
    pidl = SHBrowseForFolder(&bi); 
    if (pidl) { 
        SHGetPathFromIDList(pidl, path); wsprintf(projFile, "%s\\Project.tcproject", path); 
        f=fopen(projFile,"w"); if(f){fprintf(f,"<tcproject name=\"New\"></tcproject>"); fclose(f);} 
        LoadProject(projFile); 
    } 
}

void OnOpenProject() {
    char path[MAX_PATH]; BROWSEINFO bi; LPITEMIDLIST pidl;
    ZeroMemory(&bi, sizeof(bi)); bi.hwndOwner = hMain; bi.lpszTitle="Projekt Oeffnen (Ordner)"; bi.ulFlags=BIF_RETURNONLYFSDIRS;
    pidl = SHBrowseForFolder(&bi);
    if (pidl) {
        SHGetPathFromIDList(pidl, path);
        LoadProject(path); 
    }
}

void Resize(HWND hwnd) { 
    RECT rc; int w,h; int treeW=200; HWND hBtn; 
    GetClientRect(hwnd,&rc); w=rc.right; h=rc.bottom; 
    MoveWindow(hTree,5,5,treeW,h-40,TRUE); 
    hBtn = GetDlgItem(hwnd, ID_BTN_MKDIR); if(hBtn) MoveWindow(hBtn, 5, h-30, 100, 25, TRUE); 
    hBtn = GetDlgItem(hwnd, ID_BTN_DEL); if(hBtn) MoveWindow(hBtn, 110, h-30, 90, 25, TRUE); 
    MoveWindow(hBody, 215, 230, w-225, h-240, TRUE); 
    MoveWindow(hTitle, 215, 200, w-225, 20, TRUE); 
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch(msg) {
        case WM_CREATE:
            {
                HFONT hF = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
                hMain=hwnd;
                hTree = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, "", WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES|TVS_LINESATROOT|TVS_HASBUTTONS, 5,5,200,400,hwnd,(HMENU)ID_TREE,g_hInst,NULL);
                
                CreateWindow("STATIC","Auth:",WS_CHILD|WS_VISIBLE,215,12,40,20,hwnd,0,g_hInst,0);
                CreateWindow("STATIC","Ver:",WS_CHILD|WS_VISIBLE,370,12,30,20,hwnd,0,g_hInst,0);
                CreateWindow("STATIC","Sprache:",WS_CHILD|WS_VISIBLE,450,42,50,20,hwnd,0,g_hInst,0);
                
                hAuth = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER,260,10,100,20,hwnd,(HMENU)ID_EDIT_AUTH,g_hInst,0);
                hVer = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER,400,10,50,20,hwnd,(HMENU)ID_EDIT_VER,g_hInst,0);
                CreateWindow("BUTTON","Speichern",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,460,8,80,24,hwnd,(HMENU)ID_BTN_SAVE,g_hInst,0);
                
                hCmbLang = CreateWindow("COMBOBOX","",WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,500,40,60,100,hwnd,(HMENU)ID_CMB_LANG,g_hInst,0);
                hEditLang = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER,570,40,40,20,hwnd,(HMENU)ID_EDIT_LANG,g_hInst,0);
                hBtnAddLang = CreateWindow("BUTTON","+",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,615,40,20,20,hwnd,(HMENU)ID_BTN_ADDLANG,g_hInst,0);
                
                CreateWindow("STATIC","Beschr.:",WS_CHILD|WS_VISIBLE,450,72,50,20,hwnd,0,g_hInst,0);
                hEditDesc = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER,500,70,135,20,hwnd,(HMENU)ID_EDIT_DESC,g_hInst,0);

                CreateWindow("STATIC","Verweise:",WS_CHILD|WS_VISIBLE,215,65,70,20,hwnd,0,g_hInst,0);
                hListLink = CreateWindowEx(WS_EX_CLIENTEDGE,"LISTBOX","",WS_CHILD|WS_VISIBLE|WS_BORDER,215,85,150,80,hwnd,(HMENU)ID_LIST_LINK,g_hInst,0);
                hEditLink = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE,215,170,100,20,hwnd,(HMENU)ID_EDIT_LINK,g_hInst,0);
                CreateWindow("BUTTON","+",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,320,170,20,20,hwnd,(HMENU)ID_BTN_ADDLNK,g_hInst,0);
                CreateWindow("BUTTON","-",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,340,170,20,20,hwnd,(HMENU)ID_BTN_REMLNK,g_hInst,0);

                CreateWindow("STATIC","Sektionen:",WS_CHILD|WS_VISIBLE,400,95,70,20,hwnd,0,g_hInst,0);
                hListSec = CreateWindowEx(WS_EX_CLIENTEDGE,"LISTBOX","",WS_CHILD|WS_VISIBLE|LBS_NOTIFY|WS_VSCROLL,400,115,150,80,hwnd,(HMENU)ID_LIST_SEC,g_hInst,0);
                CreateWindow("BUTTON","+ Sektion",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,560,115,70,20,hwnd,(HMENU)ID_BTN_ADDSEC,g_hInst,0);

                hTitle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE,215,200,300,20,hwnd,(HMENU)ID_EDIT_TITLE,g_hInst,0);
                hBody = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|ES_WANTRETURN,215,230,400,200,hwnd,(HMENU)ID_EDIT_BODY,g_hInst,0);

                CreateWindow("BUTTON","Ordner +",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,5,430,100,24,hwnd,(HMENU)ID_BTN_MKDIR,g_hInst,0);
                CreateWindow("BUTTON","Loeschen",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,110,430,90,24,hwnd,(HMENU)ID_BTN_DEL,g_hInst,0);

                EnumChildWindows(hwnd, SetFontProc, (LPARAM)hF);
                g_pData = (HelpData*)malloc(sizeof(HelpData));
                TC_InitData(g_pData);
            }
            return 0;

        case WM_SIZE: Resize(hwnd); return 0;
        
        case WM_NOTIFY: 
            if (((LPNMHDR)lParam)->code == TVN_SELCHANGED) { LPNMTREEVIEW pnmtv=(LPNMTREEVIEW)lParam; OnTreeSelect(pnmtv->itemNew.lParam); } 
            if (((LPNMHDR)lParam)->code == TVN_DELETEITEM) { LPNMTREEVIEW pnmtv=(LPNMTREEVIEW)lParam; if(pnmtv->itemOld.lParam) free((void*)pnmtv->itemOld.lParam); }
            return 0;

        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case IDM_NEWPROJ: OnNewProject(); break;
                case IDM_OPENPROJ: OnOpenProject(); break; 
                case IDM_NEWFILE: OnNewFile(); break;
                case ID_BTN_SAVE: SaveFile(); break;
                case ID_BTN_ADDLNK: OnAddLink(); break;
                case ID_BTN_ADDLANG: OnAddLang(); break;
                case ID_BTN_ADDSEC: OnAddSection(); break;
                case ID_LIST_SEC: if(HIWORD(wParam)==LBN_SELCHANGE) OnSectionSelect(); break;
                case ID_BTN_MKDIR: OnNewFolder(); break;
                case ID_BTN_DEL: OnDeleteItem(); break;
                case IDM_EXIT: PostQuitMessage(0); break;
            }
            if (LOWORD(wParam) == ID_CMB_LANG && HIWORD(wParam) == CBN_SELCHANGE) {
                 if(g_CurrentSecIdx >= 0) {
                    GetWindowText(hTitle, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].name, 64);
                    GetWindowText(hBody, g_pData->lang[g_CurrentLangIdx].sec[g_CurrentSecIdx].text, CONTENT_S);
                    GetWindowText(hEditDesc, g_pData->lang[g_CurrentLangIdx].desc, 128);
                 }
                 g_CurrentLangIdx = SendMessage(hCmbLang, CB_GETCURSEL, 0, 0);
                 g_CurrentSecIdx = -1;
                 PopulateEditor();
            }
            return 0;
        case WM_DESTROY: if(g_pData) free(g_pData); PostQuitMessage(0); return 0;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR cmd, int show) {
    WNDCLASSEX wc = {0}; MSG msg; HMENU hMenu; g_hInst = hInstance; InitCommonControls();
    wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=WndProc; wc.hInstance=hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1); wc.lpszMenuName=NULL; wc.lpszClassName="TCHEdit32";
    RegisterClassEx(&wc);
    hMenu = CreateMenu();
    { 
        HMENU hSub=CreateMenu(); 
        AppendMenu(hSub,MF_STRING,IDM_NEWPROJ,"Neues Projekt"); 
        AppendMenu(hSub,MF_STRING,IDM_OPENPROJ,"Projekt oeffnen"); 
        AppendMenu(hSub,MF_STRING,IDM_NEWFILE,"Neue Datei"); 
        AppendMenu(hSub,MF_SEPARATOR,0,0); 
        AppendMenu(hSub,MF_STRING,IDM_EXIT,"Beenden"); 
        AppendMenu(hMenu,MF_POPUP,(UINT)hSub,"Datei"); 
    }
    CreateWindowEx(WS_EX_APPWINDOW,"TCHEdit32","TC Help Studio (Win32)",WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,800,600,NULL,hMenu,hInstance,NULL);
    while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
    return msg.wParam;
}