1 /*
2 * This file is part of wxSmith plugin for Code::Blocks Studio
3 * Copyright (C) 2006-2007  Bartlomiej Swiecki
4 *
5 * wxSmith is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * wxSmith is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * $Revision: 8676 $
19 * $Id: wxscontainer.cpp 8676 2012-12-16 14:08:58Z mortenmacfly $
20 * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/plugins/contrib/wxSmith/wxwidgets/wxscontainer.cpp $
21 */
22 
23 #include "wxscontainer.h"
24 #include "wxsitemresdata.h"
25 #include "wxstool.h"
26 #include "wxscodercontext.h"
27 #include <logmanager.h>
28 
wxsContainer(wxsItemResData * Data,const wxsItemInfo * Info,const wxsEventDesc * EventArray,const wxsStyleSet * StyleSet,long PropertiesFlags)29 wxsContainer::wxsContainer(
30     wxsItemResData* Data,
31     const wxsItemInfo* Info,
32     const wxsEventDesc* EventArray,
33     const wxsStyleSet* StyleSet,
34     long PropertiesFlags):
35         wxsParent(Data,Info,PropertiesFlags,EventArray,StyleSet)
36 {
37 }
38 
OnCanAddChild(wxsItem * Item,bool ShowMessage)39 bool wxsContainer::OnCanAddChild(wxsItem* Item,bool ShowMessage)
40 {
41     if ( Item->GetType() == wxsTSpacer )
42     {
43         if ( ShowMessage )
44         {
45             wxMessageBox(_("Spacer can be added into sizer only"));
46         }
47         return false;
48     }
49 
50     if ( Item->GetType() == wxsTSizer )
51     {
52         if ( GetChildCount() > 0 )
53         {
54             if ( GetChild(0)->GetType() == wxsTSizer )
55             {
56                 if ( ShowMessage )
57                 {
58                     wxMessageBox(_("This item can contain only one sizer"));
59                 }
60                 return false;
61             }
62             else
63             {
64                 if ( ShowMessage )
65                 {
66                     wxMessageBox(_("Item can not contain sizer if it has other items inside"));
67                 }
68                 return false;
69             }
70         }
71         return true;
72     }
73 
74     if ( GetChildCount() > 0 )
75     {
76         if ( GetChild(0)->GetType() == wxsTSizer )
77         {
78             if ( ShowMessage )
79             {
80                 wxMessageBox(_("Item contains sizer, can not add other items into it."));
81             }
82             return false;
83         }
84     }
85 
86     return true;
87 }
88 
OnEnumItemProperties(long Flags)89 void wxsContainer::OnEnumItemProperties(long Flags)
90 {
91     OnEnumContainerProperties(Flags);
92 }
93 
OnAddItemQPP(wxsAdvQPP * QPP)94 void wxsContainer::OnAddItemQPP(wxsAdvQPP* QPP)
95 {
96     OnAddContainerQPP(QPP);
97 }
98 
AddChildrenPreview(wxWindow * This,long Flags)99 void wxsContainer::AddChildrenPreview(wxWindow* This,long Flags)
100 {
101     for ( int i=0; i<GetChildCount(); i++ )
102     {
103         wxsItem* Child = GetChild(i);
104         wxObject* ChildPreviewAsObject = Child->BuildPreview(This,Flags);
105         if ( Child->GetType() == wxsTSizer )
106         {
107             wxSizer* ChildPreviewAsSizer = wxDynamicCast(ChildPreviewAsObject,wxSizer);
108             if ( ChildPreviewAsSizer )
109             {
110                 This->SetSizer(ChildPreviewAsSizer);
111             }
112         }
113     }
114 
115     if ( IsRootItem() )
116     {
117         // Adding all tools before calling Fit and SetSizeHints()
118 
119         wxsItemResData* Data = GetResourceData();
120         if ( Data )
121         {
122             for ( int i=0; i<Data->GetToolsCount(); i++ )
123             {
124                 Data->GetTool(i)->BuildPreview(This,Flags);
125             }
126         }
127 
128     }
129 
130     for ( int i=0; i<GetChildCount(); i++ )
131     {
132         wxsItem* Child = GetChild(i);
133         if ( Child->GetType() == wxsTSizer )
134         {
135             wxObject* ChildPreviewAsObject = Child->GetLastPreview();
136             wxSizer*  ChildPreviewAsSizer  = wxDynamicCast(ChildPreviewAsObject,wxSizer);
137             wxWindow* ChildPreviewAsWindow = wxDynamicCast(ChildPreviewAsObject,wxWindow);
138 
139             if ( ChildPreviewAsSizer )
140             {
141                 // Child preview was created directly as sizer, we use it to
142                 // call Fit() and SetSizeHints() directly
143                 if ( GetBaseProps()->m_Size.IsDefault )
144                 {
145                     ChildPreviewAsSizer->Fit(This);
146                 }
147                 ChildPreviewAsSizer->SetSizeHints(This);
148             }
149             else if ( ChildPreviewAsWindow )
150             {
151                 // Preview of sizer is given actually as some kind of panel which paints
152                 // some extra data of sizer. So we have to create out own sizer to call
153                 // Fit and SetSizeHints
154 
155                 wxSizer* IndirectSizer = new wxBoxSizer(wxHORIZONTAL);
156                 IndirectSizer->Add(ChildPreviewAsWindow,1,wxEXPAND,0);
157                 This->SetSizer(IndirectSizer);
158 
159                 if ( GetBaseProps()->m_Size.IsDefault )
160                 {
161                     IndirectSizer->Fit(This);
162                 }
163 
164                 IndirectSizer->SetSizeHints(This);
165             }
166         }
167     }
168 }
169 
AddChildrenCode()170 void wxsContainer::AddChildrenCode()
171 {
172     switch ( GetLanguage() )
173     {
174         case wxsCPP:
175         {
176             wxsCoderContext* Context = GetCoderContext();
177             if ( !Context ) return;
178 
179             // Update parent in context and clear flRoot flag
180             wxString PreviousParent = Context->m_WindowParent;
181             Context->m_WindowParent = Codef(Context,_T("%O"));
182 
183             for ( int i=0; i<GetChildCount(); i++ )
184             {
185                 wxsItem* Child = GetChild(i);
186                 Child->BuildCode(Context);
187                 if ( Child->GetType() == wxsTSizer )
188                 {
189                     // TODO: Is this right place to set-up sizer ?
190                     Codef(_T("%ASetSizer(%o);\n"),i);
191                 }
192             }
193 
194             if ( IsRootItem() )
195             {
196                 // Adding all tools before calling Fit and SetSizeHints()
197                 wxsItemResData* Data = GetResourceData();
198                 if ( Data )
199                 {
200                     for ( int i=0; i<Data->GetToolsCount(); i++ )
201                     {
202                         Data->GetTool(i)->BuildCode(Context);
203                     }
204                 }
205             }
206 
207             for ( int i=0; i<GetChildCount(); i++ )
208             {
209                 wxsItem* Child = GetChild(i);
210                 if ( Child->GetType() == wxsTSizer )
211                 {
212                     if ( GetBaseProps()->m_Size.IsDefault )
213                     {
214                         wxString ChildAccessPrefix = Child->GetAccessPrefix(GetLanguage());
215                         Codef(_T("%sFit(%O);\n"),ChildAccessPrefix.wx_str());
216 
217                         Codef(_T("%sSetSizeHints(%O);\n"),ChildAccessPrefix.wx_str());
218                     }
219                     else
220                     {
221                         wxString ChildVarName = Child->GetVarName();
222                         Codef(_T("SetSizer(%s);\n"), ChildVarName.wx_str());
223 
224                         Codef(_T("Layout();\n"));
225                     }
226                 }
227             }
228 
229             Context->m_WindowParent = PreviousParent;
230             return;
231         }
232 
233         case wxsUnknownLanguage:
234         default:
235         {
236             wxsCodeMarks::Unknown(_T("wxsContainer::AddChildrenCode"),GetLanguage());
237         }
238     }
239 }
240