1{%MainUnit ../comctrls.pp}
2
3{******************************************************************************
4                                  TCustomTrackBar
5 ******************************************************************************
6
7 *****************************************************************************
8  This file is part of the Lazarus Component Library (LCL)
9
10  See the file COPYING.modifiedLGPL.txt, included in this distribution,
11  for details about the license.
12 *****************************************************************************
13
14  current design flaws:
15
16  - I decided to support some gtk-specific properties in this class. This
17    won't break Delphi compatibility but for 100% Delphi compatibility
18    a better approach would be to derive another class.
19    BTW: When porting to another widget library you can safely ignore
20
21           FScalePosition, FScaleDigits
22
23  Delphi compatibility:
24
25   - the interface is almost like in delphi 4
26   - some Delphi properties are not supported by GTK and are currently not
27     implemented here, These are:
28       frequency, tickstyle and tickmark
29   - what about these private procs
30      procedure CNHScroll(var Message: TWMHScroll); message CN_HSCROLL;
31      procedure CNVScroll(var Message: TWMVScroll); message CN_VSCROLL;
32   - there is a new property which I've implemented because it is a
33     nice addon for the GTK interface
34      * ScalePos (left, top, right, bottom)
35
36  TODO:
37
38    - implement some more Delphi stuff
39    - range checking for min/max could raise an exception
40    - use RecreateWnd when the orientation changes!
41
42}
43{------------------------------------------------------------------------------
44  Method: TCustomTrackBar.Create
45  Params:  AOwner: the owner of the class
46  Returns: Nothing
47
48  Constructor for the trackbar.
49 ------------------------------------------------------------------------------}
50constructor TCustomTrackBar.Create (AOwner : TComponent);
51begin
52  inherited Create (aOwner);
53  fCompStyle := csTrackbar;
54  ControlStyle := ControlStyle - [csCaptureMouse];
55  FLineSize := 1;
56  FMax := 10;
57  FMin := 0;
58  FPosition := 0;
59  FPageSize := 2;
60  FFrequency := 1;
61  FOrientation := trHorizontal;
62  FScalePos := trTop;
63  FScaleDigits := 0;
64  FTickMarks := tmBottomRight;
65  FTickStyle := tsAuto;
66  FSelStart := 0;
67  FSelEnd := 0;
68  FShowSelRange := True;
69  FReversed := False;
70  TabStop := True;
71  with GetControlClassDefaultSize do
72    SetInitialBounds(0, 0, CX, CY);
73end;
74
75{------------------------------------------------------------------------------
76  Method: TCustomTrackBar.InitializeWnd
77  Params: none
78  Returns: Nothing
79
80  Set all properties after visual component has been created. Will be called
81  from TWinControl.
82 ------------------------------------------------------------------------------}
83procedure TCustomTrackBar.InitializeWnd;
84begin
85  inherited InitializeWnd;
86  ApplyChanges;
87end;
88
89procedure TCustomTrackBar.Loaded;
90begin
91  inherited Loaded;
92  ApplyChanges;
93end;
94
95procedure TCustomTrackBar.ShouldAutoAdjust(var AWidth, AHeight: Boolean);
96begin
97  if Orientation = trHorizontal then
98    begin
99      AWidth := True;
100      AHeight := not AutoSize;
101    end
102  else
103    begin
104      AWidth := not AutoSize;
105      AHeight := True;
106    end
107end;
108
109{------------------------------------------------------------------------------
110  Method: TCustomTrackBar.SetTick
111  Params: Value : new tick
112  Returns: Nothing
113
114 ------------------------------------------------------------------------------}
115procedure TCustomTrackBar.SetTick(Value: Integer);
116begin
117  if HandleAllocated then
118    TWSTrackBarClass(WidgetSetClass).SetTick(Self, Value);
119end;
120
121{------------------------------------------------------------------------------
122  Method: TCustomTrackBar.SetOrientation
123  Params: Value : new orientation
124  Returns: Nothing
125
126  Change the orientation of the trackbar.
127------------------------------------------------------------------------------}
128procedure TCustomTrackBar.SetOrientation(Value: TTrackBarOrientation);
129var
130  OldWidth: LongInt;
131  OldHeight: LongInt;
132begin
133  if FOrientation <> Value then
134  begin
135    FOrientation := Value;
136    // switch width and height, but not when loading, because we assume that the
137    // lfm contains a consistent combination of Orientation and (width, height)
138    if not (csLoading in ComponentState) then
139    begin
140      OldWidth:=Width;
141      OldHeight:=Height;
142      Constraints.UpdateInterfaceConstraints;
143      SetBounds(Left,Top,OldHeight,OldWidth);
144      if HandleAllocated then
145        TWSTrackBarClass(WidgetSetClass).SetOrientation(Self, FOrientation);
146    end;
147  end;
148end;
149
150{------------------------------------------------------------------------------
151  Method: TCustomTrackBar.SetParams
152  Params:  APosition : new position
153           AMin      : new minimum
154           AMax      : new maximum
155  Returns: Nothing
156
157  Set new parameters for the trackbar.
158 ------------------------------------------------------------------------------}
159procedure TCustomTrackBar.SetParams(APosition, AMin, AMax: Integer);
160begin
161  if not (csLoading in ComponentState) then
162    FixParams(APosition, AMin, AMax);
163
164  if (FPosition = APosition) and (FMin = AMin) and (FMax = AMax) then
165    Exit;
166
167  FPosition := APosition;
168  FMax := AMax;
169  FMin := AMin;
170  ApplyChanges;
171end;
172
173{------------------------------------------------------------------------------
174  Method: TCustomTrackBar.SetPosition
175  Params: Value : new position
176  Returns: Nothing
177
178  Set actual position of the trackbar.
179 ------------------------------------------------------------------------------}
180procedure TCustomTrackBar.SetPosition(Value: Integer);
181begin
182  FixParams(Value, FMin, FMax);
183  if FPosition <> Value then
184  begin
185    FPosition := Value;
186    if HandleAllocated then
187      TWSTrackBarClass(WidgetSetClass).SetPosition(Self, FPosition);
188    if not (csLoading in ComponentState) then
189      Changed;
190  end;
191end;
192
193procedure TCustomTrackBar.SetReversed(const AValue: Boolean);
194begin
195  if FReversed <> AValue then
196  begin
197    FReversed := AValue;
198    ApplyChanges;
199  end;
200end;
201
202{------------------------------------------------------------------------------
203  Method: TCustomTrackBar.SetMin
204  Params: Value : new minimum
205  Returns: Nothing
206
207  Set minimum value of the trackbar.
208 ------------------------------------------------------------------------------}
209procedure TCustomTrackBar.SetMin(Value: Integer);
210begin
211  if FMin <> Value then
212    SetParams(FPosition, Value, FMax);
213end;
214
215{------------------------------------------------------------------------------
216  Method: TCustomTrackBar.SetMax
217  Params: Value : new maximum
218  Returns: Nothing
219
220  Set maximum value of the trackbar.
221 ------------------------------------------------------------------------------}
222procedure TCustomTrackBar.SetMax(Value: Integer);
223begin
224  if FMax <> Value then
225    SetParams(FPosition, FMin, Value);
226end;
227
228{------------------------------------------------------------------------------
229  Method: TCustomTrackBar.SetFrequency
230  Params: Value : new frequency
231  Returns: Nothing
232
233 ------------------------------------------------------------------------------}
234procedure TCustomTrackBar.SetFrequency(Value: Integer);
235begin
236  if FFrequency <> Value then
237  begin
238    FFrequency := Value;
239    ApplyChanges;
240  end;
241end;
242
243{------------------------------------------------------------------------------
244  Method: TCustomTrackBar.SetTickStyle
245  Params: Value : new tickstyle
246  Returns: Nothing
247
248 ------------------------------------------------------------------------------}
249procedure TCustomTrackBar.SetTickStyle(Value: TTickStyle);
250begin
251  if FTickStyle <> Value then
252  begin
253    FTickStyle := Value;
254    ApplyChanges;
255    Constraints.UpdateInterfaceConstraints;
256    DoAutoSize;
257    if HandleAllocated then
258      TWSTrackBarClass(WidgetSetClass).SetTickStyle(Self, FTickStyle);
259  end;
260end;
261
262{------------------------------------------------------------------------------
263  Method: TCustomTrackBar.SetTickMarks
264  Params: Value : new tickmarks
265  Returns: Nothing
266
267 ------------------------------------------------------------------------------}
268procedure TCustomTrackBar.SetTickMarks(Value: TTickMark);
269begin
270  if FTickMarks <> Value then
271  begin
272    FTickMarks := Value;
273    ApplyChanges;
274  end;
275end;
276
277{------------------------------------------------------------------------------
278  Method: TCustomTrackBar.SetLineSize
279  Params: Value : new linesize
280  Returns: Nothing
281
282  Set the increment which is used when one of the arrow-keys is pressed.
283 ------------------------------------------------------------------------------}
284procedure TCustomTrackBar.SetLineSize(Value: Integer);
285begin
286  if FLineSize <> Value then
287  begin
288    FLineSize := Value;
289    ApplyChanges;
290  end
291end;
292
293{------------------------------------------------------------------------------
294  Method: TCustomTrackBar.SetPageSize
295  Params:  Value : new pagesize
296  Returns: Nothing
297
298  Set the increment which is used when one of the arrow-keys is pressed together
299  with a modifier or when PgUp/PgDwn are pressed.
300 ------------------------------------------------------------------------------}
301procedure TCustomTrackBar.SetPageSize(Value: Integer);
302begin
303  if FPageSize <> Value then
304  begin
305    FPageSize := Value;
306    ApplyChanges;
307  end;
308end;
309
310{------------------------------------------------------------------------------
311  Method: TCustomTrackBar.UpdateSelection
312  Params:
313  Returns: Nothing
314
315 ------------------------------------------------------------------------------}
316procedure TCustomTrackBar.UpdateSelection;
317begin
318end;
319
320class procedure TCustomTrackBar.WSRegisterClass;
321begin
322  inherited WSRegisterClass;
323  RegisterCustomTrackBar;
324end;
325
326{------------------------------------------------------------------------------
327  Method: TCustomTrackBar.ApplyChanges
328  Params: none
329  Returns: Nothing
330
331  Sends message to update the visual apperance of the object.
332 ------------------------------------------------------------------------------}
333procedure TCustomTrackBar.ApplyChanges;
334begin
335  if HandleAllocated and (not (csLoading in ComponentState)) then
336    TWSTrackBarClass(WidgetSetClass).ApplyChanges(Self);
337end;
338
339procedure TCustomTrackBar.Changed;
340begin
341  if Assigned (FOnChange) then
342    FOnChange(Self);
343end;
344
345{------------------------------------------------------------------------------
346  Method: TCustomTrackBar.DoChange
347  Params:  Msg  (longint = LM_CHANGE)
348  Returns: Nothing
349
350  Update position and call user's callback for Change event.
351 ------------------------------------------------------------------------------}
352procedure TCustomTrackBar.DoChange(var msg);
353begin
354  FPosition := TWSTrackBarClass(WidgetSetClass).GetPosition(Self);
355  Assert(True, 'Trace:Trackbar received a message -CHANGE');
356  if [csLoading,csDestroying,csDesigning]*ComponentState<>[] then
357    Exit;
358  EditingDone;
359  Changed;
360end;
361
362procedure TCustomTrackBar.FixParams(var APosition, AMin, AMax: Integer);
363begin
364  if AMin > AMax then AMin := AMax;
365  if APosition < AMin then APosition := AMin;
366  if APosition > AMax then APosition := AMax;
367end;
368
369class function TCustomTrackBar.GetControlClassDefaultSize: TSize;
370begin
371  Result.CX := 100;
372  Result.CY := 25;
373end;
374
375{------------------------------------------------------------------------------
376  Method: TCustomTrackBar.SetScalePos
377  Params:  value : position of the scaling text
378  Returns: Nothing
379
380 ------------------------------------------------------------------------------}
381procedure TCustomTrackBar.SetScalePos(Value: TTrackBarScalePos);
382begin
383  if FScalePos <> Value then
384  begin
385    FScalePos := Value;
386    ApplyChanges;
387  end;
388end;
389
390procedure TCustomTrackBar.SetSelEnd(const AValue: Integer);
391begin
392  if FSelEnd <> AValue then
393  begin
394    FSelEnd := AValue;
395    ApplyChanges;
396  end;
397end;
398
399procedure TCustomTrackBar.SetSelStart(const AValue: Integer);
400begin
401  if FSelStart <> AValue then
402  begin
403    FSelStart := AValue;
404    ApplyChanges;
405  end;
406end;
407
408procedure TCustomTrackBar.SetShowSelRange(const AValue: Boolean);
409begin
410  if FShowSelRange <> AValue then
411  begin
412    FShowSelRange := AValue;
413    ApplyChanges;
414  end;
415end;
416
417