|
| 1 | +unit bvBookMark; |
| 2 | + |
| 3 | +interface |
| 4 | + |
| 5 | +uses |
| 6 | +{$ifndef LINUX} |
| 7 | + Windows, Messages, Graphics, Controls, Forms, Dialogs, |
| 8 | + DBGrids,Grids, StdCtrls, Buttons,ExtCtrls, Menus, ComCtrls, |
| 9 | +{$else} |
| 10 | + QForms, |
| 11 | + QStdCtrls, |
| 12 | + QButtons, |
| 13 | + QExtCtrls, |
| 14 | + QDBGrids, |
| 15 | + QMenus, |
| 16 | +{$endif} |
| 17 | + SysUtils, |
| 18 | + DB, |
| 19 | + Classes, |
| 20 | + bvFindDialogUnit,bvfindUnit,bvGridPopupUnit,bvLocalization; |
| 21 | + |
| 22 | +type |
| 23 | + TbvCommonBookMarkMenuItem = class(tbvCommonGridMenuItem); |
| 24 | + TbvBookMarkMenuItem = class(tbvCommonGridMenuItem); |
| 25 | + TbvBookMark1MenuItem = class(tbvCommonGridMenuItem); |
| 26 | + TbvBookMarkPopupMenu = class(TbvCommonGridPopupMenu); |
| 27 | + |
| 28 | +type |
| 29 | + TbvBookMark = class(TComponent) |
| 30 | + private |
| 31 | + IsLoaded:boolean; |
| 32 | + FThisGrid :TDBGrid; |
| 33 | + FAutoPopup:boolean; |
| 34 | + FBookMark:Array[0..9] of TBookMark; |
| 35 | + |
| 36 | +// procedure ThFind(Sender: TObject); |
| 37 | + procedure SetThisGrid(Value:TDBGrid); |
| 38 | + { Private declarations } |
| 39 | + procedure MenuClick(Sender:TObject); |
| 40 | + procedure MenuGotoClick(Sender:TObject); |
| 41 | + |
| 42 | + protected |
| 43 | + { Protected declarations } |
| 44 | + procedure Notification(AComponent: TComponent;Operation: TOperation); override; |
| 45 | + public |
| 46 | + { Public declarations } |
| 47 | + procedure Loaded; override; |
| 48 | + constructor Create(AOwner:TComponent); override; |
| 49 | + destructor Destroy; override; |
| 50 | + function Check:boolean; |
| 51 | +// function MessageHook(var Msg: TMessage): Boolean; override; |
| 52 | + published |
| 53 | + { Published declarations } |
| 54 | + property ThisGrid:TDBGrid read FThisGrid write SetThisGrid; |
| 55 | + property AutoPopup:boolean read FAutoPopup write FAutoPopup default true; |
| 56 | +end; |
| 57 | + |
| 58 | + |
| 59 | +implementation |
| 60 | + |
| 61 | +{$R bvBookM.res} |
| 62 | + |
| 63 | + |
| 64 | +uses bvMessageUnit,bvDBGrid; |
| 65 | + |
| 66 | +const |
| 67 | + CharArr :array[0..9] of char = '0123456789'; |
| 68 | + |
| 69 | +constructor TbvBookMark.Create(AOwner:TComponent); |
| 70 | +var i:integer; |
| 71 | +Begin |
| 72 | + inherited Create(AOwner); |
| 73 | + IsLoaded:=false; |
| 74 | + FAutoPopup:=True; |
| 75 | + if AOwner is TbvDBGrid then FThisGrid:=TbvdbGrid(AOwner) |
| 76 | + else FThisGrid:=nil; |
| 77 | + |
| 78 | + for i:=low(FBookMark) to high(FBookMark) do |
| 79 | + FBookmark[i]:=nil; |
| 80 | +end; |
| 81 | + |
| 82 | + |
| 83 | +procedure TbvBookMark.SetThisGrid(Value:TDBGrid); |
| 84 | +var ThItem,thItem1:TMenuItem; |
| 85 | + i:integer; |
| 86 | +begin |
| 87 | + if (Value is TDBGrid) then begin |
| 88 | + if (Value<>FThisGrid) and not (Owner=Value) then begin |
| 89 | +//// |
| 90 | + if FAutoPopup and IsLoaded and not (csdesigning in componentState) then begin |
| 91 | + if Assigned(FThisGrid) and Assigned(FThisGrid.PopupMenu) then begin |
| 92 | + if FThisGrid.PopupMenu is TbvBookMarkPopupMenu then begin |
| 93 | +// FThisGrid.PopupMenu.Items.Items.free; |
| 94 | + FThisGrid.PopupMenu.Free; |
| 95 | + FThisGrid.Popupmenu:=nil; |
| 96 | + end |
| 97 | + else begin |
| 98 | + i:=0; |
| 99 | + while i<FThisGrid.PopupMenu.Items.Count do |
| 100 | + if ((FThisGrid.PopupMenu.Items[i] is TbvCommonBookMarkMenuItem) |
| 101 | + ) |
| 102 | + and (FThisGrid.PopupMenu.Items[i].Owner=Self) |
| 103 | + then begin |
| 104 | + //if assigned( FThisGrid.PopupMenu.Items.Items) |
| 105 | + //then FThisGrid.PopupMenu.Items.Items.free; |
| 106 | + |
| 107 | + FThisGrid.PopupMenu.Items.Delete(i); |
| 108 | + end |
| 109 | + else inc(i); |
| 110 | + end; |
| 111 | + end; |
| 112 | + if Assigned(Value) then begin |
| 113 | + if (Value.PopupMenu=nil) then begin |
| 114 | + Value.PopupMenu:=TbvBookMarkPopupMenu.create(Self); |
| 115 | +// (Value.PopupMenu as tbvFinderPopupMenu).DrawText:='Òàáëèöà'; |
| 116 | + end |
| 117 | + else if |
| 118 | + not (Value.PopupMenu is TbvCommonGridPopupMenu) |
| 119 | + and (Value.popupmenu.items.count>0) and |
| 120 | + not ((Value.popupmenu.items[Value.popupmenu.items.count-1] is tbvCommonGridMenuItem) |
| 121 | + ) |
| 122 | + then begin |
| 123 | + ThItem:=tbvCommonBookMarkMenuItem.Create(self); |
| 124 | + ThItem.Caption:='-'; |
| 125 | + Value.PopupMenu.items.Add(ThItem); |
| 126 | + end; |
| 127 | + |
| 128 | + thItem1:=TbvCommonBookMarkMenuItem.create(Self); |
| 129 | + thItem1.caption:=StrBookmarks; |
| 130 | + |
| 131 | + //ThItem1.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMark'); |
| 132 | + ThItem1.Bitmap.LoadFromResourceName(hInstance, 'bvBookMark'); |
| 133 | + Value.PopupMenu.items.Add(ThItem1); |
| 134 | + |
| 135 | + for i:=0 to 9 do begin |
| 136 | + ThItem:=tbvBookMarkMenuItem.Create(self); |
| 137 | + ThItem.Caption:=inttostr(i); |
| 138 | + thItem.ShortCut:=ShortCut(word(chararr[i]),[ssAlt]); |
| 139 | + ThItem.OnClick:=MenuClick; |
| 140 | + if i=0 then |
| 141 | + //ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMark'); |
| 142 | + ThItem.Bitmap.LoadFromResourceName(Hinstance,'bvBookMark'); |
| 143 | + |
| 144 | + thItem1.add(ThItem); |
| 145 | + end; |
| 146 | + for i:=0 to 9 do begin |
| 147 | + ThItem:=tbvBookMark1MenuItem.Create(self); |
| 148 | + ThItem.Caption:=inttostr(i); |
| 149 | + thItem.ShortCut:=ShortCut(word(chararr[i]),[ssctrl]); |
| 150 | + ThItem.OnClick:=MenuGotoClick; |
| 151 | + if i=0 then |
| 152 | + //ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMarkGOTO'); |
| 153 | + ThItem.Bitmap.loadfromresourcename(Hinstance,'bvBookMarkGOTO'); |
| 154 | + //Value.PopupMenu.items.Add(ThItem); |
| 155 | + thItem1.add(ThItem); |
| 156 | + end; |
| 157 | + //thItem2:=NewSubMenu( '',0,'SubMenu'+Value.Name,ARR); |
| 158 | + //thItem1.Add(thItem2); |
| 159 | + |
| 160 | + //SetLength(Arr,0); |
| 161 | + end; |
| 162 | + end; |
| 163 | + |
| 164 | +///// |
| 165 | + FThisGrid:=Value; |
| 166 | + end; |
| 167 | + end |
| 168 | + else FThisGrid:=nil |
| 169 | +// else ShowMessage('Òîëüêî äëÿ Tdbgrid'); |
| 170 | +end; |
| 171 | + |
| 172 | +{ |
| 173 | +function bv_find.MessageHook(var Msg: TMessage): Boolean; |
| 174 | +begin |
| 175 | + Result:= inherited MessageHook(Msg); |
| 176 | + if (Msg.Msg=WM_DESTROY) and (Assigned(ThisGrid)) then begin |
| 177 | + (ThisGrid as TDBGrid).Options:=OldOptions; |
| 178 | + end; |
| 179 | +end; |
| 180 | +} |
| 181 | + |
| 182 | +function TbvBookMark.Check:boolean; |
| 183 | +var DATA:TDataSource; |
| 184 | +begin |
| 185 | + Result:=false; |
| 186 | + if (ThisGrid=nil) then begin |
| 187 | + bvMessageError(StrErrorNotDefinedGrid); |
| 188 | + exit; |
| 189 | + end; |
| 190 | + |
| 191 | + if (ThisGrid is TdbGrid) then DATA:=(ThisGrid as TDBGrid).DataSource |
| 192 | + else Data:=nil; |
| 193 | + |
| 194 | + if DATA=nil then begin |
| 195 | + bvMessageError(strErrorNotDefinedObject); |
| 196 | + exit; |
| 197 | + end; |
| 198 | + |
| 199 | + if Data.DataSet=nil then begin |
| 200 | + bvMessageError(StrErrorNotDefinedGrid); |
| 201 | + exit; |
| 202 | + end; |
| 203 | + |
| 204 | + if Data.DataSet.Active=false then begin |
| 205 | + bvMessageError(StrErrorNotDefinedTable); |
| 206 | + exit; |
| 207 | + end; |
| 208 | + |
| 209 | + Result:=true |
| 210 | +end; |
| 211 | + |
| 212 | +procedure TbvBookMark.MenuClick(Sender:tobject); |
| 213 | +var i:integer; |
| 214 | + str:string; |
| 215 | +begin |
| 216 | + if Check then begin |
| 217 | + str:=ShortCutToText((Sender as TMenuItem).ShortCut); |
| 218 | + if pos('0',str)>0 then i:=0 |
| 219 | + else if pos('1',str)>0 then i:=1 |
| 220 | + else if pos('2',str)>0 then i:=2 |
| 221 | + else if pos('3',str)>0 then i:=3 |
| 222 | + else if pos('4',str)>0 then i:=4 |
| 223 | + else if pos('5',str)>0 then i:=5 |
| 224 | + else if pos('6',str)>0 then i:=6 |
| 225 | + else if pos('7',str)>0 then i:=7 |
| 226 | + else if pos('8',str)>0 then i:=8 |
| 227 | + else if pos('9',str)>0 then i:=9 |
| 228 | + else i:=10; |
| 229 | + |
| 230 | + if i in [0..9] |
| 231 | + then begin |
| 232 | + if Assigned(FBookMark[i]) then FreeMem(FBookMark[i]); |
| 233 | + FBookMark[i]:=FThisGrid.datasource.dataset.GetBookMark; |
| 234 | + end |
| 235 | + end; |
| 236 | +end; |
| 237 | + |
| 238 | +procedure TbvBookMark.MenuGOTOClick(Sender:tobject); |
| 239 | +var str:string; |
| 240 | + i:integer; |
| 241 | +begin |
| 242 | + if Check then begin |
| 243 | + str:=ShortCutToText((Sender as TMenuItem).ShortCut); |
| 244 | + if pos('0',str)>0 then i:=0 |
| 245 | + else if pos('1',str)>0 then i:=1 |
| 246 | + else if pos('2',str)>0 then i:=2 |
| 247 | + else if pos('3',str)>0 then i:=3 |
| 248 | + else if pos('4',str)>0 then i:=4 |
| 249 | + else if pos('5',str)>0 then i:=5 |
| 250 | + else if pos('6',str)>0 then i:=6 |
| 251 | + else if pos('7',str)>0 then i:=7 |
| 252 | + else if pos('8',str)>0 then i:=8 |
| 253 | + else if pos('9',str)>0 then i:=9 |
| 254 | + else i:=10; |
| 255 | + |
| 256 | + if i in [0..9] |
| 257 | + then begin |
| 258 | + if Assigned(FBookMark[i]) |
| 259 | + and FThisGrid.datasource.dataset.BookmarkValid(FBookMark[i]) |
| 260 | + then FThisGrid.datasource.dataset.GotoBookMark(FBookMark[i]); |
| 261 | + end |
| 262 | + end; |
| 263 | +end; |
| 264 | + |
| 265 | + |
| 266 | +procedure tbvBookMark.Loaded; |
| 267 | +var ThItem,thItem1:TMenuItem; |
| 268 | + i:integer; |
| 269 | +begin |
| 270 | + inherited; |
| 271 | + |
| 272 | + if not IsLoaded then begin |
| 273 | + IsLoaded:=true; |
| 274 | + if Assigned(FThisGrid) |
| 275 | + and (FThisGrid is tbvDBGrid) |
| 276 | + and (Owner<>FThisGrid) |
| 277 | + and (FThisGrid as TbvDBGrid).bvBookMark.AutoPopup |
| 278 | + then exit; |
| 279 | + |
| 280 | + |
| 281 | + if Assigned(FThisGrid) then begin |
| 282 | + if FAutoPopup and not (csDesigning in ComponentState) then begin |
| 283 | + if (ThisGrid.PopupMenu=nil) then begin |
| 284 | + ThisGrid.PopupMenu:=tbvBookMarkpopupmenu.Create(Self); |
| 285 | +// (ThisGrid.PopupMenu as tbvFinderPopupMenu).DrawText:='Òàáëèöà'; |
| 286 | + end |
| 287 | + else if |
| 288 | + not (ThisGrid.PopupMenu is TbvCommonGridPopupMenu) |
| 289 | + and (ThisGrid.popupmenu.items.count>0) and |
| 290 | + not ((ThisGrid.popupmenu.items[ThisGrid.popupmenu.items.count-1] is tbvCommonGridMenuItem) |
| 291 | + ) |
| 292 | + then begin |
| 293 | + ThItem:=tbvBookMarkMenuItem.Create(self); |
| 294 | + ThItem.Caption:='-'; |
| 295 | + ThisGrid.PopupMenu.items.Add(ThItem); |
| 296 | + end; |
| 297 | + |
| 298 | + thItem1:=Tbvcommonbookmarkmenuitem.create(Self); |
| 299 | + thItem1.caption:=StrBookmarks; |
| 300 | + //ThItem1.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMark'); |
| 301 | + ThItem1.Bitmap.LoadFromResourceName(Hinstance,'bvBookMark'); |
| 302 | + |
| 303 | + ThisGrid.PopupMenu.items.Add(ThItem1); |
| 304 | + |
| 305 | + //SEtLength(Arr,20); |
| 306 | + for i:=0 to 9 do begin |
| 307 | + ThItem:=tbvBookMarkMenuItem.Create(self); |
| 308 | + ThItem.Caption:=inttostr(i); |
| 309 | + thItem.ShortCut:=ShortCut(word(chararr[i]),[ssAlt]); |
| 310 | + ThItem.OnClick:=MenuClick; |
| 311 | + if i=0 then |
| 312 | + //ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMark'); |
| 313 | + ThItem.Bitmap.LoadFromResourceName(Hinstance,'bvBookMark'); |
| 314 | + |
| 315 | + thItem1.add(ThItem); |
| 316 | + end; |
| 317 | + |
| 318 | + for i:=0 to 9 do begin |
| 319 | + ThItem:=tbvBookMark1MenuItem.Create(self); |
| 320 | + ThItem.Caption:=inttostr(i); |
| 321 | + thItem.ShortCut:=ShortCut(word(chararr[i]),[ssctrl]); |
| 322 | + ThItem.OnClick:=MenuGotoClick; |
| 323 | + if i=0 then |
| 324 | + //ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMarkGOTO'); |
| 325 | + ThItem.Bitmap.LoadFromResourceName(Hinstance,'bvBookMarkGOTO'); |
| 326 | + //ThisGrid.PopupMenu.items.Add(ThItem); |
| 327 | + thItem1.add(ThItem); |
| 328 | + end; |
| 329 | + //thItem1:=NewSubMenu( '',0,'SubMenu'+ThisGrid.Name,ARR); |
| 330 | + |
| 331 | + //thItem1:=TbvCommonBookMarkMenuItem.create(Self); |
| 332 | + |
| 333 | + //SetLength(Arr,0); |
| 334 | + |
| 335 | + {ThItem:=tbvBookMarkMenuItem.Create(self); |
| 336 | + ThItem.Caption:='Çàêëàäêà'; |
| 337 | + thItem.ShortCut:=ShortCut(word('1'),[ssAlt]); |
| 338 | + ThItem.OnClick:=MenuClick; |
| 339 | + ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMark'); |
| 340 | + ThisGrid.PopupMenu.items.Add(ThItem); //resource discarded |
| 341 | +
|
| 342 | + ThItem:=tbvBookMark1MenuItem.Create(self); |
| 343 | + ThItem.Caption:='Ïåðåéòè ê çàêëàäêå'; |
| 344 | + thItem.ShortCut:=ShortCut(word('1'),[ssctrl]); |
| 345 | + ThItem.OnClick:=MenuGotoClick; |
| 346 | + ThItem.Bitmap.Handle:=LoadBitmap(Hinstance,'bvBookMarkGOTO'); |
| 347 | + ThisGrid.PopupMenu.items.Add(ThItem); //resource discarded |
| 348 | + } |
| 349 | +// SetMenuItemBitmaps(FThisGrid.popupmenu.Handle,ThItem.Command,MF_BYCOMMAND,LoadBitmap(Hinstance,'bv_Finder'),LoadBitmap(Hinstance,'bv_Finder')); |
| 350 | + end; |
| 351 | + end; |
| 352 | + end; |
| 353 | +end; |
| 354 | + |
| 355 | +destructor tbvBookMark.destroy; |
| 356 | +var i:integer; |
| 357 | +begin |
| 358 | + for i:=low(FBookMark) to high(FBookMark) do |
| 359 | + if Assigned(FBookMark[i]) then freemem(FBookMark[i]); |
| 360 | + |
| 361 | + if Assigned(FThisGrid) |
| 362 | + and Assigned(Owner) |
| 363 | + and ( (Owner=FThisGrid) |
| 364 | + or |
| 365 | + (Owner is tForm) |
| 366 | + and (Owner.FindComponent(FThisGrid.Name)<>nil) |
| 367 | + ) |
| 368 | + and Assigned(FThisGrid.PopupMenu) |
| 369 | + then begin |
| 370 | + if FThisGrid.PopupMenu is TbvBookMarkPopupMenu |
| 371 | + then FThisGrid.PopupMenu.free |
| 372 | + else begin |
| 373 | + i:=0; |
| 374 | + while i<FThisGrid.PopupMenu.Items.Count do |
| 375 | + if ( |
| 376 | + (FThisGrid.PopupMenu.Items[i] is TbvCommonBookMarkMenuItem) |
| 377 | + ) |
| 378 | + and (FThisGrid.PopupMenu.Items[i].Owner=Self) |
| 379 | + then FThisGrid.PopupMenu.Items.Delete(i) |
| 380 | + else inc(i); |
| 381 | + end; |
| 382 | + end; |
| 383 | +// DoClose; |
| 384 | + inherited Destroy; |
| 385 | +end; |
| 386 | + |
| 387 | +procedure TbvBookMark.Notification(AComponent: TComponent; |
| 388 | + Operation: TOperation); |
| 389 | +begin |
| 390 | + inherited Notification(AComponent, Operation); |
| 391 | + |
| 392 | + if (Operation = opRemove) then |
| 393 | + begin |
| 394 | + if (AComponent=ThisGrid) then ThisGrid:=nil |
| 395 | + end; |
| 396 | +end; |
| 397 | + |
| 398 | +end. |
0 commit comments