1 // Copyright (C) 2008  Davis E. King (davis@dlib.net), and Nils Labugt
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #undef DLIB_WIDGETs_STYLE_ABSTRACT_
4 #ifdef DLIB_WIDGETs_STYLE_ABSTRACT_
5 
6 #include "../algs.h"
7 #include "../gui_core.h"
8 #include "widgets_abstract.h"
9 #include "../unicode/unicode_abstract.h"
10 
11 namespace dlib
12 {
13 
14 // ----------------------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------------------
16     // button styles
17 // ----------------------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------------------
19 
20     class button_style
21     {
22         /*!
23             WHAT THIS OBJECT REPRESENTS
24                 This is an abstract class that defines the interface a
25                 button style object must implement.
26 
27                 Note that derived classes must be copyable via
28                 their copy constructors.
29         !*/
30 
31     public:
32 
~button_style()33         virtual ~button_style() {}
34 
redraw_on_mouse_over()35         virtual bool redraw_on_mouse_over (
36         ) const { return false; }
37         /*!
38             ensures
39                 - if (this style draws buttons differently when a mouse is over them) then
40                     - returns true
41                 - else
42                     - returns false
43         !*/
44 
get_invalidation_rect(const rectangle & rect)45         virtual rectangle get_invalidation_rect (
46             const rectangle& rect
47         ) const { return rect; }
48         /*!
49             requires
50                 - the mutex drawable::m is locked
51                 - rect == the get_rect() that defines where the button is
52             ensures
53                 - returns a rectangle that should be invalidated whenever a button
54                   needs to redraw itself.  (e.g. If you wanted your button style to
55                   draw outside the button then you could return a larger rectangle)
56         !*/
57 
58         virtual rectangle get_min_size (
59             const ustring& name,
60             const font& mfont
61         ) const = 0;
62         /*!
63             requires
64                 - the mutex drawable::m is locked
65             ensures
66                 - returns a rectangle that represents the minimum size of the button
67                   given the name and font.
68         !*/
69 
70         virtual void draw_button (
71             const canvas& c,
72             const rectangle& rect,
73             const bool enabled,
74             const font& mfont,
75             const long lastx,
76             const long lasty,
77             const ustring& name,
78             const bool is_depressed
79         ) const = 0;
80         /*!
81             requires
82                 - the mutex drawable::m is locked
83                 - c == the canvas to draw on
84                 - rect, enabled, mfont, lastx, and lasty are the variables
85                   defined in the protected section of the drawable class.
86                 - name == the name of the button to be drawn
87                 - is_depressed == true if the button is to be drawn in a depressed state
88             ensures
89                 - draws the button on the canvas c at the location given by rect.
90         !*/
91     };
92 
93 // ----------------------------------------------------------------------------------------
94 
95     class button_style_default : public button_style
96     {
97         /*!
98             This is the default style for button objects.  It will cause
99             a button to appear as the simple MS Windows 2000 button style.
100         !*/
101     };
102 
103 // ----------------------------------------------------------------------------------------
104 
105     class button_style_toolbar1 : public button_style
106     {
107         /*!
108             This draws a simple toolbar style button that displays its name in the
109             middle of itself.  When the mouse moves over it it will light up.
110         !*/
111     };
112 
113 // ----------------------------------------------------------------------------------------
114 
115     class button_style_toolbar_icon1 : public button_style
116     {
117         /*!
118             This draws a simple toolbar style button that displays an image in the
119             middle of itself.  When the mouse moves over it it will light up.
120         !*/
121         template <typename image_type>
122         button_style_toolbar_icon1 (
123             const image_type& img,
124             unsigned long border_size = 6
125         );
126         /*!
127             requires
128                 - image_type == an implementation of array2d/array2d_kernel_abstract.h
129                 - pixel_traits<typename image_type::type> is defined
130             ensures
131                 - displays image img in the middle of the button
132                 - the distance between the edge of the button and the image
133                   will be border_size pixels
134         !*/
135     };
136 
137 // ----------------------------------------------------------------------------------------
138 
139     class button_style_arrow : public button_style
140     {
141     public:
142         /*!
143             This draws a simple button with an arrow in it
144         !*/
145 
146         enum arrow_direction
147         {
148             UP,
149             DOWN,
150             LEFT,
151             RIGHT
152         };
153 
154         button_style_arrow (
155             arrow_direction dir
156         );
157         /*!
158             ensures
159                 - the arrow in the button will point in the given direction
160         !*/
161     };
162 
163 // ----------------------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------------------
165     // toggle button styles
166 // ----------------------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------------------
168 
169     class toggle_button_style
170     {
171         /*!
172             WHAT THIS OBJECT REPRESENTS
173                 This is an abstract class that defines the interface a
174                 toggle button style object must implement.
175 
176                 Note that derived classes must be copyable via
177                 their copy constructors.
178         !*/
179 
180     public:
181 
~toggle_button_style()182         virtual ~toggle_button_style() {}
183 
redraw_on_mouse_over()184         virtual bool redraw_on_mouse_over (
185         ) const { return false; }
186         /*!
187             ensures
188                 - if (this style draws buttons differently when a mouse is over them) then
189                     - returns true
190                 - else
191                     - returns false
192         !*/
193 
194         virtual rectangle get_min_size (
195             const ustring& name,
196             const font& mfont
197         ) const = 0;
198         /*!
199             requires
200                 - the mutex drawable::m is locked
201             ensures
202                 - returns a rectangle that represents the minimum size of the button
203                   given the name and font.
204         !*/
205 
206         virtual void draw_toggle_button (
207             const canvas& c,
208             const rectangle& rect,
209             const bool enabled,
210             const font& mfont,
211             const long lastx,
212             const long lasty,
213             const ustring& name,
214             const bool is_depressed,
215             const bool is_checked
216         ) const = 0;
217         /*!
218             requires
219                 - the mutex drawable::m is locked
220                 - c == the canvas to draw on
221                 - rect, enabled, mfont, lastx, and lasty are the variables
222                   defined in the protected section of the drawable class.
223                 - name == the name of the button to be drawn
224                 - is_depressed == true if the button is to be drawn in a depressed state
225                 - is_checked == true if the toggle_button is in the checked state
226             ensures
227                 - draws the button on the canvas c at the location given by rect.
228         !*/
229     };
230 
231 // ----------------------------------------------------------------------------------------
232 
233     class toggle_button_style_default : public toggle_button_style
234     {
235         /*!
236             This is the default style for toggle_button objects.  It will cause
237             a button to appear as the simple MS Windows 2000 button style.
238         !*/
239     };
240 
241 // ----------------------------------------------------------------------------------------
242 
243     class toggle_button_style_check_box : public toggle_button_style
244     {
245         /*!
246             This draws a simple check box style toggle button that displays its
247             name to the right of a check box.
248         !*/
249     };
250 
251 // ----------------------------------------------------------------------------------------
252 
253     class toggle_button_style_radio_button : public toggle_button_style
254     {
255         /*!
256             This draws a simple radio button style toggle button that displays its
257             name to the right of a circular radio button.
258         !*/
259     };
260 
261 // ----------------------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------------------
263     // scroll_bar styles
264 // ----------------------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------------------
266 
267     class scroll_bar_style
268     {
269         /*!
270             WHAT THIS OBJECT REPRESENTS
271                 This is an abstract class that defines the interface a
272                 scroll_bar style object must implement.
273 
274                 Note that derived classes must be copyable via
275                 their copy constructors.
276 
277                 There are three parts of a scroll bar, the slider, the background,
278                 and the two buttons on its ends.  The "slider" is the thing that you
279                 drag around on the scroll bar and the "background" is the part
280                 in between the slider and the buttons on the ends.
281         !*/
282 
283     public:
284 
~scroll_bar_style()285         virtual ~scroll_bar_style() {}
286 
redraw_on_mouse_over_slider()287         virtual bool redraw_on_mouse_over_slider (
288         ) const { return false; }
289         /*!
290             ensures
291                 - if (this style draws a scroll_bar's slider differently when a mouse is over it
292                   or it is being dragged) then
293                     - returns true
294                 - else
295                     - returns false
296         !*/
297 
298         virtual long get_width (
299         ) const = 0;
300         /*!
301             requires
302                 - the mutex drawable::m is locked
303             ensures
304                 - returns the width in pixels of the scroll bar
305         !*/
306 
307         virtual long get_slider_length (
308             long total_length,
309             long max_pos
310         ) const = 0;
311         /*!
312             requires
313                 - the mutex drawable::m is locked
314                 - total_length == the total length in pixels of the scroll bar
315                 - max_pos == the value of scroll_bar::max_slider_pos() for this
316                   scroll bar
317             ensures
318                 - returns the length in pixels of the scroll bar's slider
319         !*/
320 
321         virtual long get_button_length (
322             long total_length,
323             long max_pos
324         ) const = 0;
325         /*!
326             requires
327                 - the mutex drawable::m is locked
328                 - total_length == the total length in pixels of the scroll bar
329                 - max_pos == the value of scroll_bar::max_slider_pos() for this
330                   scroll bar
331             ensures
332                 - returns the length in pixels of each of the scroll bar's
333                   buttons
334         !*/
335 
336         virtual void draw_scroll_bar_background (
337             const canvas& c,
338             const rectangle& rect,
339             const bool enabled,
340             const long lastx,
341             const long lasty,
342             const bool is_depressed
343         ) const = 0;
344         /*!
345             requires
346                 - the mutex drawable::m is locked
347                 - c == the canvas to draw on
348                 - rect, enabled, lastx, and lasty are the variables
349                   defined in the protected section of the drawable class.
350                 - is_depressed == true if the background area of the scroll_bar is to
351                   be drawn in a depressed state (because the user is clicking on it)
352             ensures
353                 - draws the background part of the scroll_bar on the canvas c at the
354                   location given by rect.
355         !*/
356 
357         virtual void draw_scroll_bar_slider (
358             const canvas& c,
359             const rectangle& rect,
360             const bool enabled,
361             const long lastx,
362             const long lasty,
363             const bool is_being_dragged
364         ) const = 0;
365         /*!
366             requires
367                 - the mutex drawable::m is locked
368                 - c == the canvas to draw on
369                 - rect, enabled, lastx, and lasty are the variables
370                   defined in the protected section of the drawable class
371                 - is_being_dragged == true if the user is dragging the slider
372             ensures
373                 - draws the slider part of the scroll_bar on the canvas c at the
374                   location given by rect.
375         !*/
376 
377         button_style_type get_up_button_style (
378         ) const;
379         /*!
380             ensures
381                 - returns the type of button_style to use for a button on the
382                   top side of a vertical scroll bar.
383         !*/
384 
385         button_style_type get_down_button_style (
386         ) const;
387         /*!
388             ensures
389                 - returns the type of button_style to use for a button on the
390                   bottom side of a vertical scroll bar.
391         !*/
392 
393         button_style_type get_left_button_style (
394         ) const;
395         /*!
396             ensures
397                 - returns the type of button_style to use for a button on the
398                   left side of a horizontal scroll bar.
399         !*/
400 
401         button_style_type get_right_button_style (
402         ) const;
403         /*!
404             ensures
405                 - returns the type of button_style to use for a button on the
406                   right side of a horizontal scroll bar.
407         !*/
408     };
409 
410 // ----------------------------------------------------------------------------------------
411 
412     class scroll_bar_style_default : public scroll_bar_style
413     {
414         /*!
415             This is the default style for scroll_bar objects.  It will cause
416             a scroll_bar to appear as the simple MS Windows 2000 scroll_bar style.
417         !*/
418     };
419 
420 // ----------------------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------------------
422     // scrollable_region (and zoomable_region) styles
423 // ----------------------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------------------
425 
426     class scrollable_region_style
427     {
428         /*!
429             WHAT THIS OBJECT REPRESENTS
430                 This is an abstract class that defines the interface a
431                 scrollable_region and zoomable_region style object must implement.
432 
433                 Note that derived classes must be copyable via
434                 their copy constructors.
435         !*/
436     public:
437 
~scrollable_region_style()438         virtual ~scrollable_region_style() {}
439 
440         virtual long get_border_size (
441         ) const = 0;
442         /*!
443             requires
444                 - the mutex drawable::m is locked
445             ensures
446                 - returns the size of the border region in pixels
447         !*/
448 
449         virtual void draw_scrollable_region_border (
450             const canvas& c,
451             const rectangle& rect,
452             const bool enabled
453         ) const = 0;
454         /*!
455             requires
456                 - the mutex drawable::m is locked
457                 - c == the canvas to draw on
458                 - rect and enabled are the variables defined in the protected section
459                   of the drawable class.
460             ensures
461                 - draws the border part of a scrollable_region on the canvas c at the
462                   location given by rect.
463         !*/
464 
465         scroll_bar_style_type get_horizontal_scroll_bar_style (
466         ) const;
467         /*!
468             ensures
469                 - returns the style of scroll_bar to use for the
470                   horizontal scroll_bar in this widget.
471         !*/
472 
473         scroll_bar_style_type get_vertical_scroll_bar_style (
474         ) const;
475         /*!
476             ensures
477                 - returns the style of scroll_bar to use for the
478                   vertical scroll_bar in this widget.
479         !*/
480     };
481 
482 // ----------------------------------------------------------------------------------------
483 
484     class scrollable_region_style_default : public scrollable_region_style
485     {
486     public:
487         /*!
488             This is the default style for scrollable_region and zoomable_region objects.
489         !*/
490     };
491 
492 // ----------------------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------------------
494     // text_box styles
495 // ----------------------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------------------
497 
498     class text_box_style
499     {
500         /*!
501             WHAT THIS OBJECT REPRESENTS
502                 This is an abstract class that defines the interface a
503                 text_box style object must implement.
504 
505                 Note that derived classes must be copyable via
506                 their copy constructors.
507         !*/
508     public:
509 
~text_field_style()510         virtual ~text_field_style() {}
511 
512         scrollable_region_style_type get_scrollable_region_style (
513         ) const;
514         /*!
515             ensures
516                 - returns the style of scrollable_region to use for the
517                   text_box.
518         !*/
519 
520         virtual unsigned long get_padding (
521             const font& mfont
522         ) const = 0;
523         /*!
524             requires
525                 - the mutex drawable::m is locked
526             ensures
527                 - returns the number of pixels that separate the text in the text_box
528                   from the edge of the text_box widget itself.
529         !*/
530 
531         virtual void draw_text_box (
532             const canvas& c,
533             const rectangle& display_rect,
534             const rectangle& text_rect,
535             const bool enabled,
536             const font& mfont,
537             const ustring& text,
538             const rectangle& cursor_rect,
539             const rgb_pixel& text_color,
540             const rgb_pixel& bg_color,
541             const bool has_focus,
542             const bool cursor_visible,
543             const long highlight_start,
544             const long highlight_end
545         ) const = 0;
546         /*!
547             requires
548                 - the mutex drawable::m is locked
549                 - c == the canvas to draw on
550                 - enabled and mfont are the variables defined in the protected section
551                 - text_rect == the rectangle in which we should draw the given text
552                   of the drawable class.
553                 - display_rect == the rectangle returned by scrollable_region::display_rect()
554                 - text == the current text in the text_box
555                 - cursor_rect == A rectangle of width 1 that represents the current
556                   position of the cursor on the screen.
557                 - text_color == the color of the text to be drawn
558                 - bg_color == the background color of the text field
559                 - has_focus == true if this text field has keyboard input focus
560                 - cursor_visible == true if the cursor should be drawn
561                 - if (highlight_start <= highlight_end) then
562                     - text[highlight_start] though text[highlight_end] should be
563                       highlighted
564             ensures
565                 - draws the text_box on the canvas c at the location given by text_rect.
566                   (Note that the scroll bars and borders are drawn by the scrollable_region
567                   and therefore the style returned by get_scrollable_region_style()
568                   controls how those appear)
569                 - doesn't draw anything outside display_rect
570         !*/
571     };
572 
573 // ----------------------------------------------------------------------------------------
574 
575     class text_box_style_default : public text_box_style
576     {
577     public:
578         /*!
579             This is the default style for text_box objects.
580         !*/
581     };
582 
583 // ----------------------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------------------
585     // list_box styles
586 // ----------------------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------------------
588 
589     class list_box_style
590     {
591         /*!
592             WHAT THIS OBJECT REPRESENTS
593                 This is an abstract class that defines the interface a
594                 list_box style object must implement.
595 
596                 Note that derived classes must be copyable via
597                 their copy constructors.
598         !*/
599     public:
600 
~list_box_style()601         virtual ~list_box_style() {}
602 
603         virtual void draw_list_box_background (
604             const canvas& c,
605             const rectangle& display_rect,
606             const bool enabled
607         ) const = 0;
608         /*!
609             requires
610                 - the mutex drawable::m is locked
611                 - c == the canvas to draw on
612                 - display_rect == the display_rect for the list_box.  This is the area
613                   in which list box items are drawn (see display_rect in the scrollable_region
614                   widget for more info)
615                 - enabled == true if the list box is enabled
616             ensures
617                 - draws the background of a list box on the canvas c at the location given
618                   by display_rect.
619         !*/
620 
621         scrollable_region_style_type get_scrollable_region_style (
622         ) const;
623         /*!
624             ensures
625                 - returns the style of scrollable_region to use for the
626                   list_box.
627         !*/
628 
629         virtual void draw_list_box_item (
630             const canvas& c,
631             const rectangle& rect,
632             const rectangle& display_rect,
633             const bool enabled,
634             const font& mfont,
635             const std::string& text,
636             const bool is_selected
637         ) const = 0;
638         /*!
639             requires
640                 - the mutex drawable::m is locked
641                 - c == the canvas to draw on
642                 - rect == the rectangle that defines where on the screen this list box item is.
643                 - display_rect == the display_rect for the list_box.  This is the area
644                   in which list box items are drawn (see display_rect in the scrollable_region
645                   widget for more info)
646                 - mfont == the font to use to draw the list box item
647                 - text == the text of the list box item to be drawn
648                 - enabled == true if the list box is enabled
649                 - is_selected == true if the item is to be drawn in a selected state
650             ensures
651                 - draws the list box item on the canvas c at the location given by rect.
652         !*/
653 
654         // wide character overloads
655         virtual void draw_list_box_item (
656             const canvas& c,
657             const rectangle& rect,
658             const rectangle& display_rect,
659             const bool enabled,
660             const font& mfont,
661             const std::wstring& text,
662             const bool is_selected
663         ) const = 0;
664 
665         virtual void draw_list_box_item (
666             const canvas& c,
667             const rectangle& rect,
668             const rectangle& display_rect,
669             const bool enabled,
670             const font& mfont,
671             const ustring& text,
672             const bool is_selected
673         ) const = 0;
674 
675     };
676 
677 // ----------------------------------------------------------------------------------------
678 
679     class list_box_style_default : public list_box_style
680     {
681     public:
682         /*!
683             This is the default style for list_box objects.
684         !*/
685     };
686 
687 // ----------------------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------------------
689     // text_field styles
690 // ----------------------------------------------------------------------------------------
691 // ----------------------------------------------------------------------------------------
692 
693     class text_field_style
694     {
695         /*!
696             WHAT THIS OBJECT REPRESENTS
697                 This is an abstract class that defines the interface a
698                 text_field style object must implement.
699 
700                 Note that derived classes must be copyable via
701                 their copy constructors.
702         !*/
703     public:
704 
~text_field_style()705         virtual ~text_field_style() {}
706 
707         virtual unsigned long get_padding (
708             const font& mfont
709         ) const = 0;
710         /*!
711             requires
712                 - the mutex drawable::m is locked
713             ensures
714                 - returns the number of pixels that separate the text in the text_field
715                   from the edge of the text_field widget itself.
716         !*/
717 
718         virtual void draw_text_field (
719             const canvas& c,
720             const rectangle& rect,
721             const rectangle& text_rect,
722             const bool enabled,
723             const font& mfont,
724             const ustring& text,
725             const unsigned long cursor_x,
726             const unsigned long text_pos,
727             const rgb_pixel& text_color,
728             const rgb_pixel& bg_color,
729             const bool has_focus,
730             const bool cursor_visible,
731             const long highlight_start,
732             const long highlight_end
733         ) const = 0;
734         /*!
735             requires
736                 - the mutex drawable::m is locked
737                 - c == the canvas to draw on
738                 - rect, enabled, and mfont are the variables defined in the protected section
739                   of the drawable class.
740                 - text == the current text in the text_field
741                 - text_rect == the rectangle in which we should draw the given text
742                 - cursor_x == the x coordinate of the cursor relative to the left side
743                   of rect.  i.e. the number of pixels that separate the cursor from the
744                   left side of the text_field.
745                 - text_pos == the index of the first letter in text that appears in
746                   this text field.
747                 - text_color == the color of the text to be drawn
748                 - bg_color == the background color of the text field
749                 - has_focus == true if this text field has keyboard input focus
750                 - cursor_visible == true if the cursor should be drawn
751                 - if (highlight_start <= highlight_end) then
752                     - text[highlight_start] though text[highlight_end] should be
753                       highlighted
754             ensures
755                 - draws the text_field on the canvas c at the location given by rect.
756         !*/
757 
758     };
759 
760 // ----------------------------------------------------------------------------------------
761 
762     class text_field_style_default : public text_field_style
763     {
764     public:
765         /*!
766             This is the default style for text_field objects.
767         !*/
768     };
769 
770 // ----------------------------------------------------------------------------------------
771 
772 }
773 
774 #endif // DLIB_WIDGETs_STYLE_ABSTRACT_
775 
776 
777 
778