Jump to content

Remy Lebeau

Members
  • Content Count

    3000
  • Joined

  • Last visited

  • Days Won

    135

Everything posted by Remy Lebeau

  1. Remy Lebeau

    Making method with default encoding

    Being a class type, you really can't specify a specific TEncoding value as a default parameter value. So you have to either: Use an overload (that is what Delphi's RTL does): procedure Save(const AData: TStringList); overload; procedure Save(const AData: TStringList; const AEncoding: TEncoding); overload; ... procedure Save(const AData: TStringList); begin Save(AData, TEncoding.UTF8); end; procedure Save(const AData: TStringList; const AEncoding: TEncoding); begin AData.SaveToFile(FileName, AEncoding); end; Or else use nil for the default value: procedure Save(const AData: TStringList; AEncoding: TEncoding = nil); ... procedure Save(const AData: TStringList; AEncoding: TEncoding); begin if AEncoding = nil then AEncoding := TEncoding.UTF8; AData.SaveToFile(FileName, AEncoding); end;
  2. Remy Lebeau

    TListBox, TListView how to use Sort(Compare) ?

    What does that code have to do with TListBox/TListView? Their Sort() methods are quite different than TObjectList's Sort() method.
  3. Remy Lebeau

    TListBox, TListView how to use Sort(Compare) ?

    Can you be more specific? What exactly is not working for you? Please show the code you are having trouble with.
  4. Note the ETag was also different between the two responses, which means different versions of the resource were being accessed, which would explain why they had different timestamps.
  5. Remy Lebeau

    TIdSSLIOHandlerSocketOpenSSL and TLS 1.3 ?

    There is already work being done to add 1.1.x support. Not by me, codewise, but I'll review and merge it when its ready.
  6. Remy Lebeau

    Variant to generic T, how?

    And this is why I HATE Generics! They cause more headaches than they solve.
  7. Remy Lebeau

    Variant to generic T, how?

    @Jacek Laskowski I can't test this myself (no working IDE), but what about if the 'record' constraint is added to the Generic so the compiler knows that T is a value type? ToField<T: record> = class private fData : T; protected procedure SetFromVariant(const aValue : Variant); end; procedure ToField<T>.SetFromVariant(const aValue : Variant); begin fData := aValue; end;
  8. I already told you how. Clicking on a hyperlink inside the HTML is supposed to fire the OnShouldStartLoadWithRequest and OnDidStartLoad events. If it is not, then you should file a bug report. I don't understand what you are describing, but that doesn't seem to match with your screenshot. Are you running your code in the IDE's debugger? If so, the messages will go to the debugger's output window, not to DebugView.
  9. Clicking on a hyperlink starts a load request. You should be getting events for that. Lets starts with the obvious - did you actually ASSIGN a handler to that event? I'm confused. What are you trying to say exactly? Can you provide a ste-by-step example?
  10. Remy Lebeau

    Threading question

    If you are going to do that, it would be simpler to use SetString() instead: SetString(AMsg, PChar(stat), Length(stat)); Or Copy(): AMsg := Copy(stat, 1, MaxInt); Especially since both of these approaches handle empty strings, whereas accessing index 1 of an empty string is technically undefined behavior, even though Move() won't do actually anything when the Length is 0.
  11. Remy Lebeau

    Threading question

    The correct way to use UniqueString() in this example would be like this: procedure StatusOut(const stat: string); var AMsg: string; begin AMsg := stat; UniqueString(AMsg); // use AMsg as needed... end;
  12. Remy Lebeau

    Indy question readbytes VS readstring

    There are no real "advantages" per-se, they are just different methods meant for reading different kinds of data. Use whatever method is appropriate for whatever kind of data you are trying to read. If you are reading textual data, then ReadLn(), ReadString(s)(), Capture(), etc are all suitable, depending on the formatting of the text. If you are reading binary data, then ReadBytes() or ReadStream() are suitable.
  13. VCL's TWebBrowser has an OnBeforeNavigate2 event. FMX's TWebBrowser has OnShouldStartLoadWithRequest and OnDidStartLoad events.
  14. Remy Lebeau

    Variant to generic T, how?

    Can't you just assign the Variant to the field directly and let the Variant handle the conversion? Why do you need to go through TValue at all? procedure ToField<T>.SetFromVariant(const aValue : Variant); begin fData := aValue; end; Internally, when TValue is holding a Variant, TValue.AsType<T>() will look at the Variant's VType, copy the appropriate TVarData field to a temp TValue, and then assign that temp to the Result variable via TValue.Get<T>(). So just take out the middle man (TValue), Variant already knows how to do that same conversion directly.
  15. Remy Lebeau

    issues with non-Win platforms

    The Platform/Feature Manager does not exist when using the Offline (ISO-based) installer, only when using the Online (GetIt-based) installer. This is stated as much in the Installation documentation:
  16. Remy Lebeau

    Threading question

    Yes, because it CAN'T switch to PostMessage(). That would grant the caller permission to free the string data before it has actually been used to update the Memo. The Add() must be synchronous, regardless of its implementation, blocking the caller until the update is complete to ensure the integrity of the string data.
  17. Remy Lebeau

    Threading question

    You mean WM_COPYDATA, not WM_COPY.
  18. I would rewrite that to something more like this instead. Don't duplicate code that doesn't need to be duplicated: var LCount: Integer; LCount := o.{$IFDEF JSONOBJ_HAS_COUNT}Count{$ELSE}Size{$ENDIF}; if LCount <> 3 then raise Exception.CreateFmt(_('Parsing error on JSON answer: Root object size is %d not 3.'), [LCount]); I would just write a class helper or separate function, and IFDEF the code inside of it. Class helpers were first introduced in Delphi 2005, but were buggy and not officially supported until Delphi 2006. JSON was first introduced in Delphi 2010.
  19. Remy Lebeau

    JSON woes

    SuperObject is open-source. Just diff the two versions to see what is different between them. Or, just update the D2007 code to use the same version that the XE7 code is using. Or update both codes to the latest version. So pick ONE and stick with it for both codes. I don't do JSON processing in my code.
  20. Agreed. If IsSuspended were a manual-reset Event or ConditionalVariable, the main thread could sleep on it more efficiently. Or, on Windows 8+, you can use WaitOnAddress() to make the main thread sleep while it waits for the boolean to change value.
  21. Rather than polling, you might consider letting the OS notify you when the file is being used. There is no guarantee that other apps will actually keep the file open while they are working on the data. In fact, it is not unusual for an app to open a file, load the data, and then close the file, and then reopen the file later only if new data is being saved.
  22. To use a TFileStream just to check if the file is in use, you need to use fmOpenRead instead of fmCreate, and you definitely need to use fmShareExclusive instead of fmShareDenyNone, and note that the file could fail to open for any number of reasons, not all of which are related to the file being in use, so you have to check for that, eg: function FileReallyIsInUse(fName: string): boolean; begin Result := False; try TFileStream.Create(fName, fmOpenRead or fmShareExclusive).Free; except on E: EFOpenError do Result := GetLastError() in [ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION]; end; end; However, the exception doesn't carry the error code, and using GetLastError() after the exception is raised does not guarantee the original error code is preserved. You are better off simply calling the Win32 CreateFile() function directly instead (which avoids the need to allocate an object in memory, or invoke the overhead of exception handling): function FileReallyIsInUse(fName: string): boolean; var hFile: THandle; begin hFile := CreateFile(PChar(fName), GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0); if hFile <> INVALID_HANDLE_VALUE then begin CloseHandle(hFile); Result := False; end else Result := GetLastError() in [ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION]; end; Or, the RTL's FileOpen() function: function FileReallyIsInUse(fName: string): boolean; var hFile: THandle; begin hFile := FileOpen(fName, fmOpenRead or fmShareExclusive); if hFile <> INVALID_HANDLE_VALUE then begin FileClose(hFile); Result := False; end else Result := GetLastError() in [ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION]; end;
  23. Remy Lebeau

    Why is ShowMesssage blocking all visible forms?

    That is exactly what a TForm's PopupParent property is meant for. A TForm stays on top of its PopupParent Form, it can never go behind the PopupParent. Like I described earlier in this same discussion. Otherwise, just use a TFrame instead. Put it on top of the TForm that needs to be "blocked", and disable access to the TForm's controls until the TFrame is dismissed.
  24. Remy Lebeau

    Which Objects Allow ActiveControl?

    Only windowed controls (TWinControl descendants) can receive input focus, and thus be assigned as an ActiveControl. TLabel is a graphical control (TGraphicControl descendant), not a windowed control. If you need a windowed text label, use TStaticText instead of TLabel. TBitBtn is a windowed control. All windowed controls support that. All components, visual and non-visual, support that. The reason that doesn't work for you is most likely due to OS theming. So turn off theming on the button. Or, use a 3rd party button that allowed custom coloring. Or, just owner-draw the button yourself manually. I don't think any standard controls support vertical alignment, only custom-drawn controls can do that. All controls support that. For controls that are just wrappers for standard OS controls (like TButton), that behavior is controlled by the user's system settings, not by the framework. Most controls support that. Even if the event is not exposes, it still exists in all controls. Why are you posting this in a Delphi forum and not in the Lazarus forums? https://forum.lazarus.freepascal.org
  25. Remy Lebeau

    Which Objects Allow ActiveControl?

    Then why is this being posted in a Delphi forum and not in the Lazarus forums? https://forum.lazarus.freepascal.org
×