Recent

Author Topic: How to call a function?  (Read 8387 times)

A

  • Guest
How to call a function?
« on: January 07, 2018, 12:53:24 pm »
Hello,

As some of you may know, I have been trying to set default button icons to my buttons.

I fear that my original question may have been burried or something.

I found a certain function that I believe may help me with the problem that I am currently facing:

Code: Pascal  [Select][+][-]
  1. function GetDefaultButtonIcon(idButton: Integer): TCustomBitmap;
  2. begin
  3.   Result := nil;
  4.   if (idButton < Low(BitBtnResNames)) or (idButton > High(BitBtnResNames)) then
  5.     Exit;
  6.   if BitBtnResNames[idButton] = '' then
  7.     Exit;
  8.   Result := TPortableNetworkGraphic.Create;
  9.   Result.LoadFromResourceName(hInstance, BitBtnResNames[idButton]);
  10. end;

This was found in the source code for the button component.

If I have a button on my form captioned "Copy", how can I call the above function to assign the default button bitmap on Form Create?

Thanks for your help.

Thaddy

  • Hero Member
  • *****
  • Posts: 14362
  • Sensorship about opinions does not belong here.
Re: How to call a function?
« Reply #1 on: January 07, 2018, 01:09:46 pm »
I don't know if that is really there but that code is really fishy.
Code: Pascal  [Select][+][-]
  1. function GetDefaultButtonIcon(idButton: Integer): TCustomBitmap;
  2. begin
  3.   // should be simply:
  4.   if (idButton < Low(BitBtnResNames)) or (idButton > High(BitBtnResNames)) or ( BitBtnResNames[idButton] = '') then Exit(nil);
  5.   // and this leaks...
  6.   Result := TPortableNetworkGraphic.Create;
  7.   // and this should be protected, because if the resource does not exist (ok, we tested for that above ;) ) or is corrupted you have a crash:
  8.   Result.LoadFromResourceName(hInstance, BitBtnResNames[idButton]);
  9.  
« Last Edit: January 07, 2018, 01:16:23 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

A

  • Guest
Re: How to call a function?
« Reply #2 on: January 07, 2018, 01:16:16 pm »
Thank you for your reply.

What do you mean by "fishy"; I don't quite understand your comments.
It is there, in the source code.
You can see it by opening a new form, dragging a button to the form, going to [View] -> [Unit Dependencies] and then selecting "Button". You can then see the entire source code of the button class that ships with all Lazarus IDE's.

Do you think this is the function that I need, or am I barking up the wrong tree?
« Last Edit: January 07, 2018, 01:21:20 pm by A »

Thaddy

  • Hero Member
  • *****
  • Posts: 14362
  • Sensorship about opinions does not belong here.
Re: How to call a function?
« Reply #3 on: January 07, 2018, 01:17:35 pm »
What do you mean by "fishy"; I don't quite understand your comments.
It is there, in the source code.
It should not be there in the sourcecode. I will investigate.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

A

  • Guest
Re: How to call a function?
« Reply #4 on: January 07, 2018, 01:20:26 pm »
I wonder why it shouldn't.

To me, it's evident that other controls rely on this function. An example would be the "TButtonPanel" control which automatically places the default system icon for the Cancel, OK, and Help buttons.
You likely know more than I do though. :P

A

Thaddy

  • Hero Member
  • *****
  • Posts: 14362
  • Sensorship about opinions does not belong here.
Re: How to call a function?
« Reply #5 on: January 07, 2018, 01:47:26 pm »
Well, it should have been something like this:
Code: Pascal  [Select][+][-]
  1. function GetDefaultButtonIcon(idButton: Integer;var bm:TCustomBitmap):Boolean;
  2. begin
  3.   // should be simply:
  4.   if (idButton < Low(BitBtnResNames)) or (idButton > High(BitBtnResNames)) or ( BitBtnResNames[idButton] = '') then Exit(false);
  5.   try
  6.    // but I would refactor this as a boolean function too: if bm.LoadFromResourceName(hInstance, BitBtnResNames[idButton]) then...
  7.     bm.LoadFromResourceName(hInstance, BitBtnResNames[idButton]);
  8.     Result := true;
  9.   except
  10.     Result := false
  11.   end;
  12. end;
  13. var
  14.   DefBitmap: TCustomBitmap;
  15. begin
  16.   DefBitmap :=TPortableNetworkGraphic.Create;
  17.  if GetDefaultButtonIcon(idClose, DefBitmap) then
  18.  try  ....
  19.  finally
  20.    Defbitmap.free;
  21.  end;
or something along those lines.

Note current code does not leak perse, but my issue is architectural and a lot safer. If it is not like that it needs to be looked into.
« Last Edit: January 07, 2018, 01:51:23 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to call a function?
« Reply #6 on: January 07, 2018, 01:51:06 pm »
Looking at the code you posted it is obvious to me that the function is internal to lazarus only. Farther more, there is an array named BitBtnResNames declared somewhere that hold the resource name of the icons that are placed in the application by the compiler. I bet that if you look at the array declaration you will be able to correlate index(or buttonID) with the icon.
Keep in mind that it will not bring the default OS icons though.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14362
  • Sensorship about opinions does not belong here.
Re: How to call a function?
« Reply #7 on: January 07, 2018, 01:52:41 pm »
@Taazz It's architecture... Simple.. Internal or not. OP seems to think it is exposed...
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

A

  • Guest
Re: How to call a function?
« Reply #8 on: January 07, 2018, 01:55:02 pm »
And, just to be clear, I can use this to assign native button icons to my buttons? As seen in most applications:

http://www.murga-linux.com/puppy/viewtopic.php?mode=attach&id=87914&sid=9f2cbd42c2cd004542ce5eec3b7dda9b

The above icon set if GNOME or GTK is the theme/DE.

If you believe that this is the function that I'm after, how do I call it at (I presume) From Create to assign a given icon to my button?

Cheers.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9857
  • Debugger - SynEdit - and more
    • wiki
Re: How to call a function?
« Reply #9 on: January 07, 2018, 01:55:19 pm »
Well, whether the function should have more error checking or not..., that is not really relevant to the question.

You call the function like any other function.
1) add the unit to your uses clause (if not already there)
2) call it from your code (to be save prefix with unit name)
Code: Pascal  [Select][+][-]
  1. someBitMapVar := Buttons.GetDefaultButtonIcon(someId);

The more important questions would be:
A) What ID do you want? That you have to know yourself
B) What to do with the result.  (How to get it displayed on your Button, or where ever you need it)
You can do whatever you want to it. You may (depending on what you do) have to make sure that you free the bitmap, when it is no longer used. But maybe if you pass the bitmap to some other function, then that will take care. You have to investigate that.

Otherwise, I don't know what your question is.

If it is A or B, then I you may want to clarify this, and maybe someone can answer that.

Looking at the code you posted it is obvious to me that the function is internal to lazarus only.
It is in the interface part of Buttons. So I would assume it can be used.

Yet true, it has a comment: "Helper....", so it is possible that it was intended more private, and may become unavailable in the future...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to call a function?
« Reply #10 on: January 07, 2018, 01:56:20 pm »
And, just to be clear, I can use this to assign native button icons to my buttons? As seen in most applications:

http://www.murga-linux.com/puppy/viewtopic.php?mode=attach&id=87914&sid=9f2cbd42c2cd004542ce5eec3b7dda9b

The above icon set if GNOME or GTK is the theme/DE.

If you believe that this is the function that I'm after, how do I call it at (I presume) From Create to assign a given icon to my button?

Cheers.
No. The icons loaded from that function are resources compiled in the exe not system icons.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

A

  • Guest
Re: How to call a function?
« Reply #11 on: January 07, 2018, 01:59:17 pm »
And, just to be clear, I can use this to assign native button icons to my buttons? As seen in most applications:

http://www.murga-linux.com/puppy/viewtopic.php?mode=attach&id=87914&sid=9f2cbd42c2cd004542ce5eec3b7dda9b

The above icon set if GNOME or GTK is the theme/DE.

If you believe that this is the function that I'm after, how do I call it at (I presume) From Create to assign a given icon to my button?

Cheers.
No. The icons loaded from that function are resources compiled in the exe not system icons.

Oh, then that's probably not what I'm looking for then.
Any other person would just assign some random bitmaps to the buttons. It sucks being a perfectionist, especially when you're trying to perfect something that you have no clue about, and especially when you have only just started leaning how to build applications.

A

  • Guest
Re: How to call a function?
« Reply #12 on: January 07, 2018, 02:04:47 pm »
Well, whether the function should have more error checking or not..., that is not really relevant to the question.

You call the function like any other function.
1) add the unit to your uses clause (if not already there)
2) call it from your code (to be save prefix with unit name)
Code: Pascal  [Select][+][-]
  1. someBitMapVar := Buttons.GetDefaultButtonIcon(someId);

The more important questions would be:
A) What ID do you want? That you have to know yourself
B) What to do with the result.  (How to get it displayed on your Button, or where ever you need it)
You can do whatever you want to it. You may (depending on what you do) have to make sure that you free the bitmap, when it is no longer used. But maybe if you pass the bitmap to some other function, then that will take care. You have to investigate that.

Otherwise, I don't know what your question is.

If it is A or B, then I you may want to clarify this, and maybe someone can answer that.

Looking at the code you posted it is obvious to me that the function is internal to lazarus only.
It is in the interface part of Buttons. So I would assume it can be used.

Yet true, it has a comment: "Helper....", so it is possible that it was intended more private, and may become unavailable in the future...

Thanks for your reply, I didn't think about actually placing the result on the button neatly, programmatically.
Unfortunately, it would appear now that this is not the function that I need to get the native default button icons - according to Thaddy. Somewhere in Lazarus' source code, there's some function that will handle this - so many of Lazurus' components have icons that change depending on the environment. For example, the DirEdit - the "open file directory" icon that appears to the right of the text field acts as I need my buttons to act, i.e. it changes depending on OS/DE.

A

  • Guest
Re: How to call a function?
« Reply #13 on: January 07, 2018, 03:01:22 pm »
Is there any way to design the application that I'm trying to create in, for example, QT designer, and then program it in Object Pascal?

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: How to call a function?
« Reply #14 on: January 07, 2018, 03:33:43 pm »
Is there something wrong with the "TbitBtn" button?

it's on the "Additional" tab.. and allows you to assign an image to it.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018