1 /* 2 * Copyright 2001-2007, Haiku, Inc. 3 * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net 4 * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai. 5 * All rights reserved. Distributed under the terms of the MIT license. 6 */ 7 8 9 #include "AppearPrefView.h" 10 #include "MenuUtil.h" 11 #include "PrefHandler.h" 12 #include "TermConst.h" 13 14 #include <View.h> 15 #include <Button.h> 16 #include <MenuField.h> 17 #include <Menu.h> 18 #include <MenuItem.h> 19 #include <PopUpMenu.h> 20 21 #include <stdlib.h> 22 23 24 AppearancePrefView::AppearancePrefView(BRect frame, const char *name, 25 BMessenger messenger) 26 : PrefView(frame, name), 27 fAppearancePrefViewMessenger(messenger) 28 { 29 const char *color_tbl[] = { 30 PREF_TEXT_FORE_COLOR, 31 PREF_TEXT_BACK_COLOR, 32 PREF_CURSOR_FORE_COLOR, 33 PREF_CURSOR_BACK_COLOR, 34 PREF_SELECT_FORE_COLOR, 35 PREF_SELECT_BACK_COLOR, 36 #if 0 37 "", 38 PREF_IM_FORE_COLOR, 39 PREF_IM_BACK_COLOR, 40 PREF_IM_SELECT_COLOR, 41 "", 42 PREF_ANSI_BLACK_COLOR, 43 PREF_ANSI_RED_COLOR, 44 PREF_ANSI_GREEN_COLOR, 45 PREF_ANSI_YELLOW_COLOR, 46 PREF_ANSI_BLUE_COLOR, 47 PREF_ANSI_MAGENTA_COLOR, 48 PREF_ANSI_CYAN_COLOR, 49 PREF_ANSI_WHITE_COLOR, 50 #endif 51 NULL 52 }; 53 54 float fontDividerSize = StringWidth("Font:") + 8.0; 55 float sizeDividerSize = StringWidth("Size:") + 8.0; 56 57 BRect r(5, 5, 225, 25); 58 59 BMenu *menu = _MakeFontMenu(MSG_HALF_FONT_CHANGED, 60 PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY)); 61 fFont = new BMenuField(r, "font", "Font:", menu); 62 fFont->SetDivider(fontDividerSize); 63 AddChild(fFont); 64 65 r.OffsetBy(r.Width() + 10, 0); 66 menu = _MakeSizeMenu(MSG_HALF_SIZE_CHANGED, 67 PrefHandler::Default()->getInt32(PREF_HALF_FONT_SIZE)); 68 fFontSize = new BMenuField(r, "size", "Size:", menu); 69 fFontSize->SetDivider(sizeDividerSize); 70 AddChild(fFontSize); 71 72 r.OffsetBy(-r.Width() - 10,r.Height() + 25); 73 fColorField = new BMenuField(r, "color", "Change:", 74 MakeMenu(MSG_COLOR_FIELD_CHANGED, color_tbl, color_tbl[0])); 75 fColorField->SetDivider(StringWidth(fColorField->Label()) + 8.0); 76 AddChild(fColorField); 77 78 fColorControl = SetupColorControl(BPoint(r.left, r.bottom + 10), 79 B_CELLS_32x8, 7.0, MSG_COLOR_CHANGED); 80 fColorControl->SetValue(PrefHandler::Default()->getRGB(PREF_TEXT_FORE_COLOR)); 81 } 82 83 84 void 85 AppearancePrefView::GetPreferredSize(float *_width, float *_height) 86 { 87 if (_width) 88 *_width = Bounds().Width(); 89 90 if (*_height) 91 *_height = fColorControl->Frame().bottom; 92 } 93 94 95 void 96 AppearancePrefView::Revert() 97 { 98 fColorField->Menu()->ItemAt(0)->SetMarked(true); 99 fColorControl->SetValue(PrefHandler::Default()->getRGB(PREF_TEXT_FORE_COLOR)); 100 101 fFont->Menu()->FindItem(PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY))->SetMarked(true); 102 fFontSize->Menu()->FindItem(PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY))->SetMarked(true); 103 } 104 105 106 void 107 AppearancePrefView::AttachedToWindow() 108 { 109 fFontSize->Menu()->SetTargetForItems(this); 110 fFont->Menu()->SetTargetForItems(this); 111 112 fColorControl->SetTarget(this); 113 fColorField->Menu()->SetTargetForItems(this); 114 } 115 116 117 void 118 AppearancePrefView::MessageReceived(BMessage *msg) 119 { 120 bool modified = false; 121 122 switch (msg->what) { 123 case MSG_HALF_FONT_CHANGED: 124 PrefHandler::Default()->setString(PREF_HALF_FONT_FAMILY, 125 fFont->Menu()->FindMarked()->Label()); 126 modified = true; 127 break; 128 129 case MSG_HALF_SIZE_CHANGED: 130 PrefHandler::Default()->setString(PREF_HALF_FONT_SIZE, 131 fFontSize->Menu()->FindMarked()->Label()); 132 modified = true; 133 break; 134 135 case MSG_COLOR_CHANGED: 136 PrefHandler::Default()->setRGB(fColorField->Menu()->FindMarked()->Label(), 137 fColorControl->ValueAsColor()); 138 modified = true; 139 break; 140 141 case MSG_COLOR_FIELD_CHANGED: 142 fColorControl->SetValue(PrefHandler::Default()->getRGB( 143 fColorField->Menu()->FindMarked()->Label())); 144 break; 145 146 default: 147 PrefView::MessageReceived(msg); 148 return; 149 } 150 151 if (modified) { 152 fAppearancePrefViewMessenger.SendMessage(msg); 153 154 BMessenger messenger(this); 155 messenger.SendMessage(MSG_PREF_MODIFIED); 156 } 157 } 158 159 160 BMenu * 161 AppearancePrefView::_MakeFontMenu(uint32 command, const char *defaultFontName) 162 { 163 BPopUpMenu *menu = new BPopUpMenu(""); 164 int32 numFamilies = count_font_families(); 165 166 for (int32 i = 0; i < numFamilies; i++) { 167 font_family family; 168 uint32 flags; 169 170 if (get_font_family(i, &family, &flags) == B_OK) { 171 BFont font; 172 font.SetFamilyAndStyle(family, NULL); 173 if (font.IsFixed()) { 174 BMenuItem *item = new BMenuItem(family, new BMessage(command)); 175 menu->AddItem(item); 176 if (!strcmp(defaultFontName, family)) 177 item->SetMarked(true); 178 } 179 } 180 } 181 182 return menu; 183 } 184 185 186 BMenu * 187 AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize) 188 { 189 BPopUpMenu *menu = new BPopUpMenu("size"); 190 int32 sizes[] = {9, 10, 11, 12, 14, 16, 18, 0}; 191 192 for (uint32 i = 0; sizes[i]; i++) { 193 BString string; 194 string << sizes[i]; 195 196 BMenuItem* item = new BMenuItem(string.String(), new BMessage(command)); 197 menu->AddItem(item); 198 199 if (sizes[i] == defaultSize) 200 item->SetMarked(true); 201 } 202 203 return menu; 204 } 205