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