1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 #include "protectiondlg.hxx"
32 #include "protectiondlg.hrc"
33 #include "scresid.hxx"
34 #include "tabprotection.hxx"
35 
36 #include <vcl/msgbox.hxx>
37 
38 
39 // The order must match that of the list box.
40 static const ScTableProtection::Option aOptions[] = {
41     ScTableProtection::SELECT_LOCKED_CELLS,
42     ScTableProtection::SELECT_UNLOCKED_CELLS,
43 };
44 static const sal_uInt16 nOptionCount = sizeof(aOptions)/sizeof(aOptions[0]);
45 
46 
47 ScTableProtectionDlg::ScTableProtectionDlg(Window* pParent) :
48     ModalDialog(pParent, ScResId(RID_SCDLG_TABPROTECTION)),
49 
50     maBtnProtect    (this, ScResId(BTN_PROTECT)),
51     maPassword1Text (this, ScResId(FT_PASSWORD1)),
52     maPassword1Edit (this, ScResId(ED_PASSWORD1)),
53     maPassword2Text (this, ScResId(FT_PASSWORD2)),
54     maPassword2Edit (this, ScResId(ED_PASSWORD2)),
55     maOptionsLine   (this, ScResId(FL_OPTIONS)),
56     maOptionsText   (this, ScResId(FT_OPTIONS)),
57     maOptionsListBox(this, ScResId(CLB_OPTIONS)),
58 
59     maBtnOk     (this, ScResId(BTN_OK)),
60     maBtnCancel (this, ScResId(BTN_CANCEL)),
61     maBtnHelp   (this, ScResId(BTN_HELP)),
62 
63     maSelectLockedCells(ScResId(ST_SELECT_LOCKED_CELLS)),
64     maSelectUnlockedCells(ScResId(ST_SELECT_UNLOCKED_CELLS))
65 {
66     Init();
67     FreeResource();
68 }
69 
70 ScTableProtectionDlg::~ScTableProtectionDlg()
71 {
72 }
73 
74 short ScTableProtectionDlg::Execute()
75 {
76     return ModalDialog::Execute();
77 }
78 
79 void ScTableProtectionDlg::SetDialogData(const ScTableProtection& rData)
80 {
81     for (sal_uInt16 i = 0; i < nOptionCount; ++i)
82         maOptionsListBox.CheckEntryPos(i, rData.isOptionEnabled(aOptions[i]));
83 }
84 
85 void ScTableProtectionDlg::WriteData(ScTableProtection& rData) const
86 {
87     rData.setProtected(maBtnProtect.IsChecked());
88 
89     // We assume that the two password texts match.
90     rData.setPassword(maPassword1Edit.GetText());
91 
92     for (sal_uInt16 i = 0; i < nOptionCount; ++i)
93         rData.setOption(aOptions[i], maOptionsListBox.IsChecked(i));
94 }
95 
96 void ScTableProtectionDlg::Init()
97 {
98     Link aLink = LINK( this, ScTableProtectionDlg, CheckBoxHdl );
99     maBtnProtect.SetClickHdl(aLink);
100 
101     aLink = LINK( this, ScTableProtectionDlg, OKHdl );
102     maBtnOk.SetClickHdl(aLink);
103 
104     aLink = LINK( this, ScTableProtectionDlg, PasswordModifyHdl );
105     maPassword1Edit.SetModifyHdl(aLink);
106     maPassword2Edit.SetModifyHdl(aLink);
107 
108     maOptionsListBox.SetUpdateMode(false);
109     maOptionsListBox.Clear();
110 
111     maOptionsListBox.InsertEntry(maSelectLockedCells);
112     maOptionsListBox.InsertEntry(maSelectUnlockedCells);
113 
114     maOptionsListBox.CheckEntryPos(0, true);
115     maOptionsListBox.CheckEntryPos(1, true);
116 
117     maOptionsListBox.SetUpdateMode(true);
118 
119     // Set the default state of the dialog.
120     maBtnProtect.Check(true);
121     maPassword1Edit.GrabFocus();
122 }
123 
124 void ScTableProtectionDlg::EnableOptionalWidgets(bool bEnable)
125 {
126     maPassword1Text.Enable(bEnable);
127     maPassword1Edit.Enable(bEnable);
128     maPassword2Text.Enable(bEnable);
129     maPassword2Edit.Enable(bEnable);
130     maOptionsLine.Enable(bEnable);
131     maOptionsText.Enable(bEnable);
132 
133     maOptionsListBox.Enable(bEnable);
134     maOptionsListBox.Invalidate();
135 }
136 
137 IMPL_LINK( ScTableProtectionDlg, CheckBoxHdl, CheckBox*, pBtn )
138 {
139     if (pBtn == &maBtnProtect)
140     {
141         bool bChecked = maBtnProtect.IsChecked();
142         EnableOptionalWidgets(bChecked);
143         maBtnOk.Enable(bChecked);
144     }
145 
146     return 0;
147 }
148 
149 IMPL_LINK( ScTableProtectionDlg, OKHdl, OKButton*, EMPTYARG )
150 {
151     EndDialog(RET_OK);
152     return 0;
153 }
154 
155 IMPL_LINK( ScTableProtectionDlg, PasswordModifyHdl, Edit*, EMPTYARG )
156 {
157     String aPass1 = maPassword1Edit.GetText();
158     String aPass2 = maPassword2Edit.GetText();
159     maBtnOk.Enable(aPass1.Equals(aPass2));
160     return 0;
161 }
162