Recent

Author Topic: Gradient header for OS X applications  (Read 12757 times)

qdos

  • Full Member
  • ***
  • Posts: 112
Gradient header for OS X applications
« on: February 24, 2011, 03:37:05 am »
A small class that emulates the OS X "look" for Mac users of Lazarus.
Hope people like it. Im concidering building a whole package of these "tricks".

http://delphimax.wordpress.com/2011/02/24/mac-form-header-under-lazarus/

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Gradient header for OS X applications
« Reply #1 on: February 24, 2011, 05:15:17 am »
Very nice. I wasn't even aware that Mac-like toolbar capability was missing from Lazarus.

Points up a problem that we run into pretty quickly with any cross-platform framework on Mac: how to do the native stuff that users expect but that isn't supported in the [typically] Win95 look and feel framework.

With Cocoa and FPC 2.5.1's ObjectiveC modeswitch, you could just use the NSToolbar:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSToolbar_Class/Reference/Reference.html

But the Carbon widgetset isn't based on the ObjP Cocoa. However, it does have a limited set of bridge classes that can be used from Carbon. See how the tray icon is implemented (carbontrayicon.inc). This set does include NSToolbar (see NSToolbar.inc in pascocoa/appkit under lcl/interfaces/carbon). It seems like you could create an NSToolbar instead of a TCustomPanel and maybe the focus color change would be taken care of automatically.

Thanks.

-Phil
« Last Edit: February 24, 2011, 05:18:05 am by Phil »

qdos

  • Full Member
  • ***
  • Posts: 112
Re: Gradient header for OS X applications
« Reply #2 on: February 24, 2011, 08:47:24 am »
Hi Phil.

I have no idea how to write Objective Pascal (or C for that matter). In fact I learned C# just to avoid the objective C dialect (for iPhone & IPad).
One thing that could be interesting though, would be to perhaps mix & match between modes?
For instance, create the toolbar and other typical features in Objective Pascal, and wrap them in normal Object Pascal.
Could this be done?

I mean, if we were to do this properly - the toolbar should in reality be a part of the window-title (no seperation). But this would probably require a nib-file. Although creating it from C# is simple and can be done by code. But I prefer Object Pascal to C# any day of the week :)

I have no experience with Objective C. Just the sight of it makes me cringe.. :D
« Last Edit: February 24, 2011, 08:50:57 am by qdos »

qdos

  • Full Member
  • ***
  • Posts: 112
Re: Gradient header for OS X applications
« Reply #3 on: February 24, 2011, 09:05:00 am »
Just noticed:

The gradient routine should be deleted, and the call replaced with:

    canvas.GradientFill(FRect,FFromColor[FStyle],FToColor[FStyle],gdVertical);   

I didn't know that TCanvas in lazarus was different from Delphi  8-)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Gradient header for OS X applications
« Reply #4 on: February 24, 2011, 03:22:49 pm »
I have no idea how to write Objective Pascal (or C for that matter). In fact I learned C# just to avoid the objective C dialect (for iPhone & IPad).
One thing that could be interesting though, would be to perhaps mix & match between modes?
For instance, create the toolbar and other typical features in Objective Pascal, and wrap them in normal Object Pascal.
Could this be done?

You don't have to use Objective C/Pascal to use the Carbon widgetset's Cocoa subset, as I indicated.

I mean, if we were to do this properly - the toolbar should in reality be a part of the window-title (no seperation). But this would probably require a nib-file. Although creating it from C# is simple and can be done by code. But I prefer Object Pascal to C# any day of the week :)

I have no experience with Objective C. Just the sight of it makes me cringe.. :D

No ObjC or ObjP is needed - just use the normal Pascal classes in PasCocoa, which is part of Carbon widgetset. No .nib is necessary. In fact, you can do all Cocoa programmatically without a .nib if you want. Apple generally assumes .nib for the UI, but they also give examples in almost all cases of how to do it programmatically as well.

ObjC is easy to learn - only a few additions to standard C.

Here's some nice overview pages:

http://delphitococoa.wordpress.com/concept-comparison/

http://delphitococoa.wordpress.com/

Note that with the Objective Pascal dialect of FPC 2.5.1 you can work directly with Cocoa classes just like in ObjC. There are templates for using it in Xcode too as a replacement compiler.

Thanks.

-Phil

Hydexon

  • Full Member
  • ***
  • Posts: 170
Re: Gradient header for OS X applications
« Reply #5 on: February 26, 2011, 07:02:42 pm »
Hey, is possible to apply in another platforms and widgetsets?. because i'm examinated the code and don't find any Mac OS X Specific.
Lazarus 1.0.4 Stable - FPC 2.6.0 - gtk2 | Linux Mint 14 Nadia | GNOME Shell 3.6.2 - Awesome | Intel x86 | HP Mini 1020-LA Netbook (old)
Lazarus 2.0.2 Stable - FPC 3.0.2 - Windows | Lenovo IdeaPad Y

qdos

  • Full Member
  • ***
  • Posts: 112
Re: Gradient header for OS X applications
« Reply #6 on: February 26, 2011, 09:55:33 pm »
Hey, is possible to apply in another platforms and widgetsets?. because i'm examinated the code and don't find any Mac OS X Specific.

It is fairly universal. You could easily use this on windows and linux if you wish.
It was just a quick solution to make the default lazarus app look more "at home" on OS X.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Gradient header for OS X applications
« Reply #7 on: September 29, 2011, 12:14:26 am »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Fishous

  • Newbie
  • Posts: 6
Re: Gradient header for OS X applications
« Reply #8 on: October 15, 2011, 01:50:44 pm »
Google that URL and then choose to view the cache.

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: Gradient header for OS X applications
« Reply #9 on: October 17, 2011, 10:25:03 am »
Google that URL and then choose to view the cache.

Caching is good :D
Code: [Select]
Type

TOSXGradientStyle = (oxLight,oxDark);

TOSXPanel = Class(TCustomPanel)
Private
  FStyle: TOSXGradientStyle;
  FFromColor:   Array[TOSXGradientStyle] of TColor;
  FToColor:     Array[TOSXGradientStyle] of TColor;
  FLightColor:  Array[TOSXGradientStyle] of TColor;
  FDarkColor:   Array[TOSXGradientStyle] of TColor;
  Procedure SetStyle(Value:TOSXGradientStyle);
Private
  (* Hide properties that dont make sense for this control *)
  Property Caption;
  Property Color;
  property BevelInner;
  property BevelOuter;
  property BevelWidth;
  property BorderStyle;
  property ParentColor;
  property TabOrder;
  property TabStop;
  property ChildSizing;
Protected
  procedure EraseBackground(DC:HDC); override;
  Procedure Paint;Override;
Public
  Constructor Create(AOwner:TComponent);override;
Published
  Property GradientStyle:TOSXGradientStyle read FStyle write SetStyle;
  property Align;
  property Alignment;
  property Anchors;
  property AutoSize;
  property BorderSpacing;
  property BorderWidth;

  property ClientHeight;
  property ClientWidth;
  property Constraints;
  property DockSite;
  property DragCursor;
  property DragKind;
  property DragMode;
  property Enabled;
  property Font;
  property FullRepaint;

  property ParentFont;
  property ParentShowHint;
  property PopupMenu;
  property ShowHint;

  property UseDockManager default True;
  property Visible;
  property OnClick;
  property OnDockDrop;
  property OnDockOver;
  property OnDblClick;
  property OnDragDrop;
  property OnDragOver;
  property OnEndDock;
  property OnEndDrag;
  property OnEnter;
  property OnExit;
  property OnGetSiteInfo;
  property OnGetDockCaption;
  property OnMouseDown;
  property OnMouseMove;
  property OnMouseUp;
  property OnResize;
  property OnStartDock;
  property OnStartDrag;
  property OnUnDock;
End;

Constructor TOSXPanel.Create(AOwner:TComponent);
Begin
  inherited Create(AOwner);
  FFromColor[oxLight]:=rgb(237,237,237);
  FToColor[oxLight]:=rgb(218,218,218);
  FFromColor[oxDark]:=rgb(207,207,207);
  FToColor[oxDark]:=rgb(168,168,168);

  FLightColor[oxLight]:=clWhite;
  FLightColor[oxDark]:=rgb(201,201,201);

  FDarkColor[oxLight]:=RGB(153,153,153);
  FDarkColor[oxDark]:=RGB(81,81,81);

  FStyle:=oxLight;
  FullRepaint:=True;
end;

Procedure TOSXPanel.SetStyle(Value:TOSXGradientStyle);
Begin
  if Value<>FStyle then
  Begin
    FStyle:=Value;
    (* Make sure form is not being created, destroyed, loaded
       -And that a handle has been allocated before we issue a redraw.
       This is important, and the #1 cause of unexpected exceptions in
       programs. Always check these states when writing visual controls! *)
    if not (csDestroying in ComponentState)
    and not (csLoading in ComponentState)
    and not (csCreating in ControlState)
    and HandleAllocated then
    Invalidate;
  end;
end;   

procedure TOSXPanel.EraseBackground(DC:HDC);
Begin
  // Since our gradient fills the whole component, we remove the overhead
end;

Procedure TOSXPanel.Paint;
var
  mRect:  TRect;
Begin
  mRect:=GetClientRect;
  canvas.GradientFill(mRect,FFromColor[FStyle],FToColor[FStyle],gdVertical);

  Canvas.Pen.Color:=FLightColor[FStyle];
  Canvas.Line(mRect.left,mRect.top,mRect.right,mRect.Top);

  Canvas.Pen.color:=FDarkColor[FStyle];
  Canvas.Line(mRect.left,mRect.bottom-1,mRect.right,mRect.bottom-1);
end;

And you can also use TBGRAPannel (http://wiki.lazarus.freepascal.org/BGRAControls#Overview)

 

TinyPortal © 2005-2018