merge the formfield patch from ooo-build
[ooovba.git] / toolkit / workben / controls.cxx
blobf52d17716be63563f053f67891d3ea9caa5698ce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: controls.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
33 #include <tools/debug.hxx>
35 #include <vcl/window.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/wrkwin.hxx>
38 #include <vcl/msgbox.hxx>
41 #include <stardiv/uno/awt/window.hxx>
42 #include <stardiv/uno/awt/vclwin.hxx>
43 #include <stardiv/uno/awt/printer.hxx>
44 #include <stardiv/uno/repos/serinfo.hxx>
45 #include <stardiv/uno/lang/factory.hxx>
48 #include <unoctrl.hxx>
49 // #include <unoctrl2.hxx>
51 #include <svtools/unoiface.hxx> // InitExtToolkit
53 UsrAny UsrAny_UINT16( UINT16 n )
55 UsrAny aVal;
56 aVal.setUINT16( n );
57 return aVal;
60 // -----------------------------------------------------------------------
62 class MyApp : public Application
64 public:
65 void Main();
68 MyApp aMyApp;
70 // -----------------------------------------------------------------------
72 class MyWin : public WorkWindow
74 XControlRef xCtrl;
75 XPropertySetRef xCtrlModel;
76 XControlContainerRef xCont;
77 XPropertySetRef xContModel;
78 XControlRef xDrawCtrl;
79 XGraphicsRef xG;
81 public:
82 MyWin( Window* pParent, WinBits nWinStyle );
83 ~MyWin();
85 void MouseButtonDown( const MouseEvent& rMEvt );
86 void Resize();
87 void Paint( const Rectangle& r );
90 // -----------------------------------------------------------------------
92 void MyApp::Main()
94 createAndSetDefaultServiceManager();
96 MyWin aMainWin( NULL, WB_APP | WB_STDWORK );
97 aMainWin.SetText( "Uno-Controls - Workbench" );
98 aMainWin.Show();
99 Execute();
102 // -----------------------------------------------------------------------
104 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
105 WorkWindow( pParent, nWinStyle )
109 // -----------------------------------------------------------------------
110 MyWin::~MyWin()
112 XComponentRef xC1( xCont, USR_QUERY );
113 xC1->dispose();
114 XComponentRef xC2( xContModel, USR_QUERY );
115 xC2->dispose();
118 // -----------------------------------------------------------------------
120 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
122 if( rMEvt.GetClicks() == 2 )
124 XViewRef xV( xDrawCtrl, USR_QUERY );
125 XWindowRef xC( xDrawCtrl, USR_QUERY );
126 xV->draw( xC->getPosSize().Left(), xC->getPosSize().Top() );
128 // Printer Testen...
130 XServiceManagerRef xProv = getGlobalServiceManager();
131 XServiceProviderRef xSSI = xProv->queryServiceProvider( L"stardiv.vcl.PrinterServer" );
132 XPrinterServerRef xPrinterServer( xSSI->createInstance(), USR_QUERY );
134 Sequence< UString > aPrinterNames = xPrinterServer->getPrinterNames();
135 USHORT nPrinters = aPrinterNames.getLen();
136 String aInfo( "Printers: " );
137 aInfo += nPrinters;
138 if ( nPrinters )
140 for ( USHORT n = 0; n < nPrinters; n++ )
142 aInfo += '\n';
143 aInfo += OUStringToString( aPrinterNames.getConstArray()[n], CHARSET_SYSTEM );
146 XPrinterRef xPrinter = xPrinterServer->createPrinter( aPrinterNames.getConstArray()[0] );
147 xPrinter->start( L"UNOPrinterTest", 1, TRUE );
148 XDeviceRef xDev = xPrinter->startPage();
149 XGraphicsRef xGraphics = xDev->createGraphics();
150 xGraphics->drawText( 200, 200, L"Printed with UNO" );
151 xPrinter->endPage();
152 xPrinter->end();
154 InfoBox( this, aInfo ).Execute();
157 else if( rMEvt.GetClicks() == 1 )
159 if( xContModel )
161 static INT32 nColor = 0x001788ab;
162 xContModel->setPropertyValue( L"BackgroundColor", UsrAny( nColor ) );
163 nColor += 0x00111111;
164 nColor &= 0xFFFFFF;
166 else
168 XServiceManagerRef xProv = getGlobalServiceManager();
170 XMultiServiceFactoryRef xMSF = NAMESPACE_USR( getProcessServiceManager )();
172 XServiceProviderRef xSSI = xProv->queryServiceProvider( L"stardiv.vcl.VclToolkit" );
173 XToolkitRef xToolkit( xMSF->createInstance( L"stardiv.vcl.VclToolkit" ), USR_QUERY );
174 DBG_ASSERT( xToolkit, "No Toolkit!" );
176 // Uno Container + Model erzeugen
177 XServiceProviderRef xModelProv; // = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.ControlContainer" );
178 XInterfaceRef xCMRef( xMSF->createInstance(L"stardiv.vcl.controlmodel.ControlContainer") );
179 xCMRef->queryInterface( XPropertySet::getSmartUik(), xContModel );
180 xContModel->setPropertyValue( L"Border", UsrAny_UINT16( 1 ) );
181 xContModel->setPropertyValue( L"BackgroundColor", UsrAny( (UINT32)0x00CCCCCC) );
183 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.control.ControlContainer" );
184 XInterfaceRef xCCRef( xModelProv->createInstance(), USR_QUERY );
185 xCCRef->queryInterface( XControlContainer::getSmartUik(), xCont );
187 XControlModelRef xCM( xContModel, USR_QUERY );
188 XControlRef xContControl( xCont, USR_QUERY );
189 xContControl->setModel( xCM );
190 XWindowRef xContComp( xCont, USR_QUERY );
191 xContComp->setPosSize( 0, 0, 500, 600, PosSize_POSSIZE );
193 // Zwei EditControls auf einem Model...
194 // xModelProv = xProv->queryServiceProvider( L"stardiv.uno.awt.UnoControlEditModel" );
195 XInterfaceRef xModel( xMSF->createInstance(L"stardiv.uno.awt.UnoControlEditModel") );
196 DBG_ASSERT( xModel, "No Model!" );
198 XPropertySetRef xPSet( xModel, USR_QUERY );
199 xPSet->setPropertyValue( L"Text", UsrAny( UString( L"Hallo!" ) ) );
200 xPSet->setPropertyValue( L"FontName", UsrAny( UString( L"Times New Roman" ) ) );
201 // xPSet->setPropertyValue( L"FontWeight", UsrAny( 200 ) );
203 XServiceProviderRef xEditCtrlProv = xProv->queryServiceProvider( L"stardiv.uno.awt.UnoControlEdit" );
204 // Edit1
205 XControlRef xEdit1( xMSF->createInstance(L"stardiv.uno.awt.UnoControlEdit"), USR_QUERY );
206 xModel->queryInterface( XControlModel::getSmartUik(), xCM );
207 xEdit1->setModel( xCM );
208 XWindowRef xEditCmp1( xEdit1, USR_QUERY );
209 xEditCmp1->setPosSize( 50, 50, 100, 60, PosSize_POSSIZE );
210 xCont->addControl( L"", xEdit1 );
211 // Edit2
212 XControlRef xEdit2( xMSF->createInstance(L"stardiv.uno.awt.UnoControlEdit"), USR_QUERY );
213 xModel->queryInterface( XControlModel::getSmartUik(), xCM );
214 xEdit2->setModel( xCM );
215 XWindowRef xEditCmp2( xEdit2, USR_QUERY );
216 XLayoutConstrainsRef xL( xEdit2, USR_QUERY );
217 if ( xL.is() )
219 Size aSz = xL->getPreferredSize();
220 xEditCmp2->setPosSize( 100, 180, aSz.Width(), aSz.Height(), PosSize_POSSIZE );
222 else
223 xEditCmp2->setPosSize( 100, 180, 100, 40, PosSize_POSSIZE );
224 xCont->addControl( L"", xEdit2 );
226 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.NumericField" );
227 XInterfaceRef xNumModel( xModelProv->createInstance(), USR_QUERY );
228 DBG_ASSERT( xNumModel, "No Model!" );
231 XServiceProviderRef xNumFieldProv = xProv->queryServiceProvider( L"stardiv.vcl.control.NumericField" );
232 XControlRef xNumField( xNumFieldProv->createInstance(), USR_QUERY );
233 xNumModel->queryInterface( XControlModel::getSmartUik(), xCM );
234 xNumField->setModel( xCM );
235 XWindowRef xNumFieldWin( xNumField, USR_QUERY );
236 xNumFieldWin->setPosSize( 50, 250, 100, 60, PosSize_POSSIZE );
237 xCont->addControl( L"", xNumField );
239 XMultiPropertySetRef xPSet2( xNumModel, USR_QUERY );
240 Sequence<UString> Names( 2 );
241 Names.getArray()[0] = L"Value";
242 Names.getArray()[1] = L"ValueMin";
243 Sequence<UsrAny> Values( 2 );
244 Values.getArray()[0] = UsrAny( -2000000.0 );
245 Values.getArray()[1] = UsrAny( -2000000.0 );
246 xPSet2->setPropertyValues( Names, Values );
249 // Button...
250 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.Button" );
251 xModelProv->createInstance()->queryInterface( XInterface::getSmartUik(), xModel );
252 DBG_ASSERT( xModel, "No Model!" );
254 xModel->queryInterface( XPropertySet::getSmartUik(), xPSet );
255 xPSet->setPropertyValue( L"Label", UsrAny( L"Press!" ) );
257 XServiceProviderRef xButtonCtrlProv = xProv->queryServiceProvider( L"stardiv.vcl.control.Button" );
258 XControlRef xButton1( xButtonCtrlProv->createInstance(), USR_QUERY );
259 xModel->queryInterface( XControlModel::getSmartUik(), xCM );
260 xButton1->setModel( xCM );
261 XWindowRef xButtonCmp1( xButton1, USR_QUERY );
262 xButtonCmp1->setPosSize( 170, 80, 80, 80, PosSize_POSSIZE );
263 // Unsichtbar, nur im MBDown mit DrawRoutine bei Doppel-Klick...
264 xButtonCmp1->setVisible( FALSE );
265 xDrawCtrl = xButton1;
267 xCont->addControl( L"", xButton1 );
271 // ListBox...
272 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.ListBox" );
273 xModel = (XInterface*)xModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
275 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
276 Sequence<UString> aSeq( 7 );
277 aSeq.getArray()[0] = L"Item1";
278 aSeq.getArray()[1] = L"Item2";
279 aSeq.getArray()[2] = L"Item3";
280 aSeq.getArray()[3] = L"Item4";
281 aSeq.getArray()[4] = L"Item5";
282 aSeq.getArray()[5] = L"Item6";
283 aSeq.getArray()[6] = L"Item7";
284 xPSet->setPropertyValue( L"StringItemList", UsrAny( &aSeq, Sequence<UString>::getReflection() ) );
285 xPSet->setPropertyValue( L"LineCount", UsrAny_UINT16( 4 ) );
286 xPSet->setPropertyValue( L"Dropdown", UsrAny( (BOOL)TRUE ) );
288 XServiceProviderRef xListBoxCtrlProv = xProv->queryServiceProvider( L"stardiv.vcl.control.ListBox" );
289 XControlRef xListBox1 = (XControl*)xListBoxCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
290 xListBox1->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
291 XWindowRef xListBoxCmp1 = (XWindow*)xListBox1->queryInterface( XWindow::getSmartUik() );
292 xListBoxCmp1->setPosSize( 20, 250, 200, 20, PosSize_POSSIZE );
293 xCont->addControl( L"", xListBox1 );
295 // FixedText...
296 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.FixedText" );
297 xModel = (XInterface*)xModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
298 DBG_ASSERT( xModel, "No Model!" );
300 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
301 xPSet->setPropertyValue( L"Label", UsrAny( L"Label:" ) );
302 xPSet->setPropertyValue( L"BackgroundColor", UsrAny( (UINT32)0x00888888) );
304 Font_Attribs aFontAttrs;
305 aFontAttrs.Italic = ITALIC_NORMAL;
306 xPSet->setPropertyValue( L"Font_Attribs", UsrAny( &aFontAttrs, Font_Attribs_getReflection() ) );
308 XPropertyStateRef xState = (XPropertyState*)xPSet->queryInterface( XPropertyState::getSmartUik() );
309 xState->getPropertyState( L"Font_Attribs" );
310 xState->getPropertyState( L"Font_Size" );
312 XServiceProviderRef xFixedTextCtrlProv = xProv->queryServiceProvider( L"stardiv.vcl.control.FixedText" );
313 XControlRef xFixedText1 = (XControl*)xFixedTextCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
314 DBG_ASSERT( xFixedText1, "No FixedText!" );
315 xFixedText1->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
316 XWindowRef xFixedTextCmp1 = (XWindow*)xFixedText1->queryInterface( XWindow::getSmartUik() );
317 xFixedTextCmp1->setPosSize( 20, 20, 120, 20, PosSize_POSSIZE );
318 xCont->addControl( L"", xFixedText1 );
320 // TabTest...
321 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.GroupBox" );
322 xModel = (XInterface*)xModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
323 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
324 xPSet->setPropertyValue( L"Label", UsrAny( L"Radio-Test:" ) );
325 XServiceProviderRef xGroupBoxCtrlProv = xProv->queryServiceProvider( L"stardiv.vcl.control.GroupBox" );
326 XControlRef xGroupBox1 = (XControl*)xGroupBoxCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
327 xGroupBox1->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
328 XWindowRef xGroupBoxCmp1 = (XWindow*)xGroupBox1->queryInterface( XWindow::getSmartUik() );
329 xGroupBoxCmp1->setPosSize( 30, 410, 100, 130, PosSize_POSSIZE );
330 xCont->addControl( L"", xGroupBox1 );
332 XServiceProviderRef xRadioButtonModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.RadioButton" );
333 XServiceProviderRef xRadioButtonCtrlProv = xProv->queryServiceProvider( L"stardiv.vcl.control.RadioButton" );
335 xModel = (XInterface*)xRadioButtonModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
336 XControlRef xT1 = (XControl*)xRadioButtonCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
337 xT1->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
338 XWindowRef xTC1 = (XWindow*)xT1->queryInterface( XWindow::getSmartUik() );
339 xTC1->setPosSize( 40, 430, 80, 20, PosSize_POSSIZE );
340 xCont->addControl( L"", xT1 );
341 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
342 xPSet->setPropertyValue( L"Label", UsrAny( L"Radio1" ) );
343 xPSet->setPropertyValue( L"State", UsrAny_UINT16( 1 ) );
345 xModel = (XInterface*)xRadioButtonModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
346 XControlRef xT2 = (XControl*)xRadioButtonCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
347 xT2->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
348 XWindowRef xTC2 = (XWindow*)xT2->queryInterface( XWindow::getSmartUik() );
349 xTC2->setPosSize( 40, 470, 80, 20, PosSize_POSSIZE );
350 xCont->addControl( L"", xT2 );
351 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
352 xPSet->setPropertyValue( L"Label", UsrAny( L"Radio2" ) );
354 xModel = (XInterface*)xRadioButtonModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
355 XControlRef xT3 = (XControl*)xRadioButtonCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
356 xT3->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
357 XWindowRef xTC3 = (XWindow*)xT3->queryInterface( XWindow::getSmartUik() );
358 xTC3->setPosSize( 40, 510, 80, 20, PosSize_POSSIZE );
359 xCont->addControl( L"", xT3 );
360 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
361 xPSet->setPropertyValue( L"Label", UsrAny( L"Radio3" ) );
363 xModel = (XInterface*)xRadioButtonModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
364 XControlRef xT4 = (XControl*)xRadioButtonCtrlProv->createInstance()->queryInterface( XControl::getSmartUik() );
365 xT4->setModel( (XControlModel*)xModel->queryInterface( XControlModel::getSmartUik() ) );
366 XWindowRef xTC4 = (XWindow*)xT4->queryInterface( XWindow::getSmartUik() );
367 xTC4->setPosSize( 40, 550, 80, 20, PosSize_POSSIZE );
368 xCont->addControl( L"", xT4 );
369 xPSet = (XPropertySet*)xModel->queryInterface( XPropertySet::getSmartUik() );
370 xPSet->setPropertyValue( L"Label", UsrAny( L"Radio4 - no" ) );
371 xPSet->setPropertyValue( L"Tabstop", UsrAny( (BOOL)TRUE ) );
373 // TabController:
374 xModelProv = xProv->queryServiceProvider( L"stardiv.vcl.controlmodel.TabController" );
375 xModel = (XInterface*)xModelProv->createInstance()->queryInterface( XInterface::getSmartUik() );
376 XTabControllerModelRef xTCModel = (XTabControllerModel*)xModel->queryInterface( XTabControllerModel::getSmartUik() );
378 XServiceProviderRef xTCProv = xProv->queryServiceProvider( L"stardiv.vcl.control.TabController" );
379 XTabControllerRef xTC = (XTabController*)xTCProv->createInstance()->queryInterface( XTabController::getSmartUik() );
380 xTC->setModel( (XTabControllerModel*)xModel->queryInterface( XTabControllerModel::getSmartUik() ) );
381 XUnoControlContainerRef xUCC = (XUnoControlContainer*)xCont->queryInterface( XUnoControlContainer::getSmartUik() );
382 xUCC->addTabController( xTC );
384 Sequence<XControlModelRef> aControls( 5 );
385 aControls.getArray()[0] = xGroupBox1->getModel();
386 aControls.getArray()[1] = xT1->getModel();
387 aControls.getArray()[2] = xT3->getModel();
388 aControls.getArray()[3] = xT2->getModel();
389 aControls.getArray()[4] = xT4->getModel();
390 xTCModel->setControls( aControls );
392 Sequence<XControlModelRef> aGroup( 3 );
393 aGroup.getArray()[0] = xT1->getModel();
394 aGroup.getArray()[1] = xT3->getModel();
395 aGroup.getArray()[2] = xT2->getModel();
396 xTCModel->setGroup( aGroup, L"test" );
398 // Container anzeigen...
399 // Als Child zu diesem Fenster
400 xContControl->createPeer( XToolkitRef(), GetComponentInterface( TRUE ) );
402 XDeviceRef xD( xContControl->getPeer(), USR_QUERY );
403 xG = xD->createGraphics();
404 XViewRef xV ( xDrawCtrl, USR_QUERY );
405 xV->setGraphics( xG );
407 // ((UnoControl*)(XControl*)xNumField))->updateFromModel();
410 // TEST:
411 WindowDecriptor aDescr;
412 aDescr.ComponentServiceName = "window";
413 aDescr.Type = VCLCOMPONENTTYPE_CONTAINER;
414 aDescr.Parent = GetComponentInterface( TRUE );
415 aDescr.WindowAttributes = WA_SHOW|WA_BORDER;
416 aDescr.Bounds = Rectangle( Point( 500, 50 ), Size( 300, 200 ) );
417 XVclWindowPeerRef xSPWin = xToolkit->createComponent( aDescr );
419 WindowDecriptor aDescr2;
420 aDescr2.ComponentServiceName = "scrollbar";
421 aDescr2.Type = VCLCOMPONENTTYPE_SIMPLE;
422 aDescr2.Parent = xSPWin;
423 aDescr2.WindowAttributes = WA_SHOW|WA_BORDER|WA_VSCROLL;
424 aDescr2.Bounds = Rectangle( Point( 250, 0 ), Size( 50, 200 ) );
425 XVclWindowPeerRef xSB = xToolkit->createComponent( aDescr2 );
428 return;
430 WorkWindow::MouseButtonDown( rMEvt );
434 // -----------------------------------------------------------------------
436 void MyWin::Resize()
438 WorkWindow::Resize();
441 void MyWin::Paint( const Rectangle& r )
443 // Muss ueber PaintListener geschehen...
444 if ( xDrawCtrl.is() )
446 XViewRef xV( xDrawCtrl, USR_QUERY );
447 XWindowRef xC( xDrawCtrl, USR_QUERY );
448 xV->draw( xC->getPosSize().Left(), xC->getPosSize().Top() );