OSDN Git Service

import source-tree based svn r84.
[bluegriffon/BlueGriffon.git] / base / content / bluegriffon / js / col.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Mozilla.org.
15  *
16  * The Initial Developer of the Original Code is
17  * Neil Marshall.
18  * Portions created by the Initial Developer are Copyright (C) 2003
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Neil Marshall (neil.marshall@sympatico.ca), Original author
23  *   Daniel Glazman (glazman@disruptive-innovations.com), on behalf of Linspire Inc.
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 var colours;
40 var satSlider = new objColour();
41 var hexChars = "0123456789ABCDEF";
42 var selectedColour = 0;
43 var mouseDown = false;
44 var eventInitiator = null;
45 var mouseX, mouseY, offsetLeft, offsetTop;
46
47 var gColor = "";
48 var LastPickedColor = "";
49 var ColorType = "Text";
50 var TextType = false;
51 var HighlightType = false;
52 var TableOrCell = false;
53 var LastPickedIsDefault = false;
54 var NoDefault = false;
55 var gColorObj;
56
57 function StartUp()
58 {
59
60   if (!window.arguments[1])
61   {
62     dump("colourPicker: Missing color object param\n");
63     return;
64   }
65
66   // window.arguments[1] is object to get initial values and return color data
67   gColorObj = window.arguments[1];
68   gColorObj.Cancel = false;
69
70   gDialog.red              = document.getElementById("red");
71   gDialog.blue             = document.getElementById("blue");
72   gDialog.green            = document.getElementById("green");
73   gDialog.hue              = document.getElementById("hue");
74   gDialog.saturation       = document.getElementById("saturation");
75   gDialog.brightness       = document.getElementById("brightness");
76   gDialog.hexColour        = document.getElementById("hexColour");
77   gDialog.nameColour       = document.getElementById("nameColour");
78
79   gDialog.redLabel = document.getElementById("redLabel");
80   gDialog.blueLabel = document.getElementById("blueLabel");
81   gDialog.greenLabel = document.getElementById("greenLabel");
82   gDialog.hueLabel = document.getElementById("hueLabel");
83   gDialog.saturationLabel = document.getElementById("saturationLabel");
84   gDialog.brightnessLabel = document.getElementById("brightnessLabel");
85   gDialog.hexColourLabel = document.getElementById("hexColourLabel");
86   gDialog.nameColourLabel = document.getElementById("nameColourLabel");
87
88   gDialog.hueAndSaturationImg = document.getElementById("hueAndSaturationImg");
89   gDialog.hueAndSaturationCrosshair = document.getElementById("hueAndSaturationCrosshair");
90   gDialog.brightnessImg    = document.getElementById("brightnessImg");
91   gDialog.swatch           = document.getElementById("swatch");
92   gDialog.brightnessArrow  = document.getElementById("brightnessArrow");
93   gDialog.colorpicker      = document.getElementById("colorpicker");
94   gDialog.Ok               = document.documentElement.getButton("accept");
95   gDialog.transparencyCheckbox = document.getElementById("transparencyCheckbox");
96
97   gDialog.CellOrTableGroup = document.getElementById("CellOrTableGroup");
98   gDialog.TableRadio       = document.getElementById("TableRadio");
99   gDialog.CellRadio        = document.getElementById("CellRadio");
100   gDialog.LastPickedColor  = document.getElementById("LastPickedColor");
101
102   // The type of color we are setting: 
103   //  text: Text, Link, ActiveLink, VisitedLink, 
104   //  or background: Page, Table, or Cell
105   var prefs = GetPrefs();
106   if (gColorObj.Type)
107   {
108     ColorType = gColorObj.Type;
109     // Get string for dialog title from passed-in type 
110     //   (note constraint on editor.properties string name)
111     var IsCSSPrefChecked = prefs.getBoolPref("editor.use_css");
112
113     if (GetCurrentEditor())
114     {
115       window.title = GetString(ColorType+"Color");
116       if (ColorType == "Page" && IsCSSPrefChecked && IsHTMLEditor())
117         window.title = GetString("BlockColor");
118     }
119   }
120   if (!window.title)
121     window.title = GetString("Color");
122
123   colours = new objColour();
124
125   makeDraggable(gDialog.hueAndSaturationImg);
126   makeDraggable(gDialog.hueAndSaturationCrosshair);
127   makeDraggable(gDialog.brightnessImg);
128
129   gDialog.hexColour.value = "";
130   var tmpColor;
131   var haveTableRadio = false;
132   var showTransparencyCheckbox = false;
133
134   switch (ColorType)
135   {
136     case "Page":
137       tmpColor = gColorObj.PageColor;
138       if (tmpColor && tmpColor.toLowerCase() != "window")
139         gColor = tmpColor;
140       showTransparencyCheckbox = true;
141       break;
142     case "Table":
143       if (gColorObj.TableColor)
144         gColor = gColorObj.TableColor;
145       break;
146     case "Cell":
147       if (gColorObj.CellColor)
148         gColor = gColorObj.CellColor;
149       break;
150     case "TableOrCell":
151       TableOrCell = true;
152       document.getElementById("TableOrCellGroup").collapsed = false;
153       showTransparencyCheckbox = true;
154       haveTableRadio = true;
155       if (gColorObj.SelectedType == "Cell")
156       {
157         gColor = gColorObj.CellColor;
158         gDialog.CellOrTableGroup.selectedItem = gDialog.CellRadio;
159         gDialog.CellRadio.focus();
160       }
161       else
162       {
163         gColor = gColorObj.TableColor;
164         gDialog.CellOrTableGroup.selectedItem = gDialog.TableRadio;
165         gDialog.TableRadio.focus();
166       }
167       break;
168     case "Highlight":
169       HighlightType = true;
170       if (gColorObj.HighlightColor)
171         gColor = gColorObj.HighlightColor;
172       showTransparencyCheckbox = true;
173       break;
174     default:
175       // Any other type will change some kind of text,
176       TextType = true;
177       tmpColor = gColorObj.TextColor;
178       if (tmpColor && tmpColor.toLowerCase() != "windowtext")
179         gColor = gColorObj.TextColor;
180       break;
181   }
182
183   if (!gColor)
184   {
185     var useCustomColors = prefs.getBoolPref("editor.use_custom_colors");
186     switch (ColorType)
187     {
188       case "Page":
189       case "Highlight":
190         if (useCustomColors)
191           gColor = prefs.getCharPref("editor.background_color");
192         else
193         {
194           gColor = prefs.getCharPref("browser.display.background_color");
195         }
196         break;
197       case "Table":
198       case "Cell":
199       case "TableOrCell":
200         gColor = "transparent";
201         showTransparencyCheckbox = true;
202         break;
203       default:
204         if (useCustomColors)
205           gColor = prefs.getCharPref("editor.text_color");
206         else
207         {
208           gColor = prefs.getCharPref("browser.display.foreground_color");
209         }
210         break;
211     }
212   }
213
214   if (!showTransparencyCheckbox)
215     gDialog.transparencyCheckbox.setAttribute("hidden", true);
216
217   // Use last-picked colors passed in, or those persistent on dialog
218   if (TextType)
219   {
220     if ( !("LastTextColor" in gColorObj) || !gColorObj.LastTextColor)
221       gColorObj.LastTextColor = gDialog.LastPickedColor.getAttribute("LastTextColor");
222     LastPickedColor = gColorObj.LastTextColor;
223   }
224   else if (HighlightType)
225   {
226     if ( !("LastHighlightColor" in gColorObj) || !gColorObj.LastHighlightColor)
227       gColorObj.LastHighlightColor = gDialog.LastPickedColor.getAttribute("LastHighlightColor");
228     LastPickedColor = gColorObj.LastHighlightColor;
229   }
230   else
231   {
232     if ( !("LastBackgroundColor" in gColorObj) || !gColorObj.LastBackgroundColor)
233       gColorObj.LastBackgroundColor = gDialog.LastPickedColor.getAttribute("LastBackgroundColor");
234     LastPickedColor = gColorObj.LastBackgroundColor;
235   }
236   gDialog.LastPickedColor.setAttribute("style","background-color: "+LastPickedColor);
237
238   // Set initial color in input field and in the colorpicker
239   SetCurrentColor(gColor);
240   if (!showTransparencyCheckbox)
241     gDialog.colorpicker.initColor(gColor);
242
243   // Caller can prevent user from submitting an empty, i.e., default color
244   NoDefault = gColorObj.NoDefault;
245   if (NoDefault)
246   {
247     // Hide the "Default button -- user must pick a color
248     var defaultColorButton = document.getElementById("DefaultColorButton");
249     if (defaultColorButton)
250       defaultColorButton.collapsed = true;
251   }
252
253   // Set focus to colorpicker if not set to table radio buttons above
254   if (!haveTableRadio)
255     gDialog.colorpicker.focus();
256
257   SetWindowLocation();
258 }
259
260 function ValidateData()
261 {
262   if (gDialog.transparencyCheckbox.checked)
263     gColor = "transparent";
264   else if (LastPickedIsDefault)
265     gColor = LastPickedColor;
266   else
267     gColor = gDialog.hexColour.value;
268
269   if (ColorType == "TableOrCell" &&
270       gColor == "transparent")
271     gColor = "";
272   gColor = TrimString(gColor).toLowerCase();
273
274   // TODO: Validate the color string!
275
276   if (NoDefault && !gColor)
277   {
278     ShowInputErrorMessage(GetString("NoColorError"));
279     SetTextboxFocus(gDialog.hexColour);
280     return false;   
281   }
282   return true;
283 }
284
285 function onAccept()
286 {
287   if (!ValidateData())
288     return false;
289
290   // Set return values and save in persistent color attributes
291   if (TextType)
292   {
293     gColorObj.TextColor = gColor;
294     if (gColor.length > 0)
295     {
296       gDialog.LastPickedColor.setAttribute("LastTextColor", gColor);
297       gColorObj.LastTextColor = gColor;
298     }
299   }
300   else if (HighlightType)
301   {
302     gColorObj.HighlightColor = gColor;
303     if (gColor.length > 0)
304     {
305       gDialog.LastPickedColor.setAttribute("LastHighlightColor", gColor);
306       gColorObj.LastHighlightColor = gColor;
307     }
308   }
309   else
310   {
311     gColorObj.BackgroundColor = gColor;
312     if (gColor.length > 0)
313     {
314       gDialog.LastPickedColor.setAttribute("LastBackgroundColor", gColor);
315       gColorObj.LastBackgroundColor = gColor;
316     }
317     // If table or cell requested, tell caller which element to set on
318     if (TableOrCell && gDialog.TableRadio.selected)
319       gColorObj.Type = "Table";
320   }
321   SaveWindowLocation();
322
323   return true; // do close the window
324 }
325
326