~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/wiki/test/wikisearchoptions.pas

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{ Search options offline wiki
 
2
 
 
3
  Copyright (C) 2012  Mattias Gaertner  mattias@freepascal.org
 
4
 
 
5
  This source is free software; you can redistribute it and/or modify it under
 
6
  the terms of the GNU General Public License as published by the Free
 
7
  Software Foundation; either version 2 of the License, or (at your option)
 
8
  any later version.
 
9
 
 
10
  This code is distributed in the hope that it will be useful, but WITHOUT ANY
 
11
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
12
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
13
  details.
 
14
 
 
15
  A copy of the GNU General Public License is available on the World Wide Web
 
16
  at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
 
17
  to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
18
  MA 02111-1307, USA.
 
19
 
 
20
}
 
21
unit WikiSearchOptions;
 
22
 
 
23
{$mode objfpc}{$H+}
 
24
 
 
25
interface
 
26
 
 
27
uses
 
28
  Classes, SysUtils, FileUtil, LazLogger, BasicCodeTools,
 
29
  CodeToolsStructs, WikiHelpManager, WikiStrConsts, WikiFormat, Forms, Controls,
 
30
  Graphics, Dialogs, ExtCtrls, StdCtrls, ComCtrls, Grids;
 
31
 
 
32
type
 
33
 
 
34
  { TWikiSearchOptsWnd }
 
35
 
 
36
  TWikiSearchOptsWnd = class(TForm)
 
37
    ImageList1: TImageList;
 
38
    LanguagesGroupBox: TGroupBox;
 
39
    LanguagesSplitter: TSplitter;
 
40
    LanguagesTreeView: TTreeView;
 
41
    ScoresGroupBox: TGroupBox;
 
42
    ScoresStringGrid: TStringGrid;
 
43
    procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
 
44
    procedure FormCreate(Sender: TObject);
 
45
    procedure LanguagesTreeViewMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
 
46
      {%H-}Shift: TShiftState; X, Y: Integer);
 
47
    procedure ScoresStringGridEditingDone(Sender: TObject);
 
48
  private
 
49
    FLanguages: TStringToStringTree;
 
50
    FOnOptionsChanged: TNotifyEvent;
 
51
    FScoring: TWHScoring;
 
52
    function GetLangCodeEnabled(const ID: string): boolean;
 
53
    function GetLanguages: string;
 
54
    function LangNodeTextToCode(NodeText: string): string;
 
55
    function LangToNodeText(LangID: string; Count: integer = -1): string;
 
56
    procedure SetLangCodeEnabled(const ID: string; AValue: boolean);
 
57
    procedure SetLanguages(AValue: string);
 
58
    procedure FillScoresGrid;
 
59
    function Score2String(s: single): string;
 
60
    procedure DoOptionsChanged;
 
61
  public
 
62
    property Languages: string read GetLanguages write SetLanguages;
 
63
    property LangCodeEnabled[const ID: string]: boolean read GetLangCodeEnabled
 
64
                                                       write SetLangCodeEnabled;
 
65
    procedure UpdateAvailableLanguages;
 
66
    procedure UpdateEnabledLanguages;
 
67
    property OnOptionsChanged: TNotifyEvent read FOnOptionsChanged write FOnOptionsChanged;
 
68
    property Scoring: TWHScoring read FScoring;
 
69
  end;
 
70
 
 
71
var
 
72
  WikiSearchOptsWnd: TWikiSearchOptsWnd = nil;
 
73
 
 
74
implementation
 
75
 
 
76
{$R *.lfm}
 
77
 
 
78
{ TWikiSearchOptsWnd }
 
79
 
 
80
procedure TWikiSearchOptsWnd.FormCreate(Sender: TObject);
 
81
begin
 
82
  FScoring:=TWHScoring.Create;
 
83
  FScoring.Assign(WikiHelp.DefaultScoring);
 
84
 
 
85
  Caption:=wrsWikiSearchOptions;
 
86
  LanguagesGroupBox.Caption:=wrsLanguages;
 
87
  FLanguages:=TStringToStringTree.Create(false);
 
88
  FLanguages['']:='1';
 
89
 
 
90
  ScoresGroupBox.Caption:=wrsScores;
 
91
  with ScoresStringGrid do begin
 
92
    RowCount:=9;
 
93
    Cells[0, 1]:=wrsPageTitleWholeWord;
 
94
    Cells[0, 2]:=wrsPageTitlePart;
 
95
    Cells[0, 3]:=wrsHeaderWholeWord;
 
96
    Cells[0, 4]:=wrsHeaderPart;
 
97
    Cells[0, 5]:=wrsTextWholeWord;
 
98
    Cells[0, 6]:=wrsTextPart;
 
99
    Cells[0, 7]:=wrsLinkWholeWord;
 
100
    Cells[0, 8]:=wrsLinkPart;
 
101
  end;
 
102
  FillScoresGrid;
 
103
end;
 
104
 
 
105
procedure TWikiSearchOptsWnd.FormClose(Sender: TObject;
 
106
  var CloseAction: TCloseAction);
 
107
begin
 
108
  FreeAndNil(FLanguages);
 
109
  FreeAndNil(FScoring);
 
110
end;
 
111
 
 
112
procedure TWikiSearchOptsWnd.LanguagesTreeViewMouseDown(Sender: TObject;
 
113
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
 
114
var
 
115
  TVNode: TTreeNode;
 
116
  LangID: String;
 
117
begin
 
118
  TVNode:=LanguagesTreeView.GetNodeAt(X,Y);
 
119
  if TVNode=nil then exit;
 
120
  if x>=TVNode.DisplayStateIconLeft then begin
 
121
    LangID:=LangNodeTextToCode(TVNode.Text);
 
122
    LangCodeEnabled[LangID]:=not LangCodeEnabled[LangID];
 
123
  end;
 
124
end;
 
125
 
 
126
procedure TWikiSearchOptsWnd.ScoresStringGridEditingDone(Sender: TObject);
 
127
var
 
128
  Category: TWHFitsCategory;
 
129
  FitsString: TWHFitsStringFlag;
 
130
  OldScore: Single;
 
131
  NewScore: Extended;
 
132
  Row: Integer;
 
133
  Col: Integer;
 
134
begin
 
135
  Row:=ScoresStringGrid.Row;
 
136
  Col:=ScoresStringGrid.Col;
 
137
  if Col=1 then begin
 
138
    case Row of
 
139
    1: begin Category:=whfcPageTitle; FitsString:=whfsWholeWord; end;
 
140
    2: begin Category:=whfcPageTitle; FitsString:=whfsPart; end;
 
141
    3: begin Category:=whfcHeader; FitsString:=whfsWholeWord; end;
 
142
    4: begin Category:=whfcHeader; FitsString:=whfsPart; end;
 
143
    5: begin Category:=whfcText; FitsString:=whfsWholeWord; end;
 
144
    6: begin Category:=whfcText; FitsString:=whfsPart; end;
 
145
    7: begin Category:=whfcLink; FitsString:=whfsWholeWord; end;
 
146
    8: begin Category:=whfcLink; FitsString:=whfsPart; end;
 
147
    else exit;
 
148
    end;
 
149
    OldScore:=Scoring.Phrases[Category,FitsString];
 
150
    NewScore:=StrToFloatDef(ScoresStringGrid.Cells[Col,Row],OldScore);
 
151
    if (NewScore<-10000) then NewScore:=-10000;
 
152
    if (NewScore>10000) then NewScore:=10000;
 
153
    ScoresStringGrid.Cells[Col,Row]:=Score2String(NewScore);
 
154
    if OldScore<>NewScore then begin
 
155
      Scoring.Phrases[Category,FitsString]:=NewScore;
 
156
      DoOptionsChanged;
 
157
    end;
 
158
  end;
 
159
end;
 
160
 
 
161
procedure TWikiSearchOptsWnd.SetLanguages(AValue: string);
 
162
var
 
163
  p: PChar;
 
164
  StartPos: PChar;
 
165
  i: Integer;
 
166
  Lang: String;
 
167
begin
 
168
  if AValue=GetLanguages then Exit;
 
169
  FLanguages.Clear;
 
170
  if AValue='' then begin
 
171
    // empty = only english,original
 
172
    FLanguages['']:='1';
 
173
  end else begin
 
174
    p:=PChar(AValue);
 
175
    while p^<>#0 do begin
 
176
      StartPos:=p;
 
177
      while not (p^ in [#0,',']) do inc(p);
 
178
      if p>StartPos then begin
 
179
        if StartPos^='-' then begin
 
180
          // not original language
 
181
        end else if StartPos^='*' then begin
 
182
          // fit any
 
183
          for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do
 
184
            FLanguages[LangNodeTextToCode(LanguagesTreeView.Items.TopLvlItems[i].Text)]:='1';
 
185
        end else  begin
 
186
          // a specific language
 
187
          Lang:=SubString(StartPos,p-StartPos);
 
188
          if WikiLangCodeToCaption(Lang)<>Lang then
 
189
            FLanguages[Lang]:='1';
 
190
        end;
 
191
      end;
 
192
      while p^=',' do inc(p);
 
193
    end;
 
194
  end;
 
195
  UpdateEnabledLanguages;
 
196
end;
 
197
 
 
198
procedure TWikiSearchOptsWnd.FillScoresGrid;
 
199
begin
 
200
  with ScoresStringGrid do begin
 
201
    Cells[1,1]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsWholeWord]);
 
202
    Cells[1,2]:=Score2String(Scoring.Phrases[whfcPageTitle,whfsPart]);
 
203
    Cells[1,3]:=Score2String(Scoring.Phrases[whfcHeader,whfsWholeWord]);
 
204
    Cells[1,4]:=Score2String(Scoring.Phrases[whfcHeader,whfsPart]);
 
205
    Cells[1,5]:=Score2String(Scoring.Phrases[whfcText,whfsWholeWord]);
 
206
    Cells[1,6]:=Score2String(Scoring.Phrases[whfcText,whfsPart]);
 
207
    Cells[1,7]:=Score2String(Scoring.Phrases[whfcLink,whfsWholeWord]);
 
208
    Cells[1,8]:=Score2String(Scoring.Phrases[whfcLink,whfsPart]);
 
209
  end;
 
210
end;
 
211
 
 
212
function TWikiSearchOptsWnd.Score2String(s: single): string;
 
213
begin
 
214
  Result:=FloatToStrF(s,ffGeneral,5,2);
 
215
end;
 
216
 
 
217
procedure TWikiSearchOptsWnd.DoOptionsChanged;
 
218
begin
 
219
  if Assigned(OnOptionsChanged) then
 
220
    OnOptionsChanged(Self);
 
221
end;
 
222
 
 
223
procedure TWikiSearchOptsWnd.SetLangCodeEnabled(const ID: string;
 
224
  AValue: boolean);
 
225
begin
 
226
  if AValue=GetLangCodeEnabled(ID) then exit;
 
227
  if AValue then
 
228
    FLanguages[ID]:='1'
 
229
  else
 
230
    FLanguages.Remove(ID);
 
231
  UpdateEnabledLanguages;
 
232
  DoOptionsChanged;
 
233
end;
 
234
 
 
235
function TWikiSearchOptsWnd.LangNodeTextToCode(NodeText: string): string;
 
236
var
 
237
  p: SizeInt;
 
238
begin
 
239
  p:=Pos(' (',NodeText);
 
240
  if p>0 then Delete(NodeText,p,length(NodeText));
 
241
  Result:=WikiLangCaptionToCode(NodeText);
 
242
end;
 
243
 
 
244
function TWikiSearchOptsWnd.LangToNodeText(LangID: string; Count: integer
 
245
  ): string;
 
246
begin
 
247
  Result:=WikiLangCodeToCaption(LangID);
 
248
  if Count<0 then
 
249
    Result+=' (?)'
 
250
  else
 
251
    Result+=' ('+IntToStr(Count)+')';
 
252
end;
 
253
 
 
254
procedure TWikiSearchOptsWnd.UpdateAvailableLanguages;
 
255
var
 
256
  Langs: TStringList;
 
257
  i: Integer;
 
258
  TVNode: TTreeNode;
 
259
  LangToCount: TStringToPointerTree;
 
260
  Lang: String;
 
261
  S2PItem: PStringToPointerTreeItem;
 
262
begin
 
263
  // collect all languages and count them
 
264
  Langs:=TStringList.Create;
 
265
  if WikiHelp.LoadComplete then begin
 
266
    LangToCount:=TStringToPointerTree.Create(true);
 
267
    try
 
268
      for i:=0 to WikiHelp.Converter.Count-1 do begin
 
269
        Lang:=WikiHelp.Converter[i].WikiLanguage;
 
270
        LangToCount[Lang]:=LangToCount[Lang]+1;
 
271
      end;
 
272
      for S2PItem in LangToCount do
 
273
        if S2PItem^.Name<>'' then
 
274
          Langs.Add(LangToNodeText(S2PItem^.Name,{%H-}PtrUInt(S2PItem^.Value)));
 
275
      Langs.Sort;
 
276
      Langs.Insert(0,LangToNodeText('',{%H-}PtrUInt(LangToCount[''])));
 
277
      Langs.Add(LangToNodeText('*',WikiHelp.Converter.Count));
 
278
    finally
 
279
      LangToCount.Free;
 
280
    end;
 
281
  end else begin
 
282
    Langs.Add(LangToNodeText(''));
 
283
    Langs.Add(LangToNodeText('*',0));
 
284
  end;
 
285
 
 
286
  LanguagesTreeView.BeginUpdate;
 
287
  try
 
288
    for i:=0 to Langs.Count-1 do begin
 
289
      if i<LanguagesTreeView.Items.TopLvlCount then begin
 
290
        TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
 
291
        TVNode.Text:=Langs[i];
 
292
      end else begin
 
293
        TVNode:=LanguagesTreeView.Items.Add(nil,Langs[i]);
 
294
      end;
 
295
    end;
 
296
    while LanguagesTreeView.Items.TopLvlCount>Langs.Count do
 
297
      LanguagesTreeView.Items.TopLvlItems[LanguagesTreeView.Items.TopLvlCount-1].Delete;
 
298
  finally
 
299
    LanguagesTreeView.EndUpdate;
 
300
    Langs.Free;
 
301
  end;
 
302
  UpdateEnabledLanguages;
 
303
end;
 
304
 
 
305
procedure TWikiSearchOptsWnd.UpdateEnabledLanguages;
 
306
var
 
307
  i: Integer;
 
308
  TVNode: TTreeNode;
 
309
  LangEnabled: Boolean;
 
310
begin
 
311
  for i:=0 to LanguagesTreeView.Items.TopLvlCount-1 do begin
 
312
    TVNode:=LanguagesTreeView.Items.TopLvlItems[i];
 
313
    LangEnabled:=LangCodeEnabled[LangNodeTextToCode(TVNode.Text)];
 
314
    if LangEnabled then
 
315
      TVNode.StateIndex:=1
 
316
    else
 
317
      TVNode.StateIndex:=0;
 
318
  end;
 
319
end;
 
320
 
 
321
function TWikiSearchOptsWnd.GetLangCodeEnabled(const ID: string): boolean;
 
322
begin
 
323
  Result:=FLanguages.Contains(ID);
 
324
end;
 
325
 
 
326
function TWikiSearchOptsWnd.GetLanguages: string;
 
327
var
 
328
  S2SItem: PStringToStringTreeItem;
 
329
begin
 
330
  Result:='';
 
331
  if not FLanguages.Contains('') then
 
332
    Result:='-';
 
333
  for S2SItem in FLanguages do begin
 
334
    if S2SItem^.Name='' then continue;
 
335
    if Result<>'' then Result+=',';
 
336
    Result+=S2SItem^.Name;
 
337
  end;
 
338
end;
 
339
 
 
340
end.
 
341