1 /*
2     This file is part of GNU APL, a free implementation of the
3     ISO/IEC Standard 13751, "Programming Language APL, Extended"
4 
5     Copyright (C) 2008-2017  Dr. Jürgen Sauermann
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __Quad_GTK_DEFINED__
22 #define __Quad_GTK_DEFINED__
23 
24 #include <math.h>
25 #include <vector>
26 
27 #include "QuadFunction.hh"
28 #include "Value.hh"
29 #include "UCS_string_vector.hh"
30 
31 /// The class implementing ⎕GTK
32 class Quad_GTK : public QuadFunction
33 {
34 public:
35    /// Constructor.
Quad_GTK()36    Quad_GTK()
37       : QuadFunction(TOK_Quad_GTK)
38    {}
39 
40    static Quad_GTK * fun;          ///< Built-in function.
41    static Quad_GTK  _fun;          ///< Built-in function.
42 
43    /// close all open windows
44    void clear();
45 
46 protected:
47    /// the type of a function parameter or return value
48    enum Gtype
49       {
50         gtype_V = 0,   ///< void
51         gtype_S = 1,   ///< string
52         gtype_I = 2,   ///< integer
53         gtype_F = 3,   ///< float
54       };
55 
56 #include "Gtk/Gtk_enums.hh"
57 
58    /// the context for a GTL window
59    struct window_entry
60       {
61         int fd;   ///< pipe to the windw process
62       };
63 
64    /// overloaded Function::eval_AB()
65    Token eval_AB(Value_P A, Value_P B);
66 
67    /// overloaded Function::eval_AXB()
68    virtual Token eval_AXB(Value_P A, Value_P X, Value_P B);
69 
70    /// overloaded Function::eval_B()
71    Token eval_B(Value_P B);
72 
73    /// overloaded Function::eval_XB()
74    virtual Token eval_XB(Value_P X, Value_P B);
75 
76    /// X is supposed to be something like 4,"win_id". Store win_id in window_id
77    /// and return the window number (4 in this  example)
78    int resolve_window(const Value * X, UTF8_string & window_id);
79 
80    /// B is a function name (-suffix).
81    Fnum resolve_fun_name(UTF8_string & window_id, const Value * B);
82 
83    /// write a TLV with an empty V (thus L=0)
84    int write_TL0(int fd, int tag);
85 
86    /// write a TLV
87    int write_TLV(int fd, int tag, const UTF8_string & value);
88 
89    /// open the GTH window described by gui_filename and optional css_filename
90    int open_window(const UCS_string & gui_filename,
91                    const UCS_string * css_filename);
92 
93    /// return the handles of currently open windows
94    Value_P window_list() const;
95 
96    /// close the window with file descriptor fd
97    Value_P close_window(int fd);
98 
99    /// poll all fds and insert events into \b event_queue until no more
100    /// events are pending
101    void poll_all();
102 
103    /// poll for a TLV on fd with a specific (reponse-) tag
104    Value_P poll_response(int fd, int tag);
105 
106    /** read a TLV with a given tag (or any TLV if tag == -1) on a fd
107        that is ready for reading. If the TLV is an event (i.e. unexpected)
108        then insert it into event_queue and return 0; otherwise the TLV
109        is a response that is returned.
110     **/
111    Value_P read_fd(int fd, int tag);
112 
113    /// the currently open GTK windows
114    std::vector<window_entry> open_windows;
115 
116    /// event queue for events from the GTK windows
117    UCS_string_vector event_queue;
118 };
119 
120 #endif // __Quad_GTK_DEFINED__
121