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 <comphelper/processfactory.hxx>
21 #include <com/sun/star/awt/PosSize.hpp>
22 #include <com/sun/star/sdbc/XRowSet.hpp>
23 #include <com/sun/star/sdb/XColumn.hpp>
24 #include <com/sun/star/sdb/CommandType.hpp>
25 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 #include <com/sun/star/awt/XWindow.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <vcl/builder.hxx>
31 #include <vcl/settings.hxx>
32 #include <vcl/fixed.hxx>
33 #include "general.hxx"
34 #include "bibresid.hxx"
35 #include "datman.hxx"
36 #include "bibconfig.hxx"
37 #include "bibprop.hxx"
38 #include <strings.hrc>
39 #include "bibmod.hxx"
40 #include "bibview.hxx"
41 #include "bibtools.hxx"
42 #include <helpids.h>
43 #include <tools/debug.hxx>
44 #include <vcl/svapp.hxx>
45 #include <vcl/i18nhelp.hxx>
46 #include <vcl/mnemonic.hxx>
47 #include <algorithm>
48 #include <functional>
49 #include <vector>
50 #include <tools/urlobj.hxx>
51 
52 using namespace ::com::sun::star;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::form;
55 using namespace ::com::sun::star::sdb;
56 
lcl_GetColumnName(const Mapping * pMapping,sal_uInt16 nIndexPos)57 static OUString lcl_GetColumnName( const Mapping* pMapping, sal_uInt16 nIndexPos )
58 {
59     BibConfig* pBibConfig = BibModul::GetConfig();
60     OUString sRet = pBibConfig->GetDefColumnName(nIndexPos);
61     if(pMapping)
62         for(const auto & aColumnPair : pMapping->aColumnPairs)
63         {
64             if(aColumnPair.sLogicalColumnName == sRet)
65             {
66                 sRet = aColumnPair.sRealColumnName;
67                 break;
68             }
69         }
70     return sRet;
71 }
72 
73 class BibPosListener    :public cppu::WeakImplHelper <sdbc::XRowSetListener>
74 {
75     VclPtr<BibGeneralPage>     pParentPage;
76 public:
77     explicit BibPosListener(BibGeneralPage* pParent);
78 
79     //XPositioningListener
80     virtual void SAL_CALL cursorMoved(const lang::EventObject& event) override;
rowChanged(const lang::EventObject &)81     virtual void SAL_CALL rowChanged(const lang::EventObject& /*event*/) override { /* not interested in */ }
rowSetChanged(const lang::EventObject &)82     virtual void SAL_CALL rowSetChanged(const lang::EventObject& /*event*/) override { /* not interested in */ }
83 
84     //XEventListener
85     virtual void SAL_CALL disposing(const lang::EventObject& Source) override;
86 
87 };
88 
BibPosListener(BibGeneralPage * pParent)89 BibPosListener::BibPosListener(BibGeneralPage* pParent) :
90     pParentPage(pParent)
91 {
92 }
93 
cursorMoved(const lang::EventObject &)94 void BibPosListener::cursorMoved(const lang::EventObject& /*aEvent*/)
95 {
96     try
97     {
98         uno::Reference< form::XBoundComponent >  xLstBox = pParentPage->GetTypeListBoxModel();
99         uno::Reference< beans::XPropertySet >  xPropSet(xLstBox, UNO_QUERY);
100         if(xPropSet.is())
101         {
102             BibConfig* pBibConfig = BibModul::GetConfig();
103             BibDataManager* pDatMan = pParentPage->GetDataManager();
104             BibDBDescriptor aDesc;
105             aDesc.sDataSource = pDatMan->getActiveDataSource();
106             aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
107             aDesc.nCommandType = CommandType::TABLE;
108 
109             const Mapping* pMapping = pBibConfig->GetMapping(aDesc);
110             OUString sTypeMapping = pBibConfig->GetDefColumnName(AUTHORITYTYPE_POS);
111             if(pMapping)
112             {
113                 for(const auto & aColumnPair : pMapping->aColumnPairs)
114                 {
115                     if(aColumnPair.sLogicalColumnName == sTypeMapping)
116                     {
117                         sTypeMapping = aColumnPair.sRealColumnName;
118                         break;
119                     }
120                 }
121             }
122             OUString uTypeMapping = sTypeMapping;
123 
124             uno::Reference< form::XForm >  xForm = pDatMan->getForm();
125             uno::Reference< sdbcx::XColumnsSupplier >  xSupplyCols(xForm, UNO_QUERY);
126             uno::Reference< container::XNameAccess >  xValueAcc;
127             if (xSupplyCols.is())
128                 xValueAcc = xSupplyCols->getColumns();
129 
130             sal_Int16 nTempVal = -1;
131             if(xValueAcc.is() && xValueAcc->hasByName(uTypeMapping))
132             {
133                 uno::Any aVal = xValueAcc->getByName(uTypeMapping);
134                 uno::Reference< sdb::XColumn >  xCol(aVal, UNO_QUERY);
135                 DBG_ASSERT(xCol.is(), "BibPosListener::cursorMoved : invalid column (no sdb::XColumn) !");
136                 if (xCol.is())
137                 {
138                     nTempVal = xCol->getShort();
139                     // getShort returns zero if the value is not a number
140                     if (!nTempVal || xCol->wasNull())
141                     {
142                         OUString sTempVal = xCol->getString();
143                         if(sTempVal != "0")
144                             nTempVal = -1;
145                     }
146                 }
147             }
148             if(nTempVal < 0 || nTempVal >= TYPE_COUNT)
149             {
150                 uno::Any aSel;
151                 uno::Sequence<sal_Int16> aSelSeq(1);
152                 sal_Int16* pArr = aSelSeq.getArray();
153                 pArr[0] = TYPE_COUNT;
154                 aSel <<= aSelSeq;
155                 xPropSet->setPropertyValue("SelectedItems", aSel);
156             }
157         }
158     }
159     catch(const Exception&)
160     {
161         OSL_FAIL("BibPosListener::cursorMoved: something went wrong !");
162     }
163 }
164 
disposing(const lang::EventObject &)165 void BibPosListener::disposing(const lang::EventObject& /*Source*/)
166 {
167 }
168 
BibGeneralPage(vcl::Window * pParent,BibDataManager * pMan)169 BibGeneralPage::BibGeneralPage(vcl::Window* pParent, BibDataManager* pMan):
170     TabPage(pParent, "GeneralPage", "modules/sbibliography/ui/generalpage.ui"),
171     BibShortCutHandler( this ),
172     mxBibGeneralPageFocusListener(new BibGeneralPageFocusListener(this)),
173     pDatMan(pMan)
174 {
175     get(pIdentifierFT, "shortname");
176     get(pAuthTypeFT, "authtype");
177     get(pGrid, "grid");
178     get(pScrolledWindow, "scrolledwindow");
179     get(pYearFT, "year");
180     get(pAuthorFT, "authors");
181     get(pTitleFT, "title");
182     get(pPublisherFT, "publisher");
183     get(pAddressFT, "address");
184     get(pISBNFT, "isbn");
185     get(pChapterFT, "chapter");
186     get(pPagesFT, "pages");
187     get(pEditorFT, "editor");
188     get(pEditionFT, "edition");
189     get(pBooktitleFT, "booktitle");
190     get(pVolumeFT, "volume");
191     get(pHowpublishedFT, "publicationtype");
192     get(pOrganizationsFT, "organization");
193     get(pInstitutionFT, "institution");
194     get(pSchoolFT, "university");
195     get(pReportTypeFT, "reporttype");
196     get(pMonthFT, "month");
197     get(pJournalFT, "journal");
198     get(pNumberFT, "number");
199     get(pSeriesFT, "series");
200     get(pAnnoteFT, "annotation");
201     get(pNoteFT, "note");
202     get(pURLFT, "url");
203     get(pCustom1FT, "custom1");
204     get(pCustom2FT, "custom2");
205     get(pCustom3FT, "custom3");
206     get(pCustom4FT, "custom4");
207     get(pCustom5FT, "custom5");
208 
209     InitFixedTexts();
210 
211     sal_Int16* pMap = nFT2CtrlMap;
212     for( sal_uInt16 i = 0 ; i < FIELD_COUNT ; ++i, ++pMap )
213     {
214         aControls[ i ] = nullptr;
215         *pMap = -1;
216     }
217 
218     BibConfig* pBibConfig = BibModul::GetConfig();
219     BibDBDescriptor aDesc;
220     aDesc.sDataSource = pDatMan->getActiveDataSource();
221     aDesc.sTableOrQuery = pDatMan->getActiveDataTable();
222     aDesc.nCommandType = CommandType::TABLE;
223     const Mapping* pMapping = pBibConfig->GetMapping(aDesc);
224 
225     xCtrlContnr = VCLUnoHelper::CreateControlContainer(pGrid);
226 
227     std::vector<vcl::Window*> aChildren;
228 
229     AddControlWithError(lcl_GetColumnName(pMapping, IDENTIFIER_POS), *pIdentifierFT,
230         sTableErrorString,
231         HID_BIB_IDENTIFIER_POS, 0, aChildren);
232 
233     sTypeColumnName = lcl_GetColumnName(pMapping, AUTHORITYTYPE_POS);
234 
235     AddControlWithError(sTypeColumnName, *pAuthTypeFT, sTableErrorString,
236         HID_BIB_AUTHORITYTYPE_POS, 1, aChildren);
237 
238     AddControlWithError(lcl_GetColumnName(pMapping, YEAR_POS), *pYearFT,
239         sTableErrorString, HID_BIB_YEAR_POS, 2, aChildren);
240 
241     AddControlWithError(lcl_GetColumnName(pMapping, AUTHOR_POS), *pAuthorFT,
242         sTableErrorString, HID_BIB_AUTHOR_POS, 3, aChildren);
243 
244     AddControlWithError(lcl_GetColumnName(pMapping, TITLE_POS), *pTitleFT, sTableErrorString,
245         HID_BIB_TITLE_POS, 4, aChildren);
246 
247     AddControlWithError(lcl_GetColumnName(pMapping, PUBLISHER_POS), *pPublisherFT,
248         sTableErrorString, HID_BIB_PUBLISHER_POS, 5, aChildren);
249 
250     AddControlWithError(lcl_GetColumnName(pMapping, ADDRESS_POS), *pAddressFT,
251         sTableErrorString, HID_BIB_ADDRESS_POS, 6, aChildren);
252 
253     AddControlWithError(lcl_GetColumnName(pMapping, ISBN_POS), *pISBNFT,
254         sTableErrorString, HID_BIB_ISBN_POS, 7, aChildren);
255 
256     AddControlWithError(lcl_GetColumnName(pMapping, CHAPTER_POS), *pChapterFT,
257         sTableErrorString, HID_BIB_CHAPTER_POS, 8, aChildren);
258 
259     AddControlWithError(lcl_GetColumnName(pMapping, PAGES_POS), *pPagesFT,
260         sTableErrorString, HID_BIB_PAGES_POS, 9, aChildren);
261 
262     AddControlWithError(lcl_GetColumnName(pMapping, EDITOR_POS), *pEditorFT,
263         sTableErrorString, HID_BIB_EDITOR_POS, 10, aChildren);
264 
265     AddControlWithError(lcl_GetColumnName(pMapping, EDITION_POS), *pEditionFT,
266         sTableErrorString, HID_BIB_EDITION_POS, 11, aChildren);
267 
268     AddControlWithError(lcl_GetColumnName(pMapping, BOOKTITLE_POS), *pBooktitleFT,
269         sTableErrorString, HID_BIB_BOOKTITLE_POS, 12, aChildren);
270 
271     AddControlWithError(lcl_GetColumnName(pMapping, VOLUME_POS), *pVolumeFT,
272         sTableErrorString, HID_BIB_VOLUME_POS, 13, aChildren);
273 
274     AddControlWithError(lcl_GetColumnName(pMapping, HOWPUBLISHED_POS), *pHowpublishedFT,
275         sTableErrorString, HID_BIB_HOWPUBLISHED_POS, 14, aChildren);
276 
277     AddControlWithError(lcl_GetColumnName(pMapping, ORGANIZATIONS_POS), *pOrganizationsFT,
278         sTableErrorString, HID_BIB_ORGANIZATIONS_POS, 15, aChildren);
279 
280     AddControlWithError(lcl_GetColumnName(pMapping, INSTITUTION_POS), *pInstitutionFT,
281         sTableErrorString, HID_BIB_INSTITUTION_POS, 16, aChildren);
282 
283     AddControlWithError(lcl_GetColumnName(pMapping, SCHOOL_POS), *pSchoolFT,
284         sTableErrorString, HID_BIB_SCHOOL_POS, 17, aChildren);
285 
286     AddControlWithError(lcl_GetColumnName(pMapping, REPORTTYPE_POS), *pReportTypeFT,
287         sTableErrorString, HID_BIB_REPORTTYPE_POS, 18, aChildren);
288 
289     AddControlWithError(lcl_GetColumnName(pMapping, MONTH_POS), *pMonthFT,
290         sTableErrorString, HID_BIB_MONTH_POS, 19, aChildren);
291 
292     AddControlWithError(lcl_GetColumnName(pMapping, JOURNAL_POS), *pJournalFT,
293         sTableErrorString, HID_BIB_JOURNAL_POS, 20, aChildren);
294 
295     AddControlWithError(lcl_GetColumnName(pMapping, NUMBER_POS), *pNumberFT,
296         sTableErrorString, HID_BIB_NUMBER_POS, 21, aChildren);
297 
298     AddControlWithError(lcl_GetColumnName(pMapping, SERIES_POS), *pSeriesFT,
299         sTableErrorString, HID_BIB_SERIES_POS, 22, aChildren);
300 
301     AddControlWithError(lcl_GetColumnName(pMapping, ANNOTE_POS), *pAnnoteFT,
302         sTableErrorString, HID_BIB_ANNOTE_POS, 23, aChildren);
303 
304     AddControlWithError(lcl_GetColumnName(pMapping, NOTE_POS),*pNoteFT,
305         sTableErrorString, HID_BIB_NOTE_POS, 24, aChildren);
306 
307     AddControlWithError(lcl_GetColumnName(pMapping, URL_POS), *pURLFT,
308         sTableErrorString, HID_BIB_URL_POS, 25, aChildren);
309 
310     AddControlWithError(lcl_GetColumnName(pMapping, CUSTOM1_POS), *pCustom1FT,
311         sTableErrorString, HID_BIB_CUSTOM1_POS, 26, aChildren);
312 
313     AddControlWithError(lcl_GetColumnName(pMapping, CUSTOM2_POS), *pCustom2FT,
314         sTableErrorString, HID_BIB_CUSTOM2_POS, 27, aChildren);
315 
316     AddControlWithError(lcl_GetColumnName(pMapping, CUSTOM3_POS), *pCustom3FT,
317         sTableErrorString, HID_BIB_CUSTOM3_POS, 28, aChildren);
318 
319     AddControlWithError(lcl_GetColumnName(pMapping, CUSTOM4_POS), *pCustom4FT,
320         sTableErrorString, HID_BIB_CUSTOM4_POS, 29, aChildren);
321 
322     AddControlWithError(lcl_GetColumnName(pMapping, CUSTOM5_POS), *pCustom5FT,
323         sTableErrorString, HID_BIB_CUSTOM5_POS, 30, aChildren);
324 
325     BuilderUtils::reorderWithinParent(aChildren, false);
326 
327     xPosListener = new BibPosListener(this);
328     uno::Reference< sdbc::XRowSet >  xRowSet(pDatMan->getForm(), UNO_QUERY);
329     if(xRowSet.is())
330         xRowSet->addRowSetListener(xPosListener);
331     uno::Reference< form::runtime::XFormController > xFormCtrl = pDatMan->GetFormController();
332     xFormCtrl->setContainer(xCtrlContnr);
333     xFormCtrl->activateTabOrder();
334 
335     if(!sTableErrorString.isEmpty())
336         sTableErrorString = BibResId(ST_ERROR_PREFIX) + sTableErrorString;
337 
338     SetText(BibResId(ST_TYPE_TITLE));
339 
340     Size aSize(LogicToPixel(Size(0, 209), MapMode(MapUnit::MapAppFont)));
341     set_height_request(aSize.Height());
342 }
343 
~BibGeneralPage()344 BibGeneralPage::~BibGeneralPage()
345 {
346     disposeOnce();
347 }
348 
dispose()349 void BibGeneralPage::dispose()
350 {
351     if (pDatMan && xPosListener.is())
352     {
353         uno::Reference< sdbc::XRowSet >  xRowSet(pDatMan->getForm(), UNO_QUERY);
354         if(xRowSet.is())
355             xRowSet->removeRowSetListener(xPosListener);
356     }
357     pGrid.clear();
358     pScrolledWindow.clear();
359     pIdentifierFT.clear();
360     pAuthTypeFT.clear();
361     pYearFT.clear();
362     pAuthorFT.clear();
363     pTitleFT.clear();
364     pPublisherFT.clear();
365     pAddressFT.clear();
366     pISBNFT.clear();
367     pChapterFT.clear();
368     pPagesFT.clear();
369     pEditorFT.clear();
370     pEditionFT.clear();
371     pBooktitleFT.clear();
372     pVolumeFT.clear();
373     pHowpublishedFT.clear();
374     pOrganizationsFT.clear();
375     pInstitutionFT.clear();
376     pSchoolFT.clear();
377     pReportTypeFT.clear();
378     pMonthFT.clear();
379     pJournalFT.clear();
380     pNumberFT.clear();
381     pSeriesFT.clear();
382     pAnnoteFT.clear();
383     pNoteFT.clear();
384     pURLFT.clear();
385     pCustom1FT.clear();
386     pCustom2FT.clear();
387     pCustom3FT.clear();
388     pCustom4FT.clear();
389     pCustom5FT.clear();
390     for (auto & a: aFixedTexts) a.clear();
391     mxBibGeneralPageFocusListener.clear();
392     TabPage::dispose();
393 }
394 
RemoveListeners()395 void BibGeneralPage::RemoveListeners()
396 {
397     for(uno::Reference<awt::XWindow> & aControl : aControls)
398     {
399         if(aControl.is())
400         {
401             aControl->removeFocusListener( mxBibGeneralPageFocusListener.get() );
402             aControl = nullptr;
403         }
404     }
405 }
406 
CommitActiveControl()407 void BibGeneralPage::CommitActiveControl()
408 {
409     uno::Reference< form::runtime::XFormController > xFormCtrl = pDatMan->GetFormController();
410     uno::Reference< awt::XControl >  xCurr = xFormCtrl->getCurrentControl();
411     if(xCurr.is())
412     {
413         uno::Reference< awt::XControlModel >  xModel = xCurr->getModel();
414         uno::Reference< form::XBoundComponent >  xBound(xModel, UNO_QUERY);
415         if(xBound.is())
416             xBound->commit();
417     }
418 }
419 
AddControlWithError(const OUString & rColumnName,FixedText & rLabel,OUString & rErrorString,const OString & sHelpId,sal_uInt16 nIndexInFTArray,std::vector<vcl::Window * > & rChildren)420 void BibGeneralPage::AddControlWithError( const OUString& rColumnName, FixedText &rLabel,
421     OUString& rErrorString, const OString& sHelpId, sal_uInt16 nIndexInFTArray, std::vector<vcl::Window*> &rChildren)
422 {
423     const OUString aColumnUIName(rLabel.GetText());
424     // adds also the XControl and creates a map entry in nFT2CtrlMap[] for mapping between control and FT
425 
426     sal_Int16                                   nIndex = -1;
427     uno::Reference< awt::XControlModel >    xTmp = AddXControl(rColumnName, rLabel, sHelpId, nIndex, rChildren);
428     if( xTmp.is() )
429     {
430         DBG_ASSERT( nIndexInFTArray < FIELD_COUNT, "*BibGeneralPage::AddControlWithError(): wrong array index!" );
431         DBG_ASSERT( nFT2CtrlMap[ nIndexInFTArray ] < 0, "+BibGeneralPage::AddControlWithError(): index already in use!" );
432 
433         nFT2CtrlMap[ nIndexInFTArray ] = nIndex;
434     }
435     else
436     {
437         if( !rErrorString.isEmpty() )
438             rErrorString += "\n";
439 
440         rErrorString += MnemonicGenerator::EraseAllMnemonicChars( aColumnUIName );
441     }
442 }
443 
AddXControl(const OUString & rName,FixedText & rLabel,const OString & sHelpId,sal_Int16 & rIndex,std::vector<vcl::Window * > & rChildren)444 uno::Reference< awt::XControlModel >  BibGeneralPage::AddXControl(
445         const OUString& rName,
446         FixedText& rLabel, const OString& sHelpId, sal_Int16& rIndex,
447         std::vector<vcl::Window*>& rChildren)
448 {
449     uno::Reference< awt::XControlModel >  xCtrModel;
450     try
451     {
452         const bool bTypeListBox = sTypeColumnName == rName;
453         xCtrModel = pDatMan->loadControlModel(rName, bTypeListBox);
454         if ( xCtrModel.is() )
455         {
456             uno::Reference< beans::XPropertySet >  xPropSet( xCtrModel, UNO_QUERY );
457 
458             if( xPropSet.is())
459             {
460                 uno::Reference< beans::XPropertySetInfo >  xPropInfo = xPropSet->getPropertySetInfo();
461 
462                 OUString aControlName;
463                 if (bTypeListBox)
464                 {
465                     aControlName = "com.sun.star.form.control.ListBox";
466                     xLBModel.set(xCtrModel, UNO_QUERY);
467                 }
468                 else
469                 {
470                     uno::Any aAny = xPropSet->getPropertyValue( "DefaultControl" );
471                     aAny >>= aControlName;
472                 }
473 
474                 OUString uProp("HelpURL");
475                 if(xPropInfo->hasPropertyByName(uProp))
476                 {
477                     OUString sId( INET_HID_SCHEME );
478                     DBG_ASSERT( INetURLObject( OStringToOUString( sHelpId, RTL_TEXTENCODING_UTF8 ) ).GetProtocol() == INetProtocol::NotValid, "Wrong HelpId!" );
479                     sId += OStringToOUString( sHelpId, RTL_TEXTENCODING_UTF8 );
480                     xPropSet->setPropertyValue( uProp, makeAny( sId ) );
481                 }
482 
483                 uno::Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
484                 uno::Reference< awt::XControl > xControl( xContext->getServiceManager()->createInstanceWithContext(aControlName, xContext), UNO_QUERY);
485                 if ( xControl.is() )
486                 {
487                     xControl->setModel( xCtrModel);
488 
489                     // Peer as Child to the FrameWindow
490                     xCtrlContnr->addControl(rName, xControl);
491                     uno::Reference< awt::XWindow >  xCtrWin(xControl, UNO_QUERY );
492                     xCtrWin->addFocusListener( mxBibGeneralPageFocusListener.get() );
493                     rIndex = -1;    // -> implies, that not found
494                     for(sal_uInt16 i = 0; i < FIELD_COUNT; i++)
495                         if(!aControls[i].is())
496                         {
497                             aControls[i] = xCtrWin;
498                             rIndex = sal_Int16( i );
499                             break;
500                         }
501                     // initially switch on the design mode - switch it off _after_ loading the form
502                     xCtrWin->setVisible( true );
503                     xControl->setDesignMode( true );
504 
505                     VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow(xControl->getPeer());
506                     pWindow->set_grid_top_attach(rLabel.get_grid_top_attach());
507                     pWindow->set_grid_left_attach(rLabel.get_grid_left_attach()+1);
508                     pWindow->set_valign(VclAlign::Center);
509                     rLabel.set_mnemonic_widget(pWindow);
510                     if (&rLabel == pTitleFT)
511                         pWindow->set_grid_width(3);
512                     else
513                         pWindow->set_hexpand(true);
514                     rChildren.push_back(&rLabel);
515                     rChildren.push_back(pWindow);
516                 }
517             }
518         }
519     }
520     catch(const Exception&)
521     {
522         OSL_FAIL("BibGeneralPage::AddXControl: something went wrong!");
523     }
524     return xCtrModel;
525 }
526 
InitFixedTexts()527 void BibGeneralPage::InitFixedTexts()
528 {
529     aFixedTexts[0] = pIdentifierFT;
530     aFixedTexts[1] = pAuthTypeFT;
531     aFixedTexts[2] = pYearFT;
532     aFixedTexts[3] = pAuthorFT;
533     aFixedTexts[4] = pTitleFT;
534     aFixedTexts[5] = pPublisherFT;
535     aFixedTexts[6] = pAddressFT;
536     aFixedTexts[7] = pISBNFT;
537     aFixedTexts[8] = pChapterFT;
538     aFixedTexts[9] = pPagesFT;
539 
540     aFixedTexts[10] = pEditorFT;
541     aFixedTexts[11] = pEditionFT;
542     aFixedTexts[12] = pBooktitleFT;
543     aFixedTexts[13] = pVolumeFT;
544     aFixedTexts[14] = pHowpublishedFT;
545     aFixedTexts[15] = pOrganizationsFT;
546     aFixedTexts[16] = pInstitutionFT;
547     aFixedTexts[17] = pSchoolFT;
548     aFixedTexts[18] = pReportTypeFT;
549     aFixedTexts[19] = pMonthFT;
550 
551     aFixedTexts[20] = pJournalFT;
552     aFixedTexts[21] = pNumberFT;
553     aFixedTexts[22] = pSeriesFT;
554     aFixedTexts[23] = pAnnoteFT;
555     aFixedTexts[24] = pNoteFT;
556     aFixedTexts[25] = pURLFT;
557 
558     aFixedTexts[26] = pCustom1FT;
559     aFixedTexts[27] = pCustom2FT;
560     aFixedTexts[28] = pCustom3FT;
561     aFixedTexts[29] = pCustom4FT;
562     aFixedTexts[30] = pCustom5FT;
563 
564     int                 i;
565 
566     MnemonicGenerator   aMnemonicGenerator;
567 
568     OUString aFixedStrings[ FIELD_COUNT ];
569     for( i = 0 ; i < FIELD_COUNT ; ++i )
570         aFixedStrings[i] = aFixedTexts[i]->GetText();
571 
572     // init mnemonics, first register all strings
573     for( i = 0 ; i < FIELD_COUNT ; ++i )
574         aMnemonicGenerator.RegisterMnemonic( aFixedStrings[ i ] );
575 
576     // ... then get all strings
577     for( i = 0 ; i < FIELD_COUNT ; ++i )
578         aMnemonicGenerator.CreateMnemonic( aFixedStrings[ i ] );
579 
580     // set texts
581     for( i = 0 ; i < FIELD_COUNT ; ++i )
582         aFixedTexts[ i ]->SetText( aFixedStrings[ i ] );
583 }
584 
focusGained(const awt::FocusEvent & rEvent)585 void BibGeneralPage::focusGained(const awt::FocusEvent& rEvent)
586 {
587     Reference<awt::XWindow> xCtrWin(rEvent.Source, UNO_QUERY );
588     if(xCtrWin.is())
589     {
590         ::Size aOutSize = pScrolledWindow->getVisibleChildSize();
591         awt::Rectangle aRect = xCtrWin->getPosSize();
592         Point aOffset(pGrid->GetPosPixel());
593         long nX = aRect.X + aOffset.X();
594         if (nX < 0 || nX > aOutSize.Width())
595         {
596             pScrolledWindow->getHorzScrollBar().DoScroll(aRect.X);
597         }
598 
599         long nY = aRect.Y + aOffset.Y();
600         if (nY < 0 || nY > aOutSize.Height())
601         {
602             pScrolledWindow->getVertScrollBar().DoScroll(aRect.Y);
603         }
604     }
605 }
606 
focusLost()607 void BibGeneralPage::focusLost()
608 {
609     CommitActiveControl();
610 }
611 
GetFocus()612 void BibGeneralPage::GetFocus()
613 {
614     Reference< awt::XWindow >*  pxControl = aControls;
615 
616     for( int i = FIELD_COUNT ; i ; --i, ++pxControl )
617     {
618         if( pxControl->is() )
619         {
620             ( *pxControl )->setFocus();
621             return;
622         }
623     }
624 
625     // fallback
626     GrabFocus();
627 }
628 
HandleShortCutKey(const KeyEvent & rKeyEvent)629 bool BibGeneralPage::HandleShortCutKey( const KeyEvent& rKeyEvent )
630 {
631     DBG_ASSERT( KEY_MOD2 == rKeyEvent.GetKeyCode().GetModifier(), "+BibGeneralPage::HandleShortCutKey(): this is not for me!" );
632 
633     const vcl::I18nHelper&      rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
634     const sal_Unicode           c = rKeyEvent.GetCharCode();
635     bool                        bHandled = false;
636 
637     sal_Int16                   i;
638 
639     std::vector<sal_Int16>::size_type nFocused = 0xFFFF;  // index of focused in vector, no one focused initial
640     DBG_ASSERT( nFocused > 0, "*BibGeneralPage::HandleShortCutKey(): size_type works not as expected!" );
641 
642     std::vector<sal_Int16>            aMatchList;
643 
644     for( i = 0 ; i < FIELD_COUNT ; ++i )
645     {
646         if( rI18nHelper.MatchMnemonic( aFixedTexts[ i ]->GetText(), c ) )
647         {
648             bHandled = true;
649             sal_Int16           nCtrlIndex = nFT2CtrlMap[ i ];
650 
651             if( nCtrlIndex >= 0 )
652             {   // store index of control
653                 DBG_ASSERT( aControls[ nCtrlIndex ].is(), "-BibGeneralPage::HandleShortCutKey(): valid index and no control?" );
654 
655                 uno::Reference< awt::XControl >  xControl( aControls[ nCtrlIndex ], UNO_QUERY );
656                 DBG_ASSERT( xControl.is(), "-BibGeneralPage::HandleShortCutKey(): a control which is not a control!" );
657 
658                 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xControl->getPeer() );
659 
660                 if( pWindow )
661                 {
662                     aMatchList.push_back( nCtrlIndex );
663                     if( pWindow->HasChildPathFocus() )
664                     {   // save focused control
665                         DBG_ASSERT( nFocused == 0xFFFF, "+BibGeneralPage::HandleShortCutKey(): more than one with focus?!" );
666                         DBG_ASSERT( !aMatchList.empty(), "+BibGeneralPage::HandleShortCutKey(): push_back and no content?!" );
667                         nFocused = aMatchList.size() - 1;
668                     }
669                 }
670             }
671         }
672     }
673 
674     if( bHandled )
675     {
676         DBG_ASSERT( !aMatchList.empty(), "*BibGeneralPage::HandleShortCutKey(): be prepared to crash..." );
677 
678         if( nFocused >= ( aMatchList.size() - 1 ) )
679             // >=... includes 0xFFFF
680             // no one or last focused, take first
681             nFocused = 0;
682         else
683             // take next one
684             nFocused++;
685 
686         aControls[ aMatchList[ nFocused ] ]->setFocus();
687     }
688 
689     return bHandled;
690 }
691 
BibGeneralPageFocusListener(BibGeneralPage * pBibGeneralPage)692 BibGeneralPageFocusListener::BibGeneralPageFocusListener(BibGeneralPage *pBibGeneralPage): mpBibGeneralPage(pBibGeneralPage)
693 {}
694 
focusGained(const css::awt::FocusEvent & e)695 void BibGeneralPageFocusListener::focusGained( const css::awt::FocusEvent& e )
696 {
697     mpBibGeneralPage->focusGained(e);
698 }
699 
focusLost(const css::awt::FocusEvent &)700 void BibGeneralPageFocusListener::focusLost( const css::awt::FocusEvent& )
701 {
702     mpBibGeneralPage->focusLost();
703 }
704 
disposing(const css::lang::EventObject &)705 void BibGeneralPageFocusListener::disposing( const css::lang::EventObject& )
706 {}
707 
708 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
709