Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

gadget_Button.c

Go to the documentation of this file.
00001 /*
00002  *    gui - [gega user interface] the flexible solution for user interface problems
00003  *    Copyright (C) 2002  Gergely Gati
00004  *
00005  *    This program is free software; you can redistribute it and/or modify
00006  *    it under the terms of the GNU General Public License as published by
00007  *    the Free Software Foundation; version 2 of the License.
00008  *
00009  *    This program is distributed in the hope that it will be useful,
00010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *    GNU General Public License for more details.
00013  *
00014  *    You should have received a copy of the GNU General Public License
00015  *    along with this program; if not, write to the Free Software
00016  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  *    Gergely Gati
00019  *      email:           g.gati@freemail.hu
00020  *      AIM screenname:  GatiGergely
00021  *      ICQ number:      93131690
00022  *
00023  */
00024 /*
00025  *  button.c
00026  *
00027  *  Author            : Gergely Gáti
00028  *  Date              :
00029  *  Description       :
00030  *
00031  */
00032 #include <stdlib.h>
00033 #include <string.h>
00034 
00035 #include "gadgets/gadget_Button.h"
00036 
00037 #include "debug.h"
00038 #include "classes.h"
00039 #include "Bases.h"
00040 #include "Module.h"
00041 #include "Memory.h"
00042 #include "Common.h"
00043 #include "Image.h"
00044 #include "Window.h"
00045 
00046 
00047 struct _button
00048 {
00049   Gadget_t gadget;
00050   char *label;
00051 /*
00052   Image_t *img_up;
00053   Image_t *img_down;
00054 */
00055   int label_width;
00056   int state;
00057 };
00058 
00059 static bases_Modules_t *api;
00060 VERSION("Button.gadget",1,0,"Gergely Gati","g.gati@freemail.hu");
00061 
00062 static gadget_Binding_t binding[]=
00063 {
00064   {"label",     TAG_BTN_LABEL,        GADGET_STRING,""},
00065   {"image-up",  TAG_BTN_IMAGEUPNAME,  GADGET_STRING,""},
00066   {"image-down",TAG_BTN_IMAGEDOWNNAME,GADGET_STRING,""},
00067   GADGET_BINDING_DONE
00068 };
00069 
00070 static char *color_names[]=
00071 {
00072   "text",
00073   NULL
00074 };
00075 
00076 
00077 
00078 static char empty[]="";
00079 
00080 
00081 /*
00082  *  This function is always called after the Gadget_t has been created and
00083  *  attached to the Gadget_t tree, but before the call to button_layout(). It may be
00084  *  called more than once. It must fill in the Gadget_t's minimum and maximum
00085  *  sizes into the supplied extent structures.
00086  */
00087 static void button_get_extent(Gadget_t *this, Extent_t *min, Extent_t *max)
00088 {
00089     u32 font;
00090 
00091   debug_Begin();
00092 
00093   font=api->gadget_Call(GADGET_GET_FONT,TAG_GADGET_OBJECT,this,TAG_DONE);
00094 //  if(((button *)this)->label_width<0) ((button *)this)->label_width=api->glw_Call(GLW_GETTEXTLENGTH,TAG_GLW_FONT,font,TAG_GLW_TEXT,((button *)this)->label,TAG_DONE);
00095   ((button *)this)->label_width=api->glw_Call(GLW_GETTEXTLENGTH,TAG_GLW_FONT,font,TAG_GLW_TEXT,((button *)this)->label,TAG_DONE);
00096   min->width=4+((button *)this)->label_width;
00097   min->height=api->glw_Call(GLW_GETFONTSIZE,TAG_GLW_FONT,font,TAG_DONE);
00098   max->width=GADGET_MAXIMUM_SIZE;
00099   max->height=min->height;
00100   debug_Message("min: %dx%d  max: %dx%d",min->width,min->height,max->width,max->height);
00101 
00102   debug_End();
00103 }
00104 
00105 
00106 /*
00107  *  This function is called always after button_get_extent() has been called at
00108  *  least once. It receives a position and an extent which determine the
00109  *  position and the size of the Gadget_t on the screen. This 4 is always
00110  *  between the minimum and maximum sizes which were returned by
00111  *  button_get_extent() of the same gadget. After this call the Gadget_t is made
00112  *  visible.
00113  */
00114 static void button_layout(Gadget_t *this, Rect_t *rect)
00115 {
00116   Rect_t rct;
00117 
00118   debug_Begin();
00119 
00120   rct.left=rect->left+2;
00121   rct.top=rect->top;
00122   rct.width=rect->width-4;
00123   rct.height=rect->height;
00124 
00125   api->glw_Call(GLW_SETCOLOR,TAG_GLW_WINDOW,this->window->handle,TAG_GLW_RGB,api->gadget_Call(GADGET_GET_COLOR,TAG_GADGET_OBJECT,this,TAG_GADGET_COLOR,BTN_COL_TEXT,TAG_DONE),TAG_DONE);
00126   api->glw_Call(GLW_DRAWTEXT,TAG_GLW_WINDOW,this->window->handle,TAG_GLW_CLIPRECT,&rct,TAG_GLW_FONT,api->gadget_Call(GADGET_GET_FONT,TAG_GADGET_OBJECT,this,TAG_DONE),TAG_GLW_TEXT,((button *)this)->label,TAG_GLW_FLAGS,GLWFA_ALIGN_CENTER|GLWFA_VALIGN_CENTER,TAG_DONE);
00127 
00128   debug_End();
00129 }
00130 
00131 
00132 static int button_input_event_handler(Gadget_t *gad, struct glw_Event *event)
00133 {
00134   int ret=0;
00135 //  button *this=(button *)gad;
00136 
00137   debug_Begin();
00138 
00139   switch(event->event)
00140   {
00141     case GLWEV_MOUSEMOVE:
00142     {
00143       if(((button *)gad)->state==1)
00144       {
00145         if(!api->gui_Call(GUI_ISINSIDE,TAG_GUI_RECT,&gad->rect,TAG_GUI_X,event->mousex,TAG_GUI_Y,event->mousey,TAG_DONE))
00146         {
00147           api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_FOCUSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00148         }
00149         else
00150         {
00151           api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_PRESSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00152         }
00153       }
00154       break;
00155     }
00156     case GLWEV_MOUSEBUTTONS:
00157     {
00158       switch(event->data)
00159       {
00160         case GLWEVD_LEFTDOWN:
00161         {
00162           ((button *)gad)->state=1;
00163           api->window_Call(WINDOW_SETFOCUSEDGADGET,TAG_WIN_OBJECT,gad->window,TAG_WIN_GADGET,gad,TAG_DONE);
00164           api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_PRESSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00165           ret=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_DOWN,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00166           api->window_Call(WINDOW_SETACTIVE,TAG_WIN_OBJECT,gad->window,TAG_WIN_GADGET,gad,TAG_DONE);
00167           break;
00168         }
00169         case GLWEVD_LEFTUP:
00170         {
00171           if(((button *)gad)->state==1)
00172           {
00173             ((button *)gad)->state=0;
00174             api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_FOCUSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00175             api->window_Call(WINDOW_SETACTIVE,TAG_WIN_OBJECT,gad->window,TAG_WIN_GADGET,NULL,TAG_DONE);
00176             ret=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_UP,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00177             if(api->gui_Call(GUI_ISINSIDE,TAG_GUI_RECT,&gad->rect,TAG_GUI_X,event->mousex,TAG_GUI_Y,event->mousey,TAG_DONE)) ret|=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_CLICKED,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00178           }
00179           break;
00180         }
00181       }
00182       break;
00183     }
00184     case GLWEV_KEYUP:
00185     {
00186       if(((button *)gad)->state==2&&event->data==13)
00187       {
00188         ((button *)gad)->state=0;
00189         api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_FOCUSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00190         ret=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_UP,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00191         ret|=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_CLICKED,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00192       }
00193       ret=0;
00194       break;
00195     }
00196     case GLWEV_KEYDOWN:
00197     {
00198       if(((button *)gad)->state==0&&event->data==13)
00199       {
00200         ((button *)gad)->state=2;
00201         api->gadget_Call(GADGET_SET_STATE,TAG_GADGET_OBJECT,gad,TAG_GADGET_STATE,GADST_PRESSED,TAG_GADGET_VISUAL,1,TAG_DONE);
00202         ret=api->gadget_Call(GADGET_CALLBACK,TAG_GADGET_OBJECT,gad,TAG_GADGET_EVENT,CBK_GADGET_DOWN,TAG_GADGET_EVENT_DATA,0L,TAG_DONE);
00203       }
00204       ret=0;
00205       break;
00206     }
00207   }
00208 
00209   debug_End();
00210 
00211   return(ret);
00212 }
00213 
00214 
00215 int button_init_gadget(Gadget_t *this, tag *taglist)
00216 {
00217 
00218   debug_Begin();
00219 
00220   ((button *)this)->label=common_strdup((char *)tag_GetTagData(taglist,TAG_BTN_LABEL,(u32)""));
00221   if(NULL==((button *)this)->label) ((button *)this)->label=empty;
00222   ((button *)this)->state=0;
00223   ((button *)this)->label_width=-1;
00224   debug_Message("label='%s'",((button *)this)->label);
00225 
00226   debug_End();
00227 
00228   return(0);
00229 }
00230 
00231 
00232 void button_delete_gadget(Gadget_t *this)
00233 {
00234   debug_Begin();
00235 
00236   if(NULL!=((button *)this)->label&&empty!=((button *)this)->label) mem_free(((button *)this)->label);
00237 
00238   debug_End();
00239 }
00240 
00241 
00242 static int button_gadget_event_handler(Gadget_t *this, int event, tag *taglist)
00243 {
00244   int ret=-1;
00245 
00246   debug_Begin();
00247 
00248   if(this!=NULL&&event>=0)
00249   {
00250     switch(event)
00251     {
00252       case GADEV_INIT_GADGET:
00253       {
00254         ret=button_init_gadget(this,taglist);
00255         break;
00256       }
00257       case GADEV_DELETE_GADGET:
00258       {
00259         button_delete_gadget(this);
00260         ret=0;
00261         break;
00262       }
00263       case GADEV_GET_EXTENT:
00264       {
00265         Extent_t *min,*max,n,x;
00266         min=(Extent_t *)tag_GetTagData(taglist,TAG_GAD_EXTMIN,(u32)&n);
00267         max=(Extent_t *)tag_GetTagData(taglist,TAG_GAD_EXTMAX,(u32)&x);
00268         button_get_extent(this,min,max);
00269         ret=0;
00270         break;
00271       }
00272       case GADEV_LAYOUT:
00273       case GADEV_DAMAGE:
00274       {
00275         Rect_t *rect,r;
00276         rect=(Rect_t *)tag_GetTagData(taglist,TAG_GAD_RECT,(u32)&r);
00277         button_layout(this,rect);
00278         ret=0;
00279         break;
00280       }
00281       case GADEV_SAVE:
00282       {
00283         u8 **mem;
00284         int *size;
00285         mem=(u8 **)tag_GetTagData(taglist,TAG_GADGET_MEM,0L);
00286         size=(int *)tag_GetTagData(taglist,TAG_GADGET_SIZE,0L);
00287         *size=strlen(((button *)this)->label)+1;
00288         if(NULL!=(*mem=mem_malloc(*size)))
00289         {
00290           strcpy(*mem,((button *)this)->label);
00291           ret=0;
00292         }
00293         break;
00294       }
00295       case GADEV_RESTORE:
00296       {
00297         u8 *mem;
00298         int size;
00299         mem=(u8 *)tag_GetTagData(taglist,TAG_GADGET_MEM,0L);
00300         size=(int)tag_GetTagData(taglist,TAG_GADGET_SIZE,0L);
00301         if(NULL!=((button *)this)->label&&empty!=((button *)this)->label) mem_free(((button *)this)->label);
00302         if(NULL!=(((button *)this)->label=mem_malloc(size)))
00303         {
00304           strncpy(((button *)this)->label,mem,size);
00305           ((button *)this)->label_width=-1;
00306           ret=0;
00307         }
00308         break;
00309       }
00310     }
00311   }
00312 
00313   debug_End();
00314 
00315   return(ret);
00316 }
00317 
00318 
00319 /*
00320  *  This function is called once before the creation of any Gadget_t of this
00321  *  class. Gadgets can be created only after this function has been called.
00322  */
00323 static void init_button_class(u32 module)
00324 {
00325   api->gadget_Call(GADGET_REGISTER_CLASS,
00326                             TAG_GADGET_CLASS_ID,CLASS_BUTTON_ID,
00327                             TAG_GADGET_NAME,CLASS_BUTTON_NAME,
00328                 TAG_GADGET_BINDING,binding,
00329                 TAG_GADGET_COLOR_NAMES,color_names,
00330                 TAG_GADGET_CUSTOM_NAMES,NULL,
00331                 TAG_GADGET_STATE_MASK,GADSTF_NORMAL|GADSTF_PRESSED|GADSTF_FOCUSED,
00332                 TAG_GADGET_MODULE,module,
00333                 TAG_GADGET_GADGET_SIZE,sizeof(button),
00334                 TAG_GADGET_METHODS,NULL,
00335                 TAG_GADGET_INPUT_EVENT_HANDLER,button_input_event_handler,
00336                 TAG_GADGET_EVENT_HANDLER,NULL,
00337                 TAG_GADGET_GADGET_EVENT_HANDLER,button_gadget_event_handler,
00338                 TAG_DONE);
00339 }
00340 
00341 EXPORT int module_Init(u32 module, bases_Modules_t *bases)
00342 {
00343     api=bases;
00344     init_button_class(module);
00345     return(0);
00346 }

Generated on Tue Jan 7 12:11:19 2003 for THEGUI by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002