1 /* HexChat
2  * Copyright (C) 1998-2010 Peter Zelezny.
3  * Copyright (C) 2009-2013 Berke Viktor.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19 
20 #ifndef HEXCHAT_XTEXT_H
21 #define HEXCHAT_XTEXT_H
22 
23 #include <gtk/gtk.h>
24 
25 #define GTK_TYPE_XTEXT              (gtk_xtext_get_type ())
26 #define GTK_XTEXT(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_XTEXT, GtkXText))
27 #define GTK_XTEXT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_XTEXT, GtkXTextClass))
28 #define GTK_IS_XTEXT(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_XTEXT))
29 #define GTK_IS_XTEXT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_XTEXT))
30 #define GTK_XTEXT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_XTEXT, GtkXTextClass))
31 
32 #define ATTR_BOLD				'\002'
33 #define ATTR_COLOR			'\003'
34 #define ATTR_BLINK			'\006'
35 #define ATTR_BEEP				'\007'
36 #define ATTR_HIDDEN			'\010'
37 #define ATTR_ITALICS2		'\011'
38 #define ATTR_RESET			'\017'
39 #define ATTR_REVERSE			'\026'
40 #define ATTR_ITALICS			'\035'
41 #define ATTR_STRIKETHROUGH	'\036'
42 #define ATTR_UNDERLINE		'\037'
43 
44 /* these match palette.h */
45 #define XTEXT_MIRC_COLS 32
46 #define XTEXT_COLS 37		/* 32 plus 5 for extra stuff below */
47 #define XTEXT_MARK_FG 32	/* for marking text */
48 #define XTEXT_MARK_BG 33
49 #define XTEXT_FG 34
50 #define XTEXT_BG 35
51 #define XTEXT_MARKER 36		/* for marker line */
52 #define XTEXT_MAX_COLOR 41
53 
54 typedef struct _GtkXText GtkXText;
55 typedef struct _GtkXTextClass GtkXTextClass;
56 typedef struct textentry textentry;
57 
58 /*
59  * offsets_t is used for retaining search information.
60  * It is stored in the 'data' member of a GList,
61  * as chained from ent->marks.  It saves starting and
62  * ending+1 offset of a found occurrence.
63  */
64 typedef union offsets_u {
65 	struct offsets_s {
66 		guint16	start;
67 		guint16	end;
68 	} o;
69 	guint32 u;
70 } offsets_t;
71 
72 typedef enum marker_reset_reason_e {
73 	MARKER_WAS_NEVER_SET,
74 	MARKER_IS_SET,
75 	MARKER_RESET_MANUALLY,
76 	MARKER_RESET_BY_KILL,
77 	MARKER_RESET_BY_CLEAR
78 } marker_reset_reason;
79 
80 typedef struct {
81 	GtkXText *xtext;					/* attached to this widget */
82 
83 	gfloat old_value;					/* last known adj->value */
84 	textentry *text_first;
85 	textentry *text_last;
86 
87 	textentry *last_ent_start;	  /* this basically describes the last rendered */
88 	textentry *last_ent_end;	  /* selection. */
89 	int last_offset_start;
90 	int last_offset_end;
91 
92 	int last_pixel_pos;
93 
94 	int pagetop_line;
95 	int pagetop_subline;
96 	textentry *pagetop_ent;			/* what's at xtext->adj->value */
97 
98 	int num_lines;
99 	int indent;						  /* position of separator (pixels) from left */
100 
101 	textentry *marker_pos;
102 	marker_reset_reason marker_state;
103 
104 	int window_width;				/* window size when last rendered. */
105 	int window_height;
106 
107 	unsigned int time_stamp:1;
108 	unsigned int scrollbar_down:1;
109 	unsigned int needs_recalc:1;
110 	unsigned int marker_seen:1;
111 
112 	GList *search_found;		/* list of textentries where search found strings */
113 	gchar *search_text;		/* desired text to search for */
114 	gchar *search_nee;		/* prepared needle to look in haystack for */
115 	gint search_lnee;		/* its length */
116 	gtk_xtext_search_flags search_flags;	/* match, bwd, highlight */
117 	GList *cursearch;			/* GList whose 'data' pts to current textentry */
118 	GList *curmark;			/* current item in ent->marks */
119 	offsets_t curdata;		/* current offset info, from *curmark */
120 	GRegex *search_re;		/* Compiled regular expression */
121 	textentry *hintsearch;	/* textentry found for last search */
122 } xtext_buffer;
123 
124 struct _GtkXText
125 {
126 	GtkWidget widget;
127 
128 	xtext_buffer *buffer;
129 	xtext_buffer *orig_buffer;
130 	xtext_buffer *selection_buffer;
131 
132 	GtkAdjustment *adj;
133 	GdkPixmap *pixmap;				/* 0 = use palette[19] */
134 	GdkDrawable *draw_buf;			/* points to ->window */
135 	GdkCursor *hand_cursor;
136 	GdkCursor *resize_cursor;
137 
138 	int pixel_offset;					/* amount of pixels the top line is chopped by */
139 
140 	int last_win_x;
141 	int last_win_y;
142 	int last_win_h;
143 	int last_win_w;
144 
145 	GdkGC *bgc;						  /* backing pixmap */
146 	GdkGC *fgc;						  /* text foreground color */
147 	GdkGC *light_gc;				  /* sep bar */
148 	GdkGC *dark_gc;
149 	GdkGC *thin_gc;
150 	GdkGC *marker_gc;
151 	GdkColor palette[XTEXT_COLS];
152 
153 	gint io_tag;					  /* for delayed refresh events */
154 	gint add_io_tag;				  /* "" when adding new text */
155 	gint scroll_tag;				  /* marking-scroll timeout */
156 	gulong vc_signal_tag;        /* signal handler for "value_changed" adj */
157 
158 	int select_start_adj;		  /* the adj->value when the selection started */
159 	int select_start_x;
160 	int select_start_y;
161 	int select_end_x;
162 	int select_end_y;
163 
164 	int max_lines;
165 
166 	int col_fore;
167 	int col_back;
168 
169 	int depth;						  /* gdk window depth */
170 
171 	char num[8];					  /* for parsing mirc color */
172 	int nc;							  /* offset into xtext->num */
173 
174 	textentry *hilight_ent;
175 	int hilight_start;
176 	int hilight_end;
177 
178 	guint16 fontwidth[128];	  /* each char's width, only the ASCII ones */
179 
180 	struct pangofont
181 	{
182 		PangoFontDescription *font;
183 		int ascent;
184 		int descent;
185 	} *font, pango_font;
186 	PangoLayout *layout;
187 
188 	int fontsize;
189 	int space_width;				  /* width (pixels) of the space " " character */
190 	int stamp_width;				  /* width of "[88:88:88]" */
191 	int max_auto_indent;
192 
193 	unsigned char scratch_buffer[4096];
194 
195 	int (*urlcheck_function) (GtkWidget * xtext, char *word);
196 
197 	int jump_out_offset;	/* point at which to stop rendering */
198 	int jump_in_offset;	/* "" start rendering */
199 
200 	int ts_x;			/* ts origin for ->bgc GC */
201 	int ts_y;
202 
203 	int clip_x;			/* clipping (x directions) */
204 	int clip_x2;		/* from x to x2 */
205 
206 	int clip_y;			/* clipping (y directions) */
207 	int clip_y2;		/* from y to y2 */
208 
209 	/* current text states */
210 	unsigned int underline:1;
211 	unsigned int strikethrough:1;
212 	unsigned int hidden:1;
213 
214 	/* text parsing states */
215 	unsigned int parsing_backcolor:1;
216 	unsigned int parsing_color:1;
217 	unsigned int backcolor:1;
218 
219 	/* various state information */
220 	unsigned int moving_separator:1;
221 	unsigned int word_select:1;
222 	unsigned int line_select:1;
223 	unsigned int button_down:1;
224 	unsigned int dont_render:1;
225 	unsigned int dont_render2:1;
226 	unsigned int cursor_hand:1;
227 	unsigned int cursor_resize:1;
228 	unsigned int skip_border_fills:1;
229 	unsigned int skip_stamp:1;
230 	unsigned int mark_stamp:1;	/* Cut&Paste with stamps? */
231 	unsigned int force_stamp:1;	/* force redrawing it */
232 	unsigned int render_hilights_only:1;
233 	unsigned int in_hilight:1;
234 	unsigned int un_hilight:1;
235 	unsigned int recycle:1;
236 	unsigned int force_render:1;
237 	unsigned int color_paste:1; /* CTRL was pressed when selection finished */
238 
239 	/* settings/prefs */
240 	unsigned int auto_indent:1;
241 	unsigned int thinline:1;
242 	unsigned int marker:1;
243 	unsigned int separator:1;
244 	unsigned int wordwrap:1;
245 	unsigned int ignore_hidden:1;	/* rawlog uses this */
246 };
247 
248 struct _GtkXTextClass
249 {
250 	GtkWidgetClass parent_class;
251 	void (*word_click) (GtkXText * xtext, char *word, GdkEventButton * event);
252 	void (*set_scroll_adjustments) (GtkXText *xtext, GtkAdjustment *hadj, GtkAdjustment *vadj);
253 };
254 
255 GtkWidget *gtk_xtext_new (GdkColor palette[], int separator);
256 void gtk_xtext_append (xtext_buffer *buf, unsigned char *text, int len, time_t stamp);
257 void gtk_xtext_append_indent (xtext_buffer *buf,
258 										unsigned char *left_text, int left_len,
259 										unsigned char *right_text, int right_len,
260 										time_t stamp);
261 int gtk_xtext_set_font (GtkXText *xtext, char *name);
262 void gtk_xtext_set_background (GtkXText * xtext, GdkPixmap * pixmap);
263 void gtk_xtext_set_palette (GtkXText * xtext, GdkColor palette[]);
264 void gtk_xtext_clear (xtext_buffer *buf, int lines);
265 void gtk_xtext_save (GtkXText * xtext, int fh);
266 void gtk_xtext_refresh (GtkXText * xtext);
267 int gtk_xtext_lastlog (xtext_buffer *out, xtext_buffer *search_area);
268 textentry *gtk_xtext_search (GtkXText * xtext, const gchar *text, gtk_xtext_search_flags flags, GError **err);
269 void gtk_xtext_reset_marker_pos (GtkXText *xtext);
270 int gtk_xtext_moveto_marker_pos (GtkXText *xtext);
271 void gtk_xtext_check_marker_visibility(GtkXText *xtext);
272 void gtk_xtext_set_marker_last (session *sess);
273 
274 gboolean gtk_xtext_is_empty (xtext_buffer *buf);
275 typedef void (*GtkXTextForeach) (GtkXText *xtext, unsigned char *text, void *data);
276 void gtk_xtext_foreach (xtext_buffer *buf, GtkXTextForeach func, void *data);
277 
278 void gtk_xtext_set_error_function (GtkXText *xtext, void (*error_function) (int));
279 void gtk_xtext_set_indent (GtkXText *xtext, gboolean indent);
280 void gtk_xtext_set_max_indent (GtkXText *xtext, int max_auto_indent);
281 void gtk_xtext_set_max_lines (GtkXText *xtext, int max_lines);
282 void gtk_xtext_set_show_marker (GtkXText *xtext, gboolean show_marker);
283 void gtk_xtext_set_show_separator (GtkXText *xtext, gboolean show_separator);
284 void gtk_xtext_set_thin_separator (GtkXText *xtext, gboolean thin_separator);
285 void gtk_xtext_set_time_stamp (xtext_buffer *buf, gboolean timestamp);
286 void gtk_xtext_set_urlcheck_function (GtkXText *xtext, int (*urlcheck_function) (GtkWidget *, char *));
287 void gtk_xtext_set_wordwrap (GtkXText *xtext, gboolean word_wrap);
288 
289 xtext_buffer *gtk_xtext_buffer_new (GtkXText *xtext);
290 void gtk_xtext_buffer_free (xtext_buffer *buf);
291 void gtk_xtext_buffer_show (GtkXText *xtext, xtext_buffer *buf, int render);
292 void gtk_xtext_copy_selection (GtkXText *xtext);
293 GType gtk_xtext_get_type (void);
294 
295 #endif
296