1 /*
2 This file is part of "Avanor, the Land of Mystery" roguelike game
3 Home page: http://www.avanor.com/
4 Copyright (C) 2000-2003 Vadim Gaidukevich
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #ifndef __XGUI_H
22 #define __XGUI_H
23 
24 #include <stdio.h>
25 #include "xstring.h"
26 #include "global.h"
27 #include "xvector.h"
28 
29 class XGuiItem
30 {
31 public:
XGuiItem()32    XGuiItem() : next(0), prev(0) {}
~XGuiItem()33    virtual ~XGuiItem() {}
34    XGuiItem * next;
35    XGuiItem * prev;
36    virtual int isSelectable() = 0;
37    virtual int isTitle() = 0;
38 
SetWidth(int new_width)39    virtual bool SetWidth(int new_width) { return false; }
GetHeight()40    virtual int GetHeight() { return 0; }
41    virtual const char * operator[](int index) { return 0; }
42 };
43 
44 class  XGuiItem_SimpleSelect : public XGuiItem
45 {
46 	char buf[256];
47 public:
XGuiItem_SimpleSelect(char * text)48 	XGuiItem_SimpleSelect(char * text) : XGuiItem() { 	strcpy(buf, text); }
isSelectable()49 	virtual int isSelectable() {return 1;}
isTitle()50 	virtual int isTitle() {return 0;}
SetWidth(int new_width)51 	virtual bool SetWidth(int new_width) { return true; }
GetHeight()52 	virtual int GetHeight() { return 1; }
53 	virtual const char * operator[](int index) { return buf; }
54 };
55 
56 
57 class XGuiItem_Text : public XGuiItem
58 {
59    char                  * text;
60    XSimpleVector<char *>   lines;
61    int                     lines_count;
62    int                     width;
63    int                     select_flag;
64    void WideBuffer(char * buf, int new_width);
65 
66 public:
XGuiItem_Text()67    XGuiItem_Text() : XGuiItem(), select_flag(0), text(0)
68    {
69       lines_count = 0;
70       SetText("");
71    }
72 
XGuiItem()73    XGuiItem_Text(const char * _text, int sf = 0) : XGuiItem(), select_flag(sf), text(0)
74    {
75       lines_count = 0;
76       if(*_text == 0)
77          SetText("\n");
78       else
79          SetText(_text);
80    }
81 
SetText(const char * _text)82    void SetText(const char * _text)
83    {
84       while(lines_count > 0) delete [] lines[--lines_count];
85       if(text != 0) delete [] text;
86       int textsize = x_strsize(_text);
87       text = new char[textsize + 1];
88       x_strcpy(text, _text);
89       SetWidth(x_strlen(text));
90    }
91 
~XGuiItem_Text()92    virtual ~XGuiItem_Text()
93    {
94       while(lines_count > 0) delete [] lines[--lines_count];
95       if(text != 0) delete[] text;
96    }
97 
isSelectable()98    virtual int isSelectable() { return select_flag; }
isTitle()99    virtual int isTitle() { return 0; }
100 
101    virtual bool SetWidth(int new_width);
GetHeight()102    virtual int GetHeight() { return lines_count; }
103    virtual const char * operator[](int index) { return lines[index]; }
104 };
105 
106 #define XGUI_LIST_HEADER 3
107 #define XGUI_LIST_FOOTER 3
108 
109 class XGuiList
110 {
111 	XGuiItem * head;
112 	XGuiItem * tail;
113 
114 	XGuiItem * top_item;
115 	int        top_item_first_line;
116 	int        top_item_lines_count;
117 	int        top_line;
118 	int        top_item_index;
119 	int		   top_selectable_index;
120 	int		   selectable_items_count;
121 
122 	int items_count;
123 	int lines_count;
124 	int width;
125 
126 	int list_height;
127 	int list_width;
128 
129 	void SetFirstVisible();
130 	int CalculateVisibleCount();
131 
132 	const char * caption;
133 	const char * footer;
134 
135 	int last_pressed_key;
136 
137 
138 public:
XGuiList()139 	XGuiList() :
140 	  head(NULL), tail(NULL), items_count(0), lines_count(0), top_selectable_index(0), selectable_items_count(0),
141 		 caption(NULL), footer(NULL)
142 	{
143 		list_height = size_y - 5;
144 		list_width  = size_x - 6;
145 	}
146 
~XGuiList()147 	virtual ~XGuiList()
148 	{
149 		while (head != 0)
150 		{
151   			XGuiItem * tmp = head;
152   			head = head->next;
153   			delete tmp;
154 		}
155 	}
156 
AddItemHead(XGuiItem * item)157 	void AddItemHead(XGuiItem * item)
158 	{
159 		if(head != 0)
160 		{
161 			item->next = head;
162 			head->prev = item;
163 			head = item;
164 		}
165 		else
166 		{
167 			head = item;
168 			tail = item;
169 		}
170 		top_item = item;
171 		top_item_first_line = 0;
172 		top_item_lines_count = item->GetHeight();
173 		top_line = 0;
174       top_item_index = 0;
175 	}
176 
AddItemTail(XGuiItem * item)177 	void AddItemTail(XGuiItem * item)
178 	{
179  		if(tail != 0)
180  		{
181  			tail->next = item;
182  			item->prev = tail;
183  			tail = item;
184  		}
185  		else
186  		{
187  			head = item;
188  			tail = item;
189    			top_item = item;
190    			top_item_first_line = 0;
191 			top_item_lines_count = item->GetHeight();
192 	   		top_line = 0;
193 		    top_item_index = 0;
194  		}
195 	}
196 
197 	void AddItem(XGuiItem * item, int is_first = 0)
198 	{
199 		if(is_first)
200 			AddItemHead(item);
201 		else
202 			AddItemTail(item);
203 
204 		items_count++;
205 		lines_count += item->GetHeight();
206 	}
207 
208 	void AddHtmlText(char * text);
209 
210 	void Put(FILE * f = NULL);
211 	int Run(int flag = 0, int flag2 = 0);
212 
213 	void LineUp(int count = 1);
214 	void LineDown(int count = 1);
215 	void PageUp();
216 	void PageDown();
SetCaption(const char * _caption)217 	void SetCaption(const char * _caption){	caption = _caption;	}
SetFooter(const char * _footer)218 	void SetFooter(const char * _footer) { footer = _footer; }
GetHeight()219 	virtual int GetHeight() { return lines_count; }
GetTopItemIndex()220 	int GetTopItemIndex() { return top_item_index; }
221 
GetLastKey()222 	int GetLastKey() { return last_pressed_key; }
223 };
224 
225 #endif
226