12 unsigned long rgb(
unsigned long red,
unsigned long green,
unsigned long blue) {
13 return red<<16 | green<<8 | blue;
38 static void defaultAction() {}
40 static void defaultTaggedAction(
char []) {}
44 void (*taggedAction)(
char text[]);
46 Widget(
int x,
int y,
int width,
int height,
const char text[])
47 : x(x), y(y), width(width), height(height), focused(
false), hidden(
true) {
48 strcpy(this->text,text);
49 action = &defaultAction;
50 taggedAction = &defaultTaggedAction;
53 void updateGraphVariables(Display* display,
int screen,Window window,GC gc) {
54 this->display = display;
55 this->screen = screen;
56 this->window = window;
66 virtual bool triggerEvent(XEvent&) {
70 bool mouseInArea(XEvent& event) {
71 int mx =
event.xbutton.x;
72 int my =
event.xbutton.y;
73 if (mx > x && mx < x+width && my > y && my < y+height) {
79 virtual void setFocused(
bool) {
80 this->focused =
false;
87 void setText(
const char text[]) {
88 strcpy(this->text,text);
108 Label(
int x,
int y,
int width,
int height=20,
const char text[]=
"")
109 :
Widget(x,y,width,height,text){
114 XClearArea(display,window,x+1,y+1,width-1,height-1,
false);
115 unsigned int columns = (width-8)/6;
116 char* visibletext =
new char[columns+1];
117 strncpy(visibletext,text,columns);
118 visibletext[columns]=0;
119 XDrawString(display,window,gc,x+5,y+height/2+5,visibletext,strlen(visibletext));
135 TextField(
int x,
int y,
int width,
int height=20)
136 :
Widget(x,y,width,height,
""), editing(
false), tilde(
false){
141 XDrawRectangle(display,window,gc,x,y,width,height);
142 XClearArea(display,window,x+1,y+1,width-1,height-1,
false);
143 XDrawString(display,window,gc,x+5,y+height/2+5,text,strlen(text));
145 XDrawLine(display,window,gc,x+5+strlen(text)*6,y+height/2-6,x+5+strlen(text)*6,y+height/2+6);
149 bool triggerEvent(XEvent& event) {
150 switch (event.type) {
152 if (mouseInArea(event))
return true;
155 if (!editing)
return false;
158 XComposeStatus compose;
159 XLookupString(&event.xkey,buffer,10,&keysym,&compose);
165 (*taggedAction)(text);
170 if (strlen(text)>0) {
171 text[strlen(text)-1]=0;
175 unsigned int columns = (width-10)/6;
176 if (strlen(text) >= columns)
break;
183 if (keysym>65000)
break;
187 case XK_a: keysym = XK_aacute;
break;
188 case XK_e: keysym = XK_eacute;
break;
189 case XK_i: keysym = XK_iacute;
break;
190 case XK_o: keysym = XK_oacute;
break;
191 case XK_u: keysym = XK_uacute;
break;
196 int pos = strlen(text);
207 void setFocused(
bool focused) {
208 if (focused && !this->focused) {
212 else if (!focused && this->focused) {
216 this->focused = focused;
232 Button(
int x,
int y,
int width,
int height=20,
const char text[]=
"")
233 :
Widget(x,y,width,height,text), active(
false) {
238 XDrawRectangle(display,window,gc,x,y,width,height);
241 XSetForeground(display,gc,rgb(200,200,200));
242 XFillRectangle(display,window,gc,x+1,y+1,width-1,height-1);
243 XSetForeground(display,gc,rgb(0,0,0));
246 XClearArea(display,window,x+1,y+1,width-1,height-1,
false);
250 int twidth = strlen(text)*7;
252 XDrawString(display,window,gc,cx-twidth/2,cy+theight/2,text,strlen(text));
255 bool triggerEvent(XEvent& event) {
256 switch (event.type) {
258 if (mouseInArea(event)) {
264 if (active && mouseInArea(event)) {
268 (*taggedAction)(text);
273 if (active && !mouseInArea(event)) {
294 std::vector<char*> items;
296 timeval previewsclicktime;
299 static bool less(
char* item1,
char* item2) {
300 return (strcmp(item1,item2)<0);
304 static void defaultSelectionChanged(
const char[]) {}
305 static void defaultDoubleClicked(
const char[]){}
308 void (*selectionChanged)(
const char[]);
309 void (*doubleClicked)(
const char[]);
311 List(
int x,
int y,
int width=100,
int height=100)
312 :
Widget(x,y,width,height,
"") {
315 selectionChanged = &defaultSelectionChanged;
316 doubleClicked = &defaultDoubleClicked;
317 gettimeofday(&previewsclicktime,NULL);
326 XDrawRectangle(display,window,gc,x,y,width,height);
327 XClearArea(display,window,x+1,y+1,width-1,height-1,
false);
329 unsigned int rows = height/15;
330 unsigned int columns = (width-8)/6;
333 if (current>=0 && items.size()>0) {
334 XSetForeground(display,gc,rgb(150,150,255));
335 XFillRectangle(display,window,gc,x+1,y+1+current*15,width-1,15);
336 XSetForeground(display,gc,rgb(0,0,0));
337 strcpy(text,items.at(current));
339 for (
unsigned int i=first,r=0; i<items.size() && r<rows; i++) {
340 char* item =
new char[columns+1];
341 strncpy(item,items[i],columns);
343 XDrawString(display,window,gc,x+5,y+12+r*15,item,strlen(item));
349 void add(
const char item[]) {
350 char* buffer =
new char[strlen(item)+1];
352 items.push_back(buffer);
356 bool triggerEvent(XEvent& event) {
357 switch (event.type) {
359 switch (event.xbutton.button) {
361 if (mouseInArea(event)) {
362 unsigned int position = (
event.xbutton.y-y)/15;
363 unsigned int rows = height/15;
364 if (position<items.size()-first && position<rows) {
365 bool sameitem =
false;
366 if ((
unsigned int)current==position) {
370 (*selectionChanged)(items.at(current+first));
372 struct timeval currentclicktime;
373 gettimeofday(¤tclicktime,NULL);
376 currentclicktime.tv_sec%10000*1000000+currentclicktime.tv_usec -
377 previewsclicktime.tv_sec%10000*1000000+previewsclicktime.tv_usec;
379 (*doubleClicked)(items.at(current+first));
382 previewsclicktime = currentclicktime;
389 if (mouseInArea(event))
if (first>0) {
390 int rows = height/15;
392 if (current>=0 && current<rows-1) {
399 if (mouseInArea(event))
if (first<(
int)items.size()-1){
413 int getCurrentIndex() {
425 items.erase(items.begin()+i);
432 for (
unsigned int i=0; i<items.size(); i++) {
442 std::sort(items.begin(), items.end(), less );
455 static const int MAX = 500;
462 Frame(
int x=100,
int y=100,
int width=600,
int height=400,
const char title[]=
"")
463 :
Widget(x,y,width,height,title), active(
true) {
464 strcpy(this->text,title);
465 display = XOpenDisplay(NULL);
466 screen = DefaultScreen(display);
467 window = XCreateSimpleWindow(display,RootWindow(display,screen),
468 x,y,width,height,1,0,255<<16|255<<8|255);
469 gc = XDefaultGC(display,screen);
470 XMapWindow(display,window);
471 XSelectInput(display,window,ExposureMask|ButtonPressMask|ButtonReleaseMask
472 |KeyPressMask|PointerMotionMask);
473 Atom WM_DELETE_WINDOW = XInternAtom(display,
"WM_DELETE_WINDOW", False);
474 XSetWMProtocols(display, window, &WM_DELETE_WINDOW, 1);
476 XStoreName(display,window,text);
478 const char* fontname =
"-bitstream-bitstream vera sans mono-medium-r-normal--10-0-0-0-m-0-iso8859-1";
480 XFontStruct* font = XLoadQueryFont (display, fontname);
482 font = XLoadQueryFont (display,
"fixed");
484 XSetFont (display, gc, font->fid);
486 for (
int i=0; i<MAX; i++) widgets[i]=NULL;
491 for (
int i=0; i<MAX; i++) {
496 XCloseDisplay(display);
500 for (
int i=0; i<MAX; i++)
if (widgets[i]){
505 XNextEvent(display,&event);
507 if (current)
if (current->triggerEvent(event))
goto done;
509 for (
int i=0; i<MAX; i++) {
511 if (widgets[i]->triggerEvent(event)) {
512 changeFocus(widgets[i]);
518 switch (event.type) {
522 for (
int i=0; i<MAX; i++)
if (widgets[i]) {
538 for (
int i=0; i<MAX; i++)
if (!widgets[i]) {
540 widgets[i]->updateGraphVariables(display,screen,window,gc);
550 void changeFocus(
Widget* widget) {
551 if (current) current->setFocused(
false);
553 current->setFocused(
true);
The Label class is the most simple visual class, useful show information to an application final user...
Definition: simplegui.h:106
The List class is a containter of textual items, it object can be stored more items as it can to show...
Definition: simplegui.h:292
The Frame class manage all visual objects: Labels, InputText, Buttons, Lists, so on. One frame works like a window, and send all actions to each object after use them.
Definition: simplegui.h:454
The TextField class is the object used to get information since the final user. To activate its funct...
Definition: simplegui.h:130