windowing.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           paradialog.cpp  -  description
00003                              -------------------
00004     begin                : Thu Feb 21 2002
00005     copyright            : (C) 2002 by Martin Bickel
00006     email                : bickel@asc-hq.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 
00019 #include <SDL_image.h>
00020 #include <signal.h>
00021 
00022 
00023 #include <paragui.h>
00024 #include <pgapplication.h>
00025 #include <pgmessagebox.h>
00026 #include <pgdropdown.h>
00027 #include "pgbutton.h"
00028 #include "pgwidgetlist.h"
00029 #include "pglabel.h"
00030 #include "pgwindow.h"
00031 #include "pgmaskedit.h"
00032 #include "pgscrollbar.h"
00033 #include "pgprogressbar.h"
00034 #include "pgradiobutton.h"
00035 #include "pgthemewidget.h"
00036 #include "pgcheckbutton.h"
00037 #include "pgslider.h"
00038 #include "pglistbox.h"
00039 #include "pgcolumnitem.h"
00040 #include "pgpopupmenu.h"
00041 #include "pgspinnerbox.h"
00042 #include "pglog.h"
00043 #include "pgmenubar.h"
00044 #include "pgimage.h"
00045 #include "pgmessagebox.h"
00046 #include "pgwindow.h"
00047 #include "pgrichedit.h"
00048 #include "pgsdleventsupplier.h"
00049 #include "pgmultilineedit.h"
00050 #include "pgtooltiphelp.h"
00051 
00052 
00053 #include "global.h"
00054 
00055 #include "windowing.h"
00056 
00057 #include "gameoptions.h"
00058 #include "spfst.h"
00059 
00060 
00061 #include "resourceplacement.h"
00062 
00063 #include "iconrepository.h"
00064 #include "graphics/drawing.h"
00065 #include "widgets/textrenderer.h"
00066 
00067 #include "util/messaginghub.h"
00068 
00069 
00070 const int widgetTypeNum = 17;
00071 const char* widgetTypes[widgetTypeNum]
00072 =
00073    { "image",
00074      "area",
00075      "statictext",
00076      "textoutput",
00077      "bargraph",
00078      "specialDisplay",
00079      "specialInput",
00080      "dummy",
00081      "multilinetext",
00082      "scrollarea",
00083      "button",
00084      "radiobutton",
00085      "checkbox",
00086      "lineedit",
00087      "slider",
00088      "plain",
00089      "listbox"
00090    };
00091 
00092 enum  WidgetTypes  { Image,
00093                      Area,
00094                      StaticLabel,
00095                      TextOutput,
00096                      BarGraph,
00097                      SpecialDisplay,
00098                      SpecialInput,
00099                      Dummy,
00100                      MultiLineText,
00101                      ScrollArea,
00102                      Button,
00103                      RadioButton,
00104                      CheckBox,
00105                      LineEdit,
00106                      Slider,
00107                      Plain,
00108                      ListBox };
00109 
00110 const int imageModeNum = 5;
00111 const char* imageModes[imageModeNum]
00112     = { "tile",
00113         "stretch",
00114         "tile3h",
00115         "tile3v",
00116         "tile9" };
00117 
00118 
00119 const int textAlignNum = 3;
00120 const char* textAlignment[textAlignNum]
00121 =
00122    { "left",
00123      "center",
00124      "right"
00125    };
00126 
00127 
00128 const int barDirectionNum = 4;
00129 const char* barDirections[barDirectionNum]
00130 =
00131    { "left2right",
00132      "right2left",
00133      "top2buttom",
00134      "buttom2top"
00135    };
00136 
00137 const int sliderDirectionNum = 2;
00138 const char* sliderDirections[sliderDirectionNum]
00139 =
00140    { "vertical",
00141      "horizontal"
00142    };
00143    
00144 
00145 
00146 ASCGUI_Window::WidgetParameters::WidgetParameters()
00147       : backgroundMode(PG_Draw::TILE),  
00148         textAlign( PG_Label::LEFT ), textAlign_defined(false),
00149         fontColor(0xffffff), fontColor_defined(false),
00150         fontAlpha(255), fontAlpha_defined(false),
00151         fontSize(8), fontSize_defined(false),
00152         backgroundColor_defined(false),
00153         transparency(0), transparency_defined(false),
00154         bordersize(1), bordersize_defined(false),
00155         hidden(false)
00156 {
00157 }
00158 
00159 int ASCGUI_Window::RunModal()
00160 {
00161    WindowCounter wc;
00162    return PG_Window::RunModal();
00163 }
00164 
00165 
00166 void  ASCGUI_Window::WidgetParameters::runTextIO ( PropertyReadingContainer& pc )
00167 {
00168    if ( pc.find( "BackgroundImage" )) {
00169       pc.addString( "BackgroundImage", backgroundImage, backgroundImage );
00170       int i = backgroundMode;
00171       pc.addNamedInteger( "BackgroundMode", i, imageModeNum, imageModes, i);
00172       backgroundMode = PG_Draw::BkMode( i );
00173    }
00174 
00175    if ( pc.find( "TextAlign" )) {
00176       int ta = textAlign;
00177       pc.addNamedInteger( "TextAlign", ta, textAlignNum, textAlignment, ta );
00178       textAlign = PG_Label::TextAlign( ta );
00179       textAlign_defined = true;
00180    }
00181 
00182    if ( pc.find( "FontColor")) {
00183       pc.addInteger("FontColor", fontColor, fontColor );
00184       fontColor_defined = true;
00185    }
00186    
00187    pc.addString("FontName", fontName, fontName );
00188    
00189    if ( pc.find( "FontAlpha")) {
00190       pc.addInteger("FontAlpha", fontAlpha, fontAlpha );
00191       fontAlpha_defined = true;
00192    }    
00193    
00194    if ( pc.find( "FontSize")) {
00195       pc.addInteger("FontSize", fontSize, fontSize );
00196       fontSize_defined = true;
00197    }
00198    
00199    if ( pc.find( "BackgroundColor" )) {
00200       pc.addInteger("BackgroundColor", backgroundColor,  backgroundColor );
00201       backgroundImage.clear();
00202       backgroundColor_defined = true;
00203    };
00204    
00205    if ( pc.find("Transparency")) {
00206       pc.addInteger("Transparency", transparency, transparency );
00207       transparency_defined = true;
00208    }
00209    
00210    if ( pc.find("Bordersize")) {
00211       pc.addInteger("Bordersize", bordersize, bordersize );
00212       bordersize_defined = true;
00213    }
00214 
00215    pc.addBool( "hidden", hidden, hidden );
00216    pc.addString("Style", style, style );
00217 }
00218 
00219 
00220 void  ASCGUI_Window::WidgetParameters::assign( BarGraphWidget* widget )
00221 {
00222    if ( !widget )
00223       return;
00224 
00225    if ( backgroundColor_defined )
00226       widget->setColor( backgroundColor );
00227 
00228    assign( (PG_ThemeWidget*)widget );
00229 
00230 }
00231 
00232 
00233 bool hasTransparency( const Surface& surf )
00234 {
00235    if ( surf.GetPixelFormat().BytesPerPixel() == 4 ) {
00236       const Uint32* p = (const Uint32*) surf.pixels();
00237       int aMask = surf.GetPixelFormat().Amask();
00238       int aShift = surf.GetPixelFormat().Ashift();
00239       for ( int y = 0; y < surf.h() ; ++y ) {
00240          for ( int x = 0; x < surf.w() ; ++x)
00241             if ( ((p[x] & aMask) >> aShift ) != Surface::opaque )
00242                return true;
00243          p += surf.pitch() / 4;
00244       }
00245    }
00246    return false;
00247 }
00248 
00249 
00250 void  ASCGUI_Window::WidgetParameters::assign( PG_ThemeWidget* widget )
00251 {
00252    if ( !widget )
00253       return;
00254 
00255    
00256    if ( !backgroundImage.empty() ) {
00257       widget->SetBackground( IconRepository::getIcon(backgroundImage).getBaseSurface(), backgroundMode );
00258       widget->SetDirtyUpdate( !hasTransparency( IconRepository::getIcon(backgroundImage) ));
00259    } else {
00260       if ( backgroundColor_defined ) {
00261          widget->SetBackground( NULL );
00262          widget->SetSimpleBackground( true );
00263       }
00264    }
00265 
00266    if ( backgroundColor_defined )
00267       widget->SetBackgroundColor( backgroundColor );
00268       
00269    assign( (PG_Widget*)widget );
00270 }
00271 
00272 void  ASCGUI_Window::WidgetParameters::assign( PG_Label* widget )
00273 {
00274    if ( !widget )
00275       return;
00276 
00277    if ( textAlign_defined )
00278       widget->SetAlignment( textAlign );
00279 
00280    assign( (PG_Widget*)widget );
00281 }
00282 
00283 
00284 void  ASCGUI_Window::WidgetParameters::assign( PG_Widget* widget )
00285 {
00286    if ( !widget )
00287       return;
00288 
00289    if ( fontColor_defined ) 
00290       widget->SetFontColor( fontColor, true );
00291       
00292    if ( !fontName.empty() )
00293       widget->SetFontName( fontName, true );
00294       
00295    if ( fontAlpha_defined )
00296       widget->SetFontAlpha( fontAlpha, true );
00297    
00298    if ( fontSize_defined )
00299       widget->SetFontSize( fontSize, true );
00300    
00301    if ( transparency_defined )
00302       widget->SetTransparency( transparency, true );
00303 
00304    if( bordersize_defined )
00305       widget->SetBorderSize( bordersize );
00306    
00307    if ( hidden )
00308       widget->Hide(false);
00309 }
00310 
00311 
00312 ASCGUI_Window::ASCGUI_Window ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, const ASCString& baseStyle, bool loadTheme )
00313    : PG_Window ( parent, ASC_PG_Dialog::centerRectangle(r), "", DEFAULT, baseStyle, 9 ), panelName( panelName_ ), textPropertyGroup(NULL)
00314 {
00315       // FIXME Hide button does not delete Panel      
00316 }
00317 
00318 
00319 PG_Rect ASCGUI_Window::parseRect ( PropertyReadingContainer& pc, PG_Widget* parent )
00320 {
00321    int x,y,w,h,x2,y2;
00322    // pc.openBracket( "position" );
00323    pc.addInteger( "x", x );
00324    pc.addInteger( "y", y );
00325    pc.addInteger( "width", w, 0 );
00326    pc.addInteger( "height", h, 0 );
00327    pc.addInteger( "x2", x2, 0 );
00328    pc.addInteger( "y2", y2, 0 );
00329    // pc.closeBracket();
00330 
00331    PG_Rect r ( x,y,w,h);
00332 
00333    if ( x < 0 )
00334       r.x = parent->Width() - w + x;
00335 
00336    if ( r.y < 0 )
00337       r.y = parent->Height() - h + y;
00338 
00339    if ( x2 != 0 ) {
00340       if ( x2 < 0 )
00341          x2 = parent->Width() + x2;
00342 
00343       w = x2 - r.x;
00344    }
00345 
00346    if ( y2 != 0 ) {
00347       if ( y2 < 0 )
00348          y2 = parent->Height() + y2;
00349 
00350       h = y2 - r.y;
00351    }
00352 
00353 
00354    if ( w <= 0 )
00355       r.w = parent->Width() - r.x;
00356    else
00357       r.w = w;
00358 
00359    if ( h <= 0 )
00360       r.h = parent->Height() - r.y;
00361    else
00362       r.h = h;
00363 
00364    return r;
00365 }
00366 
00367 
00368 
00369 
00370 void ASCGUI_Window::parsePanelASCTXT ( PropertyReadingContainer& pc, PG_Widget* parent, WidgetParameters widgetParams )
00371 {
00372    ASCString name;
00373    pc.addString( "name", name, "" );
00374    parent->SetName( name );
00375    
00376    int transparency;
00377    pc.addInteger("localtransparency", transparency, -1 );
00378    if ( transparency != -1 ) {
00379       parent->SetTransparency( transparency );
00380    }
00381    
00382    if ( pc.find( "userHandler" )) {
00383       ASCString label;
00384       pc.addString( "userHandler", label);
00385       userHandler( label, pc, parent, widgetParams );
00386    }
00387 
00388 
00389    vector<ASCString> childNames;
00390 
00391 
00392    if ( pc.find( "ChildWidgets" )) 
00393       pc.addStringArray( "ChildWidgets", childNames );
00394 
00395 
00396    int widgetNum;
00397    pc.addInteger( "WidgetNum", widgetNum, 0 );
00398 
00399    for ( int i = 0; i < widgetNum; ++i) 
00400       childNames.push_back( ASCString("Widget") + ASCString::toString( i ));
00401 
00402       
00403    PG_RadioButton* radioButtonGroup = NULL;
00404 
00405 
00406    WidgetParameters originalWidgetParams = widgetParams;
00407             
00408    for ( int i = 0; i < childNames.size(); ++i) {
00409 
00410       widgetParams  = originalWidgetParams;
00411       
00412       pc.openBracket( childNames[i] );
00413 
00414       ASCString toolTipHelp;
00415       pc.addString( "ToolTipHelp", toolTipHelp, "" );
00416       
00417 
00418       PG_Rect r = parseRect( pc, parent );
00419 
00420       widgetParams.runTextIO( pc );
00421       
00422       bool hasStyle = pc.find( "style" );
00423       ASCString style;
00424       pc.addString( "style", style, "Panel" );
00425 
00426 
00427       int type;
00428       pc.addNamedInteger( "type", type, widgetTypeNum, widgetTypes );
00429 
00430       PG_Widget* newWidget = NULL;
00431       
00432       if ( type == Image ) {
00433          ASCString filename;
00434          pc.addString( "FileName", filename, "" );
00435          int imgMode;
00436          pc.addNamedInteger( "mode", imgMode, imageModeNum, imageModes, 0 );
00437 
00438          
00439          if ( !filename.empty() ) {
00440             try {
00441                Surface& surf = IconRepository::getIcon(filename);
00442                PG_Image* img = new PG_Image( parent, PG_Point(r.x, r.y ), surf.getBaseSurface(), false, PG_Draw::BkMode(imgMode) );
00443                newWidget = img;
00444                img->SetDirtyUpdate( !hasTransparency( surf ));
00445 
00446                widgetParams.assign ( img );
00447                parsePanelASCTXT( pc, img, widgetParams );
00448             } catch ( tfileerror ) {
00449                warningMessage( "unable to load " + filename );
00450             }
00451          } else {
00452             PG_Image* img = new PG_Image( parent, PG_Point(r.x, r.y ), NULL, false, PG_Draw::BkMode(imgMode) );
00453             newWidget = img;
00454             widgetParams.assign ( img );
00455             parsePanelASCTXT( pc, img, widgetParams );
00456          }
00457       }  else
00458       if ( type == Area ) {
00459          bool mode;
00460          pc.addBool( "in", mode, true );
00461 
00462          Emboss* tw = new Emboss ( parent, r, mode );
00463          // PG_ThemeWidget* tw = new PG_ThemeWidget ( parent, r, style );
00464          widgetParams.assign ( tw );
00465          parsePanelASCTXT( pc, tw, widgetParams );
00466          newWidget = tw;
00467       } else
00468       if ( type == StaticLabel ) {
00469          ASCString text;
00470          pc.addString( "text", text );
00471 
00472          PG_Label* lb = new PG_Label ( parent, r, text, style );
00473          if ( !hasStyle )
00474             widgetParams.assign ( lb );
00475          parsePanelASCTXT( pc, lb, widgetParams );
00476          newWidget = lb;
00477       } else
00478       if ( type == TextOutput ) {
00479          PG_Label* lb = new PG_Label ( parent, r, PG_NULLSTR, style );
00480 
00481          if ( !hasStyle )
00482             widgetParams.assign ( lb );
00483          parsePanelASCTXT( pc, lb, widgetParams );
00484          newWidget = lb;
00485       } else
00486       if ( type == MultiLineText ) {
00487          /*
00488          PG_MultiLineEdit* lb = new PG_MultiLineEdit ( parent, r, style );
00489 
00490          lb->SetEditable(false);
00491          if ( !hasStyle ) {
00492             lb->SetBorderSize(0);
00493             widgetParams.assign ( lb );
00494          }
00495          */
00496 
00497          TextRenderer* tr = new TextRenderer( parent, r, "", style );
00498          if ( !hasStyle ) 
00499             widgetParams.assign ( tr );
00500          
00501          parsePanelASCTXT( pc, tr, widgetParams );
00502          newWidget = tr;
00503       } else
00504       if ( type == BarGraph ) {
00505          int dir;
00506          pc.addNamedInteger( "direction", dir, barDirectionNum, barDirections, 0 );
00507 
00508          int cnum;
00509          pc.addInteger("Colors",cnum, 0 );
00510          BarGraphWidget::Colors colorRange;
00511          for ( int i = 0; i < cnum; ++i ) {
00512             pc.openBracket("Color" + ASCString::toString(i));
00513             int col;
00514             pc.addInteger("color", col);
00515             double f;
00516             pc.addDFloat("fraction", f );
00517             colorRange.push_back( make_pair(f,col) );
00518             pc.closeBracket();
00519          }
00520 
00521          BarGraphWidget* bg = new BarGraphWidget ( parent, r, BarGraphWidget::Direction(dir) );
00522          bg->setColor( colorRange );
00523          widgetParams.assign ( bg );
00524          parsePanelASCTXT( pc, bg, widgetParams );
00525          newWidget = bg;
00526       } else
00527       if ( type == SpecialDisplay ) {
00528          SpecialDisplayWidget* sw = new SpecialDisplayWidget ( parent, r );
00529          widgetParams.assign ( sw );
00530 
00531          parsePanelASCTXT( pc, sw, widgetParams );
00532          newWidget = sw;
00533       } else
00534       if ( type == SpecialInput ) {
00535          SpecialInputWidget* sw = new SpecialInputWidget ( parent, r );
00536 
00537          parsePanelASCTXT( pc, sw, widgetParams );
00538          newWidget = sw;
00539       } else
00540       if ( type == Dummy ) {
00541          SpecialInputWidget* sw = new SpecialInputWidget ( parent, r );
00542          parsePanelASCTXT( pc, sw, widgetParams );
00543          newWidget = sw;
00544       } else
00545       if ( type == ScrollArea ) {
00546          PG_ScrollWidget* sw = new PG_ScrollWidget( parent, r, style );
00547          ASCString scrollbar;
00548          pc.addString( "horizontal_scollbar", scrollbar, "true" );
00549          if ( scrollbar.compare_ci( "false" ) == 0)
00550             sw->EnableScrollBar(false, PG_ScrollBar::HORIZONTAL );
00551 
00552          pc.addString( "vertical_scollbar", scrollbar, "true" );
00553          if ( scrollbar.compare_ci( "false" ) == 0)
00554             sw->EnableScrollBar(false, PG_ScrollBar::VERTICAL );
00555 
00556          if ( !hasStyle )
00557             widgetParams.assign ( sw );
00558             
00559          parsePanelASCTXT( pc, sw, widgetParams );
00560          newWidget = sw;
00561       } else
00562       if ( type == Button ) {
00563          PG_Button* sw = new PG_Button( parent, r, style );
00564 
00565          ASCString text;
00566          pc.addString( "text", text, "" );
00567          
00568          // if ( !text.empty() )
00569             sw->SetText( text );
00570          
00571          
00572          if ( !hasStyle )
00573             widgetParams.assign ( sw );
00574             
00575          parsePanelASCTXT( pc, sw, widgetParams );
00576          newWidget = sw;
00577       } else
00578       if ( type == RadioButton ) {
00579          PG_RadioButton* sw = new PG_RadioButton( parent, r, style );
00580          if ( radioButtonGroup )
00581             radioButtonGroup->AddToGroup( sw );
00582             
00583          radioButtonGroup = sw;
00584 
00585          ASCString text;
00586          pc.addString( "text", text );
00587          
00588          if ( !text.empty() )
00589             sw->SetText( text );
00590          
00591          if ( !hasStyle )
00592             widgetParams.assign ( sw );
00593             
00594          parsePanelASCTXT( pc, sw, widgetParams );
00595          newWidget = sw;
00596       } else
00597       if ( type == CheckBox ) {
00598          PG_CheckButton* sw = new PG_CheckButton( parent, r, style );
00599 
00600          ASCString text;
00601          pc.addString( "text", text );
00602          
00603          if ( !text.empty() )
00604             sw->SetText( text );
00605          
00606          if ( !hasStyle )
00607             widgetParams.assign ( sw );
00608             
00609          parsePanelASCTXT( pc, sw, widgetParams );
00610          newWidget = sw;
00611       } else
00612       if ( type == LineEdit ) {
00613          PG_LineEdit* sw = new PG_LineEdit( parent, r, style );
00614          
00615          ASCString text;
00616          pc.addString( "text", text, "" );
00617          
00618          if ( !text.empty() )
00619             sw->SetText( text );
00620          
00621          if ( !hasStyle )
00622             widgetParams.assign ( sw );
00623             
00624          parsePanelASCTXT( pc, sw, widgetParams );
00625          newWidget = sw;
00626       } else
00627       if ( type == Slider ) {
00628          
00629          int direction = 0;
00630          pc.addNamedInteger( "Orientation", direction, sliderDirectionNum, sliderDirections, direction);
00631         
00632          
00633          PG_ScrollBar* sb = new PG_Slider( parent, r, PG_ScrollBar::ScrollDirection(direction), -1, style );
00634          
00635          if ( !hasStyle )
00636             widgetParams.assign ( sb );
00637             
00638          parsePanelASCTXT( pc, sb, widgetParams );
00639          newWidget = sb;
00640       } else
00641       if ( type == Plain ) {
00642          
00643          PG_ThemeWidget* w = new PG_ThemeWidget( parent, r );
00644          
00645          if ( !hasStyle )
00646             widgetParams.assign ( w );
00647             
00648          parsePanelASCTXT( pc, w, widgetParams );
00649          newWidget = w;
00650       } else
00651       if ( type == ListBox) {
00652          PG_ListBox* l = new PG_ListBox( parent, r );
00653          
00654          if ( !hasStyle )
00655             widgetParams.assign ( l );
00656             
00657          parsePanelASCTXT( pc, l, widgetParams );
00658          newWidget = l;
00659       }
00660             
00661       if ( newWidget && newWidget->GetName().empty() ) 
00662          newWidget->SetName( childNames[i] );
00663       
00664       if ( newWidget && newWidget->GetName() == "$pos" )
00665          newWidget->SetName( pc.getNameStack() );
00666       
00667 
00668       if ( newWidget && !toolTipHelp.empty() )
00669          new PG_ToolTipHelp( newWidget, toolTipHelp );
00670 
00671       pc.closeBracket();
00672    }
00673 }
00674 
00675 void ASCGUI_Window::rename( const ASCString& widgetName, const ASCString& newname, PG_Widget* parent )
00676 {
00677    if ( !parent )
00678       parent = this;
00679 
00680    PG_Widget* w = parent->FindChild( widgetName, true );
00681    if ( w )
00682       w->SetName( newname );
00683 }
00684 
00685 void ASCGUI_Window::setLabelText ( const ASCString& widgetName, const ASCString& text, PG_Widget* parent )
00686 {
00687    if ( !parent )
00688       parent = this;
00689 
00690    PG_Label* l = dynamic_cast<PG_Label*>( parent->FindChild( widgetName, true ) );
00691    if ( l )
00692       l->SetText( text );
00693    else {
00694       PG_LineEdit* l = dynamic_cast<PG_LineEdit*>( parent->FindChild( widgetName, true ) );
00695       if ( l )
00696          l->SetText( text );
00697       else {
00698          PG_MultiLineEdit* l = dynamic_cast<PG_MultiLineEdit*>( parent->FindChild( widgetName, true ) );
00699          if ( l )
00700             l->SetText( text );
00701          else {
00702             TextRenderer* l = dynamic_cast<TextRenderer*>( parent->FindChild( widgetName, true ) );
00703             if ( l )
00704                l->SetText( text );
00705          }
00706       }
00707    }
00708 }
00709 
00710 void ASCGUI_Window::setLabelColor ( const ASCString& widgetName, PG_Color color, PG_Widget* parent )
00711 {
00712    if ( !parent )
00713       parent = this;
00714 
00715    PG_Label* l = dynamic_cast<PG_Label*>( parent->FindChild( widgetName, true ) );
00716    if ( l )
00717       l->SetFontColor ( color );
00718    else {
00719       PG_MultiLineEdit* l = dynamic_cast<PG_MultiLineEdit*>( parent->FindChild( widgetName, true ) );
00720       if ( l )
00721          l->SetFontColor ( color );
00722    }
00723 }
00724 
00725 
00726 void ASCGUI_Window::setLabelText ( const ASCString& widgetName, int i, PG_Widget* parent )
00727 {
00728    ASCString s = ASCString::toString(i);
00729    setLabelText ( widgetName, s, parent );
00730 }
00731 
00732 
00733 void ASCGUI_Window::setImage ( const ASCString& widgetName, const ASCString& filename, PG_Widget* parent )
00734 {
00735    setImage( widgetName, IconRepository::getIcon( filename ), parent );
00736 }
00737 
00738 
00739 void ASCGUI_Window::setImage ( const ASCString& widgetName, Surface& image, PG_Widget* parent )
00740 {
00741    setImage( widgetName, image.getBaseSurface(), parent);
00742 }
00743 
00744 void ASCGUI_Window::setImage ( const ASCString& widgetName, SDL_Surface* image, PG_Widget* parent )
00745 {
00746    if ( !parent )
00747       parent = this;
00748 
00749    PG_Image* i = dynamic_cast<PG_Image*>( parent->FindChild( widgetName, true ) );
00750    if ( i ) {
00751       i->SetImage( image, false );
00752       if ( image )
00753          i->SizeWidget( image->w, image->h);
00754    }
00755 }
00756 
00757 void ASCGUI_Window::setWidgetTransparency ( const ASCString& widgetName, int transparency, PG_Widget* parent  )
00758 {
00759    if ( !parent )
00760       parent = this;
00761 
00762    PG_Widget* i = parent->FindChild( widgetName, true );
00763    if ( i )
00764       i->SetTransparency( transparency );
00765 }
00766 
00767 
00768 void ASCGUI_Window::hide ( const ASCString& widgetName, PG_Widget* parent )
00769 {
00770    if ( !parent )
00771       parent = this;
00772 
00773    PG_Widget* i = parent->FindChild( widgetName, true );
00774    if ( i )
00775       i->Hide();
00776 }
00777 
00778 void ASCGUI_Window::show ( const ASCString& widgetName, PG_Widget* parent )
00779 {
00780    if ( !parent )
00781       parent = this;
00782 
00783    PG_Widget* i = parent->FindChild( widgetName, true );
00784    if ( i )
00785       i->Show();
00786 }
00787 
00788 
00789 
00790 void ASCGUI_Window::setBargraphValue( const ASCString& widgetName, float fraction, PG_Widget* parent )
00791 {
00792    if ( !parent )
00793       parent = this;
00794 
00795    BarGraphWidget* bgw = dynamic_cast<BarGraphWidget*>( parent->FindChild( widgetName, true ) );
00796    if ( bgw )
00797       bgw->setFraction( fraction );
00798 }
00799 
00800 
00801 void ASCGUI_Window::setBarGraphColor( const ASCString& widgetName, PG_Color color, PG_Widget* parent )
00802 {
00803    if ( !parent )
00804       parent = this;
00805 
00806    BarGraphWidget* bgw = dynamic_cast<BarGraphWidget*>( parent->FindChild( widgetName, true ) );
00807    if ( bgw )
00808       bgw->setColor( color );
00809 }
00810 
00811 
00812 
00813 ASCGUI_Window::GuiCache ASCGUI_Window::guiCache;
00814 
00815 
00816 bool ASCGUI_Window::setup()
00817 {
00818    try {
00819       WidgetParameters widgetParameters = getDefaultWidgetParams();
00820 
00821       panelName.toLower();
00822 
00823       if ( !CGameOptions::Instance()->cacheASCGUI || guiCache.find(panelName) == guiCache.end() ) {
00824          tnfilestream s ( panelName + ".ascgui", tnstream::reading );
00825 
00826          TextFormatParser tfp ( &s );
00827          textPropertyGroup = tfp.run();
00828 
00829          if ( CGameOptions::Instance()->cacheASCGUI ) 
00830             guiCache[panelName] = textPropertyGroup;
00831       } else 
00832          textPropertyGroup = guiCache[panelName];
00833 
00834       PropertyReadingContainer pc ( "panel", textPropertyGroup );
00835 
00836       
00837      
00838       int w = 0;
00839       int h = 0;
00840       pc.addInteger( "width", w, 0 );
00841       pc.addInteger( "height", h, 0 );
00842 
00843       if ( w > 0 && h > 0 )
00844          SizeWidget( w, h, false );
00845       else
00846          if ( h > 0 )
00847             SizeWidget( Width(), h, false );
00848          else
00849             if ( w > 0 )
00850                SizeWidget( w, Height(), false );
00851 
00852 
00853       if ( pc.find("x") && pc.find("y" )) {
00854          int x1,y1;
00855          pc.addInteger( "x", x1 );
00856          pc.addInteger( "y", y1 );
00857 
00858          if ( x1 < 0 && GetParent() )
00859             x1 = GetParent()->Width() - Width() + x1;
00860 
00861          if ( y1 < 0 && GetParent() )
00862             y1 = GetParent()->Height() - Height() + y1;
00863 
00864          int xofs = GetParent() ? GetParent()->my_xpos : 0;
00865          int yofs = GetParent() ? GetParent()->my_ypos : 0;
00866 
00867          if ( x1 + xofs + Width() > PG_Application::GetScreenWidth() )
00868             x1 = max ( PG_Application::GetScreenWidth() - Width() - xofs, 0 );
00869 
00870          if ( y1 + yofs + Height() > PG_Application::GetScreenHeight() )
00871             y1 = max ( PG_Application::GetScreenHeight() - Height() - yofs, 0 );
00872 
00873          MoveWidget( x1, y1, false );
00874       }
00875 
00876       int w2 = Width();
00877       int h2 = Height();
00878       
00879       if ( w == 0 ) {
00880          int x2 ;
00881          pc.addInteger( "x2", x2, 0 );
00882          
00883          if ( x2 < 0 ) 
00884             w2 = PG_Application::GetScreenWidth() - my_xpos + x2;
00885          else
00886             if ( x2 > 0 )
00887                w2 = my_xpos + x2;
00888       }
00889       
00890       if ( h == 0 ) {
00891          int y2;
00892          pc.addInteger( "y2", y2, 0 );
00893          if ( y2 < 0 )
00894             h2 = PG_Application::GetScreenHeight() - my_ypos + y2;
00895          else
00896             if ( y2 > 0 )
00897                h2 = my_ypos + y2;
00898       }
00899       
00900       if ( h2 != Height() || w2 != Width() )
00901          SizeWidget( w2, h2, false );
00902          
00903       
00904       
00905       int titlebarHeight;
00906       pc.addInteger("Titlebarheight", titlebarHeight, -1 );
00907       if ( titlebarHeight != -1 )
00908          SetTitlebarHeight( titlebarHeight );
00909 
00910       ASCString title;
00911       pc.addString("Title", title, "" );
00912       if ( !title.empty() )
00913          SetTitle( title );
00914          
00915       widgetParameters.runTextIO( pc );
00916       widgetParameters.assign ( this );
00917 
00918       parsePanelASCTXT( pc, this, widgetParameters );
00919 
00920       return true;
00921    } catch ( ParsingError err ) {
00922       warningMessage( "parsing error: " + err.getMessage());
00923    } catch ( tfileerror err ) {
00924       warningMessage( "could not acces file: " + err.getFileName() );
00925    }
00926    return false;
00927 
00928 }
00929 
00930 
00931 void ASCGUI_Window::destruct()
00932 {
00933    if ( textPropertyGroup ) {
00934       for ( GuiCache::iterator i = guiCache.begin(); i != guiCache.end(); ++i )
00935          if ( i->second == textPropertyGroup )
00936             return;
00937 
00938       delete textPropertyGroup;
00939    }
00940 }
00941 
00942 ASCGUI_Window::~ASCGUI_Window()
00943 {
00944    destruct();
00945 }
00946 
00947 
00948 Panel :: Panel ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
00949        : ASCGUI_Window( parent, r, panelName_, "Panel", loadTheme )
00950 {
00951    if ( loadTheme )
00952       setup();
00953       
00954    BringToFront();
00955 }
00956 
00957 ASCGUI_Window::WidgetParameters Panel::getDefaultWidgetParams()
00958 {
00959     static WidgetParameters defaultWidgetParameters;
00960     static ASCString panelBackgroundImage;
00961     static bool defaultsLoaded = false;
00962     if ( !defaultsLoaded ) {
00963        tnfilestream s ( "default.ascgui", tnstream::reading );
00964 
00965        TextFormatParser tfp ( &s );
00966        auto_ptr<TextPropertyGroup> tpg ( tfp.run());
00967 
00968        PropertyReadingContainer pc ( "panel", tpg.get() );
00969 
00970        defaultWidgetParameters.runTextIO ( pc );
00971        pc.addString("PanelBackgroundImage", panelBackgroundImage );
00972        defaultsLoaded = true;
00973     }
00974     return defaultWidgetParameters;
00975 }
00976 
00977 
00978 
00979 LayoutablePanel :: LayoutablePanel ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
00980    : Panel ( parent, PositionCalculator( r, panelName_), panelName_, loadTheme ), name( panelName_ )
00981 {
00982 }
00983 
00984 
00985 PG_Rect LayoutablePanel::PositionCalculator( const PG_Rect& rect, const ASCString& panelName )
00986 {
00987    CGameOptions::PanelData pd;
00988    if ( CGameOptions::Instance()->getPanelData(panelName, pd) ) {
00989       pd.x = PG_Application::GetScreenWidth() - pd.x;
00990       PG_Rect r ( pd.x, pd.y, DashBoardWidth, rect.Height() );
00991       
00992       if ( r.x + r.w >  PG_Application::GetScreenWidth() )
00993          r.x = PG_Application::GetScreenWidth() - r.w;
00994          
00995       if ( r.y + r.h >  PG_Application::GetScreenHeight() )
00996          r.y = PG_Application::GetScreenHeight() - r.h;
00997       
00998       return r;
00999    } else
01000       return PG_Rect( rect.x, rect.y, DashBoardWidth, rect.Height() );
01001 }
01002 
01003 void LayoutablePanel::eventMoveWidget (int x, int y)
01004 {
01005    Panel::eventMoveWidget(x,y);
01006    CGameOptions::PanelData pos;
01007    pos.x = PG_Application::GetScreenWidth() - x;
01008    pos.y = y;
01009    pos.visible = true;
01010 
01011    CGameOptions::Instance()->updatePanelData( name, pos );
01012 }
01013 
01014 void LayoutablePanel::eventHide()
01015 {
01016    Panel::eventHide();
01017    
01018    CGameOptions::PanelData pos;
01019    pos.x = PG_Application::GetScreenWidth() - my_xpos;
01020    pos.y = my_ypos;
01021    pos.visible = false;
01022 
01023    CGameOptions::Instance()->updatePanelData( name, pos );
01024 }
01025 
01026 void LayoutablePanel::eventShow()
01027 {
01028    Panel::eventShow();
01029    
01030    CGameOptions::PanelData pos;
01031    pos.x = PG_Application::GetScreenWidth() - my_xpos;
01032    pos.y = my_ypos;
01033    pos.visible = true;
01034 
01035    CGameOptions::Instance()->updatePanelData( name, pos );
01036 }
01037 
01038 
01039 
01040 
01041 
01042 
01043 ConfigurableWindow :: ConfigurableWindow ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
01044        : ASCGUI_Window( parent, r, panelName_, "Window", loadTheme )
01045 {
01046    if ( loadTheme )
01047       setup();
01048       
01049    BringToFront();
01050 }
01051 
01052 ASCGUI_Window::WidgetParameters ConfigurableWindow ::getDefaultWidgetParams()
01053 {
01054    return  WidgetParameters();
01055 }
01056 

Generated on Mon May 21 01:26:40 2012 for Advanced Strategic Command by  doxygen 1.5.1