dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / application / UiBuilder / DefaultObjectPropertiesManager.cpp
blobf46c39dddde6b85fc9e7c835d633c30f3d06ca40
1 #include "FirstUiBuilder.h"
2 #include "DefaultObjectPropertiesManager.h"
4 #include "AddPropertyDialogBox.h"
5 #include "UnicodeUtils.h"
6 #include "UIString.h"
7 #include "UIWidget.h"
8 #include "resource.h"
10 #include <commctrl.h>
11 #include <cstdio>
13 #undef min
15 extern HWND gTooltip;
16 extern HWND gMainWindow;
18 extern void ClearDefPushButtonLook( HWND hwndDlg, UINT nControlID );
19 extern void AddTooltipFromControlID( HWND TooltipWindow, HWND ParentWindow, UINT ControlID, char *Tooltip );
21 DefaultObjectPropertiesManager::DefaultObjectPropertiesManager( void )
23 struct
25 UITypeID TypeID;
26 const char *TypeName;
27 const char *SizeStr;
29 InitialDefaults[] =
31 { TUIButton, "Button", "64,32" },
32 { TUICheckbox, "Checkbox", "64,32" },
33 { TUIComboBox, "ComboBox", "300,16"},
34 { TUIComposite, "Composite", "480,320" },
35 { TUICursorSet, "CursorSet", 0 },
36 { TUIData, "Data", 0 },
37 { TUIDataSource, "DataSource", 0 },
38 { TUIDataSourceContainer,"DataSourceContainer",0 },
39 { TUIDeformerHUD, "DeformerHud", 0 },
40 { TUIDeformerRotate, "DeformerRotate", 0 },
41 { TUIDeformerWave, "DeformerWave", 0 },
42 { TUIDropdownbox, "Dropdownbox", "128,32" },
43 { TUIEllipse, "Ellipse", "128,128" },
44 { TUIFontCharacter, "FontCharacter", "16,16" },
45 { TUIGridStyle, "GridStyle", 0 },
46 { TUIImage, "Image", "128,128" },
47 { TUIImageStyle, "ImageStyle", 0 },
48 { TUIList, "List", "128,160" },
49 { TUIListStyle, "ListStyle", 0 },
50 { TUIListbox, "Listbox", "128,160" },
51 { TUIListboxStyle, "ListboxStyle", 0 },
52 { TUINamespace, "Namespace", 0 },
53 { TUIPage, "Page", "320,240" },
54 { TUIPalette, "Palette", 0 },
55 { TUIPie, "Pie", "128,128" },
56 { TUIPieStyle, "PieStyle", 0 },
57 { TUIPopupMenu, "PopupMenu", "32,128" },
58 { TUIPopupMenuStyle, "PopupMenuStyle", 0 },
59 { TUIProgressbar, "Progressbar", "128,32" },
60 { TUIProgressbarStyle, "ProgressbarStyle", 0 },
61 { TUIRadialMenu, "RadialMenu", "32,128" },
62 { TUIRunner, "Runner", "128,256" },
63 { TUIScrollbar, "Scrollbar", "32,128" },
64 { TUIScrollbarStyle, "ScrollbarStyle", 0 },
65 { TUISliderbar, "Sliderbar", "128,32" },
66 { TUISliderbarStyle, "SliderbarStyle", 0 },
67 { TUISliderbarStyle, "SliderbarStyle", 0 },
68 { TUISliderplane, "Sliderplane", "128,128" },
69 { TUITabSet, "TabSet", "128,64" },
70 { TUITabSetStyle, "TabSetStyle", 0 },
71 { TUITabbedPane, "TabbedPane", "256,32" },
72 { TUITabbedPaneStyle, "TabbedPaneStyle", 0 },
73 { TUITable, "Table", "300,400" },
74 { TUITableHeader, "TableHeader", "300,32" },
75 { TUITableModelDefault,"TableModelDefault",0 },
76 { TUITemplate, "Template", 0 },
77 { TUIText, "Text", "128,64" },
78 { TUITextbox, "Textbox", "128,32" },
79 { TUITextboxStyle, "TextboxStyle", 0 },
80 { TUITextStyleManager, "TextStyleManager", "en" },
81 { TUITreeView, "TreeView", "300,400"},
82 { TUIVolumePage, "VolumePage", "128,128" },
85 for( int i = 0; i < sizeof(InitialDefaults) / sizeof(InitialDefaults[0]); ++i )
87 DefaultObjectProperties DefaultProperty;
89 DefaultProperty.TypeID = InitialDefaults[i].TypeID;
90 DefaultProperty.TypeName = InitialDefaults[i].TypeName;
92 UIString NameProperty (UI_UNICODE_T("New "));
94 NameProperty += UIUnicode::narrowToWide (InitialDefaults[i].TypeName);
95 DefaultProperty.Properties["Name"] = NameProperty;
98 if (DefaultProperty.TypeID == TUITextStyleManager)
99 DefaultProperty.Properties["FontLocale"] = Unicode::narrowToWide (InitialDefaults[i].SizeStr);
100 else if (InitialDefaults [i].SizeStr)
101 DefaultProperty.Properties["Size"] = Unicode::narrowToWide (InitialDefaults [i].SizeStr);
103 mDefaultObjectProperties.push_back( DefaultProperty );
106 mSelectedPropertyMap = 0;
107 mPropertyListbox = 0;
110 void DefaultObjectPropertiesManager::ApplyDefaultPropertiesToObject( UIBaseObject *TargetObject ) const
112 for( DefaultObjectPropertiesList::const_iterator i = mDefaultObjectProperties.begin(); i != mDefaultObjectProperties.end(); ++i )
114 if(_stricmp(TargetObject->GetTypeName(), i->TypeName.c_str()) == 0)
116 for( StringMap::const_iterator j = i->Properties.begin(); j != i->Properties.end(); ++j )
117 TargetObject->SetProperty( UILowerString (j->first), j->second );
119 break;
123 if (TargetObject->IsA (TUIWidget))
125 UIWidget * const wid = static_cast<UIWidget *>(TargetObject);
126 const UIWidget * const parent = wid->GetParentWidget ();
128 if (parent)
130 UISize size = wid->GetSize ();
131 const UISize & parentSize = parent->GetSize ();
132 size.x = std::min (size.x, parentSize.x);
133 size.y = std::min (size.y, parentSize.y);
134 wid->SetSize (size);
139 BOOL DefaultObjectPropertiesManager::DefaultObjectPropertiesManagerWindowProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
141 switch( uMsg )
143 case WM_INITDIALOG:
144 mPropertyListbox = GetDlgItem( hwndDlg, IDC_PROPERTYLIST );
146 AddTooltipFromControlID( gTooltip, gMainWindow, IDC_ADDPROPERTY, "Add Default Property" );
147 AddTooltipFromControlID( gTooltip, gMainWindow, IDC_REMOVEPROPERTY, "Remove Default Property" );
149 SendDlgItemMessage( hwndDlg, IDC_ADDPROPERTY, BM_SETIMAGE, IMAGE_ICON,
150 (LPARAM)LoadImage( GetModuleHandle(0), MAKEINTRESOURCE(IDI_ADDPROPERTY), IMAGE_ICON, 16, 16, 0 ) );
152 SendDlgItemMessage( hwndDlg, IDC_REMOVEPROPERTY, BM_SETIMAGE, IMAGE_ICON,
153 (LPARAM)LoadImage( GetModuleHandle(0), MAKEINTRESOURCE(IDI_REMOVEPROPERTY), IMAGE_ICON, 16, 16, 0 ) );
155 LoadDataToDialog( hwndDlg );
156 return TRUE;
158 case WM_CLOSE:
159 // Equivalent to pressing cancel
160 EndDialog( hwndDlg, 0 );
161 return 0;
163 case WM_COMMAND:
164 if( LOWORD( wParam ) == IDOK )
166 mDefaultObjectProperties = mWorkingObjectProperties;
167 EndDialog( hwndDlg, 0 );
169 else if( LOWORD( wParam ) == IDCANCEL )
171 EndDialog( hwndDlg, 0 );
173 else if( (LOWORD( wParam ) == IDC_OBJECTTYPE) && (HIWORD( wParam ) == LBN_SELCHANGE) )
174 LoadDefaultsToListbox( hwndDlg );
175 else if( (LOWORD( wParam ) == IDC_ADDPROPERTY) && (HIWORD( wParam ) == BN_CLICKED) )
176 AddNewProperty( hwndDlg );
177 else if( (LOWORD( wParam ) == IDC_REMOVEPROPERTY) && (HIWORD( wParam ) == BN_CLICKED) )
178 RemoveSelectedProperty( hwndDlg );
180 return 0;
182 case WM_DRAWITEM:
183 HandleOwnerDraw( hwndDlg, wParam, lParam );
184 return 0;
186 default:
187 return 0;
191 BOOL CALLBACK DefaultObjectPropertiesManager::DefaultObjectPropertiesManagerStaticWindowProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
193 static const char *PropertyName = "DefaultObjectPropertiesManager::this";
195 if( uMsg == WM_INITDIALOG )
197 SetProp( hwndDlg, PropertyName, (HANDLE)lParam );
198 return reinterpret_cast<DefaultObjectPropertiesManager *>( lParam )->DefaultObjectPropertiesManagerWindowProc( hwndDlg, uMsg, wParam, lParam );
200 else
202 DefaultObjectPropertiesManager *pManager =
203 reinterpret_cast<DefaultObjectPropertiesManager *>( GetProp( hwndDlg, PropertyName ) );
205 if( uMsg == WM_DESTROY )
206 RemoveProp( hwndDlg, PropertyName );
208 return pManager->DefaultObjectPropertiesManagerWindowProc( hwndDlg, uMsg, wParam, lParam );
212 void DefaultObjectPropertiesManager::DisplayEditDialog( HWND hParent )
214 DialogBoxParam( GetModuleHandle(0), MAKEINTRESOURCE(IDD_DEFAULTPROPERTIES),
215 hParent, DefaultObjectPropertiesManagerStaticWindowProc, (LPARAM)this );
218 void DefaultObjectPropertiesManager::LoadDataToDialog( HWND hwndDlg )
220 mWorkingObjectProperties = mDefaultObjectProperties;
222 for( DefaultObjectPropertiesList::const_iterator i = mWorkingObjectProperties.begin(); i != mWorkingObjectProperties.end(); ++i )
224 const UINarrowString & tname = (*i).TypeName;
226 long InsertedAt = SendDlgItemMessage( hwndDlg, IDC_OBJECTTYPE, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(tname.c_str ()) );
227 SendDlgItemMessage( hwndDlg, IDC_OBJECTTYPE, CB_SETITEMDATA, InsertedAt, (LPARAM)i->TypeID );
230 SendDlgItemMessage( hwndDlg, IDC_OBJECTTYPE, CB_SETCURSEL, 0, 0 );
231 LoadDefaultsToListbox( hwndDlg );
234 void DefaultObjectPropertiesManager::LoadDefaultsToListbox( HWND hwndDlg )
236 SendMessage( mPropertyListbox, LB_RESETCONTENT, 0, 0 );
238 long CurrentSelection = SendDlgItemMessage( hwndDlg, IDC_OBJECTTYPE, CB_GETCURSEL, 0, 0 );
240 if( CurrentSelection >= 0 )
242 UITypeID TypeID = (UITypeID)SendDlgItemMessage( hwndDlg, IDC_OBJECTTYPE, CB_GETITEMDATA, CurrentSelection, 0 );
243 mSelectedPropertyMap = 0;
245 for( DefaultObjectPropertiesList::iterator i = mWorkingObjectProperties.begin(); i != mWorkingObjectProperties.end(); ++i )
247 if( i->TypeID == TypeID )
249 mSelectedPropertyMap = &(i->Properties);
251 for( StringMap::iterator j = i->Properties.begin(); j != i->Properties.end(); ++j )
253 const UINarrowString & name = (*j).first;
254 SendMessage( mPropertyListbox, LB_ADDSTRING, 0, reinterpret_cast<LPARAM> (name.c_str ()));
261 void DefaultObjectPropertiesManager::HandleOwnerDraw( HWND hwndDlg, WPARAM wParam, LPARAM lParam )
263 UI_UNREF (lParam);
264 UI_UNREF (wParam);
265 UI_UNREF (hwndDlg);
267 LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT)lParam;
268 HBITMAP DoubleBufferBitmap, OldBitmap;
269 HDC DoubleBufferDC;
270 HFONT OldFont;
271 POINT ItemSize;
272 RECT rcFill;
273 RECT rcClip;
275 ItemSize.x = pdis->rcItem.right - pdis->rcItem.left;
276 ItemSize.y = pdis->rcItem.bottom - pdis->rcItem.top;
278 SetRect( &rcFill, 0, 0, ItemSize.x, ItemSize.y );
279 SetRect( &rcClip, 2, 0, ItemSize.x / 2, ItemSize.y );
281 DoubleBufferDC = CreateCompatibleDC( pdis->hDC );
282 DoubleBufferBitmap = CreateCompatibleBitmap( pdis->hDC, ItemSize.x, ItemSize.y );
284 OldBitmap = (HBITMAP)SelectObject( DoubleBufferDC, DoubleBufferBitmap );
285 OldFont = (HFONT)SelectObject( DoubleBufferDC, GetCurrentObject( pdis->hDC, OBJ_FONT ) );
287 if( pdis->itemState & ODS_SELECTED )
289 HWND hwndFocus = GetFocus();
291 if( hwndFocus == mPropertyListbox )
293 FillRect( DoubleBufferDC, &rcFill, GetSysColorBrush( COLOR_HIGHLIGHT ) );
294 SetTextColor( DoubleBufferDC, GetSysColor( COLOR_HIGHLIGHTTEXT ) );
296 else
298 FillRect( DoubleBufferDC, &rcFill, GetSysColorBrush( COLOR_BTNFACE ) );
299 SetTextColor( DoubleBufferDC, GetSysColor( COLOR_BTNTEXT ) );
302 else
304 FillRect( DoubleBufferDC, &rcFill, GetSysColorBrush( COLOR_WINDOW ) );
305 SetTextColor( DoubleBufferDC, GetSysColor( COLOR_WINDOWTEXT ) );
308 SetBkMode( DoubleBufferDC, TRANSPARENT );
310 if( pdis->itemID != -1 )
312 char *PropertyName = new char[ SendMessage( mPropertyListbox, LB_GETTEXTLEN, pdis->itemID, 0 ) + 1 ];
313 SendMessage( mPropertyListbox, LB_GETTEXT, pdis->itemID, (long)PropertyName );
314 DrawText( DoubleBufferDC, PropertyName, -1, &rcClip, DT_SINGLELINE );
316 rcClip.left = rcClip.right;
317 rcClip.right = ItemSize.x;
319 UIString Value;
321 StringMap::iterator i = mSelectedPropertyMap->find( PropertyName );
323 if( i != mSelectedPropertyMap->end() )
325 Value = (*i).second;
328 DrawText( DoubleBufferDC, UIUnicode::wideToNarrow (Value).c_str(), -1, &rcClip, DT_SINGLELINE );
329 delete PropertyName;
332 BitBlt( pdis->hDC, pdis->rcItem.left, pdis->rcItem.top, ItemSize.x, ItemSize.y, DoubleBufferDC, 0, 0, SRCCOPY );
334 SelectObject( DoubleBufferDC, OldFont );
335 SelectObject( DoubleBufferDC, OldBitmap );
336 DeleteObject( DoubleBufferBitmap );
337 DeleteDC( DoubleBufferDC );
340 void DefaultObjectPropertiesManager::AddNewProperty( HWND hwndDlg )
342 AddPropertyDialogBox AddProp;
343 UINarrowString NewName;
344 UIString NewValue;
346 if( AddProp.GetNewProperty( hwndDlg, NewName, NewValue ) )
348 (*mSelectedPropertyMap)[NewName] = NewValue;
349 LoadDefaultsToListbox(hwndDlg);
351 SetFocus(mPropertyListbox);
352 ClearDefPushButtonLook( hwndDlg, IDC_ADDPROPERTY );
355 void DefaultObjectPropertiesManager::RemoveSelectedProperty( HWND hwndDlg )
357 long CurrentSelection = SendMessage( mPropertyListbox, LB_GETCURSEL, 0, 0 );
359 if( CurrentSelection >= 0 )
361 long TextLength = SendMessage( mPropertyListbox, LB_GETTEXTLEN, CurrentSelection, 0 );
362 char *ItemText = new char[TextLength + 1];
364 SendMessage( mPropertyListbox, LB_GETTEXT, CurrentSelection, (LPARAM)ItemText );
366 StringMap::iterator i = mSelectedPropertyMap->find( ItemText );
368 if( i != mSelectedPropertyMap->end() )
369 mSelectedPropertyMap->erase(i);
371 LoadDefaultsToListbox(hwndDlg);
373 SetFocus(mPropertyListbox);
374 ClearDefPushButtonLook( hwndDlg, IDC_REMOVEPROPERTY );
377 void DefaultObjectPropertiesManager::SaveTo( FILE *fp )
379 fprintf( fp, "<DefaultObjectPropertiesManager>\n" );
381 for( DefaultObjectPropertiesList::iterator i = mDefaultObjectProperties.begin();
382 i != mDefaultObjectProperties.end(); ++i )
384 fprintf( fp, "<%s>\n", i->TypeName );
386 for( StringMap::iterator j = i->Properties.begin(); j != i->Properties.end(); ++j )
388 const UINarrowString & name = (*j).first;
389 const UINarrowString value = Unicode::wideToNarrow ((*j).second);
391 fprintf( fp, "'%s' = '%s'\n", name.c_str (), value.c_str ());
394 fprintf( fp, "</%s>\n", i->TypeName );
397 fprintf( fp, "</DefaultObjectPropertiesManager>\n" );
400 void DefaultObjectPropertiesManager::LoadFrom( FILE *fp )
402 char LineBuffer[1024];
404 while( fgets( LineBuffer, sizeof( LineBuffer ), fp ) )
406 char *pNewLine = strchr( LineBuffer, '\n' );
408 if( pNewLine )
409 *pNewLine = '\0';
411 if( !_stricmp( LineBuffer, "<DefaultObjectPropertiesManager>" ) )
412 continue;
413 else if( !_stricmp( LineBuffer, "</DefaultObjectPropertiesManager>" ) )
414 break;
415 else if( *LineBuffer == '<' )
417 mSelectedPropertyMap = 0;
419 if( *(LineBuffer + 1) == '/' )
420 continue;
422 if( strchr( LineBuffer + 1, '>' ) )
423 *strchr( LineBuffer + 1, '>' ) = '\0';
426 for( DefaultObjectPropertiesList::iterator i = mDefaultObjectProperties.begin(); i != mDefaultObjectProperties.end(); ++i )
428 if( !_stricmp( LineBuffer + 1, i->TypeName.c_str() ) )
430 mSelectedPropertyMap = &(i->Properties);
431 break;
435 else
437 UINarrowString Name;
438 UIString Value;
439 char *p = LineBuffer;
440 bool bInQuote = false;
441 bool bInValue = false;
443 while( *p )
445 if( *p == '\'' )
447 if( bInQuote )
448 bInQuote = false;
449 else
450 bInQuote = true;
452 else if( bInQuote )
454 if( bInValue )
455 Value += *p;
456 else
457 Name += *p;
459 else if( *p == '=' )
460 bInValue = true;
462 p++;
465 if( mSelectedPropertyMap )
466 (*mSelectedPropertyMap)[Name] = Value;