| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <Kernel/KeyCode.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-18 10:15:00 +02:00
										 |  |  | #include <LibDraw/CharacterBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | #include <LibDraw/Palette.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-18 10:15:00 +02:00
										 |  |  | #include <LibDraw/StylePainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | #include <LibGUI/GCheckBox.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | static const char* s_checked_bitmap_data = { | 
					
						
							|  |  |  |     "         " | 
					
						
							| 
									
										
										
										
											2019-05-10 22:50:42 +02:00
										 |  |  |     "       # " | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     "      ## " | 
					
						
							| 
									
										
										
										
											2019-05-10 22:50:42 +02:00
										 |  |  |     "     ### " | 
					
						
							|  |  |  |     " ## ###  " | 
					
						
							|  |  |  |     " #####   " | 
					
						
							|  |  |  |     "  ###    " | 
					
						
							|  |  |  |     "   #     " | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     "         " | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static CharacterBitmap* s_checked_bitmap; | 
					
						
							|  |  |  | static const int s_checked_bitmap_width = 9; | 
					
						
							|  |  |  | static const int s_checked_bitmap_height = 9; | 
					
						
							| 
									
										
										
										
											2019-04-10 03:44:23 +02:00
										 |  |  | static const int s_box_width = 13; | 
					
						
							|  |  |  | static const int s_box_height = 13; | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GCheckBox::GCheckBox(GWidget* parent) | 
					
						
							| 
									
										
										
										
											2019-05-24 17:11:42 +02:00
										 |  |  |     : GAbstractButton(parent) | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-24 22:47:01 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 14:58:02 +02:00
										 |  |  | GCheckBox::GCheckBox(const StringView& text, GWidget* parent) | 
					
						
							| 
									
										
										
										
											2019-05-24 22:47:01 +02:00
										 |  |  |     : GAbstractButton(text, parent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | GCheckBox::~GCheckBox() | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 19:34:55 +01:00
										 |  |  | void GCheckBox::paint_event(GPaintEvent& event) | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-28 17:19:56 +01:00
										 |  |  |     GPainter painter(*this); | 
					
						
							| 
									
										
										
										
											2019-03-29 15:01:54 +01:00
										 |  |  |     painter.add_clip_rect(event.rect()); | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     auto text_rect = rect(); | 
					
						
							|  |  |  |     text_rect.set_left(s_box_width + 4); | 
					
						
							| 
									
										
										
										
											2019-05-24 17:11:42 +02:00
										 |  |  |     text_rect.set_width(font().width(text())); | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     text_rect.set_top(height() / 2 - font().glyph_height() / 2); | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  |     text_rect.set_height(font().glyph_height()); | 
					
						
							| 
									
										
										
										
											2018-10-12 17:00:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     if (fill_with_background_color()) | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.fill_rect(rect(), palette().window()); | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |     Rect box_rect { | 
					
						
							| 
									
										
										
										
											2019-04-10 03:44:23 +02:00
										 |  |  |         0, height() / 2 - s_box_height / 2 - 1, | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  |         s_box_width, s_box_height | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     painter.fill_rect(box_rect, palette().base()); | 
					
						
							|  |  |  |     StylePainter::paint_frame(painter, box_rect, palette(), FrameShape::Container, FrameShadow::Sunken, 2); | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 17:11:42 +02:00
										 |  |  |     if (is_being_pressed()) | 
					
						
							| 
									
										
										
										
											2019-04-10 03:44:23 +02:00
										 |  |  |         painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray); | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:47:01 +02:00
										 |  |  |     if (is_checked()) { | 
					
						
							|  |  |  |         if (!s_checked_bitmap) | 
					
						
							|  |  |  |             s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref(); | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |         painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, palette().button_text()); | 
					
						
							| 
									
										
										
										
											2019-05-24 22:47:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 22:54:37 +02:00
										 |  |  |     paint_text(painter, text_rect, font(), TextAlignment::TopLeft); | 
					
						
							| 
									
										
										
										
											2018-10-12 14:15:14 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 17:11:42 +02:00
										 |  |  | void GCheckBox::click() | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-24 17:11:42 +02:00
										 |  |  |     if (!is_enabled()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     set_checked(!is_checked()); | 
					
						
							| 
									
										
										
										
											2019-01-27 20:22:06 +01:00
										 |  |  | } |