Menu

[r2000]: / trunk / Src / UStatusBarMgr.pas  Maximize  Restore  History

Download this file

410 lines (372 with data), 13.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{
* UStatusBarMgr.pas
*
* Implements class that manages display of status information and hints in a
* status bar.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UStatusBarMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2007-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UStatusBarMgr;
interface
uses
// Delphi
Windows, Graphics, ComCtrls;
type
{
TStatusBarMgr:
Class that manages display of status information and hints in a status bar.
}
TStatusBarMgr = class(TObject)
strict private
var
fStatusBar: TStatusBar;
{Reference to managed status bar}
fSearchGlyph: TBitmap;
{Stores reference to glyph used to indicate kind of current search}
fModifiedGlyph: TBitmap;
{Stores glyph displayed when user database has been modified}
fSearchInfoVisible: Boolean;
{Flag noting whether search information is to be displayed or not}
fUserDBInfoVisible: Boolean;
{Flag noting whether user database modification indicator is displayed}
const
cDBPanel = 0; // index of database info panel
cSearchPanel = 1; // index of search info panel
cUserPanel = 2; // index of user-defined database info panel
cSimplePanel = cDBPanel; // index of simple message panel
procedure DrawPanel(StatusBar: TStatusBar; Panel: TStatusPanel;
const Rect: TRect);
{Handles status bar's OnDrawPanel event called whenever an owner-draw
status panel needs to be displayed.
@param StatusBar [in] Reference to status bar containing panel.
@param Panel [in] Reference to status bar panel to be drawn.
@param Rect [in] Bounding rectangle of panel relative to status bar.
}
procedure ShowSnippetsInfo;
{Displays snippets statistics in status bar.
}
procedure ShowSearchInfo;
{Causes information about a search to be displayed in status bar.
}
procedure HideSearchInfo;
{Prevents search information from being displayed in status bar.
}
procedure ShowUserDBInfo;
{Displays information about state of user defined database.
}
procedure HideUserDBInfo;
{Prevents user defined database information from being displayed in status
bar.
}
public
constructor Create(const SB: TStatusBar);
{Class constructor. Sets up object to manage a status bar.
@param SB [in] Status bar to be managed.
}
destructor Destroy; override;
{Class destructor. Tears down object.
}
procedure ShowHint(const Hint: string);
{Displays a hint in status bar or clears hint text if no hint is provided.
@param Hint [in] Hint to be displayed or '' to restore previous message.
}
procedure ShowSimpleMessage(const Msg: string);
{Displays a simple text message in first panel of status bar and hides
results displayed in other panels.
@param Msg [in] Message to be displayed.
}
procedure Update;
{Updates status bar and refreshes information displayed.
}
end;
implementation
{
NOTES
------------------------------------------------------------------------------
Status bar has three panels (indexed 0..2) used as follows:
+ Panel[0]: Displays database statistics or a simple prompt. Status bar
default drawing is used. When a simple prompty is displayed Panel[1] and
Panel[2] are hidden.
+ Panel[1]: Displays information about current search. A glyph indicating
search type is displayed. The panel is owner-drawn.
+ Panel[2]: Displays a modification flag and glyph if user defined database
has been modified since last save. Nothing is displayed when database is
not modified. The panel is owner-drawn.
The status bar is also used to display hints when the mouse passes over
various UI elements. This is done by switching the status bar into SimplePanel
mode and displaying the hint in the single panel. Once the hint is deactivated
the main display is restored by switching the status bar out of SimplePanel
mode.
}
uses
// Delphi
SysUtils, Forms,
// Project
DB.UMain, UQuery, USearch, UStructs;
{ TStatusBarMgr }
constructor TStatusBarMgr.Create(const SB: TStatusBar);
{Class constructor. Sets up object to manage a status bar.
@param SB [in] Status bar to be managed.
}
resourcestring
// Message displayed when user database has been modified
sModified = 'Modified';
begin
Assert(Assigned(SB), ClassName + '.Create: SB is nil');
inherited Create;
fSearchInfoVisible := False; // we don't display search info by default
fUserDBInfoVisible := False; // we don't display modified flag by default
fSearchGlyph := TBitmap.Create;
fModifiedGlyph := TBitmap.Create;
fModifiedGlyph.LoadFromResourceName(HInstance, 'MODIFIED');
// Record reference to status bar
fStatusBar := SB;
// Ensure auto-hinting is switched off
fStatusBar.AutoHint := False;
// Enable owner drawing for second panel in status bar
fStatusBar.OnDrawPanel := DrawPanel;
fStatusBar.Panels[cSearchPanel].Style := psOwnerDraw;
fStatusBar.Panels[cUserPanel].Style := psOwnerDraw;
fStatusBar.Panels[cUserPanel].Text := sModified;
end;
destructor TStatusBarMgr.Destroy;
{Class destructor. Tears down object.
}
begin
FreeAndNil(fModifiedGlyph);
FreeAndNil(fSearchGlyph);
inherited;
end;
procedure TStatusBarMgr.DrawPanel(StatusBar: TStatusBar;
Panel: TStatusPanel; const Rect: TRect);
{Handles status bar's OnDrawPanel event called whenever an owner-draw status
panel needs to be displayed.
@param StatusBar [in] Reference to status bar containing panel.
@param Panel [in] Reference to status bar panel to be drawn.
@param Rect [in] Bounding rectangle of panel relative to status bar.
}
procedure DrawGlyphInPanel(const Glyph: TBitmap; const Transparent: TColor);
{Draws a glyph at the left of a status bar panel, centred vertically.
@param Glyph [in] Glyph to be displayed.
@param Transparent [in] Colour key to be drawn transparently.
}
var
BoundsRect: TRectEx; // Bounding rectangle of panel
DestRect: TRect; // Rectangle in panel where glyph is drawn
SrcRect: TRect; // Bounding rectangle of glyph
begin
BoundsRect := Rect;
DestRect := TRectEx.CreateBounds(
BoundsRect.Left + 2,
BoundsRect.Top + (BoundsRect.Height - Glyph.Height) div 2,
Glyph.Width,
Glyph.Height
);
SrcRect := TRectEx.CreateBounds(0, 0, Glyph.Width, Glyph.Height);
StatusBar.Canvas.BrushCopy(DestRect, Glyph, SrcRect, Transparent);
end;
procedure DrawTextInPanel(const LeftMargin, RightMargin: Integer;
Text: string);
{Draws left aligned text in current status panel.
@param LeftMargin [in] Offset of text from left of panel.
@param RightMargin [in] Defines right hand edge of test in panel. Text is
clipped and an ellipsis drawn if it overflows this margin.
@param Text [in] Text to be displayed.
}
var
BoundsRect: TRectEx; // bounding rectangle of panel
TextRect: TRect; // rectangle in which to display text
TextH: Integer; // height of text
begin
BoundsRect := Rect;
TextH := StatusBar.Canvas.TextHeight(Text);
// need this rect calculation since vertical align flag to Canvas.TextRect
// doesn't work so we need to set top of text rect to required value
TextRect := TRectEx.Create(
BoundsRect.Left + LeftMargin,
BoundsRect.Top + (BoundsRect.Height - TextH) div 2,
BoundsRect.Right - RightMargin,
Rect.Bottom
);
StatusBar.Canvas.TextRect(
TextRect, Text, [tfLeft, tfTop, tfEndEllipsis]
);
end;
begin
// Clear the panel
StatusBar.Canvas.FillRect(Rect);
case Panel.ID of
cSearchPanel:
begin
// We do nothing else if there's no glyph or search info not to be shown
if not Assigned(fSearchGlyph) or not fSearchInfoVisible then
Exit;
DrawGlyphInPanel(fSearchGlyph, clFuchsia);
DrawTextInPanel(fSearchGlyph.Width + 6, 0, Panel.Text);
end;
cUserPanel:
begin
// We do nothing else if there's no glyph or user db info not to be shown
if not Assigned(fModifiedGlyph) or not fUserDBInfoVisible then
Exit;
DrawGlyphInPanel(fModifiedGlyph, clWhite);
DrawTextInPanel(fModifiedGlyph.Width + 6, 12, Panel.Text);
end
else
// Do nothing: we only support drawing of second panel (Panel.ID=1)
end;
end;
procedure TStatusBarMgr.HideSearchInfo;
{Prevents search information from being displayed in status bar.
}
begin
// This method does not directly hide the information, but flags that it
// should be hidden then causes the status bar to update itself using stored
// data. The DrawPanel method is called by the status bar to draw the required
// panel.
fSearchInfoVisible := False;
fStatusBar.Repaint;
end;
procedure TStatusBarMgr.HideUserDBInfo;
{Prevents user defined database information from being displayed in status
bar.
}
begin
// This method does not directly hide the information, but flags that it
// should be hidden then causes the status bar to update itself using stored
// data. The DrawPanel method is called by the status bar to draw the required
// panel.
fUserDBInfoVisible := False;
fStatusBar.Repaint;
end;
procedure TStatusBarMgr.ShowHint(const Hint: string);
{Displays a hint in status bar and restores previous message if no hint is
provided.
@param Hint [in] Hint to be displayed or '' to restore previous message.
}
begin
if Hint <> '' then
begin
// We have hint: hide status bar info and display hint
// we use simple panel to display hint in whole of status bar
fStatusBar.SimplePanel := True;
fStatusBar.SimpleText := Hint;
end
else
begin
// No hint: we restore normal status bar info by switching off simple panel
fStatusBar.SimplePanel := False;
fStatusBar.SimpleText := '';
end;
// Force status bar to repaint immediately
Application.ProcessMessages;
end;
procedure TStatusBarMgr.ShowSearchInfo;
{Causes information about a search to be displayed in status bar.
}
resourcestring
// Text displayed in search panel
sNoSearch = 'All snippets selected';
sSearchActive = '%d snippets selected';
begin
// This method does not directly display the information, but records it and
// causes the status bar to update itself using the stored data. The DrawPanel
// method is called by the status bar to draw the panel.
// Store text describing search result
if Query.CurrentSearch.IsNul then
fStatusBar.Panels[cSearchPanel].Text := sNoSearch
else
fStatusBar.Panels[cSearchPanel].Text
:= Format(sSearchActive, [Query.Selection.Count]);
// Store glyph that indicates current search type
fSearchGlyph.Assign((Query.CurrentSearch.Criteria as ISearchUIInfo).Glyph);
// Ensure search info panel of status bar is displayed
fSearchInfoVisible := True;
// Force status bar to repaint itself
fStatusBar.Repaint;
end;
procedure TStatusBarMgr.ShowSimpleMessage(const Msg: string);
{Displays a simple text message in first panel of status bar and hides results
displayed in other panels.
@param Msg [in] Message to be displayed.
}
begin
// Set first panel of status bar to message and force repaint
fStatusBar.Panels[cSimplePanel].Text := Msg;
Application.ProcessMessages;
// Hide any info displayed in other panels
HideSearchInfo;
HideUserDBInfo;
end;
procedure TStatusBarMgr.ShowSnippetsInfo;
{Displays snippets statistics in status bar.
}
var
TotalSnippets: Integer; // number of snippets in database
TotalUserSnippets: Integer; // number of user-defined snippets in database
TotalCategories: Integer; // total number of categories
DisplayText: string; // text to display in database
resourcestring
// status bar messages
sNoUserInfo = '%0:d snippets in %1:d categories';
sWithUserInfo = '%0:d snippets (%2:d user defined) in %1:d categories';
begin
// Calculate database stats
TotalSnippets := Database.Snippets.Count;
TotalUserSnippets := Database.Snippets.Count(True);
TotalCategories := Database.Categories.Count;
// Build display text and display it
if TotalUserSnippets = 0 then
DisplayText := Format(sNoUserInfo, [TotalSnippets, TotalCategories])
else
DisplayText := Format(
sWithUserInfo, [TotalSnippets, TotalCategories, TotalUserSnippets]
);
fStatusBar.Panels[cDBPanel].Text := DisplayText;
end;
procedure TStatusBarMgr.ShowUserDBInfo;
{Displays information about state of user defined database.
}
begin
// This method does not directly display the information, but records whether
// panel needs to be drawn or hidden. The DrawPanel method is called by the
// status bar to draw the panel.
// We hide message if database not updated
fUserDBInfoVisible := (Database as IDatabaseEdit).Updated;
fStatusBar.Repaint;
end;
procedure TStatusBarMgr.Update;
{Updates status bar and refreshes information displayed.
}
begin
ShowSnippetsInfo;
ShowUserDBInfo;
ShowSearchInfo;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.