1 // -*- mode:C++ ; compile-command: "g++ -I.. -g -c Equation.cc" -*-
2 #ifndef _KDISPLAY_H
3 #define _KDISPLAY_H
4 #include "config.h"
5 #include "giacPCH.h"
6 #ifdef KHICAS
7 #include "misc.h"
8 
9 #include <exception>
10 
11 class autoshutdown : public std::exception{
what()12   const char * what () const throw ()
13   {
14     return "autoshutdown";
15   }
16 };
17 
18 extern  const int LCD_WIDTH_PX;
19 extern   const int LCD_HEIGHT_PX;
20 extern char* fmenu_cfg;
21 #define STATUS_AREA_PX 0 // 24
22 #define GIAC_HISTORY_MAX_TAILLE 32
23 #define GIAC_HISTORY_SIZE 2
24 
25 #ifdef MICROPY_LIB
26 extern "C" {
27   int do_file(const char *file);
28   char * micropy_init();
29   int micropy_eval(const char * line);
30   void  mp_deinit();
31   void mp_stack_ctrl_init();
32   extern int parser_errorline,parser_errorcol;
33 }
34 #endif
35 
36 #include "k_csdk.h"
37 
38 extern "C" {
39   int do_shutdown(); // auto-shutdown
40   void console_output(const char *,int );
41   const char * console_input(const char * msg1,const char * msg2,bool numeric,int ypos);
42   void c_draw_rectangle(int x,int y,int w,int h,int c);
43   void c_fill_rect(int x,int y,int w,int h,int c);
44   void c_draw_line(int x0,int y0,int x1,int y1,int c);
45   void c_draw_circle(int xc,int yc,int r,int color,bool q1,bool q2,bool q3,bool q4);
46   void c_draw_filled_circle(int xc,int yc,int r,int color,bool left,bool right);
47   void c_draw_polygon(int * x,int *y ,int n,int color);
48   void c_draw_filled_polygon(int * x,int *y, int n,int xmin,int xmax,int ymin,int ymax,int color);
49   void c_draw_arc(int xc,int yc,int rx,int ry,int color,double theta1, double theta2);
50   void c_draw_filled_arc(int x,int y,int rx,int ry,int theta1_deg,int theta2_deg,int color,int xmin,int xmax,int ymin,int ymax,bool segment);
51   void c_set_pixel(int x,int y,int c);
52   int c_draw_string(int x,int y,int c,int bg,const char * s,bool fake);
53   int c_draw_string_small(int x,int y,int c,int bg,const char * s,bool fake);
54   int c_draw_string_medium(int x,int y,int c,int bg,const char * s,bool fake);
55   int select_item(const char ** ptr,const char * title,bool askfor1=true);
56   // C conversion to gen from atomic data type
57   unsigned long long c_double2gen(double);
58   unsigned long long c_int2gen(int);
59   // linalg on double matrices
60   void doubleptr2matrice(double * x,int n,giac::matrice & m);
61   bool matrice2doubleptr(const giac::matrice &M,double *x); // x must have enough space
62   bool r_inv(double *,int n);
63   bool r_rref(double *,int n,int m);
64   double r_det(double *,int);
65   struct double_pair {
66     double r,i;
67   } ;
68   typedef struct double_pair c_complex;
69   bool matrice2c_complexptr(const giac::matrice &M,c_complex *x);
70   void c_complexptr2matrice(c_complex * x,int n,int m,giac::matrice & M);
71   bool c_inv(c_complex *,int n);
72   bool c_rref(c_complex *,int n,int m);
73   c_complex c_det(c_complex *,int);
74   bool c_egv(c_complex * x,int n); // eigenvectors
75   bool c_eig(c_complex * x,c_complex * d,int n); // x eigenvect, d reduced mat
76   bool c_proot(c_complex * x,int n); // poly root
77   bool c_pcoeff(c_complex * x,int n); // root->coeffs
78   bool c_fft(c_complex * x,int n,bool inverse); // FFT
79   void turtle_freeze();
80   void c_sprint_double(char * s,double d);
81 }
82 extern int lang;
83 extern bool warn_nr;
84 int select_interpreter(); // 0 Xcas, 1|2 Xcas python_compat(1|2), 3 MicroPython
85 const char * gettext(const char * s) ;
86 
87 #ifndef NO_NAMESPACE_XCAS
88 namespace xcas {
89 #endif // ndef NO_NAMESPACE_XCAS
90   void set_exam_mode(int i,const giac::context *);
91   int giac_filebrowser(char * filename,const char * extension,const char * title);
92   void draw_rectangle(int x,int y,int w,int h,int c);
93   void draw_line(int x0,int y0,int x1,int y1,int c);
94   void draw_circle(int xc,int yc,int r,int color,bool q1=true,bool q2=true,bool q3=true,bool q4=true);
95   void draw_filled_circle(int xc,int yc,int r,int color,bool left=true,bool right=true);
96   void draw_polygon(std::vector< std::vector<int> > & v1,int color);
97   void draw_filled_polygon(std::vector< vector<int> > &L,int xmin,int xmax,int ymin,int ymax,int color);
98   void draw_arc(int xc,int yc,int rx,int ry,int color,double theta1, double theta2);
99   void draw_filled_arc(int x,int y,int rx,int ry,int theta1_deg,int theta2_deg,int color,int xmin,int xmax,int ymin,int ymax,bool segment);
100 
101   bool textedit(char * s,int bufsize,const giac::context * contextptr);
102   // maximum "size" of symbolics displayed in an Equation (pretty print)
103   extern unsigned max_prettyprint_equation;
104   // matrix select
105   bool eqw_select(const giac::gen & eq,int l,int c,bool select,giac::gen & value);
106   void Equation_select(giac::gen & eql,bool select);
107   int eqw_select_down(giac::gen & g);
108   int eqw_select_up(giac::gen & g);
109 
110   giac::gen Equation_compute_size(const giac::gen & g,const giac::attributs & a,int windowhsize,const giac::context * contextptr);
111   giac::eqwdata Equation_total_size(const giac::gen & g);
112   // Equation_translate(giac::gen & g,int deltax,int deltay);
113   void Equation_vertical_adjust(int hp,int yp,int & h,int & y);
114   bool Equation_find_vector_pos(giac::const_iterateur it,giac::const_iterateur itend,int & i,int &nrows);
115   bool Equation_adjust_xy(giac::gen & g,int & xleft,int & ytop,int & xright,int & ybottom,giac::gen * & gsel,giac::gen * & gselparent,int &gselpos,std::vector<int> * gotosel=0);
116   // select and set value from eqwdata in eql
117   bool do_select(giac::gen & eql,bool select,giac::gen & value);
118 
119   class Equation {
120     int _x,_y;
121   public:
122     giac::gen data,undodata; // of type eqwdata or undef if empty
123     giac::attributs attr;
124     const giac::context * contextptr;
x()125     int x() const { return _x;}
y()126     int y() const { return _y;}
127     Equation(int x_, int y_, const giac::gen & g,const giac::context *);
128   };
129 
130   void display(Equation &eq ,int x,int y,const giac::context *);
131   // replace selection in eq by tmp
132   void replace_selection(Equation & eq,const giac::gen & tmp,giac::gen * gsel,const std::vector<int> * gotoptr,const giac::context *);
133   int eqw_select_leftright(xcas::Equation & g,bool left,int exchange,const giac::context *);
134 
135   class Graph2d{
136   public:
137     double window_xmin,window_xmax,window_ymin,window_ymax,
138       x_scale,y_scale,x_tick,y_tick;
139     int display_mode,show_axes,show_names,labelsize;
140     giac::gen g;
141     const giac::context * contextptr;
142     bool findij(const giac::gen & e0,double x_scale,double y_scale,double & i0,double & j0,const giac::context * ) const;
143     void update();
144     void zoomx(double d,bool round=false);
145     void zoomy(double d,bool round=false);
146     void zoom(double);
147     void left(double d);
148     void right(double d);
149     void up(double d);
150     void down(double d);
151     void autoscale(bool fullview=false);
152     void orthonormalize();
153     void draw();
154     Graph2d(const giac::gen & g_,const giac::context * );
155   };
156 
157   struct Turtle {
158     void draw();
159 #ifdef TURTLETAB
160     giac::logo_turtle * turtleptr;
161 #else
162     std::vector<giac::logo_turtle> * turtleptr;
163 #endif
164     int turtlex,turtley; // Turtle translate
165     double turtlezoom; // Zoom factor for turtle screen
166     short int maillage=0; // 0 (none), 1 (square), 2 (triangle), bit3 used for printing
167     short int speed=0;
168   };
169 
170   int displaygraph(const giac::gen & ge, const giac::context * contextptr);
171   int displaylogo();
172   giac::gen eqw(const giac::gen & ge,bool editable,const giac::context * contextptr);
173   typedef short int color_t;
174   typedef struct
175   {
176     std::string s;
177     color_t color=giac::_BLACK;
178     short int newLine=0; // if 1, new line will be drawn before the text
179     short int spaceAtEnd=0;
180     short int lineSpacing=0;
181     short int minimini=0;
182     int nlines=1;
183   } textElement;
184 
185 #define TEXTAREATYPE_NORMAL 0
186 #define TEXTAREATYPE_INSTANT_RETURN 1
187   typedef struct
188   {
189     int x=0;
190     int y=0;
191     int line=0,undoline=0;
192     int pos=0,undopos=0;
193     int clipline,undoclipline;
194     int clippos,undoclippos;
195     int width=LCD_WIDTH_PX;
196     int lineHeight=17;
197     std::vector<textElement> elements,undoelements;
198     const char* title = NULL;
199     std::string filename;
200     int scrollbar=1;
201     bool allowEXE=false; //whether to allow EXE to exit the screen
202     bool allowF1=false; //whether to allow F1 to exit the screen
203     bool OKparse=true;
204     bool editable=false;
205     bool changed=false;
206     int python=0;
207     int type=TEXTAREATYPE_NORMAL;
208   } textArea;
209 
210 #define TEXTAREA_RETURN_EXIT 0
211 #define TEXTAREA_RETURN_EXE 1
212 #define TEXTAREA_RETURN_F1 2
213   int doTextArea(textArea* text,const giac::context * contextptr); //returns 0 when user EXITs, 1 when allowEXE is true and user presses EXE, 2 when allowF1 is true and user presses F1.
214   std::string merge_area(const std::vector<textElement> & v);
215   void save_script(const char * filename,const std::string & s);
216   void add(textArea *edptr,const std::string & s);
217 
218   extern textArea * edptr;
219   std::string get_searchitem(std::string & replace);
220   int check_leave(textArea * text);
221   void reload_edptr(const char * filename,textArea *edptr,const giac::context *);
222   void print(int &X,int&Y,const char * buf,int color,bool revert,bool fake,bool minimini);
223 
224   void save_session(const giac::context * );
225 #if 1
226 #define MAX_FILENAME_SIZE 63
227   void save_console_state_smem(const char * filename,const giac::context *);
228   bool load_console_state_smem(const char * filename,const giac::context *);
229 
230   struct DISPBOX {
231     int     left;
232     int     top;
233     int     right;
234     int     bottom;
235     unsigned char mode;
236   } ;
237 
238 
239   enum CONSOLE_RETURN_VAL {
240 			   CONSOLE_NEW_LINE_SET = 1,
241 			   CONSOLE_SUCCEEDED = 0,
242 			   CONSOLE_MEM_ERR = -1,
243 			   CONSOLE_ARG_ERR = -2,
244 			   CONSOLE_NO_EVENT = -3,
245   };
246 
247   enum CONSOLE_CURSOR_DIRECTION{
248 				CURSOR_UP,
249 				CURSOR_DOWN,
250 				CURSOR_LEFT,
251 				CURSOR_RIGHT,
252 				CURSOR_SHIFT_LEFT,
253 				CURSOR_SHIFT_RIGHT,
254 				CURSOR_ALPHA_UP,
255 				CURSOR_ALPHA_DOWN,
256   };
257 
258   enum CONSOLE_LINE_TYPE{
259 			 LINE_TYPE_INPUT=0,
260 			 LINE_TYPE_OUTPUT=1
261   };
262 
263   enum CONSOLE_CASE{
264 		    LOWER_CASE,
265 		    UPPER_CASE
266   };
267 
268   enum CONSOLE_SCREEN_SPEC {
269 			    _LINE_MAX = 48,
270 			    LINE_DISP_MAX = 11,
271 #ifdef NSPIRE_NEWLIB
272 			    COL_DISP_MAX = 32,
273 #else
274 			    COL_DISP_MAX = 30,//32
275 #endif
276 			    EDIT_LINE_MAX = 2048
277   };
278 
279   struct console_line {
280     char *str;
281     short int readonly;
282     short int type;
283     int start_col;
284     int disp_len;
285   };
286 
287   struct FMenu{
288     char* name;
289     char** str;
290     unsigned char count;
291   };
292 
293   struct location{
294     int x;
295     int y;
296   };
297 
298 #define MAX_FMENU_ITEMS 8
299 #define FMENU_TITLE_LENGHT 4
300 
301 #define is_wchar(c) ((c == 0x7F) || (c == 0xF7) || (c == 0xF9) || (c == 0xE5) || (c == 0xE6) || (c == 0xE7))
302 #define printf(s) Console_Output((const char *)s);
303 
304   int Console_DelStr(char *str, int end_pos, int n);
305   int Console_InsStr(char *dest, const char *src, int disp_pos);
306   int Console_GetActualPos(const char *str, int disp_pos);
307   int Console_GetDispLen(const char *str);
308   int Console_MoveCursor(int direction);
309   int Console_Input(const char *str);
310   int Console_Output(const char *str);
311   void Console_Clear_EditLine();
312   int Console_NewLine(int pre_line_type, int pre_line_readonly);
313   int Console_Backspace(const giac::context *);
314   int Console_GetKey(const giac::context *);
315   int Console_Init(const giac::context *);
316   int Console_Disp(int redraw_mode,const giac::context*ptr);
317   int Console_FMenu(int key,const giac::context *);
318   extern char menu_f1[8],menu_f2[8],menu_f3[8],menu_f4[8],menu_f5[8],menu_f6[8];
319   const char * console_menu(int key,char* cfg,int active_app);
320   void Console_FMenu_Init(const giac::context *);
321   const char * Console_Draw_FMenu(int key, struct FMenu* menu,char * cfg_,int active_app);
322   char *Console_Make_Entry(const char* str);
323   char *Console_GetLine(const giac::context *);
324   char* Console_GetEditLine();
325   void dConsolePut(const char *);
326   void dConsolePutChar(const char );
327   void dConsoleRedraw(void);
328   extern int dconsole_mode;
329   extern int console_changed; // 1 if something new in history
330   extern char session_filename[MAX_FILENAME_SIZE+1];
331   const char * input_matrix(bool list,const giac::context *);
332   void warn_python(int python,bool autochange=false);
333   // void draw_menu(int editor); // 0 console, 1 editor
334   int get_set_session_setting(int value);
335   void menu_setup(const giac::context *);
336   int console_main(const giac::context *);
337 #endif
338   int periodic_table(const char * & name,const char * & symbol,char * protons,char * nucleons,char * mass,char * electroneg);
339 
340   struct tableur {
341     giac::matrice m,clip,undo;
342     giac::gen var;
343     int nrows,ncols;
344     int cur_row,cur_col,disp_row_begin,disp_col_begin;
345     int sel_row_begin,sel_col_begin;
346     std::string cmdline,filename;
347     int cmd_pos,cmd_row,cmd_col; // row/col of current cmdline, -1 if not active
348     bool changed,recompute,matrix_fill_cells,movedown;
349   } ;
350   extern tableur * sheetptr;
351 
352   int check_do_graph(giac::gen & ge,int do_logo_graph_eqw,const giac::context *);
353   int get_filename(char * filename,const char * extension);
354 
355 #ifndef NO_NAMESPACE_XCAS
356 } // namespace xcas
357 #endif // ndef NO_NAMESPACE_XCAS
358 
359 giac::gen sheet(const giac::context *); // in kadd.cc
360 /* ************************************************************
361 **************************************************************
362 ***********************************************************  */
363 
364 
365 #ifndef NO_NAMESPACE_XCAS
366 namespace giac {
367 #endif // ndef NO_NAMESPACE_XCAS
368   std::string help_insert(const char * cmdline,const giac::context *);
369   void copy_clipboard(const std::string & s,bool status);
370 #define TEXT_MODE_NORMAL 0
371 #define TEXT_MODE_INVERT 1
372 #define MENUITEM_NORMAL 0
373 #define MENUITEM_CHECKBOX 1
374 #define MENUITEM_SEPARATOR 2
375 #define MENUITEM_VALUE_NONE 0
376 #define MENUITEM_VALUE_CHECKED 1
377   typedef struct
378   {
379     char* text; // text to be shown on screen. mandatory, must be a valid pointer to a string.
380     int token; // for syntax help on keywords not in the catalog
381     short int isfolder=0; // for file browsers, this will signal the item is a folder
382     signed char color=giac::_BLACK; // color of the menu item (use TEXT_COLOR_* to define)
383     signed char type=MENUITEM_NORMAL; // type of the menu item. use MENUITEM_* to set this
384     signed char value=MENUITEM_VALUE_NONE; // value of the menu item. For example, if type is MENUITEM_CHECKBOX and the checkbox is checked, the value of this var will be MENUITEM_VALUE_CHECKED
385     // The following two settings require the menu type to be set to MENUTYPE_MULTISELECT
386     signed char isselected=0; // for file browsers and other multi-select screens, this will show an arrow before the item
387     signed char icon=-1; //for file browsers, to show a file icon. -1 shows no icon (default)
388   } MenuItem;
389 
390   typedef struct
391   {
392     unsigned short data[0x12*0x18];
393   } MenuItemIcon;
394 
395 #define MENUTYPE_NORMAL 0
396 #define MENUTYPE_MULTISELECT 1
397 #define MENUTYPE_INSTANT_RETURN 2 // this type of menu insantly returns even if user hasn't selected an option (allows for e.g. redrawing the GUI behind it). if user hasn't exited or selected an option, menu will return MENU_RETURN_INSTANT
398 #define MENUTYPE_NO_KEY_HANDLING 3 //this type of menu doesn't handle any keys, only draws.
399 #define MENUTYPE_FKEYS 4 // returns GetKey value of a Fkey when one is pressed
400   typedef struct {
401     char* statusText = NULL; // text to be shown on the status bar, may be empty
402     char* title = NULL; // title to be shown on the first line if not null
403     char* subtitle = NULL;
404     int titleColor=giac::_BLUE; //color of the title
405     char* nodatamsg; // message to show when there are no menu items to display
406     int startX=1; //X where to start drawing the menu. NOTE this is not absolute pixel coordinates but rather character coordinates
407     int startY=0; //Y where to start drawing the menu. NOTE this is not absolute pixel coordinates but rather character coordinates
408     int width=30; // NOTE this is not absolute pixel coordinates but rather character coordinates
409     int height=12; // NOTE this is not absolute pixel coordinates but rather character coordinates
410     int scrollbar=1; // 1 to show scrollbar, 0 to not show it.
411     int scrollout=0; // whether the scrollbar goes out of the menu area (1) or it overlaps some of the menu area (0)
412     int numitems; // number of items in menu
413     int type=MENUTYPE_NORMAL; // set to MENUTYPE_* .
414     int selection=1; // currently selected item. starts counting at 1
415     int scroll=0; // current scrolling position
416     int fkeypage=0; // for MULTISELECT menu if it should allow file selecting and show the fkey label
417     int numselitems=0; // number of selected items
418     int returnOnInfiniteScrolling=0; //whether the menu should return when user reaches the last item and presses the down key (or the first item and presses the up key)
419     int darken=0; // for dark theme on homeGUI menus
420     int miniMiniTitle=0; // if true, title will be drawn in minimini. for calendar week view
421     int pBaRtR=0; //preserve Background And Return To Redraw. Rarely used
422     MenuItem* items; // items in menu
423   } Menu;
424 
425 #define MENU_RETURN_EXIT 0
426 #define MENU_RETURN_SELECTION 1
427 #define MENU_RETURN_INSTANT 2
428 #define MENU_RETURN_SCROLLING 3 //for returnOnInfiniteScrolling
429 
430   typedef struct {
431     const char* name;
432     const char* insert;
433     const char* desc;
434     const char * example;
435     const char * example2;
436     int category;
437   } catalogFunc;
438 
439   void aide2catalogFunc(const giac::aide & a,catalogFunc & c);
440 
441   giac::gen select_var(const giac::context * contextptr);
442   const char * keytostring(int key,int keyflag,bool py,const giac::context * contextptr);
443   void insert(std::string & s,int pos,const char * add);
444 
445   int showCatalog(char* insertText,int preselect,int menupos,const giac::context * contextptr);
446   int doMenu(Menu* menu, MenuItemIcon* icontable=NULL);
447   void reset_alpha();
448   // category=0 for CATALOG, 1 for OPTN
449   // returns 0 on exit, 1 on success
450   int doCatalogMenu(char* insertText, const char* title, int category,const giac::context * contextptr);
451   extern const char shortcuts_fr_string[];
452   extern const char shortcuts_en_string[];
453   extern const char apropos_fr_string[];
454   extern const char apropos_en_string[];
455   void init_locale();
456 
457   gen turtle_state(const giac::context * contextptr);
458   int inputline(const char * msg1,const char * msg2,std::string & s,bool numeric,int ypos=65,const giac::context *contextptr=0);
459   bool inputdouble(const char * msg1,double & d,const giac::context *contextptr);
460   bool do_confirm(const char * s);
461   int confirm(const char * msg1,const char * msg2,bool acexit=false,int y=40);
462   bool confirm_overwrite();
463   void invalid_varname();
464 
465 #ifndef NO_NAMESPACE_XCAS
466 } // namespace giac
467 #endif // ndef NO_NAMESPACE_XCAS
468 
469 
470 #define COLOR_BLACK giac::_BLACK
471 #define COLOR_RED giac::_RED
472 #define COLOR_GREEN giac::_GREEN
473 #define COLOR_CYAN giac::_CYAN
474 #define COLOR_BLUE giac::_BLUE
475 #define COLOR_YELLOW giac::_YELLOW
476 #define COLOR_MAGENTA giac::_MAGENTA
477 #define COLOR_WHITE giac::_WHITE
478 #define COLOR_YELLOWDARK 64934
479 #define COLOR_BROWN 65000
480 #define TEXT_COLOR_BLACK giac::_BLACK
481 #define TEXT_COLOR_RED giac::_RED
482 #define TEXT_COLOR_GREEN giac::_GREEN
483 #define TEXT_COLOR_CYAN giac::_CYAN
484 #define TEXT_COLOR_BLUE giac::_BLUE
485 #define TEXT_COLOR_YELLOW giac::_YELLOW
486 #define TEXT_COLOR_WHITE giac::_WHITE
487 #define TEXT_COLOR_MAGENTA giac::_MAGENTA
488 
489 
490 #endif // _KDISPLAY_H
491 #endif
492