1 /***************************************************************************
2          scrollinglabel.h  -  Scrollable multiline hypertext widget
3                              -------------------
4     begin                : Thu Aug 28 2003
5     copyright            : (C) 2003 by Gabor Torok
6     email                : cctorok@yahoo.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef SCROLLING_LABEL_H
19 #define SCROLLING_LABEL_H
20 #pragma once
21 
22 #include "gui.h"
23 #include "widget.h"
24 #include "button.h"
25 #include "window.h"
26 #include "draganddrop.h"
27 #include <vector>
28 
29 /**
30   *@author Gabor Torok
31   */
32 
33 /// Looks whether a hyperlink is in view or clicked on.
34 class WordClickedHandler {
35 public:
WordClickedHandler()36 	WordClickedHandler() {
37 	}
38 
~WordClickedHandler()39 	virtual ~WordClickedHandler() {
40 	}
41 
42 	virtual void wordClicked( std::string const& word ) = 0;
43 	virtual void showingWord( char *word ) = 0;
44 };
45 
46 /// A text box widget with clickable hyperlinks.
47 class ScrollingLabel : public Widget {
48 protected:
49 	static const int TEXT_SIZE = 3000;
50 	char text[ TEXT_SIZE ];
51 	int lineWidth;
52 	std::vector<std::string> lines;
53 
54 	//  int count;
55 	//  const char **list;
56 	//  const Color *colors;
57 	//  const GLuint *icons;
58 	int value;
59 	int scrollerWidth, scrollerHeight;
60 	int listHeight;
61 	bool willSetScrollerHeight, willScrollToBottom;
62 	float alpha, alphaInc;
63 	GLint lastTick;
64 	bool inside;
65 	int scrollerY;
66 	bool dragging;
67 	int dragX, dragY;
68 	// never assigned a value: int selectedLine;
69 	//  DragAndDropHandler *dragAndDropHandler;
70 	bool innerDrag;
71 	int innerDragX, innerDragY;
72 	//  bool highlightBorders;
73 	// GLuint highlight;
74 	bool canGetFocusVar;
75 
76 	std::map<char, Color> coloring;
77 
78 	struct WordPos {
79 		int x, y, w, h;
80 		char word[255];
81 	};
82 	WordPos wordPos[1000];
83 	int wordPosCount;
84 	WordClickedHandler *handler;
85 
86 	bool interactive;
87 
88 public:
89 
90 	bool debug;
91 
92 	ScrollingLabel( int x, int y, int w, int h, char *text );
93 	virtual ~ScrollingLabel();
94 
setInteractive(bool interactive)95 	inline void setInteractive( bool interactive ) {
96 		this->interactive = interactive;
97 	}
98 
setWordClickedHandler(WordClickedHandler * handler)99 	inline void setWordClickedHandler( WordClickedHandler *handler ) {
100 		this->handler = handler;
101 	}
102 
addColoring(char c,Color color)103 	inline void addColoring( char c, Color color ) {
104 		coloring[c] = color;
105 	}
106 
getText()107 	inline char *getText() {
108 		return text;
109 	}
110 	void setText( char const* s );
111 
112 	/**
113 	 * Append text by scrolling off the top if it won't fit in the buffer.
114 	 */
115 	void appendText( const char *s );
116 
117 	//  inline int getLineCount() { return count; }
118 	//  void setLines(int count, const char *s[], const Color *colors=NULL, const GLuint *icon=NULL);
119 	//  inline const char *getLine(int index) { return list[index]; }
120 
121 	//  inline int getSelectedLine() { return selectedLine; }
122 	//  void setSelectedLine(int n);
123 
124 	void drawWidget( Widget *parent );
125 
126 	/**
127 	Return true, if the event activated this widget. (For example, button push, etc.)
128 	Another way to think about it is that if true, the widget fires an "activated" event
129 	to the outside world.
130 	 */
131 	bool handleEvent( Widget *parent, SDL_Event *event, int x, int y );
132 
133 	void removeEffects( Widget *parent );
134 
135 	// don't play sound when the value changes
hasSound()136 	virtual inline bool hasSound() {
137 		return false;
138 	}
139 
canGetFocus()140 	inline bool canGetFocus() {
141 		return canGetFocusVar;
142 	}
setCanGetFocus(bool b)143 	inline void setCanGetFocus( bool b ) {
144 		this->canGetFocusVar = b;
145 	}
146 
147 private:
148 	char *printLine( Widget *parent, int x, int y, char *s );
149 	int getWordPos( int x, int y );
150 	//  void selectLine(int x, int y);
151 	//  void drawIcon( int x, int y, GLuint icon );
152 	void moveSelectionUp();
153 	void moveSelectionDown();
154 };
155 
156 #endif
157 
158