summaryrefslogtreecommitdiff
path: root/gui/stylemanager.pas
blob: 1c5a82e586a40f1d7f0a0189433ee6b16cabe1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
{
    fpGUI  -  Free Pascal GUI Library
    
    Style Manager as a Singleton
    
    Copyright (C) 2006 - 2007 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

unit StyleManager;

{$mode objfpc}{$H+}

interface

uses
  Classes
  ,Contnrs
  ,fpGUI
  ;
  
const
  cDefaultStyle = 'auto';

type

  // A class reference for the TStyle descendants
  TStyleClass = class of TStyleAbs;


  // A class to hold the TStyle class mappings. The factory maintains
  // a list of these and uses the StyleClass property to create the objects.
  TStyleClassMapping = class(TObject)
  private
    FsMappingName: string;
    FStyleClass: TStyleClass;
  public
    constructor Create(const AMappingName: string; AStyleClass: TStyleClass); overload;
    property    MappingName: string read FsMappingName;
    property    StyleClass: TStyleClass read FStyleClass;
  end;


  // Style manager and factory class

  { TStyleManager }

  TStyleManager = class(TObject)
  private
    FList : TObjectList;
    FDefaultStyle: TStyleAbs;
    FUserStyle: TStyleAbs;
    FDefaultStyleType: string;
    function    GetDefaultStyle: TStyleAbs;
  public
    constructor Create;
    destructor  Destroy; override;
    property    DefaultStyle: TStyleAbs read GetDefaultStyle;
    procedure   SetStyle(const AStyleName: string);
    procedure   RegisterClass(const AStyleName: string; AStyleClass : TStyleClass);
    function    CreateInstance(const AStyleName: string): TStyleAbs; overload;
    function    CreateInstance: TStyleAbs; overload;
    procedure   AssignStyleTypes(AStrings: TStrings);
  end;


// Singleton
function gStyleManager: TStyleManager;


implementation
uses
  SysUtils
  ,fpGFX
  ,WindowsStyle
  ,OpenSoftStyle
  ,MotifStyle
  ;

var
  uStyleManager: TStyleManager;


{ Creation is deferred to the first request }
function gStyleManager: TStyleManager;
begin
  if uStyleManager = nil then
    uStyleManager := TStyleManager.Create;
  result := uStyleManager;
end;


{ TStyleManager }

function TStyleManager.GetDefaultStyle: TStyleAbs;
begin
  if not Assigned(FDefaultStyle) then
//    FDefaultStyle.Free;
  FDefaultStyle := CreateInstance(FDefaultStyleType);
  Result := FDefaultStyle;
end;

constructor TStyleManager.Create;
begin
  inherited Create;
  FList := TObjectList.Create;
  FUserStyle        := nil;
  FDefaultStyle     := nil;
  FDefaultStyleType := cDefaultStyle;    // will change later
end;

destructor TStyleManager.Destroy;
begin
  FDefaultStyle.Free;
  FList.Free;
  inherited Destroy;
end;

procedure TStyleManager.SetStyle(const AStyleName: string);
var
  i: integer;
begin
  for i := 0 to FList.Count - 1 do
    if UpperCase(TStyleClassMapping(FList.Items[i]).MappingName) =
         UpperCase(AStyleName) then
    begin
      FDefaultStyleType := AStyleName;
      Break; //==>
    end;

  Assert(FDefaultStyleType <> AStyleName,
          Format('<%s> does not identify a registered style class.',
                   [AStyleName]));
end;

// Register a TStyle class for creation by the factory
procedure TStyleManager.RegisterClass(const AStyleName: string;
  AStyleClass: TStyleClass);
var
  i: integer;
begin
  for i := 0 to FList.Count - 1 do
    if UpperCase(TStyleClassMapping(FList.Items[i]).MappingName) =
         UpperCase(AStyleName) then
      Assert(false,
              Format('Style class <%s> already registered.',
                      [AStyleName]));
  FList.Add(TStyleClassMapping.Create(AStyleName, AStyleClass));
  
//  writeln('Registering style: ' + AStyleName);
  // we will use this later
//  FDefaultStyleType := UpperCase(AStyleName);
end;

// Call the factory to create an instance of TStyle
function TStyleManager.CreateInstance(const AStyleName: string): TStyleAbs;
var
  i: integer;
begin
  result := nil;
  for i := 0 to FList.Count - 1 do
    if UpperCase(TStyleClassMapping(FList.Items[i]).MappingName) =
         UpperCase(AStyleName) then
    begin
      result := TStyleClassMapping(FList.Items[i]).StyleClass.Create;
      Break; //==>
    end;

  Assert(result <> nil,
          Format('<%s> does not identify a registered style class.',
                   [AStyleName]));
end;

function TStyleManager.CreateInstance: TStyleAbs;
begin
  result := CreateInstance(FDefaultStyleType);
end;

// Assing the registered list of TStyle names to a StringList
// This can be used to populate a combobox with the available TStyle
// class types.
procedure TStyleManager.AssignStyleTypes(AStrings: TStrings);
var
  i: integer;
begin
  AStrings.Clear;
  for i := 0 to FList.Count - 1 do
    AStrings.Add(TStyleClassMapping(FList.Items[i]).MappingName);
end;

{ TStyleClassMapping }

constructor TStyleClassMapping.Create(const AMappingName: string;
  AStyleClass: TStyleClass);
begin
  inherited Create;
  FsMappingName := AMappingName;
  FStyleClass   := AStyleClass;
end;


initialization
  gStyleManager.RegisterClass(cDefaultStyle, TWin2000Style);
  gStyleManager.RegisterClass('Windows 9x', TWin9xStyle);
  gStyleManager.RegisterClass('Windows 2000', TWin2000Style);
  gStyleManager.RegisterClass('OpenSoft', TOpenSoftStyle);
  gStyleManager.RegisterClass('Motif', TMotifStyle);

finalization
  uStyleManager.Free;

end.