Google
Web alhem.net

MenuWeb.cpp

Go to the documentation of this file.
00001 // MenuWeb.cpp
00002 /*
00003 Copyright (C) 2003  Anders Hedstrom
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <time.h>
00024 #ifdef WIN32
00025 #include <winsock2.h>
00026 #else
00027 #include <sys/time.h>
00028 #include <unistd.h>
00029 #endif
00030 //#include <string>
00031 
00032 #include "Form.h"
00033 #include "Cookies.h"
00034 #include "Parse.h"
00035 
00036 #include "WebForm.h"
00037 #include "Web.h"
00038 #include "MenuWeb.h"
00039 
00040 
00041 namespace Cgi {
00042 
00043 MenuWeb::MenuWeb(int style /*= MENU_STYLE_TOP*/) : Web()
00044 ,m_count(0)
00045 ,m_style(style)
00046 ,m_page(0)
00047 ,m_cookiename("page")
00048 ,m_bSetCookie(false)
00049 ,m_strClass("")
00050 {
00051 }
00052 
00053 
00054 MenuWeb::MenuWeb(const std::string &cd,const std::string &cp,int style /*= MENU_STYLE_TOP*/) : Web(cd,cp)
00055 ,m_count(0)
00056 ,m_style(style)
00057 ,m_page(0)
00058 ,m_cookiename("page")
00059 ,m_bSetCookie(false)
00060 {
00061 }
00062 
00063 
00064 MenuWeb::MenuWeb(int argc,char *argv[],const std::string &cd,const std::string &cp,int style /*= MENU_STYLE_TOP*/) : Web(argc,argv,cd,cp)
00065 ,m_count(0)
00066 ,m_style(style)
00067 ,m_page(0)
00068 ,m_cookiename("page")
00069 ,m_bSetCookie(false)
00070 {
00071 }
00072 
00073 
00074 MenuWeb::~MenuWeb()
00075 {
00076         for (menuitem_v::iterator it = m_items.begin(); it != m_items.end(); it++)
00077         {
00078                 MENUITEM *m = static_cast<MENUITEM *>(*it);
00079                 delete m;
00080         }
00081 }
00082 
00083 
00084 
00085 void MenuWeb::SetMenuStyle(int style)
00086 {
00087         m_style = style;
00088         m_bSetCookie = true;
00089 }
00090 
00091 
00092 
00093 const MENUITEM & MenuWeb::AddMenuItem(WebForm *form,const std::string &title)
00094 {
00095         MENUITEM *m = new MENUITEM;
00096 
00097         m -> num = ++m_count;
00098         m -> type = MENUITEM_TYPE_FORM;
00099         m -> form = form;
00100         m -> title = title;
00101         m -> url = "";
00102         m -> src = "";
00103         m_items.insert(m_items.end(),m);
00104         return *m;
00105 }
00106 
00107 
00108 void MenuWeb::AddMenuSeparator()
00109 {
00110         MENUITEM *m = new MENUITEM;
00111 
00112         m -> num = ++m_count;
00113         m -> type = MENUITEM_TYPE_SEPARATOR;
00114         m -> form = NULL;
00115         m -> title = "";
00116         m -> url = "";
00117         m -> src = "";
00118         m_items.insert(m_items.end(),m);
00119 }
00120 
00121 
00122 void MenuWeb::AddMenuLink(const std::string &url,const std::string &title)
00123 {
00124         MENUITEM *m = new MENUITEM;
00125 
00126         m -> num = ++m_count;
00127         m -> type = MENUITEM_TYPE_URL;
00128         m -> form = NULL;
00129         m -> title = title;
00130         m -> url = url;
00131         m -> src = "";
00132         m_items.insert(m_items.end(),m);
00133 }
00134 
00135 
00136 void MenuWeb::AddMenuImage(const std::string &src)
00137 {
00138         MENUITEM *m = new MENUITEM;
00139 
00140         m -> num = ++m_count;
00141         m -> type = MENUITEM_TYPE_URL;
00142         m -> form = NULL;
00143         m -> title = "";
00144         m -> url = "";
00145         m -> src = src;
00146         m_items.insert(m_items.end(),m);
00147 }
00148 
00149 
00150 void MenuWeb::ReadCookies()
00151 {
00152         char slask[200];
00153 
00154         Web::ReadCookies();
00155 
00156         if (GetCookies() -> getvalue( (char *)m_cookiename.c_str(),slask,200))
00157         {
00158                 Cgi::Parse pa(slask,":");
00159                 m_style = pa.getvalue();
00160                 m_page = pa.getvalue();
00161         }
00162 }
00163 
00164 
00165 void MenuWeb::FormInput()
00166 {
00167         char slask[200];
00168 
00169         Web::FormInput();
00170 
00171         if (GetForm() -> getvalue("page",slask,200) && *slask && atoi(slask) )
00172         {
00173                 SetPage(atoi(slask));
00174         }
00175         if (GetForm() -> getvalue("style",slask,200) && *slask && atoi(slask) )
00176         {
00177                 SetMenuStyle(atoi(slask));
00178         }
00179 }
00180 
00181 
00182 void MenuWeb::CreateHeader()
00183 {
00184         if (m_bSetCookie)
00185         {
00186                 SetTheCookie();
00187         }
00188         Web::CreateHeader();
00189 }
00190 
00191 
00192 void MenuWeb::GenerateDocument()
00193 {
00194         int colspan = 1;
00195         char slask[200];
00196         char cls[200];
00197 
00198         if (m_strClass.size())
00199                 sprintf(cls," class=%s",m_strClass.c_str());
00200         else
00201                 *cls = 0;
00202 
00203         switch (m_style)
00204         {
00205         case MENU_STYLE_FRAME_LEFT:
00206                 break;
00207         case MENU_STYLE_TOP:
00208     {
00209                 printf("<table width=\"100%%\"><tr><td>");
00210                 // menu items
00211                 strcpy(slask,"[ ");
00212                 for (menuitem_v::iterator it = m_items.begin(); it != m_items.end(); it++)
00213                 {
00214                         MENUITEM *m = static_cast<MENUITEM *>(*it);
00215                         
00216                         switch (m -> type)
00217                         {
00218                         case MENUITEM_TYPE_FORM:
00219                                 printf("%s<a%s href=\"%s?page=%d\">%s</a> ",
00220                                         slask,
00221                                         cls,
00222                                         GetCgiName().c_str(),
00223                                         m -> num,
00224                                         m -> title.c_str());
00225                                 strcpy(slask,"| ");
00226                                 break;
00227                         case MENUITEM_TYPE_SEPARATOR:
00228                                 if (*slask == '|')
00229                                 {
00230                                         printf("]</td><td>");
00231                                         strcpy(slask,"[ ");
00232                                         colspan++;
00233                                 }
00234                                 break;
00235                         case MENUITEM_TYPE_URL:
00236                                 printf("%s<a%s href=\"%s\">%s</a> ",
00237                                         slask,
00238                                         cls,
00239                                         m -> url.c_str(),
00240                                         m -> title.c_str());
00241                                 strcpy(slask,"| ");
00242                                 break;
00243                         case MENUITEM_TYPE_IMAGE:
00244                                 break;
00245                         }
00246                 }
00247                 if (*slask != '[')
00248                         printf("]");
00249                 printf("</td></tr>");
00250                 printf("</table>");
00251 //              printf("<tr><td colspan=%d>",colspan);
00252                 // selection
00253     {
00254                   for (menuitem_v::iterator it = m_items.begin(); it != m_items.end(); it++)
00255                   {
00256                           MENUITEM *m = static_cast<MENUITEM *>(*it);
00257                           if (m -> num == m_page)
00258                           {
00259                                   m -> form -> Display();
00260                                   break;
00261                           }
00262                   }
00263     }
00264 //              printf("</td></tr></table>");
00265                 break;
00266     }
00267         case MENU_STYLE_LEFT:
00268     {
00269                 printf("<table><tr>");
00270                 printf("<td valign=top align=middle>");
00271                 // menu items
00272                 for (menuitem_v::iterator it = m_items.begin(); it != m_items.end(); it++)
00273                 {
00274                         MENUITEM *m = static_cast<MENUITEM *>(*it);
00275                         
00276                         switch (m -> type)
00277                         {
00278                         case MENUITEM_TYPE_FORM:
00279                                 printf("<a%s href=\"%s?page=%d\">%s</a> ",
00280                                         cls,
00281                                         GetCgiName().c_str(),
00282                                         m -> num,
00283                                         m -> title.c_str());
00284                                 break;
00285                         case MENUITEM_TYPE_SEPARATOR:
00286 //                              printf("<hr>");
00287                                 break;
00288                         case MENUITEM_TYPE_URL:
00289                                 printf("<a%s href=\"%s\">%s</a> ",
00290                                         cls,
00291                                         m -> url.c_str(),
00292                                         m -> title.c_str());
00293                                 break;
00294                         case MENUITEM_TYPE_IMAGE:
00295                                 break;
00296                         }
00297                         printf("<br>");
00298                 }
00299                 printf("</td>");
00300                 printf("<td valign=top>");
00301                 // selection
00302     {
00303                   for (menuitem_v::iterator it = m_items.begin(); it != m_items.end(); it++)
00304                   {
00305                           MENUITEM *m = static_cast<MENUITEM *>(*it);
00306                           if (m -> num == m_page)
00307                           {
00308                                   m -> form -> Display();
00309                                   break;
00310                           }
00311                   }
00312     }
00313                 printf("</td>");
00314                 printf("</tr></table>");
00315                 break;
00316     }
00317         }
00318 }
00319 
00320 
00321 void MenuWeb::SetTheCookie()
00322 {
00323         char slask[200];
00324         
00325         sprintf(slask,"%d:%d",m_style,m_page);
00326         SetCookie(m_cookiename.c_str(),slask);
00327 }
00328 
00329 
00330 } // namespace

Generated for cgi++ by doxygen 1.3.7

Page, code, and content Copyright (C) 2004 by Anders Hedström