Logo Search packages:      
Sourcecode: vdkbuilder2 version File versions  Download package

vdkb_notebook.cc

/*
 * ===========================
 * VDK Builder
 * Version 0.1.1
 * Revision 0.0
 * March 1999
 * ===========================
 *
 * Copyright (C) 1998,1999 Mario Motta
 * Developed by Mario Motta <mmotta@guest.net>
 *
 * Based on VDK Library
 * Copyright (C) 1998, Mario Motta
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#if !HAVE_GNOME
  #if ENABLE_NLS
    #include <libintl.h>
//#define _(str) gettext(str)
#define _(str) \
    ( g_utf8_validate(gettext(str),-1,NULL) ? \
    gettext(str) : \
    g_locale_to_utf8(gettext(str),-1,NULL,NULL,NULL) )
#define N_(str) str
  #else
    #define _(str) str
    #define N_(str) str 
  #endif
#else
 #include <gnome.h>
#endif

#include <stdlib.h>
#include <vdkb2/vdkb_evcontain.h>
//#include <vdkb2/vdkb_labelbutton.h>
#include <vdkb2/vdkb_form.h>
#include <vdk/vdk.h>
#include <vdkb2/vdkb_utils.h>
#include <vdkb2/vdkb_parser.h>
#include <vdkb2/vdkb_prjman.h>
#include <vdkb2/vdkb_objinspect.h>
#include <vdkb2/vdkb_notebook.h>
#include <vdkb2/vdkb_fixed.h>
#include <vdkb2/vdkb_widpopmenu.h>
/*
================================
symbolic constants to templatize
a bit
================================
*/
// for methods and other stuff
#define CLASS VDKBGuiNotebook
// put here vdk class name string
#define VDK_CLASS "VDKNotebook"
// put here vdk class name
#define VDK_ANCESTOR  VDKNotebook
// put here here the widget will be named
// (name+counter)
#define VDK_WIDGET "notebook"
extern char* wi_widget_prompts[];
static char buff[128];
int CLASS::Counter = 0;

/*
 notebook properties names
 */
char* vdknotebook_props[] =
{
SCROLLABLE,POPUP,0
};
/*
notebook signal names && nicknames
Default response method name will be made by:
On<object name><nickname>.
For instance if object name = <Abutton> and nickname = <Click>
default response method name will be: <OnAbuttonClick>
*/
char* vdknotebook_signals[] =
{
"switch_page_signal", 0
};
char* vdknotebook_nicknames[] =
{
"SwitchPage",0
};

DEFINE_EVENT_LIST(CLASS,VDKBEventContainer);
DEFINE_SIGNAL_LIST(CLASS,VDKBEventContainer);


/*
 */
bool
CLASS:: DelBox(VDKObject* sender)
{
  VDKBGuiForm* ownerform = dynamic_cast<VDKBGuiForm*>(Owner());
  // call ancestor delete box
  VDKBEventContainer::DelBox(sender);
  // notify to inspector that object was deleted
  if(ownerform)
    {
      VDKBProjectManager* prjman =
      dynamic_cast<VDKBProjectManager*>(ownerform->Owner());
      if(prjman && prjman->objInspector)
      prjman->objInspector->SetActive(NULL);
    }
return true;
}

/*
 */
bool
CLASS::SetBoxSize(VDKObject* sender)
{
  VDKBGuiForm* ownerform = dynamic_cast<VDKBGuiForm*>(Owner());
  if(ownerform)
    {
      ownerform->SetBoxSize(NULL);
    }
  return true;
}
/*
 */
bool
CLASS::AddNewPage(VDKObject* sender)
{
  char local[64];
  // make the first suitable tab label
  // roughly initializes pagecount to tablabels size +1
  int pagecount = tablabels.size()+1;
  // makes the label
  sprintf(local,"%s_page%d",(char*) Name(),pagecount );
  // checks if already into the list
  // than increments page counter
  VDKString lbl(local);
  while(tablabels.find(lbl))
    {
      pagecount++;
      sprintf(local,"%s_page%d",(char*) Name(),pagecount );
      lbl = local;
    }
  // got an unique label
  VDKBEventBox *box = new VDKBEventBox(local,this);
  AddWidget(box);
  pagelist.add(box);
  return true;
}
/*
  unused
 */
bool
CLASS::OnButtonPressed(VDKObject* sender, GdkEvent* event)
{
  // commented out, just let's pass trough
  printf("\nOnButtonPressed*****");
  fflush(stdout);
  VDKBEventContainer::OnButtonPressed(this,event);
  //VDKBObject::ButtonPressed(sender,event);
  return false;
}

/*
 */
bool
CLASS::RemovePage(VDKObject* sender)
{
  int ndx = nbook->ActivePage;
  VDKBObject *page = pagelist[ndx];
  VDKBEventBox* box = dynamic_cast<VDKBEventBox*>(page);
  if(box)
    {
      int ndx = pagelist.at(page);
      if(ndx >= 0)
      {
        pagelist.remove(page);
        tablabels.unlink(ndx);
        // 2nd arg to false does not remove 
        // child on notebook.
        // it will be removed by DelBox()
        nbook->RemovePage(ndx,false);
        // calling with sender NULL forces
        // to delete box embedded into a notebook page
        // (normally wouldn't be allowed)
        box->DelBox(NULL);
        delpage->Enabled = pagelist.size() > 1;
      }
    }
  return true;
}

//===========================================
/*
 */
CLASS::CLASS(char* name, VDKForm* owner):
  VDKBEventContainer(name,owner)
{
  Counter++;
  VDKBObject::object = this;
  Init();
}

/*
 */
CLASS::CLASS(char* name,VDKBEventContainer* outer):
    VDKBEventContainer(name,outer->Owner())
{
  VDKBObject::object = this;
  Counter++;
  outerbox = outer;
  Init();
 }
/*
 */
void
CLASS::Init()
{
  int t;
  AddBox();
  // add to VDKBObject properties list
  for(t=0; vdknotebook_props[t]; t++)
    proplist.add(VDKBProperty(vdknotebook_props[t]));
  // add to VDKBObject signal list  signals
  for(t=0; vdknotebook_signals[t]; t++)
    siglist.add(VDKBSignal(vdknotebook_signals[t],
                     this,
                     vdknotebook_nicknames[t]));
  // makes a pop menu
  popmenu = new VDKBContainerPopMenu(this);
  addwidget = new VDKMenuItem(popmenu,_("Add a new page"));
  delpage = new VDKMenuItem(popmenu,_("Remove current page"));
  popmenu->Separator();
  delBox = new VDKMenuItem(popmenu,_("Remove notebook"));
  setsize = new VDKMenuItem(popmenu,_(wi_widget_prompts[19]));
  selectparent =  new VDKMenuItem(popmenu,_(wi_widget_prompts[20]));
  SignalConnect(selectparent,"activate",
            &CLASS::SelectParentContainer);
  SignalConnect(delBox,"activate",&CLASS::DelBox);
  SignalConnect(setsize,"activate",&CLASS::SetBoxSize);
  SignalConnect(addwidget,"activate",&CLASS::AddNewPage);
  SignalConnect(delpage,"activate",&CLASS::RemovePage);
  // connect after so switch page signal can be handled.
  EventConnect("expose_event",&VDKBObject::BaseExposeEvent);
  // EventConnect("button_press_event",&CLASS::ButtonPressed,true);
  EventConnect("button_press_event",&VDKBObject::ButtonPressed);
  EventConnect("button_release_event",&VDKBObject::ButtonReleased);
  // EventConnect("button_release_event",&VDKBObject::ButtonReleased,true);
  // EventConnect("enter_notify_event",&VDKBObject::OnEnter);
  // EventConnect("leave_notify_event",&VDKBObject::OnLeave);
  
  // CONNECT_COMMON_EVENTS;
  /* assign this as parent so this can receive signals  */
  popmenu->Parent(this);
  popmenu->Setup();
  /*
    better add it to owner, so will be surely
    destroyed even if never popped
  */
  Owner()->AddItem(popmenu);
  tablabel = NULL;
}
/*
 */
void
CLASS::AddBox()
{
  nbook = new VDK_ANCESTOR(Owner());
  widget = sigwid = container = nbook->Widget();
  gtk_widget_show(container);
  Owner()->Objects().add(nbook);
  nbook->Parent(this);
}

/*
only tab pages can be added to notebook
 */
void
CLASS::AddWidget(VDKObject* obj, int justify,
                  int expand, int fill , int padding,
                  bool forceArgs)
{
  VDKBObject* vdkbobj = dynamic_cast<VDKBObject*>(obj);
  nbook->AddPage(obj,vdkbobj ? (char*) vdkbobj->Name() : (char*) "#none");
  VDKBEventContainer::Add(obj,justify,expand,fill,padding);
  nbook->Visible = true;
  pagelist.add(vdkbobj);
  if(tablabels.size() >= pagelist.size())
    {
      int ndx = pagelist.size()-1;
      nbook->Pages[ndx]->TabLabel->Caption =  (char*) tablabels[ndx];
      if(vdkbobj)
      vdkbobj->SetPropValue(LABEL,(char*) tablabels[ndx]);
    }
  else
    {
      tablabels.add(vdkbobj->Name());
      if(vdkbobj)
      vdkbobj->SetPropValue(LABEL,(char*) vdkbobj->Name());
    }
  delpage->Enabled = pagelist.size() > 1;
  // update widgets tree on object inspector
  VDKBGuiForm* ownerform = dynamic_cast<VDKBGuiForm*>(Owner());
  VDKBProjectManager* prjman = 
    ownerform ?
    dynamic_cast<VDKBProjectManager*>(ownerform->Owner()):
    NULL;
  if(prjman && prjman->objInspector)
      prjman->objInspector->LoadTree(ownerform);
}
/*
extra args used
 */
void
CLASS::Add(VDKObject* wid, int justify,
                  int expand, int fill , int padding,
                  bool forceArgs)
{
  AddWidget(wid,justify, expand, fill , padding,forceArgs);
}
/*
  This method is called by global MakeWidget() in vdkb_design.cc
  MakeWidget() scans a table that maps class id's with each
  static MakeWidget() for each class. Class id's are generated
  during clicks on widget palette.
  On return:
  0 - successfull
  1 - unsupported widget
  2 - target is not a container
  3 - no active widget
  4 - unuseful call
*/
int
CLASS::MakeWidget(VDKBGuiForm* owner, GdkEvent* ev)
{
  // autogenerate first suitable frame counter
  // to ensure unicity
  int result = 0;
  VDKBEventBox *box1 = NULL;
  CLASS* notebook = NULL;
  if(owner->Active)
    {
      char local[64];
      for(sprintf(buff,"%s%d",VDK_WIDGET,CLASS::Counter);
        owner->ChildWithName(buff)!= (VDKObject*) NULL;
        CLASS::Counter++)
      sprintf(buff,"%s%d",VDK_WIDGET,CLASS::Counter);
      notebook = new CLASS(buff,owner);
      VDKBEventContainer* container =
      dynamic_cast<VDKBEventContainer*>(owner->Active);
      if(container)
      {
        if(ev && dynamic_cast<VDKBFixed*>(container))
          {
            char local[16];
            GdkEventButton* event = (GdkEventButton*) ev;
            sprintf(local,"%d",int(event->x));
            notebook->SetPropValue(JUSTIFY_INTERNAL,local);
            sprintf(local,"%d",int(event->y));
            notebook->SetPropValue(EXPAND_INTERNAL,local);
            // others than justify and flag unuseful
            container->AddWidget(notebook,int(event->x),
                           int(event->y),
                           true,true,true);
          }
        else
          container->AddWidget(notebook);
        notebook->outerbox = container;
        sprintf(local,"%s_page%d",buff,notebook->nbook->Pages.size());
        box1 = new VDKBEventBox(local,notebook);
        notebook->AddWidget(box1);
      }
      else if( owner->Active->AddToParent(notebook,ev))
      {
        sprintf(local,"%s_page%d",buff,notebook->nbook->Pages.size());
        box1 = new VDKBEventBox(local,notebook);
        notebook->AddWidget(box1);
      }
      else
      // target isn't a container
      result =  2;
    }
  else
    // no active widget
    result = 3;

  // 0 on success
  if(result && notebook)
       notebook->Destroy();
  return result;
}
////////////////////////////////////////////////////////////////////
/*
 */
void
CLASS::WriteOnFrm(FILE* fp, VDKBObject* parentobj)
{
  VDKBEventContainer::WriteOnFrm(fp,parentobj);
  // writes tab labels number
  fprintf(fp,"\n\tPages:%d;",pagelist.size());
  // writes tab labels
  fprintf(fp,"\n\tLabels:\"");
  //  int t,last = tablabels.size();
  int t,last = pagelist.size();
  for(t = 0; t < last-1 ; t++)
    fprintf(fp,"%s,", (char*) tablabels[t]);
  fprintf(fp,"%s\";", (char*) tablabels[last-1]);
  fprintf(fp,"\n\t%s%s;", 
        PROP_SCROLLABLE,(char*) GetProp(SCROLLABLE));
  fprintf(fp,"\n\t%s%s;", PROP_POPUP,(char*) GetProp(POPUP));
}

char*
CLASS::CreateSource(char* buffer,VDKBParser& parser)
{
  char* source;
  char obj_name[128];
  char obj_parent[128];
  char temp[256];
  char bw[16];
  char arg[16];
  // get name, mode and parent
  if ( !parser.GetParam(obj_name,buffer,"this:") ||
       !parser.GetParam(obj_parent,buffer,"parent:")
       )
    return NULL;
  else
    source = new char[1024];
  sprintf(temp,"\n%s = new %s(this);" ,obj_name,VDK_CLASS);
  strcpy(source,temp);
  // get size
  VDKPoint size = parser.Size(buffer);
  if(size.X() > 0 || size.Y() > 0)
    {
      sprintf(temp,"\n%s->SetSize(%d,%d);",obj_name,size.X(),size.Y());
      strcat(source,temp);
    }
  if(strcmp(obj_parent,NIHIL_PROP))
    sprintf(temp,"\n%s->Add(%s",obj_parent,obj_name);
  else
    sprintf(temp,"\nAdd(%s",obj_name);
  strcat(source,temp);
  // prepares arguments for add widget to container
  char justify[16],expand[16],fill[16],padding[16];
  if(parser.GetParam(justify,buffer,PROP_JUSTIFY_INTERNAL) &&
     parser.GetParam(expand,buffer,PROP_EXPAND_INTERNAL) &&
     parser.GetParam(fill,buffer,PROP_FILL_INTERNAL) &&
     parser.GetParam(padding,buffer,PROP_PADDING_INTERNAL))
    {
      sprintf(temp,",%s,%s,%s,%s);",
            justify,expand,fill,padding);
      strcat(source,temp);
    }
  else
    {
      sprintf(temp,");");
      strcat(source,temp);
    }

  if(parser.GetParam(bw,buffer,PROP_BORDERWIDTH) &&
     strcmp(bw,NIHIL_PROP))
    {
      sprintf(temp,"\n%s->BorderWidth(%s);",obj_name,bw);
      strcat(source,temp);
    }
  if(parser.GetParam(arg,buffer,PROP_SCROLLABLE) &&
     strcmp(arg,NIHIL_PROP))
    {
      sprintf(temp,"\n%s->Scrollable = %s;",obj_name,arg);
      strcat(source,temp);
    }
  if(parser.GetParam(arg,buffer,PROP_POPUP) &&
     strcmp(arg,NIHIL_PROP))
    {
      sprintf(temp,"\n%s->PopUp = %s;",obj_name,arg);
      strcat(source,temp);
    }
  return source;
}
/*
 */
bool
CLASS::LoadTabLabels(char* buffer,VDKBParser& parser)
{
  int n = 0 ;
  char arg[16],*local,*p;
  if(parser.GetParam(arg,buffer,"Pages:"))
    n = atoi(arg);
  else
    return false;
  local = new char[1024];
  if(!parser.GetParam(local,buffer,"Labels:"))
    {
      delete[] local;
      return false;
    }
  // now in local there are a list of labels
  p = strtok(local,",");
  while(p)
    {
      tablabels.add(VDKString(p));
      p = strtok(NULL,",");
    }
  delete[] local;
  return tablabels.size() == n;
}
/*
 */
bool
CLASS::CreateWidget(VDKBGuiForm* owner, char* buffer,VDKBParser& parser)
{
  char obj_name[128];
  char obj_parent[128];
  char arg[32];
  CLASS* box;
  // get name, mode and parent
  if ( !parser.GetParam(obj_name,buffer,"this:") ||
       !parser.GetParam(obj_parent,buffer,"parent:")
       )
    return false;
  // get mode and size
  VDKPoint size = parser.Size(buffer);
  // get packing args
  int justification = l_justify;
  int expand = 0,fill = 0,padding = 0;
  int bw; // border width
  if(parser.GetParam(arg,buffer,PROP_JUSTIFY_INTERNAL))
    justification = atoi(arg);
  if(parser.GetParam(arg,buffer,PROP_EXPAND_INTERNAL))
    expand =  atoi(arg);
  if(parser.GetParam(arg,buffer,PROP_FILL_INTERNAL))
    fill = atoi(arg);
  if(parser.GetParam(arg,buffer,PROP_PADDING_INTERNAL))
    padding = atoi(arg);
  if(parser.GetParam(arg,buffer,PROP_BORDERWIDTH) &&
     strcmp(arg,NIHIL_PROP))
    bw = atoi(arg);
  else
    bw = -1;
  // no parent, widget will be added to owner form innerbox
  if(!strcmp(obj_parent,NIHIL_PROP))
    {
      box = new CLASS(obj_name,owner->InnerBox());
      box->LoadTabLabels(buffer, parser);
      owner->AddWidget(box,justification,expand,fill,padding);
    }
  // get parent container address
  else
    {
      VDKObject* p = owner->ChildWithName(obj_parent);
      VDKBEventContainer* container = p ?
      dynamic_cast<VDKBEventContainer*>(p) : (VDKBEventContainer*) NULL;
      if(container)
      {
        box = new CLASS(obj_name,container);
        box->LoadTabLabels(buffer, parser);
        container->AddWidget(box,justification,expand,fill,padding,true);
        box->outerbox = container;
      }
      else
      // FIX ME: user warning
      return false;
    }
  //
  if(parser.GetParam(arg,buffer,PROP_SCROLLABLE) 
     && strcmp(arg,NIHIL_PROP))
    {
      box->SetPropValue(SCROLLABLE,arg);
      if(!strcmp(arg,CHECK_TRUE))
      box->nbook->Scrollable = true;
    }
  if(parser.GetParam(arg,buffer,PROP_POPUP) && strcmp(arg,NIHIL_PROP))
    box->SetPropValue(POPUP,arg);
  // call ancestor to set common properties
  VDKBObject::CreateWidget(box,buffer,parser);
  if(size.X() > 0 || size.Y() > 0)
    box->ObjectFromVDK()->SetSize(size.X(),size.Y());
  if(bw >= 0)
    {
      box->BorderWidth(bw);
      box->SetPropValue(BORDERWIDTH,arg);
    }
  return true;
}
/////////////////////////////////////////////////////
//           OBJECT INSPECTOR MANAGEMENT
////////////////////////////////////////////////////
/*
This is called by object inspector when a widget
is selected by user, thus to allow widget to set
his own property controls.
 */

VDKObjectContainer*
CLASS::ExtraWidget(VDKBObjectInspector* isp)
{
  VDKString True = CHECK_TRUE;
  inspector = isp;
  VDKBox* bframe = dynamic_cast<VDKBox*>(VDKBEventContainer::ExtraWidget(isp));
  if(!bframe)
    return NULL;
  VDKTable *table = new VDKTable(inspector,2,2);
  table->SetSize(219,-1);
  table->AddToCell(new VDKLabel(inspector,_(wi_widget_prompts[42])),0,0);
  // gint pagenum = gtk_notebook_get_current_page (GTK_NOTEBOOK(WrappedWidget()));
  // printf("\nactive page:%d - nbook->ActivePage:%d",pagenum,(int) nbook->ActivePage);
  // fflush(stdout);
  tablabel = new VDKEntry(inspector,64, (char*) tablabels[nbook->ActivePage]); /**/
  tablabel->SetSize(100,-1);
  table->AddToCell(tablabel,0,1);
  tablabel->Parent(this);

  scrollable = new VDKCheckButton(inspector,_(wi_widget_prompts[43]));
  table->AddToCell(scrollable,1,0);
  scrollable->Checked = GetProp(SCROLLABLE) == True;
  scrollable->Parent(this);
  popup = new VDKCheckButton(inspector,_(wi_widget_prompts[44]));
  table->AddToCell(popup,1,1);
  popup->Checked = GetProp(POPUP) == True;
  popup->Parent(this);

  SignalConnect(tablabel,"activate",&CLASS::OnSetTabLabel);
  SignalConnect(scrollable,"toggled",&CLASS::OnSetScrollable);
  SignalConnect(popup,"toggled",&CLASS::OnSetPopUp);

  bframe->Add(table,l_justify,false,false,false);
  return bframe;
}
/*
 */
bool
CLASS::OnSetTabLabel(VDKObject*)
{
  int activepage;
  activepage = nbook->ActivePage;
  if( (activepage >= 0) && (activepage < nbook->Pages.size() ))
    {
      if(strlen(tablabel->Text) <= 0)
      *buff = '\0';
      else
      sprintf(buff,"%s",(char*) tablabel->Text);
      nbook->Pages[nbook->ActivePage]->TabLabel->Caption = buff;
      tablabels[nbook->ActivePage] = buff;
      pagelist[activepage]->SetPropValue(LABEL,buff);
      inspector->FormNeedToBeChanged();
    }
  return true;
}
/*
 */
bool
CLASS::OnSetScrollable(VDKObject*)
{
  SetPropValue(SCROLLABLE, 
             scrollable->Checked ? CHECK_TRUE : "nihil");
  nbook->Scrollable = scrollable->Checked ? true : false;
  inspector->FormNeedToBeChanged();
  return true;
}
/*
 */
bool
CLASS::OnSetPopUp(VDKObject*)
{
  SetPropValue(POPUP, popup->Checked ? CHECK_TRUE : CHECK_FALSE);
  inspector->FormNeedToBeChanged();
  return true;
}



Generated by  Doxygen 1.6.0   Back to index