home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / wxwin140 / utils / hytext / src / hytext.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  10.1 KB  |  332 lines

  1. /*
  2.  * File:     hytext.h
  3.  * Purpose:  Hypertext library for wxWindows
  4.  *
  5.  *                       wxWindows 1.40
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                        Date: 18-4-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * hytext.h
  31.  * Hypertext Window implementation.
  32.  * This is a text window with highlighted text which can respond
  33.  * to mouse clicks. Blocks are stored in ordinary text files as:
  34.  * \hy-A{B}{C}  where A is an integer representing the type of
  35.  * block, B is an integer identifier for this block, and C is the
  36.  * text enclosed by the block. Blocks may be nested.
  37.  * Mouse input member functions are provided for overriding by an application,
  38.  * e.g. for displaying further information.
  39.  */
  40.  
  41. #ifndef hypertexth
  42. #define hypertexth
  43. #include <stdio.h>
  44. #include <wx.h>
  45. #include "wx_hash.h"
  46.  
  47. /*
  48.  * Stored in the text file are only block types, not font information.
  49.  * This mapping class is necessary to establish what types should
  50.  * be depicted with what font/colour.
  51.  */
  52.  
  53. #define wxHYPER_NONE    0
  54. #define wxHYPER_SECTION 1
  55.  
  56. class wxHTMappingStructure: public wxObject
  57. {
  58.  public:
  59.   char *name;
  60.   int block_type;
  61.   int text_size;
  62.   int text_family;
  63.   int text_style;
  64.   int text_weight;
  65.   int special_attribute; // e.g. section
  66.   Bool visibility;
  67.   char *text_colour;
  68.  
  69.   // These are only for internal use
  70.   int logical_op;
  71.   wxColour *background_colour;
  72.   
  73.   wxHTMappingStructure(void);
  74.   wxHTMappingStructure(int the_block_type, int the_text_size, int the_text_family,
  75.                   int the_text_style, int the_text_weight,
  76.                   char *the_text_colour, char *the_name, int the_attribute = wxHYPER_NONE,
  77.                   Bool visibility = TRUE);
  78.   ~wxHTMappingStructure(void);
  79.  
  80.   wxFont *GetFont(void);
  81.   wxHTMappingStructure *Copy(void);
  82. };
  83.  
  84. class wxHyperTextMapping: public wxList
  85. {
  86.  public:
  87.   wxNode *current_mapping;  // For GetFirst/NextType
  88.  
  89.   wxHyperTextMapping(void);
  90.   ~wxHyperTextMapping(void);
  91.   void ClearMapping(void);
  92.   void AddMapping(int block_type, int text_size, int text_family,
  93.                   int text_style, int text_weight, char *text_colour,
  94.                   char *name, int the_attribute = -1, Bool visibility = -1);
  95.   Bool GetMapping(int block_type, int *text_size, int *text_family,
  96.                   int *text_style, int *text_weight, char **text_colour,
  97.                   char **name, int *the_attribute, Bool *visibility);
  98.  
  99.   wxHTMappingStructure *FindByName(char *name);
  100.  
  101.   // Pop up dialog box for editing mappings
  102.   void Edit(wxWindow *parent = NULL);
  103.  
  104.   // -1 if no more types
  105.   int GetFirstType(void);
  106.   int GetNextType(void);
  107. };
  108.  
  109. class wxTextChunk;
  110. class wxHyperTextWindow: public wxCanvas
  111. {
  112.  public:
  113.   Bool modified;
  114.   Bool edit_mode;
  115.   wxList current_selections;
  116.   wxNode *selection_current;
  117.   wxList sections;         // List of chunks
  118.   wxNode *current_section; // Node points into 'sections'
  119.   char *Title;
  120.   Bool indexWriting;
  121.  
  122.   int hyleft_margin;
  123.   int hytop_margin;
  124.  
  125.   int DragState;
  126.   long DraggedBlock;
  127.   float old_drag_x, old_drag_y;  // Previous drag coordinates
  128.   float OutlineStartX;
  129.   float OutlineStartY;
  130.  
  131.   wxList text_chunks;
  132.   wxHyperTextMapping *mapping;
  133.   wxHashTable *LinkTable;
  134.   wxNode *current_block;  // For GetFirst/NextBlock
  135.   int no_lines;
  136.                           // Contains the default text attributes,
  137.                           // found on SetMapping, or default default (sic) used
  138.   wxHTMappingStructure *default_mapping_structure;
  139.  
  140.   int no_displayed_lines;
  141.   float displayed_lines[300];
  142.   int section_start_line;
  143.   int section_end_line;
  144.   int saved_block_id;  // For SaveSection and RestoreSection
  145.   int saved_y;
  146.   int saved_x;
  147.   
  148.   /* PUBLIC MEMBER FUNCTIONS */
  149.   wxHyperTextWindow(wxFrame *parent, int x, int y, int w, int h, int style);
  150.   ~wxHyperTextWindow(void);
  151.  
  152.   void OnPaint(void);
  153.   void OnEvent(wxEvent& event);
  154.  
  155.   void SetMapping(wxHyperTextMapping *mapping);
  156.  
  157.   Bool ReadIndex(char *filename, FILE *fd);
  158.   Bool LoadFile(char *filename);
  159.   Bool SaveFile(char *filename);
  160.   Bool ClearFile(void);
  161.  
  162.   /* Add a block, giving:
  163.    * - start and end coordinates in character/line positions
  164.    * - block type (arbitrary ID)
  165.    * - integer block ID
  166.    */
  167.   Bool AddBlock(int xstart, int ystart, int xend, int yend,
  168.                 int block_type, long id);
  169.  
  170.   Bool ClearBlock(long id);
  171.  
  172.   // Get blocks in this file (returns -1 for no more)
  173.   long GetFirstBlock(void);
  174.   long GetNextBlock(void);
  175.  
  176.   void SetBlockType(long block_id, int block_type);
  177.   int GetBlockType(long block_id);    // Gets block type
  178.   void GetBlockText(char *buffer, int max_size, long block_id);  // Gets plain text
  179.   void GetBlockText(char *buffer, int max_size, wxNode *node, long block_id); // Gets plain text
  180.  
  181.   // Select or deselect given block
  182.   void SelectBlock(wxTextChunk *chunk, Bool select = TRUE);
  183.   void SelectBlock(long block_id, Bool select = TRUE);
  184.  
  185.   long GetFirstSelection(void);
  186.   long GetNextSelection(void);
  187.  
  188.   inline char *GetTitle(void) { return Title; }
  189.   inline void SetTitle(char *title) { Title = copystring(title); }
  190.  
  191.   inline wxHashTable *GetLinkTable(void) { return LinkTable; }
  192.  
  193.   void SetMargins(int left, int top);
  194.  
  195.   Bool GetEditMode(void);
  196.   void SetEditMode(Bool);
  197.   inline void SetIndexWriting(Bool indexWritingMode) { indexWriting = indexWritingMode; }
  198.  
  199.   Bool Modified(void);
  200.   void DiscardEdits(void);
  201.   virtual long GenerateId(void);
  202.  
  203.   /*
  204.    * Finds x, y character position from mouse click position,
  205.    * returning block id if any.
  206.    * Returns FALSE if fails.
  207.    */
  208.   Bool FindPosition(float mouse_x, float mouse_y, 
  209.                     int *char_pos, int *line_pos,
  210.                     long *block_id);
  211.  
  212. //  void SetCurrentBlockType(int type);
  213.  
  214.   virtual void OnLeftClick(float x, float y, int char_pos, int line_pos, long block_id, int keys);
  215.   virtual void OnRightClick(float x, float y, int char_pos, int line_pos, long block_id, int keys);
  216.  
  217.   virtual void OnBeginDragLeft(float x, float y, long block_id, int keys);
  218.   virtual void OnDragLeft(Bool draw, float x, float y, long block_id, int keys);
  219.   virtual void OnEndDragLeft(float x, float y, long block_id, int keys);
  220.  
  221.   virtual void OnBeginDragRight(float x, float y, long block_id, int keys);
  222.   virtual void OnDragRight(Bool draw, float x, float y, long block_id, int keys);
  223.   virtual void OnEndDragRight(float x, float y, long block_id, int keys);
  224.  
  225.   virtual void OnSelectBlock(long block_id, Bool select);
  226.  
  227.   void DrawOutline(float x1, float y1, float x2, float y2);
  228.  
  229.   // If there are more than zero sections, always display at top of section
  230.   // containing this block (for the time being).
  231.   // Search back for a section start.
  232.   void DisplayFile(void);
  233.   void DisplayFileAt(long block_id, Bool refresh = TRUE);
  234.   void DisplayFileAtTop(void);
  235.  
  236.   void DisplayNextSection(void);
  237.   void DisplayPreviousSection(void);
  238.   int  GetCurrentSectionNumber(void);
  239.  
  240.   // Save line number at start of section
  241.   // to be restored after a Compile()
  242.   void SaveSection(void);
  243.   void RestoreSection(void);
  244.  
  245.   // Traverses chunks assigning fonts/colours using a stack
  246.   Bool Compile(void);
  247.  
  248.   // Find first chunk at given line (returns a node so can traverse
  249.   // forward from here)
  250.   wxNode *FindChunkAtLine(int line_no);
  251.  
  252.   // Finds first line-start chunk containing the given block
  253.   // (NOT the actual chunk containing the block)
  254.   wxNode *FindChunkAtBlock(long block_id);
  255.  
  256.   // Finds text chunk at start of given block
  257.   wxTextChunk *FindBlock(long block_id);
  258.   long FindBlockForSection(wxNode *node);
  259.  
  260. /*
  261.  * PRIVATE MEMBERS
  262.  *
  263.  */
  264.   void ParseLine(char *line);
  265. };
  266.  
  267. // Types of chunk
  268.  
  269. // Start a hypertext block
  270. #define CHUNK_START_BLOCK 1
  271.  
  272. // Start an unrecognized (maybe Latex) block
  273. #define CHUNK_START_UNRECOGNIZED_BLOCK 2
  274.  
  275. // End a hypertext or Latex block
  276. #define CHUNK_END_BLOCK   3
  277.  
  278. // Start a line
  279. #define CHUNK_START_LINE  4
  280.  
  281. // A chunk is not a block, since it may signify the start of a
  282. // line as well as a block boundary.
  283. class wxTextChunk: public wxObject
  284. {
  285.  public:
  286.   int chunk_type;
  287.   int line_no;      // -1 for no line start
  288.   int block_type;   // -1 for no block
  289.   long block_id;    // -1 for no block
  290.   long end_id;      // If chunk ends a block, this is the id of
  291.                     // the block which has ended.
  292.                     // block_id remains the id for the block currently
  293.                     // in scope.
  294.   Bool selected;
  295.   char *text;
  296.   wxFont *font;     // Always present
  297.   wxColour *colour; // Always present
  298.   wxColour *background_colour; // Internal use only
  299.   int logical_op;             // Internal use only
  300.   Bool visibility;
  301.   int special_attribute; // E.g. new section
  302.  
  303.   wxTextChunk(int the_chunk_type, int the_line_no, char *the_text,
  304.               wxFont *the_font, wxColour *the_colour,
  305.               int the_block_type, long the_block_id, int the_attribute, Bool the_vis);
  306.   ~wxTextChunk(void);
  307. };
  308.  
  309. class HypertextItem: public wxObject
  310. {
  311.  public:
  312.   char *filename;
  313.   long block_id;
  314.   HypertextItem(char *new_filename, long new_block_id);
  315.   ~HypertextItem(void);
  316. };
  317.  
  318. // Read an integer from line, starting at next
  319. int wxReadInteger(char *line, int next, long *value);
  320.  
  321. // Find or create a font
  322. wxFont *wxFindOrCreateFont(int PointSize, int Family, int Style, int Weight);
  323.  
  324. #ifndef KEY_SHIFT
  325. #define KEY_SHIFT 1
  326. #define KEY_CTRL  2
  327. #endif
  328.  
  329. #define BLOCK_TYPE_SELECTION 9999
  330.  
  331. #endif // hypertexth
  332.