Zoom the content of a TsWorksheetGrid [SOLVED] lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- totya · 2016-09-25 15:27 Hi! I'd like to zoom the  TsWorksheetGrid (like as excel), similar of this: http://delphi.xcjc.net/viewthread.php?tid=44368 This code working partially here, because cells width and height changed, but font size doesn't. Any idea? Tahnks! ---------------------------------------------------------------------- wp · 2016-09-25 16:11 The problem with this code is that the worksheet grid's main storage of column widths and row heights is in the worksheet; therefore, your grid-only changes are volatile and can be reverted on some unexpected occasions. A "true" zoom would only introduce a zoom factor in the worksheet, and the grid would calculate its column widths, row heights, font heights, image sizes etc from the values stored in the worksheet multiplied by this zoom factor. It will not be difficult but quite some work to get it running... ---------------------------------------------------------------------- totya · 2016-09-25 17:12 Hi! Thank you for the answer! ---------------------------------------------------------------------- wp · 2016-09-26 22:43 r5218 supports zooming of the worksheet grid. Just set a value to the new property ZoomFactor, unzoomed state has it at 1.0 (so, note that it's not a percentage scale!). Or run the demo in folder fpsctrls, here you can use the MouseWheel together with SHIFT+CTRL pressed (I had to use the combination because the grid ancestor already occupies the CTRL key for horizontal and the SHIFT key for vertical scrolling by the mousewheel). There may still be some bugs there and here: One of them is that the header cells do not zoom, I could not yet find the knob to turn. And the focused cell moves out of the viewport in case of large zoom factors. If you find other bugs please report them. ---------------------------------------------------------------------- totya · 2016-09-26 23:03 Hi! Very thanks for your job! But I revert to the older revision, because when I opened my "top secret" file, the first few lines are disappear (first few rows height are 0 I think). I didn't use zoom factor, only I recompiled my app with latest revision of fps. Luckily,  fpsgrid_no_install example works similar, the output is bad, as you can see. Thank you! ---------------------------------------------------------------------- wp · 2016-09-27 08:26 The "top-secret file" is the one you posted in http://forum.lazarus.fr eepascal.org/index.php/topic,34150.msg223047.html#msg223047? It is loaded correctly here. ---------------------------------------------------------------------- totya · 2016-09-27 08:28 Quote from: wp on September 27, 2016, 10:26:42 amThe "top-secret file" is the one you posted in http://forum.lazarus.freepascal.org/index.php /topic,34150.msg223047.html#msg223047? It is loaded correctly here. Hi! See the  fpsgrid_no_install example as I wrote in my last message in this topic. And I'm sorry, I'm wrong, I wrote my "top secret" file really, but this is the "real" top secret file, this is not the sample file. ---------------------------------------------------------------------- wp · 2016-09-27 13:28 Which version was the last one which was working? I'd like to understand what went wrong here. Anyway, the current r5221 should be correct. ---------------------------------------------------------------------- totya · 2016-09-27 15:01 Hi! Now these files displayed correctly, and this function works perfectly as I tested. Very thank you for this new function! ---------------------------------------------------------------------- totya · 2016-09-27 15:56 Hi, I found a bug in this new function, very similar of the SST problem. See sample file from here: http://forum.lazarus.freepascal.org/index.p hp/topic,34150.msg223047.html#msg223047 As you can see, the zoom only partial. You can test it with fpsctrls examples (shift-ctrl and mouse scroll), or use new ZoomFactor property. Thanks! ---------------------------------------------------------------------- wp · 2016-09-27 18:09 Ah. Missed this font size in the rich-text cells... Fixed in r5224 ---------------------------------------------------------------------- totya · 2016-09-27 18:35 Confirmed, zoom is working now with files what I tested. Thanks again for this new function! ---------------------------------------------------------------------- totya · 2016-09-27 22:17 Hi! Small remark, ZoomFactor will reset if I open file from Grid. I see this in the all load methods, for example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TsCustomWorksheetGrid.LoadFromSpreadsheetFile(AFileName: string;  AFormat: TsSpreadsheetFormat; AWorksheetIndex: Integer);begin  ZoomFactor := 1.0;  GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormat, AWorksheetIndex);end;  or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TsCustomWorksheetGrid.LoadFromWorkbook(AWorkbook: TsWorkbook;  AWorksheetIndex: Integer = -1);begin  ZoomFactor := 1.0;  GetWorkbookSource.LoadFromWorkbook(AWorkbook, AWorksheetIndex);  Invalidate;end;   But if I store the ZoomFactor settings, after file loaded, I need to set ZoomFactor (again). But this is ugly and slow, because Grid appear two times with different ZoomFactor... I suggest the KeepZoomFactorAfterLoad property... or ResetZoomFactorAfterLoad or similar solution... Thanks! ---------------------------------------------------------------------- wp · 2016-09-27 23:09 For BIFF8 and BIFF5 files, the zoom factor is now read from the file and written back to it. XLSX and ODS will follow soon. I also commented the reset of the zoomfactor in the grid, it does not seem to be needed - let's see... When writing your demo file (test_distorted_view, test_newfile) I get the warning that it contains 257 columns, but only 256 are supported; the file is written correctly, though. That's one of the things to be fixed... You said that they are created by Excel, didn't you? ---------------------------------------------------------------------- totya · 2016-09-27 23:58 Hi! Thanks for the answer, and your job,  but now I need go to sleep Yes these files created by the excel. But I play with it. If I copy only the 3 column from the test_distorted_view.xls to the new file, name is test_distorted_view_copy.xls, then GetLastColIndex(true) +1 say, this file contain only three column, this is okay. If I create new file with Excel 2003 with 3 column and 1 row, then GetLastColIndex(true) +1 say, this file contain only three column... See attached files. Elsewhere, I like it the new save load/zoom factor, but I want to use own zoom factor, which independent from the files, please don't ask me why. So, now I'd like to DoNotReadZoomFactorFromFile property really. ---------------------------------------------------------------------- wp · 2016-09-28 09:09 I added a zoom demo in which the zoom factor can be controlled independently of the file setting; there is no need for a dedicated grid property. ---------------------------------------------------------------------- totya · 2016-09-28 10:42 Hi! I haven't much time now (luchtime), so I be will quick now. Thanks for you, because you accepted my independent zoom reqest - small error in demo app: CbOverrideZoomFactor need [akLeft,akBottom] anchors... - I see this line "Grid.LoadSheetFromSpreadsheetFile(AFilename, 0);" this is mean doesn't need anymore the "UTF8ToAnsi" function like as other demo ? (My Lazarus is 1.6). - Seems to me this ZoomFactor override solution is slightly complicated than poroperty, but no problem. I hope I understand it the usage, for example (without exception handle) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i book := nil;book := TsWorkbook.Create; if Assigned(book) thenbeginbook.ReadFro mFile(AFilename);//book.GetFirstWorksheet.ZoomFactor := 0.2;book.GetWorksheetByIndex(0).ZoomFactor := 0.2;Grid.LoadFromWorkbook(book, 0);       end;  Thank you! ---------------------------------------------------------------------- wp · 2016-09-28 17:11 Maybe I should explain the zoomFactor in more detail: It is primarily a property of the worksheet, but here it is just a number, all user- defined row heights and column widths as well as font sizes are still stored in the worksheet assuming 100% zoom. The grid, then, multiplies the zoomfactor to all row heights, column widths and font sizes and thus achieves a zooming effect. Therefore, the basic idea of the "override zoom" option is to read the workbook independently of the grid (book.ReadFromFile()) and then set the requested zoom factor (the value entered to the grid's zoomfactor property is directly passed to the worksheet). Since the worksheet is not yet known to the grid at this moment these operations are not visible in the grid. Finally the worksheet is loaded from this workbook into the grid which draws the sheet for the first time. This sequence avoids unnecessary repaints at several zoom factors. As for UTF8ToAnsi and AnsiToUTF8: If you use Laz1.6/fpc 3.0 there is no need to call these functions any more (but please test - this topic is always good for surprises), and I even had cases when these functions caused trouble. Your example code is correct. But you should not skip the exception handling because reading of the file into the already-created workbook could fail, and you must cleanup this unusable instance. But it is important that you call workbook.Free only in case of such an error because the workbook is needed by the grid as its data source in the non-error case. ---------------------------------------------------------------------- totya · 2016-09-28 19:10 Hi! Thanks for the detailed informations! Quote from: wp on September 28, 2016, 07:11:47 pmAs for UTF8ToAnsi and AnsiToUTF8: If you use Laz1.6/fpc 3.0 there is no need to call these functions any more (but please test - this topic is always good for surprises), and I even had cases when these functions caused trouble. Agreed, "delete UTF8 from funciton names because Lazarus+FPC are full unicode now".... sure, sure... after my app doesn't start, because for example the GetAppConfigDir function doesn't support UTF8, even doesn't support nothing above ASCII. Luckily the GetAppConfigDirUTF8 function is working. But back to the topic... I test fps with this filename: Quoteöüóőúéáí_ÖÜÓŐÚÉÁÍ_仙は、一度死ぬことができます and it is working, the file is loaded Quote from: wp on September 28, 2016, 07:11:47 pmYour example code is correct. But you should not skip the exception handling because reading of the file into the already-created workbook could fail, and you must cleanup this unusable instance. But it is important that you call workbook.Free only in case of such an error because the workbook is needed by the grid as its data source in the non-error case. This is a small misunderstand, I never skip the recommended exception handle... only from the examples, as I wrote with my bad english, I'm sorry. Many thanks for the sample!