1 /* 2 * Copyright 2002-2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Mattias Sundblad 7 * Andrew Bachmann 8 */ 9 10 11 #include "Constants.h" 12 #include "StyledEditApp.h" 13 #include "StyledEditWindow.h" 14 15 #include <Alert.h> 16 #include <Autolock.h> 17 #include <MenuBar.h> 18 #include <CharacterSet.h> 19 #include <CharacterSetRoster.h> 20 #include <FilePanel.h> 21 #include <MenuItem.h> 22 #include <Message.h> 23 #include <Path.h> 24 #include <Screen.h> 25 26 #include <stdio.h> 27 28 29 using namespace BPrivate; 30 31 32 StyledEditApp * styled_edit_app; 33 BRect gWindowRect(7-15, 26-15, 507, 426); 34 35 36 namespace 37 { 38 void 39 cascade() 40 { 41 BScreen screen(NULL); 42 BRect screenBorder = screen.Frame(); 43 float left = gWindowRect.left + 15; 44 if (left + gWindowRect.Width() > screenBorder.right) 45 left = 7; 46 47 float top = gWindowRect.top + 15; 48 if (top + gWindowRect.Height() > screenBorder.bottom) 49 top = 26; 50 51 gWindowRect.OffsetTo(BPoint(left,top)); 52 } 53 54 55 void 56 uncascade() 57 { 58 BScreen screen(NULL); 59 BRect screenBorder = screen.Frame(); 60 61 float left = gWindowRect.left - 15; 62 if (left < 7) { 63 left = screenBorder.right - gWindowRect.Width() - 7; 64 left = left - ((int)left % 15) + 7; 65 } 66 67 float top = gWindowRect.top - 15; 68 if (top < 26) { 69 top = screenBorder.bottom - gWindowRect.Height() - 26; 70 top = top - ((int)left % 15) + 26; 71 } 72 73 gWindowRect.OffsetTo(BPoint(left,top)); 74 } 75 } 76 77 78 // #pragma mark - 79 80 81 StyledEditApp::StyledEditApp() 82 : BApplication(APP_SIGNATURE), 83 fOpenPanel(NULL) 84 { 85 fOpenPanel = new BFilePanel(); 86 BMenuBar *menuBar = 87 dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar")); 88 89 fOpenAsEncoding = 0; 90 fOpenPanelEncodingMenu= new BMenu("Encoding"); 91 menuBar->AddItem(fOpenPanelEncodingMenu); 92 fOpenPanelEncodingMenu->SetRadioMode(true); 93 94 BCharacterSetRoster roster; 95 BCharacterSet charset; 96 while (roster.GetNextCharacterSet(&charset) == B_NO_ERROR) { 97 BString name; 98 if (charset.GetFontID() == B_UNICODE_UTF8) 99 name = "Default"; 100 else 101 name = charset.GetPrintName(); 102 103 const char* mime = charset.GetMIMEName(); 104 if (mime) { 105 name.Append(" ("); 106 name.Append(mime); 107 name.Append(")"); 108 } 109 BMenuItem* item = new BMenuItem(name.String(), new BMessage(OPEN_AS_ENCODING)); 110 item->SetTarget(this); 111 fOpenPanelEncodingMenu->AddItem(item); 112 if (charset.GetFontID() == fOpenAsEncoding) 113 item->SetMarked(true); 114 } 115 116 fWindowCount = 0; 117 fNextUntitledWindow = 1; 118 119 styled_edit_app = this; 120 } 121 122 123 StyledEditApp::~StyledEditApp() 124 { 125 delete fOpenPanel; 126 } 127 128 129 void 130 StyledEditApp::DispatchMessage(BMessage *msg, BHandler *handler) 131 { 132 if (msg->what == B_ARGV_RECEIVED) { 133 int32 argc; 134 if (msg->FindInt32("argc", &argc) != B_OK) 135 argc = 0; 136 137 const char** argv = new const char*[argc]; 138 for (int arg = 0; arg < argc; arg++) { 139 if (msg->FindString("argv", arg, &argv[arg]) != B_OK) { 140 argv[arg] = ""; 141 } 142 } 143 const char* cwd; 144 if (msg->FindString("cwd", &cwd) != B_OK) 145 cwd = ""; 146 147 ArgvReceivedEx(argc, argv, cwd); 148 } else 149 BApplication::DispatchMessage(msg, handler); 150 } 151 152 153 void 154 StyledEditApp::MessageReceived(BMessage *message) 155 { 156 switch (message->what) { 157 case MENU_NEW: 158 OpenDocument(); 159 break; 160 case MENU_OPEN: 161 fOpenPanel->Show(); 162 break; 163 case B_SILENT_RELAUNCH: 164 OpenDocument(); 165 break; 166 case OPEN_AS_ENCODING: 167 void* ptr; 168 if (message->FindPointer("source", &ptr) == B_OK 169 && fOpenPanelEncodingMenu != 0) { 170 fOpenAsEncoding = (uint32)fOpenPanelEncodingMenu->IndexOf((BMenuItem*)ptr); 171 } 172 break; 173 174 default: 175 BApplication::MessageReceived(message); 176 break; 177 } 178 } 179 180 181 void 182 StyledEditApp::OpenDocument() 183 { 184 cascade(); 185 new StyledEditWindow(gWindowRect, fNextUntitledWindow++, fOpenAsEncoding); 186 fWindowCount++; 187 } 188 189 190 void 191 StyledEditApp::OpenDocument(entry_ref* ref) 192 { 193 cascade(); 194 new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding); 195 fWindowCount++; 196 } 197 198 199 void 200 StyledEditApp::CloseDocument() 201 { 202 uncascade(); 203 fWindowCount--; 204 if (fWindowCount == 0) { 205 BAutolock lock(this); 206 Quit(); 207 } 208 } 209 210 211 void 212 StyledEditApp::RefsReceived(BMessage *message) 213 { 214 int32 index = 0; 215 entry_ref ref; 216 217 while (message->FindRef("refs", index++, &ref) == B_OK) { 218 OpenDocument(&ref); 219 } 220 } 221 222 223 void 224 StyledEditApp::ArgvReceivedEx(int32 argc, const char* argv[], const char* cwd) 225 { 226 for (int i = 1 ; (i < argc) ; i++) { 227 BPath path; 228 if (argv[i][0] == '/') 229 path.SetTo(argv[i]); 230 else 231 path.SetTo(cwd, argv[i]); 232 233 if (path.InitCheck() != B_OK) { 234 fprintf(stderr, "Setting path failed: \""); 235 if (argv[i][0] == '/') 236 fprintf(stderr, "%s\".\n", argv[i]); 237 else 238 fprintf(stderr, "%s/%s\".\n", cwd, argv[i]); 239 continue; 240 } 241 242 entry_ref ref; 243 status_t status = get_ref_for_path(path.Path(), &ref); 244 if (status != B_OK) { 245 fprintf(stderr, "Could not open \"%s\": %s.\n", path.Path(), strerror(status)); 246 continue; 247 } 248 249 OpenDocument(&ref); 250 } 251 } 252 253 254 void 255 StyledEditApp::ReadyToRun() 256 { 257 if (fWindowCount == 0) 258 OpenDocument(); 259 } 260 261 262 int32 263 StyledEditApp::NumberOfWindows() 264 { 265 return fWindowCount; 266 } 267 268 269 // #pragma mark - 270 271 272 int 273 main(int argc, char** argv) 274 { 275 StyledEditApp styledEdit; 276 styledEdit.Run(); 277 return 0; 278 } 279 280