LCOV - code coverage report
Current view: top level - libreoffice/sc/source/ui/miscdlgs - solvrdlg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 118 0.8 %
Date: 2012-12-27 Functions: 2 18 11.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "rangelst.hxx"
      21             : #include "scitems.hxx"
      22             : #include <sfx2/dispatch.hxx>
      23             : #include <svl/zforlist.hxx>
      24             : #include <vcl/msgbox.hxx>
      25             : 
      26             : #include "uiitems.hxx"
      27             : #include "reffact.hxx"
      28             : #include "document.hxx"
      29             : #include "scresid.hxx"
      30             : #include "solvrdlg.hrc"
      31             : 
      32             : #define _SOLVRDLG_CXX
      33             : #include "solvrdlg.hxx"
      34             : #undef _SOLVERDLG_CXX
      35             : 
      36             : 
      37             : #define ERRORBOX(s) ErrorBox( this, WinBits( WB_OK | WB_DEF_OK), s ).Execute()
      38             : 
      39             : 
      40             : //============================================================================
      41             : //  class ScSolverDlg
      42             : //----------------------------------------------------------------------------
      43             : 
      44           0 : ScSolverDlg::ScSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
      45             :                           ScDocument* pDocument,
      46             :                           ScAddress aCursorPos )
      47             : 
      48             :     :   ScAnyRefDlg         ( pB, pCW, pParent, RID_SCDLG_SOLVER ),
      49             :         //
      50             :         aFlVariables        ( this, ScResId( FL_VARIABLES ) ),
      51             :         aFtFormulaCell      ( this, ScResId( FT_FORMULACELL ) ),
      52             :         aEdFormulaCell      ( this, this, ScResId( ED_FORMULACELL ) ),
      53             :         aRBFormulaCell      ( this, ScResId( RB_FORMULACELL ), &aEdFormulaCell, this ),
      54             :         aFtTargetVal        ( this, ScResId( FT_TARGETVAL ) ),
      55             :         aEdTargetVal        ( this, ScResId( ED_TARGETVAL ) ),
      56             :         aFtVariableCell     ( this, ScResId( FT_VARCELL ) ),
      57             :         aEdVariableCell     ( this, this, ScResId( ED_VARCELL ) ),
      58             :         aRBVariableCell     ( this, ScResId( RB_VARCELL ), &aEdVariableCell, this ),
      59             :         aBtnOk              ( this, ScResId( BTN_OK ) ),
      60             :         aBtnCancel          ( this, ScResId( BTN_CANCEL ) ),
      61             :         aBtnHelp            ( this, ScResId( BTN_HELP ) ),
      62             :         //
      63             :         theFormulaCell      ( aCursorPos ),
      64             :         theVariableCell     ( aCursorPos ),
      65             :         pDoc                ( pDocument ),
      66           0 :         nCurTab             ( aCursorPos.Tab() ),
      67             :         pEdActive           ( NULL ),
      68             :         bDlgLostFocus       ( false ),
      69             :         errMsgInvalidVar    ( ScResId( STR_INVALIDVAR ) ),
      70             :         errMsgInvalidForm   ( ScResId( STR_INVALIDFORM ) ),
      71             :         errMsgNoFormula     ( ScResId( STR_NOFORMULA ) ),
      72           0 :         errMsgInvalidVal    ( ScResId( STR_INVALIDVAL ) )
      73             : {
      74           0 :     Init();
      75           0 :     FreeResource();
      76             : 
      77           0 :     aRBFormulaCell.SetAccessibleRelationMemberOf(&aFlVariables);
      78           0 :     aRBVariableCell.SetAccessibleRelationMemberOf(&aFlVariables);
      79           0 : }
      80             : 
      81             : //----------------------------------------------------------------------------
      82             : 
      83           0 : ScSolverDlg::~ScSolverDlg()
      84             : {
      85           0 : }
      86             : 
      87             : //----------------------------------------------------------------------------
      88             : 
      89           0 : void ScSolverDlg::Init()
      90             : {
      91           0 :     String          aStr;
      92             : 
      93           0 :     aBtnOk.         SetClickHdl     ( LINK( this, ScSolverDlg, BtnHdl ) );
      94           0 :     aBtnCancel.     SetClickHdl     ( LINK( this, ScSolverDlg, BtnHdl ) );
      95             : 
      96           0 :     Link aLink = LINK( this, ScSolverDlg, GetFocusHdl );
      97           0 :     aEdFormulaCell. SetGetFocusHdl  ( aLink );
      98           0 :     aRBFormulaCell. SetGetFocusHdl  ( aLink );
      99           0 :     aEdVariableCell.SetGetFocusHdl  ( aLink );
     100           0 :     aRBVariableCell.SetGetFocusHdl  ( aLink );
     101           0 :     aEdTargetVal.   SetGetFocusHdl  ( aLink );
     102             : 
     103           0 :     aLink = LINK( this, ScSolverDlg, LoseFocusHdl );
     104           0 :     aEdFormulaCell. SetLoseFocusHdl ( aLink );
     105           0 :     aRBFormulaCell. SetLoseFocusHdl ( aLink );
     106           0 :     aEdVariableCell.SetLoseFocusHdl ( aLink );
     107           0 :     aRBVariableCell.SetLoseFocusHdl ( aLink );
     108             : 
     109           0 :     theFormulaCell.Format( aStr, SCA_ABS, NULL, pDoc->GetAddressConvention() );
     110             : 
     111           0 :     aEdFormulaCell.SetText( aStr );
     112           0 :     aEdFormulaCell.GrabFocus();
     113           0 :     pEdActive = &aEdFormulaCell;
     114           0 : }
     115             : 
     116             : //----------------------------------------------------------------------------
     117             : 
     118           0 : sal_Bool ScSolverDlg::Close()
     119             : {
     120           0 :     return DoClose( ScSolverDlgWrapper::GetChildWindowId() );
     121             : }
     122             : 
     123             : //----------------------------------------------------------------------------
     124             : 
     125           0 : void ScSolverDlg::SetActive()
     126             : {
     127           0 :     if ( bDlgLostFocus )
     128             :     {
     129           0 :         bDlgLostFocus = false;
     130           0 :         if( pEdActive )
     131           0 :             pEdActive->GrabFocus();
     132             :     }
     133             :     else
     134             :     {
     135           0 :         GrabFocus();
     136             :     }
     137           0 :     RefInputDone();
     138           0 : }
     139             : 
     140             : //----------------------------------------------------------------------------
     141             : 
     142           0 : void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
     143             : {
     144           0 :     if( pEdActive )
     145             :     {
     146           0 :         if ( rRef.aStart != rRef.aEnd )
     147           0 :             RefInputStart(pEdActive);
     148             : 
     149           0 :         String      aStr;
     150           0 :         ScAddress   aAdr = rRef.aStart;
     151           0 :         sal_uInt16      nFmt = ( aAdr.Tab() == nCurTab )
     152             :                                 ? SCA_ABS
     153           0 :                                 : SCA_ABS_3D;
     154             : 
     155           0 :         aAdr.Format( aStr, nFmt, pDocP, pDocP->GetAddressConvention() );
     156           0 :         pEdActive->SetRefString( aStr );
     157             : 
     158           0 :         if ( pEdActive == &aEdFormulaCell )
     159           0 :             theFormulaCell = aAdr;
     160           0 :         else if ( pEdActive == &aEdVariableCell )
     161           0 :             theVariableCell = aAdr;
     162             :     }
     163           0 : }
     164             : 
     165             : //----------------------------------------------------------------------------
     166             : 
     167           0 : void ScSolverDlg::RaiseError( ScSolverErr eError )
     168             : {
     169           0 :     switch ( eError )
     170             :     {
     171             :         case SOLVERR_NOFORMULA:
     172           0 :             ERRORBOX( errMsgNoFormula );
     173           0 :             aEdFormulaCell.GrabFocus();
     174           0 :             break;
     175             : 
     176             :         case SOLVERR_INVALID_FORMULA:
     177           0 :             ERRORBOX( errMsgInvalidForm );
     178           0 :             aEdFormulaCell.GrabFocus();
     179           0 :             break;
     180             : 
     181             :         case SOLVERR_INVALID_VARIABLE:
     182           0 :             ERRORBOX( errMsgInvalidVar );
     183           0 :             aEdVariableCell.GrabFocus();
     184           0 :             break;
     185             : 
     186             :         case SOLVERR_INVALID_TARGETVALUE:
     187           0 :             ERRORBOX( errMsgInvalidVal );
     188           0 :             aEdTargetVal.GrabFocus();
     189           0 :             break;
     190             :     }
     191           0 : }
     192             : 
     193             : //----------------------------------------------------------------------------
     194             : 
     195           0 : sal_Bool ScSolverDlg::IsRefInputMode() const
     196             : {
     197           0 :     return pEdActive != NULL;
     198             : }
     199             : 
     200             : //----------------------------------------------------------------------------
     201             : 
     202           0 : sal_Bool ScSolverDlg::CheckTargetValue( String& rStrVal )
     203             : {
     204           0 :     sal_uInt32 n1 = 0;
     205             :     double n2;
     206             : 
     207           0 :     return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
     208             : }
     209             : 
     210             : //----------------------------------------------------------------------------
     211             : // Handler:
     212             : 
     213           0 : IMPL_LINK( ScSolverDlg, BtnHdl, PushButton*, pBtn )
     214             : {
     215           0 :     if ( pBtn == &aBtnOk )
     216             :     {
     217           0 :         theTargetValStr = aEdTargetVal.GetText();
     218             : 
     219             :         // Zu ueberpruefen:
     220             :         // 1. enthalten die Strings korrekte Tabellenkoordinaten/def.Namen?
     221             :         // 2. verweist die Formel-Koordinate wirklich auf eine Formelzelle?
     222             :         // 3. wurde ein korrekter Zielwert eingegeben
     223             : 
     224           0 :         const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
     225           0 :         sal_uInt16  nRes1 = theFormulaCell .Parse( aEdFormulaCell.GetText(),  pDoc, eConv );
     226           0 :         sal_uInt16  nRes2 = theVariableCell.Parse( aEdVariableCell.GetText(), pDoc, eConv );
     227             : 
     228           0 :         if ( SCA_VALID == ( nRes1 & SCA_VALID ) )
     229             :         {
     230           0 :             if ( SCA_VALID == ( nRes2 & SCA_VALID ) )
     231             :             {
     232           0 :                 if ( CheckTargetValue( theTargetValStr ) )
     233             :                 {
     234             :                     CellType eType;
     235           0 :                     pDoc->GetCellType( theFormulaCell.Col(),
     236             :                                        theFormulaCell.Row(),
     237           0 :                                        theFormulaCell.Tab(),
     238           0 :                                        eType );
     239             : 
     240           0 :                     if ( CELLTYPE_FORMULA  == eType )
     241             :                     {
     242             :                         ScSolveParam aOutParam( theFormulaCell,
     243             :                                                 theVariableCell,
     244           0 :                                                 theTargetValStr );
     245           0 :                         ScSolveItem  aOutItem( SCITEM_SOLVEDATA, &aOutParam );
     246             : 
     247           0 :                         SetDispatcherLock( false );
     248             : 
     249           0 :                         SwitchToDocument();
     250           0 :                         GetBindings().GetDispatcher()->Execute( SID_SOLVE,
     251             :                                                   SFX_CALLMODE_SLOT | SFX_CALLMODE_RECORD,
     252           0 :                                                   &aOutItem, 0L, 0L );
     253           0 :                         Close();
     254             :                     }
     255           0 :                     else RaiseError( SOLVERR_NOFORMULA );
     256             :                 }
     257           0 :                 else RaiseError( SOLVERR_INVALID_TARGETVALUE );
     258             :             }
     259           0 :             else RaiseError( SOLVERR_INVALID_VARIABLE );
     260             :         }
     261           0 :         else RaiseError( SOLVERR_INVALID_FORMULA );
     262             :     }
     263           0 :     else if ( pBtn == &aBtnCancel )
     264             :     {
     265           0 :         Close();
     266             :     }
     267             : 
     268           0 :     return 0;
     269             : }
     270             : 
     271             : //----------------------------------------------------------------------------
     272             : 
     273           0 : IMPL_LINK( ScSolverDlg, GetFocusHdl, Control*, pCtrl )
     274             : {
     275           0 :     Edit* pEdit = NULL;
     276           0 :     pEdActive = NULL;
     277             : 
     278           0 :     if( (pCtrl == (Control*)&aEdFormulaCell) || (pCtrl == (Control*)&aRBFormulaCell) )
     279           0 :         pEdit = pEdActive = &aEdFormulaCell;
     280           0 :     else if( (pCtrl == (Control*)&aEdVariableCell) || (pCtrl == (Control*)&aRBVariableCell) )
     281           0 :         pEdit = pEdActive = &aEdVariableCell;
     282           0 :     else if( pCtrl == (Control*)&aEdTargetVal )
     283           0 :         pEdit = &aEdTargetVal;
     284             : 
     285           0 :     if( pEdit )
     286           0 :         pEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
     287             : 
     288           0 :     return 0;
     289             : }
     290             : 
     291             : //----------------------------------------------------------------------------
     292             : 
     293           0 : IMPL_LINK_NOARG(ScSolverDlg, LoseFocusHdl)
     294             : {
     295           0 :     bDlgLostFocus = !IsActive();
     296           0 :     return 0;
     297          15 : }
     298             : 
     299             : 
     300             : 
     301             : 
     302             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10