1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 #include "protectiondlg.hxx"
28 #include "protectiondlg.hrc"
29 #include "scresid.hxx"
30 #include "tabprotection.hxx"
31 
32 #include <vcl/msgbox.hxx>
33 
34 
35 // The order must match that of the list box.
36 static const ScTableProtection::Option aOptions[] = {
37     ScTableProtection::SELECT_LOCKED_CELLS,
38     ScTableProtection::SELECT_UNLOCKED_CELLS,
39 };
40 static const sal_uInt16 nOptionCount = sizeof(aOptions)/sizeof(aOptions[0]);
41 
42 
ScTableProtectionDlg(Window * pParent)43 ScTableProtectionDlg::ScTableProtectionDlg(Window* pParent) :
44     ModalDialog(pParent, ScResId(RID_SCDLG_TABPROTECTION)),
45 
46     maBtnProtect    (this, ScResId(BTN_PROTECT)),
47     maPassword1Text (this, ScResId(FT_PASSWORD1)),
48     maPassword1Edit (this, ScResId(ED_PASSWORD1)),
49     maPassword2Text (this, ScResId(FT_PASSWORD2)),
50     maPassword2Edit (this, ScResId(ED_PASSWORD2)),
51     maOptionsLine   (this, ScResId(FL_OPTIONS)),
52     maOptionsText   (this, ScResId(FT_OPTIONS)),
53     maOptionsListBox(this, ScResId(CLB_OPTIONS)),
54 
55     maBtnOk     (this, ScResId(BTN_OK)),
56     maBtnCancel (this, ScResId(BTN_CANCEL)),
57     maBtnHelp   (this, ScResId(BTN_HELP)),
58 
59     maSelectLockedCells(ScResId(ST_SELECT_LOCKED_CELLS)),
60     maSelectUnlockedCells(ScResId(ST_SELECT_UNLOCKED_CELLS))
61 {
62     Init();
63     FreeResource();
64 }
65 
~ScTableProtectionDlg()66 ScTableProtectionDlg::~ScTableProtectionDlg()
67 {
68 }
69 
Execute()70 short ScTableProtectionDlg::Execute()
71 {
72     return ModalDialog::Execute();
73 }
74 
SetDialogData(const ScTableProtection & rData)75 void ScTableProtectionDlg::SetDialogData(const ScTableProtection& rData)
76 {
77     for (sal_uInt16 i = 0; i < nOptionCount; ++i)
78         maOptionsListBox.CheckEntryPos(i, rData.isOptionEnabled(aOptions[i]));
79 }
80 
WriteData(ScTableProtection & rData) const81 void ScTableProtectionDlg::WriteData(ScTableProtection& rData) const
82 {
83     rData.setProtected(maBtnProtect.IsChecked());
84 
85     // We assume that the two password texts match.
86     rData.setPassword(maPassword1Edit.GetText());
87 
88     for (sal_uInt16 i = 0; i < nOptionCount; ++i)
89         rData.setOption(aOptions[i], maOptionsListBox.IsChecked(i));
90 }
91 
Init()92 void ScTableProtectionDlg::Init()
93 {
94     Link aLink = LINK( this, ScTableProtectionDlg, CheckBoxHdl );
95     maBtnProtect.SetClickHdl(aLink);
96 
97     aLink = LINK( this, ScTableProtectionDlg, OKHdl );
98     maBtnOk.SetClickHdl(aLink);
99 
100     aLink = LINK( this, ScTableProtectionDlg, PasswordModifyHdl );
101     maPassword1Edit.SetModifyHdl(aLink);
102     maPassword2Edit.SetModifyHdl(aLink);
103 
104     maOptionsListBox.SetUpdateMode(false);
105     maOptionsListBox.Clear();
106 
107     maOptionsListBox.InsertEntry(maSelectLockedCells);
108     maOptionsListBox.InsertEntry(maSelectUnlockedCells);
109 
110     maOptionsListBox.CheckEntryPos(0, true);
111     maOptionsListBox.CheckEntryPos(1, true);
112 
113     maOptionsListBox.SetUpdateMode(true);
114 
115     // Set the default state of the dialog.
116     maBtnProtect.Check(true);
117     maPassword1Edit.GrabFocus();
118 }
119 
EnableOptionalWidgets(bool bEnable)120 void ScTableProtectionDlg::EnableOptionalWidgets(bool bEnable)
121 {
122     maPassword1Text.Enable(bEnable);
123     maPassword1Edit.Enable(bEnable);
124     maPassword2Text.Enable(bEnable);
125     maPassword2Edit.Enable(bEnable);
126     maOptionsLine.Enable(bEnable);
127     maOptionsText.Enable(bEnable);
128 
129     maOptionsListBox.Enable(bEnable);
130     maOptionsListBox.Invalidate();
131 }
132 
IMPL_LINK(ScTableProtectionDlg,CheckBoxHdl,CheckBox *,pBtn)133 IMPL_LINK( ScTableProtectionDlg, CheckBoxHdl, CheckBox*, pBtn )
134 {
135     if (pBtn == &maBtnProtect)
136     {
137         bool bChecked = maBtnProtect.IsChecked();
138         EnableOptionalWidgets(bChecked);
139         maBtnOk.Enable(bChecked);
140     }
141 
142     return 0;
143 }
144 
IMPL_LINK(ScTableProtectionDlg,OKHdl,OKButton *,EMPTYARG)145 IMPL_LINK( ScTableProtectionDlg, OKHdl, OKButton*, EMPTYARG )
146 {
147     EndDialog(RET_OK);
148     return 0;
149 }
150 
IMPL_LINK(ScTableProtectionDlg,PasswordModifyHdl,Edit *,EMPTYARG)151 IMPL_LINK( ScTableProtectionDlg, PasswordModifyHdl, Edit*, EMPTYARG )
152 {
153     String aPass1 = maPassword1Edit.GetText();
154     String aPass2 = maPassword2Edit.GetText();
155     maBtnOk.Enable(aPass1.Equals(aPass2));
156     return 0;
157 }
158