(*
    Copyright 2016, 2018, 2020, 2022 Anton Krotov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)

MODULE ColorDlg;

IMPORT sys := SYSTEM, KOSAPI;

TYPE

  DRAW_WINDOW = PROCEDURE;

  TDialog = RECORD
    _type,
    procinfo,
    com_area_name,
    com_area,
    start_path: INTEGER;
    draw_window: DRAW_WINDOW;
    status*,
    X, Y,
    color_type,
    color*: INTEGER;

    procinf: ARRAY 1024 OF CHAR;
    s_com_area_name: ARRAY 32 OF CHAR
  END;

  Dialog* = POINTER TO TDialog;

VAR

  Dialog_start, Dialog_init: PROCEDURE [stdcall] (cd: Dialog);

PROCEDURE Show*(cd: Dialog);
BEGIN
  IF cd # NIL THEN
    cd.X := 0;
    cd.Y := 0;
    Dialog_start(cd)
  END
END Show;

PROCEDURE Create*(draw_window: DRAW_WINDOW): Dialog;
VAR res: Dialog;
BEGIN
  NEW(res);
  IF res # NIL THEN
    res.s_com_area_name := "FFFFFFFF_color_dlg";
    res.com_area := 0;
    res._type := 0;
    res.color_type := 0;
    res.procinfo := sys.ADR(res.procinf[0]);
    res.com_area_name := sys.ADR(res.s_com_area_name[0]);
    res.start_path := sys.SADR("/sys/colrdial");
    res.draw_window := draw_window;
    res.status := 0;
    res.X := 0;
    res.Y := 0;
    res.color := 0;
    Dialog_init(res)
  END
  RETURN res
END Create;

PROCEDURE Destroy*(VAR cd: Dialog);
BEGIN
  IF cd # NIL THEN
    DISPOSE(cd)
  END
END Destroy;

PROCEDURE Load;
VAR Lib: INTEGER;

  PROCEDURE GetProc(Lib, v: INTEGER; name: ARRAY OF CHAR);
  VAR a: INTEGER;
  BEGIN
    a := KOSAPI.GetProcAdr(name, Lib);
    ASSERT(a # 0);
    sys.PUT(v, a)
  END GetProc;

BEGIN
  Lib := KOSAPI.LoadLib("/sys/Lib/Proc_lib.obj");
  GetProc(Lib, sys.ADR(Dialog_init), "ColorDialog_init");
  GetProc(Lib, sys.ADR(Dialog_start), "ColorDialog_start");
END Load;

BEGIN
  Load
END ColorDlg.