1/****************************************************************************
2*
3*                       wxWindows HTML Applet Package
4*
5*               Copyright (C) 1991-2001 SciTech Software, Inc.
6*                            All rights reserved.
7*
8*  ========================================================================
9*
10*    The contents of this file are subject to the wxWindows License
11*    Version 3.0 (the "License"); you may not use this file except in
12*    compliance with the License. You may obtain a copy of the License at
13*    http://www.wxwindows.org/licence3.txt
14*
15*    Software distributed under the License is distributed on an
16*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17*    implied. See the License for the specific language governing
18*    rights and limitations under the License.
19*
20*  ========================================================================
21*
22* Language:		ANSI C++
23* Environment:	Any
24*
25* Description:  Implementation of wxIfElseVariable Class, Dynamically constructed
26* objects representing variables in SSI #if, #else, and #endif directives
27*
28****************************************************************************/
29
30// Include private headers
31#include "wx/applet/ifelsevar.h"
32
33// wxWindows forcelink macro
34#include "wx/html/forcelnk.h"
35#include "wx/msgdlg.h"
36
37/*---------------------------- Global variables ---------------------------*/
38
39static wxIfElseVariable *wxIfElseVariable::sm_first = NULL;
40static wxHashTable      *wxIfElseVariable::sm_varTable = NULL;
41
42/*----------------------------- Implementation ----------------------------*/
43
44/****************************************************************************
45PARAMETERS:
46varName         - The String name of the class
47getValueFn      - Pointer to the function that returns the echo variable value
48
49REMARKS:
50Constructor for the wxIfElseVariable class that self registers itself with
51the list of all echo variables when the static class instance is created
52at program init time (remember all the constructors get called before
53the main program function!).
54****************************************************************************/
55wxIfElseVariable::wxIfElseVariable(
56    const char *varName,
57    wxIfElseVariableGetValueFn getValueFn)
58{
59    m_varName = varName;
60    m_getValueFn = getValueFn;
61    m_next = sm_first;
62    sm_first = this;
63}
64
65/****************************************************************************
66REMARKS:
67Initializes parent pointers and hash table for fast searching for echo
68variables.
69****************************************************************************/
70void wxIfElseVariable::Initialize()
71{
72    wxIfElseVariable::sm_varTable = new wxHashTable(wxKEY_STRING);
73
74    // Index all class infos by their class name
75    wxIfElseVariable *info = sm_first;
76    while (info) {
77        if (info->m_varName)
78            sm_varTable->Put(info->m_varName, info);
79        info = info->m_next;
80        }
81}
82
83/****************************************************************************
84REMARKS:
85Clean up echo variable hash tables on application exit.
86****************************************************************************/
87void wxIfElseVariable::CleanUp()
88{
89    delete wxIfElseVariable::sm_varTable;
90    wxIfElseVariable::sm_varTable = NULL;
91}
92
93/****************************************************************************
94PARAMETERS:
95varName       - The String name of the class
96
97REMARKS:
98Constructor for the wxIfElseVariable class that self registers itself with
99the list of all ifelse variables when the static class instance is created
100at program init time (remember all the constructors get called before
101the main program function!).
102****************************************************************************/
103bool wxIfElseVariable::GetValue(
104    const wxChar *varName)
105{
106    wxIfElseVariable *info = wxIfElseVariable::FindVariable(varName);
107    if (info) {
108        // Return the forced value if the variable has been forced.
109        if (info->forced)
110            return info->forceVal;
111        return info->m_getValueFn();
112        }
113#ifdef CHECKED
114    wxMessageBox(wxString("wxHTML #if error: Class is not a valid if else variable (") + varName + wxString(")."),"Error",wxICON_ERROR);
115#endif
116    return wxString("");
117}
118
119/****************************************************************************
120PARAMETERS:
121varName       - The String name of the class
122
123RETURNS:
124True if the if/else variable exists, false if not.
125****************************************************************************/
126bool wxIfElseVariable::Exists(
127    const wxChar *varName)
128{
129    return wxIfElseVariable::FindVariable(varName) != NULL;
130}
131
132/****************************************************************************
133PARAMETERS:
134varName     - The String name of the class
135val         - Value to force the if/else variable with
136
137REMARKS:
138Function to forcibly override the value of an if/else variable for
139testing purposes. Once the variable has been forced, it will always return
140the forced value until the application exists.
141
142NOTE:   This is only available when compiled in CHECKED mode.
143****************************************************************************/
144void wxIfElseVariable::Force(
145    const wxChar *varName,
146    bool val)
147{
148    wxIfElseVariable *info = wxIfElseVariable::FindVariable(varName);
149    if (info) {
150        info->forced = true;
151        info->forceVal = val;
152        }
153    else {
154#ifdef CHECKED
155        wxMessageBox(wxString("wxHTML #if error: Class is not a valid if else variable (") + varName + wxString(")."),"Error",wxICON_ERROR);
156#endif
157        }
158}
159
160/*------------------------ Macro Documentation ---------------------------*/
161
162// Here we declare some fake functions to get doc-jet to properly document the macros
163
164#undef BEGIN_IFELSE_VARIABLE
165/****************************************************************************
166PARAMETERS:
167name    - The name of the variable to create
168
169REMARKS:
170This macro is used to create variables for use by the #if, #else and #endif
171blocks in the HTML preprocessor.
172To create a new variable include the code necessary to get the value of the
173variable between a block of BEGIN_IFELSE_VARIABLE and END_IFELSE_VARIABLE macros.
174
175EXAMPLE:
176BEGIN_IFELSE_VARIABLE(UserName)
177	// Get username from nucleus
178	bool tmp = GA_HasRegistered();
179END_IFELSE_VARIABLE(UserName, tmp)
180
181SEE ALSO:
182wxIfElseVariable, wxIfElsePrep, END_IFELSE_VARIABLE, IFELSE_VARIABLE
183****************************************************************************/
184void BEGIN_IFELSE_VARIABLE(
185    const char *name);
186
187#undef END_IFELSE_VARIABLE
188/****************************************************************************
189PARAMETERS:
190name        - The name of the variable to end
191returnval   - The boolean value which is the value of the variable
192
193REMARKS:
194This macro is used to create variables for use by the #if, #else and #endif
195blocks in the HTML preprocessor.
196To create a new variable include the code necessary to get the value of the
197variable between a block of BEGIN_IFELSE_VARIABLE and END_IFELSE_VARIABLE macros.
198
199EXAMPLE:
200BEGIN_IFELSE_VARIABLE(UserName)
201	// Get username from nucleus
202	bool tmp = GA_HasRegistered();
203END_IFELSE_VARIABLE(UserName, tmp)
204
205SEE ALSO:
206wxIfElseVariable, wxIfElsePrep, BEGIN_IFELSE_VARIABLE, IFELSE_VARIABLE
207****************************************************************************/
208void END_IFELSE_VARIABLE(
209    const char *name,
210    bool returnval);
211
212#undef IFELSE_VARIABLE
213/****************************************************************************
214PARAMETERS:
215name        - The name of the variable
216state       - value of the variable
217
218REMARKS:
219This macro is used to create constant boolean variables for use by the
220#if, #else and #endif blocks in the HTML preprocessor.
221This MACRO creates a variable that simply returns the given state and is
222not modifiable.
223
224SEE ALSO:
225wxIfElseVariable, wxIfElsePrep, BEGIN_IFELSE_VARIABLE, END_IFELSE_VARIABLE
226****************************************************************************/
227void IFELSE_VARIABLE(
228    const char *name,
229    bool state);
230
231FORCE_LINK_ME(ifelsevar)
232
233