1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "GUI.h"
7 #include "GUI_widgets.h"
8 #include "GUI_output.h"
9 
10 
11 /* Variables that we'll need to clean up */
12 GUI *gui;
13 GUI_TermWin *terminal;
14 enum image_names {
15 	IMAGE_HELLO,
16 	IMAGE_HELLO2,
17 	IMAGE_SCROLL_UP,
18 	IMAGE_SCROLL_DN,
19 	NUM_IMAGES
20 };
21 char *image_files[NUM_IMAGES] = {
22 	"hello.bmp", "hello2.bmp", "scroll_up.bmp", "scroll_dn.bmp"
23 };
24 SDL_Surface *images[NUM_IMAGES];
25 
26 
ShowChar(SDLKey key,Uint16 unicode)27 void ShowChar(SDLKey key, Uint16 unicode)
28 {
29 	if ( unicode && (unicode <= 255) ) {
30 		Uint8 ch;
31 
32 		ch = (Uint8)unicode;
33 		terminal->AddText((char *)&ch, 1);
34 	}
35 }
36 
cleanup(void)37 void cleanup(void)
38 {
39 	int i;
40 
41 	/* Delete the GUI */
42 	delete gui;
43 
44 	/* Clean up any images we have */
45 	for ( i=0; i<NUM_IMAGES; ++i ) {
46 		if ( images[i] ) {
47 			SDL_FreeSurface(images[i]);
48 		}
49 	}
50 }
51 
Output(SDL_Surface * screen,const char * title,const char * text)52 void Output(SDL_Surface *screen, const char *title, const char *text)
53 {
54 	GUI_Output *output;
55 
56 	output = GUI_CreateOutput(screen, 60, 5, NULL);
57 	if ( output ) {
58 		unsigned int i, pos;
59 		char formatted_text[1024];
60 #if 1
61 		/* Center the text in our window */
62 		pos = 0;
63 		formatted_text[pos++] = '\n';
64 		formatted_text[pos++] = '\n';
65 		for ( i=0; i<((60-strlen(text))/2); ++i ) {
66 			formatted_text[pos++] = ' ';
67 		}
68 		formatted_text[pos] = '\0';
69 		strcat(formatted_text, text);
70 #else
71 		strcpy(formatted_text, text);
72 #endif
73 
74 		/* Run the output window with our text */
75 		GUI_AddOutput(output, formatted_text);
76 		GUI_ShowOutput(output, 1);
77 		GUI_DeleteOutput(output);
78 	}
79 }
80 
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 	SDL_Surface *screen;
84 	int x, y, i;
85 	int error;
86 	GUI_Widget *widget;
87 	SDL_Rect null_rect = { 0, 0, 0, 0 };
88 	GUI_Menu *menu;
89 
90 	/* Initialize SDL */
91 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
92 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
93 		exit(1);
94 	}
95 	atexit(SDL_Quit);
96 
97 	/* Get a video mode for display */
98 	screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
99 	if ( screen == NULL ) {
100 		fprintf(stderr, "Couldn't set video mode: %s\n",SDL_GetError());
101 		exit(1);
102 	}
103 	SDL_WM_SetCaption("GUI Hello!", "hello");
104 
105 	/* Create a GUI container */
106 	gui = new GUI(screen);
107 
108 	/* Load our images */
109 	for ( i=0; i<NUM_IMAGES; ++i ) {
110 		images[i] = NULL;
111 	}
112 	error = 0;
113 	for ( i=0; i<NUM_IMAGES; ++i ) {
114 		images[i] = SDL_LoadBMP(image_files[i]);
115 		if ( images[i] == NULL ) {
116 			fprintf(stderr, "Couldn't load '%s': %s\n",
117 					image_files[i], SDL_GetError());
118 			++error;
119 		}
120 	}
121 	if ( error ) {
122 		cleanup();
123 		exit(1);
124 	}
125 
126 	/* Add our interface widgets:
127 	   We want a gray background and a centered image button.
128 	 */
129 	x = (screen->w-images[IMAGE_HELLO]->w)/2;
130 	y = (screen->h-images[IMAGE_HELLO]->h)/2;
131 	widget = new GUI_Button(NULL, x, y, images[IMAGE_HELLO],images[IMAGE_HELLO2]);
132 	gui->AddWidget(widget);
133 
134 	/* We also want a small text window with scroll buttons */
135 	x = images[IMAGE_SCROLL_UP]->w;
136 	terminal = new GUI_TermWin(x, 18, screen->w-x, 32, NULL, ShowChar, 32);
137 	terminal->AddText("Keystrokes will go here: ");
138 	gui->AddWidget(terminal);
139 	y = images[IMAGE_SCROLL_UP]->h;
140 	widget = new GUI_ScrollButtons(0,18, images[IMAGE_SCROLL_UP], null_rect,
141 					0, y+18, images[IMAGE_SCROLL_DN],
142 					SCROLLBAR_VERTICAL, terminal);
143 	gui->AddWidget(widget);
144 
145 	/* Add a small menu which has no sense */
146 	menu=new GUI_Menu(gui,screen->w,NULL);
147 	gui->AddWidget(menu);
148 	menu->AddSubmenu(1,"File");
149 	menu->AddMenuitem(1,11,"Don't quit...",NULL);
150 
151 	/* Run the GUI, and then clean up when it's done. */
152 	gui->Run(NULL);
153 	Output(screen,"-= Thanks =-","Thanks for trying the C++ GUI interface");
154 	cleanup();
155 	exit(0);
156 
157 	/* To make the compiler happy */
158 	return(0);
159 }
160