Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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 "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                warning( "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 
00458       }
00459       if ( type == Area ) {
00460          bool mode;
00461          pc.addBool( "in", mode, true );
00462 
00463          Emboss* tw = new Emboss ( parent, r, mode );
00464          // PG_ThemeWidget* tw = new PG_ThemeWidget ( parent, r, style );
00465          widgetParams.assign ( tw );
00466          parsePanelASCTXT( pc, tw, widgetParams );
00467          newWidget = tw;
00468       }
00469 
00470       if ( type == StaticLabel ) {
00471          ASCString text;
00472          pc.addString( "text", text );
00473 
00474          PG_Label* lb = new PG_Label ( parent, r, text, style );
00475          if ( !hasStyle )
00476             widgetParams.assign ( lb );
00477          parsePanelASCTXT( pc, lb, widgetParams );
00478          newWidget = lb;
00479       }
00480       if ( type == TextOutput ) {
00481          PG_Label* lb = new PG_Label ( parent, r, PG_NULLSTR, style );
00482 
00483          if ( !hasStyle )
00484             widgetParams.assign ( lb );
00485          parsePanelASCTXT( pc, lb, widgetParams );
00486          newWidget = lb;
00487       }
00488       if ( type == MultiLineText ) {
00489          /*
00490          PG_MultiLineEdit* lb = new PG_MultiLineEdit ( parent, r, style );
00491 
00492          lb->SetEditable(false);
00493          if ( !hasStyle ) {
00494             lb->SetBorderSize(0);
00495             widgetParams.assign ( lb );
00496          }
00497          */
00498 
00499          TextRenderer* tr = new TextRenderer( parent, r, "", style );
00500          if ( !hasStyle ) 
00501             widgetParams.assign ( tr );
00502          
00503          parsePanelASCTXT( pc, tr, widgetParams );
00504          newWidget = tr;
00505       }
00506 
00507       if ( type == BarGraph ) {
00508          int dir;
00509          pc.addNamedInteger( "direction", dir, barDirectionNum, barDirections, 0 );
00510 
00511          int cnum;
00512          pc.addInteger("Colors",cnum, 0 );
00513          BarGraphWidget::Colors colorRange;
00514          for ( int i = 0; i < cnum; ++i ) {
00515             pc.openBracket("Color" + ASCString::toString(i));
00516             int col;
00517             pc.addInteger("color", col);
00518             double f;
00519             pc.addDFloat("fraction", f );
00520             colorRange.push_back( make_pair(f,col) );
00521             pc.closeBracket();
00522          }
00523 
00524          BarGraphWidget* bg = new BarGraphWidget ( parent, r, BarGraphWidget::Direction(dir) );
00525          bg->setColor( colorRange );
00526          widgetParams.assign ( bg );
00527          parsePanelASCTXT( pc, bg, widgetParams );
00528          newWidget = bg;
00529       }
00530 
00531 
00532       if ( type == SpecialDisplay ) {
00533          SpecialDisplayWidget* sw = new SpecialDisplayWidget ( parent, r );
00534          widgetParams.assign ( sw );
00535 
00536          parsePanelASCTXT( pc, sw, widgetParams );
00537          newWidget = sw;
00538       }
00539 
00540       if ( type == SpecialInput ) {
00541          SpecialInputWidget* sw = new SpecialInputWidget ( parent, r );
00542 
00543          parsePanelASCTXT( pc, sw, widgetParams );
00544          newWidget = sw;
00545       }
00546 
00547       if ( type == Dummy ) {
00548          SpecialInputWidget* sw = new SpecialInputWidget ( parent, r );
00549          parsePanelASCTXT( pc, sw, widgetParams );
00550          newWidget = sw;
00551       }
00552       if ( type == ScrollArea ) {
00553          PG_ScrollWidget* sw = new PG_ScrollWidget( parent, r, style );
00554          ASCString scrollbar;
00555          pc.addString( "horizontal_scollbar", scrollbar, "true" );
00556          if ( scrollbar.compare_ci( "false" ) == 0)
00557             sw->EnableScrollBar(false, PG_ScrollBar::HORIZONTAL );
00558 
00559          pc.addString( "vertical_scollbar", scrollbar, "true" );
00560          if ( scrollbar.compare_ci( "false" ) == 0)
00561             sw->EnableScrollBar(false, PG_ScrollBar::VERTICAL );
00562 
00563          if ( !hasStyle )
00564             widgetParams.assign ( sw );
00565             
00566          parsePanelASCTXT( pc, sw, widgetParams );
00567          newWidget = sw;
00568       }
00569       
00570       if ( type == Button ) {
00571          PG_Button* sw = new PG_Button( parent, r, style );
00572 
00573          ASCString text;
00574          pc.addString( "text", text, "" );
00575          
00576          // if ( !text.empty() )
00577             sw->SetText( text );
00578          
00579          
00580          if ( !hasStyle )
00581             widgetParams.assign ( sw );
00582             
00583          parsePanelASCTXT( pc, sw, widgetParams );
00584          newWidget = sw;
00585       }
00586 
00587       if ( type == RadioButton ) {
00588          PG_RadioButton* sw = new PG_RadioButton( parent, r, style );
00589          if ( radioButtonGroup )
00590             radioButtonGroup->AddToGroup( sw );
00591             
00592          radioButtonGroup = sw;
00593 
00594          ASCString text;
00595          pc.addString( "text", text );
00596          
00597          if ( !text.empty() )
00598             sw->SetText( text );
00599          
00600          if ( !hasStyle )
00601             widgetParams.assign ( sw );
00602             
00603          parsePanelASCTXT( pc, sw, widgetParams );
00604          newWidget = sw;
00605          
00606       }
00607       if ( type == CheckBox ) {
00608          PG_CheckButton* sw = new PG_CheckButton( parent, r, style );
00609 
00610          ASCString text;
00611          pc.addString( "text", text );
00612          
00613          if ( !text.empty() )
00614             sw->SetText( text );
00615          
00616          if ( !hasStyle )
00617             widgetParams.assign ( sw );
00618             
00619          parsePanelASCTXT( pc, sw, widgetParams );
00620          newWidget = sw;
00621          
00622       }
00623       if ( type == LineEdit ) {
00624          PG_LineEdit* sw = new PG_LineEdit( parent, r, style );
00625          
00626          ASCString text;
00627          pc.addString( "text", text, "" );
00628          
00629          if ( !text.empty() )
00630             sw->SetText( text );
00631          
00632          if ( !hasStyle )
00633             widgetParams.assign ( sw );
00634             
00635          parsePanelASCTXT( pc, sw, widgetParams );
00636          newWidget = sw;
00637       }
00638       if ( type == Slider ) {
00639          
00640          int direction = 0;
00641          pc.addNamedInteger( "Orientation", direction, sliderDirectionNum, sliderDirections, direction);
00642         
00643          
00644          PG_ScrollBar* sb = new PG_Slider( parent, r, PG_ScrollBar::ScrollDirection(direction), -1, style );
00645          
00646          if ( !hasStyle )
00647             widgetParams.assign ( sb );
00648             
00649          parsePanelASCTXT( pc, sb, widgetParams );
00650          newWidget = sb;
00651       }
00652       if ( type == Plain ) {
00653          
00654          PG_ThemeWidget* w = new PG_ThemeWidget( parent, r );
00655          
00656          if ( !hasStyle )
00657             widgetParams.assign ( w );
00658             
00659          parsePanelASCTXT( pc, w, widgetParams );
00660          newWidget = w;
00661       }
00662       
00663       if ( type == ListBox) {
00664          PG_ListBox* l = new PG_ListBox( parent, r );
00665          
00666          if ( !hasStyle )
00667             widgetParams.assign ( l );
00668             
00669          parsePanelASCTXT( pc, l, widgetParams );
00670          newWidget = l;
00671       }
00672             
00673       if ( newWidget && newWidget->GetName().empty() ) 
00674          newWidget->SetName( childNames[i] );
00675       
00676       if ( newWidget && newWidget->GetName() == "$pos" )
00677          newWidget->SetName( pc.getNameStack() );
00678       
00679 
00680       if ( newWidget && !toolTipHelp.empty() )
00681          new PG_ToolTipHelp( newWidget, toolTipHelp );
00682 
00683       pc.closeBracket();
00684    }
00685 }
00686 
00687 void ASCGUI_Window::rename( const ASCString& widgetName, const ASCString& newname, PG_Widget* parent )
00688 {
00689    if ( !parent )
00690       parent = this;
00691 
00692    PG_Widget* w = parent->FindChild( widgetName, true );
00693    if ( w )
00694       w->SetName( newname );
00695 }
00696 
00697 void ASCGUI_Window::setLabelText ( const ASCString& widgetName, const ASCString& text, PG_Widget* parent )
00698 {
00699    if ( !parent )
00700       parent = this;
00701 
00702    PG_Label* l = dynamic_cast<PG_Label*>( parent->FindChild( widgetName, true ) );
00703    if ( l )
00704       l->SetText( text );
00705    else {
00706       PG_LineEdit* l = dynamic_cast<PG_LineEdit*>( parent->FindChild( widgetName, true ) );
00707       if ( l )
00708          l->SetText( text );
00709       else {
00710          PG_MultiLineEdit* l = dynamic_cast<PG_MultiLineEdit*>( parent->FindChild( widgetName, true ) );
00711          if ( l )
00712             l->SetText( text );
00713          else {
00714             TextRenderer* l = dynamic_cast<TextRenderer*>( parent->FindChild( widgetName, true ) );
00715             if ( l )
00716                l->SetText( text );
00717          }
00718       }
00719    }
00720 }
00721 
00722 void ASCGUI_Window::setLabelColor ( const ASCString& widgetName, PG_Color color, PG_Widget* parent )
00723 {
00724    if ( !parent )
00725       parent = this;
00726 
00727    PG_Label* l = dynamic_cast<PG_Label*>( parent->FindChild( widgetName, true ) );
00728    if ( l )
00729       l->SetFontColor ( color );
00730    else {
00731       PG_MultiLineEdit* l = dynamic_cast<PG_MultiLineEdit*>( parent->FindChild( widgetName, true ) );
00732       if ( l )
00733          l->SetFontColor ( color );
00734    }
00735 }
00736 
00737 
00738 void ASCGUI_Window::setLabelText ( const ASCString& widgetName, int i, PG_Widget* parent )
00739 {
00740    ASCString s = ASCString::toString(i);
00741    setLabelText ( widgetName, s, parent );
00742 }
00743 
00744 
00745 void ASCGUI_Window::setImage ( const ASCString& widgetName, const ASCString& filename, PG_Widget* parent )
00746 {
00747    setImage( widgetName, IconRepository::getIcon( filename ), parent );
00748 }
00749 
00750 
00751 void ASCGUI_Window::setImage ( const ASCString& widgetName, Surface& image, PG_Widget* parent )
00752 {
00753    setImage( widgetName, image.getBaseSurface(), parent);
00754 }
00755 
00756 void ASCGUI_Window::setImage ( const ASCString& widgetName, SDL_Surface* image, PG_Widget* parent )
00757 {
00758    if ( !parent )
00759       parent = this;
00760 
00761    PG_Image* i = dynamic_cast<PG_Image*>( parent->FindChild( widgetName, true ) );
00762    if ( i ) {
00763       i->SetImage( image, false );
00764       if ( image )
00765          i->SizeWidget( image->w, image->h);
00766    }
00767 }
00768 
00769 void ASCGUI_Window::setWidgetTransparency ( const ASCString& widgetName, int transparency, PG_Widget* parent  )
00770 {
00771    if ( !parent )
00772       parent = this;
00773 
00774    PG_Widget* i = parent->FindChild( widgetName, true );
00775    if ( i )
00776       i->SetTransparency( transparency );
00777 }
00778 
00779 
00780 void ASCGUI_Window::hide ( const ASCString& widgetName, PG_Widget* parent )
00781 {
00782    if ( !parent )
00783       parent = this;
00784 
00785    PG_Widget* i = parent->FindChild( widgetName, true );
00786    if ( i )
00787       i->Hide();
00788 }
00789 
00790 void ASCGUI_Window::show ( const ASCString& widgetName, PG_Widget* parent )
00791 {
00792    if ( !parent )
00793       parent = this;
00794 
00795    PG_Widget* i = parent->FindChild( widgetName, true );
00796    if ( i )
00797       i->Show();
00798 }
00799 
00800 
00801 
00802 void ASCGUI_Window::setBargraphValue( const ASCString& widgetName, float fraction, PG_Widget* parent )
00803 {
00804    if ( !parent )
00805       parent = this;
00806 
00807    BarGraphWidget* bgw = dynamic_cast<BarGraphWidget*>( parent->FindChild( widgetName, true ) );
00808    if ( bgw )
00809       bgw->setFraction( fraction );
00810 }
00811 
00812 
00813 void ASCGUI_Window::setBarGraphColor( const ASCString& widgetName, PG_Color color, PG_Widget* parent )
00814 {
00815    if ( !parent )
00816       parent = this;
00817 
00818    BarGraphWidget* bgw = dynamic_cast<BarGraphWidget*>( parent->FindChild( widgetName, true ) );
00819    if ( bgw )
00820       bgw->setColor( color );
00821 }
00822 
00823 
00824 
00825 ASCGUI_Window::GuiCache ASCGUI_Window::guiCache;
00826 
00827 
00828 bool ASCGUI_Window::setup()
00829 {
00830    try {
00831       WidgetParameters widgetParameters = getDefaultWidgetParams();
00832 
00833       panelName.toLower();
00834 
00835       if ( !CGameOptions::Instance()->cacheASCGUI || guiCache.find(panelName) == guiCache.end() ) {
00836          tnfilestream s ( panelName + ".ascgui", tnstream::reading );
00837 
00838          TextFormatParser tfp ( &s );
00839          textPropertyGroup = tfp.run();
00840 
00841          if ( CGameOptions::Instance()->cacheASCGUI ) 
00842             guiCache[panelName] = textPropertyGroup;
00843       } else 
00844          textPropertyGroup = guiCache[panelName];
00845 
00846       PropertyReadingContainer pc ( "panel", textPropertyGroup );
00847 
00848       int w, h;
00849       pc.addInteger( "width", w, 0 );
00850       pc.addInteger( "height", h, 0 );
00851 
00852       if ( w > 0 && h > 0 )
00853          SizeWidget( w, h, false );
00854       else
00855          if ( h > 0 )
00856             SizeWidget( Width(), h, false );
00857          else
00858             if ( w > 0 )
00859                SizeWidget( w, Height(), false );
00860 
00861 
00862       if ( pc.find("x") && pc.find("y" )) {
00863          int x1,y1;
00864          pc.addInteger( "x", x1 );
00865          pc.addInteger( "y", y1 );
00866 
00867          if ( x1 < 0 && GetParent() )
00868             x1 = GetParent()->Width() - Width() + x1;
00869 
00870          if ( y1 < 0 && GetParent() )
00871             y1 = GetParent()->Height() - Height() + y1;
00872 
00873          int xofs = GetParent() ? GetParent()->my_xpos : 0;
00874          int yofs = GetParent() ? GetParent()->my_ypos : 0;
00875 
00876          if ( x1 + xofs + Width() > PG_Application::GetScreenWidth() )
00877             x1 = max ( PG_Application::GetScreenWidth() - Width() - xofs, 0 );
00878 
00879          if ( y1 + yofs + Height() > PG_Application::GetScreenHeight() )
00880             y1 = max ( PG_Application::GetScreenHeight() - Height() - yofs, 0 );
00881 
00882          MoveWidget( x1, y1, false );
00883       }
00884 
00885       int titlebarHeight;
00886       pc.addInteger("Titlebarheight", titlebarHeight, -1 );
00887       if ( titlebarHeight != -1 )
00888          SetTitlebarHeight( titlebarHeight );
00889 
00890       ASCString title;
00891       pc.addString("Title", title, "" );
00892       if ( !title.empty() )
00893          SetTitle( title );
00894          
00895       widgetParameters.runTextIO( pc );
00896       widgetParameters.assign ( this );
00897 
00898       parsePanelASCTXT( pc, this, widgetParameters );
00899 
00900       return true;
00901    } catch ( ParsingError err ) {
00902       warning( "parsing error: " + err.getMessage());
00903    } catch ( tfileerror err ) {
00904       warning( "could not acces file: " + err.getFileName() );
00905    }
00906    return false;
00907 
00908 }
00909 
00910 
00911 void ASCGUI_Window::destruct()
00912 {
00913    if ( textPropertyGroup ) {
00914       for ( GuiCache::iterator i = guiCache.begin(); i != guiCache.end(); ++i )
00915          if ( i->second == textPropertyGroup )
00916             return;
00917 
00918       delete textPropertyGroup;
00919    }
00920 }
00921 
00922 ASCGUI_Window::~ASCGUI_Window()
00923 {
00924    destruct();
00925 }
00926 
00927 
00928 Panel :: Panel ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
00929        : ASCGUI_Window( parent, r, panelName_, "Panel", loadTheme )
00930 {
00931    if ( loadTheme )
00932       setup();
00933       
00934    BringToFront();
00935 }
00936 
00937 ASCGUI_Window::WidgetParameters Panel::getDefaultWidgetParams()
00938 {
00939     static WidgetParameters defaultWidgetParameters;
00940     static ASCString panelBackgroundImage;
00941     static bool defaultsLoaded = false;
00942     if ( !defaultsLoaded ) {
00943        tnfilestream s ( "default.ascgui", tnstream::reading );
00944 
00945        TextFormatParser tfp ( &s );
00946        auto_ptr<TextPropertyGroup> tpg ( tfp.run());
00947 
00948        PropertyReadingContainer pc ( "panel", tpg.get() );
00949 
00950        defaultWidgetParameters.runTextIO ( pc );
00951        pc.addString("PanelBackgroundImage", panelBackgroundImage );
00952        defaultsLoaded = true;
00953     }
00954     return defaultWidgetParameters;
00955 }
00956 
00957 
00958 
00959 LayoutablePanel :: LayoutablePanel ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
00960    : Panel ( parent, PositionCalculator( r, panelName_), panelName_, loadTheme ), name( panelName_ )
00961 {
00962 }
00963 
00964 
00965 PG_Rect LayoutablePanel::PositionCalculator( const PG_Rect& rect, const ASCString& panelName )
00966 {
00967    CGameOptions::PanelData pd;
00968    if ( CGameOptions::Instance()->getPanelData(panelName, pd) ) {
00969       pd.x = PG_Application::GetScreenWidth() - pd.x;
00970       PG_Rect r ( pd.x, pd.y, DashBoardWidth, rect.Height() );
00971       
00972       if ( r.x + r.w >  PG_Application::GetScreenWidth() )
00973          r.x = PG_Application::GetScreenWidth() - r.w;
00974          
00975       if ( r.y + r.h >  PG_Application::GetScreenHeight() )
00976          r.y = PG_Application::GetScreenHeight() - r.y;
00977       
00978       return r;
00979    } else
00980       return PG_Rect( rect.x, rect.y, DashBoardWidth, rect.Height() );
00981 }
00982 
00983 void LayoutablePanel::eventMoveWidget (int x, int y)
00984 {
00985    Panel::eventMoveWidget(x,y);
00986    CGameOptions::PanelData pos;
00987    pos.x = PG_Application::GetScreenWidth() - x;
00988    pos.y = y;
00989    pos.visible = true;
00990 
00991    CGameOptions::Instance()->updatePanelData( name, pos );
00992 }
00993 
00994 void LayoutablePanel::eventHide()
00995 {
00996    Panel::eventHide();
00997    
00998    CGameOptions::PanelData pos;
00999    pos.x = PG_Application::GetScreenWidth() - my_xpos;
01000    pos.y = my_ypos;
01001    pos.visible = false;
01002 
01003    CGameOptions::Instance()->updatePanelData( name, pos );
01004 }
01005 
01006 void LayoutablePanel::eventShow()
01007 {
01008    Panel::eventShow();
01009    
01010    CGameOptions::PanelData pos;
01011    pos.x = PG_Application::GetScreenWidth() - my_xpos;
01012    pos.y = my_ypos;
01013    pos.visible = true;
01014 
01015    CGameOptions::Instance()->updatePanelData( name, pos );
01016 }
01017 
01018 
01019 
01020 
01021 
01022 
01023 ConfigurableWindow :: ConfigurableWindow ( PG_Widget *parent, const PG_Rect &r, const ASCString& panelName_, bool loadTheme )
01024        : ASCGUI_Window( parent, r, panelName_, "Window", loadTheme )
01025 {
01026    if ( loadTheme )
01027       setup();
01028       
01029    BringToFront();
01030 }
01031 
01032 ASCGUI_Window::WidgetParameters ConfigurableWindow ::getDefaultWidgetParams()
01033 {
01034    return  WidgetParameters();
01035 }
01036 

Generated on Tue Jun 24 01:27:56 2008 for Advanced Strategic Command by  doxygen 1.4.2